API reference¶
Generated from the docstrings in the source, so it cannot fall out of step with the code.
pymzlib.pride¶
pride ¶
PRIDE Archive access, backed by mzLib's PrideArchiveClient.
The PRIDE Archive (https://www.ebi.ac.uk/pride/archive/) is EBI's public proteomics data repository. This module lets a Python user list what is in a project and pull files down, using the same paging, URL-resolution, and safe-download logic that mzLib uses in C#.
>>> import pymzlib
>>> files = pymzlib.pride.list_files("PXD000001")
>>> len(files)
8
>>> files[0].file_name
'PRIDE_Exp_Complete_Ac_22134.pride.mztab.gz'
>>> raw = [f for f in files if f.category == "RAW"]
>>> pymzlib.pride.download("PXD000001", "downloads", category="RAW")
ProjectNotFoundError ¶
Bases: PyMzLibError
No project with that accession exists, or it has no files.
PRIDE answers an unknown accession with an empty result rather than a 404, so earlier versions of pyMzLib returned an empty list. That was a mistake: an empty list is indistinguishable from "this project genuinely has nothing matching", so a typo'd accession produced a script that reported "0 files, done" and moved on. A wrong answer that looks like a right answer is worse than an error.
Source code in pkg/python/src/pymzlib/pride.py
PrideFile
dataclass
¶
One file belonging to a PRIDE Archive project.
Attributes:
| Name | Type | Description |
|---|---|---|
file_name |
str
|
The file's name, e.g. |
file_size_bytes |
int
|
Size in bytes as reported by PRIDE. |
checksum |
str
|
The repository's checksum, or |
category |
str
|
The file category, e.g. |
https_url |
str | None
|
A direct HTTPS download URL, or |
locations |
list[dict[str, str]]
|
Every published location as |
submission_date |
/ publication_date / updated_date
|
Repository timestamps. |
Source code in pkg/python/src/pymzlib/pride.py
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
size_mb
property
¶
The file size in megabytes, for the common case of eyeballing a manifest.
extension
property
¶
The file's lowercase extension including the dot, e.g. ".raw". Empty if none.
downloadable
property
¶
Whether this file can be fetched by :func:download (i.e. has an HTTPS location).
as_dict ¶
Return every attribute, including the computed ones, as a plain dict.
Use this rather than vars(f) when building a table. size_mb, extension and
downloadable are properties, so vars() and dataclasses.asdict() both skip
them — which silently produced a DataFrame missing the three attributes the
documentation pushes hardest, including the downloadable flag used to filter out
files that cannot be fetched.
Example
import pandas as pd # doctest: +SKIP df = pd.DataFrame([f.as_dict() for f in files]) # doctest: +SKIP
Source code in pkg/python/src/pymzlib/pride.py
PrideFtpFile
dataclass
¶
One file found by walking a PRIDE project's FTP directory tree — the complete listing.
This is what :func:list_ftp_files returns, and the difference from :class:PrideFile is the
whole point: the FTP walk sees everything the project holds, including the files PRIDE's REST
manifest omits and files nested in subdirectories. The trade-off is the size: PRIDE's directory
index rounds it to about three significant figures, so it is approximate_size_bytes — a good
project-size estimate, not the exact number of bytes you will transfer.
Attributes:
| Name | Type | Description |
|---|---|---|
relative_path |
str
|
Path relative to the project's FTP root, e.g. |
file_name |
str
|
The bare file name — the last segment of |
url |
str
|
The HTTPS URL the file can be downloaded from. |
approximate_size_bytes |
int
|
PRIDE's rounded index size in bytes. For the exact transfer size of
one file, issue an HTTP HEAD against |
project_accession |
str
|
The accession this file was listed under. |
Source code in pkg/python/src/pymzlib/pride.py
approximate_size_mb
property
¶
The approximate size in megabytes, for eyeballing a project's footprint.
extension
property
¶
The file's lowercase extension including the dot, e.g. ".raw". Empty if none.
as_dict ¶
Return every attribute, including the computed ones, as a plain dict for a DataFrame.
Source code in pkg/python/src/pymzlib/pride.py
PrideProjectSearchResult
dataclass
¶
One hit from :func:search.
This is not a project's full metadata, and the two are not interchangeable. PRIDE serves
search from a separate Elasticsearch projection in which every controlled-vocabulary field has
been flattened to a display string: the same project reports its instruments as
["Q Exactive"] here and as structured terms with accessions from the metadata endpoint,
contacts collapse from ten-field objects to a display name, and publications to a single
pre-formatted citation string. That is a property of PRIDE's wire, not a simplification chosen
here — the accessions are simply not sent. Follow :attr:accession when you need the
vocabulary.
A zero or an empty list means "not reported", never a measured zero. PRIDE omits nothing as
null, so absence arrives as 0, "" or [], and several fields are genuinely sparse —
sampled across 1,600 hits, :attr:project_tags was populated on 2.6%, :attr:sdrf on 2.4%,
:attr:other_omics_links on 18%, and the bot/hub/organic trio on under half. Do not read a
download_count of 0 as "nobody downloaded it".
Attributes:
| Name | Type | Description |
|---|---|---|
accession |
str
|
The project accession — the key to everything else in this module. Usually a
|
title |
str
|
The project title. |
project_description |
str
|
The submitter's free-text description. |
sample_processing_protocol |
str
|
How the sample was prepared, as free text. |
data_processing_protocol |
str
|
How the data were searched and processed, as free text. |
doi |
str
|
The dataset DOI, or |
submission_type |
str
|
|
sdrf |
str
|
The project's SDRF metadata as a single space-joined bag of term values, flattened
by the search index. Not a file, filename or URL — nothing can be fetched with it
and the row/column structure is gone. For a real SDRF see :mod: |
submission_date |
/ publication_date / updated_date
|
Calendar :class: |
project_tags |
list[str]
|
PRIDE's coarse classification tags. |
keywords |
list[str]
|
The submitter's free-text keywords. May contain empty and whitespace-only strings — PRIDE ships them on roughly 9% of hits. They are passed through rather than filtered, because dropping them here would make this module disagree with mzLib and with the Rust and R bindings about what a project's keywords are. Filter before joining. |
submitters |
/ lab_pis / affiliations
|
Display names and affiliations, flattened from the structured contact objects the metadata endpoint returns. |
instruments |
/ softwares / quantification_methods
|
Display names. |
sample_attributes |
list[str]
|
Sample characteristics by display value (e.g. |
organisms |
/ organism_parts / diseases
|
Display names. |
references |
list[str]
|
Publications, each a single pre-formatted citation string. A PubMed ID or DOI cannot be read out of one without parsing the string PRIDE assembled. |
experiment_types |
list[str]
|
e.g. |
project_file_names |
list[str]
|
File names only — a search convenience, not the manifest. It
carries no sizes, categories or download locations. Use :func: |
other_omics_links |
list[str]
|
Links to related datasets in other omics repositories. |
highlights |
dict[str, list[str]]
|
Why this project matched, keyed by the field each match was found in, with the
matched terms wrapped in |
yearly_downloads |
list[dict[str, Any]]
|
|
download_count |
/ avg_downloads_per_file / percentile
|
Download popularity. |
bot_count |
/ hub_count / organic_count
|
Downloads split by traffic kind. |
Source code in pkg/python/src/pymzlib/pride.py
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 | |
matched_fields
property
¶
Which PRIDE fields the query hit, from :attr:highlights. Empty if PRIDE reported none.
as_dict ¶
Every attribute, including the computed ones, as a plain dict.
Use this rather than vars() when building a table: matched_fields is a property, so
dataclasses.asdict() silently omits it. Same reasoning as :meth:PrideFile.as_dict.
Source code in pkg/python/src/pymzlib/pride.py
list_files ¶
Return the file manifest of a PRIDE Archive project.
This is what PRIDE's REST API publishes, which is not always everything in the
project. For PXD000001 the API returns 8 files while the FTP tree holds 13, and
the five it omits include the two largest: ...60min_01-20141210.mzML (450 MB) and the
matching .mzXML (472 MB), exactly the modern open-format conversions most people want.
The omission is PRIDE's, not mzLib's. If completeness matters, use :func:list_ftp_files,
which walks the project's FTP directory and returns everything it actually holds.
Paging is handled for you: however many pages the project spans, you get one list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
accession
|
str
|
The project accession, e.g. |
required |
page_size
|
int
|
How many files to request per underlying API call. Only affects how the manifest is fetched, never what you get back. |
100
|
timeout
|
float | None
|
Seconds to allow for the whole fetch. |
300
|
Returns:
| Type | Description |
|---|---|
list[PrideFile]
|
Every file in the project, in repository order. An unknown accession yields an empty |
list[PrideFile]
|
list rather than an error — that is PRIDE's own behavior, preserved here. |
Raises:
| Type | Description |
|---|---|
UsageError
|
the accession is blank or the page size is not positive. |
BridgeError
|
PRIDE returned an error status or was unreachable. |
Source code in pkg/python/src/pymzlib/pride.py
list_ftp_files ¶
Return the complete file list of a PRIDE project, read from its FTP directory tree.
This is the authoritative counterpart to :func:list_files. Where list_files returns
PRIDE's REST manifest — which is knowingly incomplete, omitting for PXD000001 five of the
project's 13 files, including the two largest — this walks the FTP directory (subdirectories
included) and returns everything the project actually holds. Reach for it whenever completeness
or a true project size matters; use :func:list_files when you want the rich metadata (category,
checksum, controlled-vocabulary locations) that the REST manifest carries and the directory
index does not.
This is a listing surface only. :func:download and :func:download_files operate on the
REST manifest, so a file that appears only here — the whole point of this function — is not
accepted by them; fetch it directly from its :attr:PrideFtpFile.url with an ordinary HTTPS
client (e.g. urllib.request.urlretrieve(f.url, f.file_name)).
The sizes are approximate: PRIDE's directory index rounds them (see
:attr:PrideFtpFile.approximate_size_bytes), so :func:approximate_total_size_bytes is an
estimate — but an estimate over the whole project, unlike :func:total_size_bytes.
>>> ftp = pymzlib.pride.list_ftp_files("PXD000001") # doctest: +SKIP
>>> len(ftp) # doctest: +SKIP
13
>>> nested = [f.relative_path for f in ftp if "/" in f.relative_path] # doctest: +SKIP
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
accession
|
str
|
The project accession, e.g. |
required |
timeout
|
float | None
|
Seconds to allow for the whole walk, which spans one request per directory. |
300
|
Returns:
| Type | Description |
|---|---|
list[PrideFtpFile]
|
Every file under the project's FTP root, subdirectories included, in the order the walk |
list[PrideFtpFile]
|
encounters them. Never empty — an empty result is raised as an error (see below). |
Raises:
| Type | Description |
|---|---|
UsageError
|
the accession is blank or malformed. |
ProjectNotFoundError
|
no project has that accession (or it lacks the publication date that
locates its FTP directory), or the directory listed no files. Same "no such project"
signal :func: |
ServiceUnavailableError / BridgeError
|
PRIDE was unreachable, or a directory fetch failed. |
Source code in pkg/python/src/pymzlib/pride.py
download ¶
download(accession: str, destination: str | Path, category: str | None = None, extensions: Sequence[str] | None = None, overwrite: bool = True, timeout: float | None = None) -> list[Path]
Download a project's files, optionally filtered, and return where they landed.
Files are streamed to a temporary name and moved into place only once complete, so an interrupted download never leaves a truncated file behind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
accession
|
str
|
The project accession, e.g. |
required |
destination
|
str | Path
|
Directory to write into. Created if it does not exist. |
required |
category
|
str | None
|
Keep only files of this category, e.g. |
None
|
extensions
|
Sequence[str] | None
|
Keep only files with these extensions, e.g. |
None
|
overwrite
|
bool
|
When |
True
|
timeout
|
float | None
|
Seconds to allow. |
None
|
Returns:
| Type | Description |
|---|---|
list[Path]
|
The paths written, in manifest order. |
Raises:
| Type | Description |
|---|---|
UsageError
|
the accession or destination is blank. |
BridgeError
|
a request failed, or a selected file has no HTTPS location. |
Source code in pkg/python/src/pymzlib/pride.py
download_files ¶
download_files(files: Iterable[PrideFile], destination: str | Path, overwrite: bool = True, timeout: float | None = None) -> list[Path]
Download exactly the files you selected, and nothing else.
This is the counterpart to :func:list_files, and usually the one you want. Filter the
manifest however you like — in Python, with the full expressiveness of Python — and hand the
result straight back:
>>> files = list_files("PXD000001") # doctest: +SKIP
>>> small = [f for f in files if f.size_mb < 5 and f.downloadable]
>>> download_files(small, "downloads") # doctest: +SKIP
:func:download's category and extensions filters can only express what they were
built to express; "under 5 MB", "the three newest", or "everything except the MGF" cannot be
said in that vocabulary at all. They can all be said in a list comprehension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files
|
Iterable[PrideFile]
|
The :class: |
required |
destination
|
str | Path
|
Directory to write into. Created if it does not exist. |
required |
overwrite
|
bool
|
When |
True
|
timeout
|
float | None
|
Seconds to allow. |
None
|
Returns:
| Type | Description |
|---|---|
list[Path]
|
The paths written, in the order the repository lists them. |
Raises:
| Type | Description |
|---|---|
UsageError
|
the selection is empty, spans several projects, or includes a file with no HTTPS location. |
Source code in pkg/python/src/pymzlib/pride.py
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 | |
total_size_bytes ¶
Sum the sizes of some files.
This is the size PRIDE reports, which is not the number of bytes you will transfer.
For compressed files PRIDE frequently reports the decompressed size: in PXD000001 the
reported size of PRIDE_Exp_Complete_Ac_22134.pride.mgf.gz is 16,448,103 bytes, exactly
what gzip -l gives as its uncompressed length, while the actual download is 5,984,662
bytes, 2.75x smaller.
It is also a sum over an incomplete manifest. For PXD000001 this returns 0.51 GB; the
project on disk is 1.44 GB, because PRIDE's API omits five files including the two largest
(see :func:list_files). The two errors run in opposite directions and do not cancel. For
a size that covers the whole project, use :func:approximate_total_size_bytes over
:func:list_ftp_files.
files = list_files("PXD000001") # doctest: +SKIP total_size_bytes(f for f in files if f.category == "RAW") / 1e9 # doctest: +SKIP 0.51
Source code in pkg/python/src/pymzlib/pride.py
approximate_total_size_bytes ¶
Sum the approximate sizes of some FTP files.
This is the honest project-size number, and the counterpart to :func:total_size_bytes with the
trade-offs reversed. It sums over the complete FTP listing (:func:list_ftp_files), so no
files are missing — but each size is PRIDE's directory-index value, rounded to about three
significant figures, so the total is an estimate, not an exact byte count. For PXD000001 it
lands near the true 1.44 GB, where :func:total_size_bytes reports 0.51 GB over the incomplete
REST manifest. When you need the exact bytes for one file, HTTP HEAD its
:attr:PrideFtpFile.url and read Content-Length.
ftp = list_ftp_files("PXD000001") # doctest: +SKIP approximate_total_size_bytes(ftp) / 1e9 # doctest: +SKIP 1.44
Source code in pkg/python/src/pymzlib/pride.py
search ¶
search(keyword: str, page_size: int = 100, timeout: float | None = 300) -> list[PrideProjectSearchResult]
Find PRIDE projects by keyword.
The discovery entry point. Every other function here takes an accession you already have; this is the one that produces them, so you can go from a subject to a dataset without leaving Python.
Paging is handled for you: however many pages the result set spans, you get one list, with no accession repeated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keyword
|
str
|
What to search for, e.g. |
required |
page_size
|
int
|
How many hits to request per underlying API call. Only affects how the result set is fetched, never what you get back. |
100
|
timeout
|
float | None
|
Seconds to allow for the whole fetch. |
300
|
Returns:
| Type | Description |
|---|---|
list[PrideProjectSearchResult]
|
Every matching project. An empty list is a real answer — PRIDE reports no hits as an |
list[PrideProjectSearchResult]
|
empty result rather than an error, so unlike :func: |
list[PrideProjectSearchResult]
|
class: |
Raises:
| Type | Description |
|---|---|
UsageError
|
the keyword is blank, over |
BridgeError
|
PRIDE returned an error status or was unreachable. |
Note
PRIDE pages a live index with no stable cursor, so a result set that changes during a multi-page fetch shifts its own paging. A project published mid-fetch is served on two pages and deduplicated, so it comes back once; a project removed mid-fetch can fall between two pages and be missed. A search whose hits fit on one page cannot be affected.
Example
hits = search("plasmodium falciparum schizont") # doctest: +SKIP hits[0].accession, hits[0].organisms # doctest: +SKIP ('PXD070842', ['Homo sapiens (human)', 'Plasmodium falciparum (isolate 3d7)']) hits[0].matched_fields # doctest: +SKIP ['references', 'title'] files = list_files(hits[0].accession) # doctest: +SKIP
Source code in pkg/python/src/pymzlib/pride.py
pymzlib.peptidoform¶
peptidoform ¶
Peptidoform-level questions: digest an annotated protein and fragment its peptides.
The question this answers is the one a mass spectrometrist actually asks — what fragments would I see for this protein's peptides? — in one call:
>>> import pymzlib
>>> digest = pymzlib.peptidoform.fragments("P02768") # doctest: +SKIP
>>> len(digest.peptides) # doctest: +SKIP
303
The defaults are opinions, not placeholders. Tryptic with the proline rule, two missed cleavages, ETD, both termini, UniProt's annotated modifications applied. They are the choices this lab makes when it does not have a reason to choose otherwise, so the common question needs no parameters — and every one of them is reachable, because the point is to open the doors, not to hide them.
Fragment
dataclass
¶
One backbone fragment ion.
Attributes:
| Name | Type | Description |
|---|---|---|
product_type |
str
|
The ion series, e.g. |
fragment_number |
int
|
Position in the series — |
neutral_mass |
float
|
Monoisotopic neutral mass in daltons. Not an m/z: no proton has been added and no charge assumed. Fragments deliberately expose no |
neutral_loss |
float
|
Neutral loss in daltons, |
residue_position |
int
|
One-based residue position in the peptide. |
Source code in pkg/python/src/pymzlib/peptidoform.py
Peptide
dataclass
¶
One digested peptide, with its modifications and fragment ions.
Attributes:
| Name | Type | Description |
|---|---|---|
base_sequence |
str
|
The bare amino-acid sequence. |
full_sequence |
str
|
The sequence with modifications written inline, as mzLib renders them. |
monoisotopic_mass |
float
|
The neutral monoisotopic mass, modifications included. |
one_based_start |
/ one_based_end
|
Position within the parent protein. |
missed_cleavages |
int
|
How many cleavage sites the peptide spans. |
fixed_charges |
int
|
Charges the peptide carries before any protonation, from modifications
that leave a permanently charged residue. :meth: |
modifications |
list[dict[str, Any]]
|
Each applied modification. |
fragments |
list[Fragment]
|
The fragment ions for the requested dissociation type. |
Source code in pkg/python/src/pymzlib/peptidoform.py
62 63 64 65 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
mz ¶
Return the m/z of the intact peptide at a given total charge.
Two conventions are handled explicitly here, because getting either wrong is invisible in the answer.
The proton mass (1.007276), not the hydrogen atom (1.007825). The difference is 0.55 mDa — 1.1 ppm at m/z 500, which on an Orbitrap is a match versus a miss. Libraries differ on this and rarely say which they used.
Fixed charges are not double-counted. Some modifications leave the residue permanently
charged: trimethylation of a lysine ε-amine gives a quaternary ammonium, and UniProt
records the delta as 43.054227 — C₃H₇ minus an electron — rather than the neutral
43.054775. So :attr:monoisotopic_mass already carries that charge, and only
charge - fixed_charges protons are added. Adding a full complement would put a 2+
trimethylated peptide half a Thomson high, on the most important histone modification
there is.
A peptide with a fixed charge is therefore observable at that charge with no protonation
at all, which is why charge may not be below :attr:fixed_charges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
charge
|
int
|
The total charge state, at least :attr: |
required |
Source code in pkg/python/src/pymzlib/peptidoform.py
ModificationCensus
dataclass
¶
What UniProt annotates, and what could actually be used.
mzLib loads only modified residue and lipid moiety-binding region annotations; every
other feature type is dropped on feature type alone, before any mass lookup. So the census
sees the world at feature-type granularity — one entry per type in :attr:by_type, never per
modification name. On serum albumin the 24 excluded features all sit under the single type
glycosylation site; at UniProt's finer name level 22 of those 24 are specifically
N-linked (Glc) (glycation) lysine, which does have a defined mass — but the census never
surfaces that name, so "22" is a fact you confirm by reading the UniProt entry, not a number
this class reports. Read the exclusion as "wrong feature type", not "no defined mass".
The exclusion is still correct: glycation and glycosylation are labile, heterogeneous adducts, so assigning one an exact mass and a clean fragment ladder would describe a species you cannot observe. What this class exists for is that you should not have to guess it happened: for serum albumin, 14 sites are applied out of 38 annotated, and without this the 14 arrives with no indication that a rule was ever applied. See smith-chem-wisc/mzLib#1112.
Attributes:
| Name | Type | Description |
|---|---|---|
sites |
int
|
Distinct residue positions carrying at least one modification. A histone lists several alternatives at one residue — K9me1, K9me2, K9me3, K9ac are four modifications at one site — so this is always the smaller number and is not a modification count. |
applied |
int
|
Modifications actually placed on the protein. |
annotated |
int
|
Modification-like features UniProt lists. |
by_type |
list[dict[str, Any]]
|
One entry per feature type, with |
unresolved |
list[str]
|
Modification names UniProt annotated that could not be resolved to a mass — usually because the name is absent from UniProt's own ptmlist. These vanish silently otherwise: on histone H3.1, seven N6-lactoyllysine sites were dropped while the type summary still reported "modified residue … loaded". |
Source code in pkg/python/src/pymzlib/peptidoform.py
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
excluded
property
¶
Annotated features mzLib did not apply, dropped on feature type (not for want of mass).
explain ¶
A one-paragraph, human-readable account of what was used and what was not.
It names the excluded feature types and their counts — the only granularity the census
has. It never reports a modification-name-level breakdown (e.g. "22 of 24 are glycation"),
because :attr:by_type does not carry names; such a figure comes from reading the UniProt
entry, not from this census.
Source code in pkg/python/src/pymzlib/peptidoform.py
Digest
dataclass
¶
The result of digesting a protein and fragmenting its peptides.
Source code in pkg/python/src/pymzlib/peptidoform.py
truncated
property
¶
Whether any peptide hit the isoform cap, meaning the result is incomplete.
A short answer and a truncated answer look identical from the outside. Check this before treating a Peptidoform list as exhaustive.
modified_peptides
property
¶
Only the peptides carrying at least one modification.
fragments ¶
fragments(accession: str, protease: str = 'trypsin|P', dissociation: str = 'ETD', modifications: bool = True, missed_cleavages: int = 2, min_length: int = 7, max_length: int | None = None, max_modifications: int = 2, max_isoforms: int = 1024, terminus: str = 'Both', timeout: float | None = 300) -> Digest
Fetch a UniProt entry, digest it, and fragment every peptide.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
accession
|
str
|
A UniProtKB accession, e.g. |
required |
protease
|
str
|
Read this if you are coming from MaxQuant or Mascot. mzLib's |
'trypsin|P'
|
dissociation
|
str
|
|
'ETD'
|
modifications
|
bool
|
Apply UniProt's annotated modifications. Pass This once carried a caveat saying |
True
|
missed_cleavages
|
int
|
Maximum missed cleavage sites per peptide. |
2
|
min_length
|
int
|
Shortest peptide to keep. The default of 7 silently discards shorter
peptides — roughly a third of a histone digest — so pass |
7
|
max_length
|
int | None
|
Longest peptide to keep. |
None
|
max_modifications
|
int
|
Maximum modifications considered per peptide. Modification isoforms are enumerated combinatorially: histone H3.1 yields 49 bare tryptic peptides, 2,563 at two modifications and 7,040 at three. |
2
|
max_isoforms
|
int
|
Maximum modification isoforms per peptide position. mzLib's default of 1024
truncates silently when it binds — on H3.1 at four modifications it discards
about 30% of the Peptidoforms (13,700 down to 9,536). :attr: |
1024
|
terminus
|
str
|
|
'Both'
|
timeout
|
float | None
|
Seconds to allow. Large proteins with many modification isoforms take longer. |
300
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
Digest
|
class: |
Digest
|
modification count — it reports what was annotated as well as what was applied. |
Raises:
| Type | Description |
|---|---|
UsageError
|
the accession, protease, dissociation type or terminus is not recognised. |
ServiceUnavailableError
|
UniProt was unreachable. |
Example
d = fragments("P02768") # doctest: +SKIP print(d.modification_census.explain()) # doctest: +SKIP 14 of 38 annotated modification sites were applied. Excluded: 24 × glycosylation site …
Source code in pkg/python/src/pymzlib/peptidoform.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
pymzlib.flashlfq¶
flashlfq ¶
Label-free quantification with FlashLFQ: quantify a search's peptides across mzML runs.
The question this answers is the one a quant workflow actually asks — given these identifications and these runs, how much of each peptide and protein is in each run? — in one call:
>>> import pymzlib
>>> result = pymzlib.flashlfq.quantify( # doctest: +SKIP
... psms="AllPSMs.psmtsv",
... spectra=["run_3.mzML", "run_4.mzML"],
... match_between_runs=True,
... )
>>> result.peptide_count, result.protein_count # doctest: +SKIP
(354, 943)
The whole pipeline is mzLib's: the result file is read by mzLib's Readers, turned into FlashLFQ
identifications by mzLib's own converter, and quantified by FlashLfqEngine. MetaMorpheus is not
involved — mzLib does it alone.
Names follow mzLib and FlashLFQ deliberately, so a value here means the same thing it does in the
FlashLFQ source, the MetaMorpheus output columns, and the FlashLFQ paper: match_between_runs,
ppm_tolerance, mbr_ppm_tolerance, sequence, base_sequence, protein_groups,
detection_types, ProteinGroup, FlashLfqResults.
.. note::
An MSFragger psm.tsv can now be quantified here. mzLib PR #1116 converts MSFragger's
retention time to minutes at the reader, so the value FlashLFQ consumes is in the unit it
expects; the former "MSFragger writes seconds, do not quantify it" warning no longer applies.
Three more limits worth knowing before you trust a number, in the "surface it, don't hide it" spirit of the rest of pyMzLib:
- mzML only, for now. Convert
.raw/.dto mzML first; a non-mzML path is rejected up front. - A protein intensity can be
None. FlashLFQ's median-polish protein quant marks a protein NaN when its peptide matrix is degenerate (too few peptides per run, or identical intensities across runs — a real artifact documented in mzLib's own tests). NaN is not valid JSON, so it arrives here asNone— "could not be quantified" — rather than a silently wrong number. A peptide intensity, by contrast, is0.0when missing, neverNone. - For match-between-runs, read the peaks, not the peptides. The peptide roll-up
(:attr:
FlashLfqResults.peptides, mirroringQuantifiedPeptides.tsv) reports far fewer MBR transfers than actually happened — a whole run's transfers can be absent. :attr:FlashLfqResults.peaks(and :meth:FlashLfqResults.mbr_peaks) is the complete surface; :attr:FlashLfqResults.mbr_peak_countis the number to trust.
SpectraFileInfo
dataclass
¶
One quantified run, mirroring mzLib's MassSpectrometry.SpectraFileInfo.
Attributes:
| Name | Type | Description |
|---|---|---|
file_name |
str
|
The run's base name (no directory, no extension) — the key used everywhere else here to look up this run's intensity. |
full_path |
str
|
The mzML path as provided. |
condition |
str
|
The sample-group label, or |
biological_replicate |
/ technical_replicate / fraction
|
The experimental-design coordinates. |
peak_count |
int
|
Chromatographic peaks quantified in this run. |
mbr_peak_count |
int
|
Of those, how many were transferred by match-between-runs — peaks quantified
in this run for a peptide that was never identified in it. Zero unless
|
Source code in pkg/python/src/pymzlib/flashlfq.py
Peptide
dataclass
¶
A quantified peptide, mirroring FlashLFQ's Peptide.
Attributes:
| Name | Type | Description |
|---|---|---|
sequence |
str
|
The full (modified) sequence, as FlashLFQ renders it — the identity FlashLFQ quantifies. Two different modification states of one base sequence are two peptides. |
base_sequence |
str
|
The bare amino-acid sequence. |
protein_groups |
str
|
The protein group(s) this peptide belongs to, |
intensities |
dict[str, Any]
|
Run base name → intensity in that run. Missing is |
detection_types |
dict[str, str]
|
Run base name → how it was quantified there. Values FlashLFQ emits:
|
Source code in pkg/python/src/pymzlib/flashlfq.py
intensity ¶
This peptide's intensity in the named run.
0.0 — not None — means "not quantified here." (Only protein intensities are
ever None.) Treat 0.0 as missing, not as a measured absence: log-transforming it
will mislead. And note a peptide that FlashLFQ transferred into this run by
match-between-runs may still read 0.0 here — see :attr:FlashLfqResults.peaks. Returns
0.0 for a run that was never provided.
Source code in pkg/python/src/pymzlib/flashlfq.py
detection_type ¶
How this peptide was quantified in the named run ("NotDetected" if it was not).
ProteinGroup
dataclass
¶
A quantified protein group, mirroring FlashLFQ's ProteinGroup.
Attributes:
| Name | Type | Description |
|---|---|---|
protein_group |
str
|
The protein group name (accession, or |
gene_name |
str
|
The gene name, when the result file carried one. |
organism |
str
|
The organism, when the result file carried one. |
intensities |
dict[str, Any]
|
Run base name → protein intensity in that run. May be |
Source code in pkg/python/src/pymzlib/flashlfq.py
intensity ¶
This protein's intensity in the named run.
None means FlashLFQ could not resolve a number (a degenerate peptide matrix); 0.0
means simply not measured in this run.
Source code in pkg/python/src/pymzlib/flashlfq.py
Peak
dataclass
¶
One quantified chromatographic peak, mirroring FlashLFQ's ChromatographicPeak.
This is the surface to use for match-between-runs. Unlike the peptide roll-up
(:attr:Peptide.intensities, which mirrors QuantifiedPeptides.tsv and drops most MBR
transfers), the peaks fully represent every quantified peak, transferred or not. To build an
MBR-inclusive peptide × run matrix, pivot these on (sequence, file_name).
Attributes:
| Name | Type | Description |
|---|---|---|
file_name |
str
|
The run this peak was measured in (base name). |
sequence |
str
|
The full (modified) sequence of the peptide the peak was assigned to. |
base_sequence |
str
|
The bare amino-acid sequence. |
intensity |
Any
|
The peak's intensity ( |
detection_type |
str
|
|
retention_time |
Any
|
Apex retention time in minutes, or |
num_identifications |
int
|
How many peptides could explain this peak. |
protein_groups |
str
|
The protein group(s) the assigned identification(s) belong to, |
Source code in pkg/python/src/pymzlib/flashlfq.py
FlashLfqResults
dataclass
¶
The result of a quantification run, mirroring mzLib's FlashLfqResults.
Attributes:
| Name | Type | Description |
|---|---|---|
psm_file |
str
|
The absolute path of the PSM result file that was quantified. |
identification_count |
int
|
How many identifications were read from it. |
parameters |
dict[str, Any]
|
The FlashLFQ parameters actually used, echoed back with their mzLib names. |
spectra_files |
list[SpectraFileInfo]
|
One :class: |
peptides |
list[Peptide]
|
One :class: |
proteins |
list[ProteinGroup]
|
One :class: |
peaks |
list[Peak]
|
Every quantified :class: |
output_directory |
Any
|
Where the FlashLFQ TSVs were written, or |
Source code in pkg/python/src/pymzlib/flashlfq.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | |
mbr_peak_count
property
¶
Total match-between-runs peaks (transfers) across every run.
This counts transferred peaks, not distinct peptides: one peptide rescued in two runs is
two peaks here. For "how many peptides did MBR rescue," use :attr:mbr_rescued_peptide_count.
Either way, do not count MBR from the peptide roll-up (:attr:Peptide.detection_types) — it
under-counts. Zero unless match_between_runs was on.
mbr_peaks
property
¶
Exactly the peaks transferred by match-between-runs (detection_type == "MBR").
mbr_rescued_peptide_count
property
¶
Distinct peptides quantified in at least one run only by match-between-runs.
Exactly: the number of distinct sequence values among peaks whose
detection_type is "MBR". Stated in code terms because the prose version,
"peptides quantified in at least one run only by match-between-runs", is subtly
different and on real data the two diverge: peptides having both an MBR peak and
a zero-intensity MSMS peak in the same run were identified there, so they are not
rescues under the strict reading. On the K562 pair this returns 140 where the strict
count is 135. Do not read mbr_rescued_peptide_count == mbr_peak_count as
reassurance that nothing was double-counted; on that data both are 140, and they
coincide only because every MBR peak happened to carry a distinct sequence.
Distinct modified sequences among
:attr:mbr_peaks. This equals :attr:mbr_peak_count only when no peptide was rescued in
more than one run.
quantify ¶
quantify(psms: str | PathLike[str], spectra: Sequence[SpectraInput], *, normalize: bool = False, ppm_tolerance: float = 10.0, isotope_ppm_tolerance: float = 5.0, integrate: bool = False, match_between_runs: bool = False, mbr_ppm_tolerance: float = 10.0, mbr_q_value_threshold: float = 0.05, use_shared_peptides_for_protein_quant: bool = False, bayesian_protein_quant: bool = False, use_pep_q_value: bool = False, max_threads: int = -1, output_directory: str | PathLike[str] | None = None, timeout: float | None = None) -> FlashLfqResults
Quantify a search's peptides across mzML runs with FlashLFQ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
psms
|
str | PathLike[str]
|
Path to a PSM result file — a MetaMorpheus |
required |
spectra
|
Sequence[SpectraInput]
|
The mzML runs. Each entry is either a path ( |
required |
normalize
|
bool
|
Normalize intensities across runs (FlashLFQ |
False
|
ppm_tolerance
|
float
|
Mass tolerance for peak-finding, in ppm ( |
10.0
|
isotope_ppm_tolerance
|
float
|
Mass tolerance for isotope-envelope matching, in ppm. |
5.0
|
integrate
|
bool
|
Integrate peak intensities rather than taking the apex. FlashLFQ recommends leaving this off. |
False
|
match_between_runs
|
bool
|
Quantify a peptide in a run where it was not identified, by transferring
the identification from a run where it was ( |
False
|
mbr_ppm_tolerance
|
float
|
Mass tolerance for MBR transfers, in ppm. |
10.0
|
mbr_q_value_threshold
|
float
|
The q-value cutoff below which an MBR transfer is accepted. |
0.05
|
use_shared_peptides_for_protein_quant
|
bool
|
Let peptides shared between protein groups contribute
to protein quant ( |
False
|
bayesian_protein_quant
|
bool
|
Run FlashLFQ's Bayesian protein-fold-change engine. |
False
|
use_pep_q_value
|
bool
|
Filter identifications on PEP q-value rather than q-value. |
False
|
max_threads
|
int
|
Worker threads; This is not only a performance knob - it changes results. With |
-1
|
output_directory
|
str | PathLike[str] | None
|
If given, FlashLFQ also writes |
None
|
timeout
|
float | None
|
Seconds to allow. Large experiments legitimately take a while; |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
FlashLfqResults
|
class: |
Raises:
| Type | Description |
|---|---|
UsageError
|
an argument is malformed, a run is not mzML, an mzML is missing, or the PSM file names a run with no mzML provided. |
BridgeError
|
FlashLFQ itself failed. |
Source code in pkg/python/src/pymzlib/flashlfq.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | |
median_polish ¶
median_polish(peptides: str | PathLike[str], *, design: Sequence[DesignInput] | None = None, use_shared_peptides: bool = False, output_directory: str | PathLike[str] | None = None, timeout: float | None = None) -> list[ProteinGroup]
Roll a QuantifiedPeptides.tsv up to protein intensities with FlashLFQ's median polish.
This is the second half of :func:quantify on its own: given a peptide table FlashLFQ already
wrote — its Intensity_<run> and Detection Type_<run> columns — it rebuilds the FlashLFQ
peptide/protein object graph and runs the exact same median-polish protein quant
(CalculateProteinResultsMedianPolish), without re-reading any mzML. Reach for it to
re-quantify proteins under a different experimental design, or with shared peptides toggled,
without paying for peak-finding again::
>>> import pymzlib
>>> proteins = pymzlib.flashlfq.median_polish( # doctest: +SKIP
... "QuantifiedPeptides.tsv",
... design=[
... {"file_name": "run_3", "condition": "control", "biological_replicate": 0},
... {"file_name": "run_4", "condition": "treated", "biological_replicate": 0},
... ],
... )
>>> proteins[0].intensity("control_1") # doctest: +SKIP
3005.6
The returned objects are ordinary :class:ProteinGroup\ s, so their intensity semantics are the
ones documented there and on :func:quantify: an intensity is None where median polish could
not resolve a number (a degenerate peptide matrix), 0.0 where the protein was simply not
measured in that sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
peptides
|
str | PathLike[str]
|
Path to a FlashLFQ |
required |
design
|
Sequence[DesignInput] | None
|
The experimental design, one mapping per run. Each needs a |
None
|
use_shared_peptides
|
bool
|
Let peptides shared between protein groups contribute to protein quant
(FlashLFQ's |
False
|
output_directory
|
str | PathLike[str] | None
|
If given, also write a FlashLFQ |
None
|
timeout
|
float | None
|
Seconds to allow; |
None
|
Returns:
| Type | Description |
|---|---|
list[ProteinGroup]
|
A list of :class: |
list[ProteinGroup]
|
attr: |
list[ProteinGroup]
|
is given, |
Raises:
| Type | Description |
|---|---|
UsageError
|
|
BridgeError
|
the reconstruction or quantification itself failed. |
Source code in pkg/python/src/pymzlib/flashlfq.py
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | |
pymzlib.readers¶
readers ¶
Read mass-spectrometry data files and proteomics search results: what a file is, what you can do with it, and its records.
Spectra files are read here too, not just search output. :func:read_spectra reads
mzML, Thermo .raw, Bruker .d, timsTOF .d, MGF and msalign - scan headers always,
peaks on request::
>>> scans = pymzlib.readers.read_spectra("run.mzML", peaks=True) # doctest: +SKIP
>>> scans.scan_count, scans.columns["retention_time"][:2] # doctest: +SKIP
(455, [0.0011, 0.0285])
mzLib recognises 32 file types in all - the instrument and deconvolution formats above, plus the output of a dozen search tools: MetaMorpheus, MSFragger, TopPIC, TopFD, MsPathFinderT, Crux, Casanovo, FlashDeconv, Dinosaur, DIA-NN, FlashLFQ - and dispatches each to a parser it maintains. This module asks it what a path is::
>>> import pymzlib
>>> info = pymzlib.readers.identify("psm.tsv") # doctest: +SKIP
>>> info.file_type, info.views # doctest: +SKIP
('MsFraggerPsm', ['quantifiable'])
...and reads it, whatever it turns out to be::
>>> table = pymzlib.readers.read_records("toppic_prsm.tsv") # doctest: +SKIP
>>> table.record_type, len(table.column_names) # doctest: +SKIP
('ToppicPrsm', 36)
Every one of the 32 formats is readable - :func:read_records reads any of them. What differs
between formats is not whether you can read them but what the columns mean, and that is what
:attr:FileInfo.views tells you. It is tempting to describe mzLib as reading 32 formats into one
uniform shape; it does not. They fall into disjoint families, and several belong to no family at
all:
+---------------------+-------+------------------------+---------------------------------------+
| view | types | function | columns |
+=====================+=======+========================+=======================================+
| "quantifiable" | 4 | :func:read_results | uniform: sequence, RT, charge, mass, |
| | | | protein groups. What |
| | | | :func:pymzlib.flashlfq.quantify |
| | | | consumes. |
+---------------------+-------+------------------------+---------------------------------------+
| "ms1_features" | 2 | :func:read_features | uniform: m/z, charge, RT range, |
| | | | intensity, isotope count. |
+---------------------+-------+------------------------+---------------------------------------+
| "spectral_match"| 4 | :func:read_matches | uniform: scan, sequences, accession, |
| | | | decoy flag, modifications. |
+---------------------+-------+------------------------+---------------------------------------+
| "spectra" | 7 | :func:read_spectra | uniform: scan headers, and peaks on |
| | | | request. |
+---------------------+-------+------------------------+---------------------------------------+
| (any) | 32 | :func:read_records | this format's own fields, under |
| | | | mzLib's names. Not uniform. |
+---------------------+-------+------------------------+---------------------------------------+
views == [] is a real and common answer - fifteen types have it. TopPIC, Crux, MSFragger's
peptide and protein tables and the FlashDeconv formats each parse into their own record type with
nothing in common. mzLib reads them and so does :func:read_records; there is simply no uniform
view to project them onto, and inventing one here would mean publishing a schema mzLib does not
have.
So: use a typed view when you need numbers that mean the same thing across files, and
:func:read_records when you need everything a format has. A .psmtsv read through
:func:read_results gives 10 comparable columns; the same file through :func:read_records gives
73, including the q-values and scores the uniform view does not carry.
Call :func:formats for the whole table. It is enumerated from mzLib rather than transcribed, so it
cannot drift from what mzLib actually dispatches.
Three things this module deliberately does not tell you, in the "surface it, don't hide it" spirit of the rest of pyMzLib:
- Which tool wrote the file. mzLib has a
Softwareproperty that looks like the answer and is not: readers carry their software constant on a constructor that mzLib's own file factory does not use, so the value is unset for everything the factory returns - and it is not reliably set on the other constructor either. Rather than reconstruct a plausible answer, there is nosoftwarefield. :attr:FileInfo.file_typealready names the tool. -
Whether the numbers inside mean the same thing across formats. They do not, and this is the trap most likely to produce a wrong result. mzLib's result-file readers pass through whatever the tool wrote: MetaMorpheus retention times are in minutes and MSFragger's are too (mzLib PR #1116 converts them at the reader), but TopPIC's are still in seconds, and TopFD changed from seconds to minutes between v1.6.2 and v1.7.0 within the same file type. Likewise
is_decoyis hardcodedFalsefor MSFragger, which means "mzLib cannot tell" rather than "target" - MSFragger'spsm.tsvcarries no target/decoy column at all - sois_decoyarrives asNonefor that format rather than a fabricatedFalse.monoisotopic_massis the theoretical peptide mass in both formats, never the observed precursor mass. Identifying a file is safe; comparing raw fields across formats is not. -
Anything about confidence. There is no q-value, PEP or score in this view, because
IQuantifiableRecordcarries only what FlashLFQ needs. Nothing you get back is FDR-filtered, even though every one of these files records confidence somewhere. Filter before you report.
.. note::
That units mismatch was not hypothetical, and the fix shows where such things belong. Passing an
MSFragger psm.tsv to :func:pymzlib.flashlfq.quantify used to return near-zero intensities,
because FlashLFQ read the seconds as minutes and searched for each peptide about sixty times too
late in the gradient. It was fixed upstream in mzLib (#1116
<https://github.com/smith-chem-wisc/mzLib/pull/1116>_, converting at the reader) rather than
papered over here, so every mzLib consumer benefits and this library's caveat and
retention_time_unit changed with it. That is the standing rule: a value whose meaning or
availability is wrong is repaired in the core contract, and a binding discloses rather than
repairs.
Format
dataclass
¶
One file type mzLib can recognise.
Attributes:
| Name | Type | Description |
|---|---|---|
file_type |
str
|
mzLib's |
extension |
Any
|
The extension or filename suffix mzLib dispatches on, e.g. |
reader |
Any
|
The name of the mzLib class that parses it, for cross-referencing the mzLib source. |
views |
list[str]
|
The uniform views this format supports - see the module docstring. Often empty. |
Source code in pkg/python/src/pymzlib/readers.py
is_quantifiable
property
¶
Whether this format offers the cross-format record view (and so feeds FlashLFQ).
FileInfo
dataclass
¶
What a particular file is, and what can be done with it.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The absolute path that was identified. |
file_type |
str
|
mzLib's |
extension |
Any
|
The extension mzLib dispatched on. |
reader |
Any
|
The mzLib class that would parse it. |
views |
list[str]
|
The uniform views this file supports - see the module docstring. Often empty, which means mzLib can read the file but offers no cross-format projection of it. |
Source code in pkg/python/src/pymzlib/readers.py
is_quantifiable
property
¶
Whether this file offers the cross-format record view.
When True, the path can be passed straight to :func:pymzlib.flashlfq.quantify as
psms. When False, mzLib can still read the file - it simply has no uniform view,
so quantification would fail on it.
WrittenTable
dataclass
¶
Where :func:read_results wrote a table, when asked to write one instead of returning it.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The absolute path written. |
format |
str
|
Always |
row_count |
int
|
Rows written, excluding the header. |
Source code in pkg/python/src/pymzlib/readers.py
ResultRecords
dataclass
¶
Bases: _Table
The uniform record view of a result file.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The absolute path that was read. |
file_type |
str
|
mzLib's |
record_count |
int
|
Records in the whole file, regardless of |
returned_count |
int
|
Records actually carried back in :attr: |
offset |
int
|
The offset that was applied. |
truncated |
bool
|
Whether records were left behind, by either |
retention_time_unit |
str
|
The unit :attr: |
rows_not_read |
Any
|
Data rows in the file that did not become records - mzLib drops a malformed
row silently, so a non-zero value here means the file is partly unreadable and the
table is incomplete. |
caveats |
list[str]
|
What the uniform view cannot be trusted to mean for this format. Empty for some formats, not for others; each entry cites the mzLib source it came from. Worth printing before comparing anything across formats - this is where you learn that, e.g., TopPIC retention times are seconds while MetaMorpheus's and MSFragger's are minutes. |
column_names |
list[str]
|
The field names, in order. |
columns |
Any
|
Field name -> list of values, one entry per record - the shape |
output |
Any
|
Where the table was written, or |
Source code in pkg/python/src/pymzlib/readers.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
retention_time_in_minutes
property
¶
retention_time converted to minutes, whatever unit the format wrote.
The conversion you would otherwise write by hand, using
:attr:retention_time_unit. Raises if the unit is "unknown" rather than guessing -
a silently unconverted axis is the specific mistake this module exists to prevent.
NativeRecords
dataclass
¶
Bases: _Table
A result file read into its own fields, whatever format it is.
What :func:read_records returns. Unlike every other result type in this module, the columns
here are not uniform: they are the fields of this format's own mzLib record type, under
mzLib's own names in snake_case. A TopPIC file gives you TopPIC's thirty-six columns; a Crux
file gives you Crux's twenty-three. Always read :attr:column_names rather than assuming.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The absolute path that was read. |
file_type |
str
|
mzLib's |
reader |
Any
|
The mzLib class that parsed it. |
record_type |
str
|
The mzLib record class the columns came from, e.g. |
views |
list[str]
|
The uniform views this file also supports, if any - see the module docstring. |
record_count |
int
|
Records in the whole file, regardless of |
returned_count |
int
|
Records carried back in :attr: |
offset |
int
|
The offset that was applied. |
truncated |
bool
|
Whether records were left behind, by either |
excluded_fields |
list[dict[str, Any]]
|
Fields of the record type that could not become columns, each with the reason. Nested objects and dictionaries have no faithful column shape, and inventing one would mean publishing a schema mzLib does not have. Listed rather than dropped, so an absent column is never mistaken for an absent field. |
failed_fields |
list[str]
|
Fields that raised while being read, with the exception type. Several
mzLib properties are computed and assume a UniProt-style FASTA header - Crux's and
MsPathFinderT's |
column_names |
list[str]
|
The field names, in order - base-class fields first, then declared ones. |
columns |
Any
|
Field name -> list of values, one entry per record. |
output |
Any
|
Where the table was written, or |
Source code in pkg/python/src/pymzlib/readers.py
FeatureRecords
dataclass
¶
Bases: _Table
Deconvolved MS1 features, in the cross-format ms1_features view.
What :func:read_features returns. Columns are mz, charge, retention_time_start,
retention_time_end, intensity and number_of_isotopes - the same for every format
that offers the view, so they are comparable across files, subject to
:attr:retention_time_unit.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The absolute path that was read. |
file_type |
str
|
mzLib's |
record_count |
int
|
Features in the whole file. For |
returned_count |
int
|
Features carried back in :attr: |
offset |
int
|
The offset that was applied. |
truncated |
bool
|
Whether features were left behind, by either |
retention_time_unit |
str
|
|
caveats |
list[str]
|
What this view cannot be trusted to mean for this format, each citing the mzLib source it came from. |
column_names |
list[str]
|
The field names, in order. |
columns |
Any
|
Field name -> list of values. |
output |
Any
|
Where the table was written, or |
Source code in pkg/python/src/pymzlib/readers.py
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | |
retention_time_start_in_minutes
property
¶
retention_time_start in minutes, or a raised error if the unit is unknown.
Raises rather than guessing. For _ms1.feature the unit genuinely is unknown, and a
silently unconverted time axis is the specific mistake this module exists to prevent -
mzLib's own deconvolution code guesses here, and this will not.
retention_time_end_in_minutes
property
¶
retention_time_end in minutes, or a raised error if the unit is unknown.
MatchRecords
dataclass
¶
Bases: _Table
Identifications, in the cross-format spectral_match view.
What :func:read_matches returns. Columns are file_name_without_extension,
one_based_scan_number, base_sequence, full_sequence, accession, is_decoy,
modifications and modification_count.
Nothing here is FDR-filtered, and unlike the quantifiable view there is not even a hint of
confidence to filter on - mzLib's ISpectralMatch carries identity fields only. Every format
that offers this view records an E-value or q-value in columns :func:read_records will give
you. Filter before you report.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The absolute path that was read. |
file_type |
str
|
mzLib's |
record_count |
int
|
Matches in the whole file. |
returned_count |
int
|
Matches carried back in :attr: |
offset |
int
|
The offset that was applied. |
truncated |
bool
|
Whether matches were left behind, by either |
caveats |
list[str]
|
What this view cannot be trusted to mean for this format - that MsPathFinderT
infers decoys from an |
column_names |
list[str]
|
The field names, in order. |
columns |
Any
|
Field name -> list of values. |
output |
Any
|
Where the table was written, or |
Source code in pkg/python/src/pymzlib/readers.py
ScanRecords
dataclass
¶
Bases: _Table
Scan headers - and optionally peaks - from a spectra file.
What :func:read_spectra returns. Retention times here are in minutes for every format:
mzLib's spectra readers convert at the boundary, unlike its result-file readers, which pass the
tool's own unit through untouched.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The absolute path that was read. |
file_type |
str
|
mzLib's |
reader |
Any
|
The mzLib class that parsed it, e.g. |
scan_count |
int
|
Scans in the whole file, before any |
ms_order |
Any
|
The MS level filtered to, or |
record_count |
int
|
Scans that passed the |
returned_count |
int
|
Scans carried back in :attr: |
offset |
int
|
The offset that was applied. |
truncated |
bool
|
Whether scans were left behind, by either |
peaks_included |
bool
|
Whether |
retention_time_unit |
str
|
Always |
caveats |
list[str]
|
What this view cannot be trusted to mean for this format - that msalign holds deconvolved neutral masses rather than m/z, that MGF scan numbers come from a title line, that Bruker needs Windows-x64 native libraries. |
column_names |
list[str]
|
The field names, in order. |
columns |
Any
|
Field name -> list of values. When :attr: |
output |
Any
|
Where the table was written, or |
Source code in pkg/python/src/pymzlib/readers.py
total_ion_current
property
¶
The total_ion_current column, for the commonest plot there is.
formats ¶
Every file type mzLib can recognise.
Enumerated from mzLib itself rather than from a list maintained here, so it reflects the installed version and cannot go stale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
Seconds to allow. |
60
|
Returns:
| Name | Type | Description |
|---|---|---|
One |
list[Format]
|
class: |
Example
quantifiable = [f.file_type for f in formats() if f.is_quantifiable] # doctest: +SKIP quantifiable # doctest: +SKIP ['psmtsv', 'osmtsv', 'MsFraggerPsm', 'DiaNnReport']
Source code in pkg/python/src/pymzlib/readers.py
identify ¶
Identify a result file without parsing its contents.
Cheap by design: mzLib resolves the type and stops, so identifying a million-row file costs no
more than identifying an empty one. It is not, however, pure - mzLib disambiguates a bare
.tsv by reading its first line, a .mztab by its first five, and a Bruker .d by which
analysis file the directory holds. An unreadable file will therefore raise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Path to a result or spectra file. A Bruker |
required |
timeout
|
float | None
|
Seconds to allow. |
60
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
FileInfo
|
class: |
Raises:
| Type | Description |
|---|---|
UsageError
|
the path is blank, does not exist, or is not a file type mzLib recognises.
mzLib has no "unknown" result - a file is dispatchable or it is an error - so use
:func: |
Example
info = identify("AllPSMs.psmtsv") # doctest: +SKIP info.file_type, info.is_quantifiable # doctest: +SKIP ('psmtsv', True)
Source code in pkg/python/src/pymzlib/readers.py
read_results ¶
read_results(path: str | PathLike[str], *, limit: int | None = None, offset: int = 0, out: str | PathLike[str] | None = None, timeout: float | None = None) -> ResultRecords
Read a result file into the uniform record view.
Only the three file types offering the "quantifiable" view can be read this way - check
:func:identify first, or catch the error. A file without the view is rejected with a message
naming the views it does have.
There is no default row limit. A result file can carry a million rows, and truncating by
default would mean the ordinary call returns a table that looks complete and is not. For a large
file use out rather than paging: see the note on offset below.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Path to a MetaMorpheus |
required |
limit
|
int | None
|
Maximum records to return. |
None
|
offset
|
int
|
Records to skip. This is a window, not a cursor. mzLib materializes the whole
file on every call - its readers look lazy and are not - so paging re-reads and
re-parses the file once per page. For a large file, one call with |
0
|
out
|
str | PathLike[str] | None
|
Write the records to this path as a tab-separated table and return only a summary, instead of carrying them back in the envelope. The intended path for large files, not an escape hatch. Tab-separated because these fields contain commas. |
None
|
timeout
|
float | None
|
Seconds to allow. A large file legitimately takes a while; |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
ResultRecords
|
class: |
ResultRecords
|
formats. |
Raises:
| Type | Description |
|---|---|
UsageError
|
the path is blank, missing, not a recognised format, or has no quantifiable view. |
Example
r = read_results("AllPSMs.psmtsv") # doctest: +SKIP r.record_count, r.truncated # doctest: +SKIP (8, False) import pandas as pd # doctest: +SKIP pd.DataFrame(r.columns) # doctest: +SKIP
Source code in pkg/python/src/pymzlib/readers.py
read_records ¶
read_records(path: str | PathLike[str], *, limit: int | None = None, offset: int = 0, out: str | PathLike[str] | None = None, timeout: float | None = None) -> NativeRecords
Read any file mzLib recognises, into that format's own fields.
This is the exhaustive verb: if :func:identify succeeds on a path, this reads it. All
thirty-two file types, including the fifteen that belong to no cross-format view at all -
TopPIC, Crux, MSFragger's peptide and protein tables, the FlashDeconv formats - which no other
function here can touch.
The columns are not uniform, by design. They are this format's own mzLib record fields,
under mzLib's own names in snake_case: a TopPIC file gives thirty-six columns, a Crux file
twenty-three, an experiment annotation five. Read :attr:NativeRecords.column_names, and use
:func:read_results, :func:read_features or :func:read_matches when you need columns that
mean the same thing across formats.
Nothing is silently dropped. A field that could not become a column is named in
:attr:NativeRecords.excluded_fields, and one that raised while being read is named in
:attr:NativeRecords.failed_fields - so a missing column never has to be guessed at.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Path to any file mzLib recognises. A Bruker |
required |
limit
|
int | None
|
Maximum records to return. |
None
|
offset
|
int
|
Records to skip. A window, not a cursor - see :func: |
0
|
out
|
str | PathLike[str] | None
|
Write a tab-separated table here and return only a summary. |
None
|
timeout
|
float | None
|
Seconds to allow. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
NativeRecords
|
class: |
Raises:
| Type | Description |
|---|---|
UsageError
|
the path is blank, missing, or not a file type mzLib recognises. |
Example
r = read_records("toppic_prsm.tsv") # doctest: +SKIP r.record_type, len(r.column_names) # doctest: +SKIP ('ToppicPrsm', 36) import pandas as pd # doctest: +SKIP pd.DataFrame(r.columns)[["e_value", "q_value_spectrum_level"]] # doctest: +SKIP
Source code in pkg/python/src/pymzlib/readers.py
read_features ¶
read_features(path: str | PathLike[str], *, limit: int | None = None, offset: int = 0, out: str | PathLike[str] | None = None, timeout: float | None = None) -> FeatureRecords
Read deconvolved MS1 features, in the cross-format ms1_features view.
Two file types offer it: TopFD/FLASHDeconv _ms1.feature and Dinosaur .feature.tsv. A
file without the view is rejected with a message naming the views it does have.
One row is not one line of the file for _ms1.feature. mzLib expands each deconvolved
feature into one single-charge feature per charge in its recorded range, so a hundred-feature
file can read as a thousand rows. Dinosaur is one-for-one. Both facts are in
:attr:FeatureRecords.caveats, and :func:read_records gives the file's own rows either way.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Path to a |
required |
limit
|
int | None
|
Maximum features to return. |
None
|
offset
|
int
|
Features to skip. |
0
|
out
|
str | PathLike[str] | None
|
Write a tab-separated table here and return only a summary. |
None
|
timeout
|
float | None
|
Seconds to allow. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
FeatureRecords
|
class: |
FeatureRecords
|
comparing times - it is |
Raises:
| Type | Description |
|---|---|
UsageError
|
the path is blank, missing, unrecognised, or has no |
Example
f = read_features("sample_ms1.feature") # doctest: +SKIP f.record_count, f.retention_time_unit # doctest: +SKIP (25, 'unknown')
Source code in pkg/python/src/pymzlib/readers.py
read_matches ¶
read_matches(path: str | PathLike[str], *, limit: int | None = None, offset: int = 0, out: str | PathLike[str] | None = None, timeout: float | None = None) -> MatchRecords
Read identifications, in the cross-format spectral_match view.
Four file types offer it: MsPathFinderT's targets, decoys and combined results, and Casanovo's
.mztab. These are the identification formats that share no file-level interface, so
:func:read_results cannot reach them.
Nothing here is FDR-filtered, and there is no confidence column to filter on - mzLib's
ISpectralMatch carries identity fields only. Every one of these formats records an E-value
or q-value that :func:read_records will give you. Filter before you report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Path to an MsPathFinderT |
required |
limit
|
int | None
|
Maximum matches to return. |
None
|
offset
|
int
|
Matches to skip. |
0
|
out
|
str | PathLike[str] | None
|
Write a tab-separated table here and return only a summary. |
None
|
timeout
|
float | None
|
Seconds to allow. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
MatchRecords
|
class: |
MatchRecords
|
it is inferred from a name prefix for MsPathFinderT and is |
Example
m = read_matches("results_IcTda.tsv") # doctest: +SKIP m.record_count, m.columns["modifications"] # doctest: +SKIP (6, ['', '12:Oxidation on M', '', '', '4:Acetylation on K', ''])
Source code in pkg/python/src/pymzlib/readers.py
read_spectra ¶
read_spectra(path: str | PathLike[str], *, limit: int | None = None, offset: int = 0, ms_order: int | None = None, peaks: bool = False, out: str | PathLike[str] | None = None, timeout: float | None = None) -> ScanRecords
Read the scans of a spectra file: headers always, peaks on request.
Seven file types offer the spectra view: .mzML, .mgf, _ms1.msalign,
_ms2.msalign, Thermo .raw, Bruker .d and timsTOF .d.
Peaks are opt-in and should stay that way unless you need them. A scan header is tens of
bytes; its peak list is thousands, and a mid-size mzML holds tens of thousands of scans. With
peaks=True the mz and intensity columns each become a list of arrays, one per scan -
so pair it with limit, ms_order or out.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Path to a spectra file. A Bruker |
required |
limit
|
int | None
|
Maximum scans to return. |
None
|
offset
|
int
|
Scans to skip, applied after |
0
|
ms_order
|
int | None
|
Keep only scans at this MS level - |
None
|
peaks
|
bool
|
Include the |
False
|
out
|
str | PathLike[str] | None
|
Write a tab-separated table here and return only a summary. With |
None
|
timeout
|
float | None
|
Seconds to allow. Reading a large |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
ScanRecords
|
class: |
Raises:
| Type | Description |
|---|---|
UsageError
|
the path is blank, missing, unrecognised, has no |
Example
s = read_spectra("run.mzML", ms_order=2, limit=5) # doctest: +SKIP s.scan_count, s.record_count # doctest: +SKIP (14238, 11902) s.columns["selected_ion_mz"] # doctest: +SKIP [447.7391, 551.2903, 638.8215, 712.3344, 805.9012]
Source code in pkg/python/src/pymzlib/readers.py
pymzlib.sdrf¶
sdrf ¶
Read SDRF-Proteomics experimental-design files, and pool several into one table.
Every other reader in pyMzLib answers what did the search find. SDRF answers what was searched - which sample, which organism part, which replicate, which instrument settings - and that is the half you need to group results across experiments::
>>> import pymzlib
>>> doc = pymzlib.sdrf.read("PXD000070.sdrf.tsv") # doctest: +SKIP
>>> doc.row_count, len(doc.columns) # doctest: +SKIP
(6, 31)
>>> doc.value("characteristics[organism]") # doctest: +SKIP
['plasmodium falciparum', 'plasmodium falciparum', ...]
Pool several experiments into one analysis table, giving each a name you choose::
>>> pooled = pymzlib.sdrf.pool({ # doctest: +SKIP
... "PXD000070.sdrf.tsv": "malaria",
... "PXD026824.sdrf.tsv": "colon",
... })
>>> pooled.document_count, pooled.row_count # doctest: +SKIP
(2, 24)
This module is row-major, and every other reader here is columnar. That is not a style
choice. :func:pymzlib.readers.read_records and friends hand back columns, a
name-to-values dict, because their column names are a schema. SDRF's are data, and they
repeat: 649 files in the curated corpus carry comment[modification parameters] more than
once, up to eight times in one file, and one file repeats an empty name 23 times. A dict keyed
by name would silently keep one occurrence and drop the rest. So :attr:SdrfDocument.columns is
a list that may contain duplicates, :attr:SdrfDocument.rows is a list of cell lists, and
position is what links them. Use :meth:SdrfDocument.value for the first cell under a name and
:meth:SdrfDocument.all for every one.
Three things worth knowing before you index anything:
Rows are ragged. len(row) may be less than len(columns). Real files are like this -
PXD059974 in mzLib's own fixtures has a 46-column header with 17 of its 23 rows carrying 42
cells - and mzLib preserves it rather than padding, so the file round-trips byte for byte.
:meth:SdrfDocument.value returns None for a position a row does not reach.
Cells are raw strings, never interpreted. The SDRF key=value grammar
("NT=Oxidation;AC=UNIMOD:35") arrives exactly as written. It is not decoded, because it
cannot be told apart from a cell that merely contains = and ; - comment[file uri]
routinely carries pre-signed download URLs whose query strings contain Signature= and
Expires=.
A reserved word is a real value. "not available" and "not applicable" mean the
experiment stated an absence, which is not the same as a column the document does not have.
None means the latter. Do not collapse the two.
What this module does not do yet is validate. mzLib models SDRF's structural rules in
SdrfValidator and its vocabulary-drift rules in SdrfDriftLint. Both have been public
since mzLib #1207, which the pinned mzLib (1.0.589) includes, so the bridge can call them. They
are not exposed yet. When they are, the rules will be projected once, in the bridge, for all
three bindings - a second implementation here is exactly the per-binding repair pyMzLib exists
to avoid. Until then, this module reads, pools and reports honestly, and makes no claim about
whether a document is correct.
WrittenSdrf
dataclass
¶
Where :func:pool wrote the merged document, when asked to write one.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The path written. |
row_count |
int
|
Rows written - the whole merged document, not the windowed slice. |
Source code in pkg/python/src/pymzlib/sdrf.py
SdrfDocument
dataclass
¶
One SDRF-Proteomics document: an ordered header, and rows of raw cells.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The path that was read. |
columns |
list[str]
|
The column names, verbatim and in document order. Names may repeat, and the
order is part of the document, so this is a list rather than a set. Names are never
case-normalised: the corpus contains |
rows |
list[list[str]]
|
One list of cells per row. Ragged: a row may be shorter than |
row_count |
int
|
Rows in the whole document, regardless of |
returned_count |
int
|
Rows actually carried back in :attr: |
offset |
int
|
The offset that was applied. |
truncated |
bool
|
Whether rows were left behind, by either |
caveats |
list[str]
|
What this document's data cannot tell you about itself - raggedness, repeated names, reserved words. Worth printing the first time you read an unfamiliar file. |
Source code in pkg/python/src/pymzlib/sdrf.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 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 | |
records
property
¶
The rows as dicts, for the common case of a document with no repeated column names.
Lossy when a name repeats - later positions overwrite earlier ones - which is exactly
why it is not the primary shape. :attr:has_repeated_columns says whether that applies to
this document. For a repeating document use :meth:all instead.
has_repeated_columns
property
¶
Whether any column name appears more than once - see :attr:records.
ragged_row_count
property
¶
How many returned rows carry fewer cells than there are columns.
index_of ¶
The position of the first column with this name, or -1 if absent.
Comparison is exact and case-sensitive, matching the SDRF specification and mzLib.
Source code in pkg/python/src/pymzlib/sdrf.py
indexes_of ¶
Every position carrying this name, in document order. Empty when the column is absent.
value ¶
The first cell under column, one entry per returned row.
None means the document does not have this column, or the row is too short to reach
it. It does not mean "empty": the SDRF reserved words "not available" and
"not applicable" are real values that an experiment chose to write, and they come back
as themselves.
Example
doc.value("characteristics[disease]") # doctest: +SKIP ['not applicable', 'not applicable', ...]
Source code in pkg/python/src/pymzlib/sdrf.py
all ¶
Every cell under column, one list per returned row.
The accessor for a multi-cardinality column such as comment[modification parameters],
which legitimately repeats - up to eight times in one corpus file. Positions a row is too
short to reach are skipped rather than reported as None, so each inner list holds only
cells that exist.
Source code in pkg/python/src/pymzlib/sdrf.py
PooledSdrf
dataclass
¶
Bases: SdrfDocument
Several SDRF documents merged into one table.
Everything :class:SdrfDocument offers, plus where the rows came from. path is empty:
a pooled table is not a file that was read.
Attributes:
| Name | Type | Description |
|---|---|---|
document_count |
int
|
How many documents were pooled. |
paths |
list[str]
|
The paths pooled, in the order given. |
labels |
list[str]
|
The provenance label used for each, in the same order - either what you supplied
or mzLib's |
written |
WrittenSdrf | None
|
Where the merged document was written, when |
Source code in pkg/python/src/pymzlib/sdrf.py
source_documents ¶
The provenance label of each returned row - which document it came from.
read ¶
read(path: str | PathLike[str], *, limit: int | None = None, offset: int = 0, timeout: float | None = 60) -> SdrfDocument
Read one SDRF-Proteomics file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Path to a |
required |
limit
|
int | None
|
Maximum rows to return. |
None
|
offset
|
int
|
Rows to skip. |
0
|
timeout
|
float | None
|
Seconds to allow. |
60
|
Returns:
| Name | Type | Description |
|---|---|---|
An |
SdrfDocument
|
class: |
Raises:
| Type | Description |
|---|---|
UsageError
|
the path is blank, or the file is missing or unreadable as SDRF. |
Example
doc = read("PXD000070.sdrf.tsv") # doctest: +SKIP doc.value("characteristics[organism part]")[0] # doctest: +SKIP 'human erythrocytes' doc.all("comment[modification parameters]")[0] # doctest: +SKIP ['NT=Carbamidomethyl;AC=UNIMOD:4;TA=C;MT=Fixed', 'NT=Oxidation;AC=UNIMOD:35;...']
Source code in pkg/python/src/pymzlib/sdrf.py
pool ¶
pool(documents: Sequence[str] | Mapping[str, str], *, out: str | PathLike[str] | None = None, limit: int | None = None, offset: int = 0, timeout: float | None = 60) -> PooledSdrf
Merge several SDRF documents into one analysis table.
Columns are the union of every document's, ordered by SDRF's own block structure, and a name
that repeats is carried at the highest multiplicity any single document used, so nothing is
dropped. A cell a document did not have is filled with the reserved word "not available",
and a comment[source document] column records which document each row came from.
Give your documents names. Pass a mapping to choose them. With a plain sequence, mzLib
falls back to containing-folder/file-stem, which depends on where the files happen to sit -
so the same two documents pooled from a different directory produce a different table. The
returned :attr:PooledSdrf.caveats says so when that fallback was used.
The result is an analysis table, not something to deposit. source name + assay
name + comment[label] is unique within one document, but two experiments may both have a
"Sample 1", so a pooled table will usually violate SDRF's uniqueness rule. Use the
source-document column as part of any key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
Sequence[str] | Mapping[str, str]
|
The files to pool - a sequence of paths, or a |
required |
out
|
str | PathLike[str] | None
|
Write the merged document here as SDRF. The whole document is written regardless
of |
None
|
limit
|
int | None
|
Maximum rows to return over the wire. |
None
|
offset
|
int
|
Rows to skip. |
0
|
timeout
|
float | None
|
Seconds to allow. |
60
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
PooledSdrf
|
class: |
Raises:
| Type | Description |
|---|---|
UsageError
|
no documents were given, a path is missing, or a label is blank. |
Example
pooled = pool({"a.sdrf.tsv": "malaria", "b.sdrf.tsv": "colon"}) # doctest: +SKIP set(pooled.source_documents()) # doctest: +SKIP
Source code in pkg/python/src/pymzlib/sdrf.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
Errors and diagnostics¶
Everything pyMzLib raises inherits from PyMzLibError, so a single except catches all of it.
_bridge ¶
Locating and invoking the bundled mzLib bridge executable.
This module is the only place in pyMzLib that knows the bridge exists. Everything above it sees ordinary Python functions and objects. That boundary is deliberate: the transport (today, a self-contained .NET executable invoked per call) can be replaced by an in-process binding or a long-lived local server without any public API changing.
Nothing here imports a third-party package. pyMzLib declares no runtime dependencies, so it cannot participate in a dependency conflict inside anyone's environment.
PyMzLibError ¶
UsageError ¶
Bases: PyMzLibError, ValueError
A call was malformed — a missing or invalid argument. Raised before any work happens.
BridgeError ¶
Bases: PyMzLibError
mzLib reported a failure.
Attributes:
| Name | Type | Description |
|---|---|---|
error_type |
The .NET exception type name, e.g. |
Source code in pkg/python/src/pymzlib/_bridge.py
ServiceUnavailableError ¶
Bases: BridgeError
An external service is unavailable — down, rate-limited, timing out, or unreachable.
This is deliberately a distinct type, because the difference between "the repository is having a bad morning" and "something is broken" is the difference between retrying later and filing a bug. Catch it to back off and retry::
try:
files = pymzlib.pride.list_files("PXD000001")
except pymzlib.ServiceUnavailableError:
... # EBI's problem; try again later
except pymzlib.BridgeError:
... # ours
The classification happens in the bridge rather than here, so every consumer of the wire format gets it and not only Python. HTTP 408, 429, and 5xx count as unavailable; 404 and 400 do not, because a wrong URL or a malformed request is our problem and excusing it as an outage would hide a real bug.
Source code in pkg/python/src/pymzlib/_bridge.py
BridgeTimeoutError ¶
Bases: PyMzLibError
The bridge process did not finish within the timeout.
Deliberately not a :class:ServiceUnavailableError, and the distinction is the whole
point. A subprocess timeout has several possible causes and only one of them is a slow
service: the bridge may be wedged, the executable may be corrupt, antivirus may be holding
it, or the caller may simply have passed a timeout that was too short. Reporting all of that
as "the repository is down" is how a real bug gets skipped by every test suite and never
seen again.
If the caller wants a slow network to be treated as an outage, they can catch this explicitly — but the library will not guess on their behalf.
Source code in pkg/python/src/pymzlib/_bridge.py
BridgeNotFoundError ¶
Bases: PyMzLibError
The bridge executable could not be located.
In a released wheel this should be impossible: the executable ships inside the package.
It normally means pyMzLib is being run from a source checkout where the bridge has not
been built yet (see pkg/build/publish-bridge.ps1).
Source code in pkg/python/src/pymzlib/_bridge.py
bridge_path ¶
Return the path of the bridge executable that will be used.
Resolution order: the PYMZLIB_BRIDGE environment variable, then the copy staged
inside this package for the current platform.
Raises:
| Type | Description |
|---|---|
BridgeNotFoundError
|
if neither exists. |
Source code in pkg/python/src/pymzlib/_bridge.py
bridge_version ¶
Return the bridge's own version information, and check protocol compatibility.
The payload carries:
bridge
The bridge assembly's own version.
protocol
The wire-format version. This — not the mzLib version — is the compatibility
contract: a binding is compatible with a bridge by protocol.
runtime
The bundled .NET runtime.
mzlib
Which mzLib this bridge was built against, as 1.0.0+<commit>, or absent when
the build recorded no commit. It answers "which mzLib am I actually running?" for
someone holding a wheel who has no access to the repository's pin file. It is
deliberately not a version to compare against: use protocol for that.
Because mzlib is absent from bridges built before it was added, read it with
.get("mzlib") rather than indexing.
Raises:
| Type | Description |
|---|---|
PyMzLibError
|
if the bridge speaks a different wire format than this package. |