diff --git a/modules/max_quant/010_Chromatography/chrom_01_IDfreq_by_RT.R b/modules/max_quant/010_Chromatography/chrom_01_IDfreq_by_RT.R index e8ff9de..7faabae 100644 --- a/modules/max_quant/010_Chromatography/chrom_01_IDfreq_by_RT.R +++ b/modules/max_quant/010_Chromatography/chrom_01_IDfreq_by_RT.R @@ -7,11 +7,18 @@ init <- function() { .validate <- function(data, input) { validate(need(data()[['evidence']], paste0('Upload evidence.txt'))) + + validate(need( + all(c('Retention.time', 'PEP', 'Type') %in% colnames(data()[['evidence']])), + 'Columns "Retention.time", "PEP", or "Type" not found in evidence.txt.' + )) } .plotdata <- function(data, input) { plotdata <- data()[['evidence']][,c('Raw.file', 'Retention.time', 'PEP','Type')] + print(colnames(data()[['evidence']])) plotdata <- plotdata %>% dplyr::filter(Type != "MULTI-MATCH") + validate(need((nrow(plotdata) > 0), "No valid data after filtering Type")) plotdata <- plotdata %>% dplyr::select('Raw.file', 'Retention.time', 'PEP') return(plotdata) } @@ -23,7 +30,7 @@ init <- function() { maxRT <- max(plotdata$Retention.time) ggplot(plotdata, aes(Retention.time)) + - facet_wrap(~Raw.file, nrow = 1, scales = "free_x") + + facet_wrap(~Raw.file, nrow = 1, scales = "free_x") + geom_histogram(bins=100) + coord_flip() + xlim(10, maxRT) + diff --git a/modules/max_quant/020_Ion_Sampling/instrument_03_MS1_int_z_all.R b/modules/max_quant/020_Ion_Sampling/instrument_03_MS1_int_z_all.R index 6313c03..bc57e4c 100644 --- a/modules/max_quant/020_Ion_Sampling/instrument_03_MS1_int_z_all.R +++ b/modules/max_quant/020_Ion_Sampling/instrument_03_MS1_int_z_all.R @@ -1,40 +1,62 @@ -init <- function() { +init <- function() { type <- 'plot' box_title <- 'MS1 Intensity for all ions' help_text <- 'Plotting the MS1 intensity for all ions observed (not necessarily sent to MS2).' source_file <- 'allPeptides' + # validate if the correct file is being uploaded .validate <- function(data, input) { validate(need(data()[['allPeptides']], paste0('Upload allPeptides.txt'))) } + # Plot data preparation function with an indicator message .plotdata <- function(data, input) { - plotdata <- data()[['allPeptides']][,c('Raw.file', 'Charge', 'Intensity', 'MS.MS.Count')] - plotdata$Intensity <- log10(plotdata$Intensity) - #plotdata <- plotdata[plotdata$MS.MS.Count >= 1,] + # Basic columns needed for plotting + plotdata <- data()[['allPeptides']][, c('Raw.file', 'Charge', 'Intensity')] + + # Check if MS.MS.Count or Number.of.pasef.MS.MS columns are present + msms_count_present <- "MS.MS.count" %in% colnames(data()[['allPeptides']]) + pasef_msms_present <- "Number.of.pasef.MS.MS" %in% colnames(data()[['allPeptides']]) - # Thresholding data at 1 and 99th percentiles - ceiling <- quantile(plotdata$Intensity, probs=.99, na.rm = TRUE) - floor <- quantile(plotdata$Intensity, probs=.01, na.rm = TRUE) + if (msms_count_present) { + plotdata <- data()[['allPeptides']][, c('Raw.file', 'Charge', 'Intensity', 'MS.MS.count')] + plot_type_message <- "Plot generated using MS.MS.count data." + } else if (pasef_msms_present) { + plotdata <- data()[['allPeptides']][, c('Raw.file', 'Charge', 'Intensity', 'Number.of.pasef.MS.MS')] + plot_type_message <- "Plot generated using Number.of.pasef.MS.MS data." + } else { + # if neither column is present, show validation message and stop + validate(need(FALSE, "Neither MS.MS.count nor Number.of.pasef.MS.MS is available. Cannot generate plot.")) + } + + # Log-transform Intensity column for better scaling + plotdata$Intensity <- log10(plotdata$Intensity) - plotdata <- dplyr::filter(plotdata, is.finite(Intensity)) + # Thresholding data at the 1st and 99th percentiles + ceiling <- quantile(plotdata$Intensity, probs = .99, na.rm = TRUE) + floor <- quantile(plotdata$Intensity, probs = .01, na.rm = TRUE) - plotdata[plotdata$Intensity >= ceiling, 3] <- ceiling + plotdata <- dplyr::filter(plotdata, is.finite(Intensity)) # Checks whether the Intensity values are finite (i.e., not Inf, -Inf, or NA). + # the colnames: Raw.file, Charge, Intensity, Number.of.pasef.MS.MS. the 3rd column is 'intensity' + # Any Intensity value greater than or equal to the ceiling/floor is replaced with the ceiling/floor value. This caps the upper/lower outliers. + plotdata[plotdata$Intensity >= ceiling, 3] <- ceiling plotdata[plotdata$Intensity <= floor, 3] <- floor return(plotdata) } .plot <- function(data, input) { - .validate(data, input) - plotdata <- .plotdata(data, input) + .validate(data, input) # Check if required file is uploaded + plotdata <- .plotdata(data, input) # Prepare the data for plotting + # is there data to plot? validate(need((nrow(plotdata) > 1), paste0('No Rows selected'))) + # generate the plot ggplot(plotdata, aes(Intensity)) + facet_wrap(~Raw.file, nrow = 1, scales = "free_x") + - geom_histogram(bins=30) + + geom_histogram(bins = 30) + coord_flip() + labs(x=expression(bold('Log'[10]*' Precursor Intensity')), y='Number of Ions') + theme_base(input=input)