XCMS3 workflow for LC-MS/MS metabolomics preprocessing. Covers peak detection, retention time alignment, correspondence (grouping), and gap filling. Use when processing raw LC-MS data into a feature table for untargeted metabolomics.
XCMS3 workflow for LC-MS/MS metabolomics preprocessing. Covers peak detection, retention time alignment, correspondence (grouping), and gap filling. Use when processing raw LC-MS data into a feature table for untargeted metabolomics.
Before using code patterns, verify installed versions match. If versions differ:
R: packageVersion('<pkg>') then ?function_name to verify parameters
If code throws ImportError, AttributeError, or TypeError, introspect the installed
package and adapt the example to match the actual API rather than retrying.
XCMS Metabolomics Preprocessing
Requires Bioconductor 3.18+ with xcms 4.0+ and MSnbase 2.28+.
Load Raw Data
Goal: Import raw LC-MS files into R for downstream peak detection and alignment.
Approach: Read mzML/mzXML files into an OnDiskMSnExp object using MSnbase for memory-efficient access.
"Process my raw LC-MS data into a feature table" → Detect chromatographic peaks, align retention times across samples, group corresponding peaks, and fill missing values to produce a sample-by-feature intensity matrix.
Goal: Group corresponding chromatographic peaks across samples into consensus features.
Approach: Use peak density-based grouping which models the RT distribution of peaks in m/z slices to identify features present across samples.
# Group peaks across samples
pdp <- PeakDensityParam(
sampleGroups = pData(xdata)$sample_group,
bw =5,# RT bandwidth
minFraction =0.5,# Min fraction of samples
minSamples =1,# Min samples per group
binSize =0.025# m/z bin size)
xdata <- groupChromPeaks(xdata, param = pdp)# Check feature definitions
featureDefinitions(xdata)
cat('Features:', nrow(featureDefinitions(xdata)),'\n')
Gap Filling
Goal: Recover signal for features that were missed during initial peak detection in some samples.
Approach: Integrate intensity in the expected m/z-RT region for features with missing values using ChromPeakAreaParam.
# Fill in missing peaks
fpp <- ChromPeakAreaParam()
xdata <- fillChromPeaks(xdata, param = fpp)# Alternative: FillChromPeaksParam for more control
fpp2 <- FillChromPeaksParam(
expandMz =0,
expandRt =0,
ppm =0)
Extract Feature Table
Goal: Generate a samples-by-features intensity matrix with m/z and RT annotations for downstream analysis.
Approach: Extract feature values and definitions from the processed XCMSnExp object and combine into an exportable table.
Goal: Assess preprocessing quality through TIC plots, peak counts, RT correction, and PCA.
Approach: Visualize total ion chromatograms, per-sample peak counts, RT adjustment, and PCA of the feature matrix.
# TIC for each sample
tic <- chromatogram(raw_data, aggregationFun ='sum')
plot(tic)# Peak count per sample
peak_counts <- table(chromPeaks(xdata)[,'sample'])
barplot(peak_counts, main ='Peaks per sample')# Check RT correction
par(mfrow =c(1,2))
plotAdjustedRtime(xdata, col = pData(xdata)$sample_group)# PCA of features
library(pcaMethods)
log_values <- log2(feature_values +1)
log_values[is.na(log_values)]<-0
pca <- pca(t(log_values), nPcs =3, method ='ppca')
plotPcs(pca, col = as.factor(pData(xdata)$sample_group))
CAMERA Annotation (Isotopes/Adducts)
Goal: Identify isotope patterns and adduct groups among detected peaks to reduce feature redundancy.
Approach: Use CAMERA to group peaks by RT correlation, assign isotope clusters, and annotate adduct types.
library(CAMERA)# Create CAMERA object
xsa <- xsAnnotate(as(xdata,'xcmsSet'))# Group by RT
xsa <- groupFWHM(xsa, perfwhm =0.6)# Find isotopes
xsa <- findIsotopes(xsa, mzabs =0.01, ppm =10)# Find adducts
xsa <- findAdducts(xsa, polarity ='positive')# Get annotated peak list
camera_results <- getPeaklist(xsa)
Export for MetaboAnalyst
Goal: Format the XCMS feature table for import into MetaboAnalyst web or R package.
Approach: Transpose the matrix, create M/Z-RT feature names, and prepend sample group information.
# Format for MetaboAnalyst web or R package
export_data <- t(feature_values)
colnames(export_data)<- paste0('M',round(feature_defs$mzmed,4),'T',round(feature_defs$rtmed,1))# Add sample info
export_df <- data.frame(Sample = rownames(export_data), Group = pData(xdata)$sample_group, export_data)
write.csv(export_df,'metaboanalyst_input.csv', row.names =FALSE)