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 per worker thread.
34const MIN_SCANS_PER_WORKER: u64 = 200;
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                let Some(p) = sp.precursor_mz.filter(|p| (p - q1).abs() <= tol) else {
654                    continue;
655                };
656                let start = sp.mz.partition_point(|&m| m < q3 - tol);
657                let best = sp.mz[start..]
658                    .iter()
659                    .zip(sp.intensity.get(start..).unwrap_or(&[]))
660                    .take_while(|(m, _)| **m <= q3 + tol)
661                    .min_by(|a, b| (a.0 - q3).abs().total_cmp(&(b.0 - q3).abs()));
662                let Some((m, v)) = best else { continue };
663                let Some(rt) = rt else {
664                    acc.no_rt += 1;
665                    continue;
666                };
667                if sp.centroided {
668                    acc.centroid += 1;
669                } else {
670                    acc.profile += 1;
671                }
672                acc.pts.push(Pt {
673                    rt,
674                    v: f64::from(*v),
675                    q1: p,
676                    q3: *m,
677                });
678                continue;
679            }
680            Target::Trace { .. } => continue,
681        };
682        // counted as `no_rt` above, before any value was taken
683        let Some(rt) = rt else { continue };
684        if sp.centroided {
685            acc.centroid += 1;
686        } else {
687            acc.profile += 1;
688        }
689        acc.pts.push(Pt {
690            rt,
691            v,
692            q1: 0.0,
693            q3: 0.0,
694        });
695    }
696}
697
698/// Stream every spectrum of the run once.
699fn stream_spectra(
700    ds: &mut dyn Dataset,
701    scan_count: u64,
702    targets: &[(usize, Target)],
703    req: &ChromRequest,
704    ctx: &ReadContext<'_>,
705) -> Result<(Vec<Chromatogram>, u64)> {
706    let view = if req.profile {
707        SpectrumView::Primary
708    } else {
709        SpectrumView::Centroid
710    };
711    let levels = ds.spectrum_ms_levels(req.run)?;
712    let want_level = default_level(req);
713    let needed = |i: u64| -> bool {
714        let Some(lv) = &levels else { return true };
715        let Some(&l) = lv.get(i as usize) else {
716            return true;
717        };
718        targets.iter().any(|(_, t)| match t {
719            Target::Srm { .. } => l >= 2 && req.ms_level.is_none_or(|x| x == l),
720            _ => l == want_level,
721        })
722    };
723    let workers = if ctx.opener.is_some() {
724        (rayon::current_num_threads() as u64)
725            .min(scan_count / MIN_SCANS_PER_WORKER)
726            .max(1)
727    } else {
728        1
729    };
730    let done = AtomicU64::new(0);
731    let read = AtomicU64::new(0);
732    let run_chunk = |d: &mut dyn Dataset, lo: u64, hi: u64| -> Result<Vec<Acc>> {
733        let mut accs = vec![Acc::default(); targets.len()];
734        for i in lo..hi {
735            if needed(i) {
736                let sp = d.read_spectrum_view(req.run, i, view)?;
737                read.fetch_add(1, Ordering::Relaxed);
738                visit(&sp, targets, req, &mut accs);
739            }
740            let n = done.fetch_add(1, Ordering::Relaxed) + 1;
741            if n.is_multiple_of(PROGRESS_EVERY) {
742                ctx.report(n, scan_count);
743            }
744        }
745        Ok(accs)
746    };
747    let bounds: Vec<(u64, u64)> = (0..workers)
748        .map(|w| (w * scan_count / workers, (w + 1) * scan_count / workers))
749        .collect();
750    let mut results: Vec<Option<Result<Vec<Acc>>>> = (0..workers).map(|_| None).collect();
751    if workers == 1 {
752        results[0] = Some(run_chunk(ds, 0, scan_count));
753    } else {
754        let opener = ctx.opener.ok_or_else(|| Error::Other("no opener".into()))?;
755        let (first, rest) = results.split_at_mut(1);
756        let run_chunk = &run_chunk;
757        rayon::scope(|s| {
758            let (lo, hi) = bounds[0];
759            let slot = &mut first[0];
760            s.spawn(move |_| *slot = Some(run_chunk(ds, lo, hi)));
761            for (slot, &(lo, hi)) in rest.iter_mut().zip(&bounds[1..]) {
762                s.spawn(move |_| {
763                    *slot = Some(opener().and_then(|mut d| run_chunk(d.as_mut(), lo, hi)));
764                });
765            }
766        });
767    }
768    let mut accs = vec![Acc::default(); targets.len()];
769    for r in results {
770        let part = r.ok_or_else(|| Error::Other("worker did not run".into()))??;
771        for (a, p) in accs.iter_mut().zip(part) {
772            a.merge(p);
773        }
774    }
775    ctx.report(scan_count, scan_count);
776    let chroms = targets
777        .iter()
778        .zip(accs)
779        .map(|((_, t), a)| finish_spectra(t, a, req))
780        .collect();
781    Ok((chroms, read.load(Ordering::Relaxed)))
782}
783
784/// Keep the points whose `key` (precursor or product) is the distinct value nearest `target`.
785fn nearest_group(
786    pts: &mut Vec<Pt>,
787    key: fn(&Pt) -> f64,
788    target: f64,
789    what: &str,
790) -> Option<String> {
791    let mut values: Vec<f64> = pts.iter().map(key).collect();
792    values.sort_by(f64::total_cmp);
793    values.dedup_by(|a, b| (*a - *b).abs() < 1e-4);
794    if values.len() < 2 {
795        return None;
796    }
797    let best = values
798        .iter()
799        .copied()
800        .min_by(|a, b| (a - target).abs().total_cmp(&(b - target).abs()))?;
801    pts.retain(|p| (key(p) - best).abs() < 1e-4);
802    let others: Vec<String> = values
803        .iter()
804        .filter(|v| (**v - best).abs() >= 1e-4)
805        .map(|v| num(*v))
806        .collect();
807    Some(format!(
808        "{what} {} matched; also within the tolerance and left out: {}",
809        num(best),
810        others.join(", ")
811    ))
812}
813
814fn finish_spectra(t: &Target, mut a: Acc, req: &ChromRequest) -> Chromatogram {
815    let mut c = Chromatogram {
816        source: "spectra".into(),
817        polarity: req.polarity.map(|p| p.id().to_string()),
818        scan_filter: req.scan_filter.clone(),
819        intensity_unit: Some("counts".into()),
820        ..Chromatogram::default()
821    };
822    let level = default_level(req);
823    let mut suffix = String::new();
824    if let Some(p) = req.precursor_mz {
825        suffix.push_str(&format!(" (MS{level} of {})", num(p)));
826    }
827    if let Some(p) = req.polarity {
828        suffix.push_str(if p == Polarity::Positive {
829            " +"
830        } else {
831            " −"
832        });
833    }
834    match t {
835        Target::Tic | Target::Bpc => {
836            let tic = matches!(t, Target::Tic);
837            c.kind = if tic { "tic" } else { "bpc" }.into();
838            c.label = format!("{}{suffix}", if tic { "TIC" } else { "BPC" });
839            if let Some([lo, hi]) = req.mz_range {
840                c.label.push_str(&format!(" m/z {}–{}", num(lo), num(hi)));
841                c.mz_range = req.mz_range;
842            }
843            c.ms_level = Some(level);
844            c.intensity_source = Some(
845                match (a.recorded > 0, a.computed > 0) {
846                    (true, false) => "recorded",
847                    (false, true) => "computed",
848                    (true, true) => "mixed",
849                    (false, false) => "none",
850                }
851                .into(),
852            );
853        }
854        Target::Xic { mz } => {
855            let d = req.tolerance.half_width(*mz);
856            c.kind = "xic".into();
857            c.label = format!("XIC {} {}{suffix}", num(*mz), req.tolerance.label());
858            c.mz = Some(*mz);
859            c.mz_window = Some([mz - d, mz + d]);
860            c.ms_level = Some(level);
861            c.aggregation = Some(
862                match req.aggregate {
863                    Aggregate::Sum => "sum",
864                    Aggregate::Max => "max",
865                }
866                .into(),
867            );
868        }
869        Target::Srm { q1, q3 } => {
870            c.kind = "srm".into();
871            c.label = format!("SRM {} > {}", num(*q1), num(*q3));
872            c.ms_level = req.ms_level;
873            if let Some(n) = nearest_group(&mut a.pts, |p| p.q1, *q1, "precursor") {
874                c.notes.push(n);
875            }
876            if let Some(n) = nearest_group(&mut a.pts, |p| p.q3, *q3, "product") {
877                c.notes.push(n);
878            }
879            c.precursor_mz = a.pts.first().map(|p| p.q1);
880            c.product_mz = a.pts.first().map(|p| p.q3);
881        }
882        Target::Trace { .. } => {}
883    }
884    c.centroid_scans = Some(a.centroid);
885    c.profile_scans = Some(a.profile);
886    if a.no_rt > 0 {
887        c.notes.push(format!(
888            "{} matching scan{} left out: the file states no retention time for {}",
889            a.no_rt,
890            if a.no_rt == 1 { " is" } else { "s are" },
891            if a.no_rt == 1 { "it" } else { "them" }
892        ));
893    }
894    if a.pts.windows(2).any(|w| w[1].rt < w[0].rt) {
895        a.pts.sort_by(|x, y| x.rt.total_cmp(&y.rt));
896    }
897    c.rt_min = a.pts.iter().map(|p| p.rt).collect();
898    c.intensity = a.pts.iter().map(|p| p.v).collect();
899    c
900}
901
902/// Retention time in minutes of every sample of a trace sweep, and the intensity channel.
903fn from_trace(
904    ds: &mut dyn Dataset,
905    info: &FileInfo,
906    trace: u32,
907    channel: Option<u32>,
908    req: &ChromRequest,
909) -> Result<Chromatogram> {
910    let t = info
911        .traces
912        .iter()
913        .find(|t| t.index == trace)
914        .ok_or_else(|| {
915            if info.traces.is_empty() {
916                Error::unsupported(
917                    "chromatogram",
918                    format!("stored chromatograms of a {} file", info.format.name),
919                    "This file holds no traces; see `openreadout info`.",
920                )
921            } else {
922                Error::Usage(format!(
923                    "trace {trace} out of range (file has {} traces)",
924                    info.traces.len()
925                ))
926            }
927        })?;
928    if req.sweep >= t.sweep_count {
929        return Err(Error::Usage(format!(
930            "sweep {} out of range (trace {trace} has {} sweeps)",
931            req.sweep, t.sweep_count
932        )));
933    }
934    let time_ch = t
935        .channels
936        .iter()
937        .position(|c| c.name == "time" && t.sample_rate_hz == 0.0);
938    let ch = match channel {
939        Some(c) => {
940            if c as usize >= t.channels.len() {
941                return Err(Error::Usage(format!(
942                    "channel {c} out of range (trace {trace} has {} channels)",
943                    t.channels.len()
944                )));
945            }
946            c as usize
947        }
948        None => (0..t.channels.len())
949            .find(|&c| Some(c) != time_ch)
950            .ok_or_else(|| Error::Usage(format!("trace {trace} has no signal channel")))?,
951    };
952    let axis = time_axis(t);
953    if time_ch.is_none() && matches!(axis, Axis::None) {
954        return Err(Error::unsupported(
955            "chromatogram",
956            format!(
957                "trace {trace} ({}) as a chromatogram: it has no time axis",
958                t.name.as_deref().unwrap_or("unnamed")
959            ),
960            "Only chromatograms and time signals can be integrated; see `info` → traces[].extra.axis.",
961        ));
962    }
963    let n = openreadout_core::trace::sweep_samples(t, req.sweep);
964    let mut rt = Vec::with_capacity(n.min(1 << 24) as usize);
965    let mut y = Vec::with_capacity(n.min(1 << 24) as usize);
966    let time_scale = time_ch.map_or(1.0 / 60.0, |c| {
967        if t.channels[c].unit.as_deref() == Some("min") {
968            1.0
969        } else {
970            1.0 / 60.0
971        }
972    });
973    let mut pos = 0u64;
974    while pos < n {
975        let want = TRACE_CHUNK.min(n - pos);
976        let tr = ds.read_trace(trace, req.sweep, pos, want)?;
977        let col = tr
978            .channels
979            .get(ch)
980            .ok_or_else(|| Error::Other(format!("reader returned no channel {ch}")))?;
981        let got = col.len() as u64;
982        for (i, &v) in col.iter().enumerate() {
983            let k = pos + i as u64;
984            let tm = match (time_ch, &axis) {
985                (Some(c), _) => tr
986                    .channels
987                    .get(c)
988                    .and_then(|tc| tc.get(i))
989                    .map_or(f64::NAN, |s| s * time_scale),
990                (None, Axis::Minutes { first, step }) => first + step * k as f64,
991                (None, _) => f64::NAN,
992            };
993            if req.rt_range_min.is_none_or(|[a, b]| tm >= a && tm <= b) && tm.is_finite() {
994                rt.push(tm);
995                y.push(v);
996            }
997        }
998        pos += got;
999        if got < want || got == 0 {
1000            break;
1001        }
1002    }
1003    let name = t.name.clone().unwrap_or_else(|| format!("trace {trace}"));
1004    let chan = &t.channels[ch];
1005    let label = if t.channels.len() > 1 + usize::from(time_ch.is_some()) {
1006        format!("{name}: {}", chan.name)
1007    } else {
1008        name
1009    };
1010    let mut c = Chromatogram {
1011        label,
1012        kind: "trace".into(),
1013        source: format!("trace {trace}"),
1014        intensity_unit: chan.unit.clone(),
1015        rt_min: rt,
1016        intensity: y,
1017        ..Chromatogram::default()
1018    };
1019    if let Some(p) = t
1020        .extra
1021        .get("precursor_mz")
1022        .and_then(serde_json::Value::as_f64)
1023    {
1024        c.precursor_mz = Some(p);
1025    }
1026    if let Some(p) = t
1027        .extra
1028        .get("product_mz")
1029        .and_then(serde_json::Value::as_f64)
1030    {
1031        c.product_mz = Some(p);
1032    }
1033    Ok(c)
1034}
1035
1036enum Axis {
1037    Minutes { first: f64, step: f64 },
1038    None,
1039}
1040
1041fn time_axis(t: &TraceInfo) -> Axis {
1042    if let Some(a) = t.extra.get("axis") {
1043        let q = a.get("quantity").and_then(|v| v.as_str()).unwrap_or("");
1044        let unit = a.get("unit").and_then(|v| v.as_str()).unwrap_or("");
1045        let first = a.get("first").and_then(serde_json::Value::as_f64);
1046        let step = a.get("step").and_then(serde_json::Value::as_f64);
1047        if matches!(q, "retention_time" | "time")
1048            && let (Some(first), Some(step)) = (first, step)
1049        {
1050            let k = match unit {
1051                "min" => 1.0,
1052                "s" => 1.0 / 60.0,
1053                "ms" => 1.0 / 60_000.0,
1054                "h" => 60.0,
1055                _ => return Axis::None,
1056            };
1057            return Axis::Minutes {
1058                first: first * k,
1059                step: step * k,
1060            };
1061        }
1062        // a retention-volume axis (ÄKTA/UNICORN) rides on a regular time base: fall through
1063        if !q.is_empty() && !matches!(q, "retention_time" | "time" | "retention_volume") {
1064            return Axis::None;
1065        }
1066    }
1067    if t.sample_rate_hz > 0.0 && t.sample_rate_hz.is_finite() {
1068        return Axis::Minutes {
1069            first: t.start_s.unwrap_or(0.0) / 60.0,
1070            step: 1.0 / t.sample_rate_hz / 60.0,
1071        };
1072    }
1073    Axis::None
1074}
1075
1076/// A stored TIC/BPC trace, for files without usable spectra.
1077fn stored_total(
1078    ds: &mut dyn Dataset,
1079    info: &FileInfo,
1080    t: &Target,
1081    req: &ChromRequest,
1082) -> Result<Chromatogram> {
1083    let (name, kind) = match t {
1084        Target::Bpc => ("BPC", "bpc"),
1085        _ => ("TIC", "tic"),
1086    };
1087    let found = info.traces.iter().find(|tr| {
1088        tr.name.as_deref().is_some_and(|n| {
1089            let u = n.to_ascii_uppercase();
1090            u == name || u.starts_with(&format!("{name} "))
1091        }) || tr
1092            .extra
1093            .get("chromatogram_type")
1094            .and_then(|v| v.as_str())
1095            .is_some_and(|v| {
1096                v.starts_with(if kind == "tic" {
1097                    "total ion current"
1098                } else {
1099                    "base peak"
1100                })
1101            })
1102    });
1103    if let Some(tr) = found {
1104        let mut c = from_trace(ds, info, tr.index, None, req)?;
1105        c.kind = kind.into();
1106        c.label = name.into();
1107        c.notes.push(format!(
1108            "read from the file's stored {name} (trace {}); no usable spectra",
1109            tr.index
1110        ));
1111        return Ok(c);
1112    }
1113    // an MRM table with a stored TIC column
1114    for tab in &info.tables {
1115        if kind == "tic"
1116            && let (Some(tcol), Some(vcol)) = (
1117                tab.columns.iter().position(|c| c.name == "rt_min"),
1118                tab.columns.iter().position(|c| c.name == "tic_stored"),
1119            )
1120        {
1121            let mut c = from_table(ds, tab.index, tab.row_count, tcol, vcol, req)?;
1122            c.kind = "tic".into();
1123            c.label = "TIC".into();
1124            c.source = format!("table {}", tab.index);
1125            return Ok(c);
1126        }
1127    }
1128    Err(Error::unsupported(
1129        "chromatogram",
1130        format!("a {name} of a {} file", info.format.name),
1131        "This file has no spectra and no stored chromatogram of that kind; see `openreadout info`.",
1132    ))
1133}
1134
1135fn from_table(
1136    ds: &mut dyn Dataset,
1137    table: u32,
1138    rows: u64,
1139    tcol: usize,
1140    vcol: usize,
1141    req: &ChromRequest,
1142) -> Result<Chromatogram> {
1143    let tab = ds.read_table(table, 0, rows)?;
1144    let (Some(tc), Some(vc)) = (tab.columns.get(tcol), tab.columns.get(vcol)) else {
1145        return Err(Error::Other(format!("table {table}: missing columns")));
1146    };
1147    let mut c = Chromatogram {
1148        source: format!("table {table}"),
1149        intensity_unit: Some("counts".into()),
1150        ..Chromatogram::default()
1151    };
1152    for (&t, &v) in tc.iter().zip(vc) {
1153        if t.is_finite() && req.rt_range_min.is_none_or(|[a, b]| t >= a && t <= b) {
1154            c.rt_min.push(t);
1155            c.intensity.push(v);
1156        }
1157    }
1158    Ok(c)
1159}
1160
1161/// Parse an MRM column name `Q1 > Q3`.
1162fn transition_of(name: &str) -> Option<(f64, f64)> {
1163    let (a, b) = name.split_once('>')?;
1164    Some((a.trim().parse().ok()?, b.trim().parse().ok()?))
1165}
1166
1167/// An SRM transition from stored chromatograms (traces carrying `precursor_mz`/`product_mz`) or
1168/// MRM table columns named `Q1 > Q3`.
1169fn stored_transition(
1170    ds: &mut dyn Dataset,
1171    info: &FileInfo,
1172    q1: f64,
1173    q3: f64,
1174    req: &ChromRequest,
1175) -> Result<Chromatogram> {
1176    let tol = req.transition_tolerance_da;
1177    let dist = |a: f64, b: f64| (a - q1).abs().max((b - q3).abs());
1178    let mut traces: Vec<(&TraceInfo, f64, f64)> = info
1179        .traces
1180        .iter()
1181        .filter_map(|t| {
1182            let p = t.extra.get("precursor_mz")?.as_f64()?;
1183            let d = t.extra.get("product_mz")?.as_f64()?;
1184            (dist(p, d) <= tol).then_some((t, p, d))
1185        })
1186        .collect();
1187    if !traces.is_empty() {
1188        let best = traces
1189            .iter()
1190            .map(|x| dist(x.1, x.2))
1191            .fold(f64::INFINITY, f64::min);
1192        traces.retain(|x| (dist(x.1, x.2) - best).abs() < 1e-9);
1193        let (mut rt, mut y) = (Vec::new(), Vec::new());
1194        let mut sources = Vec::new();
1195        let mut unit = None;
1196        for (t, _, _) in &traces {
1197            let c = from_trace(ds, info, t.index, None, req)?;
1198            rt.extend(c.rt_min);
1199            y.extend(c.intensity);
1200            unit = c.intensity_unit;
1201            sources.push(t.index.to_string());
1202        }
1203        let mut pts: Vec<(f64, f64)> = rt.into_iter().zip(y).collect();
1204        pts.sort_by(|a, b| a.0.total_cmp(&b.0));
1205        let (p, d) = (traces[0].1, traces[0].2);
1206        let mut c = Chromatogram {
1207            label: format!("SRM {} > {}", num(q1), num(q3)),
1208            kind: "srm".into(),
1209            source: format!("trace {}", sources.join("+")),
1210            precursor_mz: Some(p),
1211            product_mz: Some(d),
1212            intensity_unit: unit.or(Some("counts".into())),
1213            ..Chromatogram::default()
1214        };
1215        if traces.len() > 1 {
1216            c.notes.push(format!(
1217                "{} stored chromatograms of this transition (scheduled windows) merged",
1218                traces.len()
1219            ));
1220        }
1221        (c.rt_min, c.intensity) = pts.into_iter().unzip();
1222        return Ok(c);
1223    }
1224    let mut best: Option<(u32, u64, usize, usize, f64, f64)> = None;
1225    for tab in &info.tables {
1226        let Some(tcol) = tab.columns.iter().position(|c| c.name == "rt_min") else {
1227            continue;
1228        };
1229        for (k, col) in tab.columns.iter().enumerate() {
1230            if let Some((a, b)) = transition_of(&col.name)
1231                && dist(a, b) <= tol
1232                && best.is_none_or(|x| dist(a, b) < dist(x.4, x.5))
1233            {
1234                best = Some((tab.index, tab.row_count, tcol, k, a, b));
1235            }
1236        }
1237    }
1238    if let Some((table, rows, tcol, vcol, a, b)) = best {
1239        let mut c = from_table(ds, table, rows, tcol, vcol, req)?;
1240        c.label = format!("SRM {} > {}", num(q1), num(q3));
1241        c.kind = "srm".into();
1242        c.precursor_mz = Some(a);
1243        c.product_mz = Some(b);
1244        return Ok(c);
1245    }
1246    let mut hint = String::from(
1247        "No MS/MS spectra, stored SRM chromatogram or MRM table column matches this transition",
1248    );
1249    let known: Vec<String> = info
1250        .traces
1251        .iter()
1252        .filter_map(|t| {
1253            Some(format!(
1254                "{}>{}",
1255                num(t.extra.get("precursor_mz")?.as_f64()?),
1256                num(t.extra.get("product_mz")?.as_f64()?)
1257            ))
1258        })
1259        .chain(
1260            info.tables
1261                .iter()
1262                .flat_map(|t| t.columns.iter())
1263                .filter_map(|c| {
1264                    transition_of(&c.name).map(|(a, b)| format!("{}>{}", num(a), num(b)))
1265                }),
1266        )
1267        .take(12)
1268        .collect();
1269    if !known.is_empty() {
1270        hint.push_str(&format!(" (the file has e.g. {})", known.join(", ")));
1271    }
1272    hint.push_str("; widen --transition-tol or check `openreadout info`.");
1273    Err(Error::Unsupported {
1274        format: "chromatogram",
1275        feature: format!("SRM transition {} > {}", num(q1), num(q3)),
1276        hint: Some(hint),
1277    })
1278}
1279
1280#[cfg(test)]
1281#[allow(clippy::field_reassign_with_default)]
1282mod tests {
1283    use super::*;
1284
1285    fn sp(i: u64, rt_s: f64, level: u32, mz: Vec<f64>, it: Vec<f32>) -> Spectrum {
1286        Spectrum {
1287            index: i,
1288            scan_number: i + 1,
1289            ms_level: level,
1290            rt_s: Some(rt_s),
1291            polarity: "positive".into(),
1292            centroided: true,
1293            mz,
1294            intensity: it,
1295            ..Spectrum::default()
1296        }
1297    }
1298
1299    #[test]
1300    fn xic_window_and_aggregate() {
1301        let s = sp(
1302            0,
1303            60.0,
1304            1,
1305            vec![100.0, 100.0005, 100.002, 200.0],
1306            vec![1.0, 2.0, 4.0, 8.0],
1307        );
1308        let mut req = ChromRequest::default();
1309        req.targets = vec![
1310            Target::Xic { mz: 100.0 },
1311            Target::Tic,
1312            Target::Bpc,
1313            Target::Xic { mz: 150.0 },
1314        ];
1315        let t: Vec<(usize, Target)> = req.targets.iter().cloned().enumerate().collect();
1316        let mut accs = vec![Acc::default(); 4];
1317        visit(&s, &t, &req, &mut accs);
1318        assert_eq!(accs[0].pts[0].v, 3.0); // 10 ppm of 100 = ±0.001
1319        assert_eq!(accs[1].pts[0].v, 15.0);
1320        assert_eq!(accs[2].pts[0].v, 8.0);
1321        assert_eq!(accs[3].pts[0].v, 0.0);
1322        assert!((accs[0].pts[0].rt - 1.0).abs() < 1e-12);
1323        req.aggregate = Aggregate::Max;
1324        req.tolerance = Tolerance::Da(0.01);
1325        let mut accs = vec![Acc::default(); 4];
1326        visit(&s, &t, &req, &mut accs);
1327        assert_eq!(accs[0].pts[0].v, 4.0);
1328        // MS2 scans are not in an MS1 XIC; polarity filter
1329        let s2 = sp(1, 61.0, 2, vec![100.0], vec![5.0]);
1330        let mut accs = vec![Acc::default(); 4];
1331        visit(&s2, &t, &req, &mut accs);
1332        assert!(accs.iter().all(|a| a.pts.is_empty()));
1333        req.polarity = Some(Polarity::Negative);
1334        let mut accs = vec![Acc::default(); 4];
1335        visit(&s, &t, &req, &mut accs);
1336        assert!(accs.iter().all(|a| a.pts.is_empty()));
1337    }
1338
1339    /// A scan whose file states no retention time is left out of every chromatogram (never
1340    /// placed at 0), and the chromatogram says how many were.
1341    #[test]
1342    fn scans_without_retention_time_are_left_out() {
1343        let mut req = ChromRequest::default();
1344        req.targets = vec![Target::Tic, Target::Xic { mz: 100.0 }];
1345        req.rt_range_min = Some([0.0, 10.0]);
1346        let t: Vec<(usize, Target)> = req.targets.iter().cloned().enumerate().collect();
1347        let mut accs = vec![Acc::default(); 2];
1348        visit(&sp(0, 60.0, 1, vec![100.0], vec![2.0]), &t, &req, &mut accs);
1349        let mut none = sp(1, 0.0, 1, vec![100.0], vec![5.0]);
1350        none.rt_s = None;
1351        visit(&none, &t, &req, &mut accs);
1352        // an MS2 scan without a time is not a matching scan of an MS1 chromatogram
1353        let mut ms2 = sp(2, 0.0, 2, vec![100.0], vec![7.0]);
1354        ms2.rt_s = None;
1355        visit(&ms2, &t, &req, &mut accs);
1356        for a in &accs {
1357            assert_eq!(a.pts.len(), 1);
1358            assert_eq!(a.no_rt, 1);
1359        }
1360        assert_eq!(
1361            accs[0].computed, 1,
1362            "the TIC counts only the scan it placed"
1363        );
1364        let c = finish_spectra(&t[0].1, accs.remove(0), &req);
1365        assert_eq!(c.rt_min, vec![1.0]);
1366        assert!(
1367            c.notes
1368                .iter()
1369                .any(|n| n.starts_with("1 matching scan is left out")),
1370            "{:?}",
1371            c.notes
1372        );
1373    }
1374
1375    #[test]
1376    fn srm_nearest_precursor_and_product() {
1377        let mut req = ChromRequest::default();
1378        req.targets = vec![Target::Srm {
1379            q1: 115.0,
1380            q3: 71.1,
1381        }];
1382        let t: Vec<(usize, Target)> = req.targets.iter().cloned().enumerate().collect();
1383        let mut accs = vec![Acc::default()];
1384        for (i, (p, prods)) in [
1385            (115.007, vec![(27.28, 1.0f32), (71.103, 10.0)]),
1386            (115.05, vec![(71.143, 20.0)]),
1387            (115.007, vec![(27.28, 2.0), (71.103, 30.0)]),
1388        ]
1389        .into_iter()
1390        .enumerate()
1391        {
1392            let mut s = sp(i as u64, i as f64 * 6.0, 2, Vec::new(), Vec::new());
1393            s.precursor_mz = Some(p);
1394            (s.mz, s.intensity) = prods.into_iter().unzip();
1395            visit(&s, &t, &req, &mut accs);
1396        }
1397        let c = finish_spectra(&t[0].1, accs.remove(0), &req);
1398        assert_eq!(c.intensity, vec![10.0, 30.0]);
1399        assert_eq!(c.precursor_mz, Some(115.007));
1400        assert_eq!(c.product_mz, Some(71.103));
1401        assert!(c.notes[0].contains("115.05"), "{:?}", c.notes);
1402    }
1403
1404    #[test]
1405    fn decimate_keeps_maxima() {
1406        let mut c = Chromatogram {
1407            rt_min: (0..100).map(f64::from).collect(),
1408            intensity: (0..100).map(|i| if i == 37 { 99.0 } else { 1.0 }).collect(),
1409            ..Chromatogram::default()
1410        };
1411        c.summarize();
1412        c.decimate(10);
1413        assert_eq!(c.rt_min.len(), 10);
1414        assert!(c.intensity.contains(&99.0));
1415        assert_eq!(c.points, 100);
1416        assert_eq!(c.apex_rt_min, Some(37.0));
1417    }
1418
1419    #[test]
1420    fn column_names_parse_as_transitions() {
1421        assert_eq!(transition_of("207.82 > 99.66"), Some((207.82, 99.66)));
1422        assert_eq!(transition_of("rt_min"), None);
1423    }
1424}