mscompress.mszx¶
MSZX archive support — bundle MSZ spectra with annotation files in a self-describing tar archive.
Single-file (v1) archives use MSZXFile/MSZXBuilder; multi-file (v2 "batch")
archives use MSZXBatchFile/MSZXBatchWriter. See the
MSZX format spec.
Single-file archives¶
MSZXFile
¶
Bases: MSZFile
First-class reader for MSZX (tar) archives.
Extends MSZFile so all spectrum properties and methods work directly on
the embedded MSZ payload, while adding access to the archive manifest and
bundled annotation readers. Open via the open() classmethod.
annotation_readers
property
¶
Dict of all annotation readers, keyed by filename.
open
classmethod
¶
open(path: Union[str, PathLike]) -> MSZXFile
Open an MSZX archive for reading.
Mmaps the embedded MSZ payload directly out of the archive (no temp extraction) and eagerly reads annotation entries into memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, PathLike]
|
Path to the .mszx file. |
required |
get_annotation_reader
¶
Get the annotation reader for a specific file by name.
get_annotation_readers_by_format
¶
Get all annotation readers matching a specific format.
decompress
¶
decompress(output: Union[str, PathLike]) -> MZMLFile
Decompress the embedded MSZ to mzML.
If output is an existing directory or has no file extension, the
mzML is written inside it as <archive_stem>.mzML and any cached
annotation readers are written alongside. Otherwise output is
treated as a single file path and only the mzML is written.
extract
¶
extract(
output: str | PathLike,
indicies: list[int] | None = ...,
scan_numbers: list[int] | None = ...,
ms_level: int | None = ...,
) -> MSZXFile
Extract a subset of spectra and annotations to a new MSZX archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
str | PathLike
|
Path to the output .mszx file. |
required |
indicies
|
list[int] | None
|
List of spectrum indices to extract. |
...
|
scan_numbers
|
list[int] | None
|
List of scan numbers to extract. |
...
|
ms_level
|
int | None
|
Filter by MS level. |
...
|
Returns:
| Type | Description |
|---|---|
MSZXFile
|
New MSZXFile instance for the created archive. |
MSZXBuilder
¶
MSZXBuilder(
msz: MSZFile,
source_name: Optional[str] = None,
compression: bool = True,
)
Builder for creating MSZX archives from MSZ files and annotations.
Example
Initialize the builder with an MSZ file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msz
|
MSZFile
|
MSZFile object containing spectra. |
required |
source_name
|
Optional[str]
|
Optional source file name for provenance. |
None
|
add_annotations
¶
add_annotations(
reader: BasePSMReader, description: Optional[str] = None
) -> MSZXBuilder
Add annotations to the archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reader
|
BasePSMReader
|
BasePSMReader instance containing the annotations. |
required |
description
|
Optional[str]
|
Optional description of the annotations. |
None
|
Returns: Self for method chaining.
set_join_key
¶
set_join_key(join_key: str) -> MSZXBuilder
Set the key used to join spectra with search results.
save
¶
Save the MSZX archive to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
Union[str, Path]
|
Output file path (should end with .mszx). |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created archive. |
MSZXManifest
dataclass
¶
MSZXManifest(
version: str = "1.0",
created_at: str = (lambda: isoformat())(),
spectra_file: str = "spectra.msz",
num_spectra: int = 0,
annotations: List[AnnotationEntry] = list(),
join_key: str = "scan_number",
description: Optional[str] = None,
source_file: Optional[str] = None,
extra: Dict[str, Any] = dict(),
)
Manifest describing the contents of an MSZX archive.
This is stored as manifest.json inside the archive for self-description.
from_dict
classmethod
¶
from_dict(data: Dict[str, Any]) -> MSZXManifest
Create from dictionary.
Raises:
| Type | Description |
|---|---|
ValueError
|
if the manifest is a newer major version than this
build supports, or if it is a multi-file/batch archive — those
carry N spectra files and must go through
:class: |
AnnotationEntry
dataclass
¶
AnnotationEntry(
filename: str,
format: AnnotationFormat,
compressed: bool,
description: Optional[str] = None,
num_records: Optional[int] = None,
)
Entry describing an annotation file in the archive.
create_mszx
¶
create_mszx(
msz: MSZFile,
output_path: Union[str, Path],
annotations: Optional[
List[Union[str, Path, BasePSMReader]]
] = None,
description: Optional[str] = None,
) -> Path
Create an MSZX archive from an MSZ file.
Convenience function for simple archive creation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msz
|
MSZFile
|
MSZFile object. |
required |
output_path
|
Union[str, Path]
|
Output path for the .mszx file. |
required |
annotations
|
Optional[List[Union[str, Path, BasePSMReader]]]
|
List of file paths or readers for annotations. |
None
|
description
|
Optional[str]
|
Optional description for the archive. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created archive. |
Batch archives¶
MSZXBatchFile
¶
MSZXBatchFile(
path: Union[str, Path], manifest: MSZXBatchManifest
)
An MSZX archive viewed as a collection of MSZ files.
Holds N members for a v2 ("batch") archive and exactly one for a v1
archive, so the same code handles either. Behaves like an ordered,
lazily-populated collection of :class:MSZFile:
.. code-block:: python
with mscompress.read("cohort.mszx") as archive:
print(len(archive), archive.names)
print(archive.entries[0].num_spectra) # from the manifest, no open
for member in archive: # opened on demand
print(len(member.spectra))
first = archive["a.msz"] # or archive[0]
Members are cached once opened and released together by :meth:close.
entries
property
¶
entries: List[SpectraFileEntry]
Manifest records, including num_spectra where the writer set it.
open
classmethod
¶
open(path: Union[str, Path]) -> MSZXBatchFile
Open any .mszx for reading as a collection.
A v2 batch archive yields N members; a v1 single-file archive
yields exactly one. Use this when you want to handle either version
uniformly. (:class:~mscompress.MSZXFile remains the flat, is-a-MSZFile
reader for v1.)
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
if |
ValueError
|
if the archive has no manifest. |
get
¶
get(key: Union[int, str]) -> MSZFile
Open a member by index or tar member name.
The returned :class:MSZFile is owned by this archive — it is cached
and closed by :meth:close. Do not close it yourself.
get_annotation
¶
Raw bytes of an annotation member attached to a spectra entry.
Payloads flagged compressed in the manifest are returned as stored;
the caller decompresses (the writer does not compress on your behalf).
decompress
¶
Expand every member to <output_dir>/<entry stem>.mzML.
Returns the written paths, in archive order.
MSZXBatchWriter
¶
Incremental writer for a v2 multi-file ("batch") .mszx archive.
Wraps the same C writer the CLI drives, so an archive written from Python is byte-identical to one the CLI produces from the same inputs and settings. Entries stream straight into the archive file — no temp .msz staging — which is why the output must be a seekable regular file.
The archive only becomes valid once finish() succeeds. Leaving the
with block via an exception calls abort(), removing the partial file.
Open an archive for writing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, bytes, PathLike]
|
Output .mszx path. Must be a seekable regular file. |
required |
**kwargs
|
Any
|
Compression settings forwarded to RuntimeArguments (threads, blocksize, mz_lossy, int_lossy, mz_scale_factor, int_scale_factor, target_xml_format, target_mz_format, target_inten_format, zstd_compression_level). |
{}
|
add
¶
add(
source: Union[str, bytes, PathLike, MZMLFile],
name: Optional[str] = None,
) -> int
Compress one mzML into the archive as a new entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[str, bytes, PathLike, MZMLFile]
|
mzML to compress. An already-open MZMLFile reuses its existing mapping. |
required |
name
|
Optional[str]
|
Entry name inside the archive. Defaults to
" |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Index of the new entry, for add_annotation/set_join_key. |
add_annotation
¶
add_annotation(
entry_index: int,
data: bytes,
filename: str,
format: str = "tsv",
compressed: bool = False,
num_records: Optional[int] = None,
) -> None
Attach an annotation file to a spectra entry, as its own member.
Compression is the caller's job — pass already-compressed bytes and
set compressed=True.
set_join_key
¶
Set the column used to join annotations to spectra for one entry.
set_description
¶
Set an archive-level free-text description.
finish
¶
Write manifest.json and the end-of-archive marker, then close.
On failure the partial archive is removed. The writer is unusable afterwards either way.
compress_batch
¶
compress_batch(
inputs: Union[str, Path, Iterable[Union[str, Path]]],
output: Union[str, Path],
*,
recursive: bool = False,
description: Optional[str] = None,
extra: Optional[Dict[str, Any]] = None,
on_progress: Optional[Any] = None,
**compress_args: Any,
) -> Path
Compress many mzML into one v2 .mszx archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
Union[str, Path, Iterable[Union[str, Path]]]
|
A path, directory, glob, or iterable of any of those. |
required |
output
|
Union[str, Path]
|
Destination .mszx. Must be a seekable regular file. |
required |
recursive
|
bool
|
Descend into subdirectories for directory inputs. |
False
|
description
|
Optional[str]
|
Archive-level free-text description. |
None
|
extra
|
Optional[Dict[str, Any]]
|
Archive-level metadata dict, embedded in the manifest. |
None
|
on_progress
|
Optional[Any]
|
Optional |
None
|
**compress_args
|
Any
|
Forwarded to :class: |
{}
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the written archive. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if no input matched, or an input could not be compressed. |
Example
compress_batch("runs/", "cohort.mszx", recursive=True, threads=8)
MSZXBatchManifest
dataclass
¶
MSZXBatchManifest(
version: str = "2.0",
container: str = "batch",
spectra_files: List[SpectraFileEntry] = list(),
description: Optional[str] = None,
extra: Dict[str, Any] = dict(),
)
Manifest of a v2 multi-file ("batch") MSZX archive.
A batch archive bundles N independent MSZ files written by the shared C
batch writer. Unlike :class:MSZXManifest it carries no created_at —
the format is deliberately timestamp-free so identical inputs produce
byte-identical archives.
Every field beyond entry/original/size is optional, so archives
written before those fields existed still parse.
from_dict
classmethod
¶
from_dict(data: Dict[str, Any]) -> MSZXBatchManifest
Create from dictionary.
Raises:
| Type | Description |
|---|---|
ValueError
|
if the manifest is a newer major version than this build supports, or is not a batch manifest at all. |
from_single_file
classmethod
¶
from_single_file(
manifest: MSZXManifest,
) -> MSZXBatchManifest
Adapt a v1 single-file manifest into a one-member batch manifest.
Lets one container type read every .mszx: a v1 archive is simply a
collection of one. container is reported as "single" and
version is preserved, so callers can still tell the two apart.
size is left at 0 — the v1 manifest does not record the payload
length. :meth:MSZXBatchFile.open fills it in from the tar header.
SpectraFileEntry
dataclass
¶
SpectraFileEntry(
entry: str,
original: str = "",
size: int = 0,
num_spectra: Optional[int] = None,
join_key: str = "scan_number",
annotations: List[AnnotationEntry] = list(),
)
One member of a v2 ("batch") MSZX archive.
Attributes:
| Name | Type | Description |
|---|---|---|
entry |
str
|
Tar member name of the MSZ payload (e.g. |
original |
str
|
Basename of the source mzML. Only the basename is recorded, so the source directory is not recoverable from the archive. |
size |
int
|
Payload size in bytes, matching the tar header. |
num_spectra |
Optional[int]
|
Spectrum count, or |
join_key |
str
|
Column used to join annotations to spectra. |
annotations |
List[AnnotationEntry]
|
Annotation members attached to this entry. |