Skip to content

Spatial domains across sections

Intermediate 20 minutes Data: bundled toy_spatial_3sections.h5ad (900 cells in three sections, 76 genes, raw counts)

A spatial domain is a region of tissue whose cells share an expression program. Finding one needs both kinds of information: expression says which cells are alike, position says which are together. This tutorial builds a graph for each, shows what each finds on its own, and combines them.

The dataset holds three serial sections of the toy tissue laid side by side in one coordinate space, with a section column saying which cell belongs to which. Handling the section boundaries is part of the exercise.

1. Load the dataset

Open File → Load…, set Load into: to New dataset, and pick backend/skopia/data/toy_spatial_3sections.h5ad in the Skopia folder. A new tab opens next to the first dataset (Datasets, split view and compare). Color by section to see the three sections.

2. A spatial graph that respects the sections

Open Analyze → Spatial → Spatial Neighbors. Set Section column to section and click Run. The modal reports Spatial graph: 5,400 edges for 900 cells, stored as obsp['spatial_connectivities'].

Why the section column matters

Without it, the nearest neighbors of a cell on the right edge of one section include cells on the left edge of the next one, which are not its neighbors in any tissue. Every tool that reads the spatial graph (autocorrelation, smoothing, Combine Neighbors) would inherit those false edges. With the column set, edges between sections are dropped (Spatial neighbors and autocorrelation).

3. Clustering on position alone

Open Analyze → Cells → Leiden. Set kNN graph to Spatial neighbors (spatial_connectivities, 5,400 edges), keep Resolution at 0.5, and click Run. The output name fills in as leiden_spatial.

Leiden on the spatial graph: 14 patches

Leiden at resolution 0.5 on the spatial graph alone: 14 clusters, each a contiguous patch, none shared between sections.

The result is 14 compact patches, and no patch appears in more than one section. A graph built from positions only knows that cells are close, so Leiden cuts the tissue into pieces of similar size. These are not domains: nothing in this graph says whether two cells express the same genes.

4. The expression graph

Run the usual chain on expression (From counts to clusters explains each step):

  1. Analyze → Preprocess: Normalize Total, Log1p, Highly Variable Genes.
  2. Analyze → Cells: PCA, Neighbors.

Neighbors writes the expression graph to obsp['connectivities']; on this dataset it has 19,428 edges.

5. Combine the two graphs

Open Analyze → Cells → Combine Neighbors. Both graphs are listed and ticked, Expression neighbors (connectivities) and Spatial neighbors (spatial_connectivities), each with Weight 1, which normalizes to 0.5 each. Keep the output name combined_connectivities and click Combine 2 graphs.

Each graph is scaled to its own maximum edge weight before weighting, so the larger graph does not dominate by having bigger numbers. The result here has 24,802 edges (PCA, neighbors, UMAP, Leiden).

6. Domains

Open Leiden again, set kNN graph to combined neighbors (combined_connectivities), keep resolution 0.5, and run. The output is leiden_combined, and the modal reports Found 3 clusters.

Leiden on the combined graph: three domains in every section

Leiden at resolution 0.5 on the combined graph (expression 0.5, spatial 0.5): three domains, each present in all three sections in the same arrangement.

The three clusters are contiguous within a section, as the spatial graph asks, and each one recurs in all three sections, which only shared expression can explain. That recurrence is the evidence that these are domains rather than patches.

Choosing the weights

The weights are a dial between the two answers you have already seen. Push spatial toward 1 and the result moves toward the patches of step 3; push expression toward 1 and you get cell types scattered across the tissue. Try 0.7 / 0.3 both ways and keep the weighting at which the domains are stable across sections.

Where next

  • Sections and territories: define sections on a dataset that has no section column, and draw territories by hand.
  • Contour: turn a gene set's score into nested zones, as an alternative to clustering when the tissue has a gradient rather than domains.
  • Cell-type neighborhoods: ask which cell types sit next to which.