Skip to content

Commit

Permalink
PeakGroups alignment adjusts rtime on all MS levels
Browse files Browse the repository at this point in the history
- Ensure that adjustRtime,XCMSnExp,PeakGroupsParam does adjust retention times
  of all MS levels (issue #209, #208). Add a respective unit test.
  • Loading branch information
jorainer committed Aug 30, 2017
1 parent b17a3b3 commit 1eb875c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
29 changes: 20 additions & 9 deletions R/methods-XCMSnExp.R
Original file line number Diff line number Diff line change
Expand Up @@ -1657,12 +1657,18 @@ setMethod("groupChromPeaks",
#'
#' @description \code{adjustRtime,XCMSnExp,PeakGroupsParam}:
#' performs retention time correction based on the alignment of peak groups
#' (features) found in all/most samples.
#'
#' @note This method requires that a correspondence has been performed on the
#' data (see \code{\link{groupChromPeaks}}). Calling \code{adjustRtime} on
#' an \code{XCMSnExp} object will cause all peak grouping (correspondence)
#' results and any previous retention time adjustments to be dropped.
#' (features) found in all/most samples. The correction function identified
#' on these peak groups is applied to the retention time of all spectra in
#' the object, i.e. retention times of all spectra, also MS level > 1 are
#' adjusted.
#'
#' @note This method requires that a correspondence analysis has been performed
#' on the data, i.e. that grouped chromatographic peaks/features are present
#' (see \code{\link{groupChromPeaks}} for details).
#'
#' Calling \code{adjustRtime} on an \code{XCMSnExp} object will cause all
#' peak grouping (correspondence) results and any previous retention time
#' adjustments to be dropped.
#' In some instances, the \code{adjustRtime,XCMSnExp,PeakGroupsParam}
#' re-adjusts adjusted retention times to ensure them being in the same
#' order than the raw (original) retention times.
Expand Down Expand Up @@ -1692,8 +1698,10 @@ setMethod("groupChromPeaks",
setMethod("adjustRtime",
signature(object = "XCMSnExp", param = "PeakGroupsParam"),
function(object, param) {
if (hasAdjustedRtime(object))
if (hasAdjustedRtime(object)) {
message("Removing previous alignment results")
object <- dropAdjustedRtime(object)
}
if (!hasChromPeaks(object))
stop("No chromatographic peak detection results in 'object'! ",
"Please perform first a peak detection using the ",
Expand Down Expand Up @@ -1733,10 +1741,13 @@ setMethod("adjustRtime",
if (length(ph)) {
object <- addProcessHistory(object, ph[[length(ph)]])
}
## Add the process history step.
## Add the process history step, get the msLevel from the peak
## detection step.
ph <- processHistory(object, type = .PROCSTEP.PEAK.DETECTION)
xph <- XProcessHistory(param = param, date. = startDate,
type. = .PROCSTEP.RTIME.CORRECTION,
fileIndex = 1:length(fileNames(object)))
fileIndex = 1:length(fileNames(object)),
msLevel = msLevel(ph[[length(ph)]]))
object <- addProcessHistory(object, xph)
if (validObject(object))
object
Expand Down
23 changes: 20 additions & 3 deletions inst/unitTests/runit.XCMSnExp.R
Original file line number Diff line number Diff line change
Expand Up @@ -1262,10 +1262,27 @@ test_adjustRtimePeakGroups <- function() {
## No NAs allowed across samples:
isNa <- apply(pkGrp, MARGIN = 1, function(z) sum(is.na(z)))
checkTrue(all(isNa == 0))
pkGrp <- xcms:::adjustRtimePeakGroups(xod_xg,
param = PeakGroupsParam(minFraction = 0.5))
pkGrp <- xcms:::adjustRtimePeakGroups(
xod_xg, param = PeakGroupsParam(minFraction = 0.5))
isNa <- apply(pkGrp, MARGIN = 1, function(z) sum(is.na(z)))
checkTrue(max(isNa) == 1)

## Test adjustRtime adjusting also MS level > 1.
## Artificially changing the MS level of some spectra.
xod_mod <- xod_xg
## Select the spectra for MS level 2:
idx_ms2 <- c(300:500, 300:500 + 1277, 300:500 + 2554)
xod_mod@featureData$msLevel[idx_ms2] <- 2
xod_mod_adj <- adjustRtime(xod_mod,
param = PeakGroupsParam(span = 0.4))
## rtime of the MS level 2 spectra are expected to be adjusted too
checkEquals(rtime(xod_xgr), rtime(xod_mod_adj))
checkTrue(all(rtime(xod_mod)[idx_ms2] != rtime(xod_mod_adj)[idx_ms2]))
}

test_MS1_MS2_data <- function() {
## That's to test stuff for issues #208 and related.
## Set every other spectra in the original files to MS2.
}

test_extractMsData <- function() {
Expand Down Expand Up @@ -1334,7 +1351,7 @@ test_processHistory <- function() {
ph <- processHistory(xod_xgrg)
checkTrue(length(ph) == 4)
ph <- processHistory(xod_xgrg, msLevel = 1L)
checkTrue(length(ph) == 1)
checkTrue(length(ph) == 2)
checkEquals(as.character(class(processParam(ph[[1]]))), "CentWaveParam")
}

Expand Down
16 changes: 11 additions & 5 deletions man/adjustRtime-peakGroups.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1eb875c

Please sign in to comment.