From 92682bc24e28b6e4536a206269448f4e758a4f4c Mon Sep 17 00:00:00 2001 From: deril2605 Date: Thu, 28 Mar 2024 00:34:34 -0400 Subject: [PATCH 1/9] qc plot with mixture facet --- DESCRIPTION | 46 +++++------ NAMESPACE | 5 ++ R/dataProcessPlotsTMT.R | 164 ++++++++++++++++++++++++++++++++++--- man/MSstatsTMT.Rd | 21 +++++ man/dataProcessPlotsTMT.Rd | 7 +- 5 files changed, 209 insertions(+), 34 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6e95035..e5d7900 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,23 +1,23 @@ -Package: MSstatsTMT -Title: Protein Significance Analysis in shotgun mass spectrometry-based proteomic experiments with tandem mass tag (TMT) labeling -Version: 2.7.1 -Date: 2023-02-18 -Description: The package provides statistical tools for detecting differentially abundant proteins in shotgun mass spectrometry-based proteomic experiments with tandem mass tag (TMT) labeling. It provides multiple functionalities, including aata visualization, protein quantification and normalization, and statistical modeling and inference. Furthermore, it is inter-operable with other data processing tools, such as Proteome Discoverer, MaxQuant, OpenMS and SpectroMine. -Authors@R: c(person("Ting", "Huang", email = "thuang0703@gmail.com", role=c("aut","cre")), - person("Meena", "Choi", email = "mnchoi67@gmail.com", role="aut"), - person("Mateusz", "Staniak", email = "mtst@mstaniak.pl", role = "aut"), - person("Sicheng", "Hao", email = "hao.sic@husky.neu.edu", role = "aut"), - person("Olga", "Vitek", email = "o.vitek@northeastern.edu", role = "aut")) -License: Artistic-2.0 -Depends: R (>= 4.2) -Imports: limma, lme4, lmerTest, methods, data.table, - stats, utils, ggplot2, grDevices, graphics, MSstats, - MSstatsConvert, checkmate -Suggests: BiocStyle, knitr, rmarkdown, testthat -VignetteBuilder: knitr -biocViews: ImmunoOncology, MassSpectrometry, Proteomics, Software -Encoding: UTF-8 -LazyData: true -URL: http://msstats.org/msstatstmt/ -BugReports: https://groups.google.com/forum/#!forum/msstats -RoxygenNote: 7.2.3 +Package: MSstatsTMT +Title: Protein Significance Analysis in shotgun mass spectrometry-based proteomic experiments with tandem mass tag (TMT) labeling +Version: 2.7.1 +Date: 2023-02-18 +Description: The package provides statistical tools for detecting differentially abundant proteins in shotgun mass spectrometry-based proteomic experiments with tandem mass tag (TMT) labeling. It provides multiple functionalities, including aata visualization, protein quantification and normalization, and statistical modeling and inference. Furthermore, it is inter-operable with other data processing tools, such as Proteome Discoverer, MaxQuant, OpenMS and SpectroMine. +Authors@R: c(person("Ting", "Huang", email = "thuang0703@gmail.com", role=c("aut","cre")), + person("Meena", "Choi", email = "mnchoi67@gmail.com", role="aut"), + person("Mateusz", "Staniak", email = "mtst@mstaniak.pl", role = "aut"), + person("Sicheng", "Hao", email = "hao.sic@husky.neu.edu", role = "aut"), + person("Olga", "Vitek", email = "o.vitek@northeastern.edu", role = "aut")) +License: Artistic-2.0 +Depends: R (>= 4.2) +Imports: limma, lme4, lmerTest, methods, data.table, + stats, utils, ggplot2, grDevices, graphics, MSstats, + MSstatsConvert, checkmate, plotly +Suggests: BiocStyle, knitr, rmarkdown, testthat +VignetteBuilder: knitr +biocViews: ImmunoOncology, MassSpectrometry, Proteomics, Software +Encoding: UTF-8 +LazyData: true +URL: http://msstats.org/msstatstmt/ +BugReports: https://groups.google.com/forum/#!forum/msstats +RoxygenNote: 7.3.1 diff --git a/NAMESPACE b/NAMESPACE index 2a7694f..c6d27cd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -35,6 +35,11 @@ importFrom(graphics,plot.new) importFrom(graphics,title) importFrom(lmerTest,lmer) importFrom(methods,new) +importFrom(plotly,add_trace) +importFrom(plotly,ggplotly) +importFrom(plotly,plot_ly) +importFrom(plotly,style) +importFrom(plotly,subplot) importFrom(stats,anova) importFrom(stats,coef) importFrom(stats,lm) diff --git a/R/dataProcessPlotsTMT.R b/R/dataProcessPlotsTMT.R index 9b8cd6e..217d545 100644 --- a/R/dataProcessPlotsTMT.R +++ b/R/dataProcessPlotsTMT.R @@ -10,6 +10,7 @@ #' @import ggplot2 #' @importFrom graphics axis image legend mtext par plot.new title plot #' @importFrom grDevices dev.off hcl pdf +#' @importFrom plotly ggplotly style add_trace plot_ly subplot #' @param data the output of \code{\link{proteinSummarization}} function. It is a list with data frames `FeatureLevelData` and `ProteinLevelData` #' @param type choice of visualization. "ProfilePlot" represents profile plot of log intensities across MS runs. #' "QCPlot" represents box plots of log intensities across channels and MS runs. @@ -34,6 +35,9 @@ #' An output pdf file is automatically created with the default name of "ProfilePlot.pdf" or "QCplot.pdf". #' The command address can help to specify where to store the file as well as how to modify the beginning of the file name. #' If address=FALSE, plot will be not saved as pdf file but showed in window. +#' @param isPlotly Parameter to use Plotly or ggplot2. If set to TRUE, MSstats +#' will save Plotly plots as HTML files. If set to FALSE MSstats will save ggplot2 plots +#' as PDF files #' @return plot or pdf #' @examples #' data(input.pd) @@ -59,7 +63,7 @@ dataProcessPlotsTMT = function( x.axis.size = 10, y.axis.size = 10, text.size = 4, text.angle = 90, legend.size = 7, dot.size.profile = 2, ncol.guide = 5, width = 10, height = 10, which.Protein = "all", originalPlot = TRUE, summaryPlot = TRUE, - address = "" + address = "", isPlotly = FALSE ) { data.peptide <- data$FeatureLevelData data.summarization <- data$ProteinLevelData @@ -78,6 +82,12 @@ dataProcessPlotsTMT = function( "Please set one protein at a time.")) } } + warning("Avoid plotting all proteins as it can take a large amount of time + to download the files") + if(isPlotly & address != FALSE) { + print("Plots will be saved as .HTML file as plotly is selected, set isPlotly = FALSE, if + you want to generate PDF using ggplot2") + } if (toupper(type) == "PROFILEPLOT") { .plotProfileTMT(processed, summarized, @@ -88,11 +98,32 @@ dataProcessPlotsTMT = function( address) } if (toupper(type) == "QCPLOT") { - .plotQualityTMT(processed, + plots <- .plotQualityTMT(processed, ylimUp, ylimDown, x.axis.size, y.axis.size, text.size, text.angle, legend.size, dot.size.profile, ncol.guide, width, height, which.Protein, - address) + address, isPlotly) + plotly_plots = list() + if(isPlotly) { + for(i in seq_along(plots)) { + plot <- plots[[i]] + plotly_plot <- .convertGgplot2Plotly(plot) + plotly_plots[[i]] = list(plotly_plot) + } + if(address != FALSE) { + .savePlotlyPlotHTML(plotly_plots,address,"QCPlot" ,width, height) + } + plotly_plots <- unlist(plotly_plots, recursive = FALSE) + plotly_plots + } + # # print(plots[[1]]) + # plotly_plots[[1]] <- list(.convertGgplot2Plotly(plots[[1]])) + # plot<- .convertGgplot2Plotly(plots[[1]]) + # # df <- data.frame(id = seq_along(plot$x$data), legend_entries = unlist(lapply(plot$x$data, `[[`, "name"))) + # # print(plot$x$data) + # .savePlotlyPlotHTML(plotly_plots,address,"QCPlot" ,width, height) + # plot + # } } @@ -340,13 +371,11 @@ dataProcessPlotsTMT = function( ylimUp, ylimDown, x.axis.size, y.axis.size, text.size, text.angle, legend.size, dot.size.profile, ncol.guide, width, height, which.Protein, - address) { + address, isPlotly) { Condition <- cumGroupAxis <- xorder <- abundance <- Protein <- NULL yaxis.name = 'Log2-intensities' - savePlot(address, "QCPlot", width, height) - if (is.numeric(ylimUp)) { y.limup = ylimUp } else { @@ -364,6 +393,10 @@ dataProcessPlotsTMT = function( groupline.all = groupline groupline = groupline[!(Condition %in% levels(Condition)[nlevels(Condition)])] + if (!isPlotly) { + savePlot(address, "QCPlot", width, height) + } + plots <- vector("list", length(unique(processed$Protein)) + 1) # +1 for all/allonly plot if (which.Protein == "all" | which.Protein == "allonly") { message("Drew the Quality Contol plot(boxplot) over all proteins.") groupline.tmp = data.frame(groupline, @@ -375,7 +408,7 @@ dataProcessPlotsTMT = function( processed$xorder = factor(processed$xorder) # for boxplot x-axis ptemp = ggplot(aes_string(x = "xorder", y = "abundance"), data = processed) + - facet_grid(~Run) + + facet_grid(~Mixture) + geom_boxplot(aes_string(fill = "Condition"), outlier.shape = 1, outlier.size = 1.5) + labs(title = "All proteins", x = "MS runs") + @@ -393,6 +426,7 @@ dataProcessPlotsTMT = function( theme(axis.ticks.x = element_blank(), axis.text.x = element_blank()) print(ptemp) + plots[[1]] = ptemp } if (which.Protein != "allonly") { @@ -421,9 +455,8 @@ dataProcessPlotsTMT = function( "PSM" = unique(single_protein$PSM)[1], "PeptideSequence" = unique(single_protein$PeptideSequence)[1]) single_protein$xorder = factor(single_protein$xorder) # for boxplot, x-axis, xorder should be factor - ptemp = ggplot(aes_string(x = 'xorder', y = 'abundance'), data = single_protein) + - facet_grid(~Run) + + facet_grid(~Mixture) + geom_boxplot(aes_string(fill = 'Condition'), outlier.shape = 1, outlier.size = 1.5) + labs(title = unique(single_protein$Protein), x = 'MS runs') + @@ -439,8 +472,12 @@ dataProcessPlotsTMT = function( theme_msstats("QCPLOT", x.axis.size, y.axis.size, legend_size = NULL) + theme(axis.ticks.x = element_blank(), - axis.text.x = element_blank()) + axis.text.x = element_blank() + # ,strip.text.x = element_text( + # size = 5, color = "dark green",angle = 45) + ) print(ptemp) + plots[[i+1]] = ptemp # to accomodate all proteins setTxtProgressBar(pb, i) } close(pb) @@ -448,6 +485,113 @@ dataProcessPlotsTMT = function( if (address != FALSE) { dev.off() } + if (isPlotly) { + plots <- Filter(function(x) !is.null(x), plots) # remove if protein was not "all" + plots + } +} + +facet_strip_bigger <- function(gp){ + + # n_facets should be the number of facets x2 + n_facets <- c(1:length(gp[["x"]][["layout"]][["shapes"]])) + + for(i in n_facets){ + if(n_facets[i] %% 2 == 0){ + gp[["x"]][["layout"]][["shapes"]][[i]][["y0"]] <- + 80 # increase as needed + gp[["x"]][["layout"]][["shapes"]][[i]][["y1"]] <- 0 + } + } + + return(gp) +} + +#' converter for plots from ggplot to plotly +#' @noRd +.convertGgplot2Plotly = function(plot, tips = "all") { + converted_plot <- ggplotly(plot,tooltip = tips) + converted_plot <- plotly::layout( + converted_plot, + width = 1800, # Set the width of the chart in pixels + height = 600, # Set the height of the chart in pixels + title = list( + font = list( + size = 18 + ) + ), + xaxis = list( + titlefont = list( + size = 15 # Set the font size for the x-axis label + ) + ), + legend = list( + x = 0, # Set the x position of the legend + y = -0.25, # Set the y position of the legend (negative value to move below the plot) + orientation = "h", # Horizontal orientation + font = list( + size = 12 # Set the font size for legend item labels + ), + title = list( + font = list( + size = 12 # Set the font size for the legend title + ) + ) + ) + ) + # converted_plot <- facet_strip_bigger(converted_plot) + converted_plot +} + +.savePlotlyPlotHTML = function(plots, address, file_name, width, height) { + print("Saving plots as HTML") + pb <- txtProgressBar(min = 0, max = 4, style = 3) + + setTxtProgressBar(pb, 1) + file_name = getFileName(address, file_name, width, height) + file_name = paste0(file_name,".html") + + setTxtProgressBar(pb, 2) + doc <- .getPlotlyPlotHTML(plots, width, height) + + setTxtProgressBar(pb, 3) + htmltools::save_html(html = doc, file = file_name) # works but lib same folder + + setTxtProgressBar(pb, 4) + zip(paste0(gsub("\\.html$", "", file_name),".zip"), c(file_name, "lib")) + unlink(file_name) + unlink("lib",recursive = T) + + close(pb) +} + +.getPlotlyPlotHTML = function(plots, width, height) { + doc <- htmltools::tagList(lapply(plots,function(x) htmltools::div(x, style = "float:left;width:100%;"))) + # Set a specific width for each plot + plot_width <- 800 + plot_height <- 600 + + # Create a div for each plot with style settings + divs <- lapply(plots, function(x) { + htmltools::div(x, style = paste0("width:", plot_width, "px; height:", plot_height, "px; margin: 10px;")) + }) + + # Combine the divs into a tagList + doc <- htmltools::tagList(divs) + doc +} + +getFileName = function(name_base, file_name, width, height) { + all_files = list.files(".") + if(file_name == 'ProfilePlot'){ + num_same_name = sum(grepl(paste0("^", name_base, file_name, "_[0-9]?"), all_files)) + } else { + num_same_name = sum(grepl(paste0("^", name_base, file_name, "[0-9]?"), all_files)) + } + if (num_same_name > 0) { + file_name = paste(file_name, num_same_name + 1, sep = "_") + } + file_path = paste0(name_base, file_name) + return(file_path) } #' @keywords internal diff --git a/man/MSstatsTMT.Rd b/man/MSstatsTMT.Rd index da5f29c..90a6762 100644 --- a/man/MSstatsTMT.Rd +++ b/man/MSstatsTMT.Rd @@ -2,6 +2,7 @@ % Please edit documentation in R/utils_MSstatsTMT.R \docType{package} \name{MSstatsTMT} +\alias{MSstatsTMT-package} \alias{MSstatsTMT} \title{MSstatsTMT: A package for protein significance analysis in shotgun mass spectrometry-based proteomic experiments with tandem mass tag (TMT) labeling} \description{ @@ -20,3 +21,23 @@ A set of tools for detecting differentially abundant peptides and proteins in sh } } +\seealso{ +Useful links: +\itemize{ + \item \url{http://msstats.org/msstatstmt/} + \item Report bugs at \url{https://groups.google.com/forum/#!forum/msstats} +} + +} +\author{ +\strong{Maintainer}: Ting Huang \email{thuang0703@gmail.com} + +Authors: +\itemize{ + \item Meena Choi \email{mnchoi67@gmail.com} + \item Mateusz Staniak \email{mtst@mstaniak.pl} + \item Sicheng Hao \email{hao.sic@husky.neu.edu} + \item Olga Vitek \email{o.vitek@northeastern.edu} +} + +} diff --git a/man/dataProcessPlotsTMT.Rd b/man/dataProcessPlotsTMT.Rd index 0d654b0..3046cbf 100644 --- a/man/dataProcessPlotsTMT.Rd +++ b/man/dataProcessPlotsTMT.Rd @@ -21,7 +21,8 @@ dataProcessPlotsTMT( which.Protein = "all", originalPlot = TRUE, summaryPlot = TRUE, - address = "" + address = "", + isPlotly = FALSE ) } \arguments{ @@ -65,6 +66,10 @@ The other assigned folder has to be existed under the current working directory. An output pdf file is automatically created with the default name of "ProfilePlot.pdf" or "QCplot.pdf". The command address can help to specify where to store the file as well as how to modify the beginning of the file name. If address=FALSE, plot will be not saved as pdf file but showed in window.} + +\item{isPlotly}{Parameter to use Plotly or ggplot2. If set to TRUE, MSstats +will save Plotly plots as HTML files. If set to FALSE MSstats will save ggplot2 plots +as PDF files} } \value{ plot or pdf From adcb5a817df98ca4ff5f726524a22077312fa833 Mon Sep 17 00:00:00 2001 From: deril2605 Date: Thu, 28 Mar 2024 01:52:06 -0400 Subject: [PATCH 2/9] fixes --- R/dataProcessPlotsTMT.R | 49 +++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/R/dataProcessPlotsTMT.R b/R/dataProcessPlotsTMT.R index 217d545..f03429e 100644 --- a/R/dataProcessPlotsTMT.R +++ b/R/dataProcessPlotsTMT.R @@ -408,23 +408,24 @@ dataProcessPlotsTMT = function( processed$xorder = factor(processed$xorder) # for boxplot x-axis ptemp = ggplot(aes_string(x = "xorder", y = "abundance"), data = processed) + - facet_grid(~Mixture) + - geom_boxplot(aes_string(fill = "Condition"), outlier.shape = 1, outlier.size = 1.5) + - labs(title = "All proteins", - x = "MS runs") + - scale_y_continuous(yaxis.name, limits = c(y.limdown, y.limup)) + - geom_vline(data = groupline.tmp, - aes(xintercept = cumGroupAxis), - colour = "grey", linetype = "longdash") + - geom_text(data = groupline.all.tmp, - aes(x = xorder, y = abundance, label = Condition), - size = text.size, - angle = text.angle, - color = "black") + - theme_msstats("QCPLOT", x.axis.size, y.axis.size, - legend_size = NULL) + - theme(axis.ticks.x = element_blank(), - axis.text.x = element_blank()) + facet_grid(~Mixture) + + geom_boxplot(aes_string(fill = "Condition"), outlier.shape = 1, outlier.size = 1.5) + + labs(title = "All proteins", + x = "MS runs") + + scale_y_continuous(yaxis.name, limits = c(y.limdown, y.limup)) + + geom_vline(data = groupline.tmp, + aes(xintercept = cumGroupAxis), + colour = "grey", linetype = "longdash") + + geom_text(data = groupline.all.tmp, + aes(x = xorder, y = abundance, label = Condition), + size = text.size, + angle = text.angle, + color = "black") + + theme_msstats("QCPLOT", x.axis.size, y.axis.size, + legend_size = NULL) + + theme(axis.ticks.x = element_blank(), + axis.text.x = element_blank()) + print(ptemp) plots[[1]] = ptemp } @@ -454,6 +455,7 @@ dataProcessPlotsTMT = function( groupline.all.tmp = data.frame(groupline.all, "PSM" = unique(single_protein$PSM)[1], "PeptideSequence" = unique(single_protein$PeptideSequence)[1]) + print(groupline.all.tmp) single_protein$xorder = factor(single_protein$xorder) # for boxplot, x-axis, xorder should be factor ptemp = ggplot(aes_string(x = 'xorder', y = 'abundance'), data = single_protein) + facet_grid(~Mixture) + @@ -512,7 +514,7 @@ facet_strip_bigger <- function(gp){ converted_plot <- ggplotly(plot,tooltip = tips) converted_plot <- plotly::layout( converted_plot, - width = 1800, # Set the width of the chart in pixels + width = 1100, # Set the width of the chart in pixels height = 600, # Set the height of the chart in pixels title = list( font = list( @@ -521,7 +523,7 @@ facet_strip_bigger <- function(gp){ ), xaxis = list( titlefont = list( - size = 15 # Set the font size for the x-axis label + size = 15, # Set the font size for the x-axis label ) ), legend = list( @@ -539,6 +541,15 @@ facet_strip_bigger <- function(gp){ ) ) # converted_plot <- facet_strip_bigger(converted_plot) + # converted_plot <- converted_plot %>% layout(xaxis = list(tickangle = -170)) + print(converted_plot$x$data) + print(converted_plot$x$data[[30]]$text) + # for (i in seq_along(converted_plot$x$data)) { + # if (converted_plot$x$data[[i]]$mode == "text") { # Make sure to only adjust text annotations + # converted_plot$x$data[[i]]$textfont$angle <- -90 # Set your desired angle here + # } + # } + converted_plot } From 64d9820ae44e538f0a960f70945a81b2a9d9ff80 Mon Sep 17 00:00:00 2001 From: deril2605 Date: Fri, 5 Apr 2024 10:08:59 -0400 Subject: [PATCH 3/9] QCplot ready --- R/dataProcessPlotsTMT.R | 59 +++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/R/dataProcessPlotsTMT.R b/R/dataProcessPlotsTMT.R index f03429e..4442708 100644 --- a/R/dataProcessPlotsTMT.R +++ b/R/dataProcessPlotsTMT.R @@ -60,7 +60,7 @@ #' dataProcessPlotsTMT = function( data, type, ylimUp = FALSE, ylimDown = FALSE, - x.axis.size = 10, y.axis.size = 10, text.size = 4, text.angle = 90, + x.axis.size = 10, y.axis.size = 10, text.size = 2, text.angle = 90, legend.size = 7, dot.size.profile = 2, ncol.guide = 5, width = 10, height = 10, which.Protein = "all", originalPlot = TRUE, summaryPlot = TRUE, address = "", isPlotly = FALSE @@ -116,14 +116,6 @@ dataProcessPlotsTMT = function( plotly_plots <- unlist(plotly_plots, recursive = FALSE) plotly_plots } - # # print(plots[[1]]) - # plotly_plots[[1]] <- list(.convertGgplot2Plotly(plots[[1]])) - # plot<- .convertGgplot2Plotly(plots[[1]]) - # # df <- data.frame(id = seq_along(plot$x$data), legend_entries = unlist(lapply(plot$x$data, `[[`, "name"))) - # # print(plot$x$data) - # .savePlotlyPlotHTML(plotly_plots,address,"QCPlot" ,width, height) - # plot - # } } @@ -408,7 +400,7 @@ dataProcessPlotsTMT = function( processed$xorder = factor(processed$xorder) # for boxplot x-axis ptemp = ggplot(aes_string(x = "xorder", y = "abundance"), data = processed) + - facet_grid(~Mixture) + + facet_grid(~Run) + geom_boxplot(aes_string(fill = "Condition"), outlier.shape = 1, outlier.size = 1.5) + labs(title = "All proteins", x = "MS runs") + @@ -424,7 +416,8 @@ dataProcessPlotsTMT = function( theme_msstats("QCPLOT", x.axis.size, y.axis.size, legend_size = NULL) + theme(axis.ticks.x = element_blank(), - axis.text.x = element_blank()) + axis.text.x = element_blank(),strip.text.x = element_text( + size = 5, color = "black",angle = 15)) print(ptemp) plots[[1]] = ptemp @@ -455,10 +448,9 @@ dataProcessPlotsTMT = function( groupline.all.tmp = data.frame(groupline.all, "PSM" = unique(single_protein$PSM)[1], "PeptideSequence" = unique(single_protein$PeptideSequence)[1]) - print(groupline.all.tmp) single_protein$xorder = factor(single_protein$xorder) # for boxplot, x-axis, xorder should be factor ptemp = ggplot(aes_string(x = 'xorder', y = 'abundance'), data = single_protein) + - facet_grid(~Mixture) + + facet_grid(~Run) + geom_boxplot(aes_string(fill = 'Condition'), outlier.shape = 1, outlier.size = 1.5) + labs(title = unique(single_protein$Protein), x = 'MS runs') + @@ -475,9 +467,9 @@ dataProcessPlotsTMT = function( legend_size = NULL) + theme(axis.ticks.x = element_blank(), axis.text.x = element_blank() - # ,strip.text.x = element_text( - # size = 5, color = "dark green",angle = 45) - ) + ,strip.text.x = element_text( + size = 5, color = "black",angle = 15) + ) print(ptemp) plots[[i+1]] = ptemp # to accomodate all proteins setTxtProgressBar(pb, i) @@ -497,13 +489,23 @@ facet_strip_bigger <- function(gp){ # n_facets should be the number of facets x2 n_facets <- c(1:length(gp[["x"]][["layout"]][["shapes"]])) - - for(i in n_facets){ - if(n_facets[i] %% 2 == 0){ - gp[["x"]][["layout"]][["shapes"]][[i]][["y0"]] <- + 80 # increase as needed - gp[["x"]][["layout"]][["shapes"]][[i]][["y1"]] <- 0 + print("=====") + print(gp[["x"]][["layout"]][["annotations"]][[2]]) + print("===") + if (!is.null(gp$x$layout$annotations)) { + for (i in seq_along(gp$x$layout$annotations)) { + gp$x$layout$annotations[[i]]$font$size <- 7 + # gp$x$layout$annotations[[i]]$xanchor <- "center" + gp$x$layout$annotations[[i]]$xshift <- 50 } } + # for(i in n_facets){ + # if(n_facets[i] %% 2 == 0){ + # gp[["x"]][["layout"]][["shapes"]][[i]][["y0"]] <- + 80 # increase as needed + # # gp[["x"]][["layout"]][["shapes"]][[i]][["x1"]] <- +50 + # + # } + # } return(gp) } @@ -514,18 +516,13 @@ facet_strip_bigger <- function(gp){ converted_plot <- ggplotly(plot,tooltip = tips) converted_plot <- plotly::layout( converted_plot, - width = 1100, # Set the width of the chart in pixels + width = 1800, # Set the width of the chart in pixels height = 600, # Set the height of the chart in pixels title = list( font = list( size = 18 ) ), - xaxis = list( - titlefont = list( - size = 15, # Set the font size for the x-axis label - ) - ), legend = list( x = 0, # Set the x position of the legend y = -0.25, # Set the y position of the legend (negative value to move below the plot) @@ -540,10 +537,10 @@ facet_strip_bigger <- function(gp){ ) ) ) - # converted_plot <- facet_strip_bigger(converted_plot) + converted_plot <- facet_strip_bigger(converted_plot) # converted_plot <- converted_plot %>% layout(xaxis = list(tickangle = -170)) - print(converted_plot$x$data) - print(converted_plot$x$data[[30]]$text) + # print(converted_plot$x$data) + # print(converted_plot$x$data[[30]]$text) # for (i in seq_along(converted_plot$x$data)) { # if (converted_plot$x$data[[i]]$mode == "text") { # Make sure to only adjust text annotations # converted_plot$x$data[[i]]$textfont$angle <- -90 # Set your desired angle here @@ -578,7 +575,7 @@ facet_strip_bigger <- function(gp){ .getPlotlyPlotHTML = function(plots, width, height) { doc <- htmltools::tagList(lapply(plots,function(x) htmltools::div(x, style = "float:left;width:100%;"))) # Set a specific width for each plot - plot_width <- 800 + plot_width <- 2000 plot_height <- 600 # Create a div for each plot with style settings From 008f0d2aea4d0c6437a0a26d8c10debd973cd597 Mon Sep 17 00:00:00 2001 From: deril2605 Date: Wed, 10 Apr 2024 12:26:03 -0400 Subject: [PATCH 4/9] profile plots base --- R/dataProcessPlotsTMT.R | 103 +++++++++++++++++++++++++++++++++------- 1 file changed, 86 insertions(+), 17 deletions(-) diff --git a/R/dataProcessPlotsTMT.R b/R/dataProcessPlotsTMT.R index 4442708..22f0662 100644 --- a/R/dataProcessPlotsTMT.R +++ b/R/dataProcessPlotsTMT.R @@ -90,12 +90,47 @@ dataProcessPlotsTMT = function( } if (toupper(type) == "PROFILEPLOT") { - .plotProfileTMT(processed, summarized, + plots <- .plotProfileTMT(processed, summarized, ylimUp, ylimDown, x.axis.size, y.axis.size, text.size, text.angle, legend.size, dot.size.profile, ncol.guide, width, height, which.Protein, originalPlot, summaryPlot, - address) + address, isPlotly) + plotly_plots = list() + if(isPlotly) { + og_plotly_plot = NULL + summ_plotly_plot = NULL + if("original_plot" %in% names(plots)) { + for(i in seq_along(plots[["original_plot"]])) { + plot_i <- plots[["original_plot"]][[paste("plot",i)]] + og_plotly_plot <- .convertGgplot2Plotly(plot_i) + og_plotly_plot = .fixLegendPlotlyPlotsDataprocess(og_plotly_plot, "OriginalPlot") + # og_plotly_plot = .fixCensoredPointsLegendProfilePlotsPlotly(og_plotly_plot) + # + # if(toupper(featureName) == "NA") { + # og_plotly_plot = .retainCensoredDataPoints(og_plotly_plot) + # } + plotly_plots = c(plotly_plots, list(og_plotly_plot)) + } + } + if("summary_plot" %in% names(plots)) { + for(i in seq_along(plots[["summary_plot"]])) { + plot_i <- plots[["summary_plot"]][[paste("plot",i)]] + summ_plotly_plot <- .convertGgplot2Plotly(plot_i) + summ_plotly_plot = .fixLegendPlotlyPlotsDataprocess(summ_plotly_plot, "SummaryPlot") + # summ_plotly_plot = .fixCensoredPointsLegendProfilePlotsPlotly(summ_plotly_plot) + # if(toupper(featureName) == "NA") { + # summ_plotly_plot = .retainCensoredDataPoints(summ_plotly_plot) + # } + plotly_plots = c(plotly_plots, list(summ_plotly_plot)) + } + } + + if(address != FALSE) { + .savePlotlyPlotHTML(plotly_plots,address,"ProfilePlot" ,width, height) + } + plotly_plots + } } if (toupper(type) == "QCPLOT") { plots <- .plotQualityTMT(processed, @@ -143,7 +178,7 @@ dataProcessPlotsTMT = function( text.size, text.angle, legend.size, dot.size.profile, ncol.guide, width, height, which.Protein, originalPlot, summaryPlot, - address) { + address, isPlotly) { Protein <- Condition <- xorder <- Run <- NULL Channel <- PeptideSequence <- PSM <- cumGroupAxis <- NULL @@ -186,9 +221,13 @@ dataProcessPlotsTMT = function( c("Run", "Abundance")) summarized = merge(summarized, tempGroupName, by = c("Run", "Channel")) - + output_plots <- list() + output_plots[["original_plot"]] = list() + output_plots[["summary_plot"]] = list() if (originalPlot) { - savePlot(address, "ProfilePlot", width, height) + if(!isPlotly) { + savePlot(address, "ProfilePlot", width, height) + } message(paste0("Drew the Profile plot for ", length(all_proteins), " proteins.")) pb = txtProgressBar(max = length(all_proteins), style=3) for (i in seq_along(all_proteins)) { @@ -244,7 +283,8 @@ dataProcessPlotsTMT = function( color = "black") + theme_msstats("PROFILEPLOT", x.axis.size, y.axis.size, legend.size) + theme(axis.ticks.x = element_blank(), - axis.text.x = element_blank())+ + axis.text.x = element_blank(),strip.text.x = element_text( + size = 5, color = "black",angle = 15))+ guides(color = guide_legend(title = paste("# peptide:", nlevels(single_protein$PeptideSequence)), title.theme = element_text(size = 13, angle = 0), keywidth = 0.4, @@ -259,16 +299,19 @@ dataProcessPlotsTMT = function( ncol = ncol.guide)) print(ptemp) + output_plots[["original_plot"]][[paste("plot",i)]] <- ptemp setTxtProgressBar(pb, i) } close(pb) - if (address != FALSE) { - dev.off() - } + if (address != FALSE & !isPlotly) { + dev.off() + } } if (summaryPlot) { - savePlot(address, "ProfilePlot_wSummarization", width, height) + if(!isPlotly) { + savePlot(address, "ProfilePlot_wSummarization", width, height) + } message(paste0("Drew the Profile plot with summarization for ", length(all_proteins), " proteins.")) pb = txtProgressBar(max = length(all_proteins), style=3) for (i in seq_along(all_proteins)) { @@ -336,7 +379,8 @@ dataProcessPlotsTMT = function( theme_msstats("PROFILEPLOT", x.axis.size, y.axis.size, legend.size, legend.title = element_blank()) + theme(axis.ticks.x = element_blank(), - axis.text.x = element_blank())+ + axis.text.x = element_blank(),strip.text.x = element_text( + size = 5, color = "black",angle = 15))+ guides(color = guide_legend(order = 1, title = NULL, label.theme = element_text(size = 10, angle = 0))) @@ -347,13 +391,17 @@ dataProcessPlotsTMT = function( aes(x = xorder, y = abundance, size = analysis, color = analysis)) print(ptempall) + output_plots[["summary_plot"]][[paste("plot",i)]] <- ptempall setTxtProgressBar(pb, i) } # end-loop for each protein close(pb) - if (address!=FALSE) { - dev.off() - } + if (address != FALSE & !isPlotly) { + dev.off() + } + } + if(isPlotly) { + output_plots } } @@ -489,9 +537,6 @@ facet_strip_bigger <- function(gp){ # n_facets should be the number of facets x2 n_facets <- c(1:length(gp[["x"]][["layout"]][["shapes"]])) - print("=====") - print(gp[["x"]][["layout"]][["annotations"]][[2]]) - print("===") if (!is.null(gp$x$layout$annotations)) { for (i in seq_along(gp$x$layout$annotations)) { gp$x$layout$annotations[[i]]$font$size <- 7 @@ -588,6 +633,30 @@ facet_strip_bigger <- function(gp){ doc } +.fixLegendPlotlyPlotsDataprocess = function(plot, type) { + df <- data.frame(id = seq_along(plot$x$data), legend_entries = unlist(lapply(plot$x$data, `[[`, "name"))) + df$legend_group <- gsub("^\\((.*?),.*", "\\1", df$legend_entries) + df$is_first <- !duplicated(df$legend_group) + df$is_bool <- ifelse(grepl("TRUE|FALSE", df$legend_group), TRUE, FALSE) + df$is_valid_column <- ifelse(grepl("Processed feature-level data|Run summary", df$legend_entries), TRUE, FALSE) + # df[nrow(df), "is_first"] <- FALSE + plot$x$data[[nrow(df)]]$showlegend <- FALSE # remove text legend + for (i in df$id) { + is_first <- df$is_first[[i]] + is_bool <- df$is_bool[[i]] + plot$x$data[[i]]$name <- df$legend_group[[i]] + plot$x$data[[i]]$legendgroup <- plot$x$data[[i]]$name + if (!is_first) plot$x$data[[i]]$showlegend <- FALSE + if(type == "SummaryPlot") { + is_valid_column <- df$is_valid_column[[i]] + if (!is_valid_column) plot$x$data[[i]]$showlegend <- FALSE + } + if(is_bool) plot$x$data[[i]]$showlegend <- FALSE + } + plot + +} + getFileName = function(name_base, file_name, width, height) { all_files = list.files(".") if(file_name == 'ProfilePlot'){ From 2a3c56668ace65ee7200a8c01a2b7b87774736eb Mon Sep 17 00:00:00 2001 From: deril2605 Date: Tue, 23 Apr 2024 22:28:48 -0400 Subject: [PATCH 5/9] finalize changes --- DESCRIPTION | 2 +- NAMESPACE | 3 ++ R/dataProcessPlotsTMT.R | 70 +++++++++++++++++++++++++++++--------- man/dataProcessPlotsTMT.Rd | 2 +- 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e5d7900..03a4e54 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,7 +12,7 @@ License: Artistic-2.0 Depends: R (>= 4.2) Imports: limma, lme4, lmerTest, methods, data.table, stats, utils, ggplot2, grDevices, graphics, MSstats, - MSstatsConvert, checkmate, plotly + MSstatsConvert, checkmate, plotly, htmltools Suggests: BiocStyle, knitr, rmarkdown, testthat VignetteBuilder: knitr biocViews: ImmunoOncology, MassSpectrometry, Proteomics, Software diff --git a/NAMESPACE b/NAMESPACE index c6d27cd..0c8a4ae 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -33,6 +33,9 @@ importFrom(graphics,par) importFrom(graphics,plot) importFrom(graphics,plot.new) importFrom(graphics,title) +importFrom(htmltools,div) +importFrom(htmltools,save_html) +importFrom(htmltools,tagList) importFrom(lmerTest,lmer) importFrom(methods,new) importFrom(plotly,add_trace) diff --git a/R/dataProcessPlotsTMT.R b/R/dataProcessPlotsTMT.R index 22f0662..5fe832f 100644 --- a/R/dataProcessPlotsTMT.R +++ b/R/dataProcessPlotsTMT.R @@ -8,6 +8,7 @@ #' #' @export #' @import ggplot2 +#' @importFrom htmltools save_html tagList div #' @importFrom graphics axis image legend mtext par plot.new title plot #' @importFrom grDevices dev.off hcl pdf #' @importFrom plotly ggplotly style add_trace plot_ly subplot @@ -105,7 +106,7 @@ dataProcessPlotsTMT = function( plot_i <- plots[["original_plot"]][[paste("plot",i)]] og_plotly_plot <- .convertGgplot2Plotly(plot_i) og_plotly_plot = .fixLegendPlotlyPlotsDataprocess(og_plotly_plot, "OriginalPlot") - # og_plotly_plot = .fixCensoredPointsLegendProfilePlotsPlotly(og_plotly_plot) + og_plotly_plot = .fixCensoredPointsLegendProfilePlotsPlotly(og_plotly_plot) # # if(toupper(featureName) == "NA") { # og_plotly_plot = .retainCensoredDataPoints(og_plotly_plot) @@ -132,7 +133,7 @@ dataProcessPlotsTMT = function( plotly_plots } } - if (toupper(type) == "QCPLOT") { + else if (toupper(type) == "QCPLOT") { plots <- .plotQualityTMT(processed, ylimUp, ylimDown, x.axis.size, y.axis.size, text.size, text.angle, legend.size, dot.size.profile, @@ -259,16 +260,18 @@ dataProcessPlotsTMT = function( if ( check.length > 0 ){ cbp = rep(cbp, times=check.length + 1) } - + # ptemp = ggplot(single_protein, aes(x = xorder, y = abundance, shape = censored)) + + # geom_point(size = dot.size.profile) + + # scale_shape_manual(values = c(16, 1)) ptemp = ggplot(aes_string(x = 'xorder', y = 'abundance', color = 'PSM', linetype = 'PSM'), data = single_protein) + facet_grid(~Run) + geom_point(data = single_protein, aes(shape=censored), size=dot.size.profile) + - geom_point(size=dot.size.profile) + + # geom_point(size=dot.size.profile) + geom_line(size = 0.5) + scale_colour_manual(values=cbp[s]) + scale_linetype_manual(values = ss) + - scale_shape_manual(values = c(16, 1)) + + scale_shape_manual(values = c(16, 1),labels = c("Detected data", "Censored missing data")) + labs(title = unique(single_protein$Protein), x = 'MS runs') + scale_y_continuous(yaxis.name, limits = c(y.limdown, y.limup)) + @@ -297,7 +300,7 @@ dataProcessPlotsTMT = function( keyheight = 0.1, default.unit = 'inch', ncol = ncol.guide)) - + print(ptemp) output_plots[["original_plot"]][[paste("plot",i)]] <- ptemp setTxtProgressBar(pb, i) @@ -607,7 +610,7 @@ facet_strip_bigger <- function(gp){ doc <- .getPlotlyPlotHTML(plots, width, height) setTxtProgressBar(pb, 3) - htmltools::save_html(html = doc, file = file_name) # works but lib same folder + save_html(html = doc, file = file_name) # works but lib same folder setTxtProgressBar(pb, 4) zip(paste0(gsub("\\.html$", "", file_name),".zip"), c(file_name, "lib")) @@ -618,18 +621,18 @@ facet_strip_bigger <- function(gp){ } .getPlotlyPlotHTML = function(plots, width, height) { - doc <- htmltools::tagList(lapply(plots,function(x) htmltools::div(x, style = "float:left;width:100%;"))) + doc <- tagList(lapply(plots,function(x) div(x, style = "float:left;width:100%;"))) # Set a specific width for each plot plot_width <- 2000 plot_height <- 600 # Create a div for each plot with style settings divs <- lapply(plots, function(x) { - htmltools::div(x, style = paste0("width:", plot_width, "px; height:", plot_height, "px; margin: 10px;")) + div(x, style = paste0("width:", plot_width, "px; height:", plot_height, "px; margin: 10px;")) }) # Combine the divs into a tagList - doc <- htmltools::tagList(divs) + doc <- tagList(divs) doc } @@ -638,23 +641,23 @@ facet_strip_bigger <- function(gp){ df$legend_group <- gsub("^\\((.*?),.*", "\\1", df$legend_entries) df$is_first <- !duplicated(df$legend_group) df$is_bool <- ifelse(grepl("TRUE|FALSE", df$legend_group), TRUE, FALSE) - df$is_valid_column <- ifelse(grepl("Processed feature-level data|Run summary", df$legend_entries), TRUE, FALSE) # df[nrow(df), "is_first"] <- FALSE + df$is_valid_column <- ifelse(grepl("Processed feature-level data|Run summary", df$legend_entries), TRUE, FALSE) plot$x$data[[nrow(df)]]$showlegend <- FALSE # remove text legend + for (i in df$id) { is_first <- df$is_first[[i]] is_bool <- df$is_bool[[i]] plot$x$data[[i]]$name <- df$legend_group[[i]] plot$x$data[[i]]$legendgroup <- plot$x$data[[i]]$name if (!is_first) plot$x$data[[i]]$showlegend <- FALSE - if(type == "SummaryPlot") { - is_valid_column <- df$is_valid_column[[i]] - if (!is_valid_column) plot$x$data[[i]]$showlegend <- FALSE - } + if(type == "SummaryPlot") { + is_valid_column <- df$is_valid_column[[i]] + if (!is_valid_column) plot$x$data[[i]]$showlegend <- FALSE + } if(is_bool) plot$x$data[[i]]$showlegend <- FALSE } plot - } getFileName = function(name_base, file_name, width, height) { @@ -671,6 +674,41 @@ getFileName = function(name_base, file_name, width, height) { return(file_path) } +.fixCensoredPointsLegendProfilePlotsPlotly = function(plot) { + # df <- data.frame(id = seq_along(plot$x$data), legend_entries = unlist(lapply(plot$x$data, `[[`, "name"))) + # bool_values <- sapply(df$legend_entries, function(x) { + # split_string <- strsplit(x, ",")[[1]] + # as.logical(gsub("[)]", "", split_string[2])) + # }) + # + # # Finding the index of the first occurrence of TRUE + # first_true_index <- which(bool_values == TRUE)[1] + # + # # Finding the index of the first occurrence of FALSE + # first_false_index <- which(bool_values == FALSE)[1] + # print(paste("First TRUE index:", first_true_index)) + # print(paste("First FALSE index:", first_false_index)) + # + # # Update plot data for the first occurrence of "FALSE" + # if (!is.na(first_false_index)) { + # plot$x$data[[first_false_index]]$name <- "Detected data" + # plot$x$data[[first_false_index]]$showlegend <- TRUE + # } + # + # # Update plot data for the first occurrence of "TRUE" + # if (!is.na(first_true_index)) { + # plot$x$data[[first_true_index]]$name <- "Censored missing data" + # plot$x$data[[first_true_index]]$showlegend <- TRUE + # } + # # plot$x$data <- lapply(plot$x$data, function(item) { + # # if (!is.null(item$marker$symbol) && item$marker$symbol == "circle-open") { + # # item$showlegend <- TRUE # Set showlegend to TRUE + # # } + # # return(item) # Return the modified or unmodified item + # # }) + plot +} + #' @keywords internal .getXAxisOrder = function(processed) { diff --git a/man/dataProcessPlotsTMT.Rd b/man/dataProcessPlotsTMT.Rd index 3046cbf..005d9cd 100644 --- a/man/dataProcessPlotsTMT.Rd +++ b/man/dataProcessPlotsTMT.Rd @@ -11,7 +11,7 @@ dataProcessPlotsTMT( ylimDown = FALSE, x.axis.size = 10, y.axis.size = 10, - text.size = 4, + text.size = 2, text.angle = 90, legend.size = 7, dot.size.profile = 2, From 79f5bb998788d8d0278d75df39ed8ee9bd7b84e8 Mon Sep 17 00:00:00 2001 From: deril2605 Date: Wed, 24 Apr 2024 22:40:11 -0400 Subject: [PATCH 6/9] font fixes --- R/dataProcessPlotsTMT.R | 37 ++++++++++--------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/R/dataProcessPlotsTMT.R b/R/dataProcessPlotsTMT.R index 5fe832f..ca696fe 100644 --- a/R/dataProcessPlotsTMT.R +++ b/R/dataProcessPlotsTMT.R @@ -107,10 +107,6 @@ dataProcessPlotsTMT = function( og_plotly_plot <- .convertGgplot2Plotly(plot_i) og_plotly_plot = .fixLegendPlotlyPlotsDataprocess(og_plotly_plot, "OriginalPlot") og_plotly_plot = .fixCensoredPointsLegendProfilePlotsPlotly(og_plotly_plot) - # - # if(toupper(featureName) == "NA") { - # og_plotly_plot = .retainCensoredDataPoints(og_plotly_plot) - # } plotly_plots = c(plotly_plots, list(og_plotly_plot)) } } @@ -119,10 +115,6 @@ dataProcessPlotsTMT = function( plot_i <- plots[["summary_plot"]][[paste("plot",i)]] summ_plotly_plot <- .convertGgplot2Plotly(plot_i) summ_plotly_plot = .fixLegendPlotlyPlotsDataprocess(summ_plotly_plot, "SummaryPlot") - # summ_plotly_plot = .fixCensoredPointsLegendProfilePlotsPlotly(summ_plotly_plot) - # if(toupper(featureName) == "NA") { - # summ_plotly_plot = .retainCensoredDataPoints(summ_plotly_plot) - # } plotly_plots = c(plotly_plots, list(summ_plotly_plot)) } } @@ -284,10 +276,10 @@ dataProcessPlotsTMT = function( size = text.size, angle = text.angle, color = "black") + - theme_msstats("PROFILEPLOT", x.axis.size, y.axis.size, legend.size) + - theme(axis.ticks.x = element_blank(), - axis.text.x = element_blank(),strip.text.x = element_text( - size = 5, color = "black",angle = 15))+ + theme_msstats("PROFILEPLOT", x.axis.size, y.axis.size, legend.size) + + theme(axis.ticks.x = element_blank(),axis.title.x = element_text(size=14),axis.title.y = element_text(size=14), + axis.text.x = element_blank(),strip.text.x = element_text( + size = 5, color = "black",angle = 15)) guides(color = guide_legend(title = paste("# peptide:", nlevels(single_protein$PeptideSequence)), title.theme = element_text(size = 13, angle = 0), keywidth = 0.4, @@ -383,7 +375,7 @@ dataProcessPlotsTMT = function( legend.size, legend.title = element_blank()) + theme(axis.ticks.x = element_blank(), axis.text.x = element_blank(),strip.text.x = element_text( - size = 5, color = "black",angle = 15))+ + size = 5, color = "black",angle = 15),axis.title.x = element_text(size=14),axis.title.y = element_text(size=14))+ guides(color = guide_legend(order = 1, title = NULL, label.theme = element_text(size = 10, angle = 0))) @@ -537,24 +529,15 @@ dataProcessPlotsTMT = function( } facet_strip_bigger <- function(gp){ - - # n_facets should be the number of facets x2 - n_facets <- c(1:length(gp[["x"]][["layout"]][["shapes"]])) if (!is.null(gp$x$layout$annotations)) { for (i in seq_along(gp$x$layout$annotations)) { - gp$x$layout$annotations[[i]]$font$size <- 7 - # gp$x$layout$annotations[[i]]$xanchor <- "center" - gp$x$layout$annotations[[i]]$xshift <- 50 + if(gp$x$layout$annotations[[i]]$text != "Log2-intensities" && gp$x$layout$annotations[[i]]$text != "MS runs") { + gp$x$layout$annotations[[i]]$font$size <- 7 + # gp$x$layout$annotations[[i]]$xanchor <- "center" + gp$x$layout$annotations[[i]]$xshift <- 50 + } } } - # for(i in n_facets){ - # if(n_facets[i] %% 2 == 0){ - # gp[["x"]][["layout"]][["shapes"]][[i]][["y0"]] <- + 80 # increase as needed - # # gp[["x"]][["layout"]][["shapes"]][[i]][["x1"]] <- +50 - # - # } - # } - return(gp) } From 23c193bcbd72e3dfbc0252bed03c06876817f4d9 Mon Sep 17 00:00:00 2001 From: deril2605 Date: Wed, 24 Apr 2024 22:47:13 -0400 Subject: [PATCH 7/9] code cleanup --- R/dataProcessPlotsTMT.R | 52 ++--------------------------------------- 1 file changed, 2 insertions(+), 50 deletions(-) diff --git a/R/dataProcessPlotsTMT.R b/R/dataProcessPlotsTMT.R index ca696fe..075496b 100644 --- a/R/dataProcessPlotsTMT.R +++ b/R/dataProcessPlotsTMT.R @@ -104,9 +104,8 @@ dataProcessPlotsTMT = function( if("original_plot" %in% names(plots)) { for(i in seq_along(plots[["original_plot"]])) { plot_i <- plots[["original_plot"]][[paste("plot",i)]] - og_plotly_plot <- .convertGgplot2Plotly(plot_i) + og_plotly_plot <- .convertGgplot2Plotly(plot_i, tips=c("PSM","xorder","abundance","censored")) og_plotly_plot = .fixLegendPlotlyPlotsDataprocess(og_plotly_plot, "OriginalPlot") - og_plotly_plot = .fixCensoredPointsLegendProfilePlotsPlotly(og_plotly_plot) plotly_plots = c(plotly_plots, list(og_plotly_plot)) } } @@ -252,9 +251,6 @@ dataProcessPlotsTMT = function( if ( check.length > 0 ){ cbp = rep(cbp, times=check.length + 1) } - # ptemp = ggplot(single_protein, aes(x = xorder, y = abundance, shape = censored)) + - # geom_point(size = dot.size.profile) + - # scale_shape_manual(values = c(16, 1)) ptemp = ggplot(aes_string(x = 'xorder', y = 'abundance', color = 'PSM', linetype = 'PSM'), data = single_protein) + facet_grid(~Run) + @@ -279,7 +275,7 @@ dataProcessPlotsTMT = function( theme_msstats("PROFILEPLOT", x.axis.size, y.axis.size, legend.size) + theme(axis.ticks.x = element_blank(),axis.title.x = element_text(size=14),axis.title.y = element_text(size=14), axis.text.x = element_blank(),strip.text.x = element_text( - size = 5, color = "black",angle = 15)) + size = 5, color = "black",angle = 15)) + guides(color = guide_legend(title = paste("# peptide:", nlevels(single_protein$PeptideSequence)), title.theme = element_text(size = 13, angle = 0), keywidth = 0.4, @@ -569,15 +565,6 @@ facet_strip_bigger <- function(gp){ ) ) converted_plot <- facet_strip_bigger(converted_plot) - # converted_plot <- converted_plot %>% layout(xaxis = list(tickangle = -170)) - # print(converted_plot$x$data) - # print(converted_plot$x$data[[30]]$text) - # for (i in seq_along(converted_plot$x$data)) { - # if (converted_plot$x$data[[i]]$mode == "text") { # Make sure to only adjust text annotations - # converted_plot$x$data[[i]]$textfont$angle <- -90 # Set your desired angle here - # } - # } - converted_plot } @@ -657,41 +644,6 @@ getFileName = function(name_base, file_name, width, height) { return(file_path) } -.fixCensoredPointsLegendProfilePlotsPlotly = function(plot) { - # df <- data.frame(id = seq_along(plot$x$data), legend_entries = unlist(lapply(plot$x$data, `[[`, "name"))) - # bool_values <- sapply(df$legend_entries, function(x) { - # split_string <- strsplit(x, ",")[[1]] - # as.logical(gsub("[)]", "", split_string[2])) - # }) - # - # # Finding the index of the first occurrence of TRUE - # first_true_index <- which(bool_values == TRUE)[1] - # - # # Finding the index of the first occurrence of FALSE - # first_false_index <- which(bool_values == FALSE)[1] - # print(paste("First TRUE index:", first_true_index)) - # print(paste("First FALSE index:", first_false_index)) - # - # # Update plot data for the first occurrence of "FALSE" - # if (!is.na(first_false_index)) { - # plot$x$data[[first_false_index]]$name <- "Detected data" - # plot$x$data[[first_false_index]]$showlegend <- TRUE - # } - # - # # Update plot data for the first occurrence of "TRUE" - # if (!is.na(first_true_index)) { - # plot$x$data[[first_true_index]]$name <- "Censored missing data" - # plot$x$data[[first_true_index]]$showlegend <- TRUE - # } - # # plot$x$data <- lapply(plot$x$data, function(item) { - # # if (!is.null(item$marker$symbol) && item$marker$symbol == "circle-open") { - # # item$showlegend <- TRUE # Set showlegend to TRUE - # # } - # # return(item) # Return the modified or unmodified item - # # }) - plot -} - #' @keywords internal .getXAxisOrder = function(processed) { From de8bd830a3bee3ac97997ea1ca66d4b7482abdd6 Mon Sep 17 00:00:00 2001 From: deril2605 Date: Fri, 26 Apr 2024 09:54:11 -0400 Subject: [PATCH 8/9] add featureName to profile plots --- R/dataProcessPlotsTMT.R | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/R/dataProcessPlotsTMT.R b/R/dataProcessPlotsTMT.R index 075496b..a49ffd7 100644 --- a/R/dataProcessPlotsTMT.R +++ b/R/dataProcessPlotsTMT.R @@ -16,6 +16,9 @@ #' @param type choice of visualization. "ProfilePlot" represents profile plot of log intensities across MS runs. #' "QCPlot" represents box plots of log intensities across channels and MS runs. #' @param ylimUp upper limit for y-axis in the log scale. +#' @param featureName for "ProfilePlot" only, "Transition" (default) means +#' printing feature legend in transition-level; "Peptide" means printing feature +#' legend in peptide-level; "NA" means no feature legend printing. #' FALSE(Default) for Profile Plot and QC Plot uses the upper limit as rounded off maximum of log2(intensities) after normalization + 3.. #' @param ylimDown lower limit for y-axis in the log scale. FALSE(Default) for Profile Plot and QC Plot uses 0.. #' @param x.axis.size size of x-axis labeling for "Run" and "channel in Profile Plot and QC Plot. @@ -60,7 +63,7 @@ #' # height = 7) #' dataProcessPlotsTMT = function( - data, type, ylimUp = FALSE, ylimDown = FALSE, + data, type,featureName = "Transition", ylimUp = FALSE, ylimDown = FALSE, x.axis.size = 10, y.axis.size = 10, text.size = 2, text.angle = 90, legend.size = 7, dot.size.profile = 2, ncol.guide = 5, width = 10, height = 10, which.Protein = "all", originalPlot = TRUE, summaryPlot = TRUE, @@ -91,7 +94,7 @@ dataProcessPlotsTMT = function( } if (toupper(type) == "PROFILEPLOT") { - plots <- .plotProfileTMT(processed, summarized, + plots <- .plotProfileTMT(processed, summarized, featureName, ylimUp, ylimDown, x.axis.size, y.axis.size, text.size, text.angle, legend.size, dot.size.profile, ncol.guide, width, height, which.Protein, @@ -106,6 +109,9 @@ dataProcessPlotsTMT = function( plot_i <- plots[["original_plot"]][[paste("plot",i)]] og_plotly_plot <- .convertGgplot2Plotly(plot_i, tips=c("PSM","xorder","abundance","censored")) og_plotly_plot = .fixLegendPlotlyPlotsDataprocess(og_plotly_plot, "OriginalPlot") + if(toupper(featureName) == "NA") { + og_plotly_plot = .removeLegendFeatureNameNA(og_plotly_plot) + } plotly_plots = c(plotly_plots, list(og_plotly_plot)) } } @@ -114,6 +120,9 @@ dataProcessPlotsTMT = function( plot_i <- plots[["summary_plot"]][[paste("plot",i)]] summ_plotly_plot <- .convertGgplot2Plotly(plot_i) summ_plotly_plot = .fixLegendPlotlyPlotsDataprocess(summ_plotly_plot, "SummaryPlot") + if(toupper(featureName) == "NA") { + og_plotly_plot = .removeLegendFeatureNameNA(og_plotly_plot) + } plotly_plots = c(plotly_plots, list(summ_plotly_plot)) } } @@ -165,7 +174,7 @@ dataProcessPlotsTMT = function( #' @importFrom MSstats theme_msstats savePlot #' @keywords internal -.plotProfileTMT = function(processed, summarized, +.plotProfileTMT = function(processed, summarized, featureName, ylimUp, ylimDown, x.axis.size, y.axis.size, text.size, text.angle, legend.size, dot.size.profile, ncol.guide, width, height, which.Protein, @@ -251,8 +260,16 @@ dataProcessPlotsTMT = function( if ( check.length > 0 ){ cbp = rep(cbp, times=check.length + 1) } + + featureName = toupper(featureName) + if (featureName == "TRANSITION") { + feature_type = "PSM" + } else { + feature_type = "PeptideSequence" + } + ptemp = ggplot(aes_string(x = 'xorder', y = 'abundance', - color = 'PSM', linetype = 'PSM'), data = single_protein) + + color = feature_type, linetype = 'PSM'), data = single_protein) + facet_grid(~Run) + geom_point(data = single_protein, aes(shape=censored), size=dot.size.profile) + # geom_point(size=dot.size.profile) + @@ -630,6 +647,14 @@ facet_strip_bigger <- function(gp){ plot } +.removeLegendFeatureNameNA = function(plot) { + df <- data.frame(id = seq_along(plot$x$data), legend_entries = unlist(lapply(plot$x$data, `[[`, "name"))) + for (i in seq_along(plot$x$data)) { + plot$x$data[[i]]$showlegend <- FALSE + } + plot +} + getFileName = function(name_base, file_name, width, height) { all_files = list.files(".") if(file_name == 'ProfilePlot'){ From afc5fb493b2b63037014b6550fd8a56cbf4c1290 Mon Sep 17 00:00:00 2001 From: deril2605 Date: Fri, 26 Apr 2024 10:02:40 -0400 Subject: [PATCH 9/9] fix docs --- man/MSstatsTMT.Rd | 3 ++- man/dataProcessPlotsTMT.Rd | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/man/MSstatsTMT.Rd b/man/MSstatsTMT.Rd index 90a6762..c2616b7 100644 --- a/man/MSstatsTMT.Rd +++ b/man/MSstatsTMT.Rd @@ -30,10 +30,11 @@ Useful links: } \author{ -\strong{Maintainer}: Ting Huang \email{thuang0703@gmail.com} +\strong{Maintainer}: Devon Kohler \email{kohler.d@northeastern.edu} Authors: \itemize{ + \item Ting Huang \email{thuang0703@gmail.com} \item Meena Choi \email{mnchoi67@gmail.com} \item Mateusz Staniak \email{mtst@mstaniak.pl} \item Sicheng Hao \email{hao.sic@husky.neu.edu} diff --git a/man/dataProcessPlotsTMT.Rd b/man/dataProcessPlotsTMT.Rd index 005d9cd..fcd348a 100644 --- a/man/dataProcessPlotsTMT.Rd +++ b/man/dataProcessPlotsTMT.Rd @@ -7,6 +7,7 @@ dataProcessPlotsTMT( data, type, + featureName = "Transition", ylimUp = FALSE, ylimDown = FALSE, x.axis.size = 10, @@ -31,9 +32,13 @@ dataProcessPlotsTMT( \item{type}{choice of visualization. "ProfilePlot" represents profile plot of log intensities across MS runs. "QCPlot" represents box plots of log intensities across channels and MS runs.} -\item{ylimUp}{upper limit for y-axis in the log scale. +\item{featureName}{for "ProfilePlot" only, "Transition" (default) means +printing feature legend in transition-level; "Peptide" means printing feature +legend in peptide-level; "NA" means no feature legend printing. FALSE(Default) for Profile Plot and QC Plot uses the upper limit as rounded off maximum of log2(intensities) after normalization + 3..} +\item{ylimUp}{upper limit for y-axis in the log scale.} + \item{ylimDown}{lower limit for y-axis in the log scale. FALSE(Default) for Profile Plot and QC Plot uses 0..} \item{x.axis.size}{size of x-axis labeling for "Run" and "channel in Profile Plot and QC Plot.}