Skip to main content
MZMLpy Logo

A lightweight Python library for parsing mzML mass spectrometry files. Implements a type-safe, lazy-loading API with direct support for modern mzML structures (>= 1.1.0).

Python package codecov PyPI version DOI Python 3.12+ License: MIT

Installation

pip install mzmlpy

Optional extras:

pip install mzmlpy[numpress]   # MS-Numpress decoding
pip install mzmlpy[zstd]       # Zstandard compression
pip install mzmlpy[rapidgzip]  # Parallel gzip decompression (recommended for .gz files)

MCP integration

Version 0.9.0 preserves the stored numeric dtype when decoding arrays, including exact int64 values. Numpress retains its float64 reconstruction. Code requiring float64 can use .astype(np.float64). See the numeric type migration guide.

An optional local MCP server exposes file discovery, full acquisition metadata, validation, metadata inventories and comparisons, and bounded array access to AI clients. Background jobs, reference resources, workflow prompts, and optional lossless JSONL exports support larger tasks. Spectrum processing stays in Spectacular, and plotting stays in companion visualization tools. Install with pip install "mzmlpy[mcp]" and launch with python -m mzmlpy mcp --root /absolute/path/to/data. See the MCP guide.

Quick Start

from mzmlpy import Mzml

with Mzml("path/to/file.mzML") as reader:
    print(f"File: {reader.file_name}  |  Spectra: {len(reader.spectra)}")

    for spectrum in reader.spectra:
        mz = spectrum.mz
        intensity = spectrum.intensity
        print(f"  {spectrum.id} MS{spectrum.ms_level} — {len(mz)} peaks")

Both .mzML and .mzML.gz files are supported. Metadata is parsed eagerly; binary data is decoded on demand.

Reading Gzipped Files

When opening .mzML.gz files, the gzip_mode parameter controls how the file is accessed:

"auto" is the default. It selects an embedded index first, then a current extracted cache, then complete rapidgzip sidecars, and finally creates an extracted cache. The selected route is available through reader.access_strategy.

Self-indexed gzip files created by write_indexed_gzip are detected automatically when in_memory=False. Their index lives inside the gzip header, so random access needs no extracted copy, sidecar index, or optional dependency.

from mzmlpy import Mzml, write_indexed_gzip

write_indexed_gzip("data.mzML", "data.indexed.mzML.gz")

with Mzml("data.indexed.mzML.gz", in_memory=False) as reader:
    spec = reader.spectra["controllerType=0 controllerNumber=1 scan=1234"]

The writer also accepts an ordinary .mzML.gz input. It preserves the decompressed mzML bytes exactly and writes the destination atomically. The embedded layout is compatible with pyMZML's FU version 1 indexed gzip reader.

Mode Description
"auto" (default) Reuse the fastest valid representation already available, otherwise extract into the central cache.
"extract" Decompress to <tmpdir>/mzmlpy/ and cache across sessions. First open pays decompression cost. Subsequent opens reuse the cache instantly. The OS clears tmp on reboot.
"indexed" Seekable access to the compressed file using rapidgzip. No decompression to disk. Requires pip install mzmlpy[rapidgzip].
"stream" Stream sequentially. Lowest startup cost but no efficient random access.

For most use cases, "extract" or "indexed" is recommended:

# Automatic selection with observable behavior
with Mzml("data.mzML.gz", in_memory=False) as reader:
    print(reader.access_strategy)
    spec = reader.spectra[0]

# Indexed — no extraction, seekable access (requires rapidgzip)
with Mzml("data.mzML.gz", gzip_mode="indexed", in_memory=False) as reader:
    spec = reader.spectra[0]

To reclaim disk space before the OS clears tmp on reboot:

from mzmlpy import clear_cache
clear_cache()

Performance

"extract" pays a one-time decompression cost then matches plain .mzML speed on later opens (the extracted copy is cached). "indexed" pays a one-time index-build cost for seekable access with no disk copy. "stream" has the lowest startup cost but random access re-scans from the start, so it's sequential-only in practice. See benchmarks/ for a reproducible harness with real numbers on real files, including a head-to-head against pyteomics and pymzml.

mzmlpy vs pymzml

Compared against pymzml 2.6.0 on a Bruker timsTOF file with ion mobility (10 spectra, 6.7 MB):

Benchmark mzmlpy pymzml Ratio
Startup 0.012s 0.092s 8.0x faster
Iterate (decode) 0.039s 0.228s 5.8x faster
Random access 0.012s 0.110s 9.2x faster

Both libraries produce identical m/z and intensity arrays. The gap narrows on smaller files (~1.1--1.3x) and widens on larger, more complex files. See the full results in the Benchmarks page or run benchmarks/bench_vs_pymzml.py yourself.

For full usage examples see the Getting Started guide and API Reference.

Using an AI coding assistant? Point it at llms.txt — a compact, accurate API guide for generating correct mzmlpy code.

Validation and filtering

from mzmlpy import Mzml, validate

report = validate("data.mzML", decode_binary=True)
print(report.valid, report.issues)

with Mzml("data.mzML", in_memory=False) as reader:
    for spectrum in reader.spectra.filter(ms_level=2, retention_time=(60, 180)):
        print(spectrum.id)

Validation reports structural and decoding problems without repairing the input. Filtering uses metadata and inclusive retention-time bounds in seconds, without decoding arrays. See the guide for check scope, precursor filters, cache behavior, and python -m mzmlpy CLI commands.

Citation

Citation metadata are provided in CITATION.cff. The archived v0.6.0 release is available from Zenodo at doi:10.5281/zenodo.21960080.

Benchmarks

benchmarks/ contains a reproducible harness comparing mzmlpy against pyteomics and pymzml on compression-format support, throughput, and gzip handling. See benchmarks/README.md for how to run it and current results.

Development

just lint     # ruff check
just format   # ruff isort + format
just ty       # ty type checker
just test     # pytest

# or all at once:
just check

Release files for mzmlpy 0.9.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mzmlpy 0.9.1
File Size Uploaded
mzmlpy-0.9.1.tar.gz 89.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mzmlpy 0.9.1
File Interpreter ABI Platform
mzmlpy-0.9.1-py3-none-any.whl Python 3 none any Details

Total release size: 189.4 kB

Release files / mzmlpy-0.9.1.tar.gz

Download URL mzmlpy-0.9.1.tar.gz
Size 89.7 kB
Tags Source
SHA-256 checksum
How to use checksums
27d48445abbc5bc44874af5da8713d6e2fe97baa00a5c712ce511d91df812b14
BLAKE2b-256 checksum
How to use checksums
5af9b172cf36f654ebe3696cde6e90e421b71a837d615285d1e33c53d2743ffc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 23, 2026.

Transparency log

Release files / mzmlpy-0.9.1-py3-none-any.whl

Download URL mzmlpy-0.9.1-py3-none-any.whl
Size 99.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
382e842ca8293042caa5ef7c06c5b1bfa48e19d2a90c4412f18ce31f6921dcd8
BLAKE2b-256 checksum
How to use checksums
0ebfcebbb302effd8e73ac245625a1090f81bba555c243b10ada6a8271efd941
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 23, 2026.

Transparency log

Release history Release notifications | RSS feed

0.10.0

2 release files

0.9.3

2 release files

0.9.2

2 release files

This release

0.9.1 This release

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page