From 6ee929843f375cd3a13acc62eee5fa4f920574a7 Mon Sep 17 00:00:00 2001 From: toobiwankenobi Date: Wed, 4 Jun 2025 14:05:45 +0200 Subject: [PATCH] RowSums2 requires DelayedMatrixStats if count matrix is of class dgCMatrix. Added DelayedMatrixStats to suggested packages in DESCRIPTION and implemented conditional loading of package if count matrix is sparse --- DESCRIPTION | 1 + NAMESPACE | 1 + R/utils.R | 9 ++++++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index ae2cd5c..1fa4b8d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,6 +28,7 @@ Suggests: AUCell, BiocParallel, BiocStyle, + DelayedMatrixStats, dplyr, fgsea, GSEABase, diff --git a/NAMESPACE b/NAMESPACE index abc7840..51d6485 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -18,6 +18,7 @@ export(splitEnrichment) import(ggplot2) importFrom(MatrixGenerics,rowMeans2) importFrom(MatrixGenerics,rowSds) +importFrom(MatrixGenerics,rowSums2) importFrom(SummarizedExperiment,colData) importFrom(ggdist,stat_pointinterval) importFrom(grDevices,hcl.pals) diff --git a/R/utils.R b/R/utils.R index 793217c..483674c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -150,6 +150,7 @@ # ----------------------------------------------------------------------------- # EXPRESSION MATRIX EXTRACTOR ------------------------------------------------- # ----------------------------------------------------------------------------- +#' @importFrom MatrixGenerics rowSums2 .cntEval <- function(obj, assay = "RNA", type = "counts") { if (.is_seurat(obj)) { # Use generic accessor if available @@ -177,10 +178,16 @@ } else { cnts <- obj } + # Conditionally require DelayedMatrixStats if cnts is dgCMatrix + if (inherits(cnts, "dgCMatrix")) { + if (!requireNamespace("DelayedMatrixStats", quietly = TRUE)) { + stop("Package 'DelayedMatrixStats' is required to handle sparse matrices. Please install it with BiocManager::install('DelayedMatrixStats').") + } + loadNamespace("DelayedMatrixStats") + } cnts[MatrixGenerics::rowSums2(cnts) != 0, , drop = FALSE] } - # ----------------------------------------------------------------------------- # ATTACH / PULL ENRICHMENT MATRICES ------------------------------------------ # -----------------------------------------------------------------------------