An Analytical Neighborhood Enrichment Score for Spatial Omics

Axel Andersson, Hanna Nyström · 2025

Read An Analytical Neighborhood Enrichment Score for Spatial Omics by Axel Andersson, Hanna Nyström (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2506-18692.txt.

Introduction

Axel Andersson and Hanna Nyström replace a slow shuffle-based test for spatial omics with a direct formula. Their 2025 paper concerns neighborhood enrichment, a score for whether two cell types sit near each other more or less than chance. Spatial omics maps cells in tissue with labels attached. The classic test shuffles labels many times, which grows costly as data sets swell. Their version computes the score in closed form.

Across eight spatial data sets, the formula tracks the classic scores very closely. Speed gains are large. A further test on a huge modern data set shows the method stays fast at full scale. Labs can thus screen many type pairs without long waits. The authors offer the score as a drop-in swap for the shuffle test in daily spatial work.

Catalog record

CREATORAxel Andersson, Hanna Nyström
YEAR2025
COLLECTIONPAPERS
FORMATTXT
FILE SIZE15 KB
VERSION1.0
LICENSEhttp://creativecommons.org/licenses/by/4.0/
RIGHTSCC BY
SOURCEarXiv

Source: https://arxiv.org/abs/2506-18692

Browse the full library · About the Tower of Babel

Full text

The opening of the work, from the library's hosted text:

An Analytical Neighborhood Enrichment Score for Spatial Omics Axel Andersson, Hanna Nyström Abstract The neighborhood enrichment test is used to quantify spatial enrichment and depletion between spatial points with categorical labels, which is a common data type in spatial omics. Traditionally, this test relies on a permutation-based Monte Carlo approach, which tends to be computationally expensive for large datasets. In this study, we present a modified version of the test that can be computed analytically. This analytical version showed a minimum Pearson correlation of 0.95 with the conventional Monte Carlo-based method across eight spatial omics datasets, but with substantial speed-ups. Additional experiments on a large Xenium dataset demonstrated the method’s ability to efficiently analyze large-scale data, making it a valuable tool for analyzing spatial omics data. 1 Introduction Spatial omics, the study of the spatial organization of biomolecules, is a rapidly growing field [ 1 ] . Many spatial omics techniques produce image data that, after analysis, yield spatial points with categorical labels. For example, these points may represent locations of different cell types or mRNA molecules. The neighborhood enrichment test [ 2 ] is a widely used permutation-based method to quantify spatial enrichment or depletion between points with different labels. It is implemented in several statistical toolboxes for spatial omics [ 2 , 3 , 4 , 5 ] and has been applied in numerous studies [ 6 , 7 ] . The test measures how often spatial points with label A are neighbors to those with label B , then compares this observed count to a null distribution generated by repeatedly shuffling the labels across the data set. The output is a z-score-like value that indicates whether the two labels are spatially enriched or depleted. However, the repeated shuffles required to estimate this distribution can be computationally intensive, particularly for large datasets with many labels. We therefore propose a modified version of the enrichment score that can be computed analytically. This approach significantly improves computational speed and scalability. The code is publicly available at https://github.com/wahlby-lab/analytical-enrichment-test . 2 Materials and methods The neighborhood enrichment score The enrichment score, as implemented in the popular spatial omics toolbox Squidpy [ 2 ] , is given by: z_{\text{{\tt AB}}}=\frac{x_{\text{{\tt AB}}}-\mu_{\text{{\tt AB}}}}{\sigma_{\text{{\tt AB}}}} (1) where x_{\text{{\tt AB}}} is the count of points with label A that are neighbors points with label B . A neighbor can be defined in different ways, for example, if two points are within a set distance or if a point is within the k -nearest neighbors of a reference point. The variables \mu_{\text{{\tt AB}}} and \sigma_{\text{{\tt AB}}} in Eq. 1 correspond to the expectation and standard deviation in the number of neighbors between A -labeled points and B -labeled points after shuffling the labels of the points. The number of shuffles, N_{\text{MC}} , used to estimate \mu_{\text{{\tt AB}}} and \sigma_{\text{{\tt AB}}} is given as a user-defined parameter and should be large to avoid a high variance in the estimated z scores. Default in Squidpy is N_{\text{MC}}=10^{3} [ 2 ] . Analytical neighborhood enrichment score Instead of shuffling the labels and estimating the mean and standard deviation of neighbor counts through Monte Carlo sampling, we propose an analytical alternative that can be computed directly. Consider two labels: a reference label A , and a neighbor label B , and define: • \mathbf{b}\in\{0,1\}^{N} : a binary vector indicating which of the N points have label B , • \mathbf{W}\in\{0,1\}^{N\times N} : a sparse adjacency matrix encoding the neighborhood graph, • \mathbf{1}\in\mathbb{R}^{N} : a vector of ones (i.e., \mathbf{1}_{i}=1 for all i ). The vector containing the number of B -labeled neighbors for each point is: \mathbf{y}=\mathbf{W}\mathbf{b}. The expected number of B -labeled neighbors for a randomly selected point is: \mathbb{E}\big[\mathbf{y}\big]=\frac{1}{N}\mathbf{1}^{\top}\mathbf{y}, and the variance is: \mathbb{V}\big[\mathbf{y}\big]=\mathbb{E}\big[\mathbf{y}^{\circ 2}\big]-\mathbb{E}\big[\mathbf{y}\big]^{2}, where (\cdot)^{\circ 2} refers to the Hadamard square. Let n_{\text{{\tt A}}} be the number of points with the label A . If we sample n_{\text{{\tt A}}} points independently with replacement, the expected total number of B -labeled neighbors is: \mu_{\text{{\tt A}{\tt B}}}=n_{\text{{\tt A}}}\cdot\mathbb{E}\big[\mathbf{y}\big], with variance: \sigma_{\text{{\tt A}{\tt B}}}^{2}=n_{\text{{\tt A}}}\cdot\mathbb{V}\big[\mathbf{y}\big]. The proposed analytical enrichment z-score is then given by: z_{\text{{\tt A}{\tt B}}}=\frac{o_{\text{{\tt A}{\tt B}}}-\mu_{\text{{\tt A}{\tt B}}}}{\sigma_{\text{{\tt A}{\tt B}}}}, (2) where o_{\text{{\tt A}{\tt B}}} is the total number of neighbors labeled B of points labeled A , computed as: o_{\text{{\tt A}{\tt B}}}=\mathbf{a}^{\top}\mathbf{y}. with \mathbf{a}\in\{0,1\}^{N} as the indicator vector for label A points. Equation 2 can be rewritten as: z_{\text{{\tt A}{\tt B}}}=\sqrt{n_{\text{{\tt A}}}}\cdot\frac{\bar{o}_{\text{{\tt A}{\tt B}}}-\mathbb{E}[\mathbf{y}]}{\sqrt{\mathbb{V}[\mathbf{y}]}} where \bar{o}_{\text{{\tt A}{\tt B}}}=\frac{o_{\text{{\tt A}{\tt B}}}}{n_{\text{{\tt A}}}} . Matrix formulation of the enrichment score The previous pairwise formulation can be extended to a matrix-based form that simultaneously computes enrichment scores for all label pairs using basic linear algebra. Define: • \mathbf{L}\in\{0,1\}^{N\times K} : a one-hot label matrix, where L_{ik}=1 if the point i has the label k and K is the number of unique labels. • \mathbf{N}=\mathrm{diag}\big(\mathbf{L}^{\top}\mathbf{1}\big)\in\mathbb{N}^{K\times K} : a diagonal matrix where N_{kk} is the number of points with the label k . The matrix of neighbor label counts is given by: \mathbf{Y}=\mathbf{W}\mathbf{L}\in\mathbb{R}^{N\times K}, (3) where each column \mathbf{Y}_{:,k} contains the number of neighbors with label k for each point. The observed number of neighbors labeled k for each point labeled j is given by the matrix: \mathbf{O}=\mathbf{L}^{\top}\mathbf{Y}=\mathbf{L}^{\top}\mathbf{W}\mathbf{L}\in\mathbb{R}^{K\times K} and the corresponding per-point average is: \bar{\mathbf{O}}=\mathbf{N}^{-1}\mathbf{O}. The matrix of enrichment z-scores between all pairs of labels is: \mathbf{Z}=\mathbf{N}^{1/2}\left(\frac{\bar{\mathbf{O}}-\mathbb{E}\big[\mathbf{Y}\big]}{\sqrt{\mathbb{V}\big[\mathbf{Y}\big]}}\right)\in\mathbb{R}^{K\times K}, where all square roots, divisions, and differences are applied element-wise. Here, \mathbb{E}[\mathbf{Y}]\in\mathbb{R}^{1\times K} and \mathbb{V}[\mathbf{Y}]\in\mathbb{R}^{1\times K} are the expected values and variances of \mathbf{Y} across all points, calculated column-wise. Table 1 : Dataset characteristics including the number of unique labels and total number of points. Dataset # of labels # of points Ref. MIBI-TOF 8 1.2k [ 8 ] 4i 15 2.8k [ 9 ] Visium 12 2.8k [ 10 ] IMC 11 4.7k [ 11 ] MERFISH I 16 6.5k [ 12 ] SeqFISH 22 19.4k [ 13 ] osmFISH 36 1.98M [ 14 ] MERFISH II 135 3.73M [ 12 ] Figure 1 : The analytical enrichment score shows strong agreement with traditional Monte Carlo-based method across multiple datasets, while providing substantial speed-up. A Pearson correlation between the proposed Analytical enrichment score and the Monte Carlo-based implementation in Squidpy [ 2 ] with different number of label-permutations ( N_{\mathrm{MC}} ). B Speed-up of the analytical method compared to the Monte Carlo-based approach. Experiments were conducted using neighborhood graphs constructed with k=6 , k=12 , or k=18 nearest neighbors. C Speed-up of the analytical method compared to the Monte Carlo-based method for different dataset sizes (number of points). Speed-up increased with dataset size. D Comparison of z-scores computed by the analytical and Monte Carlo-based methods. Each point represents the enrichment z-score for a pair of labels in a given dataset. Overall, the z-scores produced by the analytical methods are lower than those of the Monte Carlo-based method. 3 Experiments and results To evaluate the performance of the analytical neighborhood enrichment test, we compared it to the original Monte Carlo-based approach implemented in Squidpy [ 2 ] . Our objectives were twofold: (i) to assess whether the two methods yield similar results by comparing their z-scores, and (ii) to quantify the computational speed-up achieved by the analytical method. We analyzed eight spatial

Read the complete text