From 16351ecf6643bfb7f2ccf70c8808cb483006f3be Mon Sep 17 00:00:00 2001 From: Yichen Wang Date: Tue, 11 Jun 2024 14:44:49 -0400 Subject: [PATCH] Allow tryCatch when pseudo-bulk does not work with small clusters --- R/DEG_marker.R | 28 ++++++++++++++++++++++++---- R/visualization.R | 14 ++++++++------ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/R/DEG_marker.R b/R/DEG_marker.R index ea49715..f148e88 100644 --- a/R/DEG_marker.R +++ b/R/DEG_marker.R @@ -185,13 +185,33 @@ runMarkerDEG <- function( } else { result <- list() for (i in seq_along(levels(splitBy))) { + if (isTRUE(verbose)) { + cli::cli_alert_info( + "Running psuedo-bulk DEG on: {.val {levels(splitBy)[i]}}" + ) + } + subIdx <- splitBy == levels(splitBy)[i] subCellIdx <- allCellIdx[subIdx] groups <- split(subCellIdx, conditionBy[subIdx]) - result[[levels(splitBy)[i]]] <- .runDEG( - object, groups = groups, method = method, usePeak = usePeak, - useReplicate = useReplicate, nPsdRep = nPsdRep, - minCellPerRep = minCellPerRep, seed = seed, verbose = verbose + result[[levels(splitBy)[i]]] <- tryCatch( + { + .runDEG( + object, groups = groups, method = method, + usePeak = usePeak, useReplicate = useReplicate, + nPsdRep = nPsdRep, minCellPerRep = minCellPerRep, + seed = seed, verbose = verbose + ) + }, + error = function(e) { + cli::cli_alert_danger( + "Error when computing on {.val {levels(splitBy)[i]}}: {e$message}" + ) + cli::cli_alert_warning( + "Empty result (NULL) returned for this test." + ) + return(NULL) + } ) } } diff --git a/R/visualization.R b/R/visualization.R index f9a10b8..d0cbeaa 100644 --- a/R/visualization.R +++ b/R/visualization.R @@ -702,12 +702,12 @@ plotProportionBox <- function( fill = .data[[conditionBy]] ) else ggplot2::aes( - x = .data[[conditionBy]], + x = .data[[useCluster]], y = .data[["Proportion"]] ) ) - ) + - (if (isTRUE(dot)) + ) + ( + if (isTRUE(dot)) ggplot2::geom_point( size = dotSize, color = "black", @@ -715,9 +715,11 @@ plotProportionBox <- function( if (isTRUE(dotJitter)) ggplot2::position_jitter() else "identity" ) - else - NULL) + - ggplot2::geom_boxplot() + else + NULL + ) + + ggplot2::geom_boxplot() + + ggplot2::ggtitle(paste0(useCluster, ": ", cluster)) return(.ggplotLigerTheme(p, ...)) }) names(plist) <- levels(clusterVar)