From cb97fea9b18ae4b98bd6f9051d530620954e8c68 Mon Sep 17 00:00:00 2001 From: spriyansh Date: Sun, 3 Mar 2024 16:25:47 +0100 Subject: [PATCH] Added patch work in suggestion --- DESCRIPTION | 7 +- NAMESPACE | 5 +- R/plotDiagnostics.R | 2 +- R/plotIntersect.R | 179 ++++++++++++++++++++------------- R/plotTrend.R | 2 +- man/plotIntersect.Rd | 16 ++- vignettes/Basic-Workflow.Rmd | 2 +- vignettes/scMaSigPro-Class.Rmd | 1 + 8 files changed, 128 insertions(+), 86 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6bf5759..96dcdf3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,7 +17,7 @@ Imports: assertthat, e1071, dplyr, entropy, ggplot2, igraph, magrittr, maSigPro, MASS, MatrixGenerics, methods, parallel, parallelly, plotly, RColorConesa, rlang, S4Vectors, scales, shiny, SingleCellExperiment, stats, stringr, - utils, ComplexUpset, mclust + utils, mclust Depends: R (>= 4.0) Encoding: UTF-8 LazyData: true @@ -27,7 +27,10 @@ Suggests: roxygen2, knitr, rmarkdown, - BiocStyle + BiocStyle, + ComplexUpset, + UpSetR, + patchwork Config/testthat/edition: 3 URL: https://github.com/BioBam/scMaSigPro/ biocViews: Clustering, Regression, TimeCourse, DifferentialExpression, diff --git a/NAMESPACE b/NAMESPACE index 9f15f8a..beb0120 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -50,10 +50,6 @@ exportMethods(eSparse) exportMethods(pathAssign) exportMethods(predictors) import(ggplot2) -importFrom(ComplexUpset,intersection_matrix) -importFrom(ComplexUpset,intersection_size) -importFrom(ComplexUpset,upset) -importFrom(ComplexUpset,upset_set_size) importFrom(MASS,glm.nb) importFrom(MASS,negative.binomial) importFrom(MatrixGenerics,rowMeans) @@ -171,5 +167,6 @@ importFrom(stringr,str_split_i) importFrom(utils,View) importFrom(utils,combn) importFrom(utils,data) +importFrom(utils,packageVersion) importFrom(utils,setTxtProgressBar) importFrom(utils,txtProgressBar) diff --git a/R/plotDiagnostics.R b/R/plotDiagnostics.R index a58be41..f552c85 100644 --- a/R/plotDiagnostics.R +++ b/R/plotDiagnostics.R @@ -53,7 +53,7 @@ plotDiagnostics <- function(scmpObj, legend.position = "bottom", panel.grid.major = element_line( color = "grey90", linewidth = 0.3, linetype = "dashed" - ),legend.title = element_text(hjust = 0.5), + ), panel.grid.minor = element_blank() ) diff --git a/R/plotIntersect.R b/R/plotIntersect.R index d23f4d8..b33d9e2 100644 --- a/R/plotIntersect.R +++ b/R/plotIntersect.R @@ -2,12 +2,12 @@ #' #' @description #' Generate UpSet Plot on Intersection of Significant Genes from scMaSigPro -#' object. It is a wrapper around `ComplexUpset::upset`. +#' object. It is a wrapper around `ComplexUpset::upset` and `UpSetR::upset`. #' #' @importFrom S4Vectors isEmpty -#' @importFrom ComplexUpset upset intersection_matrix intersection_size upset_set_size #' @importFrom RColorConesa colorConesa -#' +#' @importFrom utils packageVersion +#' #' @param scmpObj An object of class \code{\link{ScMaSigPro}}. #' @param min_intersection_size Minimal number of observations in an intersection #' for it to be included. @@ -16,16 +16,22 @@ #' @param keep_empty_groups Whether empty sets should be kept (including sets #' which are only empty after filtering by size) #' @param show_sets_size The overall set sizes plot, e.g. from upset_set_size() -#' (FALSE to hide) +#' @param package Which package to use for the UpsetPlot. Options are 'ComplexUpset' +#' or 'UpSetR' (Default). +#' @param verbose Print detailed output in the console. (Default is TRUE) #' -#' @return ggplot2 plot object. +#' @return ggplot2 plot object for 'ComplexUpset' or upset object for 'UpSetR'. #' #' @author Priyansh Srivastava \email{spriyansh29@@gmail.com} #' #' @export -plotIntersect <- function(scmpObj, min_intersection_size = 2, +plotIntersect <- function(scmpObj, + package = "UpSetR", + min_intersection_size = 2, keep_empty_groups = TRUE, - width_ratio = 0.1, show_sets_size = FALSE) { + width_ratio = 0.1, + show_sets_size = FALSE, + verbose = TRUE) { # Check the data assert_that( is(scmpObj, "ScMaSigPro"), @@ -37,80 +43,109 @@ plotIntersect <- function(scmpObj, min_intersection_size = 2, msg = "'sig.genes@Summary' slot is empty, please run 'sc.get.siggenes'" ) + # Check for possible options + assert_that(package %in% c("ComplexUpset", "UpSetR"), + msg = "Please provide a valid package name for UpSet plot. Options are 'ComplexUpset' or 'UpSetR'" + ) + + # Check if package is installed + if (!requireNamespace(package, quietly = TRUE)) { + stop(paste0("Package '", package, "' is not installed. Please install it first.")) + } else { + if (verbose) { + message(paste0("Using '", package, "' for UpSet plot.")) + } + } + gene_list <- scmpObj@Significant@genes - # Create a unique list of all genes - all_genes <- unique(unlist(gene_list)) - # Initialize the data frame - gene_df <- data.frame(gene = all_genes) + if (package == "UpSetR") { + # Create list object + upset_r_gene_list <- UpSetR::fromList(gene_list) - # Add columns for each pathway - for (pathway in names(gene_list)) { - gene_df[[pathway]] <- gene_df$gene %in% gene_list[[pathway]] - } + # Create Plot + p <- UpSetR::upset( + upset_r_gene_list, + main.bar.color = "#F58A53", + matrix.color = "#15918A", + line.size = 1.5, + cutoff = min_intersection_size, + empty.intersections = keep_empty_groups, + point.size = 3, + shade.color = "purple", + text.scale = 1.5, + sets.x.label = "Number of Features", + sets.bar.color = "#EE446F" + ) + return(p) + } else { + # Check version of the ggplot2 + if (packageVersion("ggplot2") >= "3.5.0") { + warning("Please downgrade the ggplot2 to '>= 3.5.0' to use 'ComplesUpset'. We will support the latest version in future. + Visit:'https://github.com/krassowski/complex-upset/issues/195' for more details.") + } else { + # # Create a unique list of all genes + all_genes <- unique(unlist(gene_list)) - # Binarize Variables and set factors - gene_df[, -1] <- lapply(gene_df[, -1], function(x) as.integer(x)) + # Initialize the data frame + gene_df <- data.frame(gene = all_genes) - # Get conesa colours - col_pal <- colorConesa(3) + # Add columns for each pathway + for (pathway in names(gene_list)) { + gene_df[[pathway]] <- gene_df$gene %in% gene_list[[pathway]] + } - if (show_sets_size) { - show_sets_size <- upset_set_size() - } + # Binarize Variables and set factors + gene_df[, -1] <- lapply(gene_df[, -1], function(x) as.integer(x)) + + # Get conesa colours + col_pal <- colorConesa(3) - # Create Upset - p <- upset( - data = gene_df, - intersect = colnames(gene_df)[-1], - width_ratio = width_ratio, - min_size = min_intersection_size, - keep_empty_groups = keep_empty_groups, - name = "Vars", - # wrap=FALSE, - set_sizes = show_sets_size, - # stripes=c('deepskyblue1'), - matrix = ( + if (show_sets_size) { + show_sets_size <- ComplexUpset::upset_set_size() + } - intersection_matrix( - geom = geom_point( - shape = "square", - size = 3.5 + # Create Upset + p <- ComplexUpset::upset( + data = gene_df, + intersect = colnames(gene_df)[-1], + width_ratio = width_ratio, + min_size = min_intersection_size, + keep_empty_groups = keep_empty_groups, + name = "Vars", + # wrap=FALSE, + set_sizes = show_sets_size, + # stripes=c('deepskyblue1'), + matrix = ( + + ComplexUpset::intersection_matrix( + geom = geom_point( + shape = "square", + size = 3.5 + ), + segment = geom_segment( + linetype = "dotted", + color = col_pal[1] + ) + ) + + scale_color_manual( + values = c("TRUE" = col_pal[1], "FALSE" = col_pal[3]), + # labels=c('TRUE'='yes', 'FALSE'='no'), + breaks = c("TRUE", "FALSE") + ) ), - segment = geom_segment( - linetype = "dotted", - color = col_pal[1] - ) - ) - + scale_color_manual( - values = c("TRUE" = col_pal[1], "FALSE" = col_pal[3]), - # labels=c('TRUE'='yes', 'FALSE'='no'), - breaks = c("TRUE", "FALSE") + base_annotations = list( + "Intersection size" = ComplexUpset::intersection_size( + counts = TRUE, + mapping = aes(fill = "bars_color") + ) + + scale_fill_manual(values = c("bars_color" = col_pal[2]), guide = "none") ) - ), - base_annotations = list( - "Intersection size" = intersection_size( - counts = TRUE, - mapping = aes(fill = "bars_color") - ) - + scale_fill_manual(values = c("bars_color" = col_pal[2]), guide = "none") - ) - ) + ggtitle("Intersection of features among paths") + - theme(legend.position = "none", legend.title = element_text(hjust = 0.5)) - - # return plot - return(p) + ) + ggtitle("Intersection of features among paths") + + theme(legend.position = "none", legend.title = element_text(hjust = 0.5)) - # Perform UpSet plot - # upset( - # fromList(gene.list), - # main.bar.color = "#F58A53", - # matrix.color = "#15918A", - # line.size = 1.5, - # point.size = 3, - # shade.color = "purple", - # text.scale = 1.5, - # sets.x.label = "Number of Features", - # sets.bar.color = "#EE446F" - # ) + # return plot + return(p) + } + } } diff --git a/R/plotTrend.R b/R/plotTrend.R index c383f1e..553cfd2 100644 --- a/R/plotTrend.R +++ b/R/plotTrend.R @@ -201,7 +201,7 @@ plotTrend <- function(scmpObj, theme( legend.position = "bottom", panel.grid.major = element_line(color = "grey90", linewidth = 0.3, linetype = "dashed"), - panel.grid.minor = element_blank(), legend.title.align = 0.5 + panel.grid.minor = element_blank() ) + scale_x_continuous(breaks = seq(min(xlim), max(xlim), by = round(log10(length(points.df[[pooled.time]]))))) + labs(color = "Paths") + diff --git a/man/plotIntersect.Rd b/man/plotIntersect.Rd index 4b1ceec..db0e09f 100644 --- a/man/plotIntersect.Rd +++ b/man/plotIntersect.Rd @@ -6,15 +6,20 @@ \usage{ plotIntersect( scmpObj, + package = "UpSetR", min_intersection_size = 2, keep_empty_groups = TRUE, width_ratio = 0.1, - show_sets_size = FALSE + show_sets_size = FALSE, + verbose = TRUE ) } \arguments{ \item{scmpObj}{An object of class \code{\link{ScMaSigPro}}.} +\item{package}{Which package to use for the UpsetPlot. Options are 'ComplexUpset' +or 'UpSetR' (Default).} + \item{min_intersection_size}{Minimal number of observations in an intersection for it to be included.} @@ -24,15 +29,16 @@ which are only empty after filtering by size)} \item{width_ratio}{Ratio of the overall set size width to intersection matrix width.} -\item{show_sets_size}{The overall set sizes plot, e.g. from upset_set_size() -(FALSE to hide)} +\item{show_sets_size}{The overall set sizes plot, e.g. from upset_set_size()} + +\item{verbose}{Print detailed output in the console. (Default is TRUE)} } \value{ -ggplot2 plot object. +ggplot2 plot object for 'ComplexUpset' or upset object for 'UpSetR'. } \description{ Generate UpSet Plot on Intersection of Significant Genes from scMaSigPro -object. It is a wrapper around `ComplexUpset::upset`. +object. It is a wrapper around `ComplexUpset::upset` and `UpSetR::upset`. } \author{ Priyansh Srivastava \email{spriyansh29@gmail.com} diff --git a/vignettes/Basic-Workflow.Rmd b/vignettes/Basic-Workflow.Rmd index d3f2e1e..5cf3d35 100644 --- a/vignettes/Basic-Workflow.Rmd +++ b/vignettes/Basic-Workflow.Rmd @@ -319,7 +319,7 @@ By setting the vars parameter to "groups", the function will add genes with $R^2$ >= 0.7 to the object. To explore the number of genes per group, we will make an upset plot: ```{r, "uspet",eval=TRUE, echo=TRUE, fig.width=8, fig.height=6} -plotIntersect(scmp_ob) +plotIntersect(scmp_ob, package = "ComplexUpset") ``` Here, we observe that 23 genes belong to both Path2vsPath1 and Path1, indicating diff --git a/vignettes/scMaSigPro-Class.Rmd b/vignettes/scMaSigPro-Class.Rmd index 32f7398..7fc0d5f 100644 --- a/vignettes/scMaSigPro-Class.Rmd +++ b/vignettes/scMaSigPro-Class.Rmd @@ -20,6 +20,7 @@ vignette: > knitr::opts_chunk$set(echo = TRUE) knitr::opts_chunk$set(crop = NULL) library(scMaSigPro) +library(patchwork) ``` ## Introduction