Mzml Reader¶
mzmlpy.run.Mzml
¶
Mzml(
file: str | Path | BinaryIO,
*,
build_index_from_scratch: bool = False,
gzip_mode: Literal[
"auto", "indexed", "stream"
] = "auto",
in_memory: bool = False,
spectrum_id_regex: str | None = None,
chromatogram_id_regex: str | None = None
)
Reader for mzML files.
Data is lazily loaded, so only the specific sections of the XML file are parsed.
The actual data and properties of objects are only parsed when accessed. Use the
context manager to ensure proper file handling. The spectra and chromatograms
properties return lookup objects that support iteration, indexing, and ID-based access.
Note
A reader is not thread-safe: random access shares a single underlying file handle,
so concurrent access from multiple threads on the same Mzml instance will interleave
seeks and reads and return corrupt or wrong data. Use one reader per thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str | Path | BinaryIO
|
Path to the mzML file (str or Path) or a file-like object. |
required |
build_index_from_scratch
|
bool
|
Build the index from scratch instead of using an existing index. |
False
|
gzip_mode
|
Literal['auto', 'indexed', 'stream']
|
Strategy for reading gzip-compressed ( Self-indexed files created by :func:
|
'auto'
|
in_memory
|
bool
|
Load the entire (decompressed) file into memory. Defaults to |
False
|
spectrum_id_regex
|
str | None
|
Optional regex applied to spectrum IDs to create a secondary lookup
key. The first capture group (or full match if no groups) becomes the simplified key.
For example, |
None
|
chromatogram_id_regex
|
str | None
|
Optional regex applied to chromatogram IDs to create a secondary
lookup key. Works identically to |
None
|
Initialize Mzml and parse metadata.
Source code in src/mzmlpy/run.py
access_strategy
property
¶
access_strategy: AccessStrategy
Concrete storage strategy selected when the file was opened.
spectra
property
¶
spectra: SpectrumLookup
Access spectra lookup.
Returns the same lookup instance across calls, so the regex id map built for
spectrum_id_regex persists and id lookups don't re-scan the file on every access.
chromatograms
property
¶
chromatograms: ChromatogramLookup
Access chromatograms lookup.
Returns the same lookup instance across calls (see :meth:spectra).
total_ion_chromatogram
property
¶
total_ion_chromatogram: Chromatogram | None
The total ion chromatogram, or None if the file has none.
Found by the id "TIC" or, failing that, by the MS:1000235 CV term.
obo_version
property
¶
Version of the PSI-MS controlled vocabulary declared in the cvList.
referenceable_param_groups
property
¶
referenceable_param_groups: dict[
str, ReferenceableParamGroup
]
Access referenceable parameter groups (a new dict on every call).
instrument_configurations
property
¶
instrument_configurations: dict[
str, InstrumentConfiguration
]
Access instrument configurations (a new dict on every call).
data_processes
property
¶
data_processes: dict[str, DataProcessing]
Access data processing steps (a new dict on every call).
scan_settings
property
¶
scan_settings: dict[str, ScanSetting]
Access scan settings (a new dict on every call).
validate
¶
validate(
*,
decode_binary: bool = False,
check_index: bool = False
) -> ValidationReport
Validate this reader's XML through a fresh handle.
See :func:mzmlpy.validate for check scope. This checks the representation selected
by the reader. Use the standalone function to validate the original file directly.
Source code in src/mzmlpy/run.py
Utilities¶
mzmlpy.run.peek_spectrum_count
¶
Return a file's spectrum count without building a random-access index.
Unlike len(Mzml(file).spectra), this does not construct a reader or index every
spectrum's byte offset — it streams forward just far enough to read the
<spectrumList count="N"> opening tag's count attribute (just after the header
metadata, before the first spectrum) and stops. Files ending in .gz or .igz are
decompressed on the fly. Useful for cheaply checking many
files (e.g. before deciding which to open fully). Returns None if the file has no
spectrumList or the tag has no count attribute.
Note
There is no equally cheap peek_chromatogram_count: per the mzML schema,
chromatogramList follows spectrumList, so reaching its opening tag requires
streaming past the entire spectrum list first — at that point building the full index
via :class:Mzml is a better fit than a "peek."