Skip to main content

A comprehensive toolkit for MALDI-TOF mass spectrometry data preprocessing for antimicrobial resistance (AMR) prediction purposes

Project description

MaldiAMRKit

CI Coverage Documentation

PyPI Version Python License

MaldiAMRKit

A comprehensive toolkit for MALDI-TOF mass spectrometry data preprocessing for antimicrobial resistance (AMR) prediction purposes

InstallationFeaturesQuick StartDocumentationTutorialsContributingCitingLicense

Installation

pip install maldiamrkit

Optional: mzML/mzXML Format Support

pip install maldiamrkit[formats]

Installs pyteomics and lxml for reading standard mass spectrometry data formats.

Optional: Batch Correction & UMAP

pip install maldiamrkit[batch]

Installs combatlearn for ComBat-based batch effect correction and umap-learn for UMAP exploratory plots.

Optional: All Extras

pip install maldiamrkit[all]

Development Installation

git clone https://github.com/EttoreRocchi/MaldiAMRKit.git
cd MaldiAMRKit
pip install -e .[dev]

Features

Preprocessing

  • Composable Pipeline: Build custom PreprocessingPipeline from individual transformers (smoothing, baseline correction, normalization, trimming), serializable to JSON/YAML
  • Multiple Binning Strategies: Uniform, logarithmic, adaptive, and custom bin edges
  • Quality Metrics: SNR estimation, comprehensive quality reports, and alignment assessment
  • Replicate Merging: Mean/median/weighted merging with correlation-based outlier detection

Alignment & Detection

  • Spectral Alignment: Shift, linear, piecewise, and DTW warping for both binned and raw full-resolution spectra
  • Peak Detection: Local maxima and persistent homology methods

Evaluation

  • AMR Metrics: VME, ME, sensitivity, specificity, categorical agreement, and amr_classification_report following EUCAST/CLSI conventions
  • Label Encoding: LabelEncoder for mapping R/I/S to binary with configurable intermediate handling
  • Stratified Splitting: Species-drug stratified and case-based (patient-grouped) splitting to prevent data leakage

Data Management

  • DRIAMS Dataset Building & Loading: Build and load DRIAMS-like dataset directories via build_driams_dataset() / load_driams_dataset()
  • Composable Filters: SpeciesFilter, DrugFilter, QualityFilter, MetadataFilter combinable with &/|/~ operators
  • mzML/mzXML Support: Read standard mass spectrometry formats via optional pyteomics dependency
  • Spectrum Export: Save spectra to CSV or TXT via MaldiSet.save_spectra()

Visualization & Tools

  • Exploratory Plots: PCA, t-SNE, and UMAP scatter plots colored by species, resistance phenotype, or any metadata column
  • Batch Effect Correction: Multi-site/multi-instrument correction via combatlearn (pip install maldiamrkit[batch])
  • CLI: maldiamrkit preprocess, maldiamrkit quality, and maldiamrkit build-driams for batch processing
  • Parallel Processing: Multi-core support via n_jobs parameter
  • ML-Ready: Direct integration with scikit-learn pipelines

Documentation

Full documentation is available at maldiamrkit.readthedocs.io.

Quick Start

Load and Preprocess a Single Spectrum

from maldiamrkit import MaldiSpectrum

# Load spectrum from file
spec = MaldiSpectrum("data/spectrum.txt")

# Preprocess: smoothing, baseline removal, normalization
spec.preprocess()

# Optional: bin to reduce dimensions
spec.bin(bin_width=3)  # 3 Da bins

# Visualize
from maldiamrkit.visualization import plot_spectrum
plot_spectrum(spec, binned=True)

Build a Dataset from Multiple Spectra

from maldiamrkit import MaldiSet

# Load multiple spectra with metadata
data = MaldiSet.from_directory(
    spectra_dir="data/spectra/",
    meta_file="data/metadata.csv",
    aggregate_by=dict(antibiotics="Drug", species="Escherichia coli"),
    bin_width=3
)

# Access features and labels
X = data.X  # Feature matrix
y = data.get_y_single("Drug")  # Target labels

Machine Learning Pipeline

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from maldiamrkit.alignment import Warping
from maldiamrkit.detection import MaldiPeakDetector

# Create ML pipeline
pipe = Pipeline([
    ("peaks", MaldiPeakDetector(binary=False, prominence=0.05)),
    ("warp", Warping(method="shift")),
    ("scaler", StandardScaler()),
    ("clf", RandomForestClassifier(n_estimators=100, random_state=42))
])

# Cross-validation
scores = cross_val_score(pipe, X, y, cv=5, scoring="accuracy")
print(f"CV Accuracy: {scores.mean():.3f} +/- {scores.std():.3f}")

For more examples covering alignment, filtering, evaluation, CLI usage, and more, see the Quickstart Guide and API Reference.

Tutorials

For more detailed examples, see the notebooks:

  • Quick Start - Loading, preprocessing, binning, and quality assessment
  • Peak Detection - Local maxima and persistent homology methods
  • Alignment - Warping methods and alignment quality
  • Evaluation - AMR metrics, label encoding, and stratified splitting
  • Exploration - PCA, t-SNE, UMAP visualizations and batch correction

Contributing

Pull requests, bug reports, and feature ideas are welcome. See the Contributing Guide for how to get started.

Citing

If you use MaldiAMRKit in your research, please cite:

Rocchi, E., Nicitra, E., Calvo, M. et al. Combining mass spectrometry and machine learning models for predicting Klebsiella pneumoniae antimicrobial resistance: a multicenter experience from clinical isolates in Italy. BMC Microbiol (2026). doi:10.1186/s12866-025-04657-2

See the full publications list for more papers using MaldiAMRKit.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgements

This toolkit is inspired by:

Weis, C., Cuénod, A., Rieck, B., et al. (2022). Direct antimicrobial resistance prediction from clinical MALDI-TOF mass spectra using machine learning. Nature Medicine, 28, 164-174. https://doi.org/10.1038/s41591-021-01619-9

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

maldiamrkit-0.10.0.tar.gz (66.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

maldiamrkit-0.10.0-py3-none-any.whl (78.5 kB view details)

Uploaded Python 3

File details

Details for the file maldiamrkit-0.10.0.tar.gz.

File metadata

  • Download URL: maldiamrkit-0.10.0.tar.gz
  • Upload date:
  • Size: 66.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for maldiamrkit-0.10.0.tar.gz
Algorithm Hash digest
SHA256 8524e99c28f10b7d13582f40bf4816c42ab1f30c1db0ff3c047ee79ae1f9a9a3
MD5 a3ca09faf49a83c1c1f5fc3a5ef6487f
BLAKE2b-256 a4f67f1d8b89d04b2ad3fb05f118c92013085374865ded8c4f7e14f05f1ed359

See more details on using hashes here.

File details

Details for the file maldiamrkit-0.10.0-py3-none-any.whl.

File metadata

  • Download URL: maldiamrkit-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 78.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for maldiamrkit-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8fd8a84ba37019cf272c5f0ff28acedf752d03be347e1e40b2793e02be339cd0
MD5 e4139db4e80c64aca1f1adb4d4434f35
BLAKE2b-256 30d2a5e3f89000aaa26429251406e122b44bca93ff39c0f042d46668dabfae96

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page