| Title: | Deconvolution and Alignment of 1d NMR Spectra |
|---|---|
| Description: | A framework for deconvolution, alignment and postprocessing of 1-dimensional (1d) nuclear magnetic resonance (NMR) spectra, resulting in a data matrix of aligned signal integrals. The deconvolution part uses the algorithm described in Koh et al. (2009) <doi:10.1016/j.jmr.2009.09.003>. The alignment part is based on functions from the 'speaq' package, described in Beirnaert et al. (2018) <doi:10.1371/journal.pcbi.1006018> and Vu et al. (2011) <doi:10.1186/1471-2105-12-405>. A detailed description and evaluation of an early version of the package, 'MetaboDecon1D v0.2.2', can be found in Haeckl et al. (2021) <doi:10.3390/metabo11070452>. |
| Authors: | Tobias Schmidt [aut, cre, cph], Martina Haeckl [aut, cph], Yanren Linda Hu [ctb], Wolfram Gronwald [aut, cph] |
| Maintainer: | Tobias Schmidt <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 1.7.0 |
| Built: | 2026-05-10 07:59:42 UTC |
| Source: | https://github.com/spang-lab/metabodecon |
get_started and aaa_Get_Started both return (and optionally open) the URL
of the "Get Started" page of the metabodecon documentation. The
aaa_Get_Started version exists, because functions are listed alphabetically
in the reference manual and we want get_started to be shown at the top of
the list (i.e., it needs to start with an 'a').
aaa_Get_Started(open_browser = interactive()) get_started(open_browser = interactive())aaa_Get_Started(open_browser = interactive()) get_started(open_browser = interactive())
open_browser |
If TRUE, the "Get Stated" page is opened in the default browser. |
A character string containing the URL of the "Get Started" page.
2024-2025 Tobias Schmidt: initial version.
get_started(open_browser = FALSE) get_started()get_started(open_browser = FALSE) get_started()
Align signals across a list of deconvoluted spectra using the 'CluPA'
algorithm from the 'speaq' package, described in Beirnaert et al. (2018)
https://doi.org/10.1371/journal.pcbi.1006018 and Vu et al. (2011)
https://doi.org/10.1186/1471-2105-12-405 plus the additional peak combination described
in combine_peaks().
align( x, maxShift = 50, maxCombine = 0, verbose = TRUE, method = 2, install_deps = NULL, nworkers = 1, ref = NULL, full = TRUE )align( x, maxShift = 50, maxCombine = 0, verbose = TRUE, method = 2, install_deps = NULL, nworkers = 1, ref = NULL, full = TRUE )
x |
An object of type |
maxShift |
Maximum number of points along the "ppm-axis" a value can be moved by the
'speaq' package. 50 is a suitable starting value for plasma spectra with a
digital resolution of 128K. Note that this parameter has to be individually
optimized depending on the type of analyzed spectra and the digital
resolution. For urine which is more prone to chemical shift variations this
value most probably has to be increased. Passed as argument |
maxCombine |
Amount of adjacent columns which may be combined for improving
the alignment during the CluPA step. We recommend setting this
to 0 and instead relying on the peak snapping implemented in
|
verbose |
Whether to print additional information during the alignment process. |
method |
Alignment backend. |
install_deps |
Only used when |
nworkers |
Number of parallel workers for the alignment. Default is 1 (no parallelism). |
ref |
Optional reference spectrum of type |
full |
If |
An object of type align as described in Metabodecon Classes.
2024-2025 Tobias Schmidt: initial version.
if (interactive()) { # Example requires an interactive R session, because in case of missing # dependencies the user will be asked for confirmation to install them. decons <- deconvolute(sim[1:2], sfr = c(3.55, 3.35)) aligned <- align(decons) }if (interactive()) { # Example requires an interactive R session, because in case of missing # dependencies the user will be asked for confirmation to install them. decons <- deconvolute(sim[1:2], sfr = c(3.55, 3.35)) aligned <- align(decons) }
Convert a object to a Metabodecon object.
as_spectrum(x, sf = c(1000, 1e+06)) as_decon0(x, sf = NULL, spectrum = NULL, optional = TRUE) as_decon1( x, sf = c(1000, 1e+06), spectrum = NULL, sfr = NULL, wshw = NULL, bwc = 2 ) as_decon2( x, sf = c(1000, 1e+06), spectrum = NULL, sfr = NULL, wshw = NULL, bwc = 2 ) as_spectra( x, file_format = "bruker", expno = 10, procno = 10, raw = FALSE, silent = TRUE, force = FALSE ) as_decons0(x, sfs = list(c(1000, 1e+06)), spectra = list(NULL), nworkers = 1) as_decons1( x, sfs = list(c(1000, 1e+06)), spectra = list(NULL), sfrs = list(NULL), wshws = list(NULL), bwc = 2, nworkers = 1 ) as_decons2( x, sfs = list(c(1000, 1e+06)), spectra = list(NULL), sfrs = list(NULL), wshws = list(NULL), bwc = 2, nworkers = 1 )as_spectrum(x, sf = c(1000, 1e+06)) as_decon0(x, sf = NULL, spectrum = NULL, optional = TRUE) as_decon1( x, sf = c(1000, 1e+06), spectrum = NULL, sfr = NULL, wshw = NULL, bwc = 2 ) as_decon2( x, sf = c(1000, 1e+06), spectrum = NULL, sfr = NULL, wshw = NULL, bwc = 2 ) as_spectra( x, file_format = "bruker", expno = 10, procno = 10, raw = FALSE, silent = TRUE, force = FALSE ) as_decons0(x, sfs = list(c(1000, 1e+06)), spectra = list(NULL), nworkers = 1) as_decons1( x, sfs = list(c(1000, 1e+06)), spectra = list(NULL), sfrs = list(NULL), wshws = list(NULL), bwc = 2, nworkers = 1 ) as_decons2( x, sfs = list(c(1000, 1e+06)), spectra = list(NULL), sfrs = list(NULL), wshws = list(NULL), bwc = 2, nworkers = 1 )
x |
The object to convert. |
sf |
Scale factor used during Only required if |
spectrum, spectra
|
The |
optional |
Logical. If |
sfr, sfrs
|
|
wshw, wshws
|
|
bwc |
Level of backwards compatibility. If |
file_format |
The file_format of the spectrum file. E.g. |
expno, procno
|
The experiment/processing number for the file. E.g. |
raw |
If |
silent |
If |
force |
If |
sfs |
List of scale factors. Only required if |
nworkers |
Number of workers for parallel processing. |
An object of the specified class.
2024-2025 Tobias Schmidt: initial version.
dirpath <- metabodecon_file("sim_subset") spectra <- read_spectra(dirpath) spectrum <- spectra[[1]] decons1 <- generate_lorentz_curves_sim(spectra) decon1 <- generate_lorentz_curves_sim(spectrum) decon2 <- as_decon2(decon1)dirpath <- metabodecon_file("sim_subset") spectra <- read_spectra(dirpath) spectrum <- spectra[[1]] decons1 <- generate_lorentz_curves_sim(spectra) decon1 <- generate_lorentz_curves_sim(spectrum) decon2 <- as_decon2(decon1)
Helper function of plot_lorentz_curves_save_as_png().
Should not be called directly by the user.
Calculates the lorentz curves of each investigated spectrum.
This function has been deprecated with metabodecon version v1.4.3 and will be removed with version 2.0.0.
calculate_lorentz_curves(deconv_result, number_of_files = NA)calculate_lorentz_curves(deconv_result, number_of_files = NA)
deconv_result |
A list as returned by |
number_of_files |
Number of spectra to analyze |
If deconv_result holds the result of a single deconvolution, a matrix
containing the generated Lorentz curves is returned, where each row depicts
one Lorentz curve. If deconv_result is a list of deconvoluted spectra, a
list of such matrices is returned.
2020-2021 Martina Haeckl: initial version.
2024-2025 Tobias Schmidt: Minor updates to pass CRAN checks
MetaboDecon1D(),
plot_triplets(),
plot_lorentz_curves_save_as_png(),
plot_spectrum_superposition_save_as_png()
## -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- ## Deconvolute the spectra in folder "bruker/sim_subset" into a list of ## Lorentz Curves (specified via the parameters A, lambda and x_0). ## -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- decons <- generate_lorentz_curves_sim(sim[1:2]) decon0 <- decons[[1]] ## -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- ## Calculate the corresponding y values at each ppm value for each Lorentz ## Curve. I.e. you get a matrix of dimension n x m for each deconvolution, ## where n is the number of Lorentz Curves and m is the number of ppm values. ## -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- yy <- calculate_lorentz_curves(decons) y1 <- yy[[1]] dim(y1) ## -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- ## Visualize the 5th, 9th and 11th Lorentz curve. ## -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- nrs <- c(5, 9, 11) col <- c("red", "blue", "orange") desc <- paste("Lorentz curve", nrs) plot(decon0$x_values_ppm, decon0$y_values, type = "l", lty = 2) for (i in 1:3) lines(decon0$x_values_ppm, y1[nrs[i], ], col = col[i]) legend("topright", legend = desc, col = col, lty = 1)## -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- ## Deconvolute the spectra in folder "bruker/sim_subset" into a list of ## Lorentz Curves (specified via the parameters A, lambda and x_0). ## -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- decons <- generate_lorentz_curves_sim(sim[1:2]) decon0 <- decons[[1]] ## -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- ## Calculate the corresponding y values at each ppm value for each Lorentz ## Curve. I.e. you get a matrix of dimension n x m for each deconvolution, ## where n is the number of Lorentz Curves and m is the number of ppm values. ## -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- yy <- calculate_lorentz_curves(decons) y1 <- yy[[1]] dim(y1) ## -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- ## Visualize the 5th, 9th and 11th Lorentz curve. ## -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- nrs <- c(5, 9, 11) col <- c("red", "blue", "orange") desc <- paste("Lorentz curve", nrs) plot(decon0$x_values_ppm, decon0$y_values, type = "l", lty = 2) for (i in 1:3) lines(decon0$x_values_ppm, y1[nrs[i], ], col = col[i]) legend("topright", legend = desc, col = col, lty = 1)
check_mdrb() returns a boolean indicating whether a suitable version of the
metabodecon Rust backend mdrb is
currently installed.
check_mdrb_deps() returns a list with information about the
installation status of mdrb system dependencies.
check_mdrb(stop_on_fail = FALSE) check_mdrb_deps(verbose = FALSE)check_mdrb(stop_on_fail = FALSE) check_mdrb_deps(verbose = FALSE)
stop_on_fail |
If TRUE, an error is thrown if the check fails, providing instructions on how to install or upgrade mdrb. |
verbose |
If TRUE, additional information is printed during the check process. |
check_mdrb() returns TRUE if a suitable version of mdrb is installed,
else FALSE.
check_mdrb_deps() returns a data.frame as follows:
check passed comment
r R >= 4.2 TRUE Current: R 4.4.2
rtools Rtools exist TRUE Tested with: pkgbuild::has_build_tools()
cargo cargo >= 1.80 TRUE Current: cargo 1.84.1 (66221abde 2024-11-19)
rustc rustc >= 1.80 TRUE Current: rustc 1.84.1 (e71f9a9a9 2025-01-27)
Column check is a string describing the performed check.
Column passed is a boolean indicating whether the check passed.
Column comment is a string string describing the check result.
The rownames of the dataframe one-word descriptions of the performed checks.
2024-2025 Tobias Schmidt: initial version.
check_mdrb() # Checking dependencies might take more than 5 seconds, as it # requires the compilation of a small test program as well as # running `cargo --version` and `rustc --version`, which, # depending on your system, might involve updating or installing # Rust toolchain components. check_mdrb_deps(verbose = TRUE)check_mdrb() # Checking dependencies might take more than 5 seconds, as it # requires the compilation of a small test program as well as # running `cargo --version` and `rustc --version`, which, # depending on your system, might involve updating or installing # Rust toolchain components. check_mdrb_deps(verbose = TRUE)
Helper function of align(). Should not be called directly by the user.
Even after calling speaq_align(), the alignment of individual signals is
not always perfect, as 'speaq' performs a segment-wise alignment i.e. groups
of signals are aligned. For further improvements, partly filled neighboring
columns are merged. See 'Details' for an illustrative example.
Direct usage of this function has been deprecated with metabodecon version 1.4.3 and will be removed with metabodecon version 2.0.0.
combine_peaks( shifted_mat, range = 5, lower_bound = 1, spectrum_data = NULL, data_path = NULL )combine_peaks( shifted_mat, range = 5, lower_bound = 1, spectrum_data = NULL, data_path = NULL )
shifted_mat |
The matrix returned by |
range |
Amount of adjacent columns which are permitted to be used for improving the alignment. |
lower_bound |
Minimum amount of non-zero elements per column to trigger the alignment improvement. |
spectrum_data |
The list of deconvoluted spectra as returned by
|
data_path |
If not NULL, the returned dataframes |
Example of what the function does:
| | 1 | 2 | 3 | 4 | 5 | |----------- |------|------|------|------|------| | Spectrum 1 | 0.13 | 0 | 0 | 0.11 | 0 | | Spectrum 2 | 0 | 0.88 | 0 | 0.12 | 0 | | Spectrum 3 | 0.07 | 0.56 | 0.30 | 0 | 0 | | Spectrum 4 | 0.08 | 0 | 0.07 | 0 | 0.07 | | Spectrum 5 | 0.04 | 0 | 0 | 0.04 | 0 |
becomes
| | 1 | 2 | 3 | 4 | 5 | |----------- |------|------|------|------|------| | Spectrum 1 | 0.13 | 0 | 0 | 0.11 | 0 | | Spectrum 2 | 0 | 0.88 | 0 | 0.12 | 0 | | Spectrum 3 | 0.07 | 0.56 | 0 | 0.30 | 0 | | Spectrum 4 | 0.08 | 0 | 0 | 0.07 | 0.07 | | Spectrum 5 | 0.04 | 0 | 0 | 0.04 | 0 |
I.e.
Column 1 and 2 get NOT merged, because they have a common non-zero entry.
Column 3 and 4 get merged, because they are in range of each other and
have no common non-zero entries.
Column 4 and 5 get NOT merged, because it is more beneficial to merge column 3 and 4, as they have more mergeable entries and after merging column 3 and 4, column 4 and 5 have a common non-zero entry.
A list containing two data frames long and short. The first data
frame contains one column for each data point in the original spectrum. The
second data frame contains only columns where at least one entry is non-zero.
2021-2024 Wolfram Gronwald: initial version.
2024-2025 Tobias Schmidt: refactored initial version.
deps <- c("MassSpecWavelet", "impute") deps_installed <- sapply(deps, requireNamespace, quietly = TRUE) if (all(deps_installed)) { # 'speaq' requires 'MassSpecWavelet' and 'impute' to be installed sim_subset <- metabodecon_file("bruker/sim_subset") spectrum_data <- generate_lorentz_curves_sim(sim_subset) shifted_mat <- speaq_align(spectrum_data = spectrum_data, verbose = FALSE) range <- 5 lower_bound <- 1 obj <- combine_peaks(shifted_mat, range, lower_bound) str(obj) }deps <- c("MassSpecWavelet", "impute") deps_installed <- sapply(deps, requireNamespace, quietly = TRUE) if (all(deps_installed)) { # 'speaq' requires 'MassSpecWavelet' and 'impute' to be installed sim_subset <- metabodecon_file("bruker/sim_subset") spectrum_data <- generate_lorentz_curves_sim(sim_subset) shifted_mat <- speaq_align(spectrum_data = spectrum_data, verbose = FALSE) range <- 5 lower_bound <- 1 obj <- combine_peaks(shifted_mat, range, lower_bound) str(obj) }
Converts positions/widths from unit A to unit B. If the direction of units A
and B is reversed, the width's sign will be reversed as well. To keep widths
strictly positive, wrap the result with abs().
convert_pos(xa, ya, yb) convert_width(xa, ya, yb)convert_pos(xa, ya, yb) convert_width(xa, ya, yb)
xa |
A numeric vector specifying widths/positions in unit A. |
ya, yb
|
A numeric vector specifying the positions of at least two points in unit A / unit B. |
A numeric vector of values converted from unit A to unit B.
2024-2025 Tobias Schmidt: initial version.
ya <- c(244, 246, 248, 250, 252) yb <- c(15, 10, 5, 0, -5) convert_width(c(2, 4, 8), ya, yb) convert_pos(c(247, 249), ya, yb)ya <- c(244, 246, 248, 250, 252) yb <- c(15, 10, 5, 0, -5) convert_width(c(2, 4, 8), ya, yb) convert_pos(c(247, 249), ya, yb)
Returns the path to the directory where
download_example_datasets() stores metabodecon's example data sets or any
file within that directory. By default this directory is a subdirectory of
R's temporary session directory. If persistent is set to TRUE, the
directory equals the data directory returned by tools::R_user_dir()
instead.
datadir(file = NULL, warn = TRUE, persistent = NULL)datadir(file = NULL, warn = TRUE, persistent = NULL)
file |
Relative path to a file within the data directory. |
warn |
Print a warning message when the requested path does not yet exist? |
persistent |
Return the path to the persistent data directory instead of the temporary one? |
The decision to use a temporary data dir as default and a persistent one only optionally was made to conform to CRAN package policies, which state that:
Packages should not write in the user's home filespace (including
clipboards), nor anywhere else on the file system apart from the R
session's temporary directory [...] Limited exceptions may be allowed
in interactive sessions if the package obtains confirmation from the
user. For R version 4.0 or later [...] packages may store user-specific
data, configuration and cache files in their respective user directories
obtained from tools::R_user_dir() [...].
Source: cran.r-project.org/web/packages/policies.
Path to the data directory or a file within it.
2024-2025 Tobias Schmidt: initial version.
download_example_datasets(),
datadir_persistent(),
datadir_temp()
# Get temporary datadir and persistent datadir datadir(persistent = FALSE, warn = FALSE) datadir(persistent = TRUE, warn = FALSE) # Get persistent datadir if existing else temp datadir. Set `warn = TRUE` # to raise a warning if none of the directories exist yet. datadir(warn = FALSE) if (interactive()) datadir() # Get PERSISTENT_DATADIR/bruker if existing else TEMP_DATADIR/bruker datadir(file = "bruker/urine", warn = FALSE)# Get temporary datadir and persistent datadir datadir(persistent = FALSE, warn = FALSE) datadir(persistent = TRUE, warn = FALSE) # Get persistent datadir if existing else temp datadir. Set `warn = TRUE` # to raise a warning if none of the directories exist yet. datadir(warn = FALSE) if (interactive()) datadir() # Get PERSISTENT_DATADIR/bruker if existing else TEMP_DATADIR/bruker datadir(file = "bruker/urine", warn = FALSE)
Returns the path to the persistent data directory where
metabodecon's data sets are stored. This directory equals the data directory
returned by tools::R_user_dir() plus additional path normalization.
datadir_persistent()datadir_persistent()
Path to the persistent data directory.
2024-2025 Tobias Schmidt: initial version.
datadir_persistent()datadir_persistent()
Returns the path to the temporary data directory where
metabodecon's data sets are stored. This directory equals subdirectory 'data'
of metabodecons temporary session directory tmpdir() plus additional path
normalization.
datadir_temp()datadir_temp()
Returns the path to the temporary data directory.
2024-2025 Tobias Schmidt: initial version.
tmpdir(), datadir(), datadir_persistent()
datadir_temp()datadir_temp()
Returns the temporary session-scoped cache directory used by deconvolution
functions such as deconvolute() and the internal
grid_deconvolute_spectrum() helper.
decon_cachedir()decon_cachedir()
Path to the shared temporary cache directory for deconvolution.
2026 Tobias Schmidt: initial version.
decon_cachedir()decon_cachedir()
Deconvolutes NMR spectra by modeling each detected signal within a spectrum as Lorentz Curve.
deconvolute( x, nfit = 3, smopts = c(2, 5), delta = 6.4, sfr = NULL, wshw = 0, ask = FALSE, force = FALSE, verbose = TRUE, nworkers = 1, use_rust = FALSE, npmax = 0, igrs = list(), cadir = decon_cachedir() )deconvolute( x, nfit = 3, smopts = c(2, 5), delta = 6.4, sfr = NULL, wshw = 0, ask = FALSE, force = FALSE, verbose = TRUE, nworkers = 1, use_rust = FALSE, npmax = 0, igrs = list(), cadir = decon_cachedir() )
x |
A |
nfit |
Integer. Number of iterations for approximating the parameters for the Lorentz curves. See 'Details'. |
smopts |
Numeric vector with two entries: the number of smoothing iterations and the number of data points to use for smoothing (must be odd). See 'Details'. |
delta |
Threshold for peak filtering. Higher values result in more peaks
being filtered out. A peak is filtered if its score is below |
sfr |
Numeric vector with two entries: the ppm positions for the left and right border of the signal-free region of the spectrum. See 'Details'. |
wshw |
Half-width of the water artifact in ppm. See 'Details'. |
ask |
Logical. Whether to ask for user input during the deconvolution process. If FALSE, the provided default values will be used. |
force |
If FALSE, the function stops with an error message if no peaks are found in the signal free region (SFR), as these peaks are required as a reference for peak filtering. If TRUE, the function instead proceeds without peak filtering, potentially increasing runtime and memory usage significantly. |
verbose |
Logical. Whether to print log messages during the deconvolution process. |
nworkers |
Number of workers to use for parallel processing. If
|
use_rust |
Controls the deconvolution backend. Accepts |
npmax |
Integer. Maximum number of peaks allowed in the result. If
|
igrs |
Ignore regions. List of length-2 numeric vectors specifying the start and endpoints of the chemical shift regions to ignore during deconvolution. Peaks whose centers fall inside any ignore region are excluded from fitting. |
cadir |
Directory for caching grid search results and deconvolution
results for |
First, an automated curvature based signal selection is performed. Each signal is represented by 3 data points to allow the determination of initial Lorentz curves. These Lorentz curves are then iteratively adjusted to optimally approximate the measured spectrum.
A 'decon2' object as described in Metabodecon Classes.
2024-2025 Tobias Schmidt: initial version.
## Deconvolute a single spectrum spectrum <- sim[[1]] decon <- deconvolute(spectrum) ## Read multiple spectra from disk and deconvolute at once spectra_dir <- metabodecon_file("sim_subset") spectra <- read_spectra(spectra_dir) decons <- deconvolute(spectra, sfr = c(3.55, 3.35))## Deconvolute a single spectrum spectrum <- sim[[1]] decon <- deconvolute(spectrum) ## Read multiple spectra from disk and deconvolute at once spectra_dir <- metabodecon_file("sim_subset") spectra <- read_spectra(spectra_dir) decons <- deconvolute(spectra, sfr = c(3.55, 3.35))
Helper function of align(). Should not be called directly by the user.
Rewrite of speaq::dohCluster(), compatible with the data format returned by
'generate_lorentz_curves()' and 'gen_feat_mat()'. The function name
"dohCluster" comes from "Do Hierarchical Clustering" which is part of the
Alignment algorithm proposed by Vu et al. (2011) in
https://doi.org/10.1186/1471-2105-12-405.
Direct usage of this function has been deprecated with metabodecon version 1.4.3 and will be removed with metabodecon version 2.0.0.
dohCluster(X, peakList, refInd = 0, maxShift = 100, verbose = TRUE, method = 2)dohCluster(X, peakList, refInd = 0, maxShift = 100, verbose = TRUE, method = 2)
X |
Dataframe of signal intensities from all spectra as returned by
|
peakList |
List of peak indices as returned |
refInd |
Number of the reference spectrum i.e. the spectrum to which all signals will be aligned to. |
maxShift |
Maximum number of points a value can be moved. |
verbose |
Whether to print additional information during the alignment process. |
method |
Alignment backend. |
A list containing two data frames Y and new_peakList. The first one
contains the aligned spectra, the second one contains the aligned signals of
each spectrum.
2021-2024 Wolfram Gronwald: initial version.
2024-2025 Tobias Schmidt: refactored initial version.
deps <- c("MassSpecWavelet", "impute") deps_installed <- sapply(deps, requireNamespace, quietly = TRUE) if (all(deps_installed)) { # 'speaq' requires 'MassSpecWavelet' and 'impute' to be installed sim_subset <- metabodecon_file("bruker/sim_subset") decons <- generate_lorentz_curves_sim(sim_subset) feat <- gen_feat_mat(decons) refObj <- speaq::findRef(feat$peakList) hclObj <- dohCluster( X = feat$data_matrix, peakList = feat$peakList, refInd = refObj$refInd, maxShift = 100, verbose = TRUE ) str(hclObj, 1) }deps <- c("MassSpecWavelet", "impute") deps_installed <- sapply(deps, requireNamespace, quietly = TRUE) if (all(deps_installed)) { # 'speaq' requires 'MassSpecWavelet' and 'impute' to be installed sim_subset <- metabodecon_file("bruker/sim_subset") decons <- generate_lorentz_curves_sim(sim_subset) feat <- gen_feat_mat(decons) refObj <- speaq::findRef(feat$peakList) hclObj <- dohCluster( X = feat$data_matrix, peakList = feat$peakList, refInd = refObj$refInd, maxShift = 100, verbose = TRUE ) str(hclObj, 1) }
Downloads example datasets that can be used to test the functionality of the metabodecon package. These datasets are not included in the package by default due to size constraints. The datasets are downloaded as zip file and extracted automatically, unless extraction is disabled by the user.
download_example_datasets( dst_dir = NULL, extract = TRUE, persistent = NULL, overwrite = FALSE, silent = FALSE )download_example_datasets( dst_dir = NULL, extract = TRUE, persistent = NULL, overwrite = FALSE, silent = FALSE )
dst_dir |
The destination directory where the downloaded datasets will be stored. If NULL, the function will return the path to the cached zip file. |
extract |
Logical. If TRUE, the downloaded zip file will be extracted. |
persistent |
Logical. If TRUE, the downloaded datasets will be cached at
|
overwrite |
Logical. If TRUE, existing files with the same name in the destination directory will be overwritten. |
silent |
Logical. If TRUE, no output will be printed to the console. |
The path to the downloaded (and possibly extracted) datasets.
2024-2025 Tobias Schmidt: initial version.
if (interactive()) { zip <- download_example_datasets(extract = FALSE, persistent = FALSE) dir <- download_example_datasets(extract = TRUE) }if (interactive()) { zip <- download_example_datasets(extract = FALSE, persistent = FALSE) dir <- download_example_datasets(extract = TRUE) }
Draws a single spectrum. Internally used by plot_spectrum(), which is
usually the recommended way to plot spectra. For usage examples see
test/testthat/test-draw_spectrum.R.
draw_spectrum( obj, foc_rgn = NULL, foc_frac = NULL, foc_only = TRUE, add = FALSE, fig_rgn = NULL, main = NULL, show = TRUE, show_d2 = FALSE, truepar = NULL, mar = c(4.1, 5.1, 1.1, 1.1), sf_vert = "auto", si_line = list(), sm_line = list(), sp_line = list(), d2_line = list(), al_line = list(), lc_lines = list(), tp_lines = list(), al_lines = list(), cent_pts = list(), bord_pts = list(), norm_pts = list(), tp_pts = list(), fp_pts = list(), miss_pts = list(), bg_rect = list(), foc_rect = list(), lc_rects = list(), tp_rects = list(), bt_axis = list(), lt_axis = list(), tp_axis = list(), rt_axis = list(), bt_text = list(), lt_text = list(), tp_text = list(), rt_text = list(), tp_verts = list(), lc_verts = list(), al_verts = list(), ze_hline = list(), al_arrows = list(), lgd = list() )draw_spectrum( obj, foc_rgn = NULL, foc_frac = NULL, foc_only = TRUE, add = FALSE, fig_rgn = NULL, main = NULL, show = TRUE, show_d2 = FALSE, truepar = NULL, mar = c(4.1, 5.1, 1.1, 1.1), sf_vert = "auto", si_line = list(), sm_line = list(), sp_line = list(), d2_line = list(), al_line = list(), lc_lines = list(), tp_lines = list(), al_lines = list(), cent_pts = list(), bord_pts = list(), norm_pts = list(), tp_pts = list(), fp_pts = list(), miss_pts = list(), bg_rect = list(), foc_rect = list(), lc_rects = list(), tp_rects = list(), bt_axis = list(), lt_axis = list(), tp_axis = list(), rt_axis = list(), bt_text = list(), lt_text = list(), tp_text = list(), rt_text = list(), tp_verts = list(), lc_verts = list(), al_verts = list(), ze_hline = list(), al_arrows = list(), lgd = list() )
obj |
An object of type |
foc_rgn |
Numeric vector specifying the start and end of focus region in ppm. |
foc_frac |
Numeric vector specifying the start and end of focus region as fraction of the full spectrum width. |
foc_only |
Logical. If TRUE, only the focused region is drawn. If FALSE, the full spectrum is drawn. |
add |
If TRUE, draw into the currently open figure. If FALSE, start a new figure. |
fig_rgn |
Drawing region in normalized device coordinates as vector of the form |
main |
Main title of the plot. Drawn via |
show |
Logical. If FALSE, the function returns without doing anything. |
show_d2 |
Logical. If TRUE, the second derivative of the spectrum is drawn. Setting this to TRUE changes most of the defaults for the drawing, e.g. by disabling the drawing of anything related to signal intensities and by changing the y-axis label to "Second Derivative". |
truepar |
Data frame with columns x0, A and lambda containing the true lorentzian that
were used to simulate the spectrum. Required if any |
mar |
Number of lines below/left-of/above/right-of plot region. |
sf_vert |
Scale factor for vertical lines corresponding to
|
si_line, sm_line, sp_line, al_line, d2_line, lc_lines, tp_lines, al_lines
|
List of parameters passed to |
cent_pts, tp_pts, fp_pts, miss_pts, bord_pts, norm_pts
|
List of parameters passed to |
bg_rect, lc_rects, foc_rect, tp_rects
|
List of parameters passed to |
bt_axis, lt_axis, tp_axis, rt_axis
|
List of parameters used to overwrite the default values passed to
|
bt_text, lt_text, tp_text, rt_text
|
List of parameters used to overwrite the default values passed to |
lc_verts, tp_verts, al_verts
|
List of parameters passed to |
ze_hline |
List of parameters passed to |
al_arrows |
List of parameters passed to |
lgd |
List of parameters passed to |
Parameters bt_axis, lt_axis, tp_axis and rt_axis all support option
n and digits, where n = 5 means "Draw 5 tickmarks over the full axis
range" and digits = 3 means "round the label shown beside each tickmark to
3 digits". If n or digits is omitted, a suitable value is chosen
automatically. Providing a vector of digits causes each digit to be tried
until a digit is encountered that results in n unique labels. Example:
Assume we have n = 4 and the corresponding calculated tickmark positions
are: 1.02421, 1.02542, 1.02663 and 1.02784. If we provide digits = 1:5, the
following representations are tried:
| digit | label 1 | label 2 | label 3 | label 4 |
| 1 | 1.0 | 1.0 | 1.0 | 1.0 |
| 2 | 1.02 | 1.03 | 1.03 | 1.03 |
| 3 | 1.024 | 1.025 | 1.027 | 1.028 |
| 4 | 1.0242 | 1.0254 | 1.0266 | 1.0278 |
| 5 | 1.02421 | 1.02542 | 1.02663 | 1.02784 |
In the above example the process would stop at digit = 3, because at this
point we have n = 4 unique labels (1.024, 1.025, 1.027 and 1.028).
NULL. Called for side effect of plotting.
2024-2025 Tobias Schmidt: initial version.
decon <- deconvolute(sim[[1]], sfr = c(3.55, 3.35)) draw_spectrum(obj = decon) draw_spectrum(obj = decon, lgd = list(x = "top", bg = NA)) draw_spectrum(obj = decon, foc_rgn = c(3.45, 3.37)) draw_spectrum(obj = decon, add = FALSE, lgd = FALSE, fig = c(.2, .8, .2, .4), mar = c( 0, 0, 0, 0)) draw_spectrum(obj = decon, add = TRUE, lgd = FALSE, fig = c(0.2, 0.8, 0.6, 0.8), mar = c(0, 0, 0, 0)) draw_spectrum(obj = decon, lc_lines = NULL, lc_rects = NULL, foc_only = FALSE)decon <- deconvolute(sim[[1]], sfr = c(3.55, 3.35)) draw_spectrum(obj = decon) draw_spectrum(obj = decon, lgd = list(x = "top", bg = NA)) draw_spectrum(obj = decon, foc_rgn = c(3.45, 3.37)) draw_spectrum(obj = decon, add = FALSE, lgd = FALSE, fig = c(.2, .8, .2, .4), mar = c( 0, 0, 0, 0)) draw_spectrum(obj = decon, add = TRUE, lgd = FALSE, fig = c(0.2, 0.8, 0.6, 0.8), mar = c(0, 0, 0, 0)) draw_spectrum(obj = decon, lc_lines = NULL, lc_rects = NULL, foc_only = FALSE)
Evaluates an expression with a predefined global state, including the:
working directory (set via setwd())
global options (set via options())
graphical parameters (set via par())
In addition to that, evalwith allows to:
Redirect or capture the output and/or message stream via sink()
Measure the runtime of the evaluated expression via system.time()
Creating a temporary test directory (inside tmpdir()) and populating it
with input files according to inputs
Predefine answers for calls to readline() happening during evaluation of
expr
Caching the result of the expression
All changes to the global state are reverted after the expression has been evaluated.
evalwith( expr, testdir = NULL, answers = NULL, output = NULL, message = NULL, plot = NULL, datadir_temp = c("default", "missing", "empty", "filled")[1], datadir_persistent = c("default", "missing", "empty", "filled")[1], inputs = character(), opts = NULL, pars = NULL, cache = FALSE, overwrite = FALSE )evalwith( expr, testdir = NULL, answers = NULL, output = NULL, message = NULL, plot = NULL, datadir_temp = c("default", "missing", "empty", "filled")[1], datadir_persistent = c("default", "missing", "empty", "filled")[1], inputs = character(), opts = NULL, pars = NULL, cache = FALSE, overwrite = FALSE )
expr |
Expression to be evaluated. |
testdir |
ID of the test directory. E.g. |
answers |
Answers to be returned by readline(). |
output |
Path to the file where output stream should be redirected to.
Use |
message |
Path to the file where message stream be redirected to. Use
|
plot |
An expression opening a device, the string "captured" or a path
ending in ".pdf", ".svg", or ".png". Examples: |
datadir_temp |
State of the mocked temporary data directory. See details section. |
datadir_persistent |
State of the mocked persistent data directory. See details section. |
inputs |
Paths to be copied to the test directory before evaluating
|
opts |
Named list of options to be set. See |
pars |
Named list of parameters to be set. See |
cache |
Logical indicating whether to cache the result of the expression. |
overwrite |
Logical indicating whether to overwrite the cache file if it already exists. |
The datadir_temp and datadir_persistent arguments accept values
"missing", "filled" and "empty". Setting a value unequal NULL causes the
functions datadir_temp() and/or
datadir_persistent() to be replaced with mock functions
pointing to fake directories. Functions depending on these functions will
then use the fake directories instead of the real ones. When set to "missing"
the returned mock directory does not exist. When set to "empty" it exists and
is guaranteed to be empty. When set to "filled", it is populated with example
datasets.
Attention: the mocked functions, i.e. datadir_temp() and
datadir_persistent() cannot be used directly inside expr
when called via devtools::test(). I'm not sure why, but it seems as if
devtools and/or testthat have their own copies of the functions which are
used when the expression is evaluated.
A list containing with following elements:
rv: The return value of the expression.
runtime: The "elapsed" runtime of the expression in seconds. Measured
with system.time().
output: The captured output.
message: The captured messages.
plot: The path to the saved plot.
testdir: The path to the test directory.
inputs: The paths to the copied input files.
2024-2025 Tobias Schmidt: initial version.
x1 <- evalwith(output = "captured", cat("Helloworld\n")) str(x1) x2 <- evalwith(datadir_persistent = "missing", message = "captured", datadir()) str(x2) x3 <- evalwith(testdir = "dummy", inputs = "bruker/urine/urine_1", dir()) str(x3) x4 <- evalwith(Sys.sleep(0.02)) str(x4)x1 <- evalwith(output = "captured", cat("Helloworld\n")) str(x1) x2 <- evalwith(datadir_persistent = "missing", message = "captured", datadir()) str(x2) x3 <- evalwith(testdir = "dummy", inputs = "bruker/urine/urine_1", dir()) str(x3) x4 <- evalwith(Sys.sleep(0.02)) str(x4)
Helper function of align(). Should not be called directly by the user.
Generates a list of elements required by speaq_align().
See 'Value' for a detailed description of the list elements.
Direct usage of this function has been deprecated with metabodecon version 1.4.3 and will be removed with metabodecon version 2.0.0.
gen_feat_mat( data_path, ppm_range = get_ppm_range(data_path), si_size_real_spectrum = length(data_path$y_values), scale_factor_x = 1000, warn = TRUE )gen_feat_mat( data_path, ppm_range = get_ppm_range(data_path), si_size_real_spectrum = length(data_path$y_values), scale_factor_x = 1000, warn = TRUE )
data_path |
A list of deconvoluted spectra as returned by
|
ppm_range |
The ppm range over which your signals are distributed. |
si_size_real_spectrum |
Number of data points in your spectra. |
scale_factor_x |
The x scale factor used during the deconvolution. |
warn |
Whether to print a warning in case a file path is passed to
|
Before version 1.2 of metabodecon, the deconvolution functions
generate_lorentz_curves and MetaboDecon1D wrote their output partially as
txt files to their input folder. Back then, gen_feat_mat() used those txt
files as input to generate the feature matrix. Since version 1.2 these txt
files are no longer created by default, to prevent accidental modifications
of the input folders. Therefore, the recommended way to pass the required
information to gen_feat_mat() is to directly pass the output of
generate_lorentz_curves() to gen_feat_mat(). However, to stay backwards
compatible, the name of parameter data_path was not changed and passing an
actual path to data_path is still possible, but will result in a warning
(unless warn is set to FALSE).
A list with the following elements:
data_matrix: A data.frame where each row corresponds to one spectrum and
each column to one data point, i.e. for 10 input spectra
with 131072 data points each data_matrix would have
dimensions 10 x 131072.
peakList: A list of vectors, where each vector contains the indices of
the peaks in the corresponding spectrum. The indices increase
from left to right, i.e. the smallest index corresponds to the
highest ppm value, as the ppm values decrease from left to
right.
w: A list of vectors where each vector contains the "position parameter"
of the peaks in the corresponding spectrum.
A: A list of vectors where each vector contains the "area parameter" of
the peaks in the corresponding spectrum.
lambda: A list of vectors where each vector contains the "width
parameter" of the peaks in the corresponding spectrum.
2021-2024 Wolfram Gronwald: initial version.
2024-2025 Tobias Schmidt: refactored initial version.
sim_subset <- metabodecon_file("sim_subset") decons <- generate_lorentz_curves_sim(sim_subset) obj <- gen_feat_mat(decons) str(obj, 2, give.attr = FALSE)sim_subset <- metabodecon_file("sim_subset") decons <- generate_lorentz_curves_sim(sim_subset) obj <- gen_feat_mat(decons) str(obj, 2, give.attr = FALSE)
Deconvolutes NMR spectra by modeling each detected signal within a spectrum as Lorentz Curve.
This function has been deprecated with metabodecon version v1.4.3 and will be
removed with version 2.0.0. Please use deconvolute() instead.
generate_lorentz_curves( data_path, file_format = "bruker", make_rds = FALSE, expno = 10, procno = 10, raw = TRUE, nfit = 10, smopts = c(2, 5), delta = 6.4, sfr = c(11.44494, -1.8828), wshw = 0.1527692, ask = TRUE, force = FALSE, verbose = TRUE, nworkers = 1 ) generate_lorentz_curves_sim( data_path, file_format = "bruker", make_rds = FALSE, expno = 10, procno = 10, raw = TRUE, nfit = 10, smopts = c(2, 5), delta = 6.4, sfr = c(3.55, 3.35), wshw = 0, ask = FALSE, force = FALSE, verbose = FALSE, nworkers = 1 )generate_lorentz_curves( data_path, file_format = "bruker", make_rds = FALSE, expno = 10, procno = 10, raw = TRUE, nfit = 10, smopts = c(2, 5), delta = 6.4, sfr = c(11.44494, -1.8828), wshw = 0.1527692, ask = TRUE, force = FALSE, verbose = TRUE, nworkers = 1 ) generate_lorentz_curves_sim( data_path, file_format = "bruker", make_rds = FALSE, expno = 10, procno = 10, raw = TRUE, nfit = 10, smopts = c(2, 5), delta = 6.4, sfr = c(3.55, 3.35), wshw = 0, ask = FALSE, force = FALSE, verbose = FALSE, nworkers = 1 )
data_path |
The path of the file/folder containing the spectrum data. E.g.
|
file_format |
The file_format of the spectrum file. E.g. |
make_rds |
Logical or character. If TRUE, stores results as an RDS file on disk. If a character string, saves the RDS file with the specified name. Should be set to TRUE if many spectra are evaluated to decrease computation time. |
expno, procno
|
The experiment/processing number for the file. E.g. |
raw |
If |
nfit |
Integer. Number of iterations for approximating the parameters for the Lorentz curves. See 'Details'. |
smopts |
Numeric vector with two entries: the number of smoothing iterations and the number of data points to use for smoothing (must be odd). See 'Details'. |
delta |
Threshold for peak filtering. Higher values result in more peaks
being filtered out. A peak is filtered if its score is below |
sfr |
Numeric vector with two entries: the ppm positions for the left and right border of the signal-free region of the spectrum. See 'Details'. |
wshw |
Half-width of the water artifact in ppm. See 'Details'. |
ask |
Logical. Whether to ask for user input during the deconvolution process. If FALSE, the provided default values will be used. |
force |
If |
verbose |
Logical. Whether to print log messages during the deconvolution process. |
nworkers |
Number of workers to use for parallel processing. If
|
First, an automated curvature based signal selection is performed. Each signal is represented by 3 data points to allow the determination of initial Lorentz curves. These Lorentz curves are then iteratively adjusted to optimally approximate the measured spectrum.
generate_lorentz_curves_sim() is identical to generate_lorentz_curves()
except for the defaults, which are optimized for deconvoluting the 'sim'
dataset, shipped with 'metabodecon'. The 'sim' dataset is a simulated
dataset, which is much smaller than a real NMR spectra and lacks a water
signal. This makes it ideal for use in examples. However, the default values
for sfr, wshw, and delta in the "normal" generate_lorentz_curves()
function are not optimal for this dataset. To avoid having to define the
optimal parameters repeatedly in examples, this function is provided to
deconvolute the "Sim" dataset with suitable parameters.
A 'decon1' object if a single spectrum was provided. A 'decons1' object if multiple spectra were provided. See Metabodecon Classes for details.
2024-2025 Tobias Schmidt: initial version.
## Define the paths to the example datasets we want to deconvolute: ## `sim_dir`: directory containing 16 simulated spectra ## `sim_01`: path to the first spectrum in the `sim` directory ## `sim_01_spec`: the first spectrum in the `sim` directory as a dataframe sim_dir <- metabodecon_file("sim_subset") sim_1_dir <- file.path(sim_dir, "sim_01") sim_2_dir <- file.path(sim_dir, "sim_02") sim_1_spectrum <- read_spectrum(sim_1_dir) sim_2_spectrum <- read_spectrum(sim_2_dir) sim_spectra <- read_spectra(sim_dir) ## Show that `generate_lorentz_curves()` and `generate_lorentz_curves_sim()` ## produce the same results: sim_1_decon0 <- generate_lorentz_curves( data_path = sim_1_dir, # Path to directory containing spectra sfr = c(3.55, 3.35), # Borders of signal free region (SFR) in ppm wshw = 0, # Half width of water signal (WS) in ppm ask = FALSE, # Don't ask for user input verbose = FALSE # Suppress status messages ) sim_1_decon1 <- generate_lorentz_curves_sim(sim_1_dir) stopifnot(all.equal(sim_1_decon0, sim_1_decon1)) ## Show that passing a spectrum produces the same results as passing the ## the corresponding directory: decon_from_spectrum_dir <- generate_lorentz_curves_sim(sim_1_dir) decon_from_spectrum_obj <- generate_lorentz_curves_sim(sim_1_spectrum) decons_from_spectra_obj <- generate_lorentz_curves_sim(sim_spectra) decons_from_spectra_dir <- generate_lorentz_curves_sim(sim_dir) most.equal <- function(x1, x2) { ignore <- which(names(x1) %in% c("number_of_files", "filename")) equal <- all.equal(x1[-ignore], x2[-ignore]) invisible(stopifnot(isTRUE(equal))) } all.equal( decon_from_spectrum_dir, decon_from_spectrum_obj ) all.equal( decons_from_spectra_dir, decons_from_spectra_obj ) most.equal( decon_from_spectrum_dir, decons_from_spectra_obj[[1]]) most.equal( decon_from_spectrum_dir, decons_from_spectra_dir[[1]])## Define the paths to the example datasets we want to deconvolute: ## `sim_dir`: directory containing 16 simulated spectra ## `sim_01`: path to the first spectrum in the `sim` directory ## `sim_01_spec`: the first spectrum in the `sim` directory as a dataframe sim_dir <- metabodecon_file("sim_subset") sim_1_dir <- file.path(sim_dir, "sim_01") sim_2_dir <- file.path(sim_dir, "sim_02") sim_1_spectrum <- read_spectrum(sim_1_dir) sim_2_spectrum <- read_spectrum(sim_2_dir) sim_spectra <- read_spectra(sim_dir) ## Show that `generate_lorentz_curves()` and `generate_lorentz_curves_sim()` ## produce the same results: sim_1_decon0 <- generate_lorentz_curves( data_path = sim_1_dir, # Path to directory containing spectra sfr = c(3.55, 3.35), # Borders of signal free region (SFR) in ppm wshw = 0, # Half width of water signal (WS) in ppm ask = FALSE, # Don't ask for user input verbose = FALSE # Suppress status messages ) sim_1_decon1 <- generate_lorentz_curves_sim(sim_1_dir) stopifnot(all.equal(sim_1_decon0, sim_1_decon1)) ## Show that passing a spectrum produces the same results as passing the ## the corresponding directory: decon_from_spectrum_dir <- generate_lorentz_curves_sim(sim_1_dir) decon_from_spectrum_obj <- generate_lorentz_curves_sim(sim_1_spectrum) decons_from_spectra_obj <- generate_lorentz_curves_sim(sim_spectra) decons_from_spectra_dir <- generate_lorentz_curves_sim(sim_dir) most.equal <- function(x1, x2) { ignore <- which(names(x1) %in% c("number_of_files", "filename")) equal <- all.equal(x1[-ignore], x2[-ignore]) invisible(stopifnot(isTRUE(equal))) } all.equal( decon_from_spectrum_dir, decon_from_spectrum_obj ) all.equal( decons_from_spectra_dir, decons_from_spectra_obj ) most.equal( decon_from_spectrum_dir, decons_from_spectra_obj[[1]]) most.equal( decon_from_spectrum_dir, decons_from_spectra_dir[[1]])
Returns the path to the directory storing the example files shipped with metabodecon.
Deprecated since metabodecon v1.2.0. Please use datadir() instead. See
examples below for usage.
get_data_dir( dataset_name = c("", "blood", "test", "urine", "aki"), warn = TRUE )get_data_dir( dataset_name = c("", "blood", "test", "urine", "aki"), warn = TRUE )
dataset_name |
Either |
warn |
Whether to print a warning message when the example folders do
not yet exist, i.e. |
Path to the directory storing the example files.
2024-2025 Tobias Schmidt: initial version.
x <- get_data_dir("urine") # Deprecated y <- datadir("example_datasets/bruker/urine") # Preferred cat(x, y, sep = "\n")x <- get_data_dir("urine") # Deprecated y <- datadir("example_datasets/bruker/urine") # Preferred cat(x, y, sep = "\n")
Returns the names of a metabodecon collection object.
get_names(x, default = "spectrum_%d")get_names(x, default = "spectrum_%d")
x |
A metabodecon collection object. |
default |
Default names if no names are found. Passed on to |
A character vector of names.
2024-2025 Tobias Schmidt: initial version.
s1 <- list() s2 <- list(name = "foo") s3 <- list(name = "foo", meta = list(name = "bar")) get_names(list(s1, s1)) # c("spectrum_1", "spectrum_2") get_names(list(s1, myspec = s1)) # c("spectrum_1", "myspec") get_names(list(s1, myspec = s2)) # c("spectrum_1", "foo") get_names(list(s1, myspec = s3)) # c("spectrum_1", "bar")s1 <- list() s2 <- list(name = "foo") s3 <- list(name = "foo", meta = list(name = "bar")) get_names(list(s1, s1)) # c("spectrum_1", "spectrum_2") get_names(list(s1, myspec = s1)) # c("spectrum_1", "myspec") get_names(list(s1, myspec = s2)) # c("spectrum_1", "foo") get_names(list(s1, myspec = s3)) # c("spectrum_1", "bar")
Helper function of align(). Should not be called directly by the user.
Returns the ppm range across all peaks of the provided deconvoluted spectra.
Direct usage of this function has been deprecated with metabodecon version 1.4.3 and will be removed with metabodecon version 2.0.0.
get_ppm_range(spectrum_data, full_range = FALSE)get_ppm_range(spectrum_data, full_range = FALSE)
spectrum_data |
A list of deconvoluted spectra as returned by |
full_range |
If TRUE, the full range of the spectra is returned. If FALSE, only the range from the lowest to the highest peak center is returned. |
A vector containing the lowest and highest ppm value over all peaks of the provided deconvoluted spectra.
2021-2024 Wolfram Gronwald: initial version.
2024-2025 Tobias Schmidt: refactored initial version.
spectrum_data <- generate_lorentz_curves( data_path = sim[1:2], nfit = 3, sfr = c(3.55, 3.35), wshw = 0, ask = FALSE, verbose = FALSE ) ppm_rng <- get_ppm_range(spectrum_data) print(ppm_rng)spectrum_data <- generate_lorentz_curves( data_path = sim[1:2], nfit = 3, sfr = c(3.55, 3.35), wshw = 0, ask = FALSE, verbose = FALSE ) ppm_rng <- get_ppm_range(spectrum_data) print(ppm_rng)
Extracts a peak-area matrix from aligned spectra. Rows are
chemical-shift positions, columns are spectra. When
maxCombine = 0 the raw aligned integral vectors are returned.
When maxCombine > 0, peaks are combined using one of two
methods controlled by combineMethod.
get_si_mat(x, ref = NULL, drop_zero = FALSE, maxCombine = 0, combineMethod = 2)get_si_mat(x, ref = NULL, drop_zero = FALSE, maxCombine = 0, combineMethod = 2)
x |
An object of type |
ref |
A single |
drop_zero |
If |
maxCombine |
Controls peak combining in datapoints. |
combineMethod |
Peak-combining backend. |
Method 1 (combineMethod = 1) passes the transposed
integral matrix to combine_peaks() with
range = maxCombine. Partly-filled neighbouring columns are
merged when they share no common non-zero row.
Method 2 (combineMethod = 2) maps every peak to the
nearest reference peak and keeps it only if the distance is
at most maxCombine datapoints. Areas of peaks that map to
the same reference peak are summed.
Example for method 2 with ref peaks at indices 5, 9, 20 and
maxCombine = 4:
Step 1 – Build intervals [ref ± maxCombine]:
ref: 5 9 20
| | |
int: [1 ····· 9] [5 ···· 13] [16 ···· 24]
overlap!
Step 2 – Shrink overlapping neighbours to midpoint.
Refs 1 & 2 overlap → mid = floor((5+9)/2) = 7.
Refs 2 & 3 don't → keep maxCombine boundary.
ref: 5 9 20
| | |
int: [1 ··· 7] [8 ·· 13] gap [16 ···· 24]
Step 3 – Assign peaks to nearest ref; keep if ≤ maxCombine:
| Peak | Nearest | Dist | ≤ 4? | Action |
|------|---------|------|------|--------|
| 3 | ref 1 | 2 | yes | keep |
| 6 | ref 1 | 1 | yes | keep |
| 8 | ref 2 | 1 | yes | keep |
| 11 | ref 2 | 2 | yes | keep |
| 15 | ref 3 | 5 | no | DROP |
| 21 | ref 3 | 1 | yes | keep |
Step 4 – Sum areas (A × pi) per reference peak:
ref 1 ← A(3) + A(6) (peaks at 3, 6)
ref 2 ← A(8) + A(11) (peaks at 8, 11)
ref 3 ← A(21) (peak 15 dropped)
A numeric matrix with chemical shifts as rownames and spectrum names as colnames.
2024-2025 Tobias Schmidt: initial version.
if (interactive()) { decons <- deconvolute(sim[1:2], sfr = c(3.55, 3.35)) aligns <- align(decons, maxCombine = 0) si_mat_0 <- get_si_mat(aligns) si_mat_1 <- get_si_mat(aligns, maxCombine = 20) si_mat_2 <- get_si_mat(aligns, maxCombine = 20, combineMethod = 1) }if (interactive()) { decons <- deconvolute(sim[1:2], sfr = c(3.55, 3.35)) aligns <- align(decons, maxCombine = 0) si_mat_0 <- get_si_mat(aligns) si_mat_1 <- get_si_mat(aligns, maxCombine = 20) si_mat_2 <- get_si_mat(aligns, maxCombine = 20, combineMethod = 1) }
Returns the first and last n rows of a matrix or data frame. If the input
has fewer than 2*n rows, overlapping rows are returned only once.
headtail(x, n = 6)headtail(x, n = 6)
x |
A matrix or data frame. |
n |
Number of rows to take from the top and bottom. |
A subset of x containing head and tail rows.
2024-2026 Tobias Schmidt: initial version.
x <- matrix(seq_len(30), nrow = 10) headtail(x, n = 2)x <- matrix(seq_len(30), nrow = 10) headtail(x, n = 2)
Installs metabodecon's Rust backend mdrb from R-Universe.
lifecycle::badge("experimental")
install_mdrb(ask = TRUE, ...)install_mdrb(ask = TRUE, ...)
ask |
Whether to ask for confirmation before attempting installation. Default is TRUE. |
... |
Additional arguments to pass to |
NULL. Called for side effect of installing the Rust backend.
2024-2025 Tobias Schmidt: initial version.
if (interactive()) try(install_mdrb())if (interactive()) try(install_mdrb())
Check if an object is an instance of a specific 'Metabodecon Class'. See Metabodecon Classes for a list of classes.
is_spectrum(x, check_class = TRUE, check_contents = FALSE) is_decon0(x) is_decon1(x) is_decon2(x) is_align(x) is_spectra( x, check_class = TRUE, check_contents = FALSE, check_child_classes = FALSE ) is_decons0(x) is_decons1(x) is_decons2(x) is_aligns(x)is_spectrum(x, check_class = TRUE, check_contents = FALSE) is_decon0(x) is_decon1(x) is_decon2(x) is_align(x) is_spectra( x, check_class = TRUE, check_contents = FALSE, check_child_classes = FALSE ) is_decons0(x) is_decons1(x) is_decons2(x) is_aligns(x)
x |
The object to check. |
check_class |
Logical indicating whether to check the class of the object. |
check_contents |
Logical indicating whether to check the contents of the object. |
check_child_classes |
Logical indicating whether to check the class of each element of the object. |
TRUE if the object is an instance of the specified class, otherwise FALSE.
2024-2025 Tobias Schmidt: initial version.
ss <- sim[1:2] s1 <- sim[[1]] is_spectra(ss) # TRUE is_spectrum(s1) # TRUE is_spectrum(s1, check_contents = TRUE) # TRUE dd <- deconvolute(ss, sfr = c(3.55, 3.35)) d1 <- dd[[1]] is_decons0(dd) # FALSE is_decons1(dd) # FALSE is_decons2(dd) # TRUE is_decon0(d1) # FALSE is_decon1(d1) # FALSE is_decon2(d1) # TRUE if (interactive()) { # Example requires an interactive R session, because in case of missing # dependencies the user will be asked for confirmation to install them. aa <- align(dd) a1 <- aa[[1]] is_align(a1) # TRUE is_aligns(aa) # TRUE }ss <- sim[1:2] s1 <- sim[[1]] is_spectra(ss) # TRUE is_spectrum(s1) # TRUE is_spectrum(s1, check_contents = TRUE) # TRUE dd <- deconvolute(ss, sfr = c(3.55, 3.35)) d1 <- dd[[1]] is_decons0(dd) # FALSE is_decons1(dd) # FALSE is_decons2(dd) # TRUE is_decon0(d1) # FALSE is_decon1(d1) # FALSE is_decon2(d1) # TRUE if (interactive()) { # Example requires an interactive R session, because in case of missing # dependencies the user will be asked for confirmation to install them. aa <- align(dd) a1 <- aa[[1]] is_align(a1) # TRUE is_aligns(aa) # TRUE }
Creates a spectrum object from the provided signal intensities, frequencies and chemical shifts.
make_spectrum( si, cs_max, cs_width, fq_ref, fq_width = NULL, force = FALSE, silent = FALSE, name = NULL, path = NULL, type = NULL, simpar = NULL, mfs = NULL )make_spectrum( si, cs_max, cs_width, fq_ref, fq_width = NULL, force = FALSE, silent = FALSE, name = NULL, path = NULL, type = NULL, simpar = NULL, mfs = NULL )
si |
Numeric vector of signal intensities, ordered from highest to lowest corresponding chemical shift. |
cs_max |
The highest chemical shift value in ppm, usually shown as left end of the spectrum. |
cs_width |
The width of the spectrum in ppm. |
fq_ref |
The reference frequency in Hz. |
fq_width |
The width of the spectrum in Hz. Only used to check whether the values
calculated from |
force |
If |
silent |
If |
name |
The name of the spectrum, e.g. "Blood 1" or "Urine Mouse X23D". |
path |
The path to the spectrum file, e.g. "/example_datasets/bruker/urine/urine_1". |
type |
The type of experiment, e.g. "H1 CPMG" or "H1 NOESY". |
simpar |
The simulation parameters used to generate the spectrum. |
mfs |
The magnetic field strength in Tesla. |
A spectrum object as described in Metabodecon Classes.
2024-2025 Tobias Schmidt: initial version.
si <- c(1, 1, 3, 7, 8, 3, 8, 5, 2, 1) cs_max <- 14.8 cs_width <- 20.0 fq_ref <- 600.25 * 1e6 fq_width <- 12005 spectrum <- make_spectrum(si, cs_max, cs_width, fq_ref, fq_width) spectrum2 <- make_spectrum(si, cs_max, cs_width, fq_ref, fq_width = 12010, force = FALSE)si <- c(1, 1, 3, 7, 8, 3, 8, 5, 2, 1) cs_max <- 14.8 cs_width <- 20.0 fq_ref <- 600.25 * 1e6 fq_width <- 12005 spectrum <- make_spectrum(si, cs_max, cs_width, fq_ref, fq_width) spectrum2 <- make_spectrum(si, cs_max, cs_width, fq_ref, fq_width = 12010, force = FALSE)
WARNING: These functions are experimental and must not be used in production. Their API is very likely to change in non-backwards-compatible ways over the next few weeks.
Utilities for fitting, tuning and benchmarking 'metabodecon models' (mdm).
A mdm is a essentially a glmnet lasso model, fitted on a feature matrix X,
obtained by deconvoluting and aligning spectra and snapping their peaks to a
shared reference spectrum. Lambda is chosen via cross-validation on X.
Deconvolution parameters (npmax or nfit/smit/smws/delta),
alignment parameter maxShift, and peak-combining parameters
(maxCombine, combineMethod) are tunable hyperparameters.
When npmax > 0, deconvolution runs an internal grid search over smoothing
and fitting parameters and keeps at most npmax peaks. The explicit
nfit/smit/smws/delta values are ignored in that case. When
npmax == 0, the explicit parameters are used directly.
fit_mdm() deconvolutes spectra, aligns detected peaks,
combines them via get_si_mat() and fits one lasso model via
glmnet::cv.glmnet(). Lambda is selected internally by cross-validation
but no performance metrics are reported. Use cv_mdm() to
tune preprocessing parameters and benchmark_mdm() for
unbiased performance estimates.
cv_mdm() evaluates a grid of preprocessing parameter
combinations. For each grid row it builds a feature matrix, runs
glmnet::cv.glmnet() with a fixed fold assignment, and records the
held-out accuracy and AUC at lambda.min. Returns the model with the best
AUC and the full performance grid.
benchmark_mdm() wraps cv_mdm() in an
outer cross-validation loop to estimate end-to-end predictive performance.
It returns the per-fold models and held-out predictions.
fit_mdm( spectra, y, sfr = NULL, use_rust = TRUE, nworkers = 1, verbosity = 2, seed = 1, cadir = decon_cachedir(), check = TRUE, npmax = 1000, nfit = 5, smit = 2, smws = 5, delta = 6.4, maxShift = 200, maxCombine = 50, combineMethod = 2, nfolds = 10 ) cv_mdm( spectra, y, sfr = NULL, use_rust = TRUE, nworkers = 1, verbosity = 2, seed = 1, cadir = decon_cachedir(), check = TRUE, combineMethod = 2, pgrid = get_pgrid("dynamic2"), ignore = NULL, warm_cache = TRUE, nfolds = 10 ) benchmark_mdm( spectra, y, sfr = NULL, use_rust = TRUE, verbosity = 2, seed = 1, cadir = decon_cachedir(), nwo = 1, nwi = half_cores(), combineMethod = 2, pgrid = get_pgrid(), nfo = 5, nfl = 10 ) get_pgrid(conf = "dynamic2")fit_mdm( spectra, y, sfr = NULL, use_rust = TRUE, nworkers = 1, verbosity = 2, seed = 1, cadir = decon_cachedir(), check = TRUE, npmax = 1000, nfit = 5, smit = 2, smws = 5, delta = 6.4, maxShift = 200, maxCombine = 50, combineMethod = 2, nfolds = 10 ) cv_mdm( spectra, y, sfr = NULL, use_rust = TRUE, nworkers = 1, verbosity = 2, seed = 1, cadir = decon_cachedir(), check = TRUE, combineMethod = 2, pgrid = get_pgrid("dynamic2"), ignore = NULL, warm_cache = TRUE, nfolds = 10 ) benchmark_mdm( spectra, y, sfr = NULL, use_rust = TRUE, verbosity = 2, seed = 1, cadir = decon_cachedir(), nwo = 1, nwi = half_cores(), combineMethod = 2, pgrid = get_pgrid(), nfo = 5, nfl = 10 ) get_pgrid(conf = "dynamic2")
spectra |
List-like spectra object with |
y |
Factor vector with class labels for each spectrum. |
sfr |
Signal free region. See |
use_rust |
Logical. Whether to use the Rust backend. |
nworkers |
Number of workers used by |
verbosity |
Integer. Verbosity level; each nested call
decrements by 1. Messages print when |
seed |
Random seed used for fold assignment in |
cadir |
Directory used to cache deconvolution results across grid points and
processes. Defaults to |
check |
Logical. Whether to validate inputs at function entry. |
npmax |
Maximum number of peaks to retain. When |
nfit |
Number of Lorentz-curve fitting iterations (used when |
smit |
Number of smoothing iterations (used when |
smws |
Smoothing window size (used when |
delta |
Peak-filter threshold (used when |
maxShift |
Maximum alignment shift. |
maxCombine |
Maximum peak-combining distance in datapoints, passed to
|
combineMethod |
Peak-combining backend passed to
|
nfolds |
Number of folds for the |
pgrid |
Data frame of preprocessing parameter combinations as returned
by |
ignore |
Optional integer vector of sample indices to exclude. |
warm_cache |
Logical. Whether to pre-populate the disk cache before starting the grid search. |
nwo |
Number of workers for the outer cross-validation in
|
nwi |
Number of workers used inside each outer fold for
|
nfo |
Number of outer folds in |
nfl |
Number of folds for the |
conf |
Character string selecting a predefined parameter grid configuration. |
cv_mdm() and benchmark_mdm() can share
deconvolution results through directory cadir. This on-disk cache lets
parallel workers reuse expensive preprocessing results while keeping RAM use
manageable. Disk caching can be disabled by setting cadir = NULL, but this will increase runtime drastically (from a few minutes/hours
to several days).
fit_mdm() caches the most recent deconvolution and alignment
results in RAM when the input spectra carries a "hash" attribute. This
avoids redundant preprocessing when cv_mdm() evaluates
several settings on the same spectra.
Each outer worker in benchmark_mdm() keeps its own copy of
spectra and y, so memory use grows roughly with nwo. Large nwo values
can therefore require substantial RAM for large input datasets.
Inner workers (nwi) are used inside each outer worker for deconvolution,
alignment and prediction on held-out spectra. They process only the spectra
needed for the current task, so their memory impact is much smaller. In
RAM-constrained settings, use nwo = 1 and increase nwi instead.
The total amount of processes spawned is nwo * nwi.
fit_mdm() returns an object of class mdm with elements
model (a glmnet::cv.glmnet() object), ref (the reference alignment
spectrum) and meta (list of preprocessing parameters).
cv_mdm() returns an mdm object with an additional element
pgrid containing the performance grid.
benchmark_mdm() returns a list with elements:
models: List of mdm objects, one per outer fold.
predictions: Data frame with columns fold, true, link, prob,
pred.
## Not run: # -~-~-~ Inputs -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- aki <- read_aki_data() spectra <- aki$spectra attr(spectra, "hash") <- rlang::hash(spectra) y <- factor(aki$meta$type, levels = c("Control", "AKI")) names(y) <- rownames(aki$meta) sfr <- c(11, -2) cadir <- cachedir("deconvs", persistent = TRUE) # -~-~-~-~-~-~-~-~-~ Single -~-~-~-~-~-~-~-~-~ # Best "simple" model from aki.R (0.797 [0.75 - 0.83]) mdm <- fit_mdm( spectra, y, sfr=NULL, nfit=5, smit=3, smws=3, delta=3, npmax=0, maxShift=128, maxCombine=256, combineMethod=1, nworkers=half_cores(), cadir=cadir ) mdm <- fit_mdm( spectra, y, sfr = NULL, nfit=5, smit=0, smws=0, delta=0, npmax=1000, maxShift=64, maxCombine=16, combineMethod=1, nworkers=half_cores(), cadir=cadir ) # -~-~-~-~-~-~-~-~-~ Search -~-~-~-~-~-~-~-~-~ mdm_grid_stat1 <- cv_mdm( spectra, y, pgrid=get_pgrid("static1"), nworkers=half_cores(), use_rust=TRUE, cadir=cadir ) saveRDS(mdm_grid_stat1, "tmp/mdm_grid_stat1.rds") mdm_grid_stat2 <- cv_mdm( spectra, y, pgrid=get_pgrid("static2"), nworkers=half_cores(), use_rust=TRUE, cadir=cadir ) saveRDS(mdm_grid_stat2, "tmp/mdm_grid_stat2.rds") # # Best static2: acc=0.82, auc=0.89 # smit=2, smws=3, delta=6, nfit=7, npmax=0, maxShift=100, maxCombine=30 mdm_grid_stat3 <- cv_mdm( spectra, y, pgrid=get_pgrid("static3"), nworkers=half_cores(), use_rust=TRUE, cadir=cadir ) saveRDS(mdm_grid_stat3, "tmp/mdm_grid_stat3.rds") # # Best static3: acc=79.34% auc=0.8589 # smit=2, smws=7, delta=8, nfit=10, npmax=0, maxShift=100, maxCombine=30 mdm_grid_dyn2 <- cv_mdm( spectra, y, pgrid=get_pgrid("dynamic2"), nworkers=half_cores(), use_rust=TRUE, cadir=cadir ) # -~-~-~ Full Benchmark -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- bm <- benchmark_mdm(spectra, y, sfr, cadir=cadir) # -~-~-~ Interactive Development -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ stub(fit_mdm, spectra=spectra, y=y, sfr=sfr) stub(cv_mdm, spectra=spectra, y=y, sfr=sfr) stub(benchmark_mdm, spectra=spectra, y=y, sfr=sfr) ## End(Not run)## Not run: # -~-~-~ Inputs -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- aki <- read_aki_data() spectra <- aki$spectra attr(spectra, "hash") <- rlang::hash(spectra) y <- factor(aki$meta$type, levels = c("Control", "AKI")) names(y) <- rownames(aki$meta) sfr <- c(11, -2) cadir <- cachedir("deconvs", persistent = TRUE) # -~-~-~-~-~-~-~-~-~ Single -~-~-~-~-~-~-~-~-~ # Best "simple" model from aki.R (0.797 [0.75 - 0.83]) mdm <- fit_mdm( spectra, y, sfr=NULL, nfit=5, smit=3, smws=3, delta=3, npmax=0, maxShift=128, maxCombine=256, combineMethod=1, nworkers=half_cores(), cadir=cadir ) mdm <- fit_mdm( spectra, y, sfr = NULL, nfit=5, smit=0, smws=0, delta=0, npmax=1000, maxShift=64, maxCombine=16, combineMethod=1, nworkers=half_cores(), cadir=cadir ) # -~-~-~-~-~-~-~-~-~ Search -~-~-~-~-~-~-~-~-~ mdm_grid_stat1 <- cv_mdm( spectra, y, pgrid=get_pgrid("static1"), nworkers=half_cores(), use_rust=TRUE, cadir=cadir ) saveRDS(mdm_grid_stat1, "tmp/mdm_grid_stat1.rds") mdm_grid_stat2 <- cv_mdm( spectra, y, pgrid=get_pgrid("static2"), nworkers=half_cores(), use_rust=TRUE, cadir=cadir ) saveRDS(mdm_grid_stat2, "tmp/mdm_grid_stat2.rds") # # Best static2: acc=0.82, auc=0.89 # smit=2, smws=3, delta=6, nfit=7, npmax=0, maxShift=100, maxCombine=30 mdm_grid_stat3 <- cv_mdm( spectra, y, pgrid=get_pgrid("static3"), nworkers=half_cores(), use_rust=TRUE, cadir=cadir ) saveRDS(mdm_grid_stat3, "tmp/mdm_grid_stat3.rds") # # Best static3: acc=79.34% auc=0.8589 # smit=2, smws=7, delta=8, nfit=10, npmax=0, maxShift=100, maxCombine=30 mdm_grid_dyn2 <- cv_mdm( spectra, y, pgrid=get_pgrid("dynamic2"), nworkers=half_cores(), use_rust=TRUE, cadir=cadir ) # -~-~-~ Full Benchmark -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~- bm <- benchmark_mdm(spectra, y, sfr, cadir=cadir) # -~-~-~ Interactive Development -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ stub(fit_mdm, spectra=spectra, y=y, sfr=sfr) stub(cv_mdm, spectra=spectra, y=y, sfr=sfr) stub(benchmark_mdm, spectra=spectra, y=y, sfr=sfr) ## End(Not run)
WARNING: These methods are experimental and must not be used in production. Their API is very likely to change in non-backwards-compatible ways over the next few weeks.
S3 methods for objects of class mdm and summary.mdm.
predict.mdm() predicts probabilities, classes, link scores, or all
three from an mdm object. When newdata is a spectra object, the
spectra are deconvoluted, aligned and snapped to the reference stored in
the model before prediction. When newdata is a numeric matrix, it is
used directly as the feature matrix.
print.mdm() prints a compact model summary.
coef.mdm() returns lasso coefficients.
plot.mdm() plots the lasso path.
summary.mdm() builds a compact summary list.
print.summary.mdm() prints formatted output for summary.mdm objects.
## S3 method for class 'mdm' predict( object, newdata, type = c("all", "prob", "class", "link"), s = "lambda.min", nworkers = 1, verbosity = 1, ... ) ## S3 method for class 'mdm' print(x, ...) ## S3 method for class 'mdm' coef(object, ...) ## S3 method for class 'mdm' plot(x, ...) ## S3 method for class 'mdm' summary(object, ...) ## S3 method for class 'summary.mdm' print(x, ...)## S3 method for class 'mdm' predict( object, newdata, type = c("all", "prob", "class", "link"), s = "lambda.min", nworkers = 1, verbosity = 1, ... ) ## S3 method for class 'mdm' print(x, ...) ## S3 method for class 'mdm' coef(object, ...) ## S3 method for class 'mdm' plot(x, ...) ## S3 method for class 'mdm' summary(object, ...) ## S3 method for class 'summary.mdm' print(x, ...)
object, x
|
A fitted |
newdata |
Spectra object or numeric feature matrix. |
type |
Prediction type, one of |
s |
Regularization value for lasso predictions. |
nworkers |
Number of workers to deconvolute and align |
verbosity |
Integer verbosity level. |
... |
Passed to underlying methods where applicable. |
predict: numeric vector of probabilities, classes, and/or link scores.
print: invisibly returns x.
coef: coefficient object from glmnet.
plot: invisibly returns NULL.
summary: object of class summary.mdm.
print.summary.mdm: invisibly returns x.
m <- structure( list( model = NULL, ref = NULL, meta = list(npmax = 1000, nfit = 3, smit = 2, smws = 5, delta = 6.4, maxShift = 100, maxCombine = 50, combineMethod = 2) ), class = "mdm" ) print(m) summary(m) ## Not run: m <- cv_mdm(spectra, y, sfr = c(11, -2)) predict(m, test_spectra, type = "prob") coef(m) plot(m) ## End(Not run)m <- structure( list( model = NULL, ref = NULL, meta = list(npmax = 1000, nfit = 3, smit = 2, smws = 5, delta = 6.4, maxShift = 100, maxCombine = 50, combineMethod = 2) ), class = "mdm" ) print(m) summary(m) ## Not run: m <- cv_mdm(spectra, y, sfr = c(11, -2)) predict(m, test_spectra, type = "prob") coef(m) plot(m) ## End(Not run)
Recursively searches for files or directories within the 'metabodecon' package that match the given name.
metabodecon_file(name = "sim_01")metabodecon_file(name = "sim_01")
name |
The name to search for. |
The file or directory path.
2024-2025 Tobias Schmidt: initial version.
# Unambiguous paths metabodecon_file("urine_1") metabodecon_file("urine_1.dx") metabodecon_file("sim/sim_01") # Ambiguous paths (i.e. multiple matches) metabodecon_file("sim") metabodecon_file("urine") # Non-existing paths (i.e. a character vector of length zero gets returned) metabodecon_file("asdfasdf")# Unambiguous paths metabodecon_file("urine_1") metabodecon_file("urine_1.dx") metabodecon_file("sim/sim_01") # Ambiguous paths (i.e. multiple matches) metabodecon_file("sim") metabodecon_file("urine") # Non-existing paths (i.e. a character vector of length zero gets returned) metabodecon_file("asdfasdf")
Automatic deconvolution of a 1D NMR spectrum into several Lorentz curves and the integration of them. The NMR file needs to be in Bruker format or jcamp-dx format.
This function has been deprecated with metabodecon version v1.2.0 and will be
removed with version 2.0.0. Please use deconvolute() instead.
MetaboDecon1D( filepath, filename = NA, file_format = "bruker", number_iterations = 10, range_water_signal_ppm = 0.1527692, signal_free_region = c(11.44494, -1.8828), smoothing_param = c(2, 5), delta = 6.4, scale_factor = c(1000, 1e+06), debug = FALSE, store_results = NULL )MetaboDecon1D( filepath, filename = NA, file_format = "bruker", number_iterations = 10, range_water_signal_ppm = 0.1527692, signal_free_region = c(11.44494, -1.8828), smoothing_param = c(2, 5), delta = 6.4, scale_factor = c(1000, 1e+06), debug = FALSE, store_results = NULL )
filepath |
Complete path of the file folder (Notice for Bruker format: filepath needs to be the spectrum folder containing one or more different spectra (e.g."C:/Users/Username/Desktop/spectra_from_bruker")) |
filename |
Name of the NMR file. (Notice for Bruker format: filename need to be the name of your spectrum which is also the name of the folder) (Default: filename = NA to analyze more spectra at once) |
file_format |
Format (bruker or jcampdx) of the NMR file. (Default: file_format = "bruker") |
number_iterations |
Number of iterations for the approximation of the parameters for the Lorentz curves (Default: number_iterations=10) |
range_water_signal_ppm |
Half width of the water artefact in ppm (Default: range_water_signal=0.1527692 (e.g. for urine NMR spectra)) |
signal_free_region |
Row vector with two entries consisting of the ppm positions for the left and right border of the signal free region of the spectrum. (Default: signal_free_region=c(11.44494, -1.8828)) |
smoothing_param |
Row vector with two entries consisting of the number of smoothing repeats for the whole spectrum and the number of data points (uneven) for the mean calculation (Default: smoothing_param=c(2,5)) |
delta |
Defines the threshold value to distinguish between signal and noise (Default: delta=6.4) |
scale_factor |
Row vector with two entries consisting of the factor to scale the x-axis and the factor to scale the y-axis (Default: scale_factor=c(1000,1000000)) |
debug |
Logical value to activate the debug mode (Default: debug=FALSE) |
store_results |
Specifies whether the lorentz curve parameters |
A decon0 object as described in Metabodecon Classes.
2020-2021 Martina Haeckl: initial version.
2024-2025 Tobias Schmidt: Minor updates to pass CRAN checks. Parameters
debug and store_results added.
Haeckl, M.; Tauber, P.; Schweda, F.; Zacharias, H.U.; Altenbuchinger, M.; Oefner, P.J.; Gronwald, W. An R-Package for the Deconvolution and Integration of 1D NMR Data: MetaboDecon1D. Metabolites 2021, 11, 452. https://doi.org/10.3390/metabo11070452
calculate_lorentz_curves(),
plot_triplets(),
plot_lorentz_curves_save_as_png(),
plot_spectrum_superposition_save_as_png()
## ATTENTION: using MetaboDecon1D() for deconvolution is deprecated. Please use ## deconvolute() instead. ## The following example shows how a subset of the Sim dataset, consisting ## of two spectrum objects, can be deconvoluted using `MetaboDecon1D()`. The ## whole example code is wrapped into `evalwith()` to simulate user input. ## When using the function interactively, you should type in the answers to ## the questions manually. expected_answers <- c( "10", # Subfolder of your filepath, i.e. the experiment number? "10", # Subsubsubfolder of filepath, i.e. the processing number? "y", # Use same parameters for all spectra? "1", # File to adjust all parameters. "n", # Signal free region borders correct selected? "3.55", # Left border. "3.35", # Right border. "y", # Signal free region borders correct selected? "n", # Water artefact fully inside red vertical lines? "0", # Half width range (in ppm) for the water artefact. "y", # Water artefact fully inside red vertical lines? "n" # Save results as text documents? ) sim <- metabodecon_file("bruker/sim_subset") evalwith(answers = expected_answers, { sim_decon <- MetaboDecon1D(sim) }) ## Deconvolute only the first spectrum of the folder "bruker/sim_subset" into evalwith(answers = expected_answers[-(3:4)], { sim_decon <- MetaboDecon1D(sim, filename = "sim_01") })## ATTENTION: using MetaboDecon1D() for deconvolution is deprecated. Please use ## deconvolute() instead. ## The following example shows how a subset of the Sim dataset, consisting ## of two spectrum objects, can be deconvoluted using `MetaboDecon1D()`. The ## whole example code is wrapped into `evalwith()` to simulate user input. ## When using the function interactively, you should type in the answers to ## the questions manually. expected_answers <- c( "10", # Subfolder of your filepath, i.e. the experiment number? "10", # Subsubsubfolder of filepath, i.e. the processing number? "y", # Use same parameters for all spectra? "1", # File to adjust all parameters. "n", # Signal free region borders correct selected? "3.55", # Left border. "3.35", # Right border. "y", # Signal free region borders correct selected? "n", # Water artefact fully inside red vertical lines? "0", # Half width range (in ppm) for the water artefact. "y", # Water artefact fully inside red vertical lines? "n" # Save results as text documents? ) sim <- metabodecon_file("bruker/sim_subset") evalwith(answers = expected_answers, { sim_decon <- MetaboDecon1D(sim) }) ## Deconvolute only the first spectrum of the folder "bruker/sim_subset" into evalwith(answers = expected_answers[-(3:4)], { sim_decon <- MetaboDecon1D(sim, filename = "sim_01") })
Plots the original spectrum and all generated Lorentz curves and save the result as png under the filepath.
Superseded by plot_spectrum() since metabodecon v1.2.0. Will be replaced
with metabodecon v2.
plot_lorentz_curves_save_as_png( deconv_result, x_range = c(), y_range = c(), out_dir = ".", ask = TRUE )plot_lorentz_curves_save_as_png( deconv_result, x_range = c(), y_range = c(), out_dir = ".", ask = TRUE )
deconv_result |
Saved result of the MetaboDecon1D() function |
x_range |
Row vector with two entries consisting of the ppm start and the ppm end value to scale the range of the x-axis (optional) |
y_range |
Row vector with two entries consisting of the ppm start and the ppm end value to scale the range of the y-axis (optional) |
out_dir |
Path to the directory where the png files should be saved. Default is the current working directory. |
ask |
Logical value. Whether to ask for confirmation from the user before writing files to disk. Default is TRUE. |
NULL, called for side effects.
2020-2021 Martina Haeckl: initial version.
2024-2025 Tobias Schmidt: Minor updates to pass CRAN checks
MetaboDecon1D(), plot_triplets(), plot_spectrum_superposition_save_as_png()
sim <- metabodecon_file("bruker/sim_subset") sim_decon <- generate_lorentz_curves_sim(sim) png_dir <- tmpdir("sim_decon/pngs", create = TRUE) plot_lorentz_curves_save_as_png(sim_decon, out_dir = png_dir, ask = FALSE) dir(png_dir, full.names = TRUE)sim <- metabodecon_file("bruker/sim_subset") sim_decon <- generate_lorentz_curves_sim(sim) png_dir <- tmpdir("sim_decon/pngs", create = TRUE) plot_lorentz_curves_save_as_png(sim_decon, out_dir = png_dir, ask = FALSE) dir(png_dir, full.names = TRUE)
Plot a set of deconvoluted spectra.
plot_spectra( obj, ..., foc_rgn = NULL, what = c("si", "sup", "supal"), sfy = 1e+06, cols = NULL, names = NULL, xlab = "Chemical Shift [ppm]", ylab = paste("Signal Intensity [au] /", sfy), mar = c(4.1, 4.1, 1.1, 0.1), lgd = list() )plot_spectra( obj, ..., foc_rgn = NULL, what = c("si", "sup", "supal"), sfy = 1e+06, cols = NULL, names = NULL, xlab = "Chemical Shift [ppm]", ylab = paste("Signal Intensity [au] /", sfy), mar = c(4.1, 4.1, 1.1, 0.1), lgd = list() )
obj |
An object of type |
... |
Additional arguments passed to the conversion function. |
foc_rgn |
Numeric vector of length 2 specifying the focus region in ppm
(e.g. |
what |
Which signal to plot: |
sfy |
Scaling factor for the y-axis. |
cols |
Character vector of colors, one per spectrum. Defaults to |
names |
Character vector of legend labels. Defaults to spectrum names. |
xlab |
Label for the x-axis. |
ylab |
Label for the y-axis. |
mar |
A numeric vector of length 4, which specifies the margins of the plot. |
lgd |
Logical or list. If TRUE, a legend is drawn at "topright" with
|
A plot of the deconvoluted spectra.
2024-2025 Tobias Schmidt: initial version.
plot_spectrum() for a much more sophisticated plotting routine
suitable for plotting a single spectrum.
obj <- deconvolute(sim[1:4], sfr = c(3.55, 3.35)) plot_spectra(obj)obj <- deconvolute(sim[1:4], sfr = c(3.55, 3.35)) plot_spectra(obj)
Plot a spectrum and zoom in on a specific region.
plot_spectrum( x, ..., obj = as_v12_singlet(x), foc_frac = get_foc_frac(obj), foc_rgn = get_foc_rgn(obj, foc_frac), sub1 = TRUE, sub2 = FALSE, sub3 = width(foc_rgn) < width(obj$cs), mar = NULL, frame = FALSE, con_lines = TRUE )plot_spectrum( x, ..., obj = as_v12_singlet(x), foc_frac = get_foc_frac(obj), foc_rgn = get_foc_rgn(obj, foc_frac), sub1 = TRUE, sub2 = FALSE, sub3 = width(foc_rgn) < width(obj$cs), mar = NULL, frame = FALSE, con_lines = TRUE )
x |
An object of type |
... |
Additional arguments passed to |
obj |
An object of type |
foc_frac |
A numeric vector specifying the start and end of the focus region as fraction
of the full spectrum width. Only used if |
foc_rgn |
A numeric vector specifying the start and end of the focus region in ppm. If
set to NULL, |
sub1, sub2, sub3
|
List of arguments passed to |
mar |
A numeric vector of length 4 specifying the margins of the plot. Passed to
|
frame |
A list of values passed to |
con_lines |
A list of values passed to |
This function first initializes a new plotting canvas. After that it calls
draw_spectrum() multiple times to draw the following sub figures onto the
plotting canvas:
The signal intensities in the focus region
The second derivative in the focus region
The signal intensities over all datapoints
The argument lists for the individual calls to draw_spectrum() are
determined at runtime and depend on the arguments passed to plot_spectrum()
as well as the currently active graphics device. To customize the appearance
of the individual sub plots, you can overwrite each value passed to
draw_spectrum() by providing a corresponding named element in sub1,
sub2 or sub3.
A sketch of the resulting figure is shown below.
__________________________________________ | ______________1_____________ | | | Sub1: Signal Intensity in | | | | Focus Region | | | | /\ | | | | / \ | | | | / \ /\ | | | 11| / \/ \ |7 | | | /\ / \ | | | | / \/ \ | | | | / \ | | | |__/___________0__________\__| | | | Sub2: Second Derivative | | | 11| in Focus Region |7 | | |____________________________| | | 3 | | __________________3_________________ | | | Sub3: Signal Intensity over all | | | | Datapoints ________________ | | | 5 | | Focus Rectangle| |1| | | /\ | /\ | | | | | / \ | / \/\ | | | | | / \ /\ | /\/ \ | | | | |__/______\_/__\_|__/__________\__|__| | |______________________5___________________|
Note that the figure created by plot_spectrum() can be part of a
multi-figure configuration as created when setting mfrow or mfcol via
par(). Example:
_______________________________________ | Plot Spectrum with | Other Figure | | sub3 = TRUE | Other Figure | | ___________ | ___________ | | | Sub Fig 1 | | | x x | | | |___________| | | x | | | |_Sub_Fig_2_| | | x | | | _________________ | | x x | | | | Sub Fig 3 | | | x | | | |_________________| | |___________| | |______________________|_______________| | Some other Figure | Plot Spectrum | | | sub3 = FALSE | | _________________ | ___________ | | | ___ | | | Sub Fig 1 | | | | ___/ \___ | | | | | | |/ \____| | |___________| | | | | | | Sub Fig 2 | | | |_________________| | |___________| | |______________________|_______________|
NULL. Called for side effect of plotting as sketched in 'Details'.
2024-2025 Tobias Schmidt: initial version.
## 1. Prepare a deconvoluted spectrum as input spec <- read_spectrum(metabodecon_file("sim/sim_01")) decon <- generate_lorentz_curves_sim(spec) ## 2.1. Plot the full (non-deconvoluted) spectrum ## 2.2. Remove connecting lines, and focus on a specific region specified in ppm ## 2.3. Show second derivative and focus on a specific region specified as fraction ## 2.4. Change color of focus rectangle and margins of sub figure 1 ## 2.5. Hide xlab and show second derivative ## 2.6. Change the figure region for sub figure 1 plot_spectrum(spec, sub1 = FALSE) plot_spectrum(decon, foc_rgn = c(3.49, 3.45), con_lines = FALSE) plot_spectrum(decon, sub2 = TRUE, foc_frac = c(0.40, 0.30)) plot_spectrum(decon, sub1 = list(mar = c(3, 6, 3, 6), lt_axis = list(col = "violet")), foc_rect = list(border = "violet", col = transp("violet")), con_lines = list(col = "violet") ) plot_spectrum(decon, sub2 = TRUE, sub3 = list(bt_text = list(text = "")), frame = TRUE, con_lines = FALSE ) plot_spectrum(decon, sub1 = list(fig_rgn_npc = c(0,1,.3,1), mar = c(0,5,0,0)))## 1. Prepare a deconvoluted spectrum as input spec <- read_spectrum(metabodecon_file("sim/sim_01")) decon <- generate_lorentz_curves_sim(spec) ## 2.1. Plot the full (non-deconvoluted) spectrum ## 2.2. Remove connecting lines, and focus on a specific region specified in ppm ## 2.3. Show second derivative and focus on a specific region specified as fraction ## 2.4. Change color of focus rectangle and margins of sub figure 1 ## 2.5. Hide xlab and show second derivative ## 2.6. Change the figure region for sub figure 1 plot_spectrum(spec, sub1 = FALSE) plot_spectrum(decon, foc_rgn = c(3.49, 3.45), con_lines = FALSE) plot_spectrum(decon, sub2 = TRUE, foc_frac = c(0.40, 0.30)) plot_spectrum(decon, sub1 = list(mar = c(3, 6, 3, 6), lt_axis = list(col = "violet")), foc_rect = list(border = "violet", col = transp("violet")), con_lines = list(col = "violet") ) plot_spectrum(decon, sub2 = TRUE, sub3 = list(bt_text = list(text = "")), frame = TRUE, con_lines = FALSE ) plot_spectrum(decon, sub1 = list(fig_rgn_npc = c(0,1,.3,1), mar = c(0,5,0,0)))
Plots the original spectrum and the superposition of all generated Lorentz curves and saves the result as png under the specified filepath.
Superseded by plot_spectrum() since metabodecon v1.2.0. Will be replaced with v2.
plot_spectrum_superposition_save_as_png( deconv_result, x_range = c(), y_range = c(), out_dir = ".", ask = TRUE )plot_spectrum_superposition_save_as_png( deconv_result, x_range = c(), y_range = c(), out_dir = ".", ask = TRUE )
deconv_result |
Saved result of the MetaboDecon1D() function |
x_range |
Row vector with two entries consisting of the ppm start and the ppm end value to scale the range of the x-axis (optional) |
y_range |
Row vector with two entries consisting of the ppm start and the ppm end value to scale the range of the y-axis (optional) |
out_dir |
Path to the directory where the png files should be saved. Default is the current working directory. |
ask |
Logical value. Whether to ask for confirmation from the user before writing files to disk. |
NULL, called for side effects.
2020-2021 Martina Haeckl: initial version.
MetaboDecon1D(),
calculate_lorentz_curves(),
plot_triplets(),
plot_lorentz_curves_save_as_png()
sim <- metabodecon_file("bruker/sim_subset") sim_decon <- generate_lorentz_curves_sim(sim) png_dir <- tmpdir("sim_decon/pngs", create = TRUE) plot_spectrum_superposition_save_as_png(sim_decon, out_dir = png_dir, ask = FALSE) dir(png_dir, full.names = TRUE)sim <- metabodecon_file("bruker/sim_subset") sim_decon <- generate_lorentz_curves_sim(sim) png_dir <- tmpdir("sim_decon/pngs", create = TRUE) plot_spectrum_superposition_save_as_png(sim_decon, out_dir = png_dir, ask = FALSE) dir(png_dir, full.names = TRUE)
Plots the peak triplets for each peak detected by MetaboDecon1D() and stores the plots as png at outdir.
Superseded by plot_spectrum() since metabodecon v1.2.0. Will be replaced with v2.
plot_triplets( deconv_result, x_range = c(), y_range = c(), out_dir = ".", ask = TRUE )plot_triplets( deconv_result, x_range = c(), y_range = c(), out_dir = ".", ask = TRUE )
deconv_result |
Saved result of the MetaboDecon1D() function |
x_range |
Row vector with two entries consisting of the ppm start and the ppm end value to scale the range of the x-axis (optional) |
y_range |
Row vector with two entries consisting of the ppm start and the ppm end value to scale the range of the y-axis (optional) |
out_dir |
Directory to save the png files (optional) |
ask |
Logical value to ask the user if the png files should be saved in the specified directory (optional) |
No return value, called for side effect of plotting.
2020-2021 Martina Haeckl: initial version.
2024-2025 Tobias Schmidt: Minor updates to pass CRAN checks.
MetaboDecon1D(), calculate_lorentz_curves(), plot_lorentz_curves_save_as_png(),
plot_spectrum_superposition_save_as_png()
sim <- metabodecon_file("bruker/sim_subset") sim_decon <- generate_lorentz_curves_sim(sim) png_dir <- tmpdir("sim_decon/pngs", create = TRUE) plot_triplets(sim_decon, out_dir = png_dir, ask = FALSE) dir(png_dir, full.names = TRUE)sim <- metabodecon_file("bruker/sim_subset") sim_decon <- generate_lorentz_curves_sim(sim) png_dir <- tmpdir("sim_decon/pngs", create = TRUE) plot_triplets(sim_decon, out_dir = png_dir, ask = FALSE) dir(png_dir, full.names = TRUE)
S3 Methods for printing metabodecon objects as described in the Metabodecon Classes.
## S3 method for class 'spectrum' print(x, name = FALSE, ...) ## S3 method for class 'decon1' print(x, name = FALSE, ...) ## S3 method for class 'decon2' print(x, name = FALSE, ...) ## S3 method for class 'align' print(x, name = FALSE, ...) ## S3 method for class 'spectra' print(x, ...) ## S3 method for class 'decons1' print(x, ...) ## S3 method for class 'decons2' print(x, ...) ## S3 method for class 'aligns' print(x, ...)## S3 method for class 'spectrum' print(x, name = FALSE, ...) ## S3 method for class 'decon1' print(x, name = FALSE, ...) ## S3 method for class 'decon2' print(x, name = FALSE, ...) ## S3 method for class 'align' print(x, name = FALSE, ...) ## S3 method for class 'spectra' print(x, ...) ## S3 method for class 'decons1' print(x, ...) ## S3 method for class 'decons2' print(x, ...) ## S3 method for class 'aligns' print(x, ...)
x |
The object to print. |
name |
Logical. If TRUE, the name of the object is printed before the object. |
... |
Not used. Only accepted to comply with generic |
NULL, called for side effect of printing to the standard output device.
2024-2025 Tobias Schmidt: initial version.
print(sim[[1]]) print(sim[[1]], name = TRUE) print(sim) decon <- deconvolute(sim[[1]], sfr = c(3.55, 3.35)) print(decon)print(sim[[1]]) print(sim[[1]], name = TRUE) print(sim) decon <- deconvolute(sim[[1]], sfr = c(3.55, 3.35)) print(decon)
read_spectrum() reads a single spectrum from disk and returns it as
spectrum object. read_spectra() can be used to read multiple spectra at
once and returns a spectra object.
read_spectra( data_path = pkg_file("example_datasets/bruker/urine"), file_format = "bruker", expno = 10, procno = 10, raw = FALSE, silent = TRUE, force = FALSE )read_spectra( data_path = pkg_file("example_datasets/bruker/urine"), file_format = "bruker", expno = 10, procno = 10, raw = FALSE, silent = TRUE, force = FALSE )
data_path |
The path of the file/folder containing the spectrum data. E.g.
|
file_format |
The file_format of the spectrum file. E.g. |
expno, procno
|
The experiment/processing number for the file. E.g. |
raw |
If |
silent |
If |
force |
If |
A spectrum object as described in Metabodecon Classes.
2024-2025 Tobias Schmidt: initial version.
relpath <- "example_datasets/bruker/urine" urine <- system.file(relpath, package = "metabodecon") urine_1 <- file.path(urine, "urine_1") urine_2 <- file.path(urine, "urine_2") x1 <- read_spectrum(urine_1) x2 <- read_spectrum(urine_2) xx <- read_spectra(urine) str(xx) str(x1) stopifnot(all.equal(x1, xx$urine_1))relpath <- "example_datasets/bruker/urine" urine <- system.file(relpath, package = "metabodecon") urine_1 <- file.path(urine, "urine_1") urine_2 <- file.path(urine, "urine_2") x1 <- read_spectrum(urine_1) x2 <- read_spectrum(urine_2) xx <- read_spectra(urine) str(xx) str(x1) stopifnot(all.equal(x1, xx$urine_1))
read_spectrum() reads a single spectrum from disk and returns it as
spectrum object. read_spectra() can be used to read multiple spectra at
once and returns a spectra object.
read_spectrum( data_path = metabodecon_file("bruker/sim/sim_01"), file_format = "bruker", expno = 10, procno = 10, raw = FALSE, silent = TRUE, force = FALSE )read_spectrum( data_path = metabodecon_file("bruker/sim/sim_01"), file_format = "bruker", expno = 10, procno = 10, raw = FALSE, silent = TRUE, force = FALSE )
data_path |
The path of the file/folder containing the spectrum data. E.g.
|
file_format |
The file_format of the spectrum file. E.g. |
expno, procno
|
The experiment/processing number for the file. E.g. |
raw |
If |
silent |
If |
force |
If |
A spectrum object as described in Metabodecon Classes.
2024-2025 Tobias Schmidt: initial version.
relpath <- "example_datasets/bruker/urine" urine <- system.file(relpath, package = "metabodecon") urine_1 <- file.path(urine, "urine_1") urine_2 <- file.path(urine, "urine_2") x1 <- read_spectrum(urine_1) x2 <- read_spectrum(urine_2) xx <- read_spectra(urine) str(xx) str(x1) stopifnot(all.equal(x1, xx$urine_1))relpath <- "example_datasets/bruker/urine" urine <- system.file(relpath, package = "metabodecon") urine_1 <- file.path(urine, "urine_1") urine_2 <- file.path(urine, "urine_2") x1 <- read_spectrum(urine_1) x2 <- read_spectrum(urine_2) xx <- read_spectra(urine) str(xx) str(x1) stopifnot(all.equal(x1, xx$urine_1))
The SAP Dataset consists of a single 'Simple-As-Possible' (SAP) spectrum. The purpose of the SAP spectrum is to provide a straightforward example that can be used to test and understand the deconvolution algorithm in detail.
sapsap
An object of class spectra of length 1.
The first (and only) spectrum within the SAP dataset contains 128 datapoints ranging from -6.4 to 6.4 ppm with four peaks. A rough sketch of the spectrum is shown below:
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
| SFR | w | SFR |
| | x www p | |
|~-~-~-~-~-~-~-~|~-|-|-~-~-~-~-~|~-~-~-~-|-~-~-~-~-~-~-~-~-~-~-~
| | | | | | |
6.4 | | 2.24 0.047 -2.22 -3.2
| 2.61
3.2
A simulated dataset generated from the Blood dataset.
simsim
A spectra object consisting of 16 spectrum objects, where each spectrum
contains 2048 datapoints ranging from 3.60 to 3.29 ppm. For details about
spectrum and spectra objects see Metabodecon Classes.
Simulates a 1D NMR spectrum based on the provided parameters.
simulate_spectrum( name = "sim_00", seed = sum(utf8ToInt(name)), ndp = 2048, npk = 10, csres = 0.00015, cs = seq(from = 3.6, length.out = ndp, by = -csres), pkr = quantile(cs, c(0.25, 0.75)), fqref = 600252806.95, x0 = sort(runif(npk, pkr[1], pkr[2])), A = runif(npk, 2.5, 20) * 1000, lambda = runif(npk, 0.9, 1.3)/1000, noise = rnorm(length(cs), sd = 1200) )simulate_spectrum( name = "sim_00", seed = sum(utf8ToInt(name)), ndp = 2048, npk = 10, csres = 0.00015, cs = seq(from = 3.6, length.out = ndp, by = -csres), pkr = quantile(cs, c(0.25, 0.75)), fqref = 600252806.95, x0 = sort(runif(npk, pkr[1], pkr[2])), A = runif(npk, 2.5, 20) * 1000, lambda = runif(npk, 0.9, 1.3)/1000, noise = rnorm(length(cs), sd = 1200) )
name |
The name of the spectrum. |
seed |
The seed for the random number generator. |
ndp |
The number of data points in the spectrum. |
npk |
The number of peaks in the spectrum. |
csres |
The chemical shift resolution in PPM. |
cs |
The vector of chemical shifts in PPM. |
pkr |
The start and stop of the peak region in PPM. |
fqref |
The reference frequency in Hz. |
x0 |
The peak center positions in PPM. |
A |
The peak area parameter. |
lambda |
The peak width parameter. |
noise |
The noise to add to the spectrum. |
A spectrum object as described in Metabodecon Classes.
2024-2025 Tobias Schmidt: initial version.
simA <- simulate_spectrum("simA") simA_copy <- simulate_spectrum("simA") simB <- simulate_spectrum("simB") simC <- simulate_spectrum("simC", npk = 20) plot_spectrum(simC) if (!identical(simA, simA_copy)) stop() if ( identical(simA, simB )) stop()simA <- simulate_spectrum("simA") simA_copy <- simulate_spectrum("simA") simB <- simulate_spectrum("simB") simC <- simulate_spectrum("simC", npk = 20) plot_spectrum(simC) if (!identical(simA, simA_copy)) stop() if ( identical(simA, simB )) stop()
Helper function of align(). Should not be called directly by the user.
Performs signal alignment across the individual spectra using the 'speaq' package (Beirnaert C, Meysman P, Vu TN, Hermans N, Apers S, Pieters L, et al. (2018) speaq 2.0: A complete workflow for high-throughput 1D NMRspectra processing and quantification. PLoS Comput Biol 14(3): e1006018. https://www.doi.org/10.1371/journal.pcbi.1006018). The spectra deconvolution process yields the signals of all spectra. Due to slight changes in measurement conditions, e.g. pH variations, signal positions may vary slightly across spectra. As a consequence, prior to further analysis signals belonging to the same compound have to be aligned across spectra. This is the purpose of the 'speaq' package.
Direct usage of this function has been deprecated with metabodecon version 1.4.3 and will be removed with metabodecon version 2.0.0.
speaq_align( feat = gen_feat_mat(spectrum_data), maxShift = 50, spectrum_data, si_size_real_spectrum = length(spectrum_data[[1]]$y_values), verbose = TRUE, show = FALSE, mfrow = c(2, 1) )speaq_align( feat = gen_feat_mat(spectrum_data), maxShift = 50, spectrum_data, si_size_real_spectrum = length(spectrum_data[[1]]$y_values), verbose = TRUE, show = FALSE, mfrow = c(2, 1) )
feat |
Output of |
maxShift |
Maximum number of points along the "ppm-axis" which a value can be moved by speaq package e.g. 50. 50 is a suitable starting value for plasma spectra with a digital resolution of 128K. Note that this parameter has to be individually optimized depending on the type of analyzed spectra and the digital resolution. For urine which is more prone to chemical shift variations this value most probably has to be increased. |
spectrum_data |
Output of |
si_size_real_spectrum |
Number of real data points in your original spectra. |
verbose |
Whether to print additional information during the alignment process. |
show |
Whether to plot the original and aligned spectra. |
mfrow |
Layout to use for the plot. Passed on to |
A matrix containing the integral values of the spectra after alignment.
There is one row per spectrum and one column per ppm value. The entry at
position i, j holds the integral value of the signal from spectrum i that
has its center at position j after alignment by speaq. If there is no
signal with center j in spectrum i, entry i, j is set to NA. The column
names of the matrix are the ppm values of the original spectra.
Example return matrix:
... 3.59 3.55 3.57 3.56 3.55 3.54 3.53 .----------------------------------------------> PPM 1 | NA NA 0.20 NA NA NA 0.25 NA 2 | NA NA 0.15 NA NA NA 0.13 NA 3 | NA NA NA 0.2 NA NA 0.18 NA SpNr
2021-2024 Wolfram Gronwald: initial version.
2024-2025 Tobias Schmidt: refactored initial version.
deps <- c("MassSpecWavelet", "impute") deps_installed <- sapply(deps, requireNamespace, quietly = TRUE) if (all(deps_installed)) { # 'speaq' requires 'MassSpecWavelet' and 'impute' to be installed sim_subset <- metabodecon_file("bruker/sim_subset") spectrum_data <- generate_lorentz_curves_sim(sim_subset) feat <- gen_feat_mat(spectrum_data) maxShift <- 200 M <- speaq_align(feat, maxShift, spectrum_data, show = TRUE) str(M) }deps <- c("MassSpecWavelet", "impute") deps_installed <- sapply(deps, requireNamespace, quietly = TRUE) if (all(deps_installed)) { # 'speaq' requires 'MassSpecWavelet' and 'impute' to be installed sim_subset <- metabodecon_file("bruker/sim_subset") spectrum_data <- generate_lorentz_curves_sim(sim_subset) feat <- gen_feat_mat(spectrum_data) maxShift <- 200 M <- speaq_align(feat, maxShift, spectrum_data, show = TRUE) str(M) }
Returns the path to metabodecon's temporary session directory. This directory
equals subdirectory 'metabodecon' of R's temporary session directory
base::tempdir() plus additional path normalization.
tmpdir(subdir = NULL, create = FALSE)tmpdir(subdir = NULL, create = FALSE)
subdir |
Optional subdirectory within the temporary session directory. |
create |
Whether to create the directory if it does not yet exist. |
Returns the path to the temporary session directory.
2024-2025 Tobias Schmidt: initial version.
datadir_temp()
datadir_persistent()
tmpdir() tmpdir("simulate_spectra")tmpdir() tmpdir("simulate_spectra")
Make a color transparent by adding an alpha channel.
transp(col = "violet", alpha = 0.08)transp(col = "violet", alpha = 0.08)
col |
Character string specifying the color to make transparent. |
alpha |
Numeric value between 0 and 1 specifying the transparency level. |
A character string representing the color with an alpha channel.
2024-2025 Tobias Schmidt: initial version.
transp("violet", 0.08) transp("black", 0.5)transp("violet", 0.08) transp("black", 0.5)
Prints the structure of a directory tree up to a specified maximum level of depth. It lists all files and directories under the specified path, displaying them in a tree-like structure.
tree( path, max.level = 2, max.entries = Inf, show.counts = FALSE, files.first = FALSE, level = 0, prefix = "" ) tree_preview( path, max.level = 1, max.entries = 9, show.counts = TRUE, files.first = TRUE )tree( path, max.level = 2, max.entries = Inf, show.counts = FALSE, files.first = FALSE, level = 0, prefix = "" ) tree_preview( path, max.level = 1, max.entries = 9, show.counts = TRUE, files.first = TRUE )
path |
The root path from which to start listing the directory structure. |
max.level |
The maximum depth of directories to list. |
max.entries |
Maximum number of children to print per directory.
If a directory has more entries than this limit, the first
|
show.counts |
Logical. If |
files.first |
Logical. If |
level |
Internal parameter used for recursion, indicating the current level of depth. |
prefix |
Internal parameter used for formatting the printed tree structure. |
NULL, called for its side effect of printing the directory structure.
2024-2025 Tobias Schmidt: initial version.
metabodecon_dir <- system.file(package = "metabodecon") tree(metabodecon_dir, max.level = 1)metabodecon_dir <- system.file(package = "metabodecon") tree(metabodecon_dir, max.level = 1)
Calculates the width of a numeric vector by computing the difference between the maximum and minimum values in the vector.
width(x)width(x)
x |
A numeric vector. |
The width of the vector, calculated as the difference between its maximum and minimum values.
2024-2025 Tobias Schmidt: initial version.
vec <- c(1, 3, 5, 7, 9) width(vec)vec <- c(1, 3, 5, 7, 9) width(vec)