UMAP and embeddings¶

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

Plot your Scanpy results with GenomeSpy. Scanpy computes the coordinates; GenomeSpy reads them.

The PBMC examples use supplied UMAP coordinates. The ovarian cortex example loads Expression Atlas data and computes UMAP with Scanpy. For your own data, run sc.tl.umap(adata) after your usual Scanpy preprocessing and neighbor calculation.

Download notebook

import scanpy as sc
import genome_spy as gs
import genome_spy.integrations.scanpy as gsc
pbmc = sc.datasets.pbmc68k_reduced()

Plot¶

Color cells by a cluster label or gene expression. Scroll over either plot to zoom both axes. Drag to pan. Hover over a cell for details.

Hover over a cell to outline it in every panel. Set linked_hover=False to turn this off. Click a legend label to focus on a group; Shift-click to add or remove groups. Drag over a colorbar to focus on a value range.

basic = gsc.pl.umap(
    pbmc,
    color=["louvain", "CD79A"],
    size=12,
    width=240,
    height=240,
    zoom=True,
    show=False,
)
basic

Select cells¶

Drag a rectangle in the left plot. The same region appears in both panels, with a cell count below. Double-click in the left plot to clear it. Zoom is off in this example so dragging selects cells.

brush = gs.selection_interval("cells", encodings=["x", "y"], on="mousedown", zoom=False)
linked = gsc.pl.umap(
    pbmc,
    color=["louvain", "CD79A"],
    selection=brush,
    size=12,
    width=240,
    height=240,
    zoom=False,
    show=False,
)

Get the selected cells in Python¶

Run these cells in Jupyter. The web page can select and count cells; returning them to Python needs a running notebook.

view = linked.widget(parameter_names=[brush.name], inline=True, controls=False)
view

Draw a selection, then run this cell to get its cell IDs and an AnnData subset. An empty selection returns no cells.

mask = gsc.selection_mask(pbmc, view.parameter_values.get(brush.name))
selected_ids = pbmc.obs_names[mask]
selected_adata = pbmc[mask].copy()
selected_adata

Prepare ovarian cortex data¶

Load 36,785 cells from Expression Atlas in one line. The first download is about 240 MB. Scanpy normalizes the counts and computes UMAP below. This takes longer than the small PBMC examples. All cells are kept.

ovary = sc.datasets.ebi_expression_atlas("E-MTAB-8381")
sc.pp.normalize_total(ovary, target_sum=1e4)
sc.pp.log1p(ovary)
sc.pp.highly_variable_genes(ovary, n_top_genes=2000)
sc.pp.pca(ovary, mask_var="highly_variable", random_state=0)
sc.pp.neighbors(ovary, random_state=0)
sc.tl.umap(ovary, random_state=0)

Customize¶

Color VWF expression in all 36,785 ovarian cortex cells. The data uses Ensembl gene IDs. Set smaller points for the overview; they grow as you zoom. The color scale stops at the 99th percentile.

custom = gsc.pl.umap(
    ovary,
    color="ENSG00000110799",
    cmap="viridis",
    vmax="p99",
    size=2,
    frameon=False,
    title="VWF expression (36,785 cells)",
    width=400,
    height=340,
    zoom=True,
    show=False,
)
custom