-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocateCells.R
72 lines (67 loc) · 1.95 KB
/
allocateCells.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#' Annotate all cells with contour level of cell type-specific density.
#'
#' @param spe A SpatialExperiment object.
#' @param to.roi Logical. Whether to allocate cells to ROIs.
#' @param to.contour Logical. Whether to allocate cells to contour levels.
#'
#' @return A SpatialExperiment object. An extra column is added to the colData.
#' @export
#'
#' @examples
#'
#' data("xenium_bc_spe")
#'
#' spe <- gridDensity(spe)
#'
#' coi <- "Breast cancer"
#'
#' spe <- findROI(spe, coi = coi)
#'
#' spe <- allocateCells(spe)
#'
allocateCells <- function(
spe,
to.roi = TRUE,
to.contour = TRUE) {
if (to.roi) {
if (is.null(spe@metadata$roi)) {
message("No ROI detected.")
} else {
message(paste(
"Assigning cells to ROIs defined by",
paste(spe@metadata$coi, collapse = ", "), "\n"
))
all_areas <- grid2sf(spe)
name_to <- "roi"
NA_level <- "None"
spe <- cellsInRegion(spe, all_areas,
name_to = name_to,
NA_level = NA_level, levels = NULL
)
}
}
if (to.contour) {
ind <- grep("_contour", names(spe@metadata))
if (length(ind) == 0) {
message("No contour detected.")
} else {
for (i in ind) {
coi <- janitor::make_clean_names(names(spe@metadata)[i],
case = "sentence", replace = c("contour" = "")
)
message(paste(
"Assigning cells to contour levels of",
coi, "\n"
))
all_areas <- getContourRegions(spe, coi = coi)
name_to <- names(spe@metadata)[i]
NA_level <- 0
spe <- cellsInRegion(spe, all_areas,
name_to = name_to,
NA_level = NA_level, levels = NULL
)
}
}
}
return(spe)
}