Skip to contents

Computes a normalized Moran's I statistic to quantify spatial autocorrelation of cell states (e.g., spatial ecotypes) while accounting for cell-type structure. The observed Moran's I is compared against a null distribution generated by permuting cell state labels within each cell type.

Usage

ComputeNormalizedMoranI(
  scmeta,
  coords = c("X", "Y"),
  SE = "SE",
  CellType = "CellType",
  nperm = 1000,
  k = 3,
  ncores = 1
)

Arguments

scmeta

A data.frame containing single-cell metadata. Each row represents a cell.

coords

Character vector specifying column names for spatial coordinates (e.g., c("X", "Y")).

SE

Character. Column name specifying spatial ecotype (or cell state) labels.

CellType

Character. Column name specifying cell type annotations.

nperm

Integer. Number of permutations used to estimate the null distribution. Default is 1000.

k

Integer. Number of nearest neighbors used to construct the spatial graph. Default is 3.

ncores

Integer. Number of cores used in Moran’s I computation (if parallelized). Default is 1.

Value

A numeric vector of Z-scores representing normalized Moran's I values for each cell state.

Details

The function performs the following steps:

  • Constructs a k-nearest neighbor graph based on spatial coordinates

  • Computes observed Moran’s I for cell state labels

  • Generates a null distribution by permuting cell state labels within each cell type

  • Normalizes observed Moran’s I into Z-scores using the permutation mean and standard deviation

This approach preserves cell-type composition while testing whether spatial organization of cell states deviates from random expectation.