From 952c9a770072245e1101ffbbbf0ad7c53887181a Mon Sep 17 00:00:00 2001 From: Daniel Garbozo <158525441+DanielGarbozo@users.noreply.github.com> Date: Thu, 24 Jul 2025 16:40:51 -0400 Subject: [PATCH 1/3] Add plot_global_GSEA function with documentation --- R/plot_GSEA.R | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 R/plot_GSEA.R diff --git a/R/plot_GSEA.R b/R/plot_GSEA.R new file mode 100644 index 0000000..1c79858 --- /dev/null +++ b/R/plot_GSEA.R @@ -0,0 +1,114 @@ +#' Plot global GSEA results +#' +#' Generates a composite plot displaying NES values, pathway labels, +#' and a \emph{logFDR} legend, organized by MSigDB collections. +#' +#' @param data Data frame containing the GSEA results. +#' @param geneset_col Name of the column containing the genesets. +#' @param collection_col Name of the column containing the collections. +#' @param nes_col Name of the column containing the NES values. +#' @param logfdr_col Name of the column containing \eqn{-\log_{10}(FDR)} values. +#' @param output_path_base Path and base name for the output files (without extension). +#' @param width_output Plot width in inches. +#' @param height_output Plot height in inches. +#' @param text_size_genesets Text size for the geneset labels. +#' @param text_size_collection Text size for the collection labels. +#' @return Saves two files: \code{output_path_base}.jpg and \code{output_path_base}.pdf. +#' @import ggplot2 patchwork cowplot scales +#' @export +plot_GSEA <- function(data, geneset_col, collection_col, nes_col, logfdr_col, output_path_base, width_output, height_output, text_size_genesets = 5, text_size_collection = 5) { + + # Rename columns dynamically + data <- data[, c(geneset_col, collection_col, nes_col, logfdr_col)] + colnames(data) <- c("Geneset", "Collection", "NES", "logFDR") + + # Order data by NES value (descending) + data <- data[order(data$NES, decreasing = TRUE), ] + + # Ensure Geneset and Collection are factors with ordered levels + data$Geneset <- factor(data$Geneset, levels = rev(unique(data$Geneset))) + data$Collection <- factor(data$Collection, levels = unique(data$Collection)) + + # Right-side label: "MSigDB" vertically centered, in bold and italic + plot_text_msigdb <- ggplot() + + annotate("text", label = "MSigDB", fontface = "bold.italic", angle = 90, size = 35, x = 0, y = 0.5)+ + theme_void() + + # Lef-side label: "Pathways" vertically centered, in bold and italic + plot_text_pathways <- ggplot() + + annotate("text", label = "Pathways", fontface = "bold.italic", angle = 90, size = 35, x = 0, y = 0.5)+ + theme_void() + + # Right panel: Collection labels (without repetition) + plot_right <- ggplot(data, aes(y = Geneset, x = 1.5, label = Collection)) + + geom_text(aes(label = ifelse(duplicated(Collection), "", Collection)), + hjust = 0.5, size = 0, fontface = "bold") + + facet_grid(Collection ~ ., scales = "free_y", space = "free", switch = "y") + + theme_void() + + theme(strip.text.y = element_text(angle = 0, hjust = 1, size = text_size_collection), + panel.spacing = unit(1, "lines")) + + # Center panel: NES bar plot + plot_center <- ggplot(data, aes(x = NES, y = Geneset, fill = logFDR)) + + geom_col(color = "black", size = 1) + + scale_fill_gradient(low = "white", high = "red", + limits = c(0,3), breaks = seq(0,3,1)) + + scale_y_discrete(position = "right") + + facet_grid(Collection ~ ., scales = "free_y", space = "free_y") + + theme_bw() + + labs(x = "NES", y = "") + + theme(axis.text.y = element_blank(), + strip.background = element_rect(fill = "white", color = "black",linewidth = 1 ), + axis.ticks.y = element_line(color = "black", size = 1.5), + axis.ticks.length = unit(0.3, "cm"), + strip.text.y = element_text(size = 1, margin = margin(0, 0, 0, 0)),# element_blank(), + legend.position = "none", + axis.title.x = element_text(size = 49), + axis.text.x = element_text(size = 45), + panel.spacing = unit(4, "lines") + ) + + # Left panel: Pathays labels + plot_left <- ggplot(data, aes(y = Geneset, x = 0, label = Geneset)) + + geom_text(hjust = 1, size = text_size_genesets) + + theme_void() + + theme(axis.text.y = element_blank(), + plot.margin = margin(0, 0, 0, -50)) + + # Legend panel + plot_legend <- ggplot(data, aes(x = NES, y = Geneset, fill = logFDR)) + + geom_tile() + + scale_fill_gradient(low = "white", high = "red", + name = expression(-log[10] ~ FDR), # log10FDR with subscrip, + limits = c(0,3), breaks = seq(0,3,1), + guide = guide_colorbar(ticks.colour = "black", # Make ticks black + ticks.linewidth = 1.5, # Make ticks thicker + draw.ulim = TRUE, # Draw upper limit tick + draw.llim = TRUE)) + # Draw lower limit tick + theme_bw() + + theme(legend.position = "right", + legend.box = "vertical", + legend.title = element_text(size = 44, hjust = 0.5, face = "bold"), # Bigger title + legend.text = element_text(size = 30), # Bigger legend text + legend.key.size = unit(1.5, "cm"), # Bigger color key size + legend.key.height = unit(2, "cm"), # Increase the height of the legend box + legend.spacing = unit(3.5, "cm"), # More space between title and legend + legend.box.margin = margin(10, 20, 10, 10))#5, 5, 10, 5)) # Adjust internal spacing + + plot_legend <- plot_legend + theme(legend.box = "vertical") + plot_right_legend <- get_legend(plot_legend) + + # Extract legend + #plot_right_legend <- get_legend(plot_legend) + + # Combine all plots + final_plot <- plot_text_pathways + plot_left + plot_center + plot_right + plot_text_msigdb + plot_right_legend + + plot_layout(ncol = 6, widths = c(4, 25, 15, 3, 10, 3)) + + # Display the plot + #print(final_plot) + + # Save the plot as JPG and PDF + ggsave(paste0(output_path_base, ".jpg"), final_plot, width = width_output, height = height_output, units = "in", dpi = 300) + ggsave(paste0(output_path_base, ".pdf"), final_plot, width = width_output, height = height_output, units = "in") +} From f6f12fdfb1adb4c532cdbf739ebfced12fe87581 Mon Sep 17 00:00:00 2001 From: Daniel Garbozo <158525441+DanielGarbozo@users.noreply.github.com> Date: Thu, 24 Jul 2025 16:41:46 -0400 Subject: [PATCH 2/3] Update NAMESPACE export(plot_global_GSEA) added --- NAMESPACE | 1 + 1 file changed, 1 insertion(+) diff --git a/NAMESPACE b/NAMESPACE index d8ad75f..2888560 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -10,6 +10,7 @@ export(nice_KM) export(nice_PCA) export(nice_UMAP) export(nice_tSNE) +export(plot_global_GSEA) export(power_analysis) export(save_results) export(split_cases) From 832ed47bbe803c72c77d857b82144dbaa77a0ec0 Mon Sep 17 00:00:00 2001 From: Daniel Garbozo <158525441+DanielGarbozo@users.noreply.github.com> Date: Thu, 24 Jul 2025 16:43:32 -0400 Subject: [PATCH 3/3] Create plot_global_GSEA.Rd --- man/plot_global_GSEA.Rd | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 man/plot_global_GSEA.Rd diff --git a/man/plot_global_GSEA.Rd b/man/plot_global_GSEA.Rd new file mode 100644 index 0000000..2504c0a --- /dev/null +++ b/man/plot_global_GSEA.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +\name{plot_global_GSEA} +\alias{plot_global_GSEA} +\title{Plot global GSEA results} +\usage{ +plot_global_GSEA(data, geneset_col, collection_col, nes_col, + logfdr_col, output_path_base, width_output, + height_output, text_size_genesets = 5, + text_size_collection = 5) +} +\arguments{ + \item{data}{Data frame containing the GSEA results.} + \item{geneset_col}{Name of the column containing the genesets.} + \item{collection_col}{Name of the column containing the MSigDB collections.} + \item{nes_col}{Name of the column containing NES values.} + \item{logfdr_col}{Name of the column containing \eqn{-\log_{10}(FDR)} values.} + \item{output_path_base}{Path and base name for the output files (no extension).} + \item{width_output}{Width of the output plot in inches.} + \item{height_output}{Height of the output plot in inches.} + \item{text_size_genesets}{Text size for the geneset labels.} + \item{text_size_collection}{Text size for the collection labels.} +} +\description{ +Generates a multi-panel plot summarizing GSEA results, showing pathways, +normalized enrichment scores (NES), and log-transformed FDR values +grouped by MSigDB collection. +} +\details{ +This function creates a visual summary of GSEA results by combining: +\itemize{ + \item \strong{Pathway labels} on the left, + \item \strong{NES bars} filled by \emph{log10 FDR} in the center, + \item \strong{Collection labels} on the right, + \item Vertical titles on both ends ("Pathways" and "MSigDB"). +} +The final figure is saved as both PDF and JPG. +} +\examples{ +\dontrun{ +df <- readr::read_tsv("collections_merged_gsea_data.tsv") +plot_global_GSEA(df, "Geneset", "COLLECTION", "NES", "Log10FDR", + "output/plotGSEA", 12, 8) +} +}