Skip to content

Commit

Permalink
fix supercell/azimuth
Browse files Browse the repository at this point in the history
  • Loading branch information
ivokwee committed Nov 1, 2024
1 parent 41484cf commit c0026ff
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions R/pgx-singlecell.R
Original file line number Diff line number Diff line change
Expand Up @@ -465,46 +465,46 @@ pgx.supercell <- function(counts, meta, group = NULL, gamma = 20) {
## require(SuperCell)

X <- log2(1 + edgeR::cpm(counts) / 100)
if (is.null(group) && "group" %in% colnames(meta)) {
if (!is.null(group) && "group" %in% colnames(meta)) {
cat("using group detected in meta\n")
group <- meta[, "group"]
}

SC <- SuperCell::SCimplify(X,
gamma = gamma,
n.var.genes = 1000,
## cell.annotation = group,
cell.split.condition = group
)

meta <- as.data.frame(meta)
dsel <- which(sapply(meta, class) %in% c("factor", "character", "logical"))
group.argmax <- function(x) tapply(x, SC$membership, function(x) names(which.max(table(x))))
dmeta <- apply(meta[, dsel, drop = FALSE], 2, function(x) as.character(group.argmax(x)))
rownames(dmeta) <- sort(unique(SC$membership))

csel <- which(sapply(meta, class) %in% c("numeric", "integer"))
group.mean <- function(x) tapply(x, SC$membership, function(x) mean(x, na.rm = TRUE))
cmeta <- apply(meta[, csel, drop = FALSE], 2, function(x) group.mean(x))

sc.meta <- data.frame(dmeta)
if (length(csel) > 0) sc.meta <- cbind(sc.meta, cmeta)
ii <- setdiff(match(colnames(meta), colnames(sc.meta)), NA)
sc.meta <- sc.meta[, ii]

## Compute metacall expression as sum of counts
sc.counts <- SuperCell::supercell_GE(counts, mode = "sum", groups = SC$membership)
sc.membership <- paste0("mc", SC$membership)
colnames(sc.counts) <- paste0("mc", 1:ncol(sc.counts))
rownames(sc.meta) <- colnames(sc.counts)

list(counts = sc.counts, meta = sc.meta, membership = sc.membership)
}

#' @export
pgx.supercellX <- function(X, meta, group = NULL, gamma = 20) {
## require(SuperCell)

if (is.null(group) && "group" %in% colnames(meta)) {
if (!is.null(group) && "group" %in% colnames(meta)) {
cat("using group detected in meta\n")
group <- meta[, "group"]
}
Expand Down Expand Up @@ -625,12 +625,17 @@ pgx.createSeuratObject <- function(counts, samples, sct = TRUE) {
obj
}


#' @export
pgx.runAzimuth <- function(counts, reference = "pbmcref") {
require(Seurat)
options(future.globals.maxSize= 4*1024^3) ## needed
obj <- pgx.createSeuratObject(counts, samples = NULL, sct = FALSE)
obj1 <- Azimuth::RunAzimuth(obj, reference = reference, verbose = FALSE)
k1 <- !(colnames(obj1@meta.data) %in% colnames(obj@meta.data))
k2 <- !grepl("score$|refAssay$", colnames(obj1@meta.data))
obj1@meta.data[, (k1 & k2)]
meta1 <- obj1@meta.data[, (k1 & k2)]
return(meta1)
}


Expand Down

0 comments on commit c0026ff

Please sign in to comment.