spectrl
Inline spectrum URL encoder for mass spectrometry.
Encodes a complete mass spectrum — peaks, metadata, precursor info — into a single compact, URL-safe token. The entire spectrum lives in the string. No backend required.
spectrl1.<base64url(CBOR document)>
Why
A USI references a spectrum stored in a repository. spectrl embeds it. Use spectrl when you want to share a spectrum directly in a URL, QR code, notebook, or paper — without requiring the reader to have access to the original file.
The two are complementary: a spectrl token can carry a USI back-link, and spectra too large to embed fall back to a USI reference.
Install
pip install spectrl
Requires Python 3.12+.
Quick start
Encode from mzmlpy
from mzmlpy.run import Mzml
from spectrl import encode_spectrum, from_mzmlpy
with Mzml("data.mzML") as mzml:
spec = mzml.spectra[0]
token = encode_spectrum(from_mzmlpy(spec))
print(token)
# spectrl1.hQ...
Encode manually
import numpy as np
from spectrl import encode_spectrum
from spectrl.model import InlineSpectrum, SpectrlCvParam
spec = InlineSpectrum(
default_array_length=3,
mz=np.array([147.0, 175.1, 246.2]),
intensity=np.array([1e5, 8e4, 3e4]),
id="scan=42",
params=[
SpectrlCvParam(accession="MS:1000511", value=2), # ms level
SpectrlCvParam(accession="MS:1000130"), # positive scan
SpectrlCvParam(accession="MS:1000127"), # centroid
],
)
token = encode_spectrum(spec)
Decode
from spectrl import decode_token
decoded = decode_token(token)
print(decoded.mz) # numpy array
print(decoded.intensity) # numpy array
print(decoded.id) # "scan=42"
URL bindings
from spectrl import to_fragment, to_query, to_data_uri, extract_token
# Embed in a URL fragment (recommended — never sent to server)
url = to_fragment(token, "https://viewer.example.com/spectrum")
# https://viewer.example.com/spectrum#spectrl1.hQ...
# Or as a query parameter
url = to_query(token, "https://viewer.example.com/spectrum")
# https://viewer.example.com/spectrum?d=spectrl1.hQ...
# Or as a data URI
uri = to_data_uri(token)
# data:application/vnd.spectrl;v=1,spectrl1.hQ...
# Extract token back from any of the above
token = extract_token(url)
Extra (auxiliary) arrays
Beyond m/z, intensity, charge, and ion mobility, you can attach any per-peak
array — keyed by a CV accession (a standard mzML binary array) or a free-text
name (a non-standard MS:1000786 array). int32/float32 dtypes are preserved.
import numpy as np
from spectrl import encode_spectrum, decode_token
from spectrl.model import InlineSpectrum
spec = InlineSpectrum(
default_array_length=3,
mz=np.array([147.0, 175.1, 246.2]),
intensity=np.array([1e5, 8e4, 3e4]),
extra_arrays={
"MS:1000517": np.array([120.0, 80.0, 45.0]), # signal-to-noise array (named CV)
"iso_score": np.array([0.98, 0.91, 0.74], np.float32), # non-standard (MS:1000786)
},
)
decoded = decode_token(encode_spectrum(spec))
decoded.extra_arrays["iso_score"] # float32 array, round-tripped
Auxiliary arrays are always lossless (raw + zlib) and ride along with the
canonical m/z sort. The JavaScript implementation exposes the same via
extraArrays (use Int32Array/Float32Array to set the data type).
User params (free-text metadata)
For values with no CV term, attach mzML userParams at the spectrum or scan
level. They're omitted entirely when empty, so a spectrum without any is
byte-identical to one produced before the feature existed.
from spectrl.model import InlineSpectrum, SpectrlUserParam
spec = InlineSpectrum(
default_array_length=3, mz=mz, intensity=intensity,
user_params=[
SpectrlUserParam(name="Mascot score", value=42.7, type="xsd:float"),
SpectrlUserParam(name="reanalysis note", value="rerun semitryptic"),
],
)
from_mzmlpy reads spectrum- and scan-level userParams automatically. The JS
implementation exposes the same via userParams. Prefer a CV term whenever one
exists — userParams are heavier (no accession to compress) and uncontrolled.
Trim large spectra
from spectrl import top_n
# Keep the 50 most intense peaks before encoding
trimmed = top_n(spec, 50)
token = encode_spectrum(trimmed)
Lossless encoding
# Default is lossy MS-Numpress (~0.003 mDa m/z error, ~0.007% intensity error)
# Use lossless=True for bit-exact IEEE-754 doubles
token = encode_spectrum(spec, lossless=True)
Token format
spectrl1.<base64url(CBOR document)>
spectrl1— magic + format version; clean version bumps.- The payload is a single CBOR document (RFC 8949), base64url-encoded without padding (RFC 4648 §5). Because it's self-contained, the raw CBOR bytes can also be shipped directly (no base64) as a backend/body payload.
- Header — a CBOR map with integer keys mirroring mzML structure: ms level, polarity, scan times, precursor isolation window, activation method, collision energy, ProForma interpretation, and a truncated SHA-256 content hash.
- Array blobs — one per array type (m/z, intensity, charge, ion mobility, plus any auxiliary arrays), each encoded as MS-Numpress (lossy) or raw IEEE-754 (lossless) + zlib — matching mzML's
binaryDataArraypipeline — and embedded inline in the CBOR document as a byte string.
Encoding precision
Measured over 479,455 peaks from a real LC-MS/MS dataset (BSA, Orbitrap):
| Array | Mean error | Max error |
|---|---|---|
| m/z (MS-Numpress linear) | 0.0025 mDa / 0.006 ppm | 0.005 mDa / 0.056 ppm |
| Intensity (MS-Numpress slof) | 0.007% relative | 0.029% relative |
Size vs mzML
Measured on the same BSA dataset (1,684 spectra):
| Format | MS1 avg (545 peaks) | MS2 avg (109 peaks) |
|---|---|---|
| Raw mzML XML | 12,876 B | 6,004 B |
| spectrl (lossy) | 4,241 B | 1,340 B |
| spectrl (lossless) | 10,302 B | 1,909 B |
CLI
# Encode from JSON
echo '{"mz":[147.0,175.1],"intensity":[1e5,8e4]}' | spectrl encode
# Decode a token
echo "spectrl1.hQ..." | spectrl decode
# Inspect the header as readable JSON
echo "spectrl1.hQ..." | spectrl inspect
Demo
A browser demo encodes example spectra live, shows the shareable URL + QR, and decodes + plots them entirely client-side (no server). Launch it with:
just demo # → http://127.0.0.1:8000
See demo/ for details.
Design
- mzML-faithful — metadata is carried as CV accession maps (MS: ontology), mirroring mzML
cvParamsemantics. No invented field names. - CV binding — all accession constants come from mzmlpy's StrEnum enums; no hardcoded integers.
- Deterministic (within an implementation) — canonical form (m/z-ascending, fixed numpress scale factors, RFC 8949 §4.2 CBOR) yields a stable token from a given implementation, plus a truncated SHA-256 content hash (key 9) verified on decode as a transport-integrity check. The hash is verified by byte-surgery on the received bytes, so it's independent of the CBOR library. Token bytes are not guaranteed identical across implementations (DEFLATE output is not canonical); see SPECIFICATION.md.
- ProForma — carries a ProForma 2.0 peptide interpretation string (key 8), the same mechanism used by USI.
Specification
The normative token format is specified in SPECIFICATION.md (draft, intended for submission to HUPO-PSI). This README is a tutorial; the specification is the contract. A machine-readable CV/codec/key registry lives in schema/registry.json.
Contributing
See CONTRIBUTING.md and the Code of Conduct. Changes to the on-the-wire token format are governed more strictly — see the Format changes section of the contributing guide.
License
Licensed under the Apache License 2.0. If you use spectrl in research, please cite it via CITATION.cff.
Related
- mzmlpy — the mzML parser this library bridges from
- PSI Universal Spectrum Identifier (USI) — references spectra in repositories; complementary to spectrl
- ProForma 2.0 — peptidoform notation carried in the token
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spectrl-0.2.0.tar.gz.
File metadata
- Download URL: spectrl-0.2.0.tar.gz
- Upload date:
- Size: 5.7 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3cfc6d18f46e54ff05c65255b64f7299bc8a1b68788b763f92974b27a8a88ffb
|
|
| MD5 |
600eb74c9a7fce25473839a318cef414
|
|
| BLAKE2b-256 |
e04394af20ed37c27a0b230d2f6e9ea19a56f0a6411fbbae30a337c1ae3d54b9
|
Provenance
The following attestation bundles were made for spectrl-0.2.0.tar.gz:
Publisher:
publish.yml on pgarrett-scripps/spectrl
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spectrl-0.2.0.tar.gz -
Subject digest:
3cfc6d18f46e54ff05c65255b64f7299bc8a1b68788b763f92974b27a8a88ffb - Sigstore transparency entry: 2000548751
- Sigstore integration time:
-
Permalink:
pgarrett-scripps/spectrl@823828e7e32956d5f4aa3d47acf6870b94f00848 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/pgarrett-scripps
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@823828e7e32956d5f4aa3d47acf6870b94f00848 -
Trigger Event:
release
-
Statement type:
File details
Details for the file spectrl-0.2.0-py3-none-any.whl.
File metadata
- Download URL: spectrl-0.2.0-py3-none-any.whl
- Upload date:
- Size: 30.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
efca350473417be54dc29beed7ddaad9b580b5c9c680729e7e1e73689c51ed3c
|
|
| MD5 |
1ae2eaa76618f6e0be64bdb1db579e3f
|
|
| BLAKE2b-256 |
9460c7671677e3c1c6e1c0f09687dec8200aad34c48ae1ccbf88a86c8009623e
|
Provenance
The following attestation bundles were made for spectrl-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on pgarrett-scripps/spectrl
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spectrl-0.2.0-py3-none-any.whl -
Subject digest:
efca350473417be54dc29beed7ddaad9b580b5c9c680729e7e1e73689c51ed3c - Sigstore transparency entry: 2000548835
- Sigstore integration time:
-
Permalink:
pgarrett-scripps/spectrl@823828e7e32956d5f4aa3d47acf6870b94f00848 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/pgarrett-scripps
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@823828e7e32956d5f4aa3d47acf6870b94f00848 -
Trigger Event:
release
-
Statement type: