Skip to main content

openreadout_quant/
extract.rs

1//! Chromatograms from any reader: total-ion (TIC), base-peak (BPC), extracted-ion (XIC) and
2//! SRM/MRM transition chromatograms computed from mass spectra in one streaming pass, and
3//! stored chromatograms or detector signals read from traces and tables.
4//!
5//! Definitions (`book/src/guides/quantitation.md`):
6//!
7//! - **XIC** at m/z `M` with tolerance `±δ` (`δ = M·ppm·10⁻⁶` or a fixed Da value): for every
8//!   selected spectrum, the sum (or, with [`Aggregate::Max`], the largest) of the intensities of
9//!   the points with `M − δ ≤ m/z ≤ M + δ`; 0 when none. Spectra are read as the instrument's
10//!   stored centroid lists where a scan has one ([`SpectrumView::Centroid`]), otherwise as
11//!   stored (profile points are then summed within the window); `profile: true` reads the
12//!   profile. Each chromatogram reports how many of its scans were centroid or profile.
13//! - **TIC**: the spectrum's recorded total ion current when the reader has one, else the sum
14//!   of its intensities; with an m/z range, the sum inside the range. **BPC**: likewise the
15//!   recorded base-peak intensity, else the largest intensity.
16//! - **SRM/MRM** `Q1 > Q3`: MS/MS scans whose precursor is within the transition tolerance of
17//!   Q1; in each, the intensity of the point nearest Q3 within the tolerance. When several
18//!   distinct precursors (or products) fall within the tolerance, only the one nearest the
19//!   requested value is kept (the others are named in `notes`). Files that store transitions
20//!   as chromatograms (mzML) or as table columns (Waters MRM functions) are read from those.
21
22use std::sync::atomic::{AtomicU64, Ordering};
23
24use schemars::JsonSchema;
25use serde::{Deserialize, Serialize};
26
27use openreadout_core::model::{FileInfo, Spectrum, TraceInfo};
28use openreadout_core::parallel::ReadContext;
29use openreadout_core::{Dataset, Error, Result, SpectrumView};
30
31/// Spectra decoded per worker between progress reports.
32const PROGRESS_EVERY: u64 = 256;
33/// Fewest spectra decoded per worker thread.
34const MIN_SCANS_PER_WORKER: u64 = 1000;
35/// Samples read from a trace per call.
36const TRACE_CHUNK: u64 = 1 << 20;
37/// Default XIC tolerance, ppm.
38pub const DEFAULT_PPM: f64 = 10.0;
39/// Default SRM/MRM and precursor tolerance, Da (unit resolution).
40pub const DEFAULT_TRANSITION_TOLERANCE_DA: f64 = 0.5;
41
42/// One chromatogram to extract.
43#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
44#[serde(tag = "kind", rename_all = "snake_case")]
45pub enum Target {
46    /// Total ion current.
47    Tic,
48    /// Base-peak intensity.
49    Bpc,
50    /// Extracted-ion chromatogram at `mz`.
51    Xic {
52        /// Target m/z.
53        mz: f64,
54    },
55    /// Selected/multiple reaction monitoring transition `q1 > q3`.
56    Srm {
57        /// Precursor (Q1) m/z.
58        q1: f64,
59        /// Product (Q3) m/z.
60        q3: f64,
61    },
62    /// A stored chromatogram or detector signal (`info` → `traces[]`).
63    Trace {
64        /// Trace index.
65        trace: u32,
66        /// Channel index (default: the first channel that is not `time`).
67        #[serde(default, skip_serializing_if = "Option::is_none")]
68        channel: Option<u32>,
69    },
70}
71
72/// m/z tolerance of an XIC (half-width of the window).
73#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
74#[serde(rename_all = "snake_case")]
75pub enum Tolerance {
76    /// Parts per million of the target m/z.
77    Ppm(f64),
78    /// Absolute, in m/z units (Da for singly charged ions).
79    Da(f64),
80}
81
82impl Default for Tolerance {
83    fn default() -> Self {
84        Tolerance::Ppm(DEFAULT_PPM)
85    }
86}
87
88impl Tolerance {
89    /// Half-width of the window around `mz`.
90    pub fn half_width(self, mz: f64) -> f64 {
91        match self {
92            Tolerance::Ppm(p) => mz * p * 1e-6,
93            Tolerance::Da(d) => d,
94        }
95    }
96    fn label(self) -> String {
97        match self {
98            Tolerance::Ppm(p) => format!("±{} ppm", num(p)),
99            Tolerance::Da(d) => format!("±{} Da", num(d)),
100        }
101    }
102}
103
104/// How the points inside an XIC window are combined.
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
106#[serde(rename_all = "snake_case")]
107pub enum Aggregate {
108    /// Sum of intensities (default; what OpenMS, pyteomics-based scripts and most vendor
109    /// software do).
110    #[default]
111    Sum,
112    /// Largest intensity.
113    Max,
114}
115
116/// Polarity filter.
117#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
118#[serde(rename_all = "snake_case")]
119pub enum Polarity {
120    /// Positive-ion scans.
121    Positive,
122    /// Negative-ion scans.
123    Negative,
124}
125
126impl std::str::FromStr for Polarity {
127    type Err = Error;
128    fn from_str(s: &str) -> Result<Self> {
129        match s.to_ascii_lowercase().as_str() {
130            "positive" | "pos" | "+" => Ok(Polarity::Positive),
131            "negative" | "neg" | "-" => Ok(Polarity::Negative),
132            _ => Err(Error::Usage(format!(
133                "unknown polarity `{s}`: use positive or negative"
134            ))),
135        }
136    }
137}
138
139impl Polarity {
140    fn id(self) -> &'static str {
141        match self {
142            Polarity::Positive => "positive",
143            Polarity::Negative => "negative",
144        }
145    }
146}
147
148/// What to extract and from which scans.
149#[derive(Debug, Clone)]
150#[non_exhaustive]
151pub struct ChromRequest {
152    /// Chromatograms to extract (empty: the TIC).
153    pub targets: Vec<Target>,
154    /// XIC tolerance.
155    pub tolerance: Tolerance,
156    /// SRM/MRM Q1 and Q3 tolerance, Da.
157    pub transition_tolerance_da: f64,
158    /// Spectra run.
159    pub run: u32,
160    /// MS level of the scans (default 1, or 2 with `precursor_mz`; SRM: any MS/MS level).
161    pub ms_level: Option<u32>,
162    /// Only scans of this polarity.
163    pub polarity: Option<Polarity>,
164    /// Only scans whose scan filter / description contains this text (case-insensitive).
165    pub scan_filter: Option<String>,
166    /// Only MS/MS scans whose precursor m/z is within `precursor_tolerance_da` of this.
167    pub precursor_mz: Option<f64>,
168    /// Precursor tolerance, Da.
169    pub precursor_tolerance_da: f64,
170    /// Only scans (or trace samples) in this retention-time range, minutes.
171    pub rt_range_min: Option<[f64; 2]>,
172    /// TIC/BPC over this m/z range only.
173    pub mz_range: Option<[f64; 2]>,
174    /// Read profile data where a scan has both profile and centroids.
175    pub profile: bool,
176    /// Combination of the points inside an XIC window.
177    pub aggregate: Aggregate,
178    /// Sweep of trace targets.
179    pub sweep: u32,
180}
181
182impl Default for ChromRequest {
183    fn default() -> Self {
184        Self {
185            targets: Vec::new(),
186            tolerance: Tolerance::default(),
187            transition_tolerance_da: DEFAULT_TRANSITION_TOLERANCE_DA,
188            run: 0,
189            ms_level: None,
190            polarity: None,
191            scan_filter: None,
192            precursor_mz: None,
193            precursor_tolerance_da: DEFAULT_TRANSITION_TOLERANCE_DA,
194            rt_range_min: None,
195            mz_range: None,
196            profile: false,
197            aggregate: Aggregate::Sum,
198            sweep: 0,
199        }
200    }
201}
202
203/// One chromatogram: retention times (minutes) and intensities, with how they were obtained
204/// and a summary.
205#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
206pub struct Chromatogram {
207    /// Short label, e.g. `TIC`, `XIC 301.1410 ±10 ppm`, `SRM 279.2 > 179.2`, `DAD1 A`.
208    pub label: String,
209    /// `tic`, `bpc`, `xic`, `srm` or `trace`.
210    pub kind: String,
211    /// Where the points come from: `spectra` (computed from the mass spectra), `trace N`
212    /// (a stored chromatogram or detector signal) or `table N` (an MRM table column).
213    pub source: String,
214    /// XIC target m/z.
215    #[serde(skip_serializing_if = "Option::is_none")]
216    pub mz: Option<f64>,
217    /// XIC window `[low, high]` m/z.
218    #[serde(skip_serializing_if = "Option::is_none")]
219    pub mz_window: Option<[f64; 2]>,
220    /// TIC/BPC m/z range.
221    #[serde(skip_serializing_if = "Option::is_none")]
222    pub mz_range: Option<[f64; 2]>,
223    /// SRM: the precursor (Q1) m/z matched in the file.
224    #[serde(skip_serializing_if = "Option::is_none")]
225    pub precursor_mz: Option<f64>,
226    /// SRM: the product (Q3) m/z matched in the file.
227    #[serde(skip_serializing_if = "Option::is_none")]
228    pub product_mz: Option<f64>,
229    /// MS level of the scans used.
230    #[serde(skip_serializing_if = "Option::is_none")]
231    pub ms_level: Option<u32>,
232    /// Polarity filter applied.
233    #[serde(skip_serializing_if = "Option::is_none")]
234    pub polarity: Option<String>,
235    /// Scan-filter text required.
236    #[serde(skip_serializing_if = "Option::is_none")]
237    pub scan_filter: Option<String>,
238    /// `sum` or `max` (XIC).
239    #[serde(skip_serializing_if = "Option::is_none")]
240    pub aggregation: Option<String>,
241    /// Scans read as centroids.
242    #[serde(skip_serializing_if = "Option::is_none")]
243    pub centroid_scans: Option<u64>,
244    /// Scans read as profiles (their points inside the window are summed).
245    #[serde(skip_serializing_if = "Option::is_none")]
246    pub profile_scans: Option<u64>,
247    /// TIC/BPC: points from the scan's recorded value (`recorded`), from its points
248    /// (`computed`), or both (`mixed`).
249    #[serde(skip_serializing_if = "Option::is_none")]
250    pub intensity_source: Option<String>,
251    /// Unit of `intensity` (`counts` for mass spectra, the detector's unit for traces).
252    #[serde(skip_serializing_if = "Option::is_none")]
253    pub intensity_unit: Option<String>,
254    /// Points in the chromatogram.
255    pub points: u64,
256    /// Retention time of the most intense point, minutes.
257    #[serde(skip_serializing_if = "Option::is_none")]
258    pub apex_rt_min: Option<f64>,
259    /// Intensity of the most intense point.
260    #[serde(skip_serializing_if = "Option::is_none")]
261    pub apex_intensity: Option<f64>,
262    /// Trapezoidal integral of the whole chromatogram (no baseline), intensity × minutes.
263    #[serde(skip_serializing_if = "Option::is_none")]
264    pub integral: Option<f64>,
265    /// Retention times, minutes (ascending).
266    pub rt_min: Vec<f64>,
267    /// Intensities.
268    pub intensity: Vec<f64>,
269    /// True when `rt_min`/`intensity` were thinned to `max_points` (the most intense point of
270    /// each of `max_points` equal slices is kept); the summary covers every point.
271    #[serde(default)]
272    pub decimated: bool,
273    /// Anything the caller should know.
274    #[serde(default, skip_serializing_if = "Vec::is_empty")]
275    pub notes: Vec<String>,
276}
277
278impl Chromatogram {
279    /// Fill the summary fields from the points.
280    pub fn summarize(&mut self) {
281        self.points = self.rt_min.len() as u64;
282        let apex = self
283            .intensity
284            .iter()
285            .enumerate()
286            .filter(|(_, v)| v.is_finite())
287            .max_by(|a, b| a.1.total_cmp(b.1).then(b.0.cmp(&a.0)));
288        self.apex_rt_min = apex.map(|(i, _)| self.rt_min[i]);
289        self.apex_intensity = apex.map(|(_, v)| *v);
290        self.integral = (self.rt_min.len() > 1).then(|| {
291            self.rt_min
292                .windows(2)
293                .zip(self.intensity.windows(2))
294                .map(|(t, y)| f64::midpoint(y[0], y[1]) * (t[1] - t[0]))
295                .filter(|v| v.is_finite())
296                .sum()
297        });
298    }
299
300    /// Keep at most `max` points: the most intense point of each of `max` equal slices.
301    pub fn decimate(&mut self, max: usize) {
302        let n = self.rt_min.len();
303        if max == 0 || n <= max {
304            return;
305        }
306        let mut t = Vec::with_capacity(max);
307        let mut y = Vec::with_capacity(max);
308        for b in 0..max {
309            let lo = b * n / max;
310            let hi = ((b + 1) * n / max).max(lo + 1).min(n);
311            let k = (lo..hi)
312                .max_by(|&i, &j| {
313                    self.intensity[i]
314                        .total_cmp(&self.intensity[j])
315                        .then(j.cmp(&i))
316                })
317                .unwrap_or(lo);
318            t.push(self.rt_min[k]);
319            y.push(self.intensity[k]);
320        }
321        self.rt_min = t;
322        self.intensity = y;
323        self.decimated = true;
324    }
325}
326
327/// Output of `analyze chromatogram`.
328#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
329pub struct ChromatogramOutput {
330    /// The input file.
331    pub path: String,
332    /// Format id of the input file.
333    pub format: String,
334    /// Spectra run used, when any chromatogram was computed from spectra.
335    #[serde(skip_serializing_if = "Option::is_none")]
336    pub run: Option<u32>,
337    /// Spectra decoded.
338    pub spectra_read: u64,
339    /// One entry per requested chromatogram.
340    pub chromatograms: Vec<Chromatogram>,
341    /// File written (`-o`), when any.
342    #[serde(skip_serializing_if = "Option::is_none")]
343    pub output: Option<String>,
344    /// Anything the caller should know.
345    #[serde(default, skip_serializing_if = "Vec::is_empty")]
346    pub notes: Vec<String>,
347}
348
349fn num(v: f64) -> String {
350    let s = format!("{v:.6}");
351    let s = s.trim_end_matches('0').trim_end_matches('.');
352    s.to_string()
353}
354
355fn validate(req: &ChromRequest) -> Result<()> {
356    let pos = |v: f64, what: &str| -> Result<()> {
357        if v.is_finite() && v > 0.0 {
358            Ok(())
359        } else {
360            Err(Error::Usage(format!(
361                "{what} must be a number > 0 (got {v})"
362            )))
363        }
364    };
365    match req.tolerance {
366        Tolerance::Ppm(p) => pos(p, "--ppm")?,
367        Tolerance::Da(d) => pos(d, "--da")?,
368    }
369    pos(req.transition_tolerance_da, "the transition tolerance")?;
370    pos(req.precursor_tolerance_da, "the precursor tolerance")?;
371    for t in &req.targets {
372        match t {
373            Target::Xic { mz } => pos(*mz, "an XIC m/z")?,
374            Target::Srm { q1, q3 } => {
375                pos(*q1, "Q1")?;
376                pos(*q3, "Q3")?;
377            }
378            _ => {}
379        }
380    }
381    if let Some(p) = req.precursor_mz {
382        pos(p, "the precursor m/z")?;
383    }
384    for (r, what) in [
385        (req.rt_range_min, "the retention-time range"),
386        (req.mz_range, "the m/z range"),
387    ] {
388        if let Some([a, b]) = r
389            && !(a.is_finite() && b.is_finite() && a < b)
390        {
391            return Err(Error::Usage(format!(
392                "{what} must be two numbers, low < high"
393            )));
394        }
395    }
396    Ok(())
397}
398
399/// Extract the requested chromatograms. Spectra are streamed once for all spectrum-derived
400/// targets, split across threads when `ctx.opener` can open extra handles (the result does not
401/// depend on the number of threads).
402pub fn extract(
403    ds: &mut dyn Dataset,
404    info: &FileInfo,
405    req: &ChromRequest,
406    ctx: &ReadContext<'_>,
407) -> Result<ChromatogramOutput> {
408    validate(req)?;
409    let targets: Vec<Target> = if req.targets.is_empty() {
410        vec![if info.spectra.is_empty() && !info.traces.is_empty() {
411            Target::Trace {
412                trace: 0,
413                channel: None,
414            }
415        } else {
416            Target::Tic
417        }]
418    } else {
419        req.targets.clone()
420    };
421    let mut out = ChromatogramOutput {
422        path: info.path.clone(),
423        format: info.format.id.clone(),
424        ..ChromatogramOutput::default()
425    };
426    let run = info.spectra.iter().find(|s| s.index == req.run);
427    if !info.spectra.is_empty() && run.is_none() {
428        return Err(Error::Usage(format!(
429            "run {} out of range (file has {} spectra runs)",
430            req.run,
431            info.spectra.len()
432        )));
433    }
434    let has_scans = run.is_some_and(|r| r.scan_count > 0);
435    // A run without MS1 scans (SRM/MRM-only, all-MS/MS): without an explicit level, TIC, BPC and
436    // XICs use the lowest level present, as the instrument's own TIC does.
437    let lowered: ChromRequest;
438    let req = match run {
439        Some(r)
440            if req.ms_level.is_none()
441                && req.precursor_mz.is_none()
442                && !r.ms_levels.is_empty()
443                && !r.ms_levels.contains(&1) =>
444        {
445            let mut q = req.clone();
446            q.ms_level = r.ms_levels.iter().copied().min();
447            out.notes.push(format!(
448                "the run has no MS1 scans: TIC/BPC/XIC use its MS{} scans",
449                q.ms_level.unwrap_or(2)
450            ));
451            lowered = q;
452            &lowered
453        }
454        _ => req,
455    };
456    // decide where each target comes from
457    let mut slots: Vec<Option<Chromatogram>> = vec![None; targets.len()];
458    let mut from_spectra: Vec<(usize, Target)> = Vec::new();
459    for (k, t) in targets.iter().enumerate() {
460        match t {
461            Target::Trace { trace, channel } => {
462                slots[k] = Some(from_trace(ds, info, *trace, *channel, req)?);
463            }
464            Target::Tic | Target::Bpc if !has_scans => {
465                slots[k] = Some(stored_total(ds, info, t, req)?);
466            }
467            Target::Srm { q1, q3 } if !has_scans || !has_msn(info, req.run) => {
468                slots[k] = Some(stored_transition(ds, info, *q1, *q3, req)?);
469            }
470            Target::Xic { .. } if !has_scans => {
471                return Err(Error::unsupported(
472                    "chromatogram",
473                    format!("extracted-ion chromatograms of a {} file", info.format.name),
474                    "This file holds no mass spectra (see `openreadout info` → spectra[]); \
475                     XICs need spectra. Use --trace for its stored chromatograms.",
476                ));
477            }
478            _ => from_spectra.push((k, t.clone())),
479        }
480    }
481    if !from_spectra.is_empty() {
482        let run = run.ok_or_else(|| Error::Other("no spectra run".into()))?;
483        let (chroms, read) = stream_spectra(ds, run.scan_count, &from_spectra, req, ctx)?;
484        out.run = Some(req.run);
485        out.spectra_read = read;
486        for ((k, _), c) in from_spectra.iter().zip(chroms) {
487            slots[*k] = Some(c);
488        }
489    }
490    out.chromatograms = slots.into_iter().flatten().collect();
491    for c in &mut out.chromatograms {
492        c.summarize();
493        if c.points == 0 && !c.notes.iter().any(|n| n.contains("no retention time")) {
494            c.notes.push(
495                "no points: no scan matched the filters (see `info` → spectra[].extra for the \
496                 MS levels, polarities and scan filters present)"
497                    .into(),
498            );
499        }
500    }
501    Ok(out)
502}
503
504fn has_msn(info: &FileInfo, run: u32) -> bool {
505    info.spectra
506        .iter()
507        .find(|s| s.index == run)
508        .is_some_and(|r| r.ms_levels.is_empty() || r.ms_levels.iter().any(|&l| l >= 2))
509}
510
511fn default_level(req: &ChromRequest) -> u32 {
512    req.ms_level
513        .unwrap_or(if req.precursor_mz.is_some() { 2 } else { 1 })
514}
515
516/// A point with the matched precursor/product (SRM only).
517#[derive(Debug, Clone, Copy)]
518struct Pt {
519    rt: f64,
520    v: f64,
521    q1: f64,
522    q3: f64,
523}
524
525#[derive(Debug, Default, Clone)]
526struct Acc {
527    pts: Vec<Pt>,
528    centroid: u64,
529    profile: u64,
530    recorded: u64,
531    computed: u64,
532    /// Scans that matched but whose file states no retention time: left out.
533    no_rt: u64,
534}
535
536impl Acc {
537    fn merge(&mut self, o: Acc) {
538        self.pts.extend(o.pts);
539        self.centroid += o.centroid;
540        self.profile += o.profile;
541        self.recorded += o.recorded;
542        self.computed += o.computed;
543        self.no_rt += o.no_rt;
544    }
545}
546
547fn polarity_ok(sp: &Spectrum, req: &ChromRequest) -> bool {
548    req.polarity.is_none_or(|p| sp.polarity == p.id())
549}
550
551/// A scan without a retention time passes the time window here: it is counted (and left out)
552/// where a point would be placed, so the note can say how many matching scans had none.
553fn common_ok(sp: &Spectrum, req: &ChromRequest) -> bool {
554    let in_window = match sp.rt_s {
555        Some(t) => req
556            .rt_range_min
557            .is_none_or(|[a, b]| t / 60.0 >= a && t / 60.0 <= b),
558        None => true,
559    };
560    in_window
561        && polarity_ok(sp, req)
562        && req.scan_filter.as_ref().is_none_or(|f| {
563            sp.scan_filter
564                .as_ref()
565                .is_some_and(|s| s.to_ascii_lowercase().contains(&f.to_ascii_lowercase()))
566        })
567}
568
569/// Sum and max of the intensities with `lo ≤ m/z ≤ hi` (m/z ascending).
570fn window(sp: &Spectrum, lo: f64, hi: f64) -> (f64, f64) {
571    let start = sp.mz.partition_point(|&m| m < lo);
572    let mut sum = 0.0;
573    let mut max = 0.0f64;
574    for (m, v) in sp.mz[start..]
575        .iter()
576        .zip(sp.intensity.get(start..).unwrap_or(&[]))
577    {
578        if *m > hi {
579            break;
580        }
581        let v = f64::from(*v);
582        sum += v;
583        max = max.max(v);
584    }
585    (sum, max)
586}
587
588fn visit(sp: &Spectrum, targets: &[(usize, Target)], req: &ChromRequest, accs: &mut [Acc]) {
589    if !common_ok(sp, req) {
590        return;
591    }
592    let rt = sp.rt_s.map(|t| t / 60.0);
593    let level = default_level(req);
594    let precursor_ok = |sp: &Spectrum| {
595        req.precursor_mz.is_none_or(|p| {
596            sp.precursor_mz
597                .is_some_and(|x| (x - p).abs() <= req.precursor_tolerance_da)
598        })
599    };
600    for ((_, t), acc) in targets.iter().zip(accs.iter_mut()) {
601        let v = match t {
602            Target::Tic | Target::Bpc | Target::Xic { .. } => {
603                if sp.ms_level != level || !precursor_ok(sp) {
604                    continue;
605                }
606                if rt.is_none() {
607                    acc.no_rt += 1;
608                    continue;
609                }
610                match t {
611                    Target::Tic => {
612                        if let Some([a, b]) = req.mz_range {
613                            acc.computed += 1;
614                            window(sp, a, b).0
615                        } else if let Some(v) = sp.total_ion_current {
616                            acc.recorded += 1;
617                            v
618                        } else {
619                            acc.computed += 1;
620                            sp.intensity.iter().map(|&v| f64::from(v)).sum()
621                        }
622                    }
623                    Target::Bpc => {
624                        if let Some([a, b]) = req.mz_range {
625                            acc.computed += 1;
626                            window(sp, a, b).1
627                        } else if let Some(v) = sp.base_peak_intensity {
628                            acc.recorded += 1;
629                            v
630                        } else {
631                            acc.computed += 1;
632                            sp.intensity
633                                .iter()
634                                .fold(0.0f64, |m, &v| m.max(f64::from(v)))
635                        }
636                    }
637                    Target::Xic { mz } => {
638                        let d = req.tolerance.half_width(*mz);
639                        let (s, m) = window(sp, mz - d, mz + d);
640                        match req.aggregate {
641                            Aggregate::Sum => s,
642                            Aggregate::Max => m,
643                        }
644                    }
645                    _ => unreachable!("matched above"),
646                }
647            }
648            Target::Srm { q1, q3 } => {
649                if sp.ms_level < 2 || req.ms_level.is_some_and(|l| sp.ms_level != l) {
650                    continue;
651                }
652                let tol = req.transition_tolerance_da;
653                // A record that measures several precursors (Shimadzu MRM events) gives each
654                // point its own precursor; otherwise the spectrum's precursor applies to all.
655                let point_q1 = point_precursors(sp);
656                if point_q1.is_none() && sp.precursor_mz.is_none_or(|p| (p - q1).abs() > tol) {
657                    continue;
658                }
659                let start = sp.mz.partition_point(|&m| m < q3 - tol);
660                let best = sp.mz[start..]
661                    .iter()
662                    .zip(sp.intensity.get(start..).unwrap_or(&[]))
663                    .enumerate()
664                    .take_while(|(_, (m, _))| **m <= q3 + tol)
665                    .filter_map(|(k, (m, v))| {
666                        let p = match &point_q1 {
667                            Some(q) => *q.get(start + k)?,
668                            None => sp.precursor_mz?,
669                        };
670                        ((p - q1).abs() <= tol).then_some((m, v, p))
671                    })
672                    .min_by(|a, b| (a.0 - q3).abs().total_cmp(&(b.0 - q3).abs()));
673                let Some((m, v, p)) = best else { continue };
674                let Some(rt) = rt else {
675                    acc.no_rt += 1;
676                    continue;
677                };
678                if sp.centroided {
679                    acc.centroid += 1;
680                } else {
681                    acc.profile += 1;
682                }
683                acc.pts.push(Pt {
684                    rt,
685                    v: f64::from(*v),
686                    q1: p,
687                    q3: *m,
688                });
689                continue;
690            }
691            Target::Trace { .. } => continue,
692        };
693        // counted as `no_rt` above, before any value was taken
694        let Some(rt) = rt else { continue };
695        if sp.centroided {
696            acc.centroid += 1;
697        } else {
698            acc.profile += 1;
699        }
700        acc.pts.push(Pt {
701            rt,
702            v,
703            q1: 0.0,
704            q3: 0.0,
705        });
706    }
707}
708
709/// Each point's own precursor m/z (`extra.point_precursors_mz`, one per point), when the
710/// spectrum records one.
711fn point_precursors(sp: &Spectrum) -> Option<Vec<f64>> {
712    let v: Vec<f64> = sp
713        .extra
714        .get("point_precursors_mz")?
715        .as_array()?
716        .iter()
717        .filter_map(serde_json::Value::as_f64)
718        .collect();
719    (v.len() == sp.mz.len()).then_some(v)
720}
721
722/// Stream every spectrum of the run once.
723fn stream_spectra(
724    ds: &mut dyn Dataset,
725    scan_count: u64,
726    targets: &[(usize, Target)],
727    req: &ChromRequest,
728    ctx: &ReadContext<'_>,
729) -> Result<(Vec<Chromatogram>, u64)> {
730    let view = if req.profile {
731        SpectrumView::Primary
732    } else {
733        SpectrumView::Centroid
734    };
735    let levels = ds.spectrum_ms_levels(req.run)?;
736    let want_level = default_level(req);
737    let needed = |i: u64| -> bool {
738        let Some(lv) = &levels else { return true };
739        let Some(&l) = lv.get(i as usize) else {
740            return true;
741        };
742        targets.iter().any(|(_, t)| match t {
743            Target::Srm { .. } => l >= 2 && req.ms_level.is_none_or(|x| x == l),
744            _ => l == want_level,
745        })
746    };
747    // Each worker opens its own handle on the file, which for a large run costs as much memory
748    // as the first: count the spectra that are decoded, not every scan of the run.
749    let to_decode = match &levels {
750        Some(_) => (0..scan_count).filter(|&i| needed(i)).count() as u64,
751        None => scan_count,
752    };
753    let workers = if ctx.opener.is_some() {
754        (rayon::current_num_threads() as u64)
755            .min(to_decode / MIN_SCANS_PER_WORKER)
756            .max(1)
757    } else {
758        1
759    };
760    let done = AtomicU64::new(0);
761    let read = AtomicU64::new(0);
762    let run_chunk = |d: &mut dyn Dataset, lo: u64, hi: u64| -> Result<Vec<Acc>> {
763        let mut accs = vec![Acc::default(); targets.len()];
764        for i in lo..hi {
765            if needed(i) {
766                let sp = d.read_spectrum_view(req.run, i, view)?;
767                read.fetch_add(1, Ordering::Relaxed);
768                visit(&sp, targets, req, &mut accs);
769            }
770            let n = done.fetch_add(1, Ordering::Relaxed) + 1;
771            if n.is_multiple_of(PROGRESS_EVERY) {
772                ctx.report(n, scan_count);
773            }
774        }
775        Ok(accs)
776    };
777    let bounds: Vec<(u64, u64)> = (0..workers)
778        .map(|w| (w * scan_count / workers, (w + 1) * scan_count / workers))
779        .collect();
780    let mut results: Vec<Option<Result<Vec<Acc>>>> = (0..workers).map(|_| None).collect();
781    if workers == 1 {
782        results[0] = Some(run_chunk(ds, 0, scan_count));
783    } else {
784        let opener = ctx.opener.ok_or_else(|| Error::Other("no opener".into()))?;
785        let (first, rest) = results.split_at_mut(1);
786        let run_chunk = &run_chunk;
787        rayon::scope(|s| {
788            let (lo, hi) = bounds[0];
789            let slot = &mut first[0];
790            s.spawn(move |_| *slot = Some(run_chunk(ds, lo, hi)));
791            for (slot, &(lo, hi)) in rest.iter_mut().zip(&bounds[1..]) {
792                s.spawn(move |_| {
793                    *slot = Some(opener().and_then(|mut d| run_chunk(d.as_mut(), lo, hi)));
794                });
795            }
796        });
797    }
798    let mut accs = vec![Acc::default(); targets.len()];
799    for r in results {
800        let part = r.ok_or_else(|| Error::Other("worker did not run".into()))??;
801        for (a, p) in accs.iter_mut().zip(part) {
802            a.merge(p);
803        }
804    }
805    ctx.report(scan_count, scan_count);
806    let chroms = targets
807        .iter()
808        .zip(accs)
809        .map(|((_, t), a)| finish_spectra(t, a, req))
810        .collect();
811    Ok((chroms, read.load(Ordering::Relaxed)))
812}
813
814/// Keep the points whose `key` (precursor or product) is the distinct value nearest `target`.
815fn nearest_group(
816    pts: &mut Vec<Pt>,
817    key: fn(&Pt) -> f64,
818    target: f64,
819    what: &str,
820) -> Option<String> {
821    let mut values: Vec<f64> = pts.iter().map(key).collect();
822    values.sort_by(f64::total_cmp);
823    values.dedup_by(|a, b| (*a - *b).abs() < 1e-4);
824    if values.len() < 2 {
825        return None;
826    }
827    let best = values
828        .iter()
829        .copied()
830        .min_by(|a, b| (a - target).abs().total_cmp(&(b - target).abs()))?;
831    pts.retain(|p| (key(p) - best).abs() < 1e-4);
832    let others: Vec<String> = values
833        .iter()
834        .filter(|v| (**v - best).abs() >= 1e-4)
835        .map(|v| num(*v))
836        .collect();
837    Some(format!(
838        "{what} {} matched; also within the tolerance and left out: {}",
839        num(best),
840        others.join(", ")
841    ))
842}
843
844fn finish_spectra(t: &Target, mut a: Acc, req: &ChromRequest) -> Chromatogram {
845    let mut c = Chromatogram {
846        source: "spectra".into(),
847        polarity: req.polarity.map(|p| p.id().to_string()),
848        scan_filter: req.scan_filter.clone(),
849        intensity_unit: Some("counts".into()),
850        ..Chromatogram::default()
851    };
852    let level = default_level(req);
853    let mut suffix = String::new();
854    if let Some(p) = req.precursor_mz {
855        suffix.push_str(&format!(" (MS{level} of {})", num(p)));
856    }
857    if let Some(p) = req.polarity {
858        suffix.push_str(if p == Polarity::Positive {
859            " +"
860        } else {
861            " −"
862        });
863    }
864    match t {
865        Target::Tic | Target::Bpc => {
866            let tic = matches!(t, Target::Tic);
867            c.kind = if tic { "tic" } else { "bpc" }.into();
868            c.label = format!("{}{suffix}", if tic { "TIC" } else { "BPC" });
869            if let Some([lo, hi]) = req.mz_range {
870                c.label.push_str(&format!(" m/z {}–{}", num(lo), num(hi)));
871                c.mz_range = req.mz_range;
872            }
873            c.ms_level = Some(level);
874            c.intensity_source = Some(
875                match (a.recorded > 0, a.computed > 0) {
876                    (true, false) => "recorded",
877                    (false, true) => "computed",
878                    (true, true) => "mixed",
879                    (false, false) => "none",
880                }
881                .into(),
882            );
883        }
884        Target::Xic { mz } => {
885            let d = req.tolerance.half_width(*mz);
886            c.kind = "xic".into();
887            c.label = format!("XIC {} {}{suffix}", num(*mz), req.tolerance.label());
888            c.mz = Some(*mz);
889            c.mz_window = Some([mz - d, mz + d]);
890            c.ms_level = Some(level);
891            c.aggregation = Some(
892                match req.aggregate {
893                    Aggregate::Sum => "sum",
894                    Aggregate::Max => "max",
895                }
896                .into(),
897            );
898        }
899        Target::Srm { q1, q3 } => {
900            c.kind = "srm".into();
901            c.label = format!("SRM {} > {}", num(*q1), num(*q3));
902            c.ms_level = req.ms_level;
903            if let Some(n) = nearest_group(&mut a.pts, |p| p.q1, *q1, "precursor") {
904                c.notes.push(n);
905            }
906            if let Some(n) = nearest_group(&mut a.pts, |p| p.q3, *q3, "product") {
907                c.notes.push(n);
908            }
909            c.precursor_mz = a.pts.first().map(|p| p.q1);
910            c.product_mz = a.pts.first().map(|p| p.q3);
911        }
912        Target::Trace { .. } => {}
913    }
914    c.centroid_scans = Some(a.centroid);
915    c.profile_scans = Some(a.profile);
916    if a.no_rt > 0 {
917        c.notes.push(format!(
918            "{} matching scan{} left out: the file states no retention time for {}",
919            a.no_rt,
920            if a.no_rt == 1 { " is" } else { "s are" },
921            if a.no_rt == 1 { "it" } else { "them" }
922        ));
923    }
924    if a.pts.windows(2).any(|w| w[1].rt < w[0].rt) {
925        a.pts.sort_by(|x, y| x.rt.total_cmp(&y.rt));
926    }
927    c.rt_min = a.pts.iter().map(|p| p.rt).collect();
928    c.intensity = a.pts.iter().map(|p| p.v).collect();
929    c
930}
931
932/// Retention time in minutes of every sample of a trace sweep, and the intensity channel.
933fn from_trace(
934    ds: &mut dyn Dataset,
935    info: &FileInfo,
936    trace: u32,
937    channel: Option<u32>,
938    req: &ChromRequest,
939) -> Result<Chromatogram> {
940    let t = info
941        .traces
942        .iter()
943        .find(|t| t.index == trace)
944        .ok_or_else(|| {
945            if info.traces.is_empty() {
946                Error::unsupported(
947                    "chromatogram",
948                    format!("stored chromatograms of a {} file", info.format.name),
949                    "This file holds no traces; see `openreadout info`.",
950                )
951            } else {
952                Error::Usage(format!(
953                    "trace {trace} out of range (file has {} traces)",
954                    info.traces.len()
955                ))
956            }
957        })?;
958    if req.sweep >= t.sweep_count {
959        return Err(Error::Usage(format!(
960            "sweep {} out of range (trace {trace} has {} sweeps)",
961            req.sweep, t.sweep_count
962        )));
963    }
964    let time_ch = t
965        .channels
966        .iter()
967        .position(|c| c.name == "time" && t.sample_rate_hz == 0.0);
968    let ch = match channel {
969        Some(c) => {
970            if c as usize >= t.channels.len() {
971                return Err(Error::Usage(format!(
972                    "channel {c} out of range (trace {trace} has {} channels)",
973                    t.channels.len()
974                )));
975            }
976            c as usize
977        }
978        None => (0..t.channels.len())
979            .find(|&c| Some(c) != time_ch)
980            .ok_or_else(|| Error::Usage(format!("trace {trace} has no signal channel")))?,
981    };
982    let axis = time_axis(t);
983    if time_ch.is_none() && matches!(axis, Axis::None) {
984        return Err(Error::unsupported(
985            "chromatogram",
986            format!(
987                "trace {trace} ({}) as a chromatogram: it has no time axis",
988                t.name.as_deref().unwrap_or("unnamed")
989            ),
990            "Only chromatograms and time signals can be integrated; see `info` → traces[].extra.axis.",
991        ));
992    }
993    let n = openreadout_core::trace::sweep_samples(t, req.sweep);
994    let mut rt = Vec::with_capacity(n.min(1 << 24) as usize);
995    let mut y = Vec::with_capacity(n.min(1 << 24) as usize);
996    let time_scale = time_ch.map_or(1.0 / 60.0, |c| {
997        if t.channels[c].unit.as_deref() == Some("min") {
998            1.0
999        } else {
1000            1.0 / 60.0
1001        }
1002    });
1003    let mut pos = 0u64;
1004    while pos < n {
1005        let want = TRACE_CHUNK.min(n - pos);
1006        let tr = ds.read_trace(trace, req.sweep, pos, want)?;
1007        let col = tr
1008            .channels
1009            .get(ch)
1010            .ok_or_else(|| Error::Other(format!("reader returned no channel {ch}")))?;
1011        let got = col.len() as u64;
1012        for (i, &v) in col.iter().enumerate() {
1013            let k = pos + i as u64;
1014            let tm = match (time_ch, &axis) {
1015                (Some(c), _) => tr
1016                    .channels
1017                    .get(c)
1018                    .and_then(|tc| tc.get(i))
1019                    .map_or(f64::NAN, |s| s * time_scale),
1020                (None, Axis::Minutes { first, step }) => first + step * k as f64,
1021                (None, _) => f64::NAN,
1022            };
1023            if req.rt_range_min.is_none_or(|[a, b]| tm >= a && tm <= b) && tm.is_finite() {
1024                rt.push(tm);
1025                y.push(v);
1026            }
1027        }
1028        pos += got;
1029        if got < want || got == 0 {
1030            break;
1031        }
1032    }
1033    let name = t.name.clone().unwrap_or_else(|| format!("trace {trace}"));
1034    let chan = &t.channels[ch];
1035    let label = if t.channels.len() > 1 + usize::from(time_ch.is_some()) {
1036        format!("{name}: {}", chan.name)
1037    } else {
1038        name
1039    };
1040    let mut c = Chromatogram {
1041        label,
1042        kind: "trace".into(),
1043        source: format!("trace {trace}"),
1044        intensity_unit: chan.unit.clone(),
1045        rt_min: rt,
1046        intensity: y,
1047        ..Chromatogram::default()
1048    };
1049    if let Some(p) = t
1050        .extra
1051        .get("precursor_mz")
1052        .and_then(serde_json::Value::as_f64)
1053    {
1054        c.precursor_mz = Some(p);
1055    }
1056    if let Some(p) = t
1057        .extra
1058        .get("product_mz")
1059        .and_then(serde_json::Value::as_f64)
1060    {
1061        c.product_mz = Some(p);
1062    }
1063    Ok(c)
1064}
1065
1066enum Axis {
1067    Minutes { first: f64, step: f64 },
1068    None,
1069}
1070
1071fn time_axis(t: &TraceInfo) -> Axis {
1072    if let Some(a) = t.extra.get("axis") {
1073        let q = a.get("quantity").and_then(|v| v.as_str()).unwrap_or("");
1074        let unit = a.get("unit").and_then(|v| v.as_str()).unwrap_or("");
1075        let first = a.get("first").and_then(serde_json::Value::as_f64);
1076        let step = a.get("step").and_then(serde_json::Value::as_f64);
1077        if matches!(q, "retention_time" | "time")
1078            && let (Some(first), Some(step)) = (first, step)
1079        {
1080            let k = match unit {
1081                "min" => 1.0,
1082                "s" => 1.0 / 60.0,
1083                "ms" => 1.0 / 60_000.0,
1084                "h" => 60.0,
1085                _ => return Axis::None,
1086            };
1087            return Axis::Minutes {
1088                first: first * k,
1089                step: step * k,
1090            };
1091        }
1092        // a retention-volume axis (ÄKTA/UNICORN) rides on a regular time base: fall through
1093        if !q.is_empty() && !matches!(q, "retention_time" | "time" | "retention_volume") {
1094            return Axis::None;
1095        }
1096    }
1097    if t.sample_rate_hz > 0.0 && t.sample_rate_hz.is_finite() {
1098        return Axis::Minutes {
1099            first: t.start_s.unwrap_or(0.0) / 60.0,
1100            step: 1.0 / t.sample_rate_hz / 60.0,
1101        };
1102    }
1103    Axis::None
1104}
1105
1106/// A stored TIC/BPC trace, for files without usable spectra.
1107fn stored_total(
1108    ds: &mut dyn Dataset,
1109    info: &FileInfo,
1110    t: &Target,
1111    req: &ChromRequest,
1112) -> Result<Chromatogram> {
1113    let (name, kind) = match t {
1114        Target::Bpc => ("BPC", "bpc"),
1115        _ => ("TIC", "tic"),
1116    };
1117    let found = info.traces.iter().find(|tr| {
1118        tr.name.as_deref().is_some_and(|n| {
1119            let u = n.to_ascii_uppercase();
1120            u == name || u.starts_with(&format!("{name} "))
1121        }) || tr
1122            .extra
1123            .get("chromatogram_type")
1124            .and_then(|v| v.as_str())
1125            .is_some_and(|v| {
1126                v.starts_with(if kind == "tic" {
1127                    "total ion current"
1128                } else {
1129                    "base peak"
1130                })
1131            })
1132    });
1133    if let Some(tr) = found {
1134        let mut c = from_trace(ds, info, tr.index, None, req)?;
1135        c.kind = kind.into();
1136        c.label = name.into();
1137        c.notes.push(format!(
1138            "read from the file's stored {name} (trace {}); no usable spectra",
1139            tr.index
1140        ));
1141        return Ok(c);
1142    }
1143    // an MRM table with a stored TIC column
1144    for tab in &info.tables {
1145        if kind == "tic"
1146            && let (Some(tcol), Some(vcol)) = (
1147                tab.columns.iter().position(|c| c.name == "rt_min"),
1148                tab.columns.iter().position(|c| c.name == "tic_stored"),
1149            )
1150        {
1151            let mut c = from_table(ds, tab.index, tab.row_count, tcol, vcol, req)?;
1152            c.kind = "tic".into();
1153            c.label = "TIC".into();
1154            c.source = format!("table {}", tab.index);
1155            return Ok(c);
1156        }
1157    }
1158    Err(Error::unsupported(
1159        "chromatogram",
1160        format!("a {name} of a {} file", info.format.name),
1161        "This file has no spectra and no stored chromatogram of that kind; see `openreadout info`.",
1162    ))
1163}
1164
1165fn from_table(
1166    ds: &mut dyn Dataset,
1167    table: u32,
1168    rows: u64,
1169    tcol: usize,
1170    vcol: usize,
1171    req: &ChromRequest,
1172) -> Result<Chromatogram> {
1173    let tab = ds.read_table(table, 0, rows)?;
1174    let (Some(tc), Some(vc)) = (tab.columns.get(tcol), tab.columns.get(vcol)) else {
1175        return Err(Error::Other(format!("table {table}: missing columns")));
1176    };
1177    let mut c = Chromatogram {
1178        source: format!("table {table}"),
1179        intensity_unit: Some("counts".into()),
1180        ..Chromatogram::default()
1181    };
1182    for (&t, &v) in tc.iter().zip(vc) {
1183        if t.is_finite() && req.rt_range_min.is_none_or(|[a, b]| t >= a && t <= b) {
1184            c.rt_min.push(t);
1185            c.intensity.push(v);
1186        }
1187    }
1188    Ok(c)
1189}
1190
1191/// Parse an MRM column name `Q1 > Q3`.
1192fn transition_of(name: &str) -> Option<(f64, f64)> {
1193    let (a, b) = name.split_once('>')?;
1194    Some((a.trim().parse().ok()?, b.trim().parse().ok()?))
1195}
1196
1197/// An SRM transition from stored chromatograms (traces carrying `precursor_mz`/`product_mz`) or
1198/// MRM table columns named `Q1 > Q3`.
1199fn stored_transition(
1200    ds: &mut dyn Dataset,
1201    info: &FileInfo,
1202    q1: f64,
1203    q3: f64,
1204    req: &ChromRequest,
1205) -> Result<Chromatogram> {
1206    let tol = req.transition_tolerance_da;
1207    let dist = |a: f64, b: f64| (a - q1).abs().max((b - q3).abs());
1208    let mut traces: Vec<(&TraceInfo, f64, f64)> = info
1209        .traces
1210        .iter()
1211        .filter_map(|t| {
1212            let p = t.extra.get("precursor_mz")?.as_f64()?;
1213            let d = t.extra.get("product_mz")?.as_f64()?;
1214            (dist(p, d) <= tol).then_some((t, p, d))
1215        })
1216        .collect();
1217    if !traces.is_empty() {
1218        let best = traces
1219            .iter()
1220            .map(|x| dist(x.1, x.2))
1221            .fold(f64::INFINITY, f64::min);
1222        traces.retain(|x| (dist(x.1, x.2) - best).abs() < 1e-9);
1223        let (mut rt, mut y) = (Vec::new(), Vec::new());
1224        let mut sources = Vec::new();
1225        let mut unit = None;
1226        for (t, _, _) in &traces {
1227            let c = from_trace(ds, info, t.index, None, req)?;
1228            rt.extend(c.rt_min);
1229            y.extend(c.intensity);
1230            unit = c.intensity_unit;
1231            sources.push(t.index.to_string());
1232        }
1233        let mut pts: Vec<(f64, f64)> = rt.into_iter().zip(y).collect();
1234        pts.sort_by(|a, b| a.0.total_cmp(&b.0));
1235        let (p, d) = (traces[0].1, traces[0].2);
1236        let mut c = Chromatogram {
1237            label: format!("SRM {} > {}", num(q1), num(q3)),
1238            kind: "srm".into(),
1239            source: format!("trace {}", sources.join("+")),
1240            precursor_mz: Some(p),
1241            product_mz: Some(d),
1242            intensity_unit: unit.or(Some("counts".into())),
1243            ..Chromatogram::default()
1244        };
1245        if traces.len() > 1 {
1246            c.notes.push(format!(
1247                "{} stored chromatograms of this transition (scheduled windows) merged",
1248                traces.len()
1249            ));
1250        }
1251        (c.rt_min, c.intensity) = pts.into_iter().unzip();
1252        return Ok(c);
1253    }
1254    let mut best: Option<(u32, u64, usize, usize, f64, f64)> = None;
1255    for tab in &info.tables {
1256        let Some(tcol) = tab.columns.iter().position(|c| c.name == "rt_min") else {
1257            continue;
1258        };
1259        for (k, col) in tab.columns.iter().enumerate() {
1260            if let Some((a, b)) = transition_of(&col.name)
1261                && dist(a, b) <= tol
1262                && best.is_none_or(|x| dist(a, b) < dist(x.4, x.5))
1263            {
1264                best = Some((tab.index, tab.row_count, tcol, k, a, b));
1265            }
1266        }
1267    }
1268    if let Some((table, rows, tcol, vcol, a, b)) = best {
1269        let mut c = from_table(ds, table, rows, tcol, vcol, req)?;
1270        c.label = format!("SRM {} > {}", num(q1), num(q3));
1271        c.kind = "srm".into();
1272        c.precursor_mz = Some(a);
1273        c.product_mz = Some(b);
1274        return Ok(c);
1275    }
1276    let mut hint = String::from(
1277        "No MS/MS spectra, stored SRM chromatogram or MRM table column matches this transition",
1278    );
1279    let known: Vec<String> = info
1280        .traces
1281        .iter()
1282        .filter_map(|t| {
1283            Some(format!(
1284                "{}>{}",
1285                num(t.extra.get("precursor_mz")?.as_f64()?),
1286                num(t.extra.get("product_mz")?.as_f64()?)
1287            ))
1288        })
1289        .chain(
1290            info.tables
1291                .iter()
1292                .flat_map(|t| t.columns.iter())
1293                .filter_map(|c| {
1294                    transition_of(&c.name).map(|(a, b)| format!("{}>{}", num(a), num(b)))
1295                }),
1296        )
1297        .take(12)
1298        .collect();
1299    if !known.is_empty() {
1300        hint.push_str(&format!(" (the file has e.g. {})", known.join(", ")));
1301    }
1302    hint.push_str("; widen --transition-tol or check `openreadout info`.");
1303    Err(Error::Unsupported {
1304        format: "chromatogram",
1305        feature: format!("SRM transition {} > {}", num(q1), num(q3)),
1306        hint: Some(hint),
1307    })
1308}
1309
1310#[cfg(test)]
1311#[allow(clippy::field_reassign_with_default)]
1312mod tests {
1313    use super::*;
1314
1315    fn sp(i: u64, rt_s: f64, level: u32, mz: Vec<f64>, it: Vec<f32>) -> Spectrum {
1316        Spectrum {
1317            index: i,
1318            scan_number: i + 1,
1319            ms_level: level,
1320            rt_s: Some(rt_s),
1321            polarity: "positive".into(),
1322            centroided: true,
1323            mz,
1324            intensity: it,
1325            ..Spectrum::default()
1326        }
1327    }
1328
1329    #[test]
1330    fn xic_window_and_aggregate() {
1331        let s = sp(
1332            0,
1333            60.0,
1334            1,
1335            vec![100.0, 100.0005, 100.002, 200.0],
1336            vec![1.0, 2.0, 4.0, 8.0],
1337        );
1338        let mut req = ChromRequest::default();
1339        req.targets = vec![
1340            Target::Xic { mz: 100.0 },
1341            Target::Tic,
1342            Target::Bpc,
1343            Target::Xic { mz: 150.0 },
1344        ];
1345        let t: Vec<(usize, Target)> = req.targets.iter().cloned().enumerate().collect();
1346        let mut accs = vec![Acc::default(); 4];
1347        visit(&s, &t, &req, &mut accs);
1348        assert_eq!(accs[0].pts[0].v, 3.0); // 10 ppm of 100 = ±0.001
1349        assert_eq!(accs[1].pts[0].v, 15.0);
1350        assert_eq!(accs[2].pts[0].v, 8.0);
1351        assert_eq!(accs[3].pts[0].v, 0.0);
1352        assert!((accs[0].pts[0].rt - 1.0).abs() < 1e-12);
1353        req.aggregate = Aggregate::Max;
1354        req.tolerance = Tolerance::Da(0.01);
1355        let mut accs = vec![Acc::default(); 4];
1356        visit(&s, &t, &req, &mut accs);
1357        assert_eq!(accs[0].pts[0].v, 4.0);
1358        // MS2 scans are not in an MS1 XIC; polarity filter
1359        let s2 = sp(1, 61.0, 2, vec![100.0], vec![5.0]);
1360        let mut accs = vec![Acc::default(); 4];
1361        visit(&s2, &t, &req, &mut accs);
1362        assert!(accs.iter().all(|a| a.pts.is_empty()));
1363        req.polarity = Some(Polarity::Negative);
1364        let mut accs = vec![Acc::default(); 4];
1365        visit(&s, &t, &req, &mut accs);
1366        assert!(accs.iter().all(|a| a.pts.is_empty()));
1367    }
1368
1369    /// A scan whose file states no retention time is left out of every chromatogram (never
1370    /// placed at 0), and the chromatogram says how many were.
1371    #[test]
1372    fn scans_without_retention_time_are_left_out() {
1373        let mut req = ChromRequest::default();
1374        req.targets = vec![Target::Tic, Target::Xic { mz: 100.0 }];
1375        req.rt_range_min = Some([0.0, 10.0]);
1376        let t: Vec<(usize, Target)> = req.targets.iter().cloned().enumerate().collect();
1377        let mut accs = vec![Acc::default(); 2];
1378        visit(&sp(0, 60.0, 1, vec![100.0], vec![2.0]), &t, &req, &mut accs);
1379        let mut none = sp(1, 0.0, 1, vec![100.0], vec![5.0]);
1380        none.rt_s = None;
1381        visit(&none, &t, &req, &mut accs);
1382        // an MS2 scan without a time is not a matching scan of an MS1 chromatogram
1383        let mut ms2 = sp(2, 0.0, 2, vec![100.0], vec![7.0]);
1384        ms2.rt_s = None;
1385        visit(&ms2, &t, &req, &mut accs);
1386        for a in &accs {
1387            assert_eq!(a.pts.len(), 1);
1388            assert_eq!(a.no_rt, 1);
1389        }
1390        assert_eq!(
1391            accs[0].computed, 1,
1392            "the TIC counts only the scan it placed"
1393        );
1394        let c = finish_spectra(&t[0].1, accs.remove(0), &req);
1395        assert_eq!(c.rt_min, vec![1.0]);
1396        assert!(
1397            c.notes
1398                .iter()
1399                .any(|n| n.starts_with("1 matching scan is left out")),
1400            "{:?}",
1401            c.notes
1402        );
1403    }
1404
1405    #[test]
1406    fn srm_nearest_precursor_and_product() {
1407        let mut req = ChromRequest::default();
1408        req.targets = vec![Target::Srm {
1409            q1: 115.0,
1410            q3: 71.1,
1411        }];
1412        let t: Vec<(usize, Target)> = req.targets.iter().cloned().enumerate().collect();
1413        let mut accs = vec![Acc::default()];
1414        for (i, (p, prods)) in [
1415            (115.007, vec![(27.28, 1.0f32), (71.103, 10.0)]),
1416            (115.05, vec![(71.143, 20.0)]),
1417            (115.007, vec![(27.28, 2.0), (71.103, 30.0)]),
1418        ]
1419        .into_iter()
1420        .enumerate()
1421        {
1422            let mut s = sp(i as u64, i as f64 * 6.0, 2, Vec::new(), Vec::new());
1423            s.precursor_mz = Some(p);
1424            (s.mz, s.intensity) = prods.into_iter().unzip();
1425            visit(&s, &t, &req, &mut accs);
1426        }
1427        let c = finish_spectra(&t[0].1, accs.remove(0), &req);
1428        assert_eq!(c.intensity, vec![10.0, 30.0]);
1429        assert_eq!(c.precursor_mz, Some(115.007));
1430        assert_eq!(c.product_mz, Some(71.103));
1431        assert!(c.notes[0].contains("115.05"), "{:?}", c.notes);
1432    }
1433
1434    /// A Shimadzu MRM event measuring two precursors with the same product: each point's own
1435    /// precursor decides which transition it belongs to.
1436    #[test]
1437    fn srm_per_point_precursors() {
1438        let mut req = ChromRequest::default();
1439        req.targets = vec![Target::Srm {
1440            q1: 496.3,
1441            q3: 184.1,
1442        }];
1443        let t: Vec<(usize, Target)> = req.targets.iter().cloned().enumerate().collect();
1444        let mut accs = vec![Acc::default()];
1445        for i in 0..2u64 {
1446            let mut s = sp(
1447                i,
1448                i as f64 * 6.0,
1449                2,
1450                vec![184.1, 184.1],
1451                vec![5.0, 7.0 + i as f32],
1452            );
1453            s.precursor_mz = Some(494.3);
1454            s.extra.insert(
1455                "point_precursors_mz".into(),
1456                serde_json::json!([494.3, 496.3]),
1457            );
1458            visit(&s, &t, &req, &mut accs);
1459        }
1460        let c = finish_spectra(&t[0].1, accs.remove(0), &req);
1461        assert_eq!(c.intensity, vec![7.0, 8.0]);
1462        assert_eq!(c.precursor_mz, Some(496.3));
1463    }
1464
1465    #[test]
1466    fn decimate_keeps_maxima() {
1467        let mut c = Chromatogram {
1468            rt_min: (0..100).map(f64::from).collect(),
1469            intensity: (0..100).map(|i| if i == 37 { 99.0 } else { 1.0 }).collect(),
1470            ..Chromatogram::default()
1471        };
1472        c.summarize();
1473        c.decimate(10);
1474        assert_eq!(c.rt_min.len(), 10);
1475        assert!(c.intensity.contains(&99.0));
1476        assert_eq!(c.points, 100);
1477        assert_eq!(c.apex_rt_min, Some(37.0));
1478    }
1479
1480    #[test]
1481    fn column_names_parse_as_transitions() {
1482        assert_eq!(transition_of("207.82 > 99.66"), Some((207.82, 99.66)));
1483        assert_eq!(transition_of("rt_min"), None);
1484    }
1485}