|
| 1 | +#// ********************************************************************************************** |
| 2 | +#// curateXICplot.R |
| 3 | +#// ********************************************************************************************** |
| 4 | +#// |
| 5 | +#// |
| 6 | +#// ********************************************************************************************** |
| 7 | +#// @Maintainer: Justin Sing |
| 8 | +#// @Author: Justin Sing |
| 9 | + |
| 10 | +#' @export |
| 11 | +#' @title Master Plotting function for plotting XIC's |
| 12 | +#' @description This function can be used to draw XIC's by calling getXIC |
| 13 | +#' |
| 14 | +#' @param pep A character vector of a peptide sequence(s). |
| 15 | +#' @param uni_mod A character vector of a modified peptide sequence. (Default: NULL) If using this argument, pep must be a single peptide |
| 16 | +#' @param in_sqMass A character vector. Full path to chromatogram file. |
| 17 | +#' @param df_lib A dataframe containing library information. |
| 18 | +#' @param in_osw A character vector. Full path to an osw results file. |
| 19 | +#' @param df_osw A dataframe containg OpenSwath Results Information |
| 20 | +#' @param plotPrecursor A logical value. True will plot precursor chromatogram |
| 21 | +#' @param plotDetecting A logcail value. True will plot detecting transitions. |
| 22 | +#' @param plotIdentifying A logical value. True will plot identifying transitions. |
| 23 | +#' @param plotIdentifying.Unique A logical value. True will plot unique identifying transitions. |
| 24 | +#' @param plotIdentifying.Shared A logical value. True will plot shared identifying transitions. (Decaprecated) |
| 25 | +#' @param plotIdentifying.Against A logical value. True will plot against identifying transitions. (Decaprecated) |
| 26 | +#' @param smooth_chromatogram A list containing p (numeric) for the polynomial order for sgolay, and n (numeric) the bandwidth for sgolay. (Defualt: list(p=4, n=9) |
| 27 | +#' @param doFacetZoom A logical valie. Should the plot be zoomed in. The default zooming operation is based on the max int divided by 4. (Default: FALSE) |
| 28 | +#' @param FacetFcnCall A facet_zoom function with user defined parameters. i.e. FacetFcnCall = facet_zoom(xlim = c(7475, 7620), ylim = c(0, 4000) ). (Default: NULL) |
| 29 | +#' @param doPlot A logical value. TRUE will perform steps to save the plot as a pdf. |
| 30 | +#' @param Charge_State A numeric value. The target charge state. (Default: NULL) |
| 31 | +#' @param store_plots_subdir A character vector. The location to store plots. |
| 32 | +#' @param printPlot A logical value. TRUE will print plot in RStudio display. |
| 33 | +#' @param use_top_trans_pep A logical value. TRUE will rank the transitions based on the posterior error probabilities. |
| 34 | +#' @param transition_selection_list A list containing transitions to display for unique identifying. i.e. transition_selection_list <- list( y = c(3), b = c(8:10) ) |
| 35 | +#' @param show_n_transitions A numeric value. Show n number of transitions |
| 36 | +#' @param show_transition_scores A logical value. If set to TRUE, will include TRANSITION PEPs as text tag when using interactive plotly. |
| 37 | +#' @param annotate_best_pkgrp A logical value. Annotate Top Peak Group |
| 38 | +#' @param show_all_pkgrprnk A logical value. Show all feature peak-group ranks. Usually 5. (Default: 5) |
| 39 | +#' @param show_manual_annotation A dataframe with leftWidth and rightWidth retention time boundary values of a manually annotated peak. Will draw a transparent blue shaded rectangle indicating manual annotation. I.e data.frame(leftWidth=300, rightWidth=330) |
| 40 | +#' @param show_legend A logical value. Display legend information for transition id, m/z and charge. (Default: TRUE) |
| 41 | +#' |
| 42 | +#' @return A ggplot-grobs table of a XIC |
| 43 | +#' |
| 44 | +#' @author Justin Sing \url{https://github.com/singjc} |
| 45 | +#' @importFrom tictoc tic toc |
| 46 | +#' @importFrom crayon blue red underline magenta bold |
| 47 | +#' @importFrom parallel mclapply detectCores |
| 48 | +#' @importFrom ggplot2 ggplot |
| 49 | +#' @importFrom dplyr %>% filter select distinct arrange |
| 50 | +#' @importFrom stringr str_replace_all |
| 51 | +#' @importFrom MazamaCoreUtils logger.isInitialized logger.info logger.error logger.warn logger.trace |
| 52 | +curateXICplot <- function( pep, |
| 53 | + uni_mod=NULL, |
| 54 | + in_sqMass, df_lib, in_osw, df_osw, |
| 55 | + plotPrecursor=T, |
| 56 | + plotDetecting=T, |
| 57 | + plotIdentifying=F, |
| 58 | + plotIdentifying.Unique=F, |
| 59 | + plotIdentifying.Shared=F, |
| 60 | + plotIdentifying.Against=F, |
| 61 | + smooth_chromatogram=list(p = 4, n = 9), |
| 62 | + doFacetZoom=F, |
| 63 | + FacetFcnCall=NULL, |
| 64 | + doPlot=T, |
| 65 | + Charge_State=NULL, |
| 66 | + store_plots_subdir = NULL, |
| 67 | + printPlot=F, |
| 68 | + use_top_trans_pep=F, |
| 69 | + transition_selection_list=NULL, |
| 70 | + show_n_transitions=NULL, |
| 71 | + show_transition_scores=FALSE, |
| 72 | + annotate_best_pkgrp=TRUE, |
| 73 | + show_all_pkgrprnk=T, |
| 74 | + show_peak_info_tbl=F, |
| 75 | + show_manual_annotation=NULL, |
| 76 | + show_legend=T, |
| 77 | + mzPntrs=NULL |
| 78 | +) { |
| 79 | + |
| 80 | + # Get XICs for Modified Peptides --------------------------------------------------------------- |
| 81 | + |
| 82 | + ## Check if logging has been initialized |
| 83 | + if( MazamaCoreUtils::logger.isInitialized() ){ |
| 84 | + mstools:::log_setup() |
| 85 | + } |
| 86 | + |
| 87 | + tictoc::tic( paste('XIC plotting for ', pep, ' peptides took: ', sep=' ')) |
| 88 | + |
| 89 | + run_name <- gsub('_osw_chrom[.]sqMass$|[.]chrom.mzML$|[.]chrom.sqMass$', '', basename(in_sqMass)) |
| 90 | + run <- gsub('_SW*|_SW_0|(*_-_SW[.]mzML[.]gz|[.]chrom[.]sqMass)', '', gsub('yanliu_I170114_\\d+_|chludwig_K150309_|lgillet_L\\d+_\\d+-Manchester_dirty_phospho_-_', '', run_name)) |
| 91 | + |
| 92 | + MazamaCoreUtils::logger.info( crayon::blue('@ Run: ', run),'\n', sep='' ) |
| 93 | + |
| 94 | + plot_chrom_error <- tryCatch({ |
| 95 | + |
| 96 | + ## Charge State |
| 97 | + Isoform_Target_Charge <- Charge_State |
| 98 | + |
| 99 | + m_score_filter_var <- ifelse( length(grep( "m_score|mss_m_score", colnames(df_osw), value = T))==2, "m_score", "ms2_m_score" ) |
| 100 | + df_osw %>% |
| 101 | + dplyr::filter( Sequence==pep ) %>% |
| 102 | + dplyr::filter( FullPeptideName==uni_mod ) %>% |
| 103 | + dplyr::filter( !is.na( !!rlang::sym(m_score_filter_var) ) ) %>% |
| 104 | + dplyr::filter( Charge==Isoform_Target_Charge ) %>% |
| 105 | + dplyr::filter( grepl(run_name, filename) ) -> tmp_osw_df |
| 106 | + |
| 107 | + if ( any(colnames(tmp_osw_df) %in% "ipf_FullPeptideName") ){ |
| 108 | + tmp_osw_df %>% |
| 109 | + dplyr::filter( ipf_FullPeptideName==mstools::unimodTocodename(uni_mod) ) -> tmp_osw_df |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + # if ( dim(tmp_osw_df)[1]==0 ){ MazamaCoreUtils::logger.error(crayon::red(pep, ' was not found in osw file!!!, skipping...\n'),sep=''); return(list()) } |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + # Filter df_lib based on only the uni modifications with specified charge state found in OSW results |
| 118 | + df_lib %>% |
| 119 | + dplyr::filter( UNMODIFIED_SEQUENCE==pep ) %>% |
| 120 | + dplyr::filter( MODIFIED_SEQUENCE==uni_mod ) %>% |
| 121 | + dplyr::filter( PRECURSOR_CHARGE==Isoform_Target_Charge ) -> df_lib |
| 122 | + |
| 123 | + |
| 124 | + # Display other peak group rank features |
| 125 | + if ( show_all_pkgrprnk ){ |
| 126 | + RT_pkgrps <- tmp_osw_df$RT |
| 127 | + } else { |
| 128 | + RT_pkgrps <- NULL |
| 129 | + } |
| 130 | + |
| 131 | + uni_mod_list <- NULL |
| 132 | + |
| 133 | + |
| 134 | + ##***********************************## |
| 135 | + ## Get TRANSITION SCORES INFO TABLE ## |
| 136 | + ##***********************************## |
| 137 | + if ( show_transition_scores ){ |
| 138 | + transition_dt <- getTransitionScores_( oswfile = in_osw, run_name = run_name, precursor_id = "", peptide_id = pep) |
| 139 | + # transition_dt_test <- getTransitionScores_( oswfile = in_osw, run_name = "chludwig_K150309_007b_SW_1_6", precursor_id = "", peptide_id = pep) |
| 140 | + } else { |
| 141 | + transition_dt <- NULL |
| 142 | + } |
| 143 | + |
| 144 | + MazamaCoreUtils::logger.info(' ~ Starting Plotting Action\n', sep='') |
| 145 | + plot_list <- list() |
| 146 | + max_Int <- 0 |
| 147 | + mod <- uni_mod |
| 148 | + tictoc::tic("Plotting: ") |
| 149 | + MazamaCoreUtils::logger.info( crayon::green(' --- Peptidoform: ', mod), '\n', sep='') |
| 150 | + ##***********************## |
| 151 | + ## PLOT PRECURSOR ## |
| 152 | + ##***********************## |
| 153 | + if ( plotPrecursor==T ){ |
| 154 | + |
| 155 | + g <- ggplot2::ggplot() |
| 156 | + g <- mstools::getXIC( graphic_obj = g, |
| 157 | + df_lib = df_lib, |
| 158 | + mod = mod, |
| 159 | + Isoform_Target_Charge = Isoform_Target_Charge, |
| 160 | + chromatogram_file = in_sqMass, |
| 161 | + transition_type = 'precursor', |
| 162 | + uni_mod_list = NULL, |
| 163 | + max_Int = max_Int, |
| 164 | + in_osw=NULL, |
| 165 | + smooth_chromatogram=smooth_chromatogram, |
| 166 | + doFacetZoom=F, |
| 167 | + top_trans_mod_list=NULL, |
| 168 | + show_n_transitions=show_n_transitions, |
| 169 | + transition_dt=transition_dt, |
| 170 | + mzPntrs=mzPntrs ) |
| 171 | + max_Int <- g$max_Int |
| 172 | + g <- g$graphic_obj |
| 173 | + } else { |
| 174 | + g <- ggplot2::ggplot() |
| 175 | + } |
| 176 | + |
| 177 | + ##*****************************## |
| 178 | + ## DETECTING TRANSITIONS ## |
| 179 | + ##*****************************## |
| 180 | + |
| 181 | + ## INTERSECTING |
| 182 | + if ( plotDetecting==T ){ |
| 183 | + g <- mstools::getXIC( graphic_obj = g, |
| 184 | + df_lib = df_lib, |
| 185 | + mod = mod, |
| 186 | + Isoform_Target_Charge = Isoform_Target_Charge, |
| 187 | + chromatogram_file = in_sqMass, |
| 188 | + transition_type = 'detecting', |
| 189 | + uni_mod_list = uni_mod_list, |
| 190 | + max_Int = max_Int, |
| 191 | + in_osw=NULL, |
| 192 | + smooth_chromatogram=smooth_chromatogram, |
| 193 | + doFacetZoom=F, |
| 194 | + top_trans_mod_list=NULL, |
| 195 | + show_n_transitions=show_n_transitions, |
| 196 | + transition_dt=transition_dt, |
| 197 | + mzPntrs=mzPntrs) |
| 198 | + max_Int <- g$max_Int |
| 199 | + g <- g$graphic_obj |
| 200 | + } |
| 201 | + |
| 202 | + ##*******************************## |
| 203 | + ## IDENTIFYING TRANSITIONS ## |
| 204 | + ##*******************************## |
| 205 | + if ( plotIdentifying==T ){ |
| 206 | + g <- mstools:: getXIC( graphic_obj = g, |
| 207 | + df_lib = df_lib, |
| 208 | + mod = mod, |
| 209 | + Isoform_Target_Charge = Isoform_Target_Charge, |
| 210 | + chromatogram_file = in_sqMass, |
| 211 | + transition_type='identifying', |
| 212 | + uni_mod_list = uni_mod_list, |
| 213 | + max_Int = max_Int, |
| 214 | + in_osw=NULL, |
| 215 | + smooth_chromatogram=smooth_chromatogram, |
| 216 | + doFacetZoom=F, |
| 217 | + top_trans_mod_list=NULL, |
| 218 | + transition_selection_list=transition_selection_list, |
| 219 | + show_n_transitions=show_n_transitions, |
| 220 | + plotIdentifying.Unique=plotIdentifying.Unique, |
| 221 | + plotIdentifying.Shared=plotIdentifying.Shared, |
| 222 | + plotIdentifying.Against=plotIdentifying.Against, |
| 223 | + transition_dt=transition_dt, |
| 224 | + mzPntrs=mzPntrs) |
| 225 | + max_Int <- g$max_Int |
| 226 | + g <- g$graphic_obj |
| 227 | + |
| 228 | + } else { |
| 229 | + MazamaCoreUtils::logger.warn(crayon::red('-- Identifying Transitions were not found for: ', crayon::underline(mod)), '\n', sep='') |
| 230 | + } |
| 231 | + tictoc::toc() |
| 232 | + ##*******************************## |
| 233 | + ## ADD OSW RESULTS INFO ## |
| 234 | + ##*******************************## |
| 235 | + g <- mstools::getXIC( graphic_obj = g, |
| 236 | + df_lib = df_lib, |
| 237 | + mod = mod, |
| 238 | + Isoform_Target_Charge = Isoform_Target_Charge, |
| 239 | + chromatogram_file = in_sqMass, |
| 240 | + transition_type='none', |
| 241 | + uni_mod_list = NULL, |
| 242 | + max_Int = max_Int, |
| 243 | + in_osw = in_osw, |
| 244 | + df_osw = tmp_osw_df, |
| 245 | + annotate_best_pkgrp=annotate_best_pkgrp, |
| 246 | + doFacetZoom=doFacetZoom, |
| 247 | + top_trans_mod_list=NULL, |
| 248 | + RT_pkgrps=RT_pkgrps, |
| 249 | + show_manual_annotation=show_manual_annotation, |
| 250 | + show_peak_info_tbl=show_peak_info_tbl, |
| 251 | + FacetFcnCall=FacetFcnCall, |
| 252 | + show_legend = show_legend ) |
| 253 | + max_Int <- g$max_Int |
| 254 | + g <- g$graphic_obj |
| 255 | + |
| 256 | + graphics.off() |
| 257 | + |
| 258 | + return( g ) |
| 259 | + |
| 260 | + }, error=function(e){ |
| 261 | + |
| 262 | + MazamaCoreUtils::logger.error(crayon::red('There was an issue trying to process ', crayon::underline(pep), ' from run: '), crayon::underline(run), '\n', sep='') |
| 263 | + stop(e$message) |
| 264 | + |
| 265 | + }) |
| 266 | + |
| 267 | +} |
| 268 | + |
0 commit comments