---
# System prepended metadata

title: XCMS peak detection

---

# XCMS peak detection
臺大生醫電資所林柏嶢

Installation
---
### Install R 
1. Download the most recent version of R. The R FAQs and the R Installation and Administration Manual contain detailed instructions for installing R on various platforms (Linux, OS X, and Windows being the main ones).
2. Start the R program; on Windows and OS X, this will usually mean double-clicking on the R application, on UNIX-like systems, type “R” at a shell prompt.
3. As a first step with R, start the R help browser by typing help.start() in the R command window. For help on any function, e.g. the “mean” function, type ? mean.

### Install Bioconductor Packages 
To install core packages, type the following in an R command window:
```R
if (!require("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install()
```
To install "XCMS" package, start R (version "4.3") and enter:
```R
if (!require("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

BiocManager::install("xcms")
```
To view documentation for the version of "XCMS" package installed in your system, start R and enter:
```R
browseVignettes("xcms")
```
choose "LCMS data preprocessing and analysis with xcms":
![](https://i.imgur.com/GdXYd15.png)

LCMS data preprocessing and analysis with xcms
---
### Data Import
```R
library(xcms)
library(RColorBrewer)
library(magrittr)
library(SummarizedExperiment)
library(readMzXmlData)
library(magrittr)
library(Spectra)
```
To install "MSnbase" package, start R (version "4.3") and enter:
```R
if (!require("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

BiocManager::install("MSnbase")
```
Move direction of "File.mzXML" under the package "readMzXmlData" 
Get the data file in msdata package
```R
file <- system.file("File_name.mzXML",
                        package = "readMzXmlData")
raw_data <- readMSData(file, mode = "onDisk")
```


## TIC 箱型分布圖

```R
tc <- split(tic(raw_data), f = fromFile(raw_data))
boxplot(tc, col = group_colors[raw_data$sample_group],
        ylab = "intensity", main = "Total ion current")
```
![](https://hackmd.io/_uploads/rk1Ho1et2.png)
## TIC圖
```R
## Get the base peak chromatograms. This reads data from the files.
bpis <- chromatogram(raw_data, aggregationFun = "sum")
## Plot all chromatograms.
plot(bpis)
```
![](https://hackmd.io/_uploads/r1jriJlY2.png)
## EIC圖(rtr = 5450-5480)
```R
## Define the rt and m/z range of the peak area
rtr <- c(5450, 5480)
mzr <- c(77, 1154)
## extract the chromatogram
chr_raw <- chromatogram(raw_data, mz = mzr, rt = rtr)
plot(chr_raw)
```
![](https://hackmd.io/_uploads/By0wskgFh.png)

![](https://hackmd.io/_uploads/HJn8i1gK2.png)
## peak原始數據視覺化
```R
raw_data %>%
    filterRt(rt = rtr) %>%
    filterMz(mz = mzr) %>%
    plot(type = "XIC")
```
![](https://hackmd.io/_uploads/S1xFoklt3.png)
## 利用centwave找peak
```R
> xchr <- findChromPeaks(chr_raw, param = CentWaveParam(snthresh = 2))
Warning message:
In serialize(data, node$con) :
  'package:stats' may not be available when loading
  
> head(chromPeaks(xchr))
          rt   rtmin   rtmax        into       intb      maxo sn row column
[1,] 1126.53 1106.31 1149.35  7161675031  595512651 195413094  6   1      1
[2,] 1314.52 1291.76 1337.42 10930483834 4032942319 354192967 62   1      1
[3,] 1691.99 1665.48 1723.95 12241405027 2589597150 243151651  8   1      1
[4,] 1800.53 1777.92 1824.01 11103091494 3377281372 336810499 15   1      1
[5,] 1892.26 1868.64 1916.15  9745887872 2185146715 272231009 24   1      1

> chromPeakData(xchr)
DataFrame with 5 rows and 4 columns
   ms_level is_filled       row    column
  <integer> <logical> <integer> <integer>
1         1     FALSE         1         1
2         1     FALSE         1         1
3         1     FALSE         1         1
4         1     FALSE         1         1
5         1     FALSE         1         1

```
## 標註peak並設置sn ratio
```R
sample_colors <- group_colors[xchr$sample_group]
plot(xchr, col = sample_colors,
     peakBg = sample_colors[chromPeaks(xchr)[, "column"]])
```
![](https://hackmd.io/_uploads/rkw5j1lFn.png)
## 信號處理
```R
> cwp <- CentWaveParam(peakwidth = c(20, 80), noise = 5000, prefilter = c(6, 5000))
> xdata <- findChromPeaks(raw_data, param = cwp)

> head(chromPeaks(xdata))
               mz     mzmin     mzmax      rt   rtmin   rtmax      into      intb     maxo    sn sample
CP00001 214.78322 214.78291 214.78360 32.5736 32.2066 67.3885 1476974.9 1419130.7 98881.38    11      1
CP00002 153.98920 153.98852 153.98988 55.6840 50.0551 66.8369  314565.6  314316.7 42804.64   121      1
CP00003 259.83954 259.83926 259.83981 54.7848 50.7856 60.3323  414399.9  414390.8 75422.00 75421      1
CP00004 408.87648 408.87585 408.87692 55.6840 50.4209 65.8763  135063.3  135052.1 19344.90 19344      1
CP00005  80.96504  80.96494  80.96511 56.9408 40.0861 71.0318  183342.5  179587.9 23295.86    21      1
CP00006  98.96429  98.96423  98.96435 56.9408 51.3237 67.3885  231917.6  231465.8 40222.71   105      1

> chromPeakData(xdata)
DataFrame with 10269 rows and 2 columns
         ms_level is_filled
        <integer> <logical>
CP00001         1     FALSE
CP00002         1     FALSE
CP00003         1     FALSE
CP00004         1     FALSE
CP00005         1     FALSE
...           ...       ...
CP10265         1     FALSE
CP10266         1     FALSE
CP10267         1     FALSE
CP10268         1     FALSE
CP10269         1     FALSE
```
## 根據一個樣品的保留時間確定 m/z
```R
plotChromPeaks(xdata)
```
![](https://hackmd.io/_uploads/Sktl21lF3.png)

## peak資訊
```R
> chr_ex <- chromatogram(xdata, mz = mzr, rt = rtr)
> chromPeaks(chr_ex)
                mz     mzmin     mzmax      rt   rtmin   rtmax         into         intb         maxo      sn sample row
CP00573  414.02662 414.02527 414.02899 1020.00 1008.76 1029.56 3.980135e+05 3.979955e+05     48335.41   48334      1   1
CP00574  403.99750 403.99600 403.99963 1020.69 1020.34 1031.81 2.264889e+05 2.264787e+05     42429.03   43276      1   1
CP00575  429.04537 429.04416 429.05051 1020.69 1007.20 1047.35 5.317195e+05 5.300225e+05     47464.35      77      1   1
CP00576  166.01755 166.01700 166.01790 1020.00 1000.22 1039.48 1.441003e+06 1.360571e+06     96657.29      25      1   1
CP00577  428.04210 428.04187 428.04504 1020.00 1004.57 1039.48 5.503697e+06 5.502905e+06    375569.09     993      1   1
CP00578  240.16063 240.15900 240.16212 1032.87 1021.89 1033.24 1.770656e+05 1.762967e+05     42124.66      40      1   1
CP00579  258.17107 258.17038 258.17142 1039.48 1019.65 1050.35 3.372856e+05 3.342481e+05     32405.90      26      1   1
CP00580  198.14935 198.14899 198.15018 1038.27 1018.44 1055.82 8.549324e+05 8.193957e+05     54138.33      17      1   1
CP00583  242.98615 242.98602 242.98639 1062.95 1050.01 1076.65 1.812556e+06 1.812530e+06    140725.98  140725      1   1
CP00586  308.15838 308.15665 308.16071 1103.47 1088.97 1124.84 9.010263e+05 8.973622e+05     58826.87      57      1   1
CP00589  291.98841 291.98666 291.98948 1102.76 1087.10 1122.45 5.370857e+06 5.370263e+06    341712.91     846      1   1
CP00591  348.91893 348.91529 348.92670 1126.53 1126.01 1142.99 4.079507e+06 3.995540e+06    981941.40      39      1   1
CP00592  720.86980 720.86450 720.87311 1127.05 1117.27 1136.45 4.369601e+05 4.369437e+05     61562.95   61562      1   1
CP00593  756.94192 756.93610 756.94598 1126.01 1116.21 1136.45 8.600201e+05 8.600002e+05     92406.80   92406      1   1
CP00594  430.94348 430.94101 430.94492 1126.53 1114.15 1138.75 6.296213e+05 6.295987e+05     55074.79   55074      1   1
CP00595   98.95521  98.95474  98.95560 1126.01 1110.69 1145.42 4.874968e+05 4.863716e+05     45978.25     103      1   1
CP00596  348.76226 348.75739 348.76721 1127.05 1114.15 1139.43 8.365823e+05 8.365583e+05     63347.85   63347      1   1
CP00597  349.11729 349.11319 349.12140 1127.05 1114.15 1140.97 8.831030e+05 8.801696e+05     62840.95      46      1   1
CP00598  351.93906 351.93823 351.94016 1126.53 1111.57 1142.48 1.143236e+06 1.143209e+06     88101.78   88101      1   1
CP00599  433.91701 433.91580 433.91794 1126.53 1113.13 1142.48 1.321640e+06 1.321613e+06     87241.27   87240      1   1
CP00600   79.95658  79.95643  79.95686 1127.05 1104.49 1150.03 2.899080e+06 2.857638e+06    183229.51      77      1   1
CP00601  348.86739 348.86096 348.87067 1126.53 1112.07 1145.76 3.357715e+06 3.356498e+06    269803.62     498      1   1
CP00602  349.01264 349.00956 349.01840 1126.53 1110.69 1144.05 3.294166e+06 3.288988e+06    241107.26     140      1   1
CP00603   87.23659  87.23653  87.23666 1126.53 1112.79 1142.99 1.692920e+06 1.692890e+06    111850.01  111849      1   1
CP00604  284.18659 284.18509 284.18726 1139.78 1128.29 1155.30 6.648265e+05 6.648036e+05     49720.16   49719      1   1
CP00605  350.93636 350.93546 350.93673 1126.53 1109.27 1146.94 2.549128e+07 2.548618e+07   1682215.04    1491      1   1
CP00606  349.94329 349.94315 349.94348 1126.53 1108.24 1146.45 2.919620e+07 2.919094e+07   1904561.25    1620      1   1
CP00607  298.20260 298.20163 298.20331 1162.70 1152.84 1175.55 3.332743e+05 3.332550e+05     36657.47   36656      1   1
CP00608  307.15511 307.15311 307.15717 1103.81 1079.77 1128.29 5.557237e+06 5.135880e+06    302354.59      27      1   1
CP00609  366.98108 366.98044 366.98175 1169.47 1158.05 1180.52 4.419095e+05 4.418891e+05     35493.66   35493      1   1
CP00610  717.99938 717.99622 718.00079 1169.97 1162.36 1176.93 2.077730e+05 2.077601e+05     33386.45   33385      1   1
CP00611  168.98168 168.98014 168.98249 1169.47 1155.30 1186.08 2.858047e+06 2.856290e+06    245717.51     269      1   1
CP00612  185.98784 185.98756 185.98824 1169.11 1158.05 1178.98 4.752630e+05 4.752429e+05     44257.94   44257      1   1
CP00613  370.95582 370.95520 370.95618 1169.97 1158.95 1179.69 9.372729e+05 9.372530e+05     97956.73   97956      1   1
CP00614  413.94538 413.94449 413.94644 1168.42 1155.98 1180.89 8.302701e+05 8.302477e+05     73932.06   73931      1   1
CP00615  103.97647 103.97636 103.97659 1168.42 1155.98 1180.89 8.965597e+05 8.965360e+05     80089.05   80088      1   1
CP00616  410.97115 410.97049 410.97177 1169.97 1156.33 1183.15 8.049183e+05 8.048945e+05     69645.88   69645      1   1
CP00617  659.94683 659.94464 659.94830 1170.85 1158.59 1180.52 1.245739e+06 1.245717e+06    131496.53  131496      1   1
CP00618  716.99709 716.99512 716.99835 1169.47 1158.05 1183.99 1.917872e+06 1.917849e+06    172905.83  172905      1   1
CP00619  168.92958 168.92790 168.93097 1170.34 1157.52 1182.81 5.563983e+05 5.563743e+05     41604.96   41604      1   1
CP00620  328.96786 328.96741 328.96838 1169.97 1155.64 1185.03 1.807909e+06 1.807883e+06    140810.28  140809      1   1
CP00621  680.92594 680.92438 680.92682 1169.97 1158.05 1181.40 2.045635e+06 2.045612e+06    198440.16  198439      1   1
CP00622  285.98103 285.98071 285.98141 1170.34 1156.67 1182.81 1.142218e+06 1.142192e+06     86813.69   86813      1   1
CP00623  168.96507 168.96414 168.96590 1170.34 1154.25 1190.15 2.467367e+06 2.465926e+06    165376.64     317      1   1
CP00624  101.00195 101.00185 101.00205 1169.97 1154.96 1187.43 1.618048e+06 1.618018e+06    111016.45  111015      1   1
CP00625  168.99620 168.99400 168.99680 1169.97 1152.84 1191.17 3.882694e+06 3.875967e+06    496143.33     423      1   1
CP00626  658.94379 658.94305 658.94434 1170.85 1156.33 1186.41 1.048224e+07 1.048221e+07    976949.81  976949      1   1
CP00627  242.18802 242.18717 242.19018 1186.08 1172.26 1202.37 6.103078e+06 5.980782e+06    780799.51      26      1   1
CP00628  169.01346 169.01226 169.01480 1170.34 1149.00 1190.83 2.518832e+06 2.500480e+06    172958.46     108      1   1
CP00629  431.96293 431.96164 431.96362 1174.14 1159.59 1189.48 1.260691e+06 1.260663e+06     88207.61   88207      1   1
CP00630  323.99443 323.99377 323.99481 1176.26 1162.00 1190.83 1.462192e+06 1.462165e+06    108070.09  108069      1   1
CP00631  428.16417 428.16257 428.16541 1183.99 1172.63 1201.53 3.171272e+05 3.151399e+05     36527.89      44      1   1
CP00632  507.34137 507.33932 507.34253 1185.40 1176.60 1193.79 2.885974e+05 2.885827e+05     38962.83   38962      1   1
CP00633  382.13834 382.13760 382.13913 1186.08 1173.30 1199.44 2.323951e+05 2.316373e+05     29112.38      47      1   1
CP00634  146.98742 146.98728 146.98749 1170.34 1153.37 1191.68 4.755749e+06 4.746197e+06    314668.94     277      1   1
CP00635  280.14940 280.14893 280.14990 1186.41 1176.60 1194.30 2.392216e+05 2.392080e+05     31136.39   31135      1   1
CP00636  301.13791 301.13733 301.13852 1185.74 1174.14 1196.61 3.531324e+05 3.528060e+05     36053.77      87      1   1
CP00637  242.27928 242.27608 242.28169 1186.08 1173.64 1197.63 7.802767e+05 7.802552e+05     60103.21   60102      1   1
CP00638  400.16530 400.16422 400.16635 1184.52 1171.73 1199.81 4.613018e+05 4.599824e+05     43206.94      59      1   1
CP00639  169.99267 169.99255 169.99282 1169.97 1151.23 1191.17 8.567885e+06 8.561490e+06    492848.47     432      1   1
CP00641  265.16120 265.16083 265.16150 1185.40 1173.64 1198.31 6.633692e+05 6.633482e+05     58462.39   58461      1   1
CP00642  242.07352 242.06995 242.07629 1185.40 1174.14 1198.92 8.103680e+05 8.103461e+05     62020.33   62019      1   1
CP00643  278.15322 278.15259 278.15356 1186.08 1173.64 1197.29 1.103253e+06 1.103230e+06     97086.02   97085      1   1
CP00644  325.18255 325.18192 325.18448 1186.08 1172.63 1206.92 6.704784e+05 6.690653e+05     50210.59      81      1   1
CP00645  406.18275 406.18188 406.18372 1186.08 1173.64 1201.53 4.606509e+05 4.600712e+05     38685.07      89      1   1
CP00646  284.97785 284.97770 284.97797 1169.97 1151.76 1190.83 2.235685e+07 2.235007e+07   1585915.81    1086      1   1
CP00647  118.99244 118.99236 118.99252 1170.34 1148.63 1191.68 5.829694e+06 5.501833e+06    339145.69      47      1   1
CP00648  302.13153 302.13113 302.13193 1185.40 1172.26 1199.44 1.030116e+06 1.030090e+06     80027.01   80026      1   1
CP00649  422.14753 422.14633 422.14853 1185.74 1170.85 1206.92 6.003514e+05 6.000055e+05     47850.74     166      1   1
CP00650  340.14590 340.14389 340.14799 1185.40 1171.22 1200.84 1.253072e+06 1.253044e+06     89242.76   89242      1   1
CP00651  346.16134 346.16071 346.16193 1185.74 1170.34 1204.30 1.117220e+06 1.117191e+06     77051.44   77050      1   1
CP00652  242.13451 242.13301 242.13671 1185.40 1169.47 1206.40 3.656942e+06 3.656317e+06    245186.15     554      1   1
CP00653  134.98744 134.98735 134.98757 1170.34 1150.03 1191.17 1.408627e+07 1.407880e+07    841154.22     807      1   1
CP00654  225.14960 225.14888 225.15047 1185.40 1164.02 1206.06 1.734845e+06 1.698659e+06    107788.79      28      1   1
CP00655  242.21762 242.21589 242.22054 1185.40 1170.34 1206.40 3.747425e+06 3.737660e+06    246075.50     127      1   1
CP00656  327.15367 327.15341 327.15427 1185.40 1170.85 1205.72 1.758679e+06 1.758647e+06    119522.83  119522      1   1
         column
CP00573       1
CP00574       1
CP00575       1
CP00576       1
CP00577       1
CP00578       1
CP00579       1
CP00580       1
CP00583       1
CP00586       1
CP00589       1
CP00591       1
CP00592       1
CP00593       1
CP00594       1
CP00595       1
CP00596       1
CP00597       1
CP00598       1
CP00599       1
CP00600       1
CP00601       1
CP00602       1
CP00603       1
CP00604       1
CP00605       1
CP00606       1
CP00607       1
CP00608       1
CP00609       1
CP00610       1
CP00611       1
CP00612       1
CP00613       1
CP00614       1
CP00615       1
CP00616       1
CP00617       1
CP00618       1
CP00619       1
CP00620       1
CP00621       1
CP00622       1
CP00623       1
CP00624       1
CP00625       1
CP00626       1
CP00627       1
CP00628       1
CP00629       1
CP00630       1
CP00631       1
CP00632       1
CP00633       1
CP00634       1
CP00635       1
CP00636       1
CP00637       1
CP00638       1
CP00639       1
CP00641       1
CP00642       1
CP00643       1
CP00644       1
CP00645       1
CP00646       1
CP00647       1
CP00648       1
CP00649       1
CP00650       1
CP00651       1
CP00652       1
CP00653       1
CP00654       1
CP00655       1
CP00656       1
 [ reached getOption("max.print") -- omitted 388 rows ]
```
## 自動突出顯示識別出的peak
```R
plot(chr_ex, peakType = "rectangle", peakBg = NA)
```
![](https://hackmd.io/_uploads/r1Q13ygt3.png)
## 以表格呈現選定的 m/z 和保留時間範圍內識別出的peak
```R
if (!require("pander", quietly = TRUE))
    install.packages("pander")
pander::install()

pander(chromPeaks(xdata, mz = mzr, rt = rtr),
       caption = paste("Identified chromatographic peaks in a selected ",
                       "m/z and retention time range."))
                       
Table: Identified chromatographic peaks in a selected  m/z and retention time range. (continued below)

 
---------------------------------------------------------
    &nbsp;        intb        maxo        sn      sample 
-------------- ----------- ----------- --------- --------
 **CP00546**     324423       37036       106       1    

 **CP00547**     135890       11490      11489      1    

 **CP00550**     398440       42227      42226      1    

 **CP00551**     389086       58121      58120      1    

 **CP00552**     358991       33551       30        1    

 **CP00553**     535904       52630       127       1    

 **CP00554**     306187       61348      61347      1    
```
#  (07/17update)LC-MS/MS data analysis with xcms
## Data import:
```R
$ dda_file <- system.file("new_cali_std_150.mzXML", package = "readMzXmlData")
$ dda_data <- readMSData(dda_file, mode = "onDisk")
```
## The number of spectra for each MS level:
```R
$ table(msLevel(dda_data))

    1     2
13593  7051 
```
## Extract the precursor m/z and show the first 6 elements:
```R
$ dda_data %>%
    filterMsLevel(2L) %>%
    precursorMz() %>%
    head()

F1.S00002 F1.S00003 F1.S00004 F1.S00017 F1.S00020 F1.S00032 
156.01245 212.11079 212.04181 212.10562 141.00084  96.96004 
```
## Extract the intensity of the precursor ion:
```R
$ dda_data %>%
    filterMsLevel(2L) %>%
    precursorIntensity() %>%
    head()
    
F1.S00002  F1.S00003  F1.S00004  F1.S00017  F1.S00020  F1.S00032 
2747194.3 78450596.6 78450596.6 74885656.6   590244.7   284270.8 
```
## Chromatographic peak detection on the MS level 1 data:
```R
$ cwp <- CentWaveParam(snthresh = 5, noise = 100, ppm = 10, peakwidth = c(3, 30))
$ dda_data <- findChromPeaks(dda_data, param = cwp)

Detecting mass traces at 10 ppm ... OK
Detecting chromatographic peaks in 248839 regions of interest ... OK: 9083 found.
```
## MS2 spectra with their precursor m/z and retention time within the rt and m/z range of the chromatographic peak:
```R
$ dda_spectra <- chromPeakSpectra(dda_data, msLevel = 2L, return.type = "Spectra")
$ dda_spectra

MSn data (Spectra) with 1076 spectra in a MsBackendMzR backend:
            msLevel     rtime scanIndex
          <integer> <numeric> <integer>
F1.S00171         2   56.7721       171
F1.S00220         2   69.7215       220
F1.S00171         2   56.7721       171
F1.S00220         2   69.7215       220
F1.S00170         2   56.6784       170
...             ...       ...       ...
F1.S20516         2   5680.51     20516
F1.S20594         2   5690.98     20594
F1.S20520         2   5681.25     20520
F1.S20599         2   5691.79     20599
F1.S20615         2   5694.22     20615
 ... 38 more variables/columns.

file(s):
new_cali_std_150.mzXML
```
## Evaluate the full MS1 signal at the peak’s apex position:
```R
$ dda_spectra$peak_id

[1] "CP0433" "CP0434" "CP0436" "CP0437" "CP0438" "CP0439" "CP0441" "CP0494" "CP0495" "CP0502" "CP0523" "CP0547"
[13] "CP0590" "CP0605" "CP0609" "CP0627" "CP0654" "CP0667" "CP0673" "CP0683" "CP0693" "CP0699" "CP0835" "CP0858"
[25] "CP0873" "CP0873" "CP0874" "CP0875" "CP0877" "CP0878" "CP0954" "CP0954" "CP0955" "CP1063" "CP1063" "CP1063"
[37] "CP1064" "CP1081" "CP1081" "CP1082" "CP1083" "CP1104" "CP1137" "CP1142" "CP1153" "CP1162" "CP1163" "CP1171"
[49] "CP1171" "CP1172" "CP1175" "CP1175" "CP1176" "CP1184" "CP1239" "CP1240" "CP1240" "CP1241" "CP1244" "CP1277"
[61] "CP1277" "CP1277" "CP1286" "CP1286" "CP1286" "CP1286" "CP1287" "CP1287" "CP1287" "CP1288" "CP1289" "CP1290"
[73] "CP1292" "CP1293" "CP1294" "CP1295" "CP1297" "CP1299" "CP1302" "CP1303" "CP1426" "CP1426" "CP1429" "CP1429"
[85] "CP1438" "CP1438" "CP1458" "CP1459" "CP1460" "CP1463" "CP1463" "CP1513" "CP1520" "CP1522" "CP1522" "CP1523"
[97] "CP1613" "CP1616" "CP1617" "CP1635" "CP1636" "CP1637" "CP1637" "CP1638" "CP1639" "CP1640" "CP1641" "CP1641"
[109] "CP1642" "CP1643" "CP1643" "CP1644" "CP1702" "CP1702" "CP1703" "CP1708" "CP1781" "CP1781" "CP1781" "CP1865"
[121] "CP1865" "CP1865" "CP1866" "CP1887" "CP1891" "CP1891" "CP1891" "CP1892" "CP2032" "CP2033" "CP2053" "CP2053"
[133] "CP2054" "CP2055" "CP2064" "CP2065" "CP2067" "CP2080" "CP2080" "CP2081" "CP2082" "CP2096" "CP2096" "CP2096"
[145] "CP2097" "CP2098" "CP2098" "CP2099" "CP2111" "CP2111" "CP2111" "CP2112" "CP2120" "CP2120" "CP2120" "CP2120"
[157] "CP2121" "CP2122" "CP2145" "CP2201" "CP2205" "CP2208" "CP2208" "CP2210" "CP2327" "CP2329" "CP2330" "CP2330"
[169] "CP2330" "CP2332" "CP2333" "CP2334" "CP2340" "CP2340" "CP2340" "CP2341" "CP2342" "CP2344" "CP2345" "CP2346"
[181] "CP2347" "CP2355" "CP2355" "CP2356" "CP2363" "CP2363" "CP2365" "CP2464" "CP2465" "CP2473" "CP2473" "CP2473"
[193] "CP2474" "CP2475" "CP2481" "CP2524" "CP2532" "CP2532" "CP2532" "CP2533" "CP2534" "CP2605" "CP2605" "CP2605"
[205] "CP2607" "CP2608" "CP2619" "CP2621" "CP2625" "CP2645" "CP2645" "CP2647" "CP2648" "CP2652" "CP2655" "CP2675"
[217] "CP2677" "CP2684" "CP2684" "CP2684" "CP2686" "CP2688" "CP2702" "CP2711" "CP2711" "CP2711" "CP2712" "CP2753"
[229] "CP2771" "CP2772" "CP2772" "CP2774" "CP2863" "CP2890" "CP2890" "CP2890" "CP2891" "CP2897" "CP2907" "CP2907"
[241] "CP2908" "CP2909" "CP2910" "CP2911" "CP2912" "CP2912" "CP2912" "CP2913" "CP2913" "CP2915" "CP2915" "CP2916"
[253] "CP2918" "CP2918" "CP2918" "CP2921" "CP2923" "CP2923" "CP2923" "CP2924" "CP2930" "CP2930" "CP2972" "CP2972"
[265] "CP2972" "CP2974" "CP2976" "CP2986" "CP2986" "CP2986" "CP2986" "CP2987" "CP2987" "CP2987" "CP2988" "CP2990"
[277] "CP2991" "CP2997" "CP2998" "CP2999" "CP3012" "CP3012" "CP3012" "CP3034" "CP3035" "CP3091" "CP3093" "CP3093"
[289] "CP3093" "CP3094" "CP3096" "CP3110" "CP3110" "CP3112" "CP3165" "CP3165" "CP3166" "CP3175" "CP3176" "CP3178"
[301] "CP3236" "CP3293" "CP3293" "CP3293" "CP3294" "CP3296" "CP3297" "CP3304" "CP3306" "CP3314" "CP3316" "CP3359"
[313] "CP3359" "CP3359" "CP3359" "CP3360" "CP3362" "CP3374" "CP3427" "CP3427" "CP3427" "CP3427" "CP3428" "CP3429"
[325] "CP3429" "CP3431" "CP3432" "CP3433" "CP3434" "CP3435" "CP3435" "CP3446" "CP3446" "CP3446" "CP3446" "CP3447"
[337] "CP3448" "CP3448" "CP3463" "CP3474" "CP3474" "CP3475" "CP3497" "CP3497" "CP3498" "CP3500" "CP3501" "CP3502"
[349] "CP3503" "CP3503" "CP3503" "CP3504" "CP3505" "CP3506" "CP3506" "CP3507" "CP3514" "CP3514" "CP3514" "CP3514"
[361] "CP3547" "CP3547" "CP3560" "CP3590" "CP3590" "CP3590" "CP3590" "CP3591" "CP3626" "CP3637" "CP3637" "CP3685"
[373] "CP3685" "CP3689" "CP3713" "CP3715" "CP3715" "CP3715" "CP3717" "CP3718" "CP3724" "CP3724" "CP3724" "CP3724"
[385] "CP3726" "CP3728" "CP3729" "CP3730" "CP3732" "CP3734" "CP3740" "CP3775" "CP3777" "CP3786" "CP3786" "CP3787"
[397] "CP4055" "CP4057" "CP4057" "CP4057" "CP4058" "CP4059" "CP4059" "CP4059" "CP4059" "CP4060" "CP4062" "CP4064"
[409] "CP4064" "CP4066" "CP4086" "CP4145" "CP4145" "CP4147" "CP4167" "CP4167" "CP4167" "CP4168" "CP4169" "CP4170"
[421] "CP4245" "CP4254" "CP4254" "CP4254" "CP4254" "CP4254" "CP4255" "CP4255" "CP4303" "CP4304" "CP4306" "CP4385"
[433] "CP4385" "CP4395" "CP4395" "CP4395" "CP4397" "CP4513" "CP4513" "CP4514" "CP4516" "CP4548" "CP4549" "CP4554"
[445] "CP4554" "CP4554" "CP4555" "CP4556" "CP4557" "CP4644" "CP4877" "CP4972" "CP4973" "CP4976" "CP5044" "CP5052"
[457] "CP5060" "CP5135" "CP5159" "CP5191" "CP5192" "CP5194" "CP5204" "CP5205" "CP5206" "CP5226" "CP5226" "CP5268"
[469] "CP5404" "CP5404" "CP5416" "CP5416" "CP5417" "CP5435" "CP5473" "CP5505" "CP5521" "CP5523" "CP5537" "CP5548"
[481] "CP5555" "CP5580" "CP5583" "CP5587" "CP5589" "CP5596" "CP5598" "CP5608" "CP5616" "CP5616" "CP5616" "CP5618"
[493] "CP5619" "CP5625" "CP5632" "CP5634" "CP5677" "CP5690" "CP5690" "CP5691" "CP5696" "CP5698" "CP5708" "CP5709"
[505] "CP5710" "CP5710" "CP5711" "CP5714" "CP5720" "CP5734" "CP5739" "CP5740" "CP5754" "CP5756" "CP5757" "CP5766"
[517] "CP5767" "CP5769" "CP5772" "CP5773" "CP5774" "CP5775" "CP5789" "CP5789" "CP5789" "CP5790" "CP5790" "CP5791"
[529] "CP5791" "CP5791" "CP5792" "CP5801" "CP5823" "CP5824" "CP5825" "CP5853" "CP5879" "CP5893" "CP5911" "CP5922"
[541] "CP5963" "CP5973" "CP5995" "CP6002" "CP6032" "CP6044" "CP6045" "CP6064" "CP6076" "CP6078" "CP6085" "CP6089"
[553] "CP6095" "CP6148" "CP6167" "CP6184" "CP6197" "CP6204" "CP6230" "CP6234" "CP6236" "CP6243" "CP6247" "CP6257"
[565] "CP6262" "CP6263" "CP6268" "CP6268" "CP6268" "CP6271" "CP6279" "CP6290" "CP6300" "CP6301" "CP6359" "CP6359"
[577] "CP6360" "CP6390" "CP6401" "CP6410" "CP6429" "CP6436" "CP6452" "CP6466" "CP6466" "CP6466" "CP6469" "CP6472"
[589] "CP6473" "CP6477" "CP6497" "CP6515" "CP6536" "CP6575" "CP6577" "CP6619" "CP6625" "CP6627" "CP6628" "CP6629"
[601] "CP6635" "CP6636" "CP6648" "CP6696" "CP6704" "CP6705" "CP6714" "CP6750" "CP6764" "CP6785" "CP6811" "CP6812"
[613] "CP6832" "CP6838" "CP6862" "CP6863" "CP6867" "CP6882" "CP6888" "CP6923" "CP6956" "CP6957" "CP6959" "CP7010"
[625] "CP7011" "CP7011" "CP7011" "CP7012" "CP7030" "CP7044" "CP7045" "CP7053" "CP7128" "CP7130" "CP7132" "CP7134"
[637] "CP7149" "CP7200" "CP7210" "CP7229" "CP7230" "CP7238" "CP7239" "CP7249" "CP7258" "CP7259" "CP7259" "CP7260"
[649] "CP7261" "CP7269" "CP7270" "CP7270" "CP7271" "CP7272" "CP7281" "CP7282" "CP7282" "CP7283" "CP7284" "CP7293"
[661] "CP7294" "CP7294" "CP7295" "CP7296" "CP7304" "CP7305" "CP7305" "CP7306" "CP7307" "CP7316" "CP7317" "CP7317"
[673] "CP7318" "CP7320" "CP7361" "CP7362" "CP7363" "CP7363" "CP7364" "CP7375" "CP7413" "CP7414" "CP7415" "CP7440"
[685] "CP7441" "CP7442" "CP7442" "CP7443" "CP7474" "CP7598" "CP7600" "CP7601" "CP7608" "CP7609" "CP7610" "CP7610"
[697] "CP7611" "CP7613" "CP7615" "CP7616" "CP7617" "CP7619" "CP7620" "CP7621" "CP7621" "CP7622" "CP7625" "CP7628"
[709] "CP7629" "CP7630" "CP7632" "CP7633" "CP7635" "CP7663" "CP7664" "CP7664" "CP7665" "CP7667" "CP7678" "CP7678"
[721] "CP7678" "CP7678" "CP7679" "CP7679" "CP7679" "CP7679" "CP7679" "CP7680" "CP7682" "CP7682" "CP7744" "CP7745"
[733] "CP7784" "CP7864" "CP7864" "CP7864" "CP7864" "CP7864" "CP7865" "CP7865" "CP7866" "CP7866" "CP7868" "CP7869"
[745] "CP7870" "CP7871" "CP7872" "CP7873" "CP7873" "CP7874" "CP7877" "CP7884" "CP7889" "CP7893" "CP7908" "CP7962"
[757] "CP7963" "CP7963" "CP7964" "CP7964" "CP7964" "CP7964" "CP7964" "CP7964" "CP7965" "CP7967" "CP7967" "CP7970"
[769] "CP7972" "CP7973" "CP7974" "CP7991" "CP7991" "CP7991" "CP7995" "CP7997" "CP7997" "CP7997" "CP8005" "CP8007"
[781] "CP8010" "CP8012" "CP8012" "CP8015" "CP8015" "CP8016" "CP8017" "CP8019" "CP8024" "CP8047" "CP8132" "CP8132"
[793] "CP8133" "CP8136" "CP8138" "CP8141" "CP8142" "CP8142" "CP8142" "CP8574" "CP8579" "CP8591" "CP8595" "CP8598"
[805] "CP8598" "CP8599" "CP8599" "CP8599" "CP8599" "CP8599" "CP8599" "CP8599" "CP8600" "CP8600" "CP8600" "CP8603"
[817] "CP8604" "CP8606" "CP8614" "CP8615" "CP8617" "CP8617" "CP8618" "CP8619" "CP8620" "CP8620" "CP8621" "CP8622"
[829] "CP8622" "CP8623" "CP8623" "CP8623" "CP8624" "CP8625" "CP8629" "CP8631" "CP8633" "CP8634" "CP8634" "CP8636"
[841] "CP8638" "CP8639" "CP8640" "CP8641" "CP8644" "CP8644" "CP8645" "CP8646" "CP8646" "CP8648" "CP8651" "CP8652"
[853] "CP8653" "CP8654" "CP8656" "CP8657" "CP8658" "CP8659" "CP8659" "CP8660" "CP8662" "CP8664" "CP8668" "CP8670"
[865] "CP8671" "CP8672" "CP8674" "CP8675" "CP8675" "CP8677" "CP8679" "CP8681" "CP8681" "CP8688" "CP8689" "CP8690"
[877] "CP8691" "CP8693" "CP8695" "CP8697" "CP8699" "CP8700" "CP8705" "CP8707" "CP8708" "CP8711" "CP8713" "CP8714"
[889] "CP8715" "CP8716" "CP8719" "CP8720" "CP8721" "CP8722" "CP8723" "CP8724" "CP8725" "CP8727" "CP8728" "CP8747"
[901] "CP8747" "CP8747" "CP8747" "CP8747" "CP8747" "CP8748" "CP8748" "CP8748" "CP8748" "CP8748" "CP8749" "CP8749"
[913] "CP8749" "CP8749" "CP8749" "CP8749" "CP8750" "CP8750" "CP8750" "CP8752" "CP8753" "CP8754" "CP8754" "CP8755"
[925] "CP8756" "CP8758" "CP8759" "CP8759" "CP8760" "CP8760" "CP8761" "CP8761" "CP8761" "CP8763" "CP8764" "CP8765"
[937] "CP8767" "CP8768" "CP8769" "CP8774" "CP8774" "CP8776" "CP8777" "CP8779" "CP8780" "CP8780" "CP8781" "CP8781"
[949] "CP8782" "CP8785" "CP8786" "CP8787" "CP8788" "CP8789" "CP8790" "CP8791" "CP8792" "CP8793" "CP8796" "CP8798"
[961] "CP8802" "CP8803" "CP8805" "CP8807" "CP8808" "CP8812" "CP8814" "CP8816" "CP8819" "CP8821" "CP8822" "CP8824"
[973] "CP8825" "CP8826" "CP8828" "CP8830" "CP8832" "CP8833" "CP8836" "CP8874" "CP8876" "CP8877" "CP8878" "CP8878"
[985] "CP8878" "CP8878" "CP8878" "CP8878" "CP8879" "CP8879" "CP8879" "CP8879" "CP8881" "CP8881" "CP8882" "CP8883"
[997] "CP8883" "CP8883" "CP8883" "CP8884"
[ reached getOption("max.print") -- omitted 76 entries ]
```
## Using the MS2 information to aid in the annotation of a chromatographic peak (pick 96.96004 for example):
```R
$ ex_mz <- 96.96004
$ chromPeaks(dda_data, mz = ex_mz, ppm = 20)

             mz    mzmin    mzmax      rt    rtmin   rtmax       into       intb      maxo  sn sample
CP4972 96.96003 96.96001 96.96006 55.6840 49.69270 62.8274 25804864.0 25629857.3 3063722.5 267      1
CP4973 96.96006 96.96003 96.96008 12.1853  7.23388 18.3794  1749971.9  1600971.8  423904.2  36      1
CP4974 96.96006 96.96004 96.96008 22.7676 18.74580 25.9691   820956.5   722541.1  272789.7  23      1
CP4975 96.96002 96.96001 96.96006 65.5072 61.93030 68.7593  5307858.2  5213835.6 1094873.2  82      1
CP4976 96.96003 96.96001 96.96006 57.6777 57.67770 61.9303  8956539.4  8895549.4 2727754.0 267      1
```
## Extract MS2 spectra that were associated with the candidate chromatographic peak:
```R
$ ex_spectra <- dda_spectra[dda_spectra$peak_id == "CP4972"]
$ ex_spectra

MSn data (Spectra) with 1 spectra in a MsBackendMzR backend:
            msLevel     rtime scanIndex
          <integer> <numeric> <integer>
F1.S00188         2   61.2675       188
 ... 38 more variables/columns.

file(s):
new_cali_std_150.mzXML
```
## MS2 spectrum created from all measured MS2 spectra for ions of chromatographic peak CP4972: 
```R
$ plotSpectra(ex_spectra)
```
![](https://hackmd.io/_uploads/B1k_kNzq3.png)

```R
for (i in 1:5440) {
    file_name <- paste0(output_folder, "/plot_", dda_spectra$peak_id[i], ".png")
    png(file_name, width = 864, height = 655)
    plotSpectra(dda_spectra[i])
    dev.off()
    print(i)
}
```