From 2a71f2224635e620893b78a360078deba9c56d19 Mon Sep 17 00:00:00 2001 From: liuy12 Date: Fri, 6 Mar 2026 16:06:19 +0000 Subject: [PATCH 1/2] fix bugs with addmodulescore and addGeneIntegrationMatrix fix addmodulescore issue with feature matching; change slot to layer for seurat v5 --- R/ModuleScore.R | 58 ++++++++++++++++++++++++++++------------------ R/RNAIntegration.R | 2 +- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/R/ModuleScore.R b/R/ModuleScore.R index b143f48f..7bda3982 100644 --- a/R/ModuleScore.R +++ b/R/ModuleScore.R @@ -50,7 +50,7 @@ #' # 0.10000000 -0.03939394 -0.05387755 #' #' @export -addModuleScore <- function( +addModuleScore_test <- function( ArchRProj = NULL, useMatrix = NULL, name = "Module", @@ -148,32 +148,46 @@ addModuleScore <- function( #Figure out the index numbers of the selected features within the given matrix if(grepl(":",unlist(features)[1])){ - sname <- stringr::str_split(unlist(features),pattern=":",simplify=TRUE)[,1] - name <- stringr::str_split(unlist(features),pattern=":",simplify=TRUE)[,2] - - idx <- lapply(seq_along(name), function(x){ - ix <- intersect( - which(tolower(name[x]) == tolower(featureDF$name)), - BiocGenerics::which(tolower(sname[x]) == tolower(featureDF$seqnames)) - ) - if(length(ix)==0){ - .logStop(sprintf("FeatureName (%s) does not exist! See available features using getFeatures()", name[x]), logFile = logFile) + idx <- lapply(seq_along(features), function(x){ + sname <- stringr::str_split(features[[x]], pattern=":", simplify=TRUE)[,1] + fname <- stringr::str_split(features[[x]], pattern=":", simplify=TRUE)[,2] + + ix <- vapply(seq_along(fname), function(z){ + hit <- intersect( + which(tolower(fname[z]) == tolower(featureDF$name)), + BiocGenerics::which(tolower(sname[z]) == tolower(featureDF$seqnames)) + ) + if(length(hit) == 0) return(NA_integer_) + hit[1] + }, integer(1)) + + missing <- features[[x]][is.na(ix)] + if(length(missing) > 0){ + if(all(is.na(ix))){ + .logStop(sprintf("All features (%s) in feature set %s not found! See available features using getFeatures()", paste(missing, collapse = ", "), x), logFile = logFile) + } + .logMessage(sprintf("Warning: %d of %d features not found and will be omitted: %s", length(missing), length(ix), paste(missing, collapse = ", ")), logFile = logFile) + warning(sprintf("Features not found and will be omitted: %s", paste(missing, collapse = ", "))) + ix <- ix[!is.na(ix)] } ix }) }else{ - idx <- lapply(seq_along(unlist(features)), function(x){ - - ix <- which(tolower(unlist(features)[x]) == tolower(featureDF$name))[1] - - if(length(ix) == 0){ - .logStop(sprintf("FeatureName (%s) no regions found overlapping! See available features using getFeatures()", unlist(features)[x]), logFile = logFile) + idx <- lapply(seq_along(features), function(x){ + + ix <- match(tolower(features[[x]]), tolower(featureDF$name)) + missing <- features[[x]][is.na(ix)] + if(length(missing) > 0){ + if(all(is.na(ix))){ + .logStop(sprintf("All features (%s) in feature set %s not found! See available features using getFeatures()", paste(missing, collapse = ", "), x), logFile = logFile) + } + .logMessage(sprintf("Warning: %d of %d features not found and will be omitted: %s", length(missing), length(ix), paste(missing, collapse = ", ")), logFile = logFile) + warning(sprintf("Features not found and will be omitted: %s", paste(missing, collapse = ", "))) + ix <- ix[!is.na(ix)] } - ix - }) } @@ -184,7 +198,7 @@ addModuleScore <- function( stop("Feature Input is Not A GRanges object!") } - idx <- lapply(seq_along(unlist(features)), function(x){ + idx <- lapply(seq_along(features), function(x){ #Check o <- tryCatch({GenomeInfoDb::seqlevelsStyle(features[[x]]) <- "UCSC"}, warning = function(w) 0, error = function(e) 0) @@ -192,8 +206,8 @@ addModuleScore <- function( #Overlaps ix <- which(overlapsAny(featureData, features[[x]], ignore.strand=TRUE)) - if(length(ix)==0){ - .logStop(sprintf("FeatureName (%s) does not exist! See available features using getFeatures()", unlist(features)[x]), logFile = logFile) + if(length(ix) == 0){ + .logStop(sprintf("All GRanges features in feature set %s have no overlapping regions! See available features using getFeatures()", x), logFile = logFile) } ix diff --git a/R/RNAIntegration.R b/R/RNAIntegration.R index 209aaa4d..8854a7b3 100644 --- a/R/RNAIntegration.R +++ b/R/RNAIntegration.R @@ -531,7 +531,7 @@ addGeneIntegrationMatrix <- function( if(addToArrow){ .logDiffTime(sprintf("%s Seurat TransferData GeneMatrix", prefix), tstart, verbose = verbose, logFile = logFile) - transferParams$refdata <- GetAssayData(subRNA, assay = "RNA", slot = "data") + transferParams$refdata <- GetAssayData(subRNA, assay = "RNA", layer = "data") gc() matchedRNA <- do.call(Seurat::TransferData, transferParams) matchedRNA <- matchedRNA@data From fdac3bed89e92e8a2d31536d866b5751ffa7c876 Mon Sep 17 00:00:00 2001 From: liuy12 Date: Fri, 6 Mar 2026 16:09:00 +0000 Subject: [PATCH 2/2] change function name back to addModuleScore --- R/ModuleScore.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/ModuleScore.R b/R/ModuleScore.R index 7bda3982..466e9a4f 100644 --- a/R/ModuleScore.R +++ b/R/ModuleScore.R @@ -50,7 +50,7 @@ #' # 0.10000000 -0.03939394 -0.05387755 #' #' @export -addModuleScore_test <- function( +addModuleScore <- function( ArchRProj = NULL, useMatrix = NULL, name = "Module",