Clustering cells with Python
Group cells into clusters and visualize them in a UMAP with the multiomics Python package and Scanpy.
Clustering groups cells with similar measurements so you can see subpopulations within a well or across treatments. This tutorial loads the 3′ transcriptome table from a Spatial Data Object (SDO), prepares it with the multiomics Python package, and clusters cells with Scanpy.
Before you begin
- Install the multiomics Python package and JupyterLab. The install includes Scanpy and the Leiden clustering libraries.
- You have a Spatial Data Object (SDO) with a 3′ transcriptome table (
ThreePrimeUntargeted). You do not need to unzip it.
Load and prepare the table
Load the 3′ transcriptome table, then filter and normalize it with the package defaults. For what each step does, see Filter and normalize.
import scanpy as sc
import elembio_spatialdata_analysis as ebsd
from elembio_spatialdata_analysis.load import Loader
loader = Loader("/path/to/SpatialData/RunID.zarr.zip")
adata = loader.load_tables(table="ThreePrimeUntargeted", lazy=False)
ebsd.pp.filter_cells(adata, "ThreePrimeUntargeted")
ebsd.pp.filter_genes(adata, "ThreePrimeUntargeted")
ebsd.pp.normalize(adata, "ThreePrimeUntargeted")
Cluster and embed
ebsd.pp.normalize already log-transforms the data, so go straight to feature selection:
sc.pp.highly_variable_genes(adata, n_top_genes=2000, subset=True)
sc.tl.pca(adata)
sc.pp.neighbors(adata)
sc.tl.leiden(adata, resolution=0.3, flavor="igraph", n_iterations=2, key_added="cluster")
sc.tl.umap(adata)
sc.pl.umap(adata, color=["cluster", "WellLabel"])
Raise resolution for more, smaller clusters, or lower it for fewer, larger ones.
Interpret the UMAP
Cells that sit close together in the UMAP have similar expression across the selected genes. Cluster numbers are labels from the Leiden algorithm, not cell types. To give clusters biological meaning, compare them with marker genes, treatment labels (WellLabel), or guide identity from an OPS table joined on cell_id_global.
To test whether a known pathway separates treatments, without clustering, see Measuring a 3′ treatment response.