# LC-MS/MS data analysis with xcms

#### 18 September 2020

#### Package

xcms 3.10.2

Package: xcms
Authors: Johannes Rainer, Michael Witting
Modified: 2020-04-27 16:18:57
Compiled: Fri Sep 18 20:03:36 2020

# 1 Introduction

Metabolite identification is an important step in non-targeted metabolomics and requires different steps. One involves the use of tandem mass spectrometry to generate fragmentation spectra of detected metabolites (LC-MS/MS), which are then compared to fragmentation spectra of known metabolites. Different approaches exist for the generation of these fragmentation spectra, whereas the most used is data dependent acquisition (DDA) also known as the top-n method. In this method the top N most intense m/z values from a MS1 scan are selected for fragmentation in the next N scans before the cycle starts again. This method allows to generate clean MS2 fragmentation spectra on the fly during acquisition without the need for further experiments, but suffers from poor coverage of the detected metabolites (since only a limited number of ions are fragmented).

Data independent approaches (DIA) like Bruker bbCID, Agilent AllIons or Waters MSe don’t use such a preselection, but rather fragment all detected molecules at once. They are using alternating schemes with scan of low and high collision energy to collect MS1 and MS2 data. Using this approach, there is no problem in coverage, but the relation between the precursor and fragment masses is lost leading to chimeric spectra. Sequential Window Acquisition of all Theoretical Mass Spectra (or SWATH [1]) combines both approaches through a middle-way approach. There is no precursor selection and acquisition is independent of acquired data, but rather than isolating all precusors at once, defined windows (i.e. ranges of m/z values) are used and scanned. This reduces the overlap of fragment spectra while still keeping a high coverage.

This document showcases the analysis of two small LC-MS/MS data sets using `r Biocpkg("xcms")`. The data files used are reversed-phase LC-MS/MS runs from the Agilent Pesticide mix obtained from a Sciex 6600 Triple ToF operated in SWATH acquisition mode. For comparison a DDA file from the same sample is included.

# 2 Analysis of DDA data

Below we load the example DDA data set using the `readMSData` function from the MSnbase package.

    library(xcms)

    dda_file <- system.file("TripleTOF-SWATH/PestMix1_DDA.mzML",
                            package = "msdata")
    dda_data <- readMSData(dda_file, mode = "onDisk")

The variable `dda_data` contains now all MS1 and MS2 spectra from the specified mzML file. The number of spectra for each MS level is listed below.

    table(msLevel(dda_data))

    ##
    ##    1    2
    ## 1504 2238

Next we perform the chromatographic peak detection on the MS level 1 data with the `findChromPeaks` method. Below we define the settings for a centWave-based peak detection and perform the analysis.

    cwp <- CentWaveParam(snthresh = 5, noise = 100, ppm = 10,
                         peakwidth = c(3, 30))
    dda_data <- findChromPeaks(dda_data, param = cwp)

In total 112 peaks were identified in the present data set.

The advantage of LC-MS/MS data is that (MS1) ions are fragmented and the corresponding MS2 spectra measured. Thus, for some of the ions (identified as MS1 chromatographic peaks) MS2 spectra are available. These can facilitate the annotation of the respective MS1 chromatographic peaks (or MS1 features after a correspondence analysis). MS2 spectra for identified chromatographic peaks can be extracted with the `chromPeakSpectra` method which returns a `Spectra` object of MS2 spectra associated with a chromatographic peak (i.e. with their retention time within the retention time window of a chromatographic peak and their precursor m/z within the m/z range of the same chromatographic peak.

    dda_spectra <- chromPeakSpectra(dda_data)
    dda_spectra

    ## Spectra with 150 spectra and 1 metadata column(s):
    ##                    msLevel     rtime peaksCount |     peak_id
    ##                  <integer> <numeric>  <integer> | <character>
    ##   CP004.F1.S1812         2   237.869       1489 |       CP004
    ##   CP004.F1.S1846         2   241.299       1807 |       CP004
    ##   CP005.F1.S2446         2   326.583         21 |       CP005
    ##   CP006.F1.S2450         2   327.113         85 |       CP006
    ##   CP006.F1.S2502         2   330.273        168 |       CP006
    ##              ...       ...       ...        ... .         ...
    ##   CP100.F1.S5110         2   574.725        890 |       CP100
    ##   CP100.F1.S5115         2   575.255       1042 |       CP100
    ##   CP101.F1.S5272         2   596.584        196 |       CP101
    ##   CP102.F1.S5236         2   592.424         12 |       CP102
    ##   CP102.F1.S5266         2   596.054         88 |       CP102

The metadata column `"peak_id"` contains the identifiers of the chromatographic peaks the MS2 spectrum is associated with.

We next use the MS2 information to aid in the annotation of a chromatographic peak. As an example we use a chromatographic peak of an ion with an m/z of 304.1131 which we extract in the code block below.

    ex_mz <- 304.1131
    chromPeaks(dda_data, mz = ex_mz, ppm = 20)

    ##             mz    mzmin    mzmax      rt   rtmin   rtmax    into     intb
    ## CP057 304.1133 304.1126 304.1143 425.024 417.985 441.773 13040.8 12884.14
    ##           maxo sn sample
    ## CP057 3978.987 79      1

A search of potential ions with a similar m/z in a reference database (e.g. Metlin) returned a large list of potential hits, most with a very small ppm. For two of the hits, Flumazenil (Metlin ID 2724) and Fenamiphos (Metlin ID 72445) experimental MS2 spectra are available. Thus, we could match the MS2 spectrum for the identified chromatographic peak against these to annotate our ion. Below we extract all MS2 spectra that were associated with the candidate chromatographic peak using the ID of the peak in the present data set.

    ex_id <- rownames(chromPeaks(dda_data, mz = ex_mz, ppm = 20))
    ex_spectra <- dda_spectra[mcols(dda_spectra)$peak_id == ex_id]
    ex_spectra

    ## Spectra with 5 spectra and 1 metadata column(s):
    ##                    msLevel     rtime peaksCount |     peak_id
    ##                  <integer> <numeric>  <integer> | <character>
    ##   CP057.F1.S3505         2   418.926         10 |       CP057
    ##   CP057.F1.S3510         2   419.306         30 |       CP057
    ##   CP057.F1.S3582         2   423.036        694 |       CP057
    ##   CP057.F1.S3603         2   423.966        783 |       CP057
    ##   CP057.F1.S3609         2   424.296        753 |       CP057

There are 5 MS2 spectra representing fragmentation of the ion(s) measured in our candidate chromatographic peak. We next reduce this to a single MS2 spectrum using the `combineSpectra` method employing the `consensusSpectrum` function to determine which peaks to keep in the resulting spectrum.

    ex_spectrum <- combineSpectra(ex_spectra, method = consensusSpectrum, mzd = 0,
                                  ppm = 20, minProp = 0.8, weighted = FALSE,
                                  intensityFun = median, mzFun = median)
    ex_spectrum

    ## Spectra with 1 spectra and 1 metadata column(s):
    ##                    msLevel     rtime peaksCount |     peak_id
    ##                  <integer> <numeric>  <integer> | <character>
    ##   CP057.F1.S3505         2   418.926         24 |       CP057

Mass peaks from all input spectra with a difference in m/z smaller 20 ppm (parameter `ppm`) were combined into one peak and the median m/z and intensity is reported for these. Due to parameter `minProp = 0.8`, the resulting MS2 spectrum contains only peaks that were present in 80% of the input spectra.

A plot of this consensus spectrum is shown below.

    plot(ex_spectrum[[1]])

Figure 1: Consensus MS2 spectrum created from all measured MS2 spectra for ions of chromatographic peak CP53

We could now match the consensus spectrum against a database of MS2 spectra. In our example we simply load MS2 spectra for the two compounds with matching m/z exported from Metlin. For each of the compounds MS2 spectra created with collision energies of 0V, 10V, 20V and 40V are available. Below we import the respective data and plot our candidate spectrum against the MS2 spectra of Flumanezil and Fenamiphos (from a collision energy of 20V).

    flumanezil <- spectra(readMgfData(
        system.file("mgf/metlin-2724.mgf", package = "xcms")))
    fenamiphos <- spectra(readMgfData(
        system.file("mgf/metlin-72445.mgf", package = "xcms")))

    par(mfrow = c(1, 2))
    plot(ex_spectrum[[1]], flumanezil[[3]], main = "against Flumanezil",
         tolerance = 40e-6)
    plot(ex_spectrum[[1]], fenamiphos[[3]], main = "against Fenamiphos",
         tolerance = 40e-6)

Figure 2: Mirror plots for the candidate MS2 spectrum against Flumanezil (left) and Fenamiphos (right)
The upper panel represents the candidate MS2 spectrum, the lower the target MS2 spectrum. Matching peaks are indicated with a dot.

Our candidate spectrum matches Fenamiphos, thus, our example chromatographic peak represents signal measured for this compound. In addition to plotting the spectra, we can also calculate similarities between them with the `compareSpectra` method (e.g. using the dotproduct method as shown below).

    compareSpectra(ex_spectrum[[1]], flumanezil[[3]], binSize = 0.02,
                   fun = "dotproduct")

    ## [1] 0.005350294

    compareSpectra(ex_spectrum[[1]], fenamiphos[[3]], binSize = 0.02,
                   fun = "dotproduct")

    ## [1] 0.9092189

Clearly, the candidate spectrum does not match Flumanezil, while it has a high similarity to Fenamiphos. While we performed here the MS2-based annotation on a single chromatographic peak, this could be easily extended to the full list of MS2 spectra (returned by `chromPeakSpectra`) for all chromatographic peaks in an experiment. Respective code facilitating this will be implemented in future.

In the present example we used only a single data file and we did thus not need to perform a sample alignment and correspondence analysis. These tasks could however be performed similarly to plain LC-MS data, retention times of recorded MS2 spectra would however also be adjusted during alignment based on the MS1 data. After correspondence analysis (peak grouping) MS2 spectra for features can be extracted with the `featureSpectra` function which returns all MS2 spectra associated with any chromatographic peak of a feature.

Note also that this workflow can be included into the Feature-Based Molecular Networking FBMN to match MS2 spectra against GNPS. See here for more details and examples.

# 3 SWATH data analysis

In this section we analyze a small SWATH data set consisting of a single mzML file with data from the same sample analyzed in the previous section but recorded in SWATH mode. We again read the data with the `readMSData` function. The resulting object will contain all recorded MS1 and MS2 spectra in the specified file.

    swath_file <- system.file("TripleTOF-SWATH",
                              "PestMix1_SWATH.mzML",
                              package = "msdata")

    swath_data <- readMSData(swath_file, mode = "onDisk")

Below we determine the number of MS level 1 and 2 spectra in the present data set.

    table(msLevel(swath_data))

    ##
    ##    1    2
    ##  444 3556

As described in the introduction, in SWATH mode all ions within pre-defined isolation windows are fragmented and MS2 spectra measured. The definition of these isolation windows (SWATH pockets) is imported from the mzML files and stored in the object’s `fData` (which provides additional annotations for each individual spectrum). Below we inspect the respective information for the first few spectra. The upper and lower isolation window m/z can be extracted with the `isolationWindowLowerMz` and `isolationWindowUpperMz`.

    head(fData(swath_data)[, c("isolationWindowTargetMZ",
                               "isolationWindowLowerOffset",
                               "isolationWindowUpperOffset",
                               "msLevel", "retentionTime")])

    ##          isolationWindowTargetMZ isolationWindowLowerOffset
    ## F1.S2000                  208.95                      21.95
    ## F1.S2001                  244.05                      14.15
    ## F1.S2002                  270.85                      13.65
    ## F1.S2003                  299.10                      15.60
    ## F1.S2004                  329.80                      16.10
    ## F1.S2005                  367.35                      22.45
    ##          isolationWindowUpperOffset msLevel retentionTime
    ## F1.S2000                      21.95       2       200.084
    ## F1.S2001                      14.15       2       200.181
    ## F1.S2002                      13.65       2       200.278
    ## F1.S2003                      15.60       2       200.375
    ## F1.S2004                      16.10       2       200.472
    ## F1.S2005                      22.45       2       200.569

    head(isolationWindowLowerMz(swath_data))

    ## [1] 187.0 229.9 257.2 283.5 313.7 344.9

    head(isolationWindowUpperMz(swath_data))

    ## [1] 230.9 258.2 284.5 314.7 345.9 389.8

In the present data set we use the value of the isolation window target m/z to define the individual SWATH pockets. Below we list the number of spectra that are recorded in each pocket/isolation window.

    table(isolationWindowTargetMz(swath_data))

    ##
    ## 163.75 208.95 244.05 270.85  299.1  329.8 367.35 601.85
    ##    444    445    445    445    445    444    444    444

We have thus 1,000 MS2 spectra measured in each isolation window.

## 3.1 Chromatographic peak detection in MS1 and MS2 data

Similar to a conventional LC-MS analysis, we perform first a chromatographic peak detection (on the MS level 1 data) with the `findChromPeaks` method. Below we define the settings for a centWave-based peak detection and perform the analysis.

    cwp <- CentWaveParam(snthresh = 5, noise = 100, ppm = 10,
                         peakwidth = c(3, 30))
    swath_data <- findChromPeaks(swath_data, param = cwp)

Next we perform a chromatographic peak detection in the MS level 2 data of each isolation window. We use the `findChromPeaksIsolationWindow` function employing the same peak detection algorithm reducing however the required signal-to-noise ratio. The `isolationWindow` parameter allows to specify which MS2 spectra belong to which isolation window and hence defines in which set of MS2 spectra chromatographic peak detection should be performed. While the default value for this parameter uses isolation windows provided by calling `isolationWindowTargetMz` on the object, it would also be possible to manually define the isolation windows, e.g. if the corresponding information is not available in the input mzML files.

    cwp <- CentWaveParam(snthresh = 3, noise = 10, ppm = 10,
                         peakwidth = c(3, 30))
    swath_data <- findChromPeaksIsolationWindow(swath_data, param = cwp)

The `findChromPeaksIsolationWindow` function added all peaks identified in the individual isolation windows to the `chromPeaks` matrix containing already the MS1 chromatographic peaks. These newly added peaks can be identified by the value of the `"isolationWindow"` column in the corresponding row in `chromPeakData`, which lists also the MS level in which the peak was identified.

    chromPeakData(swath_data)

    ## DataFrame with 368 rows and 6 columns
    ##        ms_level is_filled isolationWindow isolationWindowTargetMZ
    ##       <integer> <logical>        <factor>               <numeric>
    ## CP01          1     FALSE              NA                      NA
    ## CP02          1     FALSE              NA                      NA
    ## CP03          1     FALSE              NA                      NA
    ## CP04          1     FALSE              NA                      NA
    ## CP05          1     FALSE              NA                      NA
    ## ...         ...       ...             ...                     ...
    ## CP364         2     FALSE          601.85                  601.85
    ## CP365         2     FALSE          601.85                  601.85
    ## CP366         2     FALSE          601.85                  601.85
    ## CP367         2     FALSE          601.85                  601.85
    ## CP368         2     FALSE          601.85                  601.85
    ##       isolationWindowLowerMz isolationWindowUpperMz
    ##                    <numeric>              <numeric>
    ## CP01                      NA                     NA
    ## CP02                      NA                     NA
    ## CP03                      NA                     NA
    ## CP04                      NA                     NA
    ## CP05                      NA                     NA
    ## ...                      ...                    ...
    ## CP364                  388.8                  814.9
    ## CP365                  388.8                  814.9
    ## CP366                  388.8                  814.9
    ## CP367                  388.8                  814.9
    ## CP368                  388.8                  814.9

Below we count the number of chromatographic peaks identified within each isolation window (the number of chromatographic peaks identified in MS1 is 62).

    table(chromPeakData(swath_data)$isolationWindow)

    ##
    ## 163.75 208.95 244.05 270.85  299.1  329.8 367.35 601.85
    ##      2     38     32     14    105     23     61     31

We thus successfully identified chromatographic peaks in the different MS levels and isolation windows, but don’t have any actual MS2 spectra yet. These have to be reconstructed from the available chromatographic peak data which we will done in the next section.

## 3.2 Reconstruction of MS2 spectra

Identifying the signal of the fragment ions for the precursor measured by each MS1 chromatographic peak is a non-trivial task. The MS2 spectrum of the fragment ion for each MS1 chromatographic peak has to be reconstructed from the available MS2 signal (i.e. the chromatographic peaks identified in MS level 2). For SWATH data, fragment ion signal should be present in the isolation window that contains the m/z of the precursor ion and the chromatographic peak shape of the MS2 chromatographic peaks of fragment ions of a specific precursor should have a similar retention time and peak shape than the precursor’s MS1 chromatographic peak.

After detection of MS1 and MS2 chromatographic peaks has been performed, we can reconstruct the MS2 spectra using the `reconstructChromPeakSpectra` function. This function defines an MS2 spectrum for each MS1 chromatographic peak based on the following approach:

- Identify MS2 chromatographic peaks in the isolation window containing the m/z of the ion (the MS1 chromatographic peak) that have approximately the same retention time than the MS1 chromatographic peak (the accepted difference in retention time can be defined with the `diffRt` parameter).
- Extract the MS1 chromatographic peak and all MS2 chromatographic peaks identified by the previous step and correlate the peak shapes of the candidate MS2 chromatographic peaks with the shape of the MS1 peak. MS2 chromatographic peaks with a correlation coefficient larger than `minCor` are retained.
- Reconstruct the MS2 spectrum using the m/z of all above selected MS2 chromatographic peaks and their intensity; each MS2 chromatographic peak selected for an MS1 peak will thus represent one mass peak in the reconstructed spectrum.

To illustrate this process we perform the individual steps on the example of Fenamiphos (exact mass 303.105800777 and m/z of [M+H]+ adduct 304.113077). As a first step we extract the chromatographic peak for this ion.

    fenamiphos_mz <- 304.113077
    fenamiphos_ms1_peak <- chromPeaks(swath_data, mz = fenamiphos_mz, ppm = 2)
    fenamiphos_ms1_peak

    ##            mz    mzmin    mzmax      rt   rtmin   rtmax     into     intb
    ## CP34 304.1124 304.1121 304.1126 423.945 419.445 428.444 10697.34 10688.34
    ##          maxo  sn sample
    ## CP34 2401.849 618      1

Next we identify all MS2 chromatographic peaks that were identified in the isolation window containing the m/z of Fenamiphos. The information on the isolation window in which a chromatographic peak was identified is available in the `chromPeakData` (which contains arbitrary additional annotations to each individual chromatographic peak).

    keep <- chromPeakData(swath_data)$isolationWindowLowerMz < fenamiphos_mz &
            chromPeakData(swath_data)$isolationWindowUpperMz > fenamiphos_mz

We also require the retention time of the MS2 chromatographic peaks to be similar to the retention time of the MS1 peak and extract the corresponding peak information.

    keep <- keep &
        chromPeaks(swath_data)[, "rtmin"] < fenamiphos_ms1_peak[, "rt"] &
        chromPeaks(swath_data)[, "rtmax"] > fenamiphos_ms1_peak[, "rt"]

    fenamiphos_ms2_peak <- chromPeaks(swath_data)[which(keep), ]

In total 24 MS2 chromatographic peaks match all the above condition. Next we extract their corresponding ion chromatograms, as well as the ion chromatogram of the MS1 peak. Note that we extend the retention time range by 1 seconds on both sides. In addition we have to filter the object first by isolation window, keeping only spectra that were measured in that specific window and to specify to extract the chromatographic data from MS2 spectra (with `msLevel = 2L`).

    rtr <- fenamiphos_ms1_peak[, c("rtmin", "rtmax")]
    rtr[1] <- rtr[1] - 1
    rtr[2] <- rtr[2] + 1
    mzr <- fenamiphos_ms1_peak[, c("mzmin", "mzmax")]
    fenamiphos_ms1_chr <- chromatogram(swath_data, rt = rtr, mz = mzr)

    rtr <- fenamiphos_ms2_peak[, c("rtmin", "rtmax")]
    rtr[, 1] <- rtr[, 1] - 1
    rtr[, 2] <- rtr[, 2] + 1
    mzr <- fenamiphos_ms2_peak[, c("mzmin", "mzmax")]
    fenamiphos_ms2_chr <- chromatogram(
        filterIsolationWindow(swath_data, mz = fenamiphos_mz),
        rt = rtr, mz = mzr, msLevel = 2L)

We can now plot the extracted ion chromatogram of the MS1 and the extracted MS2 data.

    plot(rtime(fenamiphos_ms1_chr[1, 1]),
         intensity(fenamiphos_ms1_chr[1, 1]),
         xlab = "retention time [s]", ylab = "intensity", pch = 16,
         ylim = c(0, 5000), col = "blue", type = "b", lwd = 2)
    #' Add data from all MS2 peaks
    tmp <- lapply(fenamiphos_ms2_chr@.Data,
                  function(z) points(rtime(z), intensity(z),
                                     col = "#00000080",
                                     type = "b", pch = 16))

Figure 3: Extracted ion chromatograms for Fenamiphos from MS1 (blue) and potentially related signal in MS2 (grey)

Next we can calculate correlations between the peak shapes of each MS2 chromatogram with the MS1 peak. We perform the correlation below for one of the MS2 chromatographic peaks. Note that, because spectra are recorded consecutively, the retention times of the individual data points will differ for the MS2 and MS1 chromatographic data and data points have thus to be matched (aligned) before performing the correlation analysis. This is done automatically by the `correlate` function. See the help for the `align` method for more information on alignment options.

    correlate(fenamiphos_ms2_chr[1, 1],
              fenamiphos_ms1_chr[1, 1], align = "approx")

    ## [1] 0.9997871

After identifying the MS2 chromatographic peaks with shapes of enough high similarity to the MS1 chromatographic peaks, an MS2 spectrum could be reconstructed based on the m/z and intensities of the MS2 chromatographic peaks.

The `reconstructChromPeakSpectra` function performs the above analysis for each individual MS1 chromatographic peak in a SWATH data set. Below we reconstruct MS2 spectra for our example data requiring a peak shape correlation higher than `0.9` between the candidate MS2 chromatographic peak and the target MS1 chromatographic peak.

    swath_spectra <- reconstructChromPeakSpectra(swath_data, minCor = 0.9)
    swath_spectra

    ## Spectra with 62 spectra and 3 metadata column(s):
    ##       msLevel     rtime peaksCount |     ms2_peak_id      ms2_peak_cor
    ##     <integer> <numeric>  <integer> | <CharacterList>     <NumericList>
    ##   1         2        NA          0 |
    ##   1         2        NA          0 |
    ##   1         2        NA          1 |           CP063          0.950582
    ##   1         2        NA          1 |           CP105          0.966901
    ##   1         2        NA          1 |           CP153          0.924545
    ##   .       ...       ...        ... .             ...               ...
    ##   1         2        NA          0 |
    ##   1         2        NA          0 |
    ##   1         2        NA          0 |
    ##   1         2        NA          0 |
    ##   1         2        NA          2 |     CP334,CP329 0.918915,0.911944
    ##         peak_id
    ##     <character>
    ##   1        CP01
    ##   1        CP02
    ##   1        CP03
    ##   1        CP04
    ##   1        CP05
    ##   .         ...
    ##   1        CP58
    ##   1        CP59
    ##   1        CP60
    ##   1        CP61
    ##   1        CP62

As a result we got a `Spectra` object of length equal to the number of MS1 peaks in our data. A `peaksCount` of `0` indicates that no MS2 spectrum could be defined based on the used settings. For reconstructed spectra additional annotations are available such as the IDs of the MS2 chromatographic peaks from which the spectrum was reconstructed (`"ms2_peak_id"`) as well as the correlation coefficient of their chromatographic peak shape with the precursor’s shape (`"ms2_peak_cor"`). Metadata column `"peak_id"` contains the ID of the MS1 chromatographic peak.

We next extract the MS2 spectrum for our example peak most likely representing [M+H]+ ions of Fenamiphos using its chromatographic peak ID:

    fenamiphos_swath_spectrum <- swath_spectra[
        mcols(swath_spectra)$peak_id == rownames(fenamiphos_ms1_peak)]

We can now compare the reconstructed spectrum to the example consensus spectrum from the DDA experiment in the previous section (variable `ex_spectrum`) as well as to the MS2 spectrum for Fenamiphos from Metlin (with a collision energy of 10V).

    par(mfrow = c(1, 2))
    plot(fenamiphos_swath_spectrum[[1]], ex_spectrum[[1]],
         tolerance = 50e-6, main = "against DDA")
    plot(fenamiphos_swath_spectrum[[1]], fenamiphos[[2]],
         tolerance = 50e-6, main = "against Metlin")

Figure 4: Mirror plot comparing the reconstructed MS2 spectrum for Fenamiphos (upper panel) against the measured spectrum from the DDA data and the Fenamiphhos spectrum from Metlin

If we wanted to get the EICs for the MS2 chromatographic peaks used to generate this MS2 spectrum we can use the IDs of these peaks which are provided with `$ms2_peak_id` of the result spectrum.

    pk_ids <- mcols(fenamiphos_swath_spectrum)$ms2_peak_id[[1]]
    pk_ids

    ##  [1] "CP199" "CP201" "CP211" "CP208" "CP200" "CP202" "CP217" "CP215" "CP205"
    ## [10] "CP212" "CP221" "CP223" "CP213" "CP207" "CP220"

With these peak IDs available we can extract their retention time window and m/z ranges from the `chromPeaks` matrix and use the `chromatogram` function to extract their EIC. Note however that for SWATH data we have MS2 signal from different isolation windows. Thus we have to first filter the `swath_data` object by the isolation window containing the precursor m/z with the `filterIsolationWindow` to subset the data to MS2 spectra related to the ion of interest. In addition, we have to use `msLevel = 2L` in the `chromatogram` call because `chromatogram` extracts by default only data from MS1 spectra.

    rt_range <- chromPeaks(swath_data)[pk_ids, c("rtmin", "rtmax")]
    rt_range[, 1] <- rt_range[, 1] - 1
    rt_range[, 2] <- rt_range[, 2] + 1
    mz_range <- chromPeaks(swath_data)[pk_ids, c("mzmin", "mzmax")]

    pmz <- precursorMz(fenamiphos_swath_spectrum)[1]
    swath_data_iwindow <- filterIsolationWindow(swath_data, mz = pmz)

    ## Warning in object[keep]: Removed preprocessing results

    ms2_eics <- chromatogram(swath_data_iwindow, rt = rt_range,
                             mz = mz_range, msLevel = 2L)

Each row of this `ms2_eics` contains now the EIC of one of the MS2 chromatographic peaks.

As a second example we analyze the signal from an [M+H]+ ion with an m/z of 376.0381 (which would match Prochloraz). We first identify the MS1 chromatographic peak for that m/z and retrieve the reconstructed MS2 spectrum for that peak.

    prochloraz_mz <- 376.0381

    prochloraz_ms1_peak <- chromPeaks(swath_data, msLevel = 1L,
                                      mz = prochloraz_mz, ppm = 5)
    prochloraz_ms1_peak

    ##            mz   mzmin    mzmax      rt   rtmin   rtmax     into     intb
    ## CP22 376.0373 376.037 376.0374 405.046 401.446 409.546 3664.051 3655.951
    ##          maxo  sn sample
    ## CP22 897.3923 278      1

    prochloraz_swath_spectrum <- swath_spectra[
        mcols(swath_spectra)$peak_id == rownames(prochloraz_ms1_peak)]

In addition we identify the corresponding MS1 peak in the DDA data set, extract all measured MS2 chromatographic peaks and build the consensus spectrum from these.

    prochloraz_dda_peak <- chromPeaks(dda_data, msLevel = 1L,
                                      mz = prochloraz_mz, ppm = 5)
    prochloraz_dda_peak

    ##             mz    mzmin    mzmax      rt   rtmin   rtmax     into     intb
    ## CP034 376.0385 376.0378 376.0391 405.715 400.346 410.555 5088.528 5078.727
    ##           maxo  sn sample
    ## CP034 1350.633 436      1

The retention times for the chromatographic peaks from the DDA and SWATH data match almost perfectly. Next we get the MS2 spectra for this peak.

    prochloraz_dda_spectra <- dda_spectra[
        mcols(dda_spectra)$peak_id == rownames(prochloraz_dda_peak)]
    prochloraz_dda_spectra

    ## Spectra with 5 spectra and 1 metadata column(s):
    ##                    msLevel     rtime peaksCount |     peak_id
    ##                  <integer> <numeric>  <integer> | <character>
    ##   CP034.F1.S3253         2   401.438         84 |       CP034
    ##   CP034.F1.S3259         2   402.198         67 |       CP034
    ##   CP034.F1.S3306         2   404.677        277 |       CP034
    ##   CP034.F1.S3316         2   405.127        311 |       CP034
    ##   CP034.F1.S3325         2   405.877        383 |       CP034

In total 5 spectra were measured, some with a relatively high number of peaks. Next we combine them into a consensus spectrum.

    prochloraz_dda_spectrum <- combineSpectra(
        prochloraz_dda_spectra, method = consensusSpectrum, msd = 0, ppm = 20,
        minProp = 0.8, weighted = FALSE, intensityFun = median, mzFun = median)

At last we load also the Prochloraz MS2 spectra (for different collision energies) from Metlin.

    prochloraz <- spectra(readMgfData(
        system.file("mgf/metlin-68898.mgf", package = "xcms")))

To validate the reconstructed spectrum we plot it against the corresponding DDA spectrum and the MS2 spectrum for Prochloraz (for a collision energy of 10V) from Metlin.

    par(mfrow = c(1, 2))
    plot(prochloraz_swath_spectrum[[1]], prochloraz_dda_spectrum[[1]],
         tolerance = 50e-6, main = "against DDA")
    plot(prochloraz_swath_spectrum[[1]], prochloraz[[2]],
         tolerance = 50e-6, main = "against Metlin")

Figure 5: Mirror plot comparing the reconstructed MS2 spectrum for Prochloraz (upper panel) against the measured spectrum from the DDA data and the Prochloraz spectrum from Metlin

The spectra fit relatively well. Interestingly, the peak representing the precursor (the right-most peak) seems to have a slightly shifted m/z value in the reconstructed spectrum.

Similar to the DDA data, the reconstructed MS2 spectra from SWATH data could be used in the annotation of the MS1 chromatographic peaks.

# 4 Outlook

Currently, spectra data representation, handling and processing is being re-implemented as part of the RforMassSpectrometry initiative aiming at increasing the performance of methods and simplifying their use. Thus, parts of the workflow described here will be changed (improved) in future.

Along with these developments, improved matching strategies for larger data sets will be implemented as well as functionality to compare `Spectra` directly to reference MS2 spectra from public annotation resources (e.g. Massbank or HMDB).

Regarding SWATH data analysis, future development will involve improved selection of the correct MS2 chromatographic peaks considering also correlation with intensity values across several samples.

# 5 Session information

    sessionInfo()

    ## R version 4.0.2 (2020-06-22)
    ## Platform: x86_64-w64-mingw32/x64 (64-bit)
    ## Running under: Windows Server 2012 R2 x64 (build 9600)
    ##
    ## Matrix products: default
    ##
    ## locale:
    ## [1] LC_COLLATE=C
    ## [2] LC_CTYPE=English_United States.1252
    ## [3] LC_MONETARY=English_United States.1252
    ## [4] LC_NUMERIC=C
    ## [5] LC_TIME=English_United States.1252
    ##
    ## attached base packages:
    ## [1] stats4    parallel  stats     graphics  grDevices utils     datasets
    ## [8] methods   base
    ##
    ## other attached packages:
    ##  [1] pander_0.6.3           MassSpecWavelet_1.54.0 waveslim_1.8.2
    ##  [4] xcms_3.10.2            MSnbase_2.14.2         ProtGenerics_1.20.0
    ##  [7] S4Vectors_0.26.1       mzR_2.22.0             Rcpp_1.0.5
    ## [10] BiocParallel_1.22.0    Biobase_2.48.0         BiocGenerics_0.34.0
    ## [13] BiocStyle_2.16.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] vsn_3.56.0                  foreach_1.5.0
    ##  [3] highr_0.8                   BiocManager_1.30.10
    ##  [5] affy_1.66.0                 GenomeInfoDbData_1.2.3
    ##  [7] yaml_2.2.1                  robustbase_0.93-6
    ##  [9] impute_1.62.0               pillar_1.4.6
    ## [11] lattice_0.20-41             glue_1.4.2
    ## [13] limma_3.44.3                digest_0.6.25
    ## [15] GenomicRanges_1.40.0        RColorBrewer_1.1-2
    ## [17] XVector_0.28.0              colorspace_1.4-1
    ## [19] htmltools_0.5.0             preprocessCore_1.50.0
    ## [21] Matrix_1.2-18               plyr_1.8.6
    ## [23] MALDIquant_1.19.3           XML_3.99-0.5
    ## [25] pkgconfig_2.0.3             magick_2.4.0
    ## [27] bookdown_0.20               zlibbioc_1.34.0
    ## [29] purrr_0.3.4                 scales_1.1.1
    ## [31] RANN_2.6.1                  affyio_1.58.0
    ## [33] tibble_3.0.3                farver_2.0.3
    ## [35] generics_0.0.2              IRanges_2.22.2
    ## [37] ggplot2_3.3.2               ellipsis_0.3.1
    ## [39] SummarizedExperiment_1.18.2 magrittr_1.5
    ## [41] crayon_1.3.4                evaluate_0.14
    ## [43] ncdf4_1.17                  doParallel_1.0.15
    ## [45] MASS_7.3-53                 tools_4.0.2
    ## [47] lifecycle_0.2.0             matrixStats_0.56.0
    ## [49] stringr_1.4.0               munsell_0.5.0
    ## [51] DelayedArray_0.14.1         pcaMethods_1.80.0
    ## [53] compiler_4.0.2              GenomeInfoDb_1.24.2
    ## [55] mzID_1.26.0                 rlang_0.4.7
    ## [57] grid_4.0.2                  RCurl_1.98-1.2
    ## [59] iterators_1.0.12            labeling_0.3
    ## [61] bitops_1.0-6                rmarkdown_2.3
    ## [63] gtable_0.3.0                codetools_0.2-16
    ## [65] R6_2.4.1                    knitr_1.29
    ## [67] dplyr_1.0.2                 stringi_1.5.3
    ## [69] vctrs_0.3.4                 DEoptimR_1.0-8
    ## [71] tidyselect_1.1.0            xfun_0.17

# References

1. Ludwig C, Gillet L, Rosenberger G, Amon S, Collins BC, Aebersold R: Data-independent acquisition-based SWATH-MS for quantitative proteomics: a tutorial. Molecular systems biology 2018, 14:e8126.