Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced error handling and debugging in plotting the frequency of peptide identifications across a chromatographic gradient. #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion modules/max_quant/010_Chromatography/chrom_01_IDfreq_by_RT.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) +
Expand Down
46 changes: 34 additions & 12 deletions modules/max_quant/020_Ion_Sampling/instrument_03_MS1_int_z_all.R
Original file line number Diff line number Diff line change
@@ -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)
Expand Down