Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
philouail committed Mar 18, 2024
1 parent 95bd882 commit 8f464ac
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 179 deletions.
59 changes: 12 additions & 47 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ setGeneric("addParams<-", function(object, value) standardGeneric("addParams<-")
setGeneric("addProcessHistory", function(object, ...)
standardGeneric("addProcessHistory"))

#' @aliases adjustRtime ObiwarpParam-class PeakGroupsParam-class LamaParama-class
#' @aliases adjustRtime ObiwarpParam-class PeakGroupsParam-class
#'
#' @title Alignment: Retention time correction methods.
#'
#' @description
#'
#' The `adjustRtime` method(s) perform retention time correction (alignment)
#' between chromatograms of different samples/dataset. Alignment is performed by default
#' on MS level 1 data. Retention times of spectra from other MS levels, if
#' present, are subsequently adjusted based on the adjusted retention times
#' of the MS1 spectra. Note that calling `adjustRtime` on a *xcms* result object
#' will remove any eventually present previous alignment results as well as
#' any correspondence analysis results. To run a second round of alignment,
#' raw retention times need to be replaced with adjusted ones using the
#' [applyAdjustedRtime()] function.
#' between chromatograms of different samples/dataset. Alignment is performed
#' by default on MS level 1 data. Retention times of spectra from other MS
#' levels, if present, are subsequently adjusted based on the adjusted
#' retention times of the MS1 spectra. Note that calling `adjustRtime` on a
#' *xcms* result object will remove any eventually present previous alignment
#' results as well as any correspondence analysis results. To run a second
#' round of alignment, raw retention times need to be replaced with adjusted
#' ones using the [applyAdjustedRtime()] function.
#'
#' The alignment method can be specified (and configured) using a dedicated
#' `param` argument.
Expand Down Expand Up @@ -100,10 +100,6 @@ setGeneric("addProcessHistory", function(object, ...)
#' @param BPPARAM parallel processing setup. Defaults to `BPPARAM = bpparam()`.
#' See [bpparam()] for details.
#'
#' @param bs For `LamaParama`: `character(1)` defining the GAM moothing method.
#' (defaults to thin plate; NB: B- and P-splines have been shown to produce
#' artefacts).
#'
#' @param centerSample \code{integer(1)} defining the index of the center sample
#' in the experiment. It defaults to
#' \code{floor(median(1:length(fileNames(object))))}. Note that if
Expand Down Expand Up @@ -164,16 +160,9 @@ setGeneric("addProcessHistory", function(object, ...)
#' @param initPenalty For `ObiwarpParam`: `numeric(1)` defining the penalty for
#' initiating an alignment (for local alignment only).
#'
#' @param lamas For `LamaParama`: `matrix` or `data.frame` with the m/z and
#' retention times values of features (as first and second column) from the
#' external dataset on which the alignment will be based on.
#'
#' @param localAlignment For `ObiwarpParam`: `logical(1)` whether a local
#' alignment should be performed instead of the default global alignment.
#'
#' @param method For `LamaParama`:`character(1)` with the type of warping.
#' Either `method = "gam"` or `method = "loess"` (default).
#'
#' @param minFraction For `PeakGroupsParam`: `numeric(1)` between 0 and 1
#' defining the minimum required proportion of samples in which peaks for
#' the peak group were identified. Peak groups passing this criteria will
Expand All @@ -191,12 +180,6 @@ setGeneric("addProcessHistory", function(object, ...)
#' @param object For `adjustRtime`: an [OnDiskMSnExp()], [XCMSnExp()],
#' [MsExperiment()] or [XcmsExperiment()] object.
#'
#' @param outlierTolerance For `LamaParama`: `numeric(1)` defining the settings
#' for outlier removal during the fitting. By default
#' (with `outlierTolerance = 3`), all data points with absolute residuals
#' larger than 3 times the mean absolute residual of all data points from
#' the first, initial fit, are removed from the final model fit.
#'
#' @param param The parameter object defining the alignment method (and its
#' setting).
#'
Expand All @@ -206,10 +189,6 @@ setGeneric("addProcessHistory", function(object, ...)
#' feature/peak group. The `adjustRtimePeakGroups` method is used by
#' default to determine this matrix on the provided `object`.
#'
#' @param ppm For `LamaParama`: `numeric(1)` defining the m/z-relative maximal
#' allowed difference in m/z between `lamas` and chromatographic peaks. Used
#' for the mapping of identified chromatographic peaks and lamas.
#'
#' @param response For `ObiwarpParam`: `numeric(1)` defining the
#' *responsiveness* of warping with `response = 0` giving linear warping on
#' start and end points and `response = 100` warping using all bijective
Expand All @@ -219,9 +198,9 @@ setGeneric("addProcessHistory", function(object, ...)
#' be used to interpolate corrected retention times for all peak groups.
#' Can be either `"loess"` or `"linear"`.
#'
#' @param span For `PeakGroupsParam` and `LamaParama`: `numeric(1)` defining
#' the degree of smoothing (if `smooth = "loess"` or `method = "loess"`).
#' This parameter is passed to the internal call to [loess()].
#' @param span For `PeakGroupsParam`: `numeric(1)` defining
#' the degree of smoothing (if `smooth = "loess"`). This parameter is
#' passed to the internal call to [loess()].
#'
#' @param subset For `ObiwarpParam` and `PeakGroupsParam`: `integer` with the
#' indices of samples within the experiment on which the alignment models
Expand All @@ -234,24 +213,10 @@ setGeneric("addProcessHistory", function(object, ...)
#' Supported options are `"previous"` and `"average"` (default).
#' See *Subset-based alignment* section for details.
#'
#' @param tolerance For `LamaParama`: `numeric(1)` defining the absolute
#' acceptable difference in m/z between lamas and chromatographic peaks.
#' Used for the mapping of identified chromatographic peaks and `lamas`.
#'
#' @param toleranceRt For `LamaParama`: `numeric(1)` defining the absolute
#' acceptable difference in retention time between lamas and
#' chromatographic peaks. Used for the mapping of identified chromatographic
#' peaks and `lamas`.
#'
#' @param value For all assignment methods: the value to set/replace.
#'
#' @param x An `ObiwarpParam`, `PeakGroupsParam` or `LamaParama` object.
#'
#' @param zeroWeight For `LamaParama`: `numeric(1)`: defines the weight of the
#' first data point (i.e. retention times of the first lama-chromatographic
#' peak pair). Values larger than 1 reduce warping problems in the early RT
#' range.
#'
#' @param ... ignored.
#'
#' @return
Expand Down
2 changes: 1 addition & 1 deletion R/XcmsExperiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ setMethod(
object
})

#'@rdname adjustRtime
#'@rdname LamaParama
setMethod(
"adjustRtime", signature(object = "XcmsExperiment", param = "LamaParama"),
function(object, param, BPPARAM = bpparam(), ...) {
Expand Down
75 changes: 62 additions & 13 deletions R/do_adjustRtime-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,9 @@ adjustRtimeSubset <- function(rtraw, rtadj, subset,
#'
#' Other functions related to this method:
#'
#' - `LamaParama()`: create the parameter object for alignment using
#' `adjustRtime()` function. Is also the input for functions listed below.
#' - `LamaParama()`: return the respective the parameter object for alignment
#' using `adjustRtime()` function. Is also the input for functions listed
#' below.
#'
#' - `matchLamasChromPeaks()`: quickly matches each file's ChromPeaks
#' to Lamas, allowing the user to evaluate the matches for each file.
Expand All @@ -634,18 +635,71 @@ adjustRtimeSubset <- function(rtraw, rtadj, subset,
#' - `matchedRtimes()`: Access the list of `data.frame` saved in the
#' `LamaParama` object, generated by the `matchLamasChromPeaks()` function.
#'
#' - `plot()`:plot the chromatographic peaks versus the reference lamas as
#' well as the fitting line for the chosen model type. The user can decide
#' what file to inspect by specifying the assay number with the parameter
#' `assay`
#'
#'
#' @param BPPARAM For `matchLamasChromPeaks()`: parallel processing setup.
#' Defaults to `BPPARAM = bpparam()`. See [bpparam()] for more information.
#'
#' @param bs For `LamaParama()`: `character(1)` defining the GAM smoothing method.
#' (defaults to thin plate, `bs = "tp"`)
#'
#' @param colPoints For `plot()`: color for the plotting of the datapoint.
#'
#' @param colFit For `plot()`: color of the fitting line.
#'
#' @param index For `plot()`: `numeric(1)` index of the file that should be
#' plotted.
#'
#' @param lamas For `LamaParama`: `matrix` or `data.frame` with the m/z and
#' retention times values of features (as first and second column) from the
#' external dataset on which the alignment will be based on.
#'
#'
#' @param method For `LamaParama`:`character(1)` with the type of warping.
#' Either `method = "gam"` or `method = "loess"` (default).
#'
#' @param object An object of class `XcmsExperiment` with defined ChromPeaks.
#'
#' @param outlierTolerance For `LamaParama`: `numeric(1)` defining the settings
#' for outlier removal during the fitting. By default
#' (with `outlierTolerance = 3`), all data points with absolute residuals
#' larger than 3 times the mean absolute residual of all data points from
#' the first, initial fit, are removed from the final model fit.
#'
#' @param param An object of class `LamaParama` that will later be used for
#' adjustment using the `[adjustRtime()]` function.
#'
#' @param LamaParama same object that will be passed to the `adjustRtime()`
#' function. To run this function the `matchLamasChromPeaks()` need to be run
#' on this first.
#' @param ppm For `LamaParama`: `numeric(1)` defining the m/z-relative maximal
#' allowed difference in m/z between `lamas` and chromatographic peaks. Used
#' for the mapping of identified chromatographic peaks and lamas.
#'
#' @param span For `LamaParama`: `numeric(1)` defining
#' the degree of smoothing (`method = "loess"`). This parameter is passed
#' to the internal call to [loess()].
#'
#' @param tolerance For `LamaParama`: `numeric(1)` defining the absolute
#' acceptable difference in m/z between lamas and chromatographic peaks.
#' Used for the mapping of identified chromatographic peaks and `lamas`.
#'
#' @param toleranceRt For `LamaParama`: `numeric(1)` defining the absolute
#' acceptable difference in retention time between lamas and
#' chromatographic peaks. Used for the mapping of identified chromatographic
#' peaks and `lamas`.
#'
#' @param x For `plot()`: object of class `LamaParama` to be plotted.
#'
#' @param xlab,ylab For `plot()`: x- and y-axis labels.
#'
#' @param zeroWeight For `LamaParama`: `numeric(1)`: defines the weight of the
#' first data point (i.e. retention times of the first lama-chromatographic
#' peak pair). Values larger than 1 reduce warping problems in the early RT
#' range.
#'
#' @param ... For `plot()`: extra parameters to be passed to the function.
#'
#' @return
#' For `matchLamasChromPeaks()`: A `LamaParama` object with new slot `rtMap`
Expand All @@ -671,6 +725,7 @@ adjustRtimeSubset <- function(rtraw, rtadj, subset,
#' tst <- loadXcmsData("faahko_sub2")
#'
#' ## create lamas input from the reference dataset
#' library(MsExperiment)
#' f <- sampleData(ref)$sample_type
#' f[f == "QC"] <- NA
#' ref <- filterFeatures(ref, PercentMissingFilter(threshold = 0, f = f))
Expand Down Expand Up @@ -861,7 +916,7 @@ matchLamasChromPeaks <- function(object, param, BPPARAM = bpparam()){
if (!hasChromPeaks(object))
stop("'object' needs to have detected ChromPeaks. ",
"Run 'findChromPeaks()' first.")
f <- as.factor(chromPeaks(object)[, "sample"], levels = seq_along(object))
f <- factor(chromPeaks(object)[, "sample"], levels = seq_along(object))
cp_raw <- split.data.frame(chromPeaks(object)[, c("mz", "rt")], f)
param@nChromPeaks <- vapply(cp_raw, nrow, numeric(1))
param@rtMap <- bplapply(cp_raw, FUN = function(x) {
Expand Down Expand Up @@ -894,13 +949,7 @@ summarizeLamaMatch <- function(param){
res
}

#' @title Perform linear interpolation for unsorted retention time.
#'
#' @description
#' This function performs linear interpolation on the non-sorted parts of an
#' input vector of retention time. To see more details on the interpolation,
#' see [approx()]
#'

#' @param rtime `numeric` vector with the retention times for one file/sample.
#'
#' @return vector with sorted retention time.
Expand Down
10 changes: 3 additions & 7 deletions R/functions-Params.R
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ PeakGroupsParam <- function(minFraction = 0.9, extraPeaks = 1,
subset = as.integer(subset), subsetAdjust = subsetAdjust)
}

#' @rdname adjustRtime
#' @rdname LamaParama
LamaParama <- function(lamas = matrix(ncol = 2, nrow = 0,
dimnames = list(NULL, c("mz", "rt"))),
method = c("loess", "gam"),
Expand All @@ -284,9 +284,7 @@ LamaParama <- function(lamas = matrix(ncol = 2, nrow = 0,
ppm = 20,
tolerance = 0,
toleranceRt = 5,
bs = "tp",
rtMap = list(),
nChromPeaks = numeric()) {
bs = "tp") {
method <- match.arg(method)
if (method == "gam")
.check_gam_library()
Expand All @@ -304,9 +302,7 @@ LamaParama <- function(lamas = matrix(ncol = 2, nrow = 0,
ppm = ppm,
tolerance = tolerance,
toleranceRt = toleranceRt,
bs = bs,
rtMap = rtMap,
nChromPeaks = nChromPeaks)
bs = bs)
}

#' @rdname adjustRtime
Expand Down
44 changes: 15 additions & 29 deletions R/methods-Params.R
Original file line number Diff line number Diff line change
Expand Up @@ -1252,35 +1252,21 @@ setReplaceMethod("subsetAdjust", "PeakGroupsParam", function(object, value) {
############################################################
## LamaParama

#' @title Plot summary of information of matching lamas to chromPeaks
#'
#' @description
#' the `plot()` function for `LamaParama` object allows to plot the obs
#' chromatographic peaks versus the reference lamas as well as the fitting
#' line for the chosen model type. The user can decide what file to inspect by
#' specifying the assay number with the parameter `assay`
#'
#' @param assay `numeric(1)`, assay that should be plotted.
#'
#' @return A plot
#'
#' @export
#'
#' @noRd
setMethod("plot", "LamaParama", function(x, index = 1L, colPoints = "#00000060",
colFit = "#00000080",
xlab = "Matched Chromatographic peaks",
ylab = "Lamas",
main = NULL,...){
model <- xcms:::.rt_model(method = param@method,
rt_map= x@rtMap[[index]], span = param@span,
resid_ratio = param@outlierTolerance,
zero_weight = param@zeroWeight,
bs = param@bs)
x <- x@rtMap[[index]]
plot(x, type = "p", xlab = xlab, ylab = ylab, col = "blue",
main = main)
points(model, type = "l", col = "black")
#' @rdname LamaParama
setMethod("plot", signature(x = "LamaParama"),
function(x, index = 1L,
colPoints = "#00000060",
colFit = "#00000080",
xlab = "Matched Chromatographic peaks",
ylab = "Lamas",...){
model <- .rt_model(method = x@method,
rt_map= x@rtMap[[index]], span = x@span,
resid_ratio = x@outlierTolerance,
zero_weight = x@zeroWeight,
bs = x@bs)
datap <- x@rtMap[[index]]
plot(datap, type = "p", xlab = xlab, ylab = ylab, col = colPoints, ...)
points(model, type = "l", col = colFit)
})


Expand Down
Loading

0 comments on commit 8f464ac

Please sign in to comment.