Skip to content

Commit

Permalink
Added patch work in suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
spriyansh committed Mar 3, 2024
1 parent b287ee0 commit cb97fea
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 86 deletions.
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion R/plotDiagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)

Expand Down
179 changes: 107 additions & 72 deletions R/plotIntersect.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"),
Expand All @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion R/plotTrend.R
Original file line number Diff line number Diff line change
Expand Up @@ -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") +
Expand Down
16 changes: 11 additions & 5 deletions man/plotIntersect.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vignettes/Basic-Workflow.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions vignettes/scMaSigPro-Class.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ vignette: >
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(crop = NULL)
library(scMaSigPro)
library(patchwork)
```

## Introduction
Expand Down

0 comments on commit cb97fea

Please sign in to comment.