Skip to content

Commit

Permalink
Improve performance of chromPeakSpectra and featureSpectra
Browse files Browse the repository at this point in the history
  • Loading branch information
jorainer committed Oct 26, 2018
1 parent 9137359 commit a26a40e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 31 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: xcms
Version: 3.3.4
Date: 2018-10-24
Version: 3.3.5
Date: 2018-10-26
Title: LC/MS and GC/MS Data Analysis
Author: Colin A. Smith <csmith@scripps.edu>,
Ralf Tautenhahn <rtautenh@gmail.com>,
Expand Down
21 changes: 11 additions & 10 deletions R/do_findChromPeaks-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -2336,6 +2336,17 @@ do_define_isotopes <- function(peaks., maxCharge = 3, maxIso = 5,
#'
#' @param polarity character(1) defining the polarity, either \code{"positive"}
#' or \code{"negative"}.
#'
#' @note
#'
#' Reference for considered adduct distances:
#' Huang N.; Siegel M.M.1; Kruppa G.H.; Laukien F.H.; J Am Soc Mass Spectrom
#' 1999, 10, 1166-1173.
#'
#' Reference for contaminants:
#' Interferences and comtaminants encountered in modern mass spectrometry
#' Bernd O. Keller, Jie Sui, Alex B. Young and Randy M. Whittal, ANALYTICA
#' CHIMICA ACTA, 627 (1): 71-81)
#'
#' @return see do_define_isotopes.
#'
Expand All @@ -2351,16 +2362,6 @@ do_define_adducts <- function(peaks., polarity = "positive") {
}
if (is.data.frame(peaks.))
peaks. <- as.matrix(peaks.)
## considered adduct distances
## reference: Huang N.; Siegel M.M.1; Kruppa G.H.; Laukien F.H.; J Am Soc
## Mass Spectrom 1999, 10, 1166–1173; Automation of a Fourier transform ion
## cyclotron resonance mass spectrometer for acquisition, analysis, and
## e-mailing of high-resolution exact-mass electrospray ionization mass
## spectral data
## see also for contaminants: Interferences and contaminants encountered
## in modern mass spectrometry (Bernd O. Keller, Jie Sui, Alex B. Young
## and Randy M. Whittal, ANALYTICA CHIMICA ACTA, 627 (1): 71-81)

mH <- 1.0078250322
mNa <- 22.98976928
mK <- 38.96370649
Expand Down
42 changes: 23 additions & 19 deletions R/functions-XCMSnExp.R
Original file line number Diff line number Diff line change
Expand Up @@ -1828,7 +1828,11 @@ exportMetaboAnalyst <- function(x, file = NULL, label,
#' Return the index of all MS2 spectra that are within a given rt and m/z
#' range.
#'
#' @param x `XCMSnExp` object.
#' @param precursorMz_all `numeric` with the precursor m/z for all spectra of
#' an `XCMSnExp` (i.e. the result from `precursorMz(x)`).
#'
#' @param rt_all `numeric` with the retention time for all spectra of an
#' `XCMSnExp` object (i.e. the result from `rtime(x)`).
#'
#' @param rt `numeric(2)` with the retention time range in which spectra
#' should be identified.
Expand All @@ -1843,17 +1847,16 @@ exportMetaboAnalyst <- function(x, file = NULL, label,
#'
#' @author Johannes Rainer
#'
#' @md
#'
#' @noRd
ms2_spectra_in_slice <- function(x, rt, mz, expandRt = 0, expandMz = 0,
ppm = 0) {
spectra_in_slice <- function(precursorMz_all, rt_all, rt, mz,
expandRt = 0, expandMz = 0, ppm = 0) {
rt <- range(rt) + c(-expandRt, expandRt)
mz_ppm <- mean(mz) * ppm / 1e6
mz_ppm <- (mz[1] + mz[2]) * ppm / 2e6
mz <- range(mz) + c(-expandMz, expandMz) + c(-mz_ppm, mz_ppm)
pmz <- precursorMz(x)
rtm <- rtime(x)
which(msLevel(x) > 1 &
(pmz > mz[1] & pmz < mz[2]) &
(rtm > rt[1] & rtm < rt[2]))
which((precursorMz_all > mz[1] & precursorMz_all < mz[2]) &
(rt_all > rt[1] & rt_all < rt[2]))
}

#' For details see chromPeakSpectra
Expand All @@ -1878,22 +1881,22 @@ ms2_spectra_for_peaks <- function(x, expandRt = 0, expandMz = 0,
if (!(min(subset) >= 1 && max(subset) <= nrow(pks)))
stop("If 'subset' is defined it has to be >= 1 and <= ",
nrow(pks), ".")
use_subset <- TRUE
}
} else subset <- seq_len(nrow(pks))
x <- filterMsLevel(as(x, "OnDiskMSnExp"), 2L)
fromF <- fromFile(x)
## We are faster getting all MS2 spectra once at the start.
sps <- spectra(x)
pmz <- precursorMz(x)
rtm <- rtime(x)
res <- vector(mode = "list", nrow(pks))
for (i in 1:nrow(pks)) {
if (use_subset && (!i %in% subset))
next
for (i in subset) {
if (skipFilled && pks[i, "is_filled"] == 1)
next
idx <- ms2_spectra_in_slice(x, rt = pks[i, c("rtmin", "rtmax")],
mz = pks[i, c("mzmin", "mzmax")],
expandRt = expandRt,
expandMz = expandMz, ppm = ppm)
idx <- spectra_in_slice(
precursorMz_all = pmz, rt_all = rtm,
rt = pks[i, c("rtmin", "rtmax")],
mz = pks[i, c("mzmin", "mzmax")],
expandRt = expandRt, expandMz = expandMz, ppm = ppm)
idx <- idx[fromF[idx] == pks[i, "sample"]]
if (length(idx)) {
if (length(idx) > 1 & method != "all") {
Expand Down Expand Up @@ -2011,7 +2014,8 @@ ms2_spectra_for_features <- function(x, expandRt = 0, expandMz = 0, ppm = 0,
sp_pks <- ms2_spectra_for_peaks(x, expandRt = expandRt,
expandMz = expandMz, ppm = ppm,
skipFilled = skipFilled,
subset = unique(unlist(idxs)), ...)
subset = unique(unlist(
idxs)), ...)
res <- lapply(idxs, function(z) unlist(sp_pks[z]))
names(res) <- rownames(featureDefinitions(x))
res
Expand Down
5 changes: 5 additions & 0 deletions inst/NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Changes in version 3.3.5

- Performance enhancement of the chromPeakSpectra and featureSpectra functions.


Changes in version 3.3.4

- Add featureChromatograms to extract ion chromatograms for each feature.
Expand Down

0 comments on commit a26a40e

Please sign in to comment.