Ranked genes

Plot rankings stored by Scanpy. Start with the packaged scores, then prepare fold changes and adjusted p-values explicitly. GenomeSpy plotting reads those results.

Install or update with pip install --upgrade 'genome-spy-python[scanpy]'. Choose that environment as your Jupyter kernel.

Download notebook

import scanpy as sc
import genome_spy.integrations.scanpy as gsc
pbmc = sc.datasets.pbmc68k_reduced()
sc.tl.dendrogram(pbmc, groupby="bulk_labels")
ranked_groups = ["CD19+ B", "CD14+ Monocyte"]

Inspect ranking scores

Show the five highest-ranked genes for each selected cell type. Hover for the stored scores.

scores = gsc.pl.rank_genes_groups(
    pbmc,
    groups=ranked_groups,
    n_genes=5,
    ncols=2,
    width=240,
    height=280,
    show=False,
)
scores

Summarize ranked genes with dots

Use the top three genes from each selected ranking. Color shows mean expression; size shows the fraction of cells expressing each gene.

ranked_dots = gsc.pl.rank_genes_groups_dotplot(
    pbmc,
    groups=ranked_groups,
    n_genes=3,
    zoom=False,
    highlight=True,
    width=240,
    height=320,
    show=False,
)
ranked_dots

Compare ranked gene means

Use the same ranked genes in a matrixplot. Each gene’s group means are scaled from 0 to 1.

ranked_means = gsc.pl.rank_genes_groups_matrixplot(
    pbmc,
    groups=ranked_groups,
    n_genes=3,
    standard_scale="var",
    zoom=False,
    width=240,
    height=320,
    show=False,
)
ranked_means

Inspect ranked genes in individual cells

Show individual cells for the same ranked gene set. Scroll to zoom rows and hover for cell IDs and values.

ranked_cells = gsc.pl.rank_genes_groups_heatmap(
    pbmc,
    groups=ranked_groups,
    n_genes=3,
    standard_scale="var",
    cmap="viridis",
    zoom=True,
    width=300,
    height=400,
    show=False,
)
ranked_cells

Prepare ranking statistics

Compute complete Wilcoxon rankings once with Scanpy. Each cell type is compared with the rest. Here, raw contains log-normalized expression.

sc.tl.rank_genes_groups(
    pbmc,
    "bulk_labels",
    method="wilcoxon",
    use_raw=True,
    n_genes=pbmc.raw.n_vars,
    key_added="rank_stats",
)

Color dots by fold change

Color shows the stored log2 fold change. Dot size still shows the fraction of cells expressing each gene. Hover for values.

fold_changes = gsc.pl.rank_genes_groups_dotplot(
    pbmc,
    groups=ranked_groups,
    n_genes=3,
    key="rank_stats",
    values_to_plot="logfoldchanges",
    cmap="redblue",
    vmin=-4,
    vmax=4,
    colorbar_title="Log2 fold change",
    zoom=False,
    highlight=True,
    width=300,
    height=320,
    show=False,
)
fold_changes

Color tiles by adjusted p-value

Color shows −log10 of the stored adjusted p-value. Larger values mean smaller adjusted p-values. Hover for values.

adjusted_p_values = gsc.pl.rank_genes_groups_matrixplot(
    pbmc,
    groups=ranked_groups,
    n_genes=3,
    key="rank_stats",
    values_to_plot="log10_pvals_adj",
    cmap="viridis",
    colorbar_title="−log10 adjusted p-value",
    zoom=False,
    width=300,
    height=320,
    show=False,
)
adjusted_p_values

Data use and provenance

These examples use Scanpy’s packaged, preprocessed PBMC68k data. Credit 10x Genomics and Scanpy’s preprocessing contributors. Source data: CC BY 4.0. Supplied expression, annotations and rankings are retained. The final two plots use complete Wilcoxon rankings computed explicitly with Scanpy on the supplied log-normalized expression.