Skip to contents

Conducts hypothesis testing to identify cell type–specific spatially variable genes (ctSVGs) using a data-driven colocalization gating strategy, and provides cell type–specific Effective Spatial Variability (ct-ESV) scores (ranging from 0 to 1), where higher values indicate greater spatial variability in gene expression within the focal cell type.

Usage

spacelink_ctSVG(
  normalized_counts,
  spatial_coords,
  cell_type_proportions,
  focal_cell_type,
  covariates = NULL,
  lite = FALSE,
  grid_size = NULL,
  kernel = "Exponential",
  n_lengthscales = 5,
  c1 = 0.1,
  c2 = 0.1,
  n_workers = 1
)

Arguments

normalized_counts

Normalized count matrix (genes x spots). Accepts a numeric matrix, data.frame, or sparse matrix (sparseMatrix).

spatial_coords

Two-dimensional spatial coordinate matrix (spots x 2). Accepts a numeric matrix or data.frame.

cell_type_proportions

Cell type proportion matrix (spots x cell types, e.g., estimated by RCTD). Accepts a numeric matrix or data.frame.

focal_cell_type

Name of the cell type to be tested. Must match a column name of cell_type_proportions.

covariates

Optional covariate matrix (spots x covariates, without intercept). Default is NULL.

lite

Logical. If TRUE, uses a grid-based approximation. Default is FALSE.

grid_size

Grid cell size for lite mode. If NULL, set automatically to 5 times the minimum nearest-neighbor distance. Default is NULL.

kernel

Spatial kernel type. One of "Exponential" (default) or "Gaussian".

n_lengthscales

Number of length scales (kernels). Default is 5.

c1

Gating parameter controlling the proportion threshold for determining if the focal cell type dominates. Default is 0.1.

c2

Gating parameter controlling the colocalization threshold. Default is 0.1.

n_workers

Number of workers for parallelization. Default is 1.

Value

A data frame (genes x results) containing:

time

Computation time per gene (seconds)

pval

Combined p-value (ACAT)

padj

Benjamini-Hochberg adjusted p-value

ESV

Cell type-specific Effective Spatial Variability score

ESV_adj

Cell type-specific ESV adjusted for non-ctSVGs (0 if padj > 0.05)

If all spots are pure (no mixing), falls back to spacelink output (same columns) for the focal cell type spots.

Examples

# \donttest{
library(spacelink)
set.seed(123)
n_spots    <- 100
n_genes    <- 10
n_celltypes <- 2

coords <- expand.grid(
  x = seq(0, 10, length.out = sqrt(n_spots)),
  y = seq(0, 10, length.out = sqrt(n_spots))
)

cell_type_props <- matrix(nrow = n_spots, ncol = n_celltypes)
cell_type_props[1:(n_spots / 2), 1]            <- runif(n_spots / 2, 0, 0.5)
cell_type_props[(n_spots / 2 + 1):n_spots, 1]  <- runif(n_spots / 2, 0.5, 1)
cell_type_props[, 2] <- 1 - cell_type_props[, 1]
colnames(cell_type_props) <- paste0("Cell_type_", 1:n_celltypes)

expr_data <- matrix(nrow = n_genes, ncol = n_spots)
rownames(expr_data) <- paste0("Gene_", 1:n_genes)
D <- as.matrix(dist(coords)); K <- exp(-D)
Sigma <- chol(K) %*% diag(cell_type_props[, "Cell_type_1"])
for (i in 1:(n_genes / 2))
  expr_data[i, ] <- rnorm(n_spots) + i * rnorm(n_spots) %*% Sigma
Sigma <- chol(K) %*% diag(cell_type_props[, "Cell_type_2"])
for (i in (n_genes / 2 + 1):n_genes)
  expr_data[i, ] <- rnorm(n_spots) + rnorm(n_spots) %*% Sigma

results <- spacelink_ctSVG(
  normalized_counts     = expr_data,
  spatial_coords        = coords,
  cell_type_proportions = cell_type_props,
  focal_cell_type       = "Cell_type_1"
)
print(results[, c("pval", "ESV")])
#>                 pval          ESV
#> Gene_1  8.204439e-02 4.167043e-01
#> Gene_2  5.121033e-03 6.063774e-01
#> Gene_3  5.702071e-04 6.964893e-01
#> Gene_4  5.175750e-06 6.582678e-01
#> Gene_5  1.856661e-04 6.579634e-01
#> Gene_6  8.726797e-01 3.665291e-06
#> Gene_7  6.295618e-01 2.876897e-03
#> Gene_8  8.989732e-01 8.545377e-06
#> Gene_9  7.814450e-01 3.790261e-06
#> Gene_10 7.961633e-01 3.368537e-06
# }