Skill: scRNA-seq Clustering
Use When
- User has a QC-filtered count matrix and wants to cluster cells into groups.
- User wants to generate UMAP or tSNE embeddings for visualization.
- User needs to perform dimensionality reduction before cell type annotation or differential expression.
Inputs
- Required:
- Filtered AnnData (.h5ad) or Seurat object (.rds)
- Optional:
- Framework:
scanpyorseurat(default:scanpy) - Normalization method:
lognormorscttransform(default:lognorm) - Number of highly variable genes (default: 3000)
- Number of PCs to compute (default: 50)
- Number of PCs to use for neighbor graph (default: 30; refine with elbow plot)
- Number of neighbors for kNN graph (default: 15)
- Clustering resolution (default: 0.5; range 0.1–2.0)
- Clustering algorithm:
leidenorlouvain(default:leiden) - Variables to regress out (default: none; options:
nCount_RNA,percent.mt) - Random seed (default: 42)
- Framework:
Workflow
- Normalize counts: CPM normalization (counts per 10,000) followed by log1p transformation (
sc.pp.normalize_total+sc.pp.log1pin Scanpy;NormalizeDatain Seurat), or run SCTransform (Seurat v5 /scranfor Scanpy). - Select highly variable genes (HVGs): use
seurat_v3flavor in Scanpy orFindVariableFeaturesin Seurat; default to top 3000 HVGs. - Scale gene expression to zero mean and unit variance (
sc.pp.scaleorScaleData). If regression is requested, regress outnCount_RNAand/orpercent.mtduring scaling. Skip scaling for SCTransform workflows. - Run PCA on the HVG-scaled matrix (
sc.tl.pcaorRunPCA); compute 50 PCs. Plot an elbow plot to identify where variance plateaus and select the number of PCs for the neighbor graph. - Compute the k-nearest neighbor (kNN) graph in PCA space (
sc.pp.neighborsorFindNeighbors) using the selected number of PCs. - Cluster cells with the Leiden algorithm (
sc.tl.leiden) or Louvain (sc.tl.louvain/FindClusters). Test at least two resolutions (e.g., 0.3 and 0.8) and compare cluster stability. - Compute UMAP embedding (
sc.tl.umaporRunUMAP); optionally compute tSNE. Always set a fixed random seed. - Plot UMAP colored by: cluster ID, QC metrics (
nFeature_RNA,nCount_RNA,percent.mt), and sample of origin. Flag clusters with elevated QC metrics as potentially low-quality. - Save updated AnnData (.h5ad) or Seurat object (.rds) with embeddings and cluster labels.
Output Contract
- Updated AnnData (.h5ad) or Seurat object (.rds) with cluster labels (
leidenorseurat_clusterscolumn) and UMAP/tSNE coordinates - UMAP plots colored by cluster, QC metrics, and sample (PDF)
- PCA elbow plot (PDF)
- Cluster membership table (TSV): cell barcode, cluster ID, UMAP1, UMAP2
Limits
- Leiden resolution parameter strongly controls granularity: lower values (0.1–0.3) yield fewer, broader clusters; higher values (1.0–2.0) yield many fine-grained clusters.
- Scaling with regression is incompatible with SCTransform; choose one normalization path.
- UMAP is non-deterministic without a fixed random seed; always set
random_state=42. - Cell cycle effects may separate proliferating cells into their own cluster regardless of lineage; regress out cell cycle scores if this is not the biology of interest (
sc.tl.score_genes_cell_cycle). - SCTransform is computationally expensive for datasets with more than 100,000 cells; use
lognormfor very large datasets. - Neighbor graph quality depends on the number of PCs chosen; too few PCs lose biological signal, too many add noise. Use the elbow plot as a guide.
- Common failure cases:
- Leiden clustering producing a single cluster (resolution too low) or as many clusters as cells (resolution too high); adjust resolution.
- UMAP looks identical across runs due to missing random seed; check
random_stateparameter. - SCTransform failing due to insufficient memory on large datasets (>200k cells); fall back to lognorm.
Scan to join WeChat group