This vignette demonstrates data import and preprocessing workflow for NMR-based metabolomics with metabom8.
library(metabom8)
In routine high-throughput NMR workflows, raw time-domain data (the free-induction decay) are typically processed directly on the spectrometer using the vendor software TopSpin. Downstream metabolomics analysis therefore commonly starts from frequency-domain spectra. Consequently, this vignette focuses on importing spectra generated by TopSpin.
For completeness and teaching purposes, the section (Starting point: raw FID) briefly illustrates importing and transforming time domain data to obtain spectra.
The function read1d_proc() imports TopSpin-processed 1D NMR
spectra from the pdata subdirectory of Bruker experiment folders
(typically pdata/1). It reads the processed absorption-mode spectrum
(e.g. 1r) together with associated acquisition (acqus) and processing
(procs) parameters.
At minimum two arguments are required:
path: path to the directory that encloses NMR experimentsexp_type: list of spectrometer parameters to filter desired
experiment types based on acquisition and processing metadata.Further details on supported filtering options are provided in ?read1d_proc.
## Example: import 1D spectra
exp_dir <- system.file("extdata", package = "metabom8")
exp_type <- list(pulprog="noesygppr1d")
nmr_data <- read1d_proc(exp_dir, exp_type, n_max = 100)
#> Imported 2 spectra.
names(nmr_data)
#> [1] "X" "ppm" "meta"
Both import functions (read1d_proc() and read1d_raw()) return a named
list containing three core components used throughout this vignette:
the spectral matrix (X), the chemical-shift axis (ppm), and associated
metadata (meta).
For interactive exploration, setting to_global = TRUE assigns the
components X, ppm, and meta directly to the global environment
(.GlobalEnv). Existing objects with these names will be overwritten.
The row names of X and meta correspond to experiment directories and can be
used to join external sample annotations. The meta data frame
contains full TopSpin acquisition and processing parameters for each
spectrum.
Example acquisition and processing parameters as they appear in meta:
| Prefix | Meaning | Examples |
|---|---|---|
| a_ | Acquisition parameters | a_PULPROG, a_SFO1, a_RG, a_SW_h, a_TE |
| p_ | Processing parameters | p_WDW, p_LB, p_SI, p_PHC, p_BC, p_NC_proc |
See ?read1d_proc() for further details.
For completeness, metabom8 also provides functionality to import and
process raw FIDs. This allows users to reproduce basic spectral processing
steps within R or to explore how different processing parameters influence
the resulting spectra.
Processing an FID converts the time-domain signal into a frequency-domain spectrum. Typical processing steps include digital-filter correction (group-delay), apodisation (windowing), zero-filling, Fourier transformation, phase correction.
The function read1d_raw() performs these operations:
# import 1D NMR data
nmr_raw <- read1d_raw(
exp_dir,
exp_type,
apodisation = list(fun = "exponential", lb = 0.2),
zerofil = 1L,
mode = "absorption"
)
#> Processing 2 experiments.
names(nmr_raw)
#> [1] "X" "ppm" "meta"
The following examples illustrate several commonly used apodisation
profiles implemented internally in metabom8.
# selected FID apodisation functions
f_apod <- c("sine","cosine","exponential","sem")
# create toy fid
n <- 200; t <- seq(0,1,len=n)
fid <- exp(-5*t)*cos(20*pi*t)
# apply apodisation
A <- sapply(f_apod, \(f) metabom8:::.fidApodisationFct(n, list(fun=f, lb=-0.2)))
Fp <- sweep(A, 1, fid, `*`)
S <- apply(Fp, 2, \(x) Mod(fft(x))[1:(n/2)])
# compare graphically
cols <- 1:ncol(A)
par(mfrow=c(2,2), mar=c(3,3,2,1))
plot(t, fid, type="l", lwd=2, main="FID")
matplot(t, A, type="l", lwd=2, col=cols, main="Apodisation windows")
matplot(t, Fp, type="l", lwd=2, col=cols, main="Windowed FID")
matplot(S, type="l", lwd=2, col=cols, main="Spectra")
legend("topright", f_apod, col=cols, lty=1, bty="n", cex=.8)
Spectra can be visualised with the function plot_spec(). The minimum
set of arguments include the spectral data X (array or matrix) and the
chemical shift vector ppm.
Three plotting backends are available: "plotly" (default), "base", and
"ggplot2". The "plotly" backend is set up to use WebGL, enabling
smooth interactive graphics even when the number of data spectra is
large. The "ggplot2" backend is not iteractive and renders much slower.
However, the returned object integrates with the extensive "ggplot2" ecosystem
and can be further customised to generate publication-quality figures.
See ?plot_spec for additional details.
data("covid", package = "metabom8")
## plot directly from the dataset object
plot_spec(covid, shift = c(0.5, 2))