Mzml Reader
mzmlpy.run.Mzml
Mzml(
file: str | Path | Any,
build_index_from_scratch: bool = False,
gzip_mode: Literal[
"extract", "indexed", "stream"
] = "extract",
in_memory: bool = True,
extract_dir: str | Path | None = None,
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 | Any
|
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['extract', 'indexed', 'stream']
|
Strategy for reading gzip-compressed (
|
'extract'
|
in_memory
|
bool
|
Load the entire file into memory for faster access. |
True
|
extract_dir
|
str | Path | None
|
Directory to store extracted |
None
|
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
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
file_path
property
file_path: Path | None
Access the file path as a Path object if available.
file_name
property
file_name: str
Access the file name as a string.
spectra
property
spectra: SpectrumLookup
Access spectra lookup.
Returns the same lookup instance across calls so its next()/reset() cursor and
regex _id_map persist — reader.spectra.next() in a loop advances instead of
restarting, 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).
id
property
id: str
Access mzML id.
version
property
version: str
Access mzML version.
referenceable_param_groups
property
referenceable_param_groups: dict[
str, ReferenceableParamGroup
]
Access referenceable parameter groups.
instrument_configurations
property
instrument_configurations: dict[
str, InstrumentConfiguration
]
Access instrument configurations.
Utilities
mzmlpy.run.peek_spectrum_count
peek_spectrum_count(file: str | Path) -> int | None
Return a file's spectrum count without building a random-access index.
Unlike Mzml(file).spectrum_count, 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 (typically a few KB into the
file, well before the header content is complete) and stops. 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."
Source code in src/mzmlpy/run.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
mzmlpy.util.clear_cache
clear_cache() -> None
Remove all cached files from the mzmlpy temporary directory.
Deletes the <tmpdir>/mzmlpy/ directory and all its contents.
This includes extracted .mzML files created by gzip_mode='extract'.
Example::
from mzmlpy import clear_cache
clear_cache()
Source code in src/mzmlpy/util.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |