openszraw 0.1.3

Rust reader for Shimadzu LabSolutions mass spectrometry raw data (.qgd GC-MS, .lcd LC-MS IT-TOF/QTOF).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
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
//! Conformance tests against real corpus fixtures, one per on-disk
//! variant. The corpus lives out of tree (`/workspaces/Projects/Data/SZRaw`)
//! and is not checked into this repo, so these tests skip cleanly (rather
//! than failing the build) when it is absent, e.g. on CI.

use openmassspec_core::conformance::assert_source_invariants;
use openmassspec_core::SpectrumSource;
use openszraw::reader::Reader;
use std::path::{Path, PathBuf};

fn qgd_profile_fixture() -> PathBuf {
    PathBuf::from("/workspaces/Projects/Data/SZRaw/PXD019638/L-B2-Br0-1.qgd")
}

fn qgd_mrm_fixture() -> PathBuf {
    PathBuf::from("/workspaces/Projects/Data/SZRaw/PXD034978/49_27a__8122021_11.qgd")
}

fn qtfl_fixture() -> PathBuf {
    PathBuf::from("/workspaces/Projects/Data/SZRaw/MSV000084197/20190607_NM16.lcd")
}

fn ttfl_fixture() -> PathBuf {
    PathBuf::from("/workspaces/Projects/Data/SZRaw/PXD020792/UY02-01-01p400.LCD")
}

/// Same file as `ttfl_fixture`; `PXD020792` is also where
/// `LC Raw Data/Chromatogram Ch5`/`Ch6` were found populated (see
/// `docs/format/04-lcd-chromatogram-pda.md`, Sigilweaver/OpenSZRaw#21).
fn ttfl_lc_chromatogram_fixture() -> PathBuf {
    ttfl_fixture()
}

/// Regression fixture for the `Data Index` `entry_i`/trailing-partial-
/// block bug fixed this session - see the addendum in
/// `docs/format/03-lcd-ttfl-msdata.md` and
/// `docs/format/06-known-limitations.md` section 1b. This file has only
/// 2 real interleaved channels per RT point (packing 2 RT points per
/// 64-byte `Data Index` block) and an odd RT-point count, producing a
/// trailing 32-byte partial block.
fn ttfl_partial_block_fixture() -> PathBuf {
    PathBuf::from("/workspaces/Projects/Data/SZRaw/PXD025121/17.lcd")
}

fn single_quad_fixture() -> PathBuf {
    PathBuf::from("/workspaces/Projects/Data/SZRaw/MTBLS1960/E1.lcd")
}

fn open_or_skip(path: &Path) -> Option<Reader> {
    if !path.exists() {
        eprintln!("skip: corpus not present at {}", path.display());
        return None;
    }
    Some(Reader::open(path).unwrap_or_else(|e| panic!("Reader::open({path:?}) failed: {e}")))
}

#[test]
fn qgd_profile_conformance() {
    let Some(mut reader) = open_or_skip(&qgd_profile_fixture()) else {
        return;
    };
    let n = assert_source_invariants(&mut reader).expect("conformance invariants failed");
    assert!(n > 0, "expected at least one spectrum");
    println!("qgd profile: {n} spectra");
}

#[test]
fn qgd_profile_peaks_are_plausible() {
    let Some(mut reader) = open_or_skip(&qgd_profile_fixture()) else {
        return;
    };
    let spectra: Vec<_> = reader.iter_spectra().collect();
    assert!(!spectra.is_empty());
    assert!(spectra.iter().all(|s| s.ms_level == 1));

    let with_peaks: Vec<_> = spectra.iter().filter(|s| !s.mz.is_empty()).collect();
    assert!(
        !with_peaks.is_empty(),
        "expected at least one spectrum with peaks"
    );

    // GC-MS EI full-scan m/z is typically in the tens-to-low-hundreds Da
    // range (this reference file's first scan example in
    // docs/format/02-gcms-qgd-scans.md starts at m/z 100.0); allow up to
    // 2000 Da to cover wider-range methods without being a vacuous check.
    for s in &with_peaks {
        for &mz in &s.mz {
            assert!(
                (1.0..=2000.0).contains(&mz),
                "implausible GC-MS m/z {mz} in spectrum {}",
                s.native_id
            );
        }
    }

    // Retention time must be non-decreasing across the whole run (single
    // linear GC acquisition, no interleaving).
    let mut last_rt = f64::MIN;
    for s in &spectra {
        assert!(
            s.retention_time_sec + 1e-6 >= last_rt,
            "RT regressed: {} -> {}",
            last_rt,
            s.retention_time_sec
        );
        last_rt = s.retention_time_sec;
    }
}

#[test]
fn qgd_mrm_conformance() {
    let Some(mut reader) = open_or_skip(&qgd_mrm_fixture()) else {
        return;
    };
    let n = assert_source_invariants(&mut reader).expect("conformance invariants failed");
    assert!(n > 0, "expected at least one spectrum");
    println!("qgd mrm: {n} spectra");
}

#[test]
fn qgd_mrm_transitions_are_plausible() {
    let Some(mut reader) = open_or_skip(&qgd_mrm_fixture()) else {
        return;
    };
    let spectra: Vec<_> = reader.iter_spectra().collect();
    assert!(!spectra.is_empty());
    assert!(spectra.iter().all(|s| s.ms_level == 2));
    assert!(spectra
        .iter()
        .all(|s| s.mz.len() == 1 && s.intensity.len() == 1));

    for s in &spectra {
        let precursor = s
            .precursor
            .as_ref()
            .unwrap_or_else(|| panic!("MRM spectrum {} missing precursor", s.native_id));
        let precursor_mz = precursor.selected_mz.expect("selected_mz set for MRM");
        assert!(
            (1.0..=2000.0).contains(&precursor_mz),
            "implausible precursor m/z {precursor_mz}"
        );
        let product_mz = s.mz[0];
        assert!(
            (1.0..=2000.0).contains(&product_mz),
            "implausible product m/z {product_mz}"
        );
    }
}

#[test]
fn qtfl_conformance() {
    let Some(mut reader) = open_or_skip(&qtfl_fixture()) else {
        return;
    };
    let n = assert_source_invariants(&mut reader).expect("conformance invariants failed");
    assert!(n > 0, "expected at least one spectrum");
    println!("qtfl: {n} spectra");
}

#[test]
fn qtfl_centroid_mz_is_plausible_and_has_ms2() {
    let Some(mut reader) = open_or_skip(&qtfl_fixture()) else {
        return;
    };
    let spectra: Vec<_> = reader.iter_spectra().collect();
    assert!(!spectra.is_empty());

    let has_ms1 = spectra.iter().any(|s| s.ms_level == 1);
    let has_ms2 = spectra.iter().any(|s| s.ms_level == 2);
    assert!(has_ms1, "expected at least one MS1 (event_id==1) scan");
    assert!(
        has_ms2,
        "expected at least one MS2 (event_id>1) scan - see docs/format/05 addendum"
    );

    let with_peaks: Vec<_> = spectra.iter().filter(|s| !s.mz.is_empty()).collect();
    assert!(!with_peaks.is_empty());
    for s in &with_peaks {
        for &mz in &s.mz {
            assert!(
                (50.0..=5000.0).contains(&mz),
                "implausible QTOF m/z {mz} in spectrum {}",
                s.native_id
            );
        }
    }

    // Every MS2 spectrum must carry a precursor reference (even though we
    // do not decode the real precursor m/z this session - see
    // docs/format/06-known-limitations.md).
    for s in spectra.iter().filter(|s| s.ms_level == 2) {
        assert!(
            s.precursor
                .as_ref()
                .is_some_and(|p| p.precursor_native_id.is_some()),
            "MS2 spectrum {} missing precursor_native_id",
            s.native_id
        );
    }
}

#[test]
fn ttfl_conformance() {
    let Some(mut reader) = open_or_skip(&ttfl_fixture()) else {
        return;
    };
    let n = assert_source_invariants(&mut reader).expect("conformance invariants failed");
    assert!(n > 0, "expected at least one spectrum");
    println!("ttfl: {n} spectra");
}

#[test]
fn ttfl_partial_block_conformance() {
    let Some(mut reader) = open_or_skip(&ttfl_partial_block_fixture()) else {
        return;
    };
    let n = assert_source_invariants(&mut reader).expect("conformance invariants failed");
    assert!(n > 0, "expected at least one spectrum");
    println!("ttfl (2-channel, partial trailing block): {n} spectra");
}

#[test]
fn ttfl_mz_is_calibrated_finite_and_nonnegative() {
    // `mz` is now calibrated physical m/z (see
    // `raw::ttfl::Calibration` / `docs/format/03-lcd-ttfl-msdata.md`
    // section 3c), not the raw time-bin index. This must hold for
    // *every* decoded value, including the noise-tail high-index
    // positions documented in `docs/format/06-known-limitations.md`
    // (those map to very large but still finite, non-negative m/z -
    // squaring never produces NaN or negative output here).
    let Some(mut reader) = open_or_skip(&ttfl_fixture()) else {
        return;
    };
    let spectra: Vec<_> = reader.iter_spectra().collect();
    assert!(!spectra.is_empty());
    assert!(spectra.iter().all(|s| s.ms_level == 1));

    let with_peaks: Vec<_> = spectra.iter().filter(|s| !s.mz.is_empty()).collect();
    assert!(!with_peaks.is_empty());

    for s in &with_peaks {
        for &mz in &s.mz {
            assert!(mz.is_finite(), "non-finite calibrated m/z {mz}");
            assert!(mz >= 0.0, "negative calibrated m/z {mz}");
        }
    }
}

#[test]
fn ttfl_base_peak_mz_is_plausible_for_most_scans() {
    // The raw RLE-decoded stream includes a great deal of low-intensity
    // noise across the whole time-bin axis (many points map to
    // implausibly large m/z once calibrated - see
    // `raw::ttfl::Calibration::mz`'s doc comment on the noise-tail
    // caveat), so this deliberately does not assert a bound on *every*
    // peak. Restricted to each scan's single highest-intensity peak (a
    // proxy for "the most likely real ion in this scan"), the great
    // majority should fall in a plausible small-molecule/metabolite
    // range - this was independently verified against real corpus data
    // during the calibration's derivation (base peaks predominantly
    // <2000 Da, see docs/format/03-lcd-ttfl-msdata.md section 3c).
    let Some(mut reader) = open_or_skip(&ttfl_fixture()) else {
        return;
    };
    let spectra: Vec<_> = reader.iter_spectra().collect();
    let base_peaks: Vec<f64> = spectra
        .iter()
        .filter_map(|s| {
            s.intensity
                .iter()
                .enumerate()
                .max_by(|(_, a), (_, b)| a.total_cmp(b))
                .map(|(i, _)| s.mz[i])
        })
        .collect();
    assert!(!base_peaks.is_empty());

    let plausible = base_peaks.iter().filter(|&&mz| mz < 20_000.0).count();
    let fraction = plausible as f64 / base_peaks.len() as f64;
    assert!(
        fraction > 0.8,
        "only {:.1}% of base peaks fell under 20,000 Da ({} of {})",
        fraction * 100.0,
        plausible,
        base_peaks.len()
    );
}

/// See `raw::lc_chrom` and docs/format/04's "LC Raw Data Chromatogram
/// Ch5/Ch6 decode" session (Sigilweaver/OpenSZRaw#21). Only `Ch6` is
/// expected to decode - `Ch5` is deliberately skipped (single repeated
/// value in every corpus file, grammar untestable, see
/// `lc_chrom::decode_stream`'s doc comment).
#[test]
fn ttfl_lc_chromatogram_ch6_decodes_and_is_plausible() {
    let Some(mut reader) = open_or_skip(&ttfl_lc_chromatogram_fixture()) else {
        return;
    };
    let chroms: Vec<_> = reader.iter_chromatograms().collect();
    assert_eq!(
        chroms.len(),
        1,
        "expected exactly one decoded LC chromatogram (Ch6); Ch5 must be skipped"
    );
    let ch6 = &chroms[0];
    assert!(ch6.id.ends_with("Ch6"), "unexpected id: {}", ch6.id);
    assert_eq!(ch6.intensity.len(), 7200);
    assert_eq!(ch6.time_sec.len(), 7200);

    // Retention time must be non-decreasing and evenly spaced at the
    // corpus-derived 0.5s sample interval.
    for w in ch6.time_sec.windows(2) {
        assert!(
            (w[1] - w[0] - 0.5).abs() < 1e-3,
            "sample interval not ~0.5s: {} -> {}",
            w[0],
            w[1]
        );
    }

    // Physical plausibility (docs/format/04's "Further avenues" section
    // explicitly endorses this as a soft validator): intensities should
    // be non-negative and stay within the corpus's observed dynamic
    // range, not blow up into an unbounded ramp.
    for &v in &ch6.intensity {
        assert!(v.is_finite(), "non-finite intensity {v}");
        assert!(
            (0.0..20_000.0).contains(&v),
            "implausible LC Ch6 intensity {v}"
        );
    }
}

/// Confirms `Ch5` (constant across every corpus file) is not emitted as a
/// fabricated chromatogram trace - see `lc_chrom::decode_stream`'s doc
/// comment for why guessing its numeric grammar would be dishonest.
#[test]
fn ttfl_lc_chromatogram_ch5_is_not_emitted() {
    let Some(mut reader) = open_or_skip(&ttfl_lc_chromatogram_fixture()) else {
        return;
    };
    let chroms: Vec<_> = reader.iter_chromatograms().collect();
    assert!(
        chroms.iter().all(|c| !c.id.ends_with("Ch5")),
        "Ch5 should not be emitted (single repeated value, grammar untestable)"
    );
}

#[test]
fn single_quad_conformance() {
    let Some(mut reader) = open_or_skip(&single_quad_fixture()) else {
        return;
    };
    let n = assert_source_invariants(&mut reader).expect("conformance invariants failed");
    assert!(n > 0, "expected at least one spectrum");
    println!("single-quad: {n} spectra");
}

/// See `docs/format/07-mass-raw-data-single-quad.md`: `raw::mass_raw`
/// decodes full-scan profile spectra with a per-scan peak-record width
/// (4 or 5 bytes, observed in this corpus) derived from the header's own
/// peak count, verified byte-exact against `Mass Raw Data/TIC Data`
/// during development (0 mismatches across 19,200 scans, 8 corpus
/// files) - that check lives in the format doc / decode session, not
/// here, since `TIC Data` itself is not currently surfaced through
/// `SpectrumSource`.
#[test]
fn single_quad_peaks_are_plausible() {
    let Some(mut reader) = open_or_skip(&single_quad_fixture()) else {
        return;
    };
    let spectra: Vec<_> = reader.iter_spectra().collect();
    assert!(!spectra.is_empty());
    assert!(spectra.iter().all(|s| s.ms_level == 1));

    let with_peaks: Vec<_> = spectra.iter().filter(|s| !s.mz.is_empty()).collect();
    assert!(
        !with_peaks.is_empty(),
        "expected at least one spectrum with peaks"
    );

    // MTBLS1960 (LCMS-2020) scans m/z 400-2000 Da (see docs/format/07);
    // allow a wider 1-3000 Da margin to cover other single-quad methods
    // without being a vacuous check.
    for s in &with_peaks {
        for &mz in &s.mz {
            assert!(
                (1.0..=3000.0).contains(&mz),
                "implausible single-quad m/z {mz} in spectrum {}",
                s.native_id
            );
        }
    }

    // Retention time must be non-decreasing across the whole run, even
    // though the header carries an alternating (plausibly polarity)
    // flag - the two interleaved acquisition types still share one
    // monotonically increasing `Retention Time` axis (see
    // docs/format/07).
    let mut last_rt = f64::MIN;
    for s in &spectra {
        assert!(
            s.retention_time_sec + 1e-6 >= last_rt,
            "RT regressed: {} -> {}",
            last_rt,
            s.retention_time_sec
        );
        last_rt = s.retention_time_sec;
    }
}