Skip to content

Spatial neighbors and autocorrelation

Spatial Neighbors builds a graph that connects each spot or cell to the ones next to it in the tissue. Spatial Autocorrelation uses that graph to ask which genes are spatially patterned: is a gene's expression more similar between neighbors than chance would give?

Where to find it

Analyze → Spatial → Spatial Neighbors, then Analyze → Spatial → Spatial Autocorrelation

Both tools need spatial coordinates. If the dataset has none under a name Skopia recognizes (for example a map predicted by Localize), see Choosing the dataset's coordinates below.

Spatial Neighbors

How it works

Skopia calls squidpy's spatial_neighbors (Palla et al. 2022) on the dataset's spatial coordinates. Each cell is connected to its nearest neighbors in physical space, or to the cells it shares a Delaunay triangle with. Expression plays no part.

The graph feeds several later steps: Spatial Autocorrelation, Preprocess → Smooth (averaging expression over neighbors), Combine Neighbors (mixing the spatial graph with an expression graph before Leiden or UMAP) and the conflict vote in multi-gene-set Contour.

Run it

  1. Open Analyze → Spatial → Spatial Neighbors.
  2. Set Neighbors. Six suits a hexagonal Visium array, where each spot has six direct neighbors.
  3. Leave Coord Type empty unless you know squidpy guesses wrong.
  4. If the dataset holds several tissue sections in one coordinate space, set Section column (see Sections and territories).
  5. Click Run. The result line reports the number of edges and cells.

Parameters

Parameter Default What it does
Neighbors 6 Number of spatial neighbors per cell (n_neighs).
Coord Type empty (auto-detect) grid for a regular spot array such as Visium, generic for anything else. Empty lets squidpy decide.
Delaunay false Connect cells by Delaunay triangulation instead of k nearest neighbors.
Section column none A categorical .obs column of section labels. When set, edges between cells in different sections are dropped, so the graph is block-diagonal and no neighborhood spans the gap between two sections.

What it writes

Where What
obsp['spatial_connectivities'] The spatial adjacency graph.
obsp['spatial_distances'] Distances along each edge.
uns['spatial_neighbors'] squidpy's record of the parameters.

Running again overwrites all three.

Spatial Autocorrelation

How it works

For each gene, Skopia computes a global spatial autocorrelation statistic over the spatial graph: Moran's I (Moran 1950) or Geary's C (Geary 1954), through squidpy's spatial_autocorr. Moran's I is positive when neighbors have similar values; Geary's C is below 1 in the same case. P-values are corrected for multiple testing with Benjamini-Hochberg (Benjamini and Hochberg 1995).

A gene is marked spatially variable when its adjusted p-value is below P-value Threshold and its statistic points toward clustering (Moran's I above 0, or Geary's C below 1).

Method details
  • Chunked for progress. squidpy has no progress hook, so Skopia runs it on about 20 chunks of genes and reports after each. The analytic statistics and p-values are identical to a single call, because each gene's result depends only on its own column. The multiple-testing correction and the sort by statistic run once over the joined table. Chunking cost 1.5% in time (212 s against 209 s, 20,000 cells, 1,000 genes, 50 permutations, one job).
  • Empty genes do not poison the correction. A gene with no counts has a NaN p-value, and the statsmodels routine squidpy uses returns NaN for every gene if any input is NaN. Skopia corrects over the finite p-values only and leaves empty genes NaN. Before this fix, a whole-transcriptome run on an E12.5 hindlimb section (191,425 spots, 39,812 genes, 3,700 of them empty) reported no spatially variable genes at all.
  • Which p-value makes the call. spatially_variable and var['spatial_pval_adj'] use the analytic (normal-approximation) p-value, BH-adjusted. With Permutations above 0, squidpy also computes permutation p-values; they are kept in the full table in uns with their own BH-adjusted column, but they do not change which genes are flagged.

Run it

  1. Run Spatial Neighbors first; the tool refuses to run without obsp['spatial_connectivities'].
  2. Open Analyze → Spatial → Spatial Autocorrelation.
  3. Pick Method and the genes to test under Gene Subset.
  4. Click Run. A progress bar shows genes tested and time left; Cancel takes effect only after the current run returns.
  5. The result line reports how many genes were called spatially variable and the top three. The top 20 are also saved as a gene set named like Top SVG (Moran's I) <date> in the Gene Panel.

Parameters

Parameter Default What it does
Method moran Moran's I or Geary's C.
Permutations 100 Permutations for squidpy's permutation p-value, stored in uns but not used for spatially_variable. 0 skips them (analytic p-values only), which is much faster and gives the same call.
P-value Threshold 0.05 Adjusted p-value below which a gene is flagged.
Parallel Jobs 1 Worker processes squidpy may use.
Gene Subset highly_variable Boolean .var columns that restrict which genes are tested. Combine several with intersection or union. With no column picked, Skopia falls back to highly_variable if that column exists, and to every gene only if it does not.

What it writes

Where What
var['spatially_variable'] True for genes that pass the threshold in the clustering direction. Genes not tested are False.
var['moranI'] or var['gearyC'] The statistic per tested gene; NaN for genes not tested.
var['spatial_pval_adj'] BH-adjusted analytic p-value; NaN for genes not tested.
uns['moranI'] or uns['gearyC'] squidpy's full results table, sorted by the statistic, with every p-value column and its BH-adjusted copy.

Each run resets spatially_variable and spatial_pval_adj for every gene, so a second run on a different subset replaces the first rather than adding to it.

Choosing the dataset's coordinates

Spatial tools look for coordinates in .obsm['spatial'], then .obsm['X_spatial']. A map predicted by Localize lands in .obsm['X_spatial_pred'] (or whatever name you gave it), which auto-detection does not look for. Spatial Neighbors, Contour, Define Sections, Define and Assign Territories, Neighborhood, Ligand-Receptor and Merge Spots then report Requires: HAS_SPATIAL to be computed first.

That warning carries a picker:

  1. Select the tool in Analyze → Spatial. The prerequisite warning lists every .obsm array that could serve as 2-D coordinates.
  2. Pick one. A predicted map is preselected and marked predicted; an array with cells left unplaced shows how many.
  3. Click Use as coordinates.

The choice is stored with the dataset in uns['skopia_spatial_key'], so it survives saving and reloading, and every spatial tool reads the same array. Switching to a second predicted map re-points all of them at once. If the chosen array is later deleted, Skopia falls back to auto-detection.

Predicted coordinates are a weaker claim than measured ones

Every neighborhood statistic computed over a Localize map inherits that map's error. Score the map under Localize's Map quality before reading a spatial graph, an autocorrelation or a ligand-receptor score on it as biology. Cells left without a coordinate (by a Min confidence threshold) cannot take part in a spatial graph.

This is an explicit choice rather than a wider auto-detect on purpose: a query localized under several settings carries several predicted maps, and guessing among them would silently decide which analysis you meant.

Tips and pitfalls

Several sections in one coordinate space

Without a Section column, kNN edges connect cells on facing edges of neighboring sections. Those edges then leak into Autocorrelation, Smooth and Combine Neighbors. Set the column on Spatial Neighbors; the downstream steps inherit the corrected graph.

Feed Localize

spatially_variable computed on a spatial reference is the principled gene basis for Localize: those are the genes that carry positional information.

Testing every gene

The default tests highly variable genes only, and so does clearing the subset when a highly_variable column exists. A gene that is spatially patterned but not highly variable across cells is then never tested. To test the whole transcriptome on such a dataset, pick a boolean .var column that is true for every gene you want. The gene mask, when set, also limits which genes are tested.