## nolint start
suppressPackageStartupMessages({
    library(goalie)
    library(basejump)
    library(dplyr)
    library(pointillism)
})
prepareTemplate()
source("_setup.R")
## nolint end

Load Seurat object

file <- params[["seurat_file"]]
object <- import(file)
name <- basenameSansExt(file)
rm(file)
assert(
    is(object, "Seurat"),
    is.character(name)
)
invisible(validObject(object))
print(object)
idents <- levels(Idents(object))
interestingGroups <- interestingGroups(object)
if (is.null(interestingGroups)) {
    interestingGroups <- "sampleName"
}

UMAP (all clusters) {.tabset}

groups <- unique(c("ident", "sampleName", interestingGroups))
invisible(lapply(
    X = groups,
    FUN = function(group) {
        markdownHeader(group, level = 2L, asis = TRUE)
        show(plotUmap(object, interestingGroups = group))
    }
))
rm(groups)

UMAP per cluster {.tabset}

groups <- unique(c("sampleName", interestingGroups))
invisible(lapply(
    X = idents,
    FUN = function(ident) {
        subset <- subset(object, idents = ident)
        markdownHeader(ident, level = 2L, asis = TRUE, tabset = TRUE)
        invisible(lapply(groups, function(group) {
            markdownHeader(group, level = 3L, asis = TRUE)
            show(plotUmap(subset, interestingGroups = group))
        }))
    }
))
rm(groups)

Cell counts per cluster {.tabset}

invisible(lapply(idents, function(ident) {
    markdownHeader(ident, level = 2L, asis = TRUE, tabset = TRUE)
    subset <- subset(object, idents = ident)
    master <- subset[[]]
    assert(is.data.frame(master))
    master <- as(master, "DataFrame")
    master[["ident"]] <- Idents(subset)
    if (!isSubset("sampleName", colnames(master))) {
        master[["sampleName"]] <- as.factor("unknown")
    }
    master <- uniteInterestingGroups(
        object = master,
        interestingGroups = interestingGroups
    )
    sampleStats <-
        master |>
        as_tibble() |>
        arrange(sampleName) |>
        group_by(sampleName) |>
        summarize(count = n()) |>
        mutate(pct = signif(x = count / sum(count), digits = 2L))
    show(sampleStats)
    intgroupStats <-
        master |>
        as_tibble() |>
        arrange(interestingGroups) |>
        group_by(interestingGroups) |>
        summarize(count = n()) |>
        mutate(pct = signif(x = count / sum(count), digits = 2L))
    show(intgroupStats)
}))



steinbaugh/pointillism documentation built on Oct. 13, 2023, 10:43 p.m.