Skip to main content

PSI Spectral Library format - Python implementation

GitHub Actions Workflow Status Read the Docs

This is a Python reference implementation of the mzSpecLib spectral library format. It provides readers and writers for the Text and JSON serialization of mzSpecLib, as well as readers for the following spectral library formats found in the wild:

  • NIST MSP (also minimal writing facility)
  • SPTXT
  • BiblioSpec
  • EncyclopeDIA
  • DIA-NN
  • Spectronaut

Once installed, it can be used programmatically by importing the mzspeclib Python library, or using the mzspeclib command line tool to read, write, and manipulate spectral libraries.

Development

For development, run:

pip install --editable .

Test with

pytest

Usage

Python library

mzspeclib-py provides a Python API with the name mzspeclib. The top-level type, SpectrumLibrary is the main entry point into the library:

from mzspeclib import SpectrumLibrary

# Open a spectrum library
lib = SpectrumLibrary(filename="examples/fetal_brain_tiny.mzspeclib.txt")
print(lib)

# Get the number of spectra in the library
n_spectra = len(lib)
print(n_spectra)

# Get a specific spectrum from the library
spec = lib.get_spectrum(spectrum_number=3)
print(f"Key={spec.key}; Name={spec.name}; Num Peaks={len(spec.peak_list)}")
print(spec.get_interpretation('1'))

# Loop over a library and count the number of unique peptide analytes
unique_analytes = set()
n_spectra = 0
for spec in lib:
    for analyte in spec.analytes.values():
        unique_analytes.add(str(analyte.peptide))
    n_spectra += 1

print(f"\n{len(unique_analytes)} unique analytes over {n_spectra} spectra")
print(unique_analytes)

Command Line Tool

It also provides a command line tool, also called mzspeclib that lets the user convert supported spectrum library formats into one of the PSI MzSpecLib formats.

All of the commands provide a limited automatic file format detection, but you can specify the input format if needed.

$ mzspeclib --help
Usage: mzspeclib [OPTIONS] COMMAND [ARGS]...

  A collection of utilities for inspecting and manipulating spectral
  libraries.

Options:
  -d, --debug-logging  Enable debug logging
  -l, --log-file PATH  Write log messages to this file as well as STDERR
  -h, --help           Show this message and exit.

Commands:
  convert   Convert a spectral library from one format to another
  describe  Produce a minimal textual description of a spectral library
  index     Build an on-disk index for a spectral library
  validate  Semantically validate a spectral library

File Conversion

mzspeclib convert can read a variety of different text and binary file formats and write them out in PSI MzSpecLib's Text and JSON encodings, as well as a dialect of MSP.

$ mzspeclib convert --help
Usage: mzspeclib convert [OPTIONS] INPATH OUTPATH

  Convert a spectral library from one format to another. If `outpath` is `-`,
  instead of writing to file, data will instead be sent to STDOUT.

Options:
  -i, --input-format [bibliospec|blib|dia-nn.tsv|dlib|encyclopedia|json|msp|mzSpecLib.json|mzSpecLib.txt|mzlb.json|mzlb.txt|mzlib.json|mzlib.txt|mzspeclib.json|mzspeclib.txt|spectronaut.tsv|sptxt|text]
                                  The file format of the input file. If
                                  omitted, will attempt to infer
                                  automatically.
  -f, --format [text|json|msp]
  -k, --library-attribute <TEXT TEXT>...
                                  Specify an attribute to add to the library
                                  metadata section. May be repeated.
  -K, --header-file PATH          Specify a file to read name-value pairs
                                  from. May be JSON or TAB-separated
  -h, --help                      Show this message and exit.

Semantic Validation

mzspeclib includes a reference implementation of a semantic validator tool for spectrum libraries, testing whether or not a library is compliant with the specification and a subset of community recommended practices.

$ mzspeclib validate --help
Usage: mzspeclib validate [OPTIONS] INPATH

  Semantically and structurally validate a spectral library.

Options:
  -p, --profile [consensus|single|silver|peptide|gold]
  -i, --input-format [bibliospec|blib|dia-nn.tsv|dlib|encyclopedia|json|msp|mzSpecLib.json|mzSpecLib.txt|mzlb.json|mzlb.txt|mzlib.json|mzlib.txt|mzspeclib.json|mzspeclib.txt|spectronaut.tsv|sptxt|text]
                                  The file format of the input file. If
                                  omitted, will attempt to infer
                                  automatically.
  -h, --help                      Show this message and exit.

Metadata

Release files for mzspeclib 1.0.7

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

Source distribution (sdist)

Source distribution for mzspeclib 1.0.7
File Size Uploaded
mzspeclib-1.0.7.tar.gz 129.0 kB Details

Built distribution (wheel)

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

Total release size: 282.5 kB

Release files / mzspeclib-1.0.7.tar.gz

Download URL mzspeclib-1.0.7.tar.gz
Size 129.0 kB
Tags Source
SHA-256 checksum
How to use checksums
159526a2aacf96774c687efdf9b7e7c330ed4d57a744d00dbd4d46f0711e41d0
BLAKE2b-256 checksum
How to use checksums
c590b024c0c824ef15a20da5fdc64a8db7485c9be773a9bba9380e9ce947a95e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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 Jun 19, 2025.

Transparency log

Release files / mzspeclib-1.0.7-py3-none-any.whl

Download URL mzspeclib-1.0.7-py3-none-any.whl
Size 153.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f7bf0ce97f9d876620892215cdb04a36f5f6b1c8065e6b25a9cde486a33d14bd
BLAKE2b-256 checksum
How to use checksums
e38fcf754949442dfbde7e8372f982626376c6b61bfee14826dd647e7bf0bc6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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 Jun 19, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.7 This release

2 release files

1.0.5

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

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