From 60d89d3e7d8a04b03831f6ec2e7799fd009f33fd Mon Sep 17 00:00:00 2001 From: Orhun Kok <59579802+OrhunKok@users.noreply.github.com> Date: Sat, 28 Oct 2023 09:06:52 -0400 Subject: [PATCH] Add files via upload MS2:MS1 Ratio per precursor. Log2 Scale. --- .../instrument_20_DIA_MS1_MS2_Ratio.R | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 modules/dia-nn/020_Ion_Sampling/instrument_20_DIA_MS1_MS2_Ratio.R diff --git a/modules/dia-nn/020_Ion_Sampling/instrument_20_DIA_MS1_MS2_Ratio.R b/modules/dia-nn/020_Ion_Sampling/instrument_20_DIA_MS1_MS2_Ratio.R new file mode 100644 index 0000000..6690962 --- /dev/null +++ b/modules/dia-nn/020_Ion_Sampling/instrument_20_DIA_MS1_MS2_Ratio.R @@ -0,0 +1,66 @@ +init <- function() { + + type <- 'plot' + box_title <- 'MS2/MS1 Intensity Ratio per precursor' + help_text <- 'Plotting the MS2/MS1 Intensity Ratio for all precursors.' + source_file <- 'report' + + .validate <- function(data, input) { + validate(need(data()[['report']], paste0('Upload report.txt'))) + validate(need((nrow(data()[['report']]) > 1), paste0('No Rows selected'))) + + } + + .plotdata <- function(data, input) { + plotdata <- data()[['report']][,c('Raw.file', 'Ms1.Area', 'Precursor.Quantity', 'Precursor.Id')] + plotdata$Ms1.Area <- as.numeric(plotdata$Ms1.Area) + plotdata$Precursor.Quantity <- as.numeric(plotdata$Precursor.Quantity) + + plotdata <- plotdata %>% + group_by(Raw.file, Precursor.Id) %>% + mutate(Ms2.Ms1.Ratio = Precursor.Quantity / Ms1.Area) + + plotdata <- dplyr::filter(plotdata, Ms2.Ms1.Ratio>0) + plotdata$Ms2.Ms1.Ratio <- log2(plotdata$Ms2.Ms1.Ratio) + # Thresholding data at 1 and 99th percentiles + ceiling <- quantile(plotdata$Ms2.Ms1.Ratio, probs=.99, na.rm = TRUE) + floor <- quantile(plotdata$Ms2.Ms1.Ratio, probs=.01, na.rm = TRUE) + + plotdata <- dplyr::filter(plotdata, is.finite(Ms2.Ms1.Ratio)) + if(nrow(plotdata) > 0){ + plotdata[plotdata$Ms2.Ms1.Ratio >= ceiling, 2] <- ceiling + plotdata[plotdata$Ms2.Ms1.Ratio <= floor, 2] <- floor + } + return(plotdata) + } + + .plot <- function(data, input) { + .validate(data, input) + plotdata <- .plotdata(data, input) + validate(need((nrow(plotdata) > 1), paste0('No Rows selected'))) + + + medianData = plotdata %>% group_by(Raw.file) %>% + summarise(median = median(Ms2.Ms1.Ratio), .groups = "drop") + + + ggplot(plotdata, aes(Ms2.Ms1.Ratio)) + + facet_wrap(~Raw.file, nrow = 1, scales = "free_x") + + geom_histogram(bins=50, fill=custom_colors[[6]]) + + coord_flip() + + labs(x=expression(bold('Log'[2]*' Precursor Ratio')), y='Number of Precursors') + + theme_diann(input=input, show_legend=T) + } + + return(list( + type=type, + box_title=box_title, + help_text=help_text, + source_file=source_file, + validate_func=.validate, + plotdata_func=.plotdata, + plot_func=.plot, + dynamic_width=200, + dynamic_width_base=50 + )) +}