Cell-type neighborhoods¶
Neighborhood asks two questions about a categorical column such as clusters or cell types: which types surround each type, and which pairs of types sit next to each other more (or less) often than their abundances alone would predict.
Where to find it
Analyze → Spatial → Neighborhood → Open Neighborhood tool…
How it works¶
The method is the standard permutation approach of histoCAT (Schapiro et al. 2017) and squidpy's nhood_enrichment (Palla et al. 2022), with per-cell composition windows as in Schürch et al. 2020. Skopia implements it directly rather than calling squidpy.
- Graph. Each cell is connected to its k nearest cells in space (default k = 10), or to every cell within a fixed radius.
- Composition. For each cell, the fraction of its neighbors that belong to each type. Averaging over the cells of one type gives a directed type × type matrix: row A, column B is the share of A's neighbors that are B. It is directional, so a type infiltrating a host reads differently from the host.
- Enrichment. Skopia counts directed neighbor pairs for every pair of types, then shuffles the type labels over the fixed cell positions many times and recounts. This null keeps both the tissue's geometry and each type's abundance, so a type cannot look enriched merely by being common.
- Statistics. A z-score of the observed count against the permutation mean and standard deviation, an empirical two-sided p-value (with a +1 correction, so the smallest possible value is about 2/(permutations + 1)), and a Benjamini-Hochberg q-value (Benjamini and Hochberg 1995) across all pairs. A log2 fold change of observed over expected uses a +1 pseudocount.
With a section column, no edge crosses a section boundary and labels are shuffled only within their own section.
Run it¶
- Open Analyze → Spatial → Neighborhood → Open Neighborhood tool….
- Pick the Cell type / cluster column.
- Choose Neighborhood (k nearest neighbors or radius) and its size.
- Set Permutations and, for multi-section data, Section column.
- Click Quantify neighborhoods. A progress bar counts permutations.
- Read the heatmap. Reopening the tool on the same dataset shows the previous result without rerunning.
Parameters¶
| Parameter | Default | What it does |
|---|---|---|
| Cell type / cluster column | first column whose name contains leiden, cell_type, cluster or type, else the first categorical column |
A categorical .obs column. It must have 2 to 200 categories and no missing values; a float column is refused. |
| Neighborhood | k nearest neighbors | knn adapts to density differences and is the usual choice. radius takes every cell within a fixed distance. |
| Neighbors (k) | 10 | Neighbors per cell in kNN mode. 5 to 15 is standard; 10 is close to the CODEX window of Schürch et al. Within a section, k is clamped to the section size minus 1. |
| Radius | none | Neighborhood radius in coordinate units (radius mode only; required there). |
| Permutations | 1000 | Label shuffles for the null. |
| Section column | none | Keep edges and shuffles within each section. |
Reading the results¶
The result opens as a types × types heatmap with two views:
- Enrichment (z): red when the row type has the column type as a neighbor more often than chance, blue when less, neutral at z = 0. A dot marks q < 0.05.
- Composition: what percentage of the row type's neighbors are the column type. Rows sum to 100%.
Hover a cell for z, q, observed and expected percentages and pair counts. Axis swatches use the column's colors from the embedding.
Types are ordered by hierarchical clustering of their profiles, with a dendrogram over the shared row and column order, so co-locating types form visible blocks. The ordering controls above the heatmap:
| Control | Default | What it does |
|---|---|---|
| Order | Clustered | Input order uses the column's own category order. Clustering needs at least 3 types. |
| Cluster on | Enrichment | Order by the enrichment matrix (types that like or avoid the same partners) or by composition (types whose neighborhoods have the same makeup). |
| Distance | Correlation | Correlation compares profile shape and ignores magnitude. Euclidean also weighs magnitude; cosine sits between. |
| Linkage | Average | Complete makes tight, equal-sized blocks; single tends to chain types into one ladder; Ward minimizes within-block variance and pairs with Euclidean. |
Switching between Enrichment and Composition does not reshuffle the order.
Exports¶
Export ▾ offers three files, all in the order shown:
- Statistics table (CSV): long format, one row per ordered pair of types, with composition, expected, pair count, z, log2 fold change, p and q.
- Enrichment matrix (CSV) or Composition matrix (CSV): the displayed matrix.
- Figure (SVG): the heatmap as a standalone vector file.
What it writes¶
| Where | What |
|---|---|
obsm['neighborhood_composition'] |
Cells × types matrix of neighbor fractions. NaN for a cell with no neighbors. |
uns['skopia_score_matrices']['neighborhood_composition'] |
Column names (the types) and the source column, so each type's fraction appears as a score you can color by. |
uns['skopia_neighborhood'] |
The type × type composition, counts, z, p, q, log2 fold change and expected matrices, plus the parameters. |
Each run overwrites the previous one, whatever column it used.
Tips and pitfalls¶
Annotate a cluster by the company it keeps
After a run, color by a type's neighbor fraction (for example "fraction of muscle in this cell's neighborhood") from the score pills. This helps place an ambiguous cluster by what surrounds it.
Read z-scores, not only q-values
With 1000 permutations the smallest possible p-value is about 0.002, so strongly enriched pairs can share the same minimum q-value. The z-score keeps ranking them past that floor.
Predicted coordinates
On a dataset whose coordinates come from Localize, the neighborhood is computed on predicted positions and inherits their error. Check the map under Localize's Map quality first.