serrf
Rust implementation of SERRF: Systematic Error Removal Using Random Forest.
SERRF is a QC-sample-based normalization algorithm for large-scale untargeted metabolomics and lipidomics data. It trains a random forest model per compound on quality-control samples to estimate systematic measurement drift, then applies a multiplicative correction to all samples.
Reference implementations:
Original paper:
Fan, S., Kind, T., Cajka, T., Dolinoy, D. C., Halden, R. U., Barupal, D. K., ... & Fiehn, O. (2019). Systematic Error Removal Using Random Forest for Normalizing Large-Scale Untargeted Lipidomics Data. Analytical Chemistry, 91(5), 3590-3596. https://doi.org/10.1021/acs.analchem.8b05592
The example dataset bundled in examples/shiny/ and SERRF example dataset.xlsx is taken from the
Shiny-SERRF repository (MIT License) and is used here
for validation purposes.
Quick start: Shiny-SERRF example dataset
The repository includes the example dataset from the original Shiny-SERRF project (268 compounds,
1299 samples across multiple batches). Pre-converted CSVs are in examples/shiny/.
Expected output:
Normalized 268/268 compounds.
Median QC train RSD: 0.2760 -> 0.1086, improvement 2.54x.
Median QC validate RSD: 0.2786 -> 0.1220, improvement 2.28x.
To regenerate the CSVs from the raw XLSX (requires Python 3, no extra packages):
Input format
Two CSV files are required.
intensities.csv
Rows are compounds, columns are samples. First column is compound ID. Empty cells are treated as missing (NaN). Invalid non-numeric cells are errors.
compound,sample_001,sample_002,qc_001
glucose,123.4,125.0,120.0
lactate,55.1,,54.9
metadata.csv
One row per sample. Supported sample types: qc, sample, validate.
sample_id,sample_type,batch,injection_order
qc_001,qc,A,1
sample_001,sample,A,2
sample_002,sample,A,3
Every sample column in the intensity file must have a metadata row, and vice versa.
CLI usage
All flags:
| Flag | Default | Description |
|---|---|---|
--intensities |
required | Intensity matrix CSV |
--metadata |
required | Sample metadata CSV |
--output |
required | Normalized CSV output path |
--report |
required | JSON report output path |
--trees |
500 | Number of RF trees per compound |
--max-depth |
unlimited | Maximum tree depth |
--min-samples-leaf |
1 | Minimum samples per leaf |
--qc-validation-fraction |
0.2 | Fraction of QC held out as validation (when no explicit validate samples) |
--seed |
42 | Random seed |
--min-qc-non-missing-fraction |
0.6 | Minimum fraction of non-missing QC values required per compound |
--epsilon |
1e-12 | Near-zero threshold |
--use-correlated-compounds |
false | Include other compound intensities as predictors |
--log-space |
false | Train RF on log-transformed intensities (recommended for log-normal data) |
--quiet |
false | Suppress stderr summary |
-v / -vv |
Verbose / debug logging |
Algorithm
For each compound:
- Select QC training samples with at least
min-qc-non-missing-fractionnon-missing values. - Build a feature matrix: injection order (normalized to [0,1]), batch index, and optionally other compound intensities.
- Train a random forest regressor to predict the compound's intensity from those features.
- Predict for all samples.
- Apply multiplicative correction:
normalized = raw / prediction * anchor, whereanchor = mean(predictions at QC training samples).
Compounds with too few QC values, invalid predictions, or invalid anchors are skipped and their raw values are copied unchanged to the output.
Metrics
The report includes:
median_qc_train_rsd_before/after: median relative standard deviation across QC training samples, before and after normalization.median_qc_validate_rsd_before/after: same for validation QC.qc_train_rsd_improvement_ratio: before/after ratio. Values above 1.0 indicate improvement.- Per-compound
qc_train_rsd_before/afterand skip reasons.
Warnings and skipped compounds
The report includes a warnings array. Compounds are skipped when:
insufficient_qc: too few non-missing QC values.invalid_anchor: the mean RF prediction at QC training samples is near zero or non-finite.model_training_failed/prediction_failed: the RF model failed.
Skipped compounds have their raw values copied unchanged to the output.
Limitations
- Batch encoding is integer-based. One-hot encoding would be more correct for large batch counts.
- No log-space normalization mode (planned).
use-correlated-compoundsis off by default; it increases runtime and can remove biological signal if biological variation correlates with injection order.- Numerically validated on the Shiny-SERRF example dataset (same RSD improvement direction); output values are not bit-for-bit identical to SERRFweb or pySERRF due to different RF implementations.
Future work
- One-hot batch encoding
- Python bindings via pyo3 (
serrf-py) - PCA report output
- MTBLS79 / pySERRF metric golden comparison
- Config file support (
--config serrf.toml)