From 481ea5e8d05626616a835ef87b0c86607da9b18e Mon Sep 17 00:00:00 2001 From: John Elizarraras <13990267+iblacksand@users.noreply.github.com> Date: Tue, 7 May 2024 16:06:02 -0500 Subject: [PATCH] fix(gsea): fix margin too large error (#45) * fix(gsea): fix margin too large error * fix(gsea): only use try instead of tryCatch --- R/gseaEnrichment.R | 74 +++++++++++++++++++++------------------ R/multiGseaEnrichment.R | 77 ++++++++++++++++++++++------------------- 2 files changed, 82 insertions(+), 69 deletions(-) diff --git a/R/gseaEnrichment.R b/R/gseaEnrichment.R index 422c9f9..90c6e8b 100644 --- a/R/gseaEnrichment.R +++ b/R/gseaEnrichment.R @@ -129,39 +129,47 @@ gseaEnrichment <- function(hostName, outputDirectory, projectName, geneRankList, #' @importFrom svglite svglite plotEnrichmentPlot <- function(title, outputDir, fileName, format = "png", runningSums, ranks, scores, peakIndex) { - if (format == "png") { - png(file.path(outputDir, paste0(sanitizeFileName(fileName), ".png")), bg = "transparent", width = 2000, height = 2000) - cex <- list(main = 5, axis = 2.5, lab = 3.2) - } else if (format == "svg") { - svglite(file.path(outputDir, paste0(sanitizeFileName(fileName), ".svg")), bg = "transparent", width = 7, height = 7) - cex <- list(main = 1.5, axis = 0.6, lab = 0.8) - # svg seems to have a problem with long title (figure margins too large) - if (!is.na(nchar(title))) { - if (nchar(title) > 80) { - title <- paste0(substr(title, 1, 80), "...") + try( + { + if (format == "png") { + output_file <- file.path(outputDir, paste0(sanitizeFileName(fileName), ".png")) + png(output_file, bg = "transparent", width = 2000, height = 2000) + cex <- list(main = 5, axis = 2.5, lab = 3.2) + } else if (format == "svg") { + output_file <- file.path(outputDir, paste0(sanitizeFileName(fileName), ".svg")) + svglite(output_file, bg = "transparent", width = 7, height = 7) + cex <- list(main = 1.5, axis = 0.6, lab = 0.8) + # svg seems to have a problem with long title (figure margins too large) + if (!is.na(nchar(title))) { + if (nchar(title) > 80) { + title <- paste0(substr(title, 1, 80), "...") + } + } } - } - } - wrappedTitle <- strwrap(paste0("Enrichment plot: ", title), 60) - plot.new() - par(fig = c(0, 1, 0.5, 1), mar = c(0, 6, 6 * length(wrappedTitle), 2), cex.axis = cex$axis, cex.main = cex$main, cex.lab = cex$lab, lwd = 2, new = TRUE) - plot(1:length(runningSums), runningSums, - type = "l", main = paste(wrappedTitle, collapse = "\n"), - xlab = "", ylab = "Enrichment Score", xaxt = "n", lwd = 3 - ) - abline(v = peakIndex, lty = 3) - par(fig = c(0, 1, 0.35, 0.5), mar = c(0, 6, 0, 2), new = TRUE) - plot(ranks, rep(1, length(ranks)), - type = "h", - xlim = c(1, length(scores)), ylim = c(0, 1), axes = FALSE, ann = FALSE - ) - par(fig = c(0, 1, 0, 0.35), mar = c(6, 6, 0, 2), cex.axis = cex$axis, cex.lab = cex$lab, new = TRUE) - # use polygon to greatly reduce file size of SVG - plot(1:length(scores), scores, - type = "n", - ylab = "Ranked list metric", xlab = "Rank in Ordered Dataset" + wrappedTitle <- strwrap(paste0("Enrichment plot: ", title), 60) + plot.new() + par(fig = c(0, 1, 0.5, 1), mar = c(0, 6, 6 * length(wrappedTitle), 2), cex.axis = cex$axis, cex.main = cex$main, cex.lab = cex$lab, lwd = 2, new = TRUE) + plot(1:length(runningSums), runningSums, + type = "l", main = paste(wrappedTitle, collapse = "\n"), + xlab = "", ylab = "Enrichment Score", xaxt = "n", lwd = 3 + ) + abline(v = peakIndex, lty = 3) + par(fig = c(0, 1, 0.35, 0.5), mar = c(0, 6, 0, 2), new = TRUE) + plot(ranks, rep(1, length(ranks)), + type = "h", + xlim = c(1, length(scores)), ylim = c(0, 1), axes = FALSE, ann = FALSE + ) + par(fig = c(0, 1, 0, 0.35), mar = c(6, 6, 0, 2), cex.axis = cex$axis, cex.lab = cex$lab, new = TRUE) + # use polygon to greatly reduce file size of SVG + plot(1:length(scores), scores, + type = "n", + ylab = "Ranked list metric", xlab = "Rank in Ordered Dataset" + ) + polygon(c(1, 1:length(scores), length(scores)), c(0, scores, 0), col = "black") + abline(v = peakIndex, lty = 3) + dev.off() + return(output_file) + }, + silent = TRUE ) - polygon(c(1, 1:length(scores), length(scores)), c(0, scores, 0), col = "black") - abline(v = peakIndex, lty = 3) - dev.off() } diff --git a/R/multiGseaEnrichment.R b/R/multiGseaEnrichment.R index 84da0b2..5954813 100644 --- a/R/multiGseaEnrichment.R +++ b/R/multiGseaEnrichment.R @@ -208,42 +208,47 @@ multiGseaEnrichment <- function(hostName = NULL, outputDirectory = NULL, project #' @importFrom svglite svglite plotEnrichmentPlot <- function(title, outputDir, fileName, format = "png", runningSums, ranks, scores, peakIndex) { - if (format == "png") { - output_file <- file.path(outputDir, paste0(sanitizeFileName(fileName), ".png")) - png(output_file, bg = "transparent", width = 2000, height = 2000) - cex <- list(main = 5, axis = 2.5, lab = 3.2) - } else if (format == "svg") { - output_file <- file.path(outputDir, paste0(sanitizeFileName(fileName), ".svg")) - svglite(output_file, bg = "transparent", width = 7, height = 7) - cex <- list(main = 1.5, axis = 0.6, lab = 0.8) - # svg seems to have a problem with long title (figure margins too large) - if (!is.na(nchar(title))) { - if (nchar(title) > 80) { - title <- paste0(substr(title, 1, 80), "...") + try( + { + if (format == "png") { + output_file <- file.path(outputDir, paste0(sanitizeFileName(fileName), ".png")) + png(output_file, bg = "transparent", width = 2000, height = 2000) + cex <- list(main = 5, axis = 2.5, lab = 3.2) + } else if (format == "svg") { + output_file <- file.path(outputDir, paste0(sanitizeFileName(fileName), ".svg")) + svglite(output_file, bg = "transparent", width = 7, height = 7) + cex <- list(main = 1.5, axis = 0.6, lab = 0.8) + # svg seems to have a problem with long title (figure margins too large) + if (!is.na(nchar(title))) { + if (nchar(title) > 80) { + title <- paste0(substr(title, 1, 80), "...") + } + } } - } - } - wrappedTitle <- strwrap(paste0("Enrichment plot: ", title), 60) - plot.new() - par(fig = c(0, 1, 0.5, 1), mar = c(0, 6, 6 * length(wrappedTitle), 2), cex.axis = cex$axis, cex.main = cex$main, cex.lab = cex$lab, lwd = 2, new = TRUE) - plot(1:length(runningSums), runningSums, - type = "l", main = paste(wrappedTitle, collapse = "\n"), - xlab = "", ylab = "Enrichment Score", xaxt = "n", lwd = 3 - ) - abline(v = peakIndex, lty = 3) - par(fig = c(0, 1, 0.35, 0.5), mar = c(0, 6, 0, 2), new = TRUE) - plot(ranks, rep(1, length(ranks)), - type = "h", - xlim = c(1, length(scores)), ylim = c(0, 1), axes = FALSE, ann = FALSE - ) - par(fig = c(0, 1, 0, 0.35), mar = c(6, 6, 0, 2), cex.axis = cex$axis, cex.lab = cex$lab, new = TRUE) - # use polygon to greatly reduce file size of SVG - plot(1:length(scores), scores, - type = "n", - ylab = "Ranked list metric", xlab = "Rank in Ordered Dataset" + wrappedTitle <- strwrap(paste0("Enrichment plot: ", title), 60) + plot.new() + par(fig = c(0, 1, 0.5, 1), mar = c(0, 6, 6 * length(wrappedTitle), 2), cex.axis = cex$axis, cex.main = cex$main, cex.lab = cex$lab, lwd = 2, new = TRUE) + plot(1:length(runningSums), runningSums, + type = "l", main = paste(wrappedTitle, collapse = "\n"), + xlab = "", ylab = "Enrichment Score", xaxt = "n", lwd = 3 + ) + abline(v = peakIndex, lty = 3) + par(fig = c(0, 1, 0.35, 0.5), mar = c(0, 6, 0, 2), new = TRUE) + plot(ranks, rep(1, length(ranks)), + type = "h", + xlim = c(1, length(scores)), ylim = c(0, 1), axes = FALSE, ann = FALSE + ) + par(fig = c(0, 1, 0, 0.35), mar = c(6, 6, 0, 2), cex.axis = cex$axis, cex.lab = cex$lab, new = TRUE) + # use polygon to greatly reduce file size of SVG + plot(1:length(scores), scores, + type = "n", + ylab = "Ranked list metric", xlab = "Rank in Ordered Dataset" + ) + polygon(c(1, 1:length(scores), length(scores)), c(0, scores, 0), col = "black") + abline(v = peakIndex, lty = 3) + dev.off() + return(output_file) + }, + silent = TRUE ) - polygon(c(1, 1:length(scores), length(scores)), c(0, scores, 0), col = "black") - abline(v = peakIndex, lty = 3) - dev.off() - return(output_file) }