Using msPurity for Precursor Ion Purity Assessments, Data Processing and Metabolite Annotation of Mass Spectrometry Fragmentation Data

Introduction

The msPurity R package was originally developed to assess the contribution of the targeted precursor in a fragmentation isolation window using a metric called “precursor ion purity”. See associated paper (Lawson et al. 2017).

A number of updates have been made since the original paper and the full functionality of msPurity now includes the following:

  • Assess the contribution of the targeted precursor of acquired fragmentation spectra by checking isolation windows using a metric called “precursor ion purity” (Works for both LC-MS(/MS) and DIMS(/MS) data)
  • Assess the anticipated precursor ion purity of XCMS LC-MS features where no fragmentation has been acquired
  • Map fragmentation spectra to XCMS LC-MS features
  • Filter and average MS/MS spectra from an LC-MS/MS dataset
  • Create a spectral-database of LC-MS(/MS) data and associated annotations
  • Perform spectral matching of query MS/MS spectra against library MS/MS spectra
  • Export fragmentation spectra to MSP format

What we call “Precursor ion purity” is a measure of the contribution of a selected precursor peak in an isolation window used for fragmentation. The simple calculation involves dividing the intensity of the selected precursor peak by the total intensity of the isolation window. When assessing MS/MS spectra this calculation is done before and after the MS/MS scan of interest and the purity is interpolated at the time of the MS/MS acquisition. The calculation is similar to the “Precursor Ion Fraction” (PIF) metric described by (Michalski et al. 2011) for proteomics with the exception that purity here is interpolated at the recorded point of MS/MS acquisition using bordering full-scan spectra. Low-abundance ions can be excluded, and an instrument-specific isolation efficiency curve can weight the contributing ions.

The supported workflows use two main classes

  • purityA
    • Assessing precursor purity of previously acquired MS/MS spectra: A user has acquired either LC-MS/MS or DIMS/MS spectra and an assessment is made of the precursor purity for each MS/MS scan
    • Further processing of the purityA object include - fragmentation spectra filtering, averaging, MSP creation, spectral-database creation and spectral matching
  • purityX
    • Assessing precursor purity of anticipated isolation windows for LC-MS/MS spectra. i.e. a user has acquired from an LC-MS full scan (MS1) data and an assessment is to be made of the precursor purity of detected features using anticipated or theoretical isolation windows. This information can then be used to guide further targeted MS/MS experiments.

The DIMS workflow, including purityD() and dimsPredictPuritySingle(), is deprecated and will receive no further bug fixes or feature development. There is no direct replacement in msPurity. Existing calls remain functional during deprecation; see help("msPurity-deprecated", package = "msPurity").

purityA

Assessing precursor purity of previously acquired MS/MS spectra

Start with the four runs in msPurityData. Two contain acquired MS/MS scans (LCMSMS_) and two contain MS1 scans only (LCMS_). Convert them into one Parquet study. Both workflows below reuse these spectra.

library(msPurity)
library(xcms)
mzml_dir <- system.file("extdata", "lcms", "mzML", package = "msPurityData",
                        mustWork = TRUE)
files <- list.files(mzml_dir, pattern = "[.]mzML$", full.names = TRUE)
work <- tempfile("mspurity-")
dir.create(work)
study <- file.path(work, "study")
MsBackendParquet::mzMLToParquet(files, path = study, engine = "mzr",
                                verbose = FALSE)
raw <- Spectra::Spectra(Spectra::backendInitialize(
    MsBackendParquet::MsBackendParquet(), path = study))
is_dda <- startsWith(basename(Spectra::dataOrigin(raw)), "LCMSMS_")
dda <- raw[is_dda]

For each fragmentation event, purityA() assesses the isolation window in the preceding and following MS1 scans and interpolates to the acquisition time. Select MS2 events in the feature detection region (30–90 seconds, precursor m/z 100–200). Retain every MS1 scan and its full mass range so each event has its original neighbours and potential contaminating ions.

Assessment of acquired MS/MS precursor ion purity for a DDA experiment (figure 1 of Lawson et al. 2017)
Assessment of acquired MS/MS precursor ion purity for a DDA experiment (figure 1 of Lawson et al. 2017)
rt <- Spectra::rtime(dda)
precursor <- Spectra::precursorMz(dda)
selected_ms2 <- rt >= 30 & rt <= 90 & precursor >= 100 & precursor <= 200
scans <- dda[which(Spectra::msLevel(dda) == 1L | selected_ms2)]
pa <- purityA(scans, cores = 1L)
## Warning: 9 MS2 precursors are outside their recorded isolation bounds; purity
## is unavailable. Supply offsets when known.
## Warning: 9 MS2 precursors are outside their recorded isolation bounds; purity
## is unavailable. Supply offsets when known.
purity <- purityTable(pa)
head(purity[c("filename", "seqNum", "precursorMZ", "precursorRT", "inPurity")])
##        filename seqNum precursorMZ precursorRT  inPurity
## 1 LCMSMS_1.mzML    193    136.0200    30.08727 0.8554426
## 2 LCMSMS_1.mzML    195    158.1538    30.08727 0.8173094
## 3 LCMSMS_1.mzML    199    171.1490    30.95213 0.7657010
## 4 LCMSMS_1.mzML    200    163.1327    30.95213 0.7066918
## 5 LCMSMS_1.mzML    201    187.1262    30.95213 0.8594988
## 6 LCMSMS_1.mzML    205    199.1692    31.85027 0.7561714

aPurity measures the peak closest to the recorded precursor mass in the nearest MS1 scan. iPurity measures the most intense peak in that window. inPurity is the interpolated score and inpkNm is the interpolated number of peaks in the window. Some example scans have a recorded precursor outside its isolation bounds. Their interpolated purity is missing.

Isolation efficiency

An ion’s position in the isolation window affects how much of its intensity reaches the fragmentation spectrum. In the example below the target (red) has weight 1, while a contaminant (black) has weight 0.1. Its contribution falls from 10,000 to 1,000 after weighting.

Example of a weighted isolation window
Example of a weighted isolation window

Sliding a window across a single precursor of known mass measures how the instrument transmits ions at different positions in the window.

Sliding a window across a precursor at m/z 200
Sliding a window across a precursor at m/z 200

The measured response for a Thermo Fisher Q Exactive with a 0.5 Da window is available as iwNormQE.5() (Lawson et al. 2017). Other options include a Gaussian curve (iwNormGauss()), a raised cosine (iwNormRcosine()), or a custom function returning a weight between zero and one for each offset.

Isolation efficiency on the Q Exactive (figure 3 of Lawson et al. 2017)
Isolation efficiency on the Q Exactive (figure 3 of Lawson et al. 2017)

To see how weighting affects the same scans, apply a Gaussian curve to one of the two DDA runs.

origin <- Spectra::dataOrigin(scans)
weighted <- purityA(scans[origin == origin[1]], cores = 1L, iwNorm = TRUE,
                    iwNormFun = iwNormGauss(sdlim = 3, minOff = -0.5, maxOff = 0.5))
## Warning: 9 MS2 precursors are outside their recorded isolation bounds; purity
## is unavailable. Supply offsets when known.
head(purityTable(weighted)[c("seqNum", "precursorMZ", "inPurity")])
##   seqNum precursorMZ  inPurity
## 1    193    136.0200 0.8668862
## 2    195    158.1538 0.8584787
## 3    199    171.1490 0.7684834
## 4    200    163.1327 0.7276813
## 5    201    187.1262 0.8914972
## 6    205    199.1692 0.7957914

Map fragmentation scans to xcms features

In xcms 4, each sample links to its MS1 spectra through the run recorded in dataOrigin. The helper below detects chromatographic peaks with centWave and groups peaks across the two runs. We use the same procedure later for the MS1-only samples. Restrict feature detection to 30–90 seconds for this example.

detect_features <- function(ms1, noise = 100) {
    origins <- unique(Spectra::dataOrigin(ms1))
    samples <- S4Vectors::DataFrame(sample_name = basename(origins), origin = origins)
    experiment <- MsExperiment::MsExperiment(sampleData = samples, spectra = ms1)
    experiment <- MsExperiment::linkSampleData(
        experiment, with = "sampleData.origin = spectra.dataOrigin")

    peaks <- findChromPeaks(experiment, param = CentWaveParam(
        snthresh = 3, noise = noise, ppm = 10, peakwidth = c(3, 30)),
        BPPARAM = BiocParallel::SerialParam())
    mz <- chromPeaks(peaks)[, "mz"]
    peaks <- filterChromPeaks(peaks, keep = mz >= 100 & mz <= 200)
    groupChromPeaks(peaks, param = PeakDensityParam(
        sampleGroups = rep("sample", nrow(samples)), minFraction = 0,
        bw = 5, binSize = 0.017))
}
ms1_window <- function(sp) {
    Spectra::filterRt(Spectra::filterMsLevel(sp, 1L), c(30, 90))
}
features <- detect_features(Spectra::filterMzRange(ms1_window(dda), c(100, 200)))
pa <- frag4feature(pa, xcmsObj = features)
linked <- groupedSpectra(pa, legacy = FALSE)
head(Spectra::spectraData(linked, c("precursorMz", "rtime")))
## DataFrame with 6 rows and 2 columns
##   precursorMz     rtime
##     <numeric> <numeric>
## 1     104.990   40.2070
## 2     104.990   39.8783
## 3     112.051   65.9289
## 4     112.051   64.9588
## 5     113.035   81.7076
## 6     113.035   82.4634

frag4feature() links a fragmentation scan to a chromatographic peak by its precursor mass, retention time and sample. A scan outside the detected peaks can still have a precursor purity measurement.

Filter, average and export fragmentation spectra

filterFragSpectra() flags peaks using signal-to-noise ratio, relative abundance, intensity and precursor purity. Here we pool scans linked to each feature across both runs and export the averages for exchange with MSP tools.

pa <- filterFragSpectra(pa)
pa <- averageAllFragSpectra(pa)
length(averagedSpectra(pa, legacy = FALSE))
## [1] 23
createMSP(pa, method = "all", msp_file_pth = file.path(work, "features.msp"))

To give each run its own average before combining runs, use the following sequence on the filtered object instead.

pa <- averageIntraFragSpectra(pa)
pa <- averageInterFragSpectra(pa)

Reopen and match the processed spectra

The results dataset stores feature associations, consensus spectra and links back to the raw study. Reopen the MS2 averages with readProcessedSpectra() and search them against the small MoNA library bundled with msPurity.

results <- createDatabase(pa, features, format = "parquet", study = study,
                          outDir = work, dbName = "results")
consensus <- readProcessedSpectra(results, msLevel = 2L, method = "all")
length(consensus)
## [1] 23
msp <- system.file("extdata", "tests", "library", "mini_mona.msp",
                   package = "msPurity", mustWork = TRUE)
reference <- convertLibraryToParquet(msp, file.path(work, "library"),
                                     format = "msp", dialect = "mona")
search <- spectralMatching(results, reference, format = "parquet",
                           q_spectraTypes = "av_all")
head(search$matchedResults[c("qpid", "library_accession", "dpc", "rank")])
##   qpid  library_accession       dpc rank
## 1    1 CCMSLIB00000577898 0.8739497    1
## 2    2           ML005101 0.9341948    1
## 3    3 CCMSLIB00003740024 0.5071041    1
## 4   19           PR100407 0.9693564    1

The LC-MS/MS processing and spectral matching vignette compares this score with BLINK (Harwood et al. 2023) and shows how to recover the match evidence from disk.

purityX

Predict precursor purity from an MS1-only run

purityX() estimates how much of a hypothetical isolation window a feature would occupy. Use the MS1-only runs from the same study and choose offsets that reflect the planned fragmentation experiment. Here a higher centWave noise threshold selects strong chromatographic peaks and speeds up feature detection. The purity calculation uses the original MS1 intensities across the full isolation window. Select the two features with the largest mean peak area observed in both runs.

Assessing anticipated isolation windows for two chromatographic features (figure 2 of Lawson et al. 2017)
Assessing anticipated isolation windows for two chromatographic features (figure 2 of Lawson et al. 2017)
ms1_features <- detect_features(ms1_window(raw[!is_dda]), noise = 5e5)
area <- rowMeans(featureValues(ms1_features, value = "into"))
selected <- head(order(area, decreasing = TRUE, na.last = NA), 2)
px <- purityX(ms1_features, xgroups = selected, purityType = "purityFWmedian",
              offsets = c(0.5, 0.5), ilim = 0, cores = 1L)
px@predictions
##   grpid      mean    median           sd         stde         RSD pknm
## 1    46 0.9951185 0.9951185 6.156590e-05 4.353366e-05 0.006186791  6.5
## 2   220 0.9963177 0.9963177 4.402101e-05 3.112755e-05 0.004418371  4.0
##            i       mz
## 1 2364626831 132.1017
## 2 1563307516 182.0808

Each row summarises one feature across its chromatographic peaks. Here each peak contributes its median purity over its full retention-time width. The mean and median describe predicted precursor purity, and pknm gives the median number of peaks in the isolation window. Features with high predicted purity are candidates for targeted MS/MS acquisition.

References

Harwood, Thomas V., Daniel G. C. Treen, Mingxun Wang, Wibe de Jong, Trent R. Northen, and Benjamin P. Bowen. 2023. “BLINK Enables Ultrafast Tandem Mass Spectrometry Cosine Similarity Scoring.” Scientific Reports 13: 13462. https://doi.org/10.1038/s41598-023-40496-9.
Lawson, Thomas Nigel, Ralf J. M. Weber, Martin R. Jones, et al. 2017. “msPurity: Automated Evaluation of Precursor Ion Purity for Mass Spectrometry Based Fragmentation in Metabolomics.” Analytical Chemistry, acs.analchem.6b04358. https://doi.org/10.1021/acs.analchem.6b04358.
Michalski, Annette, Juergen Cox, and Matthias Mann. 2011. “More than 100,000 detectable peptide species elute in single shotgun proteomics runs but the majority is inaccessible to data-dependent LC-MS/MS.” Journal of Proteome Research 10 (4): 1785–93. https://doi.org/10.1021/pr101060v.