Aligns chromatograms using parametric time warping ("ptw"), as
implemented in ptw, or landmark parametric time warping
("lptw"), with warping functions estimated from within-group averages.
This is useful when samples fall into batches with shared retention time
shifts, or when individual samples contain peaks absent in other groups that
would otherwise confound alignment. One warp is fit per group rather than one
per sample, so the cost of warping falls roughly in proportion to the number
of groups.
Usage
correct_rt_group(
chrom_list,
lambdas,
groups,
reference = "best",
reference_group = NULL,
alg = c("ptw", "lptw"),
init.coef = c(0, 1, 0),
n_traces = NULL,
fill_zeros = FALSE,
n_zeros = 0,
scale = FALSE,
trwdth = 0.2,
plot_it = FALSE,
penalty = NULL,
max_shift = 1,
rt_window = 0.3,
spectral_cutoff = 0.95,
regularization = 0,
verbose = getOption("verbose"),
show_progress = NULL,
cl = 2,
what = c("corrected.values", "models"),
...,
n.traces = NULL,
n.zeros = NULL,
maxshift = NULL
)Arguments
- chrom_list
A list of chromatograms in matrix format, sharing a common time axis (see
preprocess).- lambdas
A character or numeric vector specifying the wavelengths to use for alignment.
- groups
A vector of group assignments for each chromatogram, or a single string naming a metadata attribute from which to extract the group assignments (e.g.
"batch"). If a vector is provided, it must be the same length aschrom_listand may be named (matchingnames(chrom_list)) or positional. All samples must have a group assignment;NAvalues will trigger an error.- reference
Index or name of a sample in
chrom_listwhose group will be used as the alignment reference. Defaults to"best", which selects the reference group automatically usingbestrefapplied to the group averages.- reference_group
Name of the group to use as the reference group. If supplied, overrides
reference. Defaults toNULL.- alg
Alignment algorithm to use: parametric time warping (
"ptw", the default) or landmark parametric time warping ("lptw").- init.coef
Starting values for the optimization. Named to match the argument of the same name in
ptw, which receives it. Foralg = "lptw", only its length is used, setting the degree of the warping polynomial.- n_traces
Number of wavelengths to warp by, chosen from
lambdas(or from all wavelengths iflambdasis missing) by the CODA criterion inselect.traces. Defaults toNULL, which uses every wavelength inlambdas.- fill_zeros
Logical. If
TRUE, out-of-bounds regions produced by warping are filled with zeros; otherwise they are returned asNA(default).- n_zeros
Number of zeros to pad onto each end of the chromatograms before fitting. Only used when
alg = "ptw".- scale
Logical. If
TRUE, divide each chromatogram by its largest absolute value before fitting the warp, so that samples of very different concentration are matched on the shape of their signal. The corrected values are returned in their original units; to rescale the data themselves, use themaxIargument ofpreprocess."ptw"is unaffected under its default criterion, weighted cross-correlation, which does not change when a sample is multiplied by a constant.- trwdth
Width of the triangle in the weighted cross-correlation (WCC) criterion, passed to
ptw. Defaults to0.2, which matches the default of20timepoints inptwitself on a chromatogram sampled every0.01minutes. Roughly a peak width is a reasonable choice.- plot_it
Logical. Whether to plot alignment. Defaults to
FALSE.- penalty
Unused.
correct_rt_groupdoes not support"vpdtw", so the"vpdtw"penalty has no effect here.- max_shift
Unused.
correct_rt_groupdoes not support"vpdtw", so the"vpdtw"shift limit has no effect here.- rt_window
Numeric. Argument to
match_peaks. Maximum allowed difference in retention time, in minutes, between a reference and sample peak to be considered a candidate match. Defaults to0.3.- spectral_cutoff
Numeric. Argument to
match_peaks. Minimum spectral correlation for a candidate peak pair to be retained. Defaults to0.95.- regularization
Numeric >= 0. Strength of the penalty against warping for
alg = "lptw": a dimensionless weight pulling the warp toward the identity warp, passed aspenaltytofit_penalized_warp. A higher number allows less warping;0(the default) applies no penalty at all.1weights a unit of squared warp displacement equally against a unit of squared peak-position mismatch, and around2.4halves the applied warp, so useful values run roughly0-10. Unbounded above, approaching the identity warp asymptotically.- verbose
Logical. Whether to print a progress message as the warping models are fit and applied. Defaults to
getOption("verbose").- show_progress
Logical. Whether to show progress bar. Defaults to
TRUEifpbapplyis installed. Has no effect whenalg = "vpdtw".- cl
Either an integer specifying the number of cores to use for parallel processing or a cluster object created by
makeCluster. Defaults to2. On Windows systems, integers are ignored. Has no effect whenalg = "vpdtw".- what
What to return:
"corrected.values"(the default) for the warped chromatograms, or"models"for aptw_listorlptw_listholding each sample's group warp, which can be passed tocorrect_peaksor tocorrect_rtviamodels.- ...
For
alg = "ptw", optional additional arguments passed toptw. The only argument that cannot be changed iswarp.type, which is hard-coded to"global"to permit warping on multiple wavelengths. Foralg = "lptw", optional arguments passed tofit_penalized_warp:penalty_scale(per-coefficient penalty multipliers) andmin_peaks(minimum matched peaks required to fit the polynomial). Itspenaltyargument is set byregularizationrather than through....- n.traces
The
n.tracesargument is deprecated. Please usen_tracesinstead.- n.zeros
The
n.zerosargument is deprecated. Please usen_zerosinstead.- maxshift
Deprecated and unused. See
max_shift.
Value
If what = "corrected.values", a list of warped chromatogram
matrices in the same order as chrom_list, with each sample warped using
the warp coefficients estimated from its group average. If
what = "models", a ptw_list (alg = "ptw") or lptw_list
(alg = "lptw") with one model per sample, in the same order.
Details
With alg = "lptw", peaks are found in each group average by get_peaks
at its default settings and matched to the peaks of the reference group's
average, so landmarks come from the averaged signal rather than from
individual samples.
