Skip to main content

Multi-dimensional gene set scoring and visualization for single-cell transcriptomics

Project description

multiscoresplot

CI PyPI License: MIT Python

Multi-dimensional gene set scoring and visualization for single-cell transcriptomics.

Color dimensionality reduction plots (UMAP, PCA, etc.) using a multi-dimensional color space derived from gene set scores — so you can visualize the activity of multiple gene programs simultaneously in a single plot.

Installation

pip install multiscoresplot

For interactive Plotly plots:

pip install 'multiscoresplot[interactive]'

Quick Start

import multiscoresplot as msp

# Define gene sets of interest
gene_sets = {
    "qNSCs": ["Id3", "Aldoc", "Slc1a3", ...],
    "aNSCs": ["Egfr", "Ascl1", "Mki67", ...],
    "TAP":   ["Dll1", "Dcx", "Neurod1", ...],
    "NB":    ["Dcx", "Sox11", "Tubb3", ...],
}

# 1. Score gene sets per cell
scores = msp.score_gene_sets(adata, gene_sets, inplace=True)

# 2. Map scores to RGB colors
rgb = msp.reduce_to_rgb(scores, method="pca")

# 3. Plot
msp.plot_embedding(
    adata, rgb,
    basis="umap",
    method="pca",
    gene_set_names=list(gene_sets.keys()),
)

Pipeline

multiscoresplot follows a 5-step pipeline:

Step 1 — Score gene sets

Calculate per-cell gene set scores using pyUCell. Scores are stored in adata.obs as score-<name> columns with values in [0, 1].

scores = msp.score_gene_sets(adata, gene_sets, inplace=True)

# Options
scores = msp.score_gene_sets(
    adata, gene_sets,
    max_rank=1500,    # rank cap (tune to median genes per cell)
    chunk_size=1000,  # cells per batch
    n_jobs=-1,        # parallelism (-1 = all cores)
    inplace=False,    # don't store in adata.obs
)

Step 2 — Blend to RGB (2–3 gene sets)

For 2–3 gene sets, use multiplicative blending from white. Each gene set darkens toward its base color proportional to the score.

# Default colors (2 sets: blue/red, 3 sets: R/G/B)
rgb = msp.blend_to_rgb(scores)

# Custom colors
rgb = msp.blend_to_rgb(scores, colors=[(1, 0, 0), (0, 0.5, 1)])

Step 3 — Reduce to RGB (2+ gene sets)

For any number of gene sets (2 or more), use dimensionality reduction to map scores to 3 RGB channels. Built-in methods: PCA, NMF, and ICA.

rgb = msp.reduce_to_rgb(scores, method="pca")  # default
rgb = msp.reduce_to_rgb(scores, method="nmf")
rgb = msp.reduce_to_rgb(scores, method="ica")

Step 4 — Plot embedding

Scatter plot of embedding coordinates colored by RGB values, with an integrated color-space legend.

# Static matplotlib plot
msp.plot_embedding(
    adata, rgb,
    basis="umap",
    method="pca",
    gene_set_names=["qNSCs", "aNSCs", "TAP", "NB"],
)

# Options
ax = msp.plot_embedding(
    adata, rgb,
    basis="umap",
    method="pca",
    gene_set_names=["qNSCs", "aNSCs", "TAP", "NB"],
    legend=True,              # show color legend (default)
    legend_style="inset",     # "inset" or "side"
    legend_loc="lower right", # legend position
    point_size=3,
    alpha=0.8,
    figsize=(6, 6),
    title="SVZ lineage",
    show=False,               # return axes instead of displaying
)

Step 4b — Interactive plot (requires plotly)

WebGL-accelerated Plotly scatter plot with hover info showing gene set scores, RGB channel values, and custom metadata.

msp.plot_embedding_interactive(
    adata, rgb,
    basis="umap",
    scores=scores,
    method="nmf",
    gene_set_names=["qNSCs", "aNSCs", "TAP", "NB"],
    hover_columns=["n_counts", "cell_type"],  # extra adata.obs columns
    legend=True,
    legend_loc="lower right",
    point_size=2,
    width=600,
    height=500,
)

Step 5 — Standalone legend

Render the color-space legend on any matplotlib axes.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

# Direct mode (2-set square or 3-set triangle)
msp.render_legend(ax, "direct", gene_set_names=["A", "B", "C"])

# Reduction mode (RGB triangle with component labels)
msp.render_legend(ax, "pca")
msp.render_legend(ax, "nmf", component_labels=["NMF1", "NMF2", "NMF3"])

Extensibility — Custom reducers

Register your own dimensionality reduction method:

def my_umap_reducer(X, n_components, **kwargs):
    """X: (n_cells, n_gene_sets), returns (n_cells, 3) in [0, 1]."""
    import umap
    embedding = umap.UMAP(n_components=n_components, **kwargs).fit_transform(X)
    # min-max normalize each column to [0, 1]
    for j in range(embedding.shape[1]):
        col = embedding[:, j]
        lo, hi = col.min(), col.max()
        if hi > lo:
            embedding[:, j] = (col - lo) / (hi - lo)
    return embedding

msp.register_reducer("umap", my_umap_reducer, component_prefix="UMAP")

# Now use it like any built-in method
rgb = msp.reduce_to_rgb(scores, method="umap")

API Reference

Function Description
score_gene_sets(adata, gene_sets) Score gene sets per cell via pyUCell
blend_to_rgb(scores) Multiplicative blend to RGB (2–3 sets)
reduce_to_rgb(scores, method="pca") Dimensionality reduction to RGB (2+ sets)
plot_embedding(adata, rgb, basis=...) Static matplotlib scatter plot
plot_embedding_interactive(adata, rgb, basis=...) Interactive Plotly scatter plot
render_legend(ax, method) Draw color-space legend on axes
register_reducer(name, fn) Register a custom reduction method
get_component_labels(method) Get axis labels for a reduction method

Development

git clone https://github.com/andrecmacedo/multiscoresplot.git
cd multiscoresplot

# Install in editable mode with dev dependencies
pip install -e ".[interactive]"
pip install ruff pre-commit pytest pytest-cov mypy

# Run tests
pytest

# Lint & format
ruff check src/ tests/
ruff format src/ tests/

# Type check
mypy src/

# Set up pre-commit hooks
pre-commit install

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

multiscoresplot-1.0.2.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

multiscoresplot-1.0.2-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file multiscoresplot-1.0.2.tar.gz.

File metadata

  • Download URL: multiscoresplot-1.0.2.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for multiscoresplot-1.0.2.tar.gz
Algorithm Hash digest
SHA256 f74a24369aafb11bc67902c8961278db2862b2d2a5f75f3bb83fcc29cc0dfa0e
MD5 78fdd657e7e03eec146ad54924bea221
BLAKE2b-256 c10205e111f107a528a1bc1c9bfa6c97702b8aa709e67b4999ba234645735a30

See more details on using hashes here.

Provenance

The following attestation bundles were made for multiscoresplot-1.0.2.tar.gz:

Publisher: publish.yml on AndreMacedo88/multiscoresplot

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file multiscoresplot-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for multiscoresplot-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 91f3c35e6f91a6c90dfa4047dfa62c33dc92013afa20d0be0e39f28ac3e6c7a1
MD5 bdb299a8e2f45057a4ba09e3c50b22b5
BLAKE2b-256 f6affc16ff4126c77ddc33ba6a37d59861ee5d810697171dce5bd8f6c6ec6c7e

See more details on using hashes here.

Provenance

The following attestation bundles were made for multiscoresplot-1.0.2-py3-none-any.whl:

Publisher: publish.yml on AndreMacedo88/multiscoresplot

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page