Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into DO-MS-DIA
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgWa committed Jul 30, 2023
2 parents 547a174 + 988f658 commit 593d56a
Show file tree
Hide file tree
Showing 23 changed files with 2,209 additions and 930 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
init <- function() {

type <- 'plot'
box_title <- 'MS2 Intensity for Intersected Precursors, summed over all Channels.'
help_text <- 'Plotting the MS2 intensity for all precursors summed over all channels. Only intersected precursors present in all loaded experiments are shown.'
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')))
validate(need(config[['ChemicalLabels']], paste0('Please provide a list of labels under the key: ChemicalLabels in the settings.yaml file')))

}

.plotdata <- function(data, input) {
plotdata <- data()[['report']][,c('Raw.file', 'Precursor.Quantity','Precursor.Id')]
labelsdata <- config[['ChemicalLabels']]

# iterate over labels and calculate label-less ID
for (i in 1:length(labelsdata)){
current_label <- labelsdata[[i]]

# subtract all label modifications, but not other modifications
plotdata$Precursor.Id = gsub(paste0('\\Q',current_label,'\\E'),'',plotdata$Precursor.Id)
}

plotdata %>%
group_by(Raw.file, Precursor.Id) %>%
summarise(Precursor.Quantity = sum(Precursor.Quantity), .groups = "drop")


plotdata <- dplyr::filter(plotdata, Precursor.Quantity>1)
plotdata$Intensity <- log10(plotdata$Precursor.Quantity)

# Remove runs with less than 200 IDs
r_t<-table(plotdata$Raw.file)
r_rmv<-names(r_t)[r_t<2]
plotdata<-plotdata[!plotdata$Raw.file%in%r_rmv, ]



# 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)

plotdata <- dplyr::filter(plotdata, is.finite(Intensity))

if(nrow(plotdata) > 0){
plotdata[plotdata$Intensity >= ceiling, 2] <- ceiling
plotdata[plotdata$Intensity <= floor, 2] <- floor

#Assemble list of peptides in each Raw file
plotdata$Raw.file <- factor(plotdata$Raw.file)
expsInDF <- unique(plotdata$Raw.file)
peplist <- list()
for (i in 1:length(expsInDF)){
peptidesDF <- dplyr::filter(plotdata, Raw.file == expsInDF[i])
peptides <- dplyr::select(peptidesDF, Precursor.Id)
peplist[[i]] <- peptides
}

#Get intersection of all peptides
intersectList <- as.vector(Reduce(intersect, peplist))

#Get reduced dataframe for elements that match intersection
plotdata_Intersected <- dplyr::filter(plotdata, Precursor.Id %in% intersectList$Precursor.Id)

plotdata <- plotdata_Intersected
}
return(plotdata)

}

.plot <- function(data, input) {
.validate(data, input)
plotdata <- .plotdata(data, input)

validate(need(nrow(plotdata) > 19, paste0('Less than 20 peptides in common')),
need((nrow(plotdata) > 1), paste0('No Rows selected')))

medianData = plotdata %>% group_by(Raw.file) %>%
summarise(median = median(Intensity), .groups = "drop")

ggplot(plotdata, aes(Intensity)) +
facet_wrap(~Raw.file, nrow = 1, scales = "free_x") +
geom_histogram(bins=50, fill=custom_colors[[6]]) +
geom_text(data=medianData,
aes(label=paste0("median: ", round(median,2))), x = -Inf, y = -Inf, colour=custom_colors[[1]],
hjust = 0, vjust=0)+
geom_vline(data=medianData, aes(xintercept = median),col=custom_colors[[1]],size=1) +
xlim(0,6)+
coord_flip() +
labs(x=expression(bold('Log'[10]*' Precursor Intensity')), 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,
plot_height=340,
dynamic_width=200,
dynamic_width_base=50
))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
init <- function() {

type <- 'plot'
box_title <- 'Normalized MS2 Intensity for Intersected Precursors'
help_text <- 'Plotting the MS2 Intensity for intersected precursors summed over all channels. Experiments are normalized to the first experiment. '
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')))
validate(need(config[['ChemicalLabels']], paste0('Please provide a list of labels under the key: ChemicalLabels in the settings.yaml file')))

}

.plotdata <- function(data, input) {
plotdata <- data()[['report']][,c('Raw.file', 'Precursor.Quantity','Precursor.Id')]
labelsdata <- config[['ChemicalLabels']]

# iterate over labels and calculate label-less ID
for (i in 1:length(labelsdata)){
current_label <- labelsdata[[i]]

# subtract all label modifications, but not other modifications
plotdata$Precursor.Id = gsub(paste0('\\Q',current_label,'\\E'),'',plotdata$Precursor.Id)
}

plotdata %>%
group_by(Raw.file, Precursor.Id) %>%
summarise(Precursor.Quantity = sum(Precursor.Quantity), .groups = "drop")

plotdata <- dplyr::filter(plotdata, Precursor.Quantity>0)
plotdata$Intensity = plotdata$Precursor.Quantity

# Remove runs with less than 200 IDs
r_t<-table(plotdata$Raw.file)
r_rmv<-names(r_t)[r_t<2]
plotdata<-plotdata[!plotdata$Raw.file%in%r_rmv, ]

# 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)

plotdata <- dplyr::filter(plotdata, is.finite(Intensity))

plotdata[plotdata$Intensity >= ceiling, 2] <- ceiling
plotdata[plotdata$Intensity <= floor, 2] <- floor

#Assemble list of peptides in each Raw file
plotdata$Raw.file <- factor(plotdata$Raw.file)
expsInDF <- levels(plotdata$Raw.file)
peplist <- list()
for (i in 1:length(expsInDF)){
peptidesDF <- dplyr::filter(plotdata, Raw.file == expsInDF[i])
peptides <- dplyr::select(peptidesDF, Precursor.Id)
peplist[[i]] <- peptides
}

#Get intersection of all peptides
intersectList <- as.vector(Reduce(intersect, peplist))

#Get reduced dataframe for elements that match intersection
plotdata_Intersected <- dplyr::filter(plotdata, Precursor.Id %in% intersectList$Precursor.Id)



plotdata <- plotdata_Intersected %>% group_by(Raw.file, Precursor.Id) %>%
summarize(IntAvg=mean(Intensity, na.rm = TRUE), .groups = "drop")

plotdata.w <- reshape2::dcast(plotdata, Precursor.Id ~ Raw.file, value = "IntAvg")
baselineInd <- ncol(plotdata.w) + 1
plotdata.w$baseline <- plotdata.w[,2]

for (j in 2:(baselineInd-1)){
plotdata.w[,j] <- log10(plotdata.w[,j])-log10(plotdata.w[,baselineInd])

}

plotdata.w.m <- melt(plotdata.w)
plotdata.w.m.clean <- plotdata.w.m %>% filter(!(variable == "baseline"))
colnames(plotdata.w.m.clean) <- c("Precursor.Id","Raw.file","Intensity")
plotdata <- plotdata.w.m.clean

return(plotdata)
}

.plot <- function(data, input) {
.validate(data, input)
plotdata <- .plotdata(data, input)
validate(need(nrow(plotdata) > 19, paste0('Less than 20 peptides in common')))

ggplot(plotdata, aes(Intensity)) +
facet_wrap(~Raw.file, nrow = 1, scales = "free_x") +
geom_histogram(bins=50, fill=custom_colors[[6]]) +
xlim(c(-3,3))+
coord_flip() +
labs(x=expression(bold('Log'[10]*' Intensity Relative to first experiment')), 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
))
}
68 changes: 68 additions & 0 deletions modules/dia-nn/020_Ion_Sampling/instrument_09_DIA_MS2_vs_MZ.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
init <- function() {

type <- 'plot'
box_title <- 'MS2 Intensity vs M/Z'
help_text <- 'Plotting the MS2 Intensity versus Mass/Charge ratio. '
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')))
validate(need(config[['ChemicalLabels']], paste0('Please provide a list of labels under the key: ChemicalLabels in the settings.yaml file')))

}

.plotdata <- function(data, input) {
plotdata <- data()[['report']][,c('Raw.file', 'Precursor.Quantity','Precursor.Mz','Precursor.Id')]
labelsdata <- config[['ChemicalLabels']]


plotdata <- plotdata %>% filter(Precursor.Quantity != 0)
# Remove runs with less than 200 IDs
r_t<-table(plotdata$Raw.file)
r_rmv<-names(r_t)[r_t<2]
plotdata<-plotdata[!plotdata$Raw.file%in%r_rmv, ]



plotdata$Precursor.Mz <- round(plotdata$Precursor.Mz/25)*25
plotdata <- plotdata %>%
dplyr::group_by(Raw.file, Precursor.Mz) %>%
dplyr::summarise(Precursor.Quantity = median(Precursor.Quantity,na.rm=T), .groups = "drop")

plotdata$Intensity <- plotdata$Precursor.Quantity




return(plotdata)
}

.plot <- function(data, input) {
.validate(data, input)
plotdata <- .plotdata(data, input)

validate(need(nrow(plotdata) > 2, paste0('Less than 20 peptides in common')),
need((nrow(plotdata) > 1), paste0('No Rows selected')))

ggplot(plotdata, aes(y=log10(Intensity),x= Precursor.Mz)) +
facet_wrap(~Raw.file, nrow = 1) +
geom_line() +
coord_flip() +
labs(y=expression(bold('Median Log'[10]*' MS2 Intensity')), x='Precursor M/Z') +
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
))
}
Loading

0 comments on commit 593d56a

Please sign in to comment.