Title: | Spatial cell-type inter-correlation by density in R |
---|---|
Description: | scider is a user-friendly R package providing functions to model the global density of cells in a slide of spatial transcriptomics data. All functions in the package are built based on the SpatialExperiment object, allowing integration into various spatial transcriptomics-related packages from Bioconductor. After modelling density, the package allows for serveral downstream analysis, including colocalization analysis, boundary detection analysis and differential density analysis. |
Authors: | Ning Liu [aut] , Mengbo Li [aut] , Yunshun Chen [aut, cre] |
Maintainer: | Yunshun Chen <[email protected]> |
License: | GPL-3 + file LICENSE |
Version: | 1.5.0 |
Built: | 2024-11-19 04:13:24 UTC |
Source: | https://github.com/bioc/scider |
Annotate all cells with contour level of cell type-specific density.
allocateCells(spe, to.roi = TRUE, to.contour = TRUE)
allocateCells(spe, to.roi = TRUE, to.contour = TRUE)
spe |
A SpatialExperiment object. |
to.roi |
Logical. Whether to allocate cells to ROIs. |
to.contour |
Logical. Whether to allocate cells to contour levels. |
A SpatialExperiment object. An extra column is added to the colData.
data("xenium_bc_spe") spe <- gridDensity(spe) coi <- "Breast cancer" spe <- findROI(spe, coi = coi) spe <- allocateCells(spe)
data("xenium_bc_spe") spe <- gridDensity(spe) coi <- "Breast cancer" spe <- findROI(spe, coi = coi) spe <- allocateCells(spe)
Check which cells are in which regions
cellsInRegion(spe, region, name_to, NA_level = "0", levels = NULL)
cellsInRegion(spe, region, name_to, NA_level = "0", levels = NULL)
spe |
A SpatialExperiment object. |
region |
List or an sf object that represents a region or an ROI. |
name_to |
Colname in colData(spe) to store the annotation. |
NA_level |
Label for cells not falling in any of the regions. Default to 0. |
levels |
Factor levels. |
A SpatialExperiment object. The region information of each cell is stored in the colData.
Perform kernel density estimation on SpatialExperiment
computeDensity( spe, mode = "pixels", kernel = "gaussian", bandwidth = NULL, weights = NULL, ngrid.x = 100, ngrid.y = NULL, grid.length.x = NULL, grid.length.y = NULL, xlim = NULL, ylim = NULL, diggle = FALSE )
computeDensity( spe, mode = "pixels", kernel = "gaussian", bandwidth = NULL, weights = NULL, ngrid.x = 100, ngrid.y = NULL, grid.length.x = NULL, grid.length.y = NULL, xlim = NULL, ylim = NULL, diggle = FALSE )
spe |
A SpatialExperiment object. |
mode |
Choose either points or pixels. Specifying whether to compute the density at a grid pixel location or at at point. |
kernel |
The smoothing kernel. Options are gaussian, epanechnikov, quartic or disc. |
bandwidth |
The smoothing bandwidth. By default performing automatic bandwidth selection using cross-validation using function spatstat.explore::bw.diggle. |
weights |
Optional weights to be attached to the points. |
ngrid.x |
Number of grids in the x-direction. Default to 100. |
ngrid.y |
Number of grids in the y-direction. |
grid.length.x |
Grid length in the x-direction. |
grid.length.y |
Grid length in the y-direction. |
xlim |
The range of the x-coordinates of the image. |
ylim |
The range of the y-coordinates of the image. |
diggle |
Logical. If TRUE, use the Jones-Diggle improved edge correction. See spatstat.explore::density.ppp() for details. |
Output from spatstat.explore::density.ppp.
Draw a contour region on some density level
contour2sf(spe, contour, coi, cutoff)
contour2sf(spe, contour, coi, cutoff)
spe |
A SpatialExperiment object. |
contour |
Name in metadata. |
coi |
A character vector of cell types of interest (COIs). |
cutoff |
A numeric scalar specifying the density cutoff. |
An sf object of the contour region of the specified level.
Test for density correlation between two cell types.
corDensity(spe, by.roi = TRUE)
corDensity(spe, by.roi = TRUE)
spe |
A SpatialExperiment object. |
by.roi |
Logical. If TRUE (default), then return the testing results at ROI level. If FALSE, then combine the testing results across all ROIs. |
A DataFrame containing the testing results.
data("xenium_bc_spe") coi <- c("Breast cancer", "Fibroblasts", "B cells", "T cells") spe <- gridDensity(spe, coi = coi) spe <- findROI(spe, coi = coi, method = "walktrap") result <- corDensity(spe)
data("xenium_bc_spe") coi <- c("Breast cancer", "Fibroblasts", "B cells", "T cells") spe <- gridDensity(spe, coi = coi) spe <- findROI(spe, coi = coi, method = "walktrap") result <- corDensity(spe)
Find ROIs based on cell type-specific densities via graph-based method.
findROI( spe, coi, probs = 0.85, ngrid.min = 20, method = "walktrap", diag.nodes = FALSE, sequential.roi.name = TRUE, ... )
findROI( spe, coi, probs = 0.85, ngrid.min = 20, method = "walktrap", diag.nodes = FALSE, sequential.roi.name = TRUE, ... )
spe |
A SpatialExperiment object. |
coi |
A character vector of cell types of interest (COIs). |
probs |
A numeric scalar. The threshold of proportion that used to filter grid by density. Default to 0.85. |
ngrid.min |
An integer. The minimum number of grids required for defining a ROI. Default to 20. |
method |
The community dectection method to be used, either walktrap or connected. Default to walktrap. |
diag.nodes |
Logical. Set this to TRUE to allow diagonal grid points to be adjacent nodes. |
sequential.roi.name |
Logical. Set this to FALSE if you want the original ROI name before filtering are retained. |
... |
Other parameters that passed to walktrap.community. |
A SpatialExperiment object.
data("xenium_bc_spe") coi <- c("Breast cancer", "Fibroblasts") spe <- gridDensity(spe, coi = coi) spe <- findROI(spe, coi = coi, method = "walktrap")
data("xenium_bc_spe") coi <- c("Breast cancer", "Fibroblasts") spe <- gridDensity(spe, coi = coi) spe <- findROI(spe, coi = coi, method = "walktrap")
Get contour from density
getContour(spe, coi, bins = NULL, binwidth = NULL, breaks = NULL)
getContour(spe, coi, bins = NULL, binwidth = NULL, breaks = NULL)
spe |
A SpatialExperiment object. |
coi |
A character vector of cell types of interest (COIs). |
bins |
An integer. Number of contour levels. |
binwidth |
A numeric scale of the smoothing bandwidth. |
breaks |
A numeric scale referring to the breaks in
|
A SpatialExperiment object. An sf object of the contour region of the specified level is stored in the metadata of the SpatialExperiment object.
data("xenium_bc_spe") spe <- gridDensity(spe) coi <- "Breast cancer" spe <- getContour(spe, coi = coi)
data("xenium_bc_spe") spe <- gridDensity(spe) coi <- "Breast cancer" spe <- getContour(spe, coi = coi)
Calculate areas between every two density levels
getContourRegions(spe, coi)
getContourRegions(spe, coi)
spe |
A SpatialExperiment object. |
coi |
A character vector of cell types of interest (COIs). |
A list of sf objects, each representing the region between two contour density levels.
Combine grids in each ROI to a sf region
grid2sf(spe)
grid2sf(spe)
spe |
A SpatialExperiment object. |
List of ROIs saved as sf objects.
Perform kernel density estimation on SpatialExperiment for cell types of interest
gridDensity( spe, coi = NULL, id = "cell_type", kernel = "gaussian", bandwidth = NULL, ngrid.x = 100, ngrid.y = NULL, grid.length.x = NULL, grid.length.y = NULL, diggle = FALSE )
gridDensity( spe, coi = NULL, id = "cell_type", kernel = "gaussian", bandwidth = NULL, ngrid.x = 100, ngrid.y = NULL, grid.length.x = NULL, grid.length.y = NULL, diggle = FALSE )
spe |
A SpatialExperiment object. |
coi |
A character vector of cell types of interest (COIs). Default to all cell types. |
id |
A character. The name of the column of colData(spe) containing the cell type identifiers. Set to cell_type by default. |
kernel |
The smoothing kernel. Options are "gaussian", "epanechnikov", "quartic" or "disc". |
bandwidth |
The smoothing bandwidth. By default performing automatic bandwidth selection using cross-validation using function spatstat.explore::bw.diggle. |
ngrid.x |
Number of grids in the x-direction. Default to 100. |
ngrid.y |
Number of grids in the y-direction. |
grid.length.x |
Grid length in the x-direction. |
grid.length.y |
Grid length in the y-direction. |
diggle |
Logical. If TRUE, use the Jones-Diggle improved edge correction. See spatstat.explore::density.ppp() for details. |
A SpatialExperiment object. Grid density estimates for all cell type of interest are stored in spe@metadata$grid_density. Grid information is stored in spe@metadata$grid_info
data("xenium_bc_spe") spe <- gridDensity(spe)
data("xenium_bc_spe") spe <- gridDensity(spe)
Manually merge ROIs
mergeROI(spe, merge.list, id = "component", rename = FALSE)
mergeROI(spe, merge.list, id = "component", rename = FALSE)
spe |
A SpatialExperiment object. |
merge.list |
A (named) list of vectors of ROI ids to be merged. Each vector in the list should be of length greater than or equal to 2. If no name is specified, the merged ROI will be named by concatenating ROIs being merged. |
id |
Character. The name of the column in |
rename |
Logical. If TRUE, names of merge.list are ignored. ROIs will be given a new name. For the unmerged ROIs, their new names are not necessarily the same as those before merging. |
A SpatialExperiment object.
data("xenium_bc_spe") coi <- c("Breast cancer", "Fibroblasts") spe <- gridDensity(spe, coi = coi) spe <- findROI(spe, coi = coi, method = "walktrap") spe <- mergeROI(spe, list("1-2" = 1:2))
data("xenium_bc_spe") coi <- c("Breast cancer", "Fibroblasts") spe <- gridDensity(spe, coi = coi) spe <- findROI(spe, coi = coi, method = "walktrap") spe <- mergeROI(spe, list("1-2" = 1:2))
Plot cell type composition in each density level of cell of interest.
plotCellCompo( spe, coi, id = "cell_type", level.name = paste0(janitor::make_clean_names(coi), "_contour"), by.roi = FALSE )
plotCellCompo( spe, coi, id = "cell_type", level.name = paste0(janitor::make_clean_names(coi), "_contour"), by.roi = FALSE )
spe |
A SpatialExperiment object. |
coi |
A character vector of cell types of interest (COIs). |
id |
A character. The name of the column of colData(spe) containing the cell type identifiers. Set to cell_type by default. |
level.name |
The column name that generated by cellAssign. |
by.roi |
Logical. Default is FALSE, set to TRUE to allow plotting by ROI. |
A ggplot object.
data("xenium_bc_spe") spe <- gridDensity(spe, coi = c("Breast cancer", "Fibroblasts")) spe <- findROI(spe, coi = c("Breast cancer", "Fibroblasts")) spe <- getContour(spe, coi = "Breast cancer") spe <- allocateCells(spe) plotCellCompo(spe, coi = "Breast cancer") plotCellCompo(spe, coi = "Breast cancer", by.roi = TRUE)
data("xenium_bc_spe") spe <- gridDensity(spe, coi = c("Breast cancer", "Fibroblasts")) spe <- findROI(spe, coi = c("Breast cancer", "Fibroblasts")) spe <- getContour(spe, coi = "Breast cancer") spe <- allocateCells(spe) plotCellCompo(spe, coi = "Breast cancer") plotCellCompo(spe, coi = "Breast cancer", by.roi = TRUE)
Plot contour lines.
plotContour( spe, coi, overlay = c("cell", "density"), id = "cell_type", sub.level = NULL, ... )
plotContour( spe, coi, overlay = c("cell", "density"), id = "cell_type", sub.level = NULL, ... )
spe |
A SpatialExperiment object. |
coi |
A character vector of length 1 of the cell type of interest. |
overlay |
Character vector. Either plot overlay on density or cell. By default is cell. |
id |
A character. The name of the column of colData(spe) containing the cell type identifiers. Set to cell_type by default. |
sub.level |
Character vector. Subset on specific level. |
... |
Aesthetic mappings to pass to |
A ggplot object.
data("xenium_bc_spe") spe <- gridDensity(spe) coi <- "Breast cancer" spe <- getContour(spe, coi = coi) plotContour(spe, coi = coi, size = 0.3, alpha = 0.2)
data("xenium_bc_spe") spe <- gridDensity(spe) coi <- "Breast cancer" spe <- getContour(spe, coi = coi) plotContour(spe, coi = coi, size = 0.3, alpha = 0.2)
Visualising an sf object (for internal use only at the moment)
plotContourRegion( spe, coi, id = "cell_type", overlay = c("density", "cell"), sub.level )
plotContourRegion( spe, coi, id = "cell_type", overlay = c("density", "cell"), sub.level )
spe |
A SpatialExperiment object. |
coi |
A character vector of length 1 of the cell type of interest. |
id |
A character. The name of the column of colData(spe) containing the cell type identifiers. Set to cell_type by default. |
overlay |
Character vector. Either plot overlay on density or cells. |
sub.level |
Numeric vector of length 1 or 2, identifies which density level to plot. When length is 1, plot the density region above this level. When length is 2, plot the density region between the two levels. |
A ggplot object.
Plot model statistics using heatmap.
plotCorHeatmap( model.result, stats = c("cor.coef", "t", "p.Pos", "p.Neg"), roi = "all", cell.type = "all" )
plotCorHeatmap( model.result, stats = c("cor.coef", "t", "p.Pos", "p.Neg"), roi = "all", cell.type = "all" )
model.result |
A data.frame object. |
stats |
Character value. Choose either coefficient or t. Coefficient by default. |
roi |
Character value. By default is all. The specific ROIs to be plotted. |
cell.type |
Character value. By default is all. The cell types to be plotted. |
A pheatmap object.
data("xenium_bc_spe") coi <- c("Breast cancer", "Fibroblasts", "B cells", "T cells") spe <- gridDensity(spe, coi = coi) spe <- findROI(spe, coi = coi, method = "walktrap") model_result <- corDensity(spe) plotCorHeatmap(model_result)
data("xenium_bc_spe") coi <- c("Breast cancer", "Fibroblasts", "B cells", "T cells") spe <- gridDensity(spe, coi = coi) spe <- findROI(spe, coi = coi, method = "walktrap") model_result <- corDensity(spe) plotCorHeatmap(model_result)
Plot density correlation between two cell types
plotDensCor( spe, celltype1 = NULL, celltype2 = NULL, by.roi = TRUE, fit = c("spline", "linear"), df = 3, ... )
plotDensCor( spe, celltype1 = NULL, celltype2 = NULL, by.roi = TRUE, fit = c("spline", "linear"), df = 3, ... )
spe |
A SpatialExperiment object. |
celltype1 |
Cell type 1 to compare. |
celltype2 |
Cell type 2 to compare. |
by.roi |
Logical. Plot facet by ROIs or not. |
fit |
Character. Options are "spline" and "linear". |
df |
Integer. Degrees of freedom of the spline fit. Default to 3 (i.e., a cubic spline fit). |
... |
aesthetic mappings to pass to |
A ggplot object.
data("xenium_bc_spe") coi <- c("Breast cancer", "Fibroblasts") spe <- gridDensity(spe, coi = coi) spe <- findROI(spe, coi = coi, method = "walktrap") plotDensCor(spe, celltype1 = "Breast cancer", celltype2 = "Fibroblasts")
data("xenium_bc_spe") coi <- c("Breast cancer", "Fibroblasts") spe <- gridDensity(spe, coi = coi) spe <- findROI(spe, coi = coi, method = "walktrap") plotDensCor(spe, celltype1 = "Breast cancer", celltype2 = "Fibroblasts")
Plot grid-based density.
plotDensity(spe, coi, probs = 0.8)
plotDensity(spe, coi, probs = 0.8)
spe |
A SpatialExperiment object. |
coi |
A character vector of cell types of interest (COIs). |
probs |
Numeric value between 0 and 1, used for filtering uninformative grid, default is 0.8. |
A ggplot object.
data("xenium_bc_spe") spe <- gridDensity(spe) plotDensity(spe, coi = "Breast cancer") plotDensity(spe, coi = "Fibroblasts")
data("xenium_bc_spe") spe <- gridDensity(spe) plotDensity(spe, coi = "Breast cancer") plotDensity(spe, coi = "Fibroblasts")
Plot ROIs on spatial.
plotROI(spe, id = "cell_type", show.legend = FALSE, ...)
plotROI(spe, id = "cell_type", show.legend = FALSE, ...)
spe |
A SpatialExperiment object. |
id |
Character. The name of the column of colData(spe) containing the cell type identifiers. Set to cell_type by default. |
show.legend |
Logical. Show legend or not. |
... |
Aesthetic mappings pass for point. |
A ggplot object.
data("xenium_bc_spe") coi <- c("Breast cancer", "Fibroblasts") spe <- gridDensity(spe, coi = coi) spe <- findROI(spe, coi = coi, method = "walktrap", steps = 5) plotROI(spe, size = 0.3, alpha = 0.2)
data("xenium_bc_spe") coi <- c("Breast cancer", "Fibroblasts") spe <- gridDensity(spe, coi = coi) spe <- findROI(spe, coi = coi, method = "walktrap", steps = 5) plotROI(spe, size = 0.3, alpha = 0.2)
Plot cells based on spatial coordinates.
plotSpatial(spe, reverseY = FALSE, n = 30, ...)
plotSpatial(spe, reverseY = FALSE, n = 30, ...)
spe |
A SpatialExperiment object. |
reverseY |
Reverse y coordinates. |
n |
Integer value. The number of distinct color to be generated, default is 30. |
... |
Aesthetic mappings to pass to |
A ggplot object.
data("xenium_bc_spe") plotSpatial(spe, shape = ".", color = cell_type, size = 0.3, alpha = 0.2)
data("xenium_bc_spe") plotSpatial(spe, shape = ".", color = cell_type, size = 0.3, alpha = 0.2)
Merge sel_region from the selectRegion function to SpatialExperiment.
postSelRegion(spe, sel_region)
postSelRegion(spe, sel_region)
spe |
A SpatialExperiment object. |
sel_region |
A dataframe object. Can be generated from function selectRegion. |
A SpatialExperiment object.
data("xenium_bc_spe") coi <- c("Breast cancer", "Fibroblasts", "B cells", "T cells") spe <- gridDensity(spe, coi = coi) sel_region <- data.frame( "node" = seq(10), "node_x" = seq(10), "node_y" = seq(10) ) spe1 <- postSelRegion(spe, sel_region)
data("xenium_bc_spe") coi <- c("Breast cancer", "Fibroblasts", "B cells", "T cells") spe <- gridDensity(spe, coi = coi) sel_region <- data.frame( "node" = seq(10), "node_x" = seq(10), "node_y" = seq(10) ) spe1 <- postSelRegion(spe, sel_region)
Select region of interest from plot
selectRegion(data, x.col = "x", y.col = "y")
selectRegion(data, x.col = "x", y.col = "y")
data |
A data.frame object. |
x.col |
Column name of the x coordinates. |
y.col |
Column name of the y coordinates. |
A data.frame object in the global environment.
data("xenium_bc_spe") spe_b <- spe[, SummarizedExperiment::colData(spe)$cell_type == "B cells"] dat <- as.data.frame(SpatialExperiment::spatialCoords(spe_b)) # selectRegion(dat, x.col = "x_centroid", y.col = "y_centroid")
data("xenium_bc_spe") spe_b <- spe[, SummarizedExperiment::colData(spe)$cell_type == "B cells"] dat <- as.data.frame(SpatialExperiment::spatialCoords(spe_b)) # selectRegion(dat, x.col = "x_centroid", y.col = "y_centroid")
Given a 'SpatialExperiment' data object, create pseudo-bulk samples using the colData information and return a DGEList object
spe2PB( spe, by.group = TRUE, group.id = "cell_type", by.roi = TRUE, roi.only = TRUE, contour = NULL )
spe2PB( spe, by.group = TRUE, group.id = "cell_type", by.roi = TRUE, roi.only = TRUE, contour = NULL )
spe |
A SpatialExperiment object. |
by.group |
Logical. Whether to perform pseudo-bulking by group. TRUE by default. |
group.id |
Character. The column name of the colData(spe) that contains the group information. Default to 'cell_type'. |
by.roi |
Logical. Whether to perform pseudo-bulking by ROI. TRUE by default. |
roi.only |
Logical. Whether to filter out pseudo-bulk samples formed by cells not in any ROIs. TRUE by default. |
contour |
Character. The name of the group or cell type on which the contour level is computed. If NULL, then no pseudo-bulking will be performed based on contour level. Default to NULL. |
An edgeR::DGEList object where each library (column) is a pseudo-bulk sample.
data("xenium_bc_spe") spe <- gridDensity(spe) coi <- "Breast cancer" spe <- findROI(spe, coi = coi) spe <- allocateCells(spe) y <- spe2PB(spe)
data("xenium_bc_spe") spe <- gridDensity(spe) coi <- "Breast cancer" spe <- findROI(spe, coi = coi) spe <- allocateCells(spe) y <- spe2PB(spe)