openwraw 1.2.7

Rust reader for Waters MassLynx RAW mass spectrometry directories.
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! High-level reader for a Waters `.raw/` bundle directory.
//!
//! Wraps the low-level primitives in [`crate::raw`] into a single
//! `Reader::open(dir)` entry point that:
//!
//! * Parses `_HEADER.TXT`, `_FUNCTNS.INF`, `_extern.inf`.
//! * Discovers every `_FUNCnnn.IDX` / `_FUNCnnn.DAT` pair on disk.
//! * Picks an encoding (A / B / C) per function based on the IDX stride
//!   (Variant A -> Encoding A) plus the instrument name on Variant B
//!   (`SYNAPT*` -> Encoding B IMS, anything else -> Encoding C).
//! * Provides [`Reader::iter_spectra`] which yields one decoded spectrum
//!   per scan, in `(function_index, scan_index_in_function)` order,
//!   skipping lock-mass functions.
//!
//! Mass-spec-core integration lives in [`crate::mzml`].

use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};

use crate::raw::data::{
    decode_encoding_a, decode_encoding_b, decode_encoding_c, DecodeParams, ImsSpectrum, Spectrum,
};
use crate::raw::extern_inf::ExternInf;
use crate::raw::func_sts::FuncSts;
use crate::raw::functions_inf::{FunctionInfo, FunctionTable};
use crate::raw::header::{FunctionCal, Header};
use crate::raw::index::ScanIndex;

/// Which decoder applies to a given function's `_FUNCnnn.DAT`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Encoding {
    /// 6-byte records, sentinel-anchored. Variant A index.
    A,
    /// 8-byte IMS records (count, dt_bin, tof_bin). Variant B index.
    B,
    /// 8-byte non-IMS records (intensity, sub_bin, tof_bin). Variant B index.
    C,
}

/// One acquisition function's static metadata, ready for decoding.
#[derive(Debug, Clone)]
pub struct FunctionEntry {
    /// 1-based function index.
    pub index: u32,
    /// `_FUNCTNS.INF` record for this function.
    pub info: FunctionInfo,
    /// Scan index parsed from `_FUNCnnn.IDX`.
    pub scan_index: ScanIndex,
    /// Path to `_FUNCnnn.DAT`.
    pub dat_path: PathBuf,
    /// Length of `_FUNCnnn.DAT` in bytes; used to size the trailing scan.
    pub dat_size: u64,
    /// Decoder this function's DAT requires.
    pub encoding: Encoding,
    /// Calibration polynomial pulled from `_HEADER.TXT`.
    pub cal: FunctionCal,
    /// Parsed `_FUNCnnn.STS` scan-statistics table, when the file is present
    /// and well-formed. `None` for bundles that lack it (older instrument
    /// generations) or where it fails to parse - a missing/bad STS file is
    /// not fatal to opening the bundle, since it only supplies supplementary
    /// per-scan housekeeping values (e.g. collision energy), not the peak
    /// data itself.
    pub sts: Option<FuncSts>,
}

impl FunctionEntry {
    /// Number of scans in this function.
    pub fn scan_count(&self) -> usize {
        self.scan_index.len()
    }

    /// Build the [`DecodeParams`] needed for one of the `decode_encoding_*`
    /// primitives.
    fn decode_params(&self, extern_inf: &ExternInf) -> DecodeParams {
        DecodeParams {
            a_us: extern_inf.a_us(),
            cal: self.cal.clone(),
            mz_low: self.info.mz_low as f64,
            mz_high: self.info.mz_high as f64,
            scan_time_ms: self.info.scan_time_s as f64 * 1000.0,
        }
    }
}

/// A fully-parsed Waters `.raw/` bundle, ready to stream spectra.
#[derive(Debug, Clone)]
pub struct Reader {
    pub dir: PathBuf,
    pub bundle_name: String,
    pub header: Header,
    pub extern_inf: ExternInf,
    pub functions: Vec<FunctionEntry>,
}

impl Reader {
    /// Open a `.raw/` bundle directory and parse every required side file.
    pub fn open<P: AsRef<Path>>(dir: P) -> crate::Result<Self> {
        let dir = dir.as_ref().to_path_buf();
        let header = Header::from_path(&dir.join("_HEADER.TXT"))?;
        let extern_inf = ExternInf::from_path(&dir.join("_extern.inf"))?;
        let func_table = FunctionTable::from_path(&dir.join("_FUNCTNS.INF"))?;

        let instrument = header.instrument.clone().unwrap_or_default();
        let is_synapt = instrument.to_ascii_uppercase().starts_with("SYNAPT");

        let mut functions: Vec<FunctionEntry> = Vec::new();
        for info in &func_table.functions {
            let idx_name = format!("_FUNC{:03}.IDX", info.index);
            let dat_name = format!("_FUNC{:03}.DAT", info.index);
            let idx_path = dir.join(&idx_name);
            let dat_path = dir.join(&dat_name);
            if !idx_path.exists() || !dat_path.exists() {
                continue;
            }
            let scan_index = ScanIndex::from_path(&idx_path)?;
            let dat_size = fs::metadata(&dat_path)?.len();
            let encoding = match &scan_index {
                ScanIndex::A(_) => Encoding::A,
                ScanIndex::B(_) => {
                    if is_synapt {
                        Encoding::B
                    } else {
                        Encoding::C
                    }
                }
            };
            let cal = header
                .cal_functions
                .get(&info.index)
                .cloned()
                .unwrap_or_default();

            let sts_path = dir.join(format!("_FUNC{:03}.STS", info.index));
            let sts = FuncSts::from_path(&sts_path).ok();

            functions.push(FunctionEntry {
                index: info.index,
                info: info.clone(),
                scan_index,
                dat_path,
                dat_size,
                encoding,
                cal,
                sts,
            });
        }

        let bundle_name = dir
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_else(|| "bundle.raw".into());

        Ok(Reader {
            dir,
            bundle_name,
            header,
            extern_inf,
            functions,
        })
    }

    /// Returns the total number of scans across all non-lock-mass functions.
    pub fn total_scan_count(&self) -> usize {
        self.functions
            .iter()
            .filter(|f| !f.info.is_lock_mass())
            .map(|f| f.scan_count())
            .sum()
    }

    /// Decode the `i`-th scan (0-based) of the given function.
    pub fn decode_scan(&self, function_index: u32, scan_idx: usize) -> crate::Result<DecodedScan> {
        let entry = self
            .functions
            .iter()
            .find(|f| f.index == function_index)
            .ok_or_else(|| {
                crate::Error::Parse(format!("function {function_index} not present in bundle"))
            })?;
        let (offset, length, rt_min) = scan_slice(entry, scan_idx)?;
        let bytes = read_slice(&entry.dat_path, offset, length)?;
        let params = entry.decode_params(&self.extern_inf);
        let decoded = match entry.encoding {
            Encoding::A => DecodedSpectrum::Plain(decode_encoding_a(&bytes, &params)?),
            Encoding::B => DecodedSpectrum::Ims(decode_encoding_b(&bytes, &params)?),
            Encoding::C => DecodedSpectrum::Plain(decode_encoding_c(&bytes, &params)?),
        };
        let collision_energy_ev = entry
            .sts
            .as_ref()
            .and_then(|sts| sts.collision_energy(scan_idx));
        Ok(DecodedScan {
            function_index,
            scan_idx,
            retention_time_min: rt_min,
            spectrum: decoded,
            collision_energy_ev,
        })
    }

    /// Iterate every non-lock-mass scan across the bundle, in function then
    /// scan order. Lock-mass / reference functions are skipped.
    pub fn iter_spectra(&self) -> impl Iterator<Item = crate::Result<DecodedScan>> + '_ {
        let plan: Vec<(u32, usize)> = self
            .functions
            .iter()
            .filter(|f| !f.info.is_lock_mass())
            .flat_map(|f| (0..f.scan_count()).map(move |i| (f.index, i)))
            .collect();
        plan.into_iter()
            .map(move |(fi, si)| self.decode_scan(fi, si))
    }
}

/// One scan after decoding.
#[derive(Debug, Clone)]
pub struct DecodedScan {
    pub function_index: u32,
    /// 0-based position within the function.
    pub scan_idx: usize,
    pub retention_time_min: f32,
    pub spectrum: DecodedSpectrum,
    /// Per-scan collision energy (eV) from `_FUNCnnn.STS`'s "Collision
    /// Energy" channel, when the file is present and defines that channel.
    pub collision_energy_ev: Option<f64>,
}

/// Decoded payload of a scan; varies by encoding.
#[derive(Debug, Clone)]
pub enum DecodedSpectrum {
    /// Output of Encoding A or C.
    Plain(Spectrum),
    /// Output of Encoding B (IMS).
    Ims(ImsSpectrum),
}

/// Resolve the byte slice for scan `scan_idx` within `entry`'s DAT file.
///
/// Returns `(offset, length, retention_time_min)`. Trailing scans take the
/// length implied by `entry.dat_size`.
///
/// `dat_offset` and (for Variant B) the next record's `dat_offset` are raw
/// fields read straight from the `.IDX` file, so `length` is capped against
/// `entry.dat_size` (the real, already-known size of the paired `.DAT` file)
/// before returning: an IDX record claiming a scan larger than the DAT file
/// that actually exists must not be able to force an allocation sized from
/// unvalidated file-controlled offsets in `read_slice`.
fn scan_slice(entry: &FunctionEntry, scan_idx: usize) -> crate::Result<(u64, u64, f32)> {
    let (offset, length, retention_time_min) = match &entry.scan_index {
        ScanIndex::A(records) => {
            let rec = records.get(scan_idx).ok_or_else(|| {
                crate::Error::Parse(format!(
                    "function {} scan {} out of range",
                    entry.index, scan_idx
                ))
            })?;
            // Variant A stores n_records directly: each record is 6 bytes.
            let offset = rec.dat_offset as u64;
            let length = (rec.n_records as u64) * 6;
            (offset, length, rec.retention_time_min)
        }
        ScanIndex::B(records) => {
            let rec = records.get(scan_idx).ok_or_else(|| {
                crate::Error::Parse(format!(
                    "function {} scan {} out of range",
                    entry.index, scan_idx
                ))
            })?;
            let offset = rec.dat_offset as u64;
            let next_offset = records
                .get(scan_idx + 1)
                .map(|r| r.dat_offset as u64)
                .unwrap_or(entry.dat_size);
            let length = next_offset.saturating_sub(offset);
            (offset, length, rec.retention_time_min)
        }
    };
    let remaining = entry.dat_size.saturating_sub(offset);
    Ok((offset, length.min(remaining), retention_time_min))
}

fn read_slice(path: &Path, offset: u64, length: u64) -> crate::Result<Vec<u8>> {
    use std::io::{Read, Seek, SeekFrom};
    let mut f = fs::File::open(path)?;
    f.seek(SeekFrom::Start(offset))?;
    let mut buf = vec![0u8; length as usize];
    f.read_exact(&mut buf)?;
    Ok(buf)
}

/// Group functions by encoding for quick reporting.
pub fn encoding_counts(reader: &Reader) -> BTreeMap<&'static str, usize> {
    let mut out = BTreeMap::new();
    for f in &reader.functions {
        let key = match f.encoding {
            Encoding::A => "A",
            Encoding::B => "B",
            Encoding::C => "C",
        };
        *out.entry(key).or_insert(0) += 1;
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::raw::functions_inf::FunctionInfo;
    use crate::raw::header::FunctionCal;
    use crate::raw::index::{ScanIndexA, ScanIndexB};

    fn dummy_info(index: u32) -> FunctionInfo {
        FunctionInfo {
            index,
            function_type: 0,
            scan_subtype: 0,
            cycle_time_s: 0.0,
            interscan_delay_s: 0.0,
            scan_time_s: 0.0,
            tof_depth: 0,
            mz_low: 0.0,
            mz_high: 0.0,
        }
    }

    fn entry_with(scan_index: ScanIndex, dat_size: u64) -> FunctionEntry {
        FunctionEntry {
            index: 1,
            info: dummy_info(1),
            scan_index,
            dat_path: PathBuf::new(),
            dat_size,
            encoding: Encoding::C,
            cal: FunctionCal::default(),
            sts: None,
        }
    }

    // A corrupt/malicious IDX can claim a scan far larger than the real DAT
    // file: dat_offset=0 for this scan, dat_offset=u32::MAX-1 for the "next"
    // scan used to compute Variant B's length by subtraction. Before this
    // was capped, `read_slice` would allocate a `Vec` sized from that
    // difference (up to ~4.29 GB) regardless of how small the real DAT file
    // on disk actually is - which aborts the process under a virtual-memory
    // limit rather than returning a recoverable error.
    #[test]
    fn variant_b_scan_slice_caps_length_to_dat_size() {
        let entry = entry_with(
            ScanIndex::B(vec![
                ScanIndexB {
                    dat_offset: 0,
                    retention_time_min: 0.0,
                },
                ScanIndexB {
                    dat_offset: u32::MAX - 1,
                    retention_time_min: 0.1,
                },
            ]),
            64, // real DAT file is tiny
        );
        let (offset, length, _) = scan_slice(&entry, 0).unwrap();
        assert_eq!(offset, 0);
        assert!(length <= 64, "length {length} exceeds dat_size 64");
    }

    #[test]
    fn variant_b_offset_beyond_dat_size_yields_zero_length() {
        let entry = entry_with(
            ScanIndex::B(vec![ScanIndexB {
                dat_offset: 1_000_000,
                retention_time_min: 0.0,
            }]),
            64,
        );
        let (_, length, _) = scan_slice(&entry, 0).unwrap();
        assert_eq!(length, 0);
    }

    #[test]
    fn variant_b_normal_scan_is_unaffected() {
        let entry = entry_with(
            ScanIndex::B(vec![
                ScanIndexB {
                    dat_offset: 0,
                    retention_time_min: 0.0,
                },
                ScanIndexB {
                    dat_offset: 40,
                    retention_time_min: 0.1,
                },
            ]),
            100,
        );
        let (offset, length, _) = scan_slice(&entry, 0).unwrap();
        assert_eq!(offset, 0);
        assert_eq!(length, 40);
    }

    #[test]
    fn variant_a_scan_slice_caps_length_to_dat_size() {
        let entry = entry_with(
            ScanIndex::A(vec![ScanIndexA {
                dat_offset: 0,
                n_records: u16::MAX, // claims 393,210 bytes
                retention_time_min: 0.0,
                peak_count: 0,
            }]),
            64,
        );
        let (_, length, _) = scan_slice(&entry, 0).unwrap();
        assert!(length <= 64, "length {length} exceeds dat_size 64");
    }

    #[test]
    fn variant_a_normal_scan_is_unaffected() {
        let entry = entry_with(
            ScanIndex::A(vec![ScanIndexA {
                dat_offset: 0,
                n_records: 5,
                retention_time_min: 0.0,
                peak_count: 0,
            }]),
            100,
        );
        let (_, length, _) = scan_slice(&entry, 0).unwrap();
        assert_eq!(length, 30);
    }
}