Skip to main content

Python API

The openaraw wheel exposes a small, eager reader built on the same Rust core as the Rust reader. Install it with pip install openaraw (or pip install openmassspec[agilent] for the umbrella).

import openaraw

reader = openaraw.RawReader("sample.d")

Opening a RawReader parses the .d directory and decodes every spectrum up front into memory, so construction is where the work happens and subsequent access is cheap. For streaming access over very large acquisitions, use openaraw.iter_spectra(path), which decodes one spectrum at a time.

A scan that cannot be decoded raises RuntimeError: from the RawReader constructor, from read_chromatograms(), or from the iter_spectra iterator when it reaches that scan.

RawReader​

MemberTypeDescription
RawReader(path)constructorOpen and fully decode the .d directory at path
scan_countintNumber of decoded spectra
read_spectrum(index)SpectrumThe spectrum at zero-based index (raises if out of range)
print(reader.scan_count)
for i in range(reader.scan_count):
spectrum = reader.read_spectrum(i)
...

Spectrum​

AttributeTypeDescription
mzlist[float]m/z values (float64)
intensitylist[float]Intensities (float32)
ms_levelintMS level (1 for MS1, 2+ for MS/MS)
retention_time_secfloat | NoneRetention time in seconds (always set by this reader)
scan_modestr | None"profile", "centroid", or None for a scan with no payload block
analyzerstr | None"tof" (Q-TOF) or "tqms" (QQQ)
base_peak_mz, base_peak_intensityfloat | NoneBase peak of the decoded peaks; None when the spectrum has no peaks
low_mz, high_mzfloat | NoneLowest and highest m/z of the spectrum, as the scan record gives them; None when the spectrum has no peaks
reported_base_peak_mz, reported_base_peak_intensityfloat | NoneThe base peak the scan record reported, also on a spectrum with no peaks
precursordict | NoneMS2+ only, see below
extradict[str, str]Reader-specific values, such as openaraw.mrm_channel_id

A spectrum with no peaks (len(spectrum) == 0) declares no base peak and no m/z range: base_peak_mz, base_peak_intensity, low_mz and high_mz are None. The reported_* attributes still return what the scan record held (for example reported_base_peak_mz of 0.0). A range the record reported is kept as strings in extra["openaraw.reported_low_mz"] and extra["openaraw.reported_high_mz"].

len(spectrum) returns the peak count. See Scan data for how mz/intensity are derived from the on-disk profile and centroid records.

spectrum = reader.read_spectrum(0)
print(spectrum.ms_level, spectrum.retention_time_sec, len(spectrum))
mz, intensity = spectrum.mz, spectrum.intensity

precursor​

For MS2+ spectra precursor is a dict with target_mz, selected_mz, isolation_width, charge, intensity, collision_energies, precursor_native_id, activation, analyzer and ccs. Q-TOF MS/MS scans set target_mz and one collision energy; QQQ MRM scans set neither (the channel is in spectrum.extra["openaraw.mrm_channel_id"]).

collision_energies is a list of {"value": float, "unit": str | None}. The file does not state the unit, so unit is None:

>>> spectrum.precursor["collision_energies"]
[{'value': 13.15, 'unit': None}]

Next​