From c23c2e637d43dc8cdc37bf0dc85cf5d2aa404051 Mon Sep 17 00:00:00 2001 From: LTLA Date: Sat, 14 Dec 2024 19:14:41 -0800 Subject: [PATCH] Switch to beachmat to check each row for missing values. This allows us to get rid of a lot of the BPPARAM= machinery. --- DESCRIPTION | 6 +++--- NAMESPACE | 4 +--- R/SingleR.R | 7 +------ R/classifySingleR.R | 2 +- R/combineRecomputedResults.R | 2 +- R/getClassicMarkers.R | 7 +------ R/plotMarkerHeatmap.R | 4 ++-- R/trainSingleR.R | 11 +++-------- R/utils.R | 17 +++++++---------- 9 files changed, 20 insertions(+), 40 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 549ccd3..6bbfea0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: SingleR Title: Reference-Based Single-Cell RNA-Seq Annotation -Version: 2.9.2 -Date: 2024-11-24 +Version: 2.9.3 +Date: 2024-12-14 Authors@R: c(person("Dvir", "Aran", email="dvir.aran@ucsf.edu", role=c("aut", "cph")), person("Aaron", "Lun", email="infinite.monkeys.with.keyboards@gmail.com", role=c("ctb", "cre")), person("Daniel", "Bunis", role="ctb"), @@ -23,7 +23,7 @@ Imports: stats, utils, Rcpp, - beachmat (>= 2.21.1) + beachmat (>= 2.23.5) LinkingTo: Rcpp, beachmat, diff --git a/NAMESPACE b/NAMESPACE index 4824489..f69870d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -33,10 +33,7 @@ importFrom(BiocNeighbors,AnnoyParam) importFrom(BiocNeighbors,KmknnParam) importFrom(BiocNeighbors,defineBuilder) importFrom(BiocParallel,SerialParam) -importFrom(BiocParallel,bpisup) importFrom(BiocParallel,bpnworkers) -importFrom(BiocParallel,bpstart) -importFrom(BiocParallel,bpstop) importFrom(DelayedArray,DelayedArray) importFrom(DelayedArray,colsum) importFrom(DelayedArray,getAutoBPPARAM) @@ -59,6 +56,7 @@ importFrom(S4Vectors,selfmatch) importFrom(SummarizedExperiment,SummarizedExperiment) importFrom(SummarizedExperiment,assay) importFrom(beachmat,initializeCpp) +importFrom(beachmat,tatami.row.nan.counts) importFrom(methods,as) importFrom(methods,is) importFrom(stats,mad) diff --git a/R/SingleR.R b/R/SingleR.R index 2704c56..424b249 100644 --- a/R/SingleR.R +++ b/R/SingleR.R @@ -102,14 +102,9 @@ SingleR <- function( BNPARAM = NULL, BPPARAM=SerialParam()) { - if (!bpisup(BPPARAM) && !is(BPPARAM, "MulticoreParam")) { - bpstart(BPPARAM) - on.exit(bpstop(BPPARAM)) - } - # We have to do all this row-subsetting at the start before trainSingleR, # otherwise 'test.genes' won't match up to the filtered 'test'. - test <- .to_clean_matrix(test, assay.type.test, check.missing.test, msg="test", BPPARAM=BPPARAM) + test <- .to_clean_matrix(test, assay.type.test, check.missing.test, msg="test", num.threads=num.threads) tmp.ref <- ref if (!is.list(tmp.ref) || is.data.frame(tmp.ref)) { diff --git a/R/classifySingleR.R b/R/classifySingleR.R index 2304538..6e4b015 100644 --- a/R/classifySingleR.R +++ b/R/classifySingleR.R @@ -104,7 +104,7 @@ classifySingleR <- function( num.threads = bpnworkers(BPPARAM), BPPARAM=SerialParam()) { - test <- .to_clean_matrix(test, assay.type, check.missing=FALSE, msg="test", BPPARAM=BPPARAM) + test <- .to_clean_matrix(test, assay.type, check.missing=FALSE, msg="test", num.threads=num.threads) solo <- .is_solo(trained) if (solo) { diff --git a/R/combineRecomputedResults.R b/R/combineRecomputedResults.R index 71220c1..dd4e59f 100644 --- a/R/combineRecomputedResults.R +++ b/R/combineRecomputedResults.R @@ -120,7 +120,7 @@ combineRecomputedResults <- function( num.threads = bpnworkers(BPPARAM), BPPARAM=SerialParam()) { - test <- .to_clean_matrix(test, assay.type=assay.type.test, check.missing=FALSE, msg="test", BPPARAM=BPPARAM) + test <- .to_clean_matrix(test, assay.type=assay.type.test, check.missing=FALSE, msg="test", num.threads=num.threads) # Applying the sanity checks. stopifnot(length(results) == length(trained)) diff --git a/R/getClassicMarkers.R b/R/getClassicMarkers.R index 55c05b3..02fec1d 100644 --- a/R/getClassicMarkers.R +++ b/R/getClassicMarkers.R @@ -54,18 +54,13 @@ #' @importFrom utils relist #' @importFrom beachmat initializeCpp getClassicMarkers <- function(ref, labels, assay.type="logcounts", check.missing=TRUE, de.n=NULL, num.threads=bpnworkers(BPPARAM), BPPARAM=SerialParam()) { - if (!bpisup(BPPARAM) && !is(BPPARAM, "MulticoreParam")) { - bpstart(BPPARAM) - on.exit(bpstop(BPPARAM)) - } - if (!.is_list(ref)) { ref <- list(ref) labels <- list(labels) } for (i in seq_along(ref)) { - ref[[i]] <- .to_clean_matrix(ref[[i]], assay.type, check.missing, msg="ref", BPPARAM=BPPARAM) + ref[[i]] <- .to_clean_matrix(ref[[i]], assay.type, check.missing, msg="ref", num.threads=num.threads) } # Setting up references. diff --git a/R/plotMarkerHeatmap.R b/R/plotMarkerHeatmap.R index f3acf3d..4298f77 100644 --- a/R/plotMarkerHeatmap.R +++ b/R/plotMarkerHeatmap.R @@ -81,7 +81,7 @@ plotMarkerHeatmap <- function( BPPARAM = SerialParam(), ...) { - test <- .to_clean_matrix(test, assay.type, check.missing=FALSE) + test <- .to_clean_matrix(test, assay.type, check.missing=FALSE, num.threads=num.threads) config <- configureMarkerHeatmap( results, test, @@ -129,7 +129,7 @@ configureMarkerHeatmap <- function( order.by.summary="min.rank", num.threads=1) { - test <- .to_clean_matrix(test, assay.type, check.missing=FALSE) + test <- .to_clean_matrix(test, assay.type, check.missing=FALSE, num.threads=num.threads) all.markers <- metadata(results)$de.genes[[label]] if (use.pruned) { diff --git a/R/trainSingleR.R b/R/trainSingleR.R index 5197861..21c037d 100644 --- a/R/trainSingleR.R +++ b/R/trainSingleR.R @@ -195,7 +195,7 @@ #' @export #' @importFrom S4Vectors List isSingleString metadata metadata<- #' @importFrom BiocNeighbors defineBuilder AnnoyParam KmknnParam -#' @importFrom BiocParallel SerialParam bpisup bpstart bpstop +#' @importFrom BiocParallel SerialParam #' @importFrom beachmat initializeCpp #' @importFrom S4Vectors List #' @importFrom SummarizedExperiment assay @@ -243,15 +243,10 @@ trainSingleR <- function( } } - if (!bpisup(BPPARAM) && !is(BPPARAM, "MulticoreParam")) { - bpstart(BPPARAM) - on.exit(bpstop(BPPARAM)) - } - output <- vector("list", length(ref)) names(output) <- names(ref) for (l in seq_along(ref)) { - curref <- .to_clean_matrix(ref[[l]], assay.type, check.missing, msg="ref", BPPARAM=BPPARAM) + curref <- .to_clean_matrix(ref[[l]], assay.type, check.missing, msg="ref", num.threads=num.threads) curlabels <- as.character(labels[[l]]) stopifnot(length(curlabels) == ncol(curref)) @@ -274,7 +269,7 @@ trainSingleR <- function( ) if (aggr.ref) { - aggr <- do.call(aggregateReference, c(list(ref=quote(curref), label=curlabels, check.missing=FALSE, BPPARAM=BPPARAM), aggr.args)) + aggr <- do.call(aggregateReference, c(list(ref=quote(curref), label=curlabels, check.missing=FALSE, num.threads=num.threads), aggr.args)) curref <- assay(aggr) curlabels <- aggr$label } diff --git a/R/utils.R b/R/utils.R index ad1573f..61dee77 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,8 +1,9 @@ +#' @importFrom beachmat initializeCpp tatami.row.nan.counts #' @importFrom SummarizedExperiment assay #' @importFrom DelayedMatrixStats rowAnyNAs #' @importClassesFrom SummarizedExperiment SummarizedExperiment -#' @importFrom DelayedArray DelayedArray setAutoBPPARAM getAutoBPPARAM -.to_clean_matrix <- function(x, assay.type, check.missing, msg="x", BPPARAM=SerialParam()) { +#' @importFrom DelayedArray DelayedArray +.to_clean_matrix <- function(x, assay.type, check.missing, msg="x", num.threads=1) { if (is.null(rownames(x)) && nrow(x)) { # zero-length matrices have NULL dimnames. stop(sprintf("'%s' must have row names", msg)) } @@ -20,15 +21,11 @@ # Stripping out genes with NA's from 'x'. if (check.missing) { - old <- getAutoBPPARAM() - setAutoBPPARAM(BPPARAM) - on.exit(setAutoBPPARAM(old)) - - y <- DelayedArray(x) - discard <- rowAnyNAs(y) - if (any(discard)) { + ptr <- initializeCpp(x) + keep <- tatami.row.nan.counts(ptr, num.threads=num.threads) == 0 + if (!all(keep)) { # Returning a DelayedArray to avoid making an actual subset. - x <- y[!discard,,drop=FALSE] + x <- DelayedArray(x)[keep,,drop=FALSE] } }