data-beans 0.6.12

Sparse genomics data backends, QC, algorithms, and simulation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
//! Cell-axis quality control (library-only): robust (MAD) outlier detection
//! plus the near-empty floor. Built on the streaming stat collectors in the
//! sibling `qc` module and consumed by senna / pinto — NOT by the data-beans
//! CLI — so it lives in its own module that the binary never compiles (keeps
//! `qc` fully bin-used and avoids dead-code in the bin target).

use crate::qc::{collect_column_stat_across_vec, collect_row_stat_across_vec};
use crate::sparse_io_stack::SparseIoStack;
use crate::sparse_io_vector::SparseIoVec;
use legume_numeric::matrix::common_io::write_lines;
use legume_numeric::matrix::traits::RunningStatOps;
use log::warn;
use regex::Regex;

///////////////////////////////////////////////////////////////////////////////////
// Cell-axis quality control: robust (MAD) outlier detection + near-empty floor. //
///////////////////////////////////////////////////////////////////////////////////
//
// This MAY DROP CELLS (columns): callers that enable QC end up with fewer
// cells in the working set and/or in the per-cell outputs than the input had,
// so downstream consumers must not assume a 1:1 positional mapping to the
// input barcodes — join by cell name. (Feature/row QC, off by default, may
// likewise drop genes/rows.)
//
// Shared by senna and pinto. Two-tier policy (see `compute_qc`):
//   * near-empty cells (`nnz < min_cell_nnz`) are kept in training but
//     dropped from the *output* (gem-style; see `senna gem --min-cell-nnz`);
//   * non-near-empty MAD outliers are dropped from *training* via
//     `SparseIoVec::mask_columns` (so they leave the outputs too).

/// Which side(s) of the robust band count as outliers.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Tail {
    /// Flag values far *below* the median (e.g. low counts / few genes).
    Lower,
    /// Flag values far *above* the median (e.g. high mito fraction).
    Upper,
    /// Flag both extremes.
    Both,
}

/// Configuration for cell QC. Plain struct (no clap) so non-clap crates
/// can construct it directly; the clap surface is [`QcArgs`].
#[derive(Clone, Debug)]
pub struct QcConfig {
    /// MAD multiplier for the robust band (larger = more permissive).
    pub n_mads: f32,
    /// Near-empty floor: cells with fewer than this many detected features
    /// (nnz across all rows) are masked at output, not dropped from
    /// training. 0 disables the floor.
    pub min_cell_nnz: usize,
    /// Hard floor on total counts per cell, in addition to MAD. 0 disables.
    pub min_counts_per_cell: f32,
    /// Regex over row (feature) names selecting mitochondrial genes; enables
    /// the per-cell mito-fraction metric. `None` disables it.
    pub mito_pattern: Option<String>,
    /// Optional hard max mitochondrial fraction (0..1).
    pub mito_max_frac: Option<f32>,
    /// Regex selecting ribosomal genes (enables ribo-fraction metric).
    pub ribo_pattern: Option<String>,
    /// Optional hard max ribosomal fraction (0..1).
    pub ribo_max_frac: Option<f32>,
    /// Per-cell metrics that drive MAD outlier flagging.
    pub mad_on_n_genes: bool,
    pub mad_on_counts: bool,
    pub mad_on_mito: bool,
    /// Feature-axis QC (off by default): drop genes expressed in fewer than
    /// this many cells. 0 disables.
    pub feature_min_cells: usize,
    /// Master switch for the MAD train-drop tier. When `false`, only the
    /// near-empty floor (output mask) is computed — used by inference
    /// (predict/impute) so query cells are never silently dropped.
    pub drop_outliers: bool,
    /// Automatic cell calling: pick the per-cell nnz cutoff at the trough
    /// between the ambient and the cell peak ([`crate::qc::suggest_nnz_cutoff`])
    /// and **train-drop** every cell below it. So called-out ambient is removed
    /// up front (via the caller's `mask_columns`), not just output-masked — it
    /// never shapes the model. The cutoff is authoritative; `min_cell_nnz` does
    /// NOT floor it (it only drives the separate near-empty output mask). No-op
    /// when there is no trough (unimodal) or when `drop_outliers` is false
    /// (inference). Unlike the loader's per-file empty-barcode gate, this runs
    /// on the pooled cell axis.
    pub auto_cell_cutoff: bool,
    /// Print the per-cell nnz histogram + the suggested/applied cutoff (the
    /// same ASCII summary as `data-beans squeeze --show-histogram`).
    pub qc_histogram: bool,
}

impl Default for QcConfig {
    fn default() -> Self {
        Self {
            n_mads: 5.0,
            min_cell_nnz: 2,
            min_counts_per_cell: 0.0,
            mito_pattern: None,
            mito_max_frac: None,
            ribo_pattern: None,
            ribo_max_frac: None,
            mad_on_n_genes: true,
            mad_on_counts: true,
            mad_on_mito: false,
            feature_min_cells: 0,
            drop_outliers: true,
            auto_cell_cutoff: false,
            qc_histogram: false,
        }
    }
}

/// Median of a non-empty slice via `O(n)` quickselect (reorders in place).
fn median_in_place(xs: &mut [f32]) -> f32 {
    let mid = xs.len() / 2;
    xs.select_nth_unstable_by(mid, |a, b| a.total_cmp(b));
    xs[mid]
}

/// Robust outlier keep-mask: `keep[i] = false` when `values[i]` falls
/// outside `median ± n_mads · MAD · 1.4826` on the requested `tail`.
/// Count-like metrics should pass `log1p = true` so the band is symmetric
/// on the multiplicative scale. Consolidates the median+MAD idiom used in
/// `cnv::hmm`.
/// `consider` (when `Some`) restricts the median/MAD band to the cells it
/// marks `true` — so e.g. near-empty cells don't contaminate the robust
/// center — while the keep decision is still returned for every cell.
pub fn robust_outlier_keep(
    values: &[f32],
    n_mads: f32,
    tail: Tail,
    log1p: bool,
    consider: Option<&[bool]>,
) -> Vec<bool> {
    let n = values.len();
    if n == 0 {
        return vec![];
    }
    let xform = |v: f32| if log1p { v.max(0.0).ln_1p() } else { v };

    // Band statistics computed over the considered subset only.
    let mut xs: Vec<f32> = values
        .iter()
        .enumerate()
        .filter(|(i, _)| consider.is_none_or(|c| c[*i]))
        .map(|(_, &v)| xform(v))
        .filter(|v| v.is_finite())
        .collect();
    if xs.is_empty() {
        return vec![true; n];
    }
    // O(n) median via quickselect (no full sort).
    let median = median_in_place(&mut xs);

    let mut dev: Vec<f32> = xs.iter().map(|&v| (v - median).abs()).collect();
    let mad = (median_in_place(&mut dev) * 1.4826).max(1e-8);

    let lo = median - n_mads * mad;
    let hi = median + n_mads * mad;
    values
        .iter()
        .map(|&raw| {
            let v = xform(raw);
            if !v.is_finite() {
                return false;
            }
            match tail {
                Tail::Lower => v >= lo,
                Tail::Upper => v <= hi,
                Tail::Both => v >= lo && v <= hi,
            }
        })
        .collect()
}

/// Resolve a regex over row (feature) names into matching row indices, in
/// the same compact-row order as `SparseIoVec::row_names()`. Feeds
/// `collect_column_stat_across_vec(.., Some(&rows), ..)` for subset sums.
pub fn resolve_rows_by_regex(row_names: &[Box<str>], pattern: &str) -> anyhow::Result<Vec<usize>> {
    let re = Regex::new(pattern)?;
    Ok(row_names
        .iter()
        .enumerate()
        .filter(|(_, name)| re.is_match(name))
        .map(|(i, _)| i)
        .collect())
}

/// Per-cell / per-feature QC outcome.
pub struct QcReport {
    /// Cells to retain in training (len = num_columns). `false` only for
    /// non-near-empty MAD outliers — drives `SparseIoVec::mask_columns`.
    pub train_keep: Vec<bool>,
    /// Near-empty cells (len = num_columns): kept in training, masked at
    /// output via [`QcReport::output_keep_idx`].
    pub near_empty: Vec<bool>,
    /// Features to retain (len = num_rows). All `true` unless
    /// `feature_min_cells > 0`.
    pub feature_keep: Vec<bool>,
    pub n_genes: Vec<f32>,
    pub total_counts: Vec<f32>,
    pub mito_frac: Option<Vec<f32>>,
    pub ribo_frac: Option<Vec<f32>>,
    pub n_cells_dropped: usize,
    pub n_features_dropped: usize,
}

impl QcReport {
    /// Indices, in *post-`mask_columns`* column order, of the cells to emit
    /// at output: surviving (training-kept) cells that are not near-empty.
    /// Apply with `Mat::select_rows` on per-cell output matrices and filter
    /// the barcode slice by the same indices.
    pub fn output_keep_idx(&self) -> Vec<usize> {
        let mut idx = Vec::new();
        let mut new_pos = 0usize;
        for c in 0..self.train_keep.len() {
            if !self.train_keep[c] {
                continue; // dropped from training entirely
            }
            if !self.near_empty[c] {
                idx.push(new_pos); // c survives at compact index new_pos
            }
            new_pos += 1;
        }
        idx
    }

    /// Emit-keep indices in the **original** (unmasked) column order: cells
    /// that are neither MAD-dropped nor near-empty. For paths that keep every
    /// cell in training and instead `select_rows` the per-cell outputs
    /// directly (e.g. bge's `UnifiedData`, which has no `mask_columns`).
    pub fn emit_idx_unmasked(&self) -> Vec<usize> {
        (0..self.train_keep.len())
            .filter(|&c| self.train_keep[c] && !self.near_empty[c])
            .collect()
    }
}

/// Compute the two-tier cell-QC report. `block_size` controls the
/// streaming stat passes (`None` = default chunking).
pub fn compute_qc(
    data: &SparseIoVec,
    cfg: &QcConfig,
    block_size: Option<usize>,
) -> anyhow::Result<QcReport> {
    compute_qc_exempting(data, cfg, block_size, None)
}

/// [`compute_qc`] with columns that are **not cells** exempted — a prior
/// run's carried pseudobulks. `exempt[c] = true` keeps column `c` out of all
/// band statistics and out of every verdict. See `qc_from_metrics`.
pub fn compute_qc_exempting(
    data: &SparseIoVec,
    cfg: &QcConfig,
    block_size: Option<usize>,
    exempt: Option<&[bool]>,
) -> anyhow::Result<QcReport> {
    let row_names = data.row_names()?;

    // Per-cell totals across all rows (nnz = n_genes, tot = total_counts).
    // Use the running-stat accessors directly to skip the unused mean/std.
    let col_stat = collect_column_stat_across_vec(data, None, block_size)?;
    let n_genes = col_stat.count_positives();
    let total_counts = col_stat.sum();

    // Optional mito / ribo fractions via row-name regex subsets.
    let frac_for = |pattern: &Option<String>| -> anyhow::Result<Option<Vec<f32>>> {
        let Some(pat) = pattern else {
            return Ok(None);
        };
        let rows = resolve_rows_by_regex(&row_names, pat)?;
        if rows.is_empty() {
            warn!(
                "QC: pattern `{}` matched no features — metric disabled",
                pat
            );
            return Ok(None);
        }
        let sub_tot = collect_column_stat_across_vec(data, Some(&rows), block_size)?.sum();
        let frac = sub_tot
            .iter()
            .zip(total_counts.iter())
            .map(|(&s, &t)| if t > 0.0 { s / t } else { 0.0 })
            .collect::<Vec<f32>>();
        Ok(Some(frac))
    };
    let mito_frac = frac_for(&cfg.mito_pattern)?;
    let ribo_frac = frac_for(&cfg.ribo_pattern)?;

    // Feature axis stat (only when feature QC is enabled).
    let feature_n_cells = if cfg.feature_min_cells > 0 {
        Some(collect_row_stat_across_vec(data, block_size)?.count_positives())
    } else {
        None
    };

    Ok(qc_from_metrics(
        QcMetrics {
            n_genes,
            total_counts,
            mito_frac,
            ribo_frac,
            feature_n_cells,
            n_rows: data.num_rows(),
        },
        cfg,
        exempt,
    ))
}

/// Modality-agnostic cell QC for a [`SparseIoStack`]: per-cell `n_genes` /
/// `total_counts` are **summed across all member modalities** (mirrors
/// `senna gem`'s "a cell rich in any one modality is kept"). Mito/ribo and
/// feature-axis QC are skipped on stacks (row names are per-modality).
pub fn compute_qc_stack(
    stack: &SparseIoStack,
    cfg: &QcConfig,
    block_size: Option<usize>,
) -> anyhow::Result<QcReport> {
    let n_cols = stack.num_columns()?;
    let mut n_genes = vec![0f32; n_cols];
    let mut total_counts = vec![0f32; n_cols];
    for member in stack.stack.iter() {
        let cs = collect_column_stat_across_vec(member, None, block_size)?;
        let ng = cs.count_positives();
        let ct = cs.sum();
        for c in 0..n_cols {
            n_genes[c] += ng[c];
            total_counts[c] += ct[c];
        }
    }
    if cfg.mito_pattern.is_some() || cfg.ribo_pattern.is_some() || cfg.feature_min_cells > 0 {
        warn!("QC: mito/ribo/feature thresholds are ignored for stacked (multi-modal) data");
    }
    Ok(qc_from_metrics(
        QcMetrics {
            n_genes,
            total_counts,
            mito_frac: None,
            ribo_frac: None,
            feature_n_cells: None,
            n_rows: 0, // feature axis not masked on stacks
        },
        cfg,
        None,
    ))
}

/// Pre-computed per-cell / per-feature metrics fed to [`qc_from_metrics`].
/// Lets the single-modality and stacked QC paths share one decision rule.
struct QcMetrics {
    n_genes: Vec<f32>,
    total_counts: Vec<f32>,
    mito_frac: Option<Vec<f32>>,
    ribo_frac: Option<Vec<f32>>,
    /// `None` when feature-axis QC is disabled.
    feature_n_cells: Option<Vec<f32>>,
    n_rows: usize,
}

/// Apply the two-tier QC decision (near-empty floor + MAD outliers) to
/// pre-computed metrics. Shared by [`compute_qc`] and [`compute_qc_stack`].
///
/// `exempt`, when given, marks columns that are **not cells** — a prior run's
/// carried pseudobulks. They are excluded from every band statistic (a few
/// hundred smooth averages otherwise drag the MAD center and can guillotine
/// the real cells wholesale — measured: 400 of 400 real cells dropped as
/// "outliers" of the carried columns' band) and they receive no verdict:
/// never near-empty, never dropped. Their exclusion from OUTPUT is a separate
/// concern handled by the caller.
fn qc_from_metrics(m: QcMetrics, cfg: &QcConfig, exempt: Option<&[bool]>) -> QcReport {
    let QcMetrics {
        n_genes,
        total_counts,
        mito_frac,
        ribo_frac,
        feature_n_cells,
        n_rows,
    } = m;
    let n_cols = n_genes.len();

    let is_exempt = |c: usize| exempt.is_some_and(|e| e[c]);

    // Tier 1: near-empty floor. Exempt columns are never near-empty.
    let near_empty: Vec<bool> = n_genes
        .iter()
        .enumerate()
        .map(|(c, &g)| !is_exempt(c) && (g as usize) < cfg.min_cell_nnz)
        .collect();

    // Tier 2: MAD outliers among non-near-empty cells. The band statistics
    // are fit over the non-near-empty, non-exempt cells only, so neither
    // near-empty cells nor carried pseudobulks contaminate the robust center.
    let not_near_empty: Vec<bool> = near_empty
        .iter()
        .enumerate()
        .map(|(c, &e)| !e && !is_exempt(c))
        .collect();
    let consider = Some(not_near_empty.as_slice());
    let mut outlier = vec![false; n_cols];
    if cfg.drop_outliers {
        let mut bands: Vec<Vec<bool>> = Vec::new();
        if cfg.mad_on_n_genes {
            bands.push(robust_outlier_keep(
                &n_genes,
                cfg.n_mads,
                Tail::Lower,
                true,
                consider,
            ));
        }
        if cfg.mad_on_counts {
            bands.push(robust_outlier_keep(
                &total_counts,
                cfg.n_mads,
                Tail::Lower,
                true,
                consider,
            ));
        }
        if cfg.mad_on_mito {
            if let Some(mf) = mito_frac.as_ref() {
                bands.push(robust_outlier_keep(
                    mf,
                    cfg.n_mads,
                    Tail::Upper,
                    false,
                    consider,
                ));
            }
        }
        for c in 0..n_cols {
            if near_empty[c] || is_exempt(c) {
                continue; // floor takes precedence; exempt columns get no verdict
            }
            let mut fail = total_counts[c] < cfg.min_counts_per_cell;
            if let (Some(mf), Some(cap)) = (mito_frac.as_ref(), cfg.mito_max_frac) {
                fail |= mf[c] > cap;
            }
            if let (Some(rf), Some(cap)) = (ribo_frac.as_ref(), cfg.ribo_max_frac) {
                fail |= rf[c] > cap;
            }
            for band in bands.iter() {
                if !band[c] {
                    fail = true;
                    break;
                }
            }
            outlier[c] = fail;
        }

        // Automatic cell calling: the trough of n_genes picks the ambient↔real
        // boundary and train-drops every cell below it, so the caller's
        // `mask_columns(train_keep)` removes ambient up front. The cutoff
        // is authoritative — there is NO redundant `min_cell_nnz` floor on it.
        // When the data is unimodal the trough search returns `None`, so no auto
        // cutoff is applied and the near-empty floor / MAD tiers stand alone.
        // The histogram + cutoff are optionally printed.
        if cfg.auto_cell_cutoff || cfg.qc_histogram {
            let suggested = crate::qc::suggest_nnz_cutoff(&n_genes);
            // Display cutoff: the trough suggestion if found, else the
            // near-empty floor (what the non-auto tier would use).
            let shown = suggested.unwrap_or(cfg.min_cell_nnz);
            crate::qc::print_nnz_summary("Cell", "nnz", &n_genes, shown, suggested);
            if cfg.auto_cell_cutoff {
                if let Some(cut) = suggested {
                    for (c, &g) in n_genes.iter().enumerate() {
                        if (g as usize) < cut {
                            outlier[c] = true;
                        }
                    }
                }
            }
        }
    }

    let mut train_keep: Vec<bool> = outlier.iter().map(|&o| !o).collect();
    let mut n_cells_dropped = outlier.iter().filter(|&&o| o).count();

    // Guardrail: never produce an empty matrix.
    if n_cols > 0 && n_cells_dropped >= n_cols {
        warn!(
            "QC would drop all {} cells — keeping all (check thresholds)",
            n_cols
        );
        train_keep = vec![true; n_cols];
        n_cells_dropped = 0;
    }

    // Feature axis (only when feature_n_cells was computed).
    let (feature_keep, n_features_dropped) = match feature_n_cells {
        Some(n_cells_expr) => {
            let keep: Vec<bool> = n_cells_expr
                .iter()
                .map(|&c| (c as usize) >= cfg.feature_min_cells)
                .collect();
            let dropped = keep.iter().filter(|&&k| !k).count();
            if !keep.is_empty() && dropped >= keep.len() {
                warn!("QC would drop all features — keeping all");
                (vec![true; keep.len()], 0)
            } else {
                (keep, dropped)
            }
        }
        None => (vec![true; n_rows], 0),
    };

    QcReport {
        train_keep,
        near_empty,
        feature_keep,
        n_genes,
        total_counts,
        mito_frac,
        ribo_frac,
        n_cells_dropped,
        n_features_dropped,
    }
}

/// Filter a per-cell `Vec<T>` in lockstep with a cell keep-mask. Used to
/// keep batch labels / coordinates aligned after `mask_columns`.
pub fn filter_by_keep<T: Clone>(items: &[T], keep: &[bool]) -> Vec<T> {
    items
        .iter()
        .zip(keep.iter())
        .filter(|&(_, &k)| k)
        .map(|(x, _)| x.clone())
        .collect()
}

/// Write a per-cell QC table (TSV): name, n_genes, total_counts,
/// [mito_frac], [ribo_frac], near_empty (0/1), train_keep (0/1).
pub fn write_qc_report(
    path: &str,
    cell_names: &[Box<str>],
    report: &QcReport,
) -> anyhow::Result<()> {
    use std::fmt::Write as _;
    let n = report.train_keep.len();
    anyhow::ensure!(
        cell_names.len() == n,
        "write_qc_report: {} names != {} cells",
        cell_names.len(),
        n
    );

    let mut header = String::from("#cell\tn_genes\ttotal_counts");
    if report.mito_frac.is_some() {
        header.push_str("\tmito_frac");
    }
    if report.ribo_frac.is_some() {
        header.push_str("\tribo_frac");
    }
    header.push_str("\tnear_empty\ttrain_keep");

    let mut lines: Vec<Box<str>> = Vec::with_capacity(n + 1);
    lines.push(header.into_boxed_str());
    for c in 0..n {
        let mut line = String::new();
        let _ = write!(
            line,
            "{}\t{}\t{}",
            cell_names[c], report.n_genes[c], report.total_counts[c]
        );
        if let Some(mf) = report.mito_frac.as_ref() {
            let _ = write!(line, "\t{}", mf[c]);
        }
        if let Some(rf) = report.ribo_frac.as_ref() {
            let _ = write!(line, "\t{}", rf[c]);
        }
        let _ = write!(
            line,
            "\t{}\t{}",
            report.near_empty[c] as u8, report.train_keep[c] as u8
        );
        lines.push(line.into_boxed_str());
    }
    write_lines(&lines, path)
}

/// Clap surface for cell QC, shared by senna and pinto subcommands.
///
/// **Cell QC is ON by default and CAN DROP CELLS (columns).** Low-quality
/// cells are removed: near-empty cells are omitted from the per-cell outputs,
/// and robust (MAD) outlier cells are excluded from training entirely. As a
/// result, the per-cell output files (`*.latent.parquet`, `*.cell_proj.parquet`,
/// `*.cell_to_pb.parquet`, `*.cell_embedding.parquet`, pinto propensity, etc.)
/// **may contain fewer rows (cells) than the input** — do not assume a 1:1,
/// positional correspondence with the input barcodes; always join by the cell
/// name/barcode column. Pass `--no-qc` to disable and keep every cell, or
/// `--qc-report <path>` to dump the per-cell keep/drop flags. (Feature/row QC
/// is OFF unless `--qc-feature-min-cells` is set.)
#[derive(clap::Args, Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(default = "legume_numeric::matrix::clap_defaults::clap_defaults")]
pub struct QcArgs {
    /// Disable cell QC entirely (keep every input cell).
    #[arg(
        long = "no-qc",
        default_value_t = false,
        long_help = "Disable cell quality control entirely and keep every input cell.\n\
                     \n\
                     By DEFAULT (without this flag) cell QC is:\n  \
                     - a near-empty nnz floor (--qc-min-cell-nnz), and\n  \
                     - MAD-outlier drops on detected features and total counts\n    \
                     (--qc-mad-on-genes / --qc-mad-on-counts, band --qc-mads).\n\
                     \n\
                     Empty barcodes are dropped earlier, per input file, by the loader.\n\
                     A pooled trough cut on top of that stays OFF unless --qc-auto-cutoff.\n\
                     \n\
                     Outputs may therefore have FEWER ROWS than the input.\n\
                     Join by the cell/barcode name column, never by position.\n\
                     \n\
                     Use --qc-report to see exactly what was dropped.\n\
                     For the older near-empty-floor-only gate,\n\
                     pass `--qc-mad-on-genes=false --qc-mad-on-counts=false`."
    )]
    pub no_qc: bool,

    /// MAD multiplier for the robust outlier band; smaller = drops more cells.
    #[arg(long = "qc-mads", default_value_t = 5.0)]
    pub qc_mads: f32,

    #[arg(
        long = "qc-min-cell-nnz",
        default_value_t = 2,
        help = "Near-empty floor on a cell's detected-feature count",
        long_help = "Near-empty floor on the detected-feature count.\n\
                     Cells below it are dropped from the per-cell outputs.\n\
                     They are still kept in training."
    )]
    pub qc_min_cell_nnz: usize,

    #[arg(
        long = "qc-min-counts",
        hide = true,
        default_value_t = 0.0,
        help = "Hard floor on total counts per cell",
        long_help = "Hard floor on total counts per cell.\n\
                     Cells below it are dropped from training. 0 disables the floor."
    )]
    pub qc_min_counts: f32,

    #[arg(
        long = "qc-mito-pattern",
        hide = true,
        help = "Regex over feature names selecting mitochondrial genes",
        long_help = "Regex over feature names selecting mitochondrial genes.\n\
                     It enables the mito-fraction outlier metric. An example is `(?i)^MT-`."
    )]
    pub qc_mito_pattern: Option<String>,

    /// Hard max mitochondrial fraction (0..1).
    #[arg(long = "qc-mito-max-frac", hide = true)]
    pub qc_mito_max_frac: Option<f32>,

    /// Regex over feature names selecting ribosomal genes.
    #[arg(long = "qc-ribo-pattern", hide = true)]
    pub qc_ribo_pattern: Option<String>,

    /// Hard max ribosomal fraction (0..1).
    #[arg(long = "qc-ribo-max-frac", hide = true)]
    pub qc_ribo_max_frac: Option<f32>,

    #[arg(
        long = "qc-feature-min-cells",
        hide = true,
        default_value_t = 0,
        help = "Feature/row QC: drop genes expressed in too few cells",
        long_help = "Feature/row QC; off by default.\n\
                     It DROPS gene rows expressed in fewer than this many cells.\n\
                     \n\
                     Not every consumer applies it.\n\
                     `bge` does not, since QC there is cell-only.\n\
                     `pinto` does not either: it reads only the cell verdict,\n\
                     so setting this costs a stats pass and changes nothing."
    )]
    pub qc_feature_min_cells: usize,

    #[arg(
        long = "qc-report",
        help = "Write a per-cell QC table (.tsv)",
        long_help = "Write a per-cell QC table, as .tsv.\n\
                     It carries the metrics plus near_empty and train_keep flags.\n\
                     You can then see exactly which cells were dropped."
    )]
    pub qc_report: Option<Box<str>>,

    #[arg(
        long = "qc-histogram",
        hide = true,
        default_value_t = false,
        help = "Print the per-cell nnz histogram + the (diagnostic) suggested trough cutoff",
        long_help = "Print an ASCII histogram of the per-cell nnz distribution.\n\
                     The suggested trough cutoff is marked.\n\
                     It is the same summary as `data-beans squeeze --show-histogram`.\n\
                     \n\
                     This is purely diagnostic. The cutoff is shown, not applied.\n\
                     The upfront gate is the conservative --qc-min-cell-nnz floor.\n\
                     Use the histogram to pick --qc-min-cell-nnz by hand."
    )]
    pub qc_histogram: bool,

    #[arg(
        long = "qc-mad-on-genes",
        hide = true,
        default_value_t = true,
        help = "MAD-outlier drop on the per-cell detected-feature count",
        long_help = "Drop cells whose detected-feature count falls outside `median +/- --qc-mads * MAD * 1.4826`.\n\
                     \n\
                     ON by default. This and --qc-mad-on-counts were previously hardcoded OFF,\n\
                     with no way to enable them. That also made --qc-mads inert.\n\
                     Only a set --qc-mito-pattern revived it.\n\
                     \n\
                     Pass `--qc-mad-on-genes=false` for the old behaviour.\n\
                     That is the conservative near-empty nnz gate alone."
    )]
    pub qc_mad_on_genes: bool,

    #[arg(
        long = "qc-mad-on-counts",
        hide = true,
        default_value_t = true,
        help = "MAD-outlier drop on per-cell total counts",
        long_help = "Drop cells whose total count falls outside `median +/- --qc-mads * MAD * 1.4826`.\n\
                     ON by default; see --qc-mad-on-genes."
    )]
    pub qc_mad_on_counts: bool,

    #[arg(
        long = "qc-auto-cutoff",
        hide = true,
        default_value_t = false,
        help = "Apply the nnz trough cell-calling cutoff on the pooled cell axis",
        long_help = "Apply the ambient/cell trough cutoff as a hard cell call.\n\
                     It runs on the pooled per-cell nnz distribution, after the loader's per-file gate.\n\
                     Without this flag it is only reported, via --qc-histogram.\n\
                     \n\
                     OFF by default. The intended gate is the conservative near-empty floor,\n\
                     plus the model's own empty-call.\n\
                     This flag was referenced in the docs before it existed."
    )]
    pub qc_auto_cutoff: bool,
}

impl QcArgs {
    /// QC config, or `None` under `--no-qc`.
    pub fn to_config(&self) -> Option<QcConfig> {
        (!self.no_qc).then(|| QcConfig {
            n_mads: self.qc_mads,
            min_cell_nnz: self.qc_min_cell_nnz,
            min_counts_per_cell: self.qc_min_counts,
            mito_pattern: self.qc_mito_pattern.clone(),
            mito_max_frac: self.qc_mito_max_frac,
            ribo_pattern: self.qc_ribo_pattern.clone(),
            ribo_max_frac: self.qc_ribo_max_frac,
            // MAD gates are ON by default and settable. They were hardcoded
            // `false` here with no CLI path, which also made `--qc-mads` a dead
            // flag unless `--qc-mito-pattern` was set (nothing else reads
            // `n_mads`). Mito stays implicit: it can only run when a pattern
            // selects the rows to measure.
            mad_on_n_genes: self.qc_mad_on_genes,
            mad_on_counts: self.qc_mad_on_counts,
            mad_on_mito: self.qc_mito_pattern.is_some(),
            feature_min_cells: self.qc_feature_min_cells,
            drop_outliers: true,
            // Off by default: the near-empty floor plus the model's own
            // empty-call (bge's embedding-norm two-step; topic/masked-topic's
            // flag-don't-drop) is the intended gate. `--qc-auto-cutoff` opts in.
            auto_cell_cutoff: self.qc_auto_cutoff,
            qc_histogram: self.qc_histogram,
        })
    }
}

#[cfg(test)]
mod qc_tests {
    use super::*;

    #[test]
    fn robust_lower_flags_low_outlier() {
        let v = vec![100.0, 110.0, 90.0, 105.0, 95.0, 1.0];
        let keep = robust_outlier_keep(&v, 3.0, Tail::Lower, true, None);
        assert!(!keep[5], "the value 1.0 should be a lower outlier");
        assert!(keep[..5].iter().all(|&k| k), "the bulk should be kept");
        // upper tail must not flag a low value
        let keep_up = robust_outlier_keep(&v, 3.0, Tail::Upper, true, None);
        assert!(keep_up[5], "lower outlier kept under Tail::Upper");
    }

    #[test]
    fn robust_uniform_keeps_all() {
        let v = vec![7.0; 20];
        let keep = robust_outlier_keep(&v, 5.0, Tail::Both, true, None);
        assert!(keep.iter().all(|&k| k));
    }

    #[test]
    fn auto_cutoff_train_drops_ambient() {
        // Bimodal nnz: 30 ambient (~2) + 30 real (~100). With auto cell calling on
        // (MAD tiers off so the auto floor is the only decider), the trough
        // cutoff lands between the modes and the ambient cells are train-dropped.
        let n_genes: Vec<f32> = [vec![2.0; 30], vec![100.0; 30]].concat();
        let cfg = QcConfig {
            auto_cell_cutoff: true,
            qc_histogram: false,
            drop_outliers: true,
            mad_on_n_genes: false,
            mad_on_counts: false,
            mad_on_mito: false,
            min_cell_nnz: 0,
            ..QcConfig::default()
        };
        let report = qc_from_metrics(
            QcMetrics {
                n_genes: n_genes.clone(),
                total_counts: n_genes,
                mito_frac: None,
                ribo_frac: None,
                feature_n_cells: None,
                n_rows: 0,
            },
            &cfg,
            None,
        );
        assert_eq!(
            report.train_keep,
            [vec![false; 30], vec![true; 30]].concat()
        );
        assert_eq!(report.n_cells_dropped, 30);
    }

    #[test]
    fn robust_consider_excludes_contaminants_from_band() {
        // A real cluster around 100 plus a large block of near-empty 0s that
        // would drag a naive median/MAD down. With `consider` masking the
        // zeros out of the band, a genuine low real cell (40) is still flagged.
        let mut v = vec![0.0; 12];
        v.extend([100.0, 102.0, 98.0, 101.0, 99.0, 40.0]);
        let mut consider = vec![false; 12];
        consider.extend([true; 6]);
        let keep = robust_outlier_keep(&v, 2.0, Tail::Lower, true, Some(&consider));
        assert!(
            !keep[17],
            "40 is a lower outlier of the real cluster (~100)"
        );
        // Without `consider`, the zeros dominate the median and 40 survives.
        let keep_naive = robust_outlier_keep(&v, 2.0, Tail::Lower, true, None);
        assert!(
            keep_naive[17],
            "naive band (contaminated by zeros) keeps 40"
        );
    }

    #[test]
    fn output_keep_idx_skips_dropped_and_near_empty() {
        // cells: 0 keep, 1 near-empty (kept in training), 2 MAD-drop, 3 keep
        let report = QcReport {
            train_keep: vec![true, true, false, true],
            near_empty: vec![false, true, false, false],
            feature_keep: vec![],
            n_genes: vec![],
            total_counts: vec![],
            mito_frac: None,
            ribo_frac: None,
            n_cells_dropped: 1,
            n_features_dropped: 0,
        };
        // post-mask order: cell0->0, cell1->1, cell3->2 (cell2 dropped)
        // emit non-near-empty survivors: 0 and 2
        assert_eq!(report.output_keep_idx(), vec![0, 2]);
    }
}

#[cfg(test)]
#[path = "qc_lib_tests.rs"]
mod tests;