
Construct Spatial Metacells from Single-Cell Spatial Data
Source:R/GetSpatialMetacells.R
GetSpatialMetacells.RdThis function computes spatial metacells from single-cell spatial transcriptomics. Spatial metacells represent aggregated expression profiles for spatially proximal cells. The function utilizes k-nearest neighbor (KNN) weighting to aggregate expression profiles from neighboring cells and generate metacell expression profiles for each cell type.
Usage
GetSpatialMetacells(
normdata,
metadata,
X = "X",
Y = "Y",
CellType = "CellType",
spotCoord = NULL,
k = 20,
radius = 50,
grid.size = 50,
min.cells.per.region = 1,
ncores = 4
)Arguments
- normdata
A matrix or data frame representing normalized gene expression data, where rows correspond to genes and columns correspond to cells.
- metadata
A data frame containing metadata associated with each cell. Must include spatial coordinates (e.g., X and Y) as well as cell type annotations. The row names of the
metadatamust match the column names of thenormdata.- X
Character string specifying the column name in
metadatacontaining the X spatial coordinates.- Y
Character string specifying the column name in
metadatacontaining the Y spatial coordinates.- CellType
Character string specifying the column name in
metadatacontaining the cell type annotations.- spotCoord
A data frame containing the spatial coordinates (
XandY) of each spatial neighborhood, with neighborhood identifiers as row names. If NULL, a regular grid of spatial neighborhoods is generated automatically, spaced `grid.size` apart.- k
Integer specifying the number of nearest spatial neighbors for constructing metacells.
- radius
Numeric value specifying the radius (in units of spatial coordinates) within which neighboring cells are considered.
- grid.size
numeric, spacing between adjacent SN centers when `spotCoord` is auto-generated.
- min.cells.per.region
Integer specifying the minimum number of cells required in each spatial neighborhood to compute metacells.
- ncores
Integer specifying the number of CPU cores to use for parallel processing (default: 4).
Value
A matrix containing the spatial metacell expression profiles, with rows representing individual genes and columns representing metacells.
Examples
library(data.table)
library(Seurat)
library(SpatialEcoTyper)
scdata <- fread("https://spatialecotyper.stanford.edu/inc/inc.public.vignettes.php?file=Melanoma1_subset_counts.tsv.gz",
sep = "\t",header = TRUE, data.table = FALSE)
rownames(scdata) <- scdata[, 1]
scdata <- as.matrix(scdata[, -1])
normdata = NormalizeData(scdata)
scmeta <- read.table("https://spatialecotyper.stanford.edu/inc/inc.public.vignettes.php?file=Melanoma1_subset_scmeta.tsv",
sep = "\t",header = TRUE, row.names = 1)
# Construct spatial metacells around B cells
snmeta = scmeta[scmeta$CellType=="B", c("X", "Y")]
metacells <- GetSpatialMetacells(normdata = normdata,
metadata = scmeta,
X = "X", Y = "Y",
CellType = "CellType",
spotCoord = snmeta)
head(metacells[, 1:10])
#> 6 x 10 Matrix of class "dgeMatrix"
#> HumanMelanomaPatient1__cell_4572..B
#> PDK4 0
#> TNFRSF17 0
#> ICAM3 0
#> FAP 0
#> GZMB 0
#> TSC2 0
#> HumanMelanomaPatient1__cell_4599..B
#> PDK4 0.000000
#> TNFRSF17 0.000000
#> ICAM3 3.585688
#> FAP 0.000000
#> GZMB 0.000000
#> TSC2 0.000000
#> HumanMelanomaPatient1__cell_4634..B
#> PDK4 0.000000
#> TNFRSF17 0.000000
#> ICAM3 2.297794
#> FAP 0.000000
#> GZMB 0.000000
#> TSC2 0.000000
#> HumanMelanomaPatient1__cell_4647..B
#> PDK4 0.0000000
#> TNFRSF17 0.9508847
#> ICAM3 4.6057927
#> FAP 0.0000000
#> GZMB 0.0000000
#> TSC2 0.9333217
#> HumanMelanomaPatient1__cell_4648..B
#> PDK4 0.000000
#> TNFRSF17 0.000000
#> ICAM3 3.330895
#> FAP 0.000000
#> GZMB 0.000000
#> TSC2 0.000000
#> HumanMelanomaPatient1__cell_4652..B
#> PDK4 0.000000
#> TNFRSF17 0.000000
#> ICAM3 3.661183
#> FAP 0.000000
#> GZMB 0.000000
#> TSC2 0.000000
#> HumanMelanomaPatient1__cell_4694..B
#> PDK4 0.000000
#> TNFRSF17 0.000000
#> ICAM3 2.143066
#> FAP 0.000000
#> GZMB 0.000000
#> TSC2 0.000000
#> HumanMelanomaPatient1__cell_4723..B
#> PDK4 0.0000000
#> TNFRSF17 0.6030263
#> ICAM3 1.9151089
#> FAP 0.0000000
#> GZMB 2.6278250
#> TSC2 0.0000000
#> HumanMelanomaPatient1__cell_4728..B
#> PDK4 0.000000
#> TNFRSF17 0.000000
#> ICAM3 2.713943
#> FAP 0.000000
#> GZMB 0.000000
#> TSC2 0.000000
#> HumanMelanomaPatient1__cell_4732..B
#> PDK4 0.000000
#> TNFRSF17 1.137039
#> ICAM3 2.299928
#> FAP 0.000000
#> GZMB 0.000000
#> TSC2 0.000000