PCA, neighbors, UMAP, Leiden¶
The Cells tab of the Analyze modal holds the clustering chain: PCA reduces the genes to components, Neighbors builds a cell-cell graph on them, and UMAP and Leiden draw and cluster that graph. The same tab can embed or cluster any other graph (spatial, combined), drop technical components, and build a 2-D view from two .obs columns.
Where to find it
Analyze → Cells, then pick the operation from the dropdown.
For how the modal works (prerequisites, the cell mask banner, run history), see The Analyze modal. These operations run inside the request: Run reads Running... until the result arrives, and they cannot be cancelled.
The usual order¶
- Preprocess: normalize, log-transform, flag highly variable genes.
- PCA.
- PCA Loadings (optional): look for technical components and drop them.
- Neighbors.
- UMAP and Leiden, in either order.
Each step runs on the previous step's output, and each writes to a key named after its input, so results from different graphs or cell subsets sit side by side. On a cell mask, the chain asks for a subset name and writes only to that subset's keys (X_pca_<name>, <name>_connectivities, X_umap_<name>, leiden_<name>); the dataset's own results are left as they are. See Named subsets and the subset tree.
PCA¶
Principal component analysis of the cells (sc.tl.pca, Wolf et al. 2018) on .X.
| Parameter | Default | What it does |
|---|---|---|
| Components | 50 | Number of principal components. Lowered automatically when fewer genes or cells are available. |
| SVD solver | arpack |
arpack, randomized or auto |
| Gene Subset | none | Boolean .var columns that pick the genes; see below |
| Scale genes | no (recommended) | Yes z-scores each gene, clipped at 10, before the PCA |
Which genes PCA uses¶
- No Gene Subset picked: the genes in
highly_variablewhen that column exists, otherwise every gene. On a named subset, its ownhighly_variable__<name>column is used first when it exists. - Gene Subset picked: exactly the genes where the picked columns are True (with two or more columns, AND (intersection) or OR (union)). They are not narrowed further to
highly_variable. Pickhighly_variabletogether with your column, with AND, if you want both.
When you pick genes, the result message says how many the PCA used.
Why this is spelled out
scanpy's pca restricts itself to highly_variable whenever that column exists, even inside a gene subset you already chose. Earlier versions of Skopia let that happen, so a PCA on spatially_variable silently ran on the spatially variable genes that were also HVGs. Skopia now turns scanpy's restriction off whenever you pick genes yourself.
Scale genes¶
Scaling gives every gene the same weight in the components, so a lowly expressed gene pulls as hard as a strongly expressed one. Left unscaled, expression magnitude still counts as information; the single-cell best-practices guide (Heumos et al. 2023) leaves genes unscaled and relies on gene selection instead, which is why no is the default. Scaling works on a copy, and .X is not changed.
What it writes¶
| Where | What |
|---|---|
obsm['X_pca'] |
Cells × components |
varm['PCs'] |
Gene loadings (0 or NaN for genes the PCA did not use) |
uns['pca'] |
Variance and variance ratio per component, and the parameters |
obsm['X_pca_<subset>'], varm['PCs_<subset>'], uns['pca_<subset>'] |
A run on a named subset; NaN outside the subset |
Warning
Re-running PCA clears every PC subset made from the previous PCA (a toast says how many), since their columns refer to the old components.
PCA Loadings¶
A table of the genes with the largest positive and negative loadings on each component, used to spot components driven by technical signal (cell cycle, mitochondrial or ribosomal genes, stress) and to make an embedding without them.
- Run PCA. The table needs the loadings PCA stores; a PCA imported from another tool without them reports "Run PCA first to explore loadings."
- Set Top-N genes per side (default 10). Each row is a PC with its Var %, its Top + loading genes and its Top − loading genes. Hover a gene for its exact loading.
- Optionally, type genes into Highlight genes (comma, semicolon, space or newline separated), or fill it from a Genes panel set with From gene set…. Each listed gene that appears in a PC's displayed lists is marked, and a Hits column gives
total (pos+/neg−). Matching ignores case. A summary line says how many of the genes appear in the top N of at least one PC, and a muted line lists the genes in no displayed list (they may load slightly below the top N). Order PCs: Most gene-set hits sorts the rows by hits. - Check the PCs to drop. The line under the table says how many would remain.
- Optionally type a Suffix; the default is built from the dropped PCs, such as
noPC2_5. - Click Create PC subset →.
The new embedding, such as X_pca_noPC2_5, holds the kept components only. It appears under Existing PC subsets (✕ deletes it) and in the Neighbors PC source picker. UMAP and Leiden inherit the choice through the graph that Neighbors builds from it.
| Where | What |
|---|---|
obsm['X_pca_<suffix>'] |
The kept components |
varm['PCs_<suffix>'] |
Their loadings |
uns['pca']['variance_ratio_<suffix>'], uns['pca']['subsets'][<suffix>] |
Their variance ratios and the dropped PCs |
With a saved subset active, the table shows that subset's own PCA (X_pca_<name>), and a PC subset made there is X_pca_<name>_<suffix>. Re-running the dataset's PCA leaves a subset's PC subsets alone.
Neighbors¶
Builds the k-nearest-neighbor graph of the cells (sc.pp.neighbors) on a PCA embedding. UMAP, Leiden and Diffusion map run on this graph by default.
| Parameter | Default | What it does |
|---|---|---|
| PC source | X_pca (all PCs) |
The PCA embedding to use, including any PC subset from PCA Loadings |
| Neighbors | 15 | Number of neighbors per cell |
| PCs to use | blank (all) | How many leading components of the PC source to use |
| Metric | euclidean |
euclidean, cosine or manhattan |
Above the parameters, a bar chart shows the percent of variance each PC explains, with an elbow marked when one is found (Kneedle, Satopää et al. 2011). Use it to choose PCs to use: components past the elbow add little variance.
On a saved subset, PC source points at the subset's own PCA (X_pca_<name>) unless you choose another.
| Where | What |
|---|---|
obsp['connectivities'], obsp['distances'], uns['neighbors'] |
The graph and its parameters |
obsp['<subset>_connectivities'], obsp['<subset>_distances'], uns['<subset>'] |
A run on a named subset |
UMAP and Leiden on any graph¶
UMAP (McInnes et al. 2018) embeds a graph in two dimensions, and Leiden (Traag et al. 2019) finds communities in it. Both take a kNN graph picker listing every connectivity graph in the dataset: the expression graph from Neighbors, the graph from Spatial Neighbors, any Combine Neighbors result, and a subset's own graph. When the expression graph exists it is preselected, labeled with its key and edge count. The empty option, Dataset default graph, is the graph uns['neighbors'] points to.
Results are named after the graph, so a map of one graph never overwrites a map of another:
| Graph | UMAP writes | Leiden writes |
|---|---|---|
connectivities (expression) |
obsm['X_umap'] |
obs['leiden'] |
spatial_connectivities |
obsm['X_umap_spatial'] |
obs['leiden_spatial'] |
combined_connectivities |
obsm['X_umap_combined'] |
obs['leiden_combined'] |
| A named subset's graph | obsm['X_umap_<subset>'] |
obs['leiden_<subset>'] |
The name field is filled from the graph and refilled when you change graphs, unless you have typed your own.
Clustering the spatial graph finds spatial domains, contiguous regions of tissue, rather than cell types. It needs neither PCA nor Neighbors, so it works on a spatial dataset as soon as Spatial Neighbors has run. On the bundled toy dataset, Leiden on the spatial graph gave 10 domains whose labels agreed with each spot's six nearest neighbors 0.97 of the time on average, against about 0.1 for random labels.
UMAP¶
| Parameter | Default | What it does |
|---|---|---|
| kNN graph | the expression graph | The graph to embed |
| Embedding name | blank (from the graph) | obsm key for the result |
| Min distance | 0.5 | How tightly points pack; smaller values give tighter clumps |
| Spread | 1.0 | Scale of the embedded points |
| Dimensions | 2 | Number of dimensions |
Leiden¶
| Parameter | Default | What it does |
|---|---|---|
| kNN graph | the expression graph | The graph to cluster |
| Resolution | 0.5 | Higher values give more clusters |
| Column name | leiden (from the graph) |
.obs column for the cluster labels |
On a named subset, cells outside the subset get the label unassigned.
Combine Neighbors¶
Combines two or more cell graphs, such as the expression graph and the spatial graph, into one weighted graph, for clustering that respects both expression and position.
- Run Neighbors and Spatial Neighbors (or build any two graphs).
- Open Analyze → Cells → Combine Neighbors. Every graph is listed and checked, with Weight 1.
- Uncheck graphs you do not want and set the weights. The line below shows the weights normalized to sum to 1.
- Set Output graph name (obsp key) (default
combined_connectivities). A_connectivitiessuffix is added if missing, so the graph appears in the graph pickers. - Click Combine N graphs, then pick the result in the kNN graph field of Leiden or UMAP.
Each graph is scaled so its largest edge weight is 1 before weighting, so graphs with different edge scales contribute in proportion to their weights. The weighted sum is symmetrized.
| Where | What |
|---|---|
obsp['combined_connectivities'] (or your name) |
The combined graph |
Warning
Naming the output connectivities overwrites the expression graph, and Leiden and UMAP then use the combined graph by default. The modal warns when you type that name. Use another name to keep both. (The description at the top of the tool still says the result replaces obsp['connectivities']; that is only true for this name.)
Downsample¶
Picks a random subset of cells, a count or a percent, and makes it the selection. With a label column under Balance by, it takes the same number of cells from each label instead. The sample is an ordinary selection, so Mask, Delete, Invert and Set Active apply to it. To thin one dominant cluster and leave the rest untouched, lasso the cluster, downsample, invert the selection, and mask or delete.
Click Open Downsample tool…, or Downsample… on any selection in the Cells panel. The tool samples from Selected cells, Active (unmasked) cells or All cells; Keep takes a number of cells or a percent of the pool; with Balance by set it is the number of cells per label, blank meaning as many as the smallest label has, and labels with fewer cells are left out or kept whole as you choose; Seed is an integer, blank draws one, and the seed used is reported so the same seed gives the same cells. Nothing is written to the dataset. See Select, mask and delete.
Embedding from .obs¶
Builds a 2-D embedding from two numeric .obs columns, for example a barnyard plot of human against mouse counts per cell (Multi-genome data).
| Parameter | Default | What it does |
|---|---|---|
| X column | none | Numeric .obs column for the x axis |
| Y column | none | Numeric .obs column for the y axis |
| Log axes | none |
Apply log1p to x, y or both; refused for a column with negative values |
| Name (optional) | X_<x>_vs_<y> |
Embedding name; an X_ prefix is added if missing. An existing name is refused. |
| Where | What |
|---|---|
obsm['X_<x>_vs_<y>'] (or your name) |
Cells × 2 |
Related¶
- Preprocess
- Diffusion maps and pseudotime: also takes any graph
- Tight clusters: labels only the cells whose cluster is stable
- Metacells: groups near-identical cells on the kNN graph into a smaller dataset
- Named subsets and the subset tree
- Spatial neighbors and autocorrelation: the spatial graph
- Differential expression and markers: compare the clusters
- Navigate and adjust embeddings