Skip to content

Instantly share code, notes, and snippets.

@chasemc
Created November 18, 2019 18:12
Show Gist options
  • Save chasemc/d970fa7f08889648f135cb84601fc21b to your computer and use it in GitHub Desktop.
Save chasemc/d970fa7f08889648f135cb84601fc21b to your computer and use it in GitHub Desktop.
Example of extracted ion chromatograms
library(ggplot2)
library(mzR)
mass <- c( 654.3920, 668.3920)
numCores <- parallel::detectCores()
numCores <- ifelse(numCores > 2,
numCores - 1,
1)
cl <- parallel::makeCluster(numCores)
abc <- parallel::parLapply(cl,
mass,
function(mass){
tol <- .05
base_dir <- "C:/Users/chase/Documents/GitHub/labNotebook"
mzml <- list.files(file.path(base_dir,
"data/untargeted_sponge_discovery/p114/crude_xad16_extract/qtof/mzml_centroid_vendor"),
pattern = "mzML",
full.names = T)
mzml <- mzml[3:5]
spec <- lapply(mzml, mzR::openMSfile)
names(spec) <- sapply(basename(mzml), function(x) strsplit(x, "_")[[1]][[1]])
lapply(spec,
function(x){
pp <- mzR::header(x)
ms1 <- pp$msLevel == 1
ret <- pp$retentionTime[ms1]
scans <- mzR::spectra(x)[ms1]
int <- sapply(scans, function(x) {
a <- x[,1] < mass + tol
b <- x[,1] > mass - tol
sum(x[which(a+b == 2),2])
})
return(data.frame(ret = ret,
int = int,
mass = mass))
})
})
parallel::stopCluster(cl)
abc2 <- lapply(abc,
function(a){
a1 <- lapply(a, cbind.data.frame)
a1 <- lapply(seq_along(a1), function(x) cbind.data.frame(a1[[x]], facet = names(a1)[[x]]))
do.call(rbind.data.frame, a1)
})
abc3 <- do.call(rbind.data.frame, abc2)
cbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
ggplot(data = abc3) +
geom_line(aes(x = ret,
y = int,
col = factor(mass)),
size = 1) +
scale_colour_manual(values = cbPalette,
name = bquote(italic("m/z"))) +
scale_x_continuous(expand = c(0,0))+
scale_y_continuous(labels = function(x) format(x, scientific = TRUE, digits = 1),
breaks = function(x) c(0, max(x)/2 , max(x))) +
xlab("Retention (s)") +
ylab("Intensity") +
facet_wrap(~facet,
ncol = 1,
scale = "free_y") +
ggtitle("Extracted Ion Chromatograms")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment