This is a guide for using TarMet with R scripts.
library(TarMet)
data("isotopes", package = "enviPat")
data("adducts", package = "enviPat")
# path of raw data
rawfiles <- list.files(system.file('rawfiles', package = 'TarMet'), full.names = TRUE) # path of data files
samples <- sapply(strsplit(rawfiles, '/'), function(s) s[length(s)]) # name of data files
rawDataset <- lapply(rawfiles, LoadData) # load raw data
# path of config file
config <- read.csv(system.file('data/targets.csv', package = 'TarMet'))
# global parameters
fineness <- 'Medium' # fineness of peak detection of MassSpecWavelet
resolution <- 50000 # resolution of your MS
shift <- 20 # parameter for alignment
segment <- 20 # parameter for alignment
# select the referance sample
sampleInd <- 1 # choose a reference file
i = 1 # use the first metabolite
# set the parameters for the specific metabolite
threshold <- 0.01
name <- config$name[i]
formula <- config$formula[i]
mz <- config$mz[i]
# adduct type
if (is.na(config$adduct[i])){
adduct <- 'M+H'
} else {
adduct <- config$adduct[i]
}
# ppm for EIC generation
if (is.na(config$ppm[i])){
ppm <- 100
} else {
ppm <- config$ppm[i]
}
# scales for peak detection
if (is.na(config$scale[i])){
scale <- 5
} else {
scale <- config$scale[i]
}
# start of retention time for EIC generation
if (is.na(config$rtmin[i])){
rtmin <- 0
} else {
rtmin <- config$rtmin[i]
}
# end of retention time for EIC generation
if (is.na(config$rtmax[i])){
rtmax <- max(rawDataset[[1]]$times)
} else {
rtmax <- config$rtmax[i]
}
# minimum peak height of a peak
if (is.na(config$height[i])){
height <- 0
} else {
height <- config$height[i]
}
# minimum SNR of a peak
if (is.na(config$snr[i])){
snr <- 5
} else {
snr <- config$snr[i]
}
pattern <- getIsotopicPattern(formula, adduct, threshold, resolution)
## done.
## Calculate profiles ... done.
## Detect centroids... done.
mzs <- pattern[,1]
targetMzRanges <- getMzRanges(mzs, resolution=resolution, ppm=ppm)
targetEICs <- list()
rtranges <- c(rtmin, rtmax)
for (j in seq_along(rawfiles)){
targetEICs[[j]] <- getMzEICs(rawDataset[[j]], rtranges=rtranges, mzranges=targetMzRanges, baseline=FALSE, smooth=FALSE)
}
theoretical <- as.numeric(pattern[,2])
targetPeaks <- getIsotopicPeaks(targetEICs[[sampleInd]], SNR.Th=snr, peakScaleRange=scale, peakThr=height, theoretical=theoretical, fineness=fineness)
plotEICs(targetEICs[[sampleInd]], targetPeaks)
outputPeakInfo <- targetPeaks$PeakInfo
knitr::kable(outputPeakInfo, format = "markdown")
| Name | Position | Start | End | Similarity |
|---|---|---|---|---|
| Peak 1 | 540.466 | 534.073 | 550.221 | 0.988 |
alignedEICs <- getAlignedEICs(targetEICs, sampleInd, shift, segment, align=TRUE)
WhtoPlot <- 1 # plot the monoisotopic feature of the targeted metabolite of all samples
EicstoPlot <- lapply(alignedEICs, function(eics){
eics[[WhtoPlot]]
})
names(EicstoPlot) <- samples
plotEICs(EicstoPlot, rtrange=c(outputPeakInfo$Start[WhtoPlot], outputPeakInfo$End[WhtoPlot]))
whPeak <- which.max(outputPeakInfo$Similarity) # Using the feature with most similar isotopic pattern
outputSamplesArea <- getQuantifiedResult(alignedEICs, outputPeakInfo$Start[whPeak], outputPeakInfo$End[whPeak])
colnames(outputSamplesArea) <- samples
knitr::kable(outputSamplesArea, format = "markdown")
| MM14_20um.mzxml | Mspos_Leaf_MM48_20uM_1-A,6_01_14611.mzdata | |
|---|---|---|
| 285.0615-285.09 | 649934.82 | 607414.37 |
| 286.0648-286.0934 | 110295.40 | 112381.49 |
| 287.0671-287.0958 | 15524.51 | 12008.88 |
| 288.0696-288.0985 | 1599.30 | 1352.85 |
| 289.0723-289.1012 | 72.45 | 91.65 |
plotStackBar(outputSamplesArea)
if (!'AssayRdata' %in% installed.packages()){
install.packages(
"https://gitlab.com/jimiwills/assay.R/raw/master/AssayR_0.1.5.tar.gz",
repos = NULL, type = "source")
install.packages(
"https://gitlab.com/jimiwills/assay.R/raw/2190202fd4bc0325421bb10c4ec42089d45e1952/AssayRdata_0.1.3.tar.gz",
repos = NULL, type = "source")
}
rawfiles <- list.files(system.file("extdata", package = "AssayRdata"), full.names = TRUE)
samples <- sapply(strsplit(rawfiles, '/'), function(s) s[length(s)])
rawDataset <- lapply(rawfiles, LoadData)
name <- 'glucose'
formula <- 'C6H12O6'
adduct <- 'M-H'
ppm <- 50
scale <- 5
rtmin <- 0
rtmax <- max(rawDataset[[1]]$times)
height <- 0
snr <- 10
tracer_element <- 'C' # type of element
tracer_isotope <- '13C' # type of isotope tracer
tracer_number <- 6 # maximum number of isotope tagger
mzs <- getMzWithTracer(formula, adduct, tracer_element, tracer_isotope, tracer_number)
targetMzRanges <- getMzRanges(mzs, resolution=resolution, ppm=ppm)
targetEICs <- list()
rtranges <- c(rtmin, rtmax)
for (j in seq_along(rawfiles)){
targetEICs[[j]] <- getMzEICs(rawDataset[[j]], rtranges=rtranges, mzranges=targetMzRanges, baseline=FALSE, smooth=FALSE)
}
theoretical <- as.numeric(pattern[,2])
targetPeaks <- getIsotopicPeaks(targetEICs[[sampleInd]], SNR.Th=snr, peakScaleRange=scale, peakThr=height, theoretical=theoretical, fineness=fineness)
plotEICs(targetEICs[[sampleInd]], targetPeaks)
outputPeakInfo <- targetPeaks$PeakInfo
knitr::kable(outputPeakInfo, format = "markdown")
| Name | Position | Start | End | Similarity |
|---|---|---|---|---|
| Peak 1 | 908.756 | 900.492 | 914.107 | NA |
| Peak 2 | 928.768 | 914.473 | 976.229 | NA |
| Peak 3 | 992.366 | 983.439 | 1003.931 | NA |
Set peak bounds manually to combine peak 1 and peak 2
targetRtLeft <- 900.492
targetRtRight <- 976.229
targetRtAxis <- 928.768
userInfo <- data.frame(Name='User', Position=targetRtAxis, Start=targetRtLeft, End=targetRtRight)
outputPeakInfo <- rbind(targetPeaks$PeakInfo[,1:ncol(userInfo)], userInfo)
knitr::kable(outputPeakInfo, format = "markdown")
| Name | Position | Start | End |
|---|---|---|---|
| Peak 1 | 908.756 | 900.492 | 914.107 |
| Peak 2 | 928.768 | 914.473 | 976.229 |
| Peak 3 | 992.366 | 983.439 | 1003.931 |
| User | 928.768 | 900.492 | 976.229 |
userArea <- round(getArea(targetEICs[[sampleInd]], targetRtLeft, targetRtRight), 2)
outputPeakArea <- {
res <- targetPeaks$PeakArea
res <- cbind(rownames(res), res)
res <- cbind(res, userArea, round(userArea/sum(userArea),3))
colnames(res)[1] <- 'mzRange'
colnames(res)[(ncol(res)-1) : ncol(res)] <- c('User','Abundance')
res
}
knitr::kable(outputPeakArea, format = "markdown")
| mzRange | Peak 1 | Peak 2 | Peak 3 | User | Abundance | |
|---|---|---|---|---|---|---|
| 179.0516-179.0606 | 179.0516-179.0606 | 1156033.85 | 4612776.52 | 15866939.16 | 5749613.18 | 0.059 |
| 180.055-180.064 | 180.055-180.064 | 34193.77 | 92606.14 | 884895.83 | 126649.13 | 0.001 |
| 181.0583-181.0673 | 181.0583-181.0673 | 75354.17 | 464183.24 | 201373.60 | 541305.15 | 0.006 |
| 182.0616-182.0707 | 182.0616-182.0707 | 0.00 | 6310.11 | 1989.10 | 6310.11 | 0.000 |
| 183.065-183.0741 | 183.065-183.0741 | 63231.63 | 373169.99 | 129932.96 | 435033.23 | 0.004 |
| 184.0683-184.0775 | 184.0683-184.0775 | 799966.50 | 3058292.11 | 118941.63 | 3872913.99 | 0.039 |
| 185.0716-185.0809 | 185.0716-185.0809 | 19486037.41 | 67679800.39 | 1317331.94 | 87534708.58 | 0.891 |
alignedEICs <- getAlignedEICs(targetEICs, sampleInd, shift, segment, align=TRUE)
WhtoPlot <- 7 # plot the M+6 feature of the targeted metabolite of all samples
EicstoPlot <- lapply(alignedEICs, function(eics){
eics[[WhtoPlot]]
})
names(EicstoPlot) <- samples
plotEICs(EicstoPlot, rtrange=c(outputPeakInfo$Start[WhtoPlot], outputPeakInfo$End[WhtoPlot]))
whPeak <- which(outputPeakInfo$Name == 'User')
outputSamplesArea <- getQuantifiedResult(alignedEICs, outputPeakInfo$Start[whPeak], outputPeakInfo$End[whPeak])
colnames(outputSamplesArea) <- samples
knitr::kable(outputSamplesArea, format = "markdown")
| 02_n1-60minutepulse1.mzML | 03_n1-5minutepulse2.mzML | 04_n1-60minutepulse2.mzML | 08_n1-60minutepulse4.mzML | 09_n1-5minutepulse5.mzML | 11_n1-5minutepulse6.mzML | |
|---|---|---|---|---|---|---|
| 179.0516-179.0606 | 5749613.18 | 7690342.68 | 3974550.17 | 6972055.88 | 6613996.47 | 9918241.30 |
| 180.055-180.064 | 126649.13 | 198314.72 | 80314.51 | 155238.23 | 149387.68 | 240990.81 |
| 181.0583-181.0673 | 541305.15 | 386873.34 | 336773.71 | 367260.01 | 342136.57 | 209746.41 |
| 182.0616-182.0707 | 6310.11 | 1303.91 | 1561.42 | 3999.24 | 0.00 | 5213.95 |
| 183.065-183.0741 | 435033.23 | 292361.46 | 278744.00 | 146440.55 | 135493.90 | 115178.45 |
| 184.0683-184.0775 | 3872913.99 | 5108263.25 | 3169319.83 | 5869383.85 | 4884388.55 | 6046015.02 |
| 185.0716-185.0809 | 87534708.58 | 119785772.96 | 69510418.47 | 136390328.11 | 115243131.74 | 132121538.22 |
plotStackBar(outputSamplesArea)