Skill Tracer
Skills › Scientific Agent Skills › pyopenms › file_io.md

file_io.md

91 lines · skills/pyopenms/references/file_io.md at 5f2bc03

Open on GitHub
1# File I/O (pyOpenMS 3.6.0)
2
3The bundled scripts use local files; they do not fetch vendor data or databases.
4Native tests cover tiny synthetic mzML, chromatogram, featureXML, consensusXML,
5idXML, FASTA, MGF and local TSV examples. Instrument conversion, mzIdentML/pepXML,
6TraML, and large-file throughput remain untested here.
7

File I/O (pyOpenMS 3.6.0)

The bundled scripts use local files; they do not fetch vendor data or databases. Native tests cover tiny synthetic mzML, chromatogram, featureXML, consensusXML, idXML, FASTA, MGF and local TSV examples. Instrument conversion, mzIdentML/pepXML, TraML, and large-file throughput remain untested here.

Raw lines 1 to 7
8## mzML and indexed access
9
10```python
11import pyopenms as ms
12exp = ms.MSExperiment()
13ms.MzMLFile().load("sample.mzML", exp)
14print(exp.getNrSpectra(), exp.getNrChromatograms())
15writer = ms.MzMLFile()
16options = writer.getOptions()
17options.setWriteIndex(True)
18options.setCompression(True)
19writer.setOptions(options)
20writer.store("indexed.mzML", exp)
22on_disk = ms.OnDiscMSExperiment()
23if not on_disk.openFile("indexed.mzML"):
24 raise ValueError("An indexed mzML is required")
25for i in range(on_disk.getNrSpectra()):
26 spec = on_disk.getSpectrum(i)
27 # RT is seconds; get_peaks() yields m/z and intensity arrays.
28```
30`IndexedMzMLFileLoader.load(filename, on_disk)` is an alternative loader; it
31requires an `OnDiscMSExperiment` output argument. The loader itself is not a
32spectrum container. Preserve a known indexed input for lazy reading; converting
33an unindexed input via `MSExperiment` first still loads the full input in memory.
34`CachedmzML.store(path, exp)` and `CachedmzML.load(path, cached)` are available for
35local binary caches, which require their paired metadata files and disk space.

mzML and indexed access

import pyopenms as ms
exp = ms.MSExperiment()
ms.MzMLFile().load("sample.mzML", exp)
print(exp.getNrSpectra(), exp.getNrChromatograms())
writer = ms.MzMLFile()
options = writer.getOptions()
options.setWriteIndex(True)
options.setCompression(True)
writer.setOptions(options)
writer.store("indexed.mzML", exp)

on_disk = ms.OnDiscMSExperiment()
if not on_disk.openFile("indexed.mzML"):
    raise ValueError("An indexed mzML is required")
for i in range(on_disk.getNrSpectra()):
    spec = on_disk.getSpectrum(i)
    # RT is seconds; get_peaks() yields m/z and intensity arrays.

IndexedMzMLFileLoader.load(filename, on_disk) is an alternative loader; it requires an OnDiscMSExperiment output argument. The loader itself is not a spectrum container. Preserve a known indexed input for lazy reading; converting an unindexed input via MSExperiment first still loads the full input in memory. CachedmzML.store(path, exp) and CachedmzML.load(path, cached) are available for local binary caches, which require their paired metadata files and disk space.

Raw lines 8 to 36
37## Format-specific I/O
39```python
40proteins, peptides = ms.IdXMLFile().load("search.idXML")
41ms.IdXMLFile().store("copy.idXML", proteins, peptides)
42# The output-argument form also works:
43proteins = []
44peptides = ms.PeptideIdentificationList()
45ms.IdXMLFile().load("search.idXML", proteins, peptides)
47features = ms.FeatureMap()
48ms.FeatureXMLFile().load("features.featureXML", features)
49consensus = ms.ConsensusMap()
50ms.ConsensusXMLFile().load("study.consensusXML", consensus)
51entries = []
52ms.FASTAFile().load("database.fasta", entries)
53```
55For untested formats, inspect installed signatures before adapting:
56`help(ms.MzIdentMLFile.load)`, `help(ms.PepXMLFile.load)`,
57`help(ms.TraMLFile.load)`. pepXML is a general peptide-identification exchange
58format, not an X! Tandem-only format. An `MSPFile.load` call requires a
59`PeptideIdentificationList` and `MSExperiment` (or `AnnotatedMSRun`), not a list
60of spectra.

Format-specific I/O

proteins, peptides = ms.IdXMLFile().load("search.idXML")
ms.IdXMLFile().store("copy.idXML", proteins, peptides)
# The output-argument form also works:
proteins = []
peptides = ms.PeptideIdentificationList()
ms.IdXMLFile().load("search.idXML", proteins, peptides)

features = ms.FeatureMap()
ms.FeatureXMLFile().load("features.featureXML", features)
consensus = ms.ConsensusMap()
ms.ConsensusXMLFile().load("study.consensusXML", consensus)
entries = []
ms.FASTAFile().load("database.fasta", entries)

For untested formats, inspect installed signatures before adapting: help(ms.MzIdentMLFile.load), help(ms.PepXMLFile.load), help(ms.TraMLFile.load). pepXML is a general peptide-identification exchange format, not an X! Tandem-only format. An MSPFile.load call requires a PeptideIdentificationList and MSExperiment (or AnnotatedMSRun), not a list of spectra.

Raw lines 37 to 61
62## Metadata and loss
64Use `exp.getInstrument()`, `exp.getSample()`, and `exp.getSourceFiles()` to inspect
65provenance. `MSChromatogram.get_peaks()` gives time in seconds and intensity;
66`getNativeID()` identifies stored traces, but a stored TIC need not be present
67or equal to the sum of the MS1 peaks retained after conversion.
69`convert_format.py` preserves experiment metadata when filtering spectra, and
70retains synchronized peak arrays using `MSSpectrum.select(indices)`. Its RT and
71intensity filters apply to spectra only. Chromatograms pass through unchanged
72unless `--ms-level` is selected, when they are omitted.
74MGF exports tandem spectra and loses much mzML metadata, chromatograms, and
75other acquisition information; mzXML also cannot represent all mzML metadata.
76Round-trip counts alone do not establish lossless conversion. Keep the original
77file, conversion program/version/options, native IDs, charge/polarity, source
78metadata, and calibration state. Native vendor-reader availability in OpenMS
793.6 does not establish that any particular vendor file works in this wheel.

Metadata and loss

Use exp.getInstrument(), exp.getSample(), and exp.getSourceFiles() to inspect provenance. MSChromatogram.get_peaks() gives time in seconds and intensity; getNativeID() identifies stored traces, but a stored TIC need not be present or equal to the sum of the MS1 peaks retained after conversion.

convert_format.py preserves experiment metadata when filtering spectra, and retains synchronized peak arrays using MSSpectrum.select(indices). Its RT and intensity filters apply to spectra only. Chromatograms pass through unchanged unless --ms-level is selected, when they are omitted.

MGF exports tandem spectra and loses much mzML metadata, chromatograms, and other acquisition information; mzXML also cannot represent all mzML metadata. Round-trip counts alone do not establish lossless conversion. Keep the original file, conversion program/version/options, native IDs, charge/polarity, source metadata, and calibration state. Native vendor-reader availability in OpenMS 3.6 does not establish that any particular vendor file works in this wheel.

Raw lines 62 to 80
81## mzTab
83Use the producing algorithm's populated `MzTab`/`MzTabM` with the corresponding
84writer. A title plus empty rows is not a complete report. When changing a copied
85metadata object, explicitly call `mztab.setMetaData(metadata)` before storing.
86Retain candidate multiplicity, adduct assumptions and database provenance.
88Source: [OpenMS release changes](https://openms.de/documentation/html/ChangeLog.html)
89and installed pyOpenMS 3.6.0 binding signatures; the older
90[mzML tutorial](https://pyopenms.readthedocs.io/en/latest/user_guide/mzml_files.html)
91may show different output-argument conventions.

mzTab

Use the producing algorithm's populated MzTab/MzTabM with the corresponding writer. A title plus empty rows is not a complete report. When changing a copied metadata object, explicitly call mztab.setMetaData(metadata) before storing. Retain candidate multiplicity, adduct assumptions and database provenance.

Source: OpenMS release changes and installed pyOpenMS 3.6.0 binding signatures; the older mzML tutorial may show different output-argument conventions.

Raw lines 81 to 91