Skip to content

Commit

Permalink
Switch to beachmat to check each row for missing values.
Browse files Browse the repository at this point in the history
This allows us to get rid of a lot of the BPPARAM= machinery.
  • Loading branch information
LTLA committed Dec 15, 2024
1 parent 80b9923 commit afd3d99
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 40 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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="[email protected]", role=c("aut", "cph")),
person("Aaron", "Lun", email="[email protected]", role=c("ctb", "cre")),
person("Daniel", "Bunis", role="ctb"),
Expand All @@ -23,7 +23,7 @@ Imports:
stats,
utils,
Rcpp,
beachmat (>= 2.21.1)
beachmat (>= 2.23.5)
LinkingTo:
Rcpp,
beachmat,
Expand Down
4 changes: 1 addition & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions R/SingleR.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion R/classifySingleR.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion R/combineRecomputedResults.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 1 addition & 6 deletions R/getClassicMarkers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions R/plotMarkerHeatmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 3 additions & 8 deletions R/trainSingleR.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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.theads=num.threads), aggr.args))
curref <- assay(aggr)
curlabels <- aggr$label
}
Expand Down
17 changes: 7 additions & 10 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -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))
}
Expand All @@ -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(y)[keep,,drop=FALSE]
}
}

Expand Down

0 comments on commit afd3d99

Please sign in to comment.