
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,
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.- 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.
- 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).
- bin
Character string specifying the column name in
metadatacontaining the spatial neighborhood identifier.- bin.X
Character string specifying the column name in
metadatacontaining the X spatial coordinates of spatial neighborhoods.- bin.Y
Character string specifying the column name in
metadatacontaining the Y spatial coordinates of spatial neighborhoods.
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 from single-cell spatial data
metacells <- GetSpatialMetacells(normdata = normdata, metadata = scmeta,
X = "X", Y = "Y", CellType = "CellType")
#> Error in spotCoord %>% group_by(SpotID) %>% summarize(X = median(X), Y = median(Y)) %>% as.data.frame: could not find function "%>%"
head(metacells)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'head': object 'metacells' not found