View source: R/filterSpatialContext.R
filterSpatialContext | R Documentation |
Function to filter detected spatial contexts (SCs) based on a user-defined threshold for number of group entries and/or cells.
filterSpatialContext(
object,
entry = "spatial_context",
group_by = "sample_id",
group_threshold = NULL,
cells_threshold = NULL,
name = "spatial_context_filtered"
)
object |
a |
entry |
a single character specifying the |
group_by |
a single character indicating the |
group_threshold |
a single numeric specifying the minimum number of group entries in which a SC is detected. |
cells_threshold |
a single numeric specifying the minimum total number of cells in a SC. |
name |
a single character specifying the name of the output saved in
|
returns an object of class(object)
containing a new column
entry to colData(object)[[name]]
and a new data.frame
entry to
metadata(object)[["filterSpatialContext"]]
containing the group and
cell counts per SC.
Lasse Meyer (lasse.meyer@uzh.ch)
detectSpatialContext
for the function to detect
spatial contexts
plotSpatialContext
for the function to plot
spatial context graphs
set.seed(22)
library(cytomapper)
data(pancreasSCE)
## 1. Cellular neighborhood (CN)
sce <- buildSpatialGraph(pancreasSCE, img_id = "ImageNb",
type = "knn",
name = "knn_cn_graph",
k = 5)
sce <- aggregateNeighbors(sce, colPairName = "knn_cn_graph",
aggregate_by = "metadata",
count_by = "CellType",
name = "aggregatedCellTypes")
cur_cluster <- kmeans(sce$aggregatedCellTypes, centers = 3)
sce$cellular_neighborhood <- factor(cur_cluster$cluster)
plotSpatial(sce, img_id = "ImageNb",
colPairName = "knn_cn_graph",
node_color_by = "cellular_neighborhood",
scales = "free")
## 2. Spatial context (SC)
sce <- buildSpatialGraph(sce, img_id = "ImageNb",
type = "knn",
name = "knn_sc_graph",
k = 15)
sce <- aggregateNeighbors(sce, colPairName = "knn_sc_graph",
aggregate_by = "metadata",
count_by = "cellular_neighborhood",
name = "aggregatedNeighborhood")
# Detect spatial context
sce <- detectSpatialContext(sce, entry = "aggregatedNeighborhood",
threshold = 0.9)
plotSpatial(sce, img_id = "ImageNb",
colPairName = "knn_sc_graph",
node_color_by = "spatial_context",
scales = "free")
# Filter spatial context
# By group
sce <- filterSpatialContext(sce, group_by = "ImageNb",
group_threshold = 2)
plotSpatial(sce, img_id = "ImageNb",
colPairName = "knn_sc_graph",
node_color_by = "spatial_context_filtered",
scales = "free")
# By cells
sce <- filterSpatialContext(sce, group_by = "ImageNb",
cells_threshold = 15)
plotSpatial(sce, img_id = "ImageNb",
colPairName = "knn_sc_graph",
node_color_by = "spatial_context_filtered",
scales = "free")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.