Skip to contents

Performs cell type-specific spatial gene variability analysis

Usage

spacelink_cell_type(
  normalized_counts,
  spatial_coords,
  cell_type_proportions,
  focal_cell_type,
  covariates = NULL,
  global_spacelink_results = NULL,
  lengthscales = NULL,
  n_lengthscales = 5,
  M = 1,
  calculate_ESV = TRUE,
  c1 = 0.25,
  c2 = 0.2,
  n_workers = 1
)

Arguments

normalized_counts

Normalized count matrix. Rows and columns indicate genes and spots, respectively.

spatial_coords

Spatial coordinates matrix. Rows indicate spots.

cell_type_proportions

Cell type proportion matrix (e.g., estimated by RCTD).

focal_cell_type

Name of cell-type to be tested.

covariates

Spatial features (without intercept)

Spacelink global SVG test results from spacelink_global.

lengthscales

If NULL, length-scales are calculated by using our two-step approach.

n_lengthscales

Number of length-scales (i.e., number of kernels)

M

It controls the minimum length-scale. Minimum length-scale is set to be the minimum distance multiplied by M.

calculate_ESV

If TRUE, ct-ESV is returned.

c1

Gating parameter 1. Default is 0.25.

c2

Gating parameter 2. Default is 0.2.

n_workers

Number of workers for parallelization. Default is 1.

Value

A data frame containing:

time

Computation time

pval

Combined p-value for cell type-specific spatial patterns

ESV

ct-ESV for cell type-specific spatial patterns

Examples

library(spacelink)
# Set up simulated data
set.seed(123)
n_spots <- 100
n_genes <- 10
n_cell_types <- 2

# Generate spatial coordinates (2D grid)
coords <- expand.grid(
  x = seq(0, 10, length.out = sqrt(n_spots)),
  y = seq(0, 10, length.out = sqrt(n_spots))
)

# Generate cell type proportions for 2 cell types
cell_type_props <- matrix(nrow = n_spots, ncol = n_cell_types)
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_cell_types)

# Simulate (normalized) expression data with spatial autocorrelation for cell type 1
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,] <- matrix(rnorm(n_spots, 0, 1), nrow=1, ncol=n_spots)
  expr_data[i,] <- expr_data[i,] + i*matrix(rnorm(n_spots, 0, 1), nrow=1, ncol=n_spots)%*%Sigma
}
# Simulate (normalized) expression data with spatial autocorrelation for cell type 2
Sigma <- chol(K)%*%diag(cell_type_props[,'Cell_type_2'])
for(i in (n_genes/2+1):n_genes){
  expr_data[i,] <- matrix(rnorm(n_spots, 0, 1), nrow=1, ncol=n_spots)
  expr_data[i,] <- expr_data[i,] + matrix(rnorm(n_spots, 0, 1), nrow=1, ncol=n_spots)%*%Sigma
}

# Test cell-type-1 specific SVG
cell_type_results <- spacelink_cell_type(normalized_counts = expr_data, 
                                         spatial_coords = coords,
                                         cell_type_proportions = cell_type_props,
                                         focal_cell_type = "Cell_type_1")
print(cell_type_results[, c("pval", "ESV")])
#>                 pval          ESV
#> Gene_1  6.975021e-02 4.167041e-01
#> Gene_2  3.853336e-03 5.590444e-01
#> Gene_3  3.952311e-04 6.964892e-01
#> Gene_4  3.027289e-06 6.672739e-01
#> Gene_5  1.246179e-04 6.579634e-01
#> Gene_6  1.000000e+00 0.000000e+00
#> Gene_7  6.681283e-01 3.128719e-06
#> Gene_8  9.629749e-01 3.459269e-06
#> Gene_9  1.000000e+00 0.000000e+00
#> Gene_10 1.000000e+00 0.000000e+00