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:
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, Cox, and Mann 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
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").
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.
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.
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.
Sliding a window across a single precursor of known mass measures how the instrument transmits ions at different positions in the window.
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.
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
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.
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)
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() 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.
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.