Function to rasterize cell type labels in spatially-resolved omics data represented as SpatialExperiment class.
This function assumes that the input is provided as a SpatialExperiment
object or a list
of SpatialExperiment
objects.
Usage
rasterizeCellType(
input,
col_name,
resolution = 100,
square = TRUE,
fun = "sum",
n_threads = 1,
BPPARAM = NULL,
verbose = FALSE
)
Arguments
- input
SpatialExperiment
orlist
: Input data represented as aSpatialExperiment
orlist
ofSpatialExperiment
. EachSpatialExperiment
is assumed to have acolData
slot containing cell type labels for observations as a data frame column and aspatialCoords
slot containing spatial x,y coordinates of observations as matrix array. Further, x,y coordinates are assumed to be stored in column 1 and 2 ofspatialCoords
.- col_name
character
: Column name of thecolData
object containing cell type labels for observations. If the input is alist
, col_name is assumed to be present in all elements (SpatialExperiment
) of the input.- resolution
integer
ordouble
: Resolution refers to the side length of each pixel for square pixels and the distance between opposite edges of each pixel for hexagonal pixels. The unit of this parameter is assumed to be the same as the unit of spatial coordinates of the input data.- square
logical
: If TRUE (default), rasterize into square pixels. If FALSE, rasterize into hexagonal pixels.- fun
character
: If "mean", pixel value for each pixel would be the proportion of each cell type based on the one-hot-encoded cell type labels for all cells within the pixel. If "sum", pixel value for each pixel would be the number of cells of each cell type based on the one-hot-encoded cell type labels for all cells within the pixel.- n_threads
integer
: Number of threads for parallelization. Default = 1. Inputting this argument when theBPPARAM
argument is missing would set parallel exeuction back-end to beBiocParallel::MulticoreParam(workers = n_threads)
. We recommend setting this argument to be the number of cores available (parallel::detectCores(logical = FALSE)
). IfBPPARAM
argument is not missing, theBPPARAM
argument would overriden_threads
argument.- BPPARAM
BiocParallelParam
: Optional additional argument for parallelization. This argument is provided for advanced users ofBiocParallel
for further flexibility for setting up parallel-execution back-end. Default is NULL. If provided, this is assumed to be an instance ofBiocParallelParam
.- verbose
logical
: Whether to display verbose output or warning. Default is FALSE
Value
If the input was given as SpatialExperiment
, the output is returned
as a new SpatialExperiment
object with assay
slot containing the
feature (cell types) x observations (pixels) matrix (dgCmatrix), spatialCoords
slot containing spatial x,y coordinates of pixel centroids, and colData
slot
containing meta data for pixels (number of cells that were aggregated in each pixel,
cell IDs of cells that were aggregated in each pixel, pixel type based on the
square
argument, pixel resolution based on the resolution
argument,
pixel geometry as sfc_POLYGON
). If the input was provided as list
of SpatialExperiment
, the output is returned as a new list
of
SpatialExperiment
containing information described above for corresponding
SpatialExperiment
. Further, names(input)
is inherited in the output.
Examples
library(SpatialExperiment)
data("merfish_mousePOA")
# check assay names for this particular SpatialExperiment object (you can see
# that cell-type labels are stored in the "celltype" column)
head(colData(merfish_mousePOA))
#> DataFrame with 6 rows and 4 columns
#> bregma celltype neurontype
#> <numeric> <character> <character>
#> 6d6b1d59-6f3b-4a9d-b5a4-8c8b073ae025 -0.29 OD Mature 2
#> 76200644-c14a-4cfa-8752-2a02e5f10d20 -0.29 OD Immature 1
#> 6b08ca36-b395-415a-bb34-d7b67550c35d -0.29 Inhibitory I-7
#> b9cb9cfb-fff7-426e-8c36-18fe428ca156 -0.29 Excitatory E-13
#> 982cc0fc-6d11-4dc4-9ffc-c8c0cee48e6d -0.29 OD Mature 2
#> ee13ce4c-adf8-4602-9a21-23fdf91d28e0 -0.29 Inhibitory I-7
#> sample_id
#> <character>
#> 6d6b1d59-6f3b-4a9d-b5a4-8c8b073ae025 sample01
#> 76200644-c14a-4cfa-8752-2a02e5f10d20 sample01
#> 6b08ca36-b395-415a-bb34-d7b67550c35d sample01
#> b9cb9cfb-fff7-426e-8c36-18fe428ca156 sample01
#> 982cc0fc-6d11-4dc4-9ffc-c8c0cee48e6d sample01
#> ee13ce4c-adf8-4602-9a21-23fdf91d28e0 sample01
# rasterize a single SpatialExperiment object
# make sure to specify the col_name argument
out <- rasterizeCellType(merfish_mousePOA, col_name = "celltype", fun = "sum")
# rasterize a single SpatialExperiment object with user-defined resolution and hexagonal pixels
out <- rasterizeCellType(merfish_mousePOA, col_name = "celltype", resolution = 200,
square = FALSE, fun = "sum")
# rasterize a list of SpatialExperiment objects (in this case, permutated datasets
# with 3 different rotations)
spe_list <- permutateByRotation(merfish_mousePOA, n_perm = 3)
out_list <- rasterizeCellType(spe_list, col_name = "celltype", resolution = 100,
square = TRUE, fun = "sum")