pmdshodh 0.4.1

Per-read post-mortem damage (PMD) scoring and filtering of ancient DNA alignments, compatible with PMDtools
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
//! What a run prints to stderr: the end-of-run summary block and, in a terminal, a live progress
//! line. Nothing here touches stdout, the JSON summary or the exit status.

use std::io::{IsTerminal, Read, Write};
use std::sync::OnceLock;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};

/// Bytes read so far from the input file, for the progress line's percentage.
static BYTES_READ: AtomicU64 = AtomicU64::new(0);
/// Size of the input file, if it is a regular file.
static INPUT_SIZE: OnceLock<u64> = OnceLock::new();

/// Counts the bytes read through it into `BYTES_READ`.
pub struct Counting<R>(pub R);

impl<R: Read> Read for Counting<R> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let n = self.0.read(buf)?;
        BYTES_READ.fetch_add(n as u64, Ordering::Relaxed);
        Ok(n)
    }
}

/// Record the input file's size, so progress can show how much of it has been read.
pub fn set_input_size(bytes: u64) {
    let _ = INPUT_SIZE.set(bytes);
}

/// Colour on stderr: only in a terminal that understands ANSI codes, and never with `NO_COLOR`.
#[derive(Clone, Copy)]
pub struct Style {
    color: bool,
}

impl Style {
    pub fn detect() -> Style {
        let no_color = std::env::var_os("NO_COLOR").is_some_and(|v| !v.is_empty());
        let dumb = std::env::var_os("TERM").is_some_and(|t| t == "dumb");
        // Windows consoles show raw escape codes unless the terminal enables them; Windows
        // Terminal does (WT_SESSION), as does any terminal that sets TERM.
        let ansi = cfg!(unix)
            || std::env::var_os("WT_SESSION").is_some()
            || std::env::var_os("TERM").is_some();
        Style {
            color: std::io::stderr().is_terminal() && ansi && !no_color && !dumb,
        }
    }

    #[cfg(test)]
    pub fn plain() -> Style {
        Style { color: false }
    }

    fn paint(self, code: &str, s: &str) -> String {
        if self.color {
            format!("\x1b[{code}m{s}\x1b[0m")
        } else {
            s.to_string()
        }
    }

    fn bold(self, s: &str) -> String {
        self.paint("1", s)
    }
    fn label(self, s: &str) -> String {
        self.paint("36", s)
    }
    fn good(self, s: &str) -> String {
        self.paint("32", s)
    }
    fn warn(self, s: &str) -> String {
        self.paint("33", s)
    }
}

/// An integer with thousands separators: 1234567 → "1,234,567".
pub fn grouped(n: u64) -> String {
    let digits = n.to_string();
    let mut out = String::with_capacity(digits.len() + digits.len() / 3);
    for (i, c) in digits.chars().enumerate() {
        if i > 0 && (digits.len() - i).is_multiple_of(3) {
            out.push(',');
        }
        out.push(c);
    }
    out
}

/// Elapsed time: "0.4 s", "12.3 s", "4:05", "1:02:03".
pub fn duration(d: Duration) -> String {
    let s = d.as_secs_f64();
    if s < 1.0 {
        return format!("{s:.2} s");
    }
    if s < 60.0 {
        return format!("{s:.1} s");
    }
    let t = d.as_secs();
    let (h, m, sec) = (t / 3600, t / 60 % 60, t % 60);
    if h > 0 {
        format!("{h}:{m:02}:{sec:02}")
    } else {
        format!("{m}:{sec:02}")
    }
}

/// Reads per second: "15,000 reads/s" or "1.92 M reads/s".
pub fn rate(reads: u64, d: Duration) -> String {
    let per_s = reads as f64 / d.as_secs_f64().max(1e-9);
    if per_s >= 1e6 {
        format!("{:.2} M reads/s", per_s / 1e6)
    } else {
        format!("{} reads/s", grouped(per_s.round() as u64))
    }
}

/// A PMD bound as given, without a trailing ".0": 3 → "3", 2.5 → "2.5".
fn bound(x: f64) -> String {
    let s = format!("{x}");
    s.strip_suffix(".0").map_or(s.clone(), str::to_string)
}

/// Everything the end-of-run summary shows.
pub struct RunReport {
    pub subcommand: String,
    pub input: String,
    pub format: &'static str,
    pub reference: Option<String>,
    pub library: &'static str,
    pub treatment: &'static str,
    pub compat: bool,
    pub threshold: Option<f64>,
    pub upper_threshold: Option<f64>,
    pub reads: u64,
    pub scored: u64,
    pub no_sites: u64,
    pub kept: Option<u64>,
    /// Skip reasons with their counts, in the summary's order; zero counts are left out here.
    pub skipped: Vec<(&'static str, u64)>,
    /// (T, scored reads with PMD score ≥ T): what `filter -t T` alone would keep.
    pub at_threshold: Vec<(f64, u64)>,
    /// First-base 5′ C→T rate, its standard error, and the number of sites, if damage was counted.
    pub first_base: Option<(f64, f64, u64)>,
    /// Where the damage pattern disagrees with `--library` or `--treatment` (src/mode_check.rs).
    pub mode_notes: Vec<String>,
    pub md_disagreeing: u64,
    /// Input records flagged as paired (mates not merged into one read).
    pub paired: u64,
    /// Header sequences missing from the reference given with `-r`; no read is mapped to them.
    pub missing_sequences: usize,
    pub outputs: Vec<String>,
    pub elapsed: Duration,
}

impl RunReport {
    /// The summary block, one line per topic, labels aligned.
    pub fn render(&self, style: Style) -> String {
        let treatment = match self.treatment {
            "half" => "UDG-half",
            "full" => "full UDG",
            _ => "no UDG",
        };
        let mut head = format!(
            "pmdshodh {} {} · {} · {treatment}",
            self.subcommand,
            env!("CARGO_PKG_VERSION"),
            self.library
        );
        let keep = match (self.threshold, self.upper_threshold) {
            (Some(t), Some(u)) => Some(format!("keep {} ≤ PMD < {}", bound(t), bound(u))),
            (Some(t), None) => Some(format!("keep PMD ≥ {}", bound(t))),
            (None, Some(u)) => Some(format!("keep PMD < {}", bound(u))),
            (None, None) => None,
        };
        if let Some(k) = keep {
            head += &format!(" · {k}");
        }
        if self.compat {
            head += " · --pmdtools-compat";
        }

        let mut lines = vec![style.bold(&head)];
        let mut row = |label: &str, text: String| {
            lines.push(format!("  {} {text}", style.label(&format!("{label:<9}"))));
        };
        let bases = match &self.reference {
            Some(r) => format!("reference bases from {r}"),
            None => "reference bases from MD tags".to_string(),
        };
        row(
            "input",
            format!("{} ({}) · {bases}", self.input, self.format.to_uppercase()),
        );
        let skipped: u64 = self.skipped.iter().map(|(_, n)| n).sum();
        let mut reads = format!(
            "{} read · {} scored · {} skipped",
            grouped(self.reads),
            grouped(self.scored),
            grouped(skipped)
        );
        if self.no_sites > 0 {
            reads += &format!(
                " · {} scored without informative sites",
                grouped(self.no_sites)
            );
        }
        row("reads", reads);
        if skipped > 0 {
            let why: Vec<String> = self
                .skipped
                .iter()
                .map(|(r, n)| format!("{r} {}", grouped(*n)))
                .collect();
            row("skipped", style.warn(&why.join(" · ")));
        }
        if let Some(kept) = self.kept {
            let share = if self.scored > 0 {
                format!(" ({:.1}%)", 100.0 * kept as f64 / self.scored as f64)
            } else {
                String::new()
            };
            row(
                "kept",
                style.good(&format!(
                    "{} of {} scored{share}",
                    grouped(kept),
                    grouped(self.scored)
                )),
            );
        }
        if self.scored > 0 && !self.at_threshold.is_empty() {
            let at: Vec<String> = self
                .at_threshold
                .iter()
                .map(|(t, n)| {
                    format!(
                        "≥{} {} ({:.1}%)",
                        bound(*t),
                        grouped(*n),
                        100.0 * *n as f64 / self.scored as f64
                    )
                })
                .collect();
            row("threshold", format!("{} of scored reads", at.join(" · ")));
        }
        if let Some((rate, se, n)) = self.first_base {
            row(
                "damage",
                format!(
                    "5′ C→T at the first base {:.1}% ± {:.*}% ({} site{})",
                    100.0 * rate,
                    // Large libraries have standard errors well under 0.05%.
                    if 100.0 * se < 0.05 { 2 } else { 1 },
                    100.0 * se,
                    grouped(n),
                    if n == 1 { "" } else { "s" }
                ),
            );
        }
        for note in &self.mode_notes {
            row("note", style.warn(note));
        }
        if self.md_disagreeing > 0 {
            row(
                "note",
                style.warn(&format!(
                    "{} scored reads have an MD tag that disagrees with the reference (written \
                     before realignment, or against N bases that the aligner replaced, as bwa \
                     does); they were scored from the reference",
                    grouped(self.md_disagreeing)
                )),
            );
        }
        if self.missing_sequences > 0 {
            row(
                "note",
                style.warn(&format!(
                    "{} header {} not in the reference; no read is mapped to {}",
                    grouped(self.missing_sequences as u64),
                    if self.missing_sequences == 1 {
                        "sequence is"
                    } else {
                        "sequences are"
                    },
                    if self.missing_sequences == 1 {
                        "it"
                    } else {
                        "them"
                    }
                )),
            );
        }
        if self.paired > 0 {
            row(
                "note",
                style.warn(&format!(
                    "{} input {}, not merged into one read: each mate is scored on its own{}, \
                     and its 3′ end is the end of the molecule only when the insert is no longer \
                     than the read",
                    grouped(self.paired),
                    if self.paired == 1 {
                        "read is a paired mate"
                    } else {
                        "reads are paired mates"
                    },
                    if self.kept.is_some() {
                        ", so one mate can be kept without the other"
                    } else {
                        ""
                    }
                )),
            );
        }
        let empty = if self.reads == 0 {
            Some("the input has no reads")
        } else if self.scored == 0 {
            Some("no read was scored: every read was skipped")
        } else if self.kept == Some(0) {
            Some("no read passed the filter: the output has no reads")
        } else {
            None
        };
        if let Some(e) = empty {
            row("note", style.warn(e));
        }
        if !self.outputs.is_empty() {
            row("written", self.outputs.join(" · "));
        }
        row(
            "time",
            format!(
                "{} · {}",
                duration(self.elapsed),
                rate(self.reads, self.elapsed)
            ),
        );
        lines.join("\n")
    }
}

/// A one-line progress display on stderr, in a terminal only: reads so far, rate, share of the
/// input file read, and elapsed time. It appears after a second, so short runs never show it,
/// redraws a few times a second, and is erased before anything else is printed.
pub struct Progress {
    label: &'static str,
    enabled: bool,
    start: Instant,
    last: Instant,
    width: usize,
}

impl Progress {
    const DELAY: Duration = Duration::from_secs(1);
    const EVERY: Duration = Duration::from_millis(250);

    pub fn new(label: &'static str, quiet: bool) -> Progress {
        let now = Instant::now();
        Progress {
            label,
            enabled: !quiet && std::io::stderr().is_terminal(),
            start: now,
            last: now,
            width: 0,
        }
    }

    pub fn elapsed(&self) -> Duration {
        self.start.elapsed()
    }

    /// Called after each batch of reads; cheap when there is nothing to draw.
    pub fn tick(&mut self, reads: u64) {
        if !self.enabled {
            return;
        }
        let now = Instant::now();
        if now.duration_since(self.start) < Self::DELAY
            || now.duration_since(self.last) < Self::EVERY
        {
            return;
        }
        self.last = now;
        let elapsed = now.duration_since(self.start);
        let mut text = format!(
            "  pmdshodh {} · {} reads · {}",
            self.label,
            grouped(reads),
            rate(reads, elapsed)
        );
        if let Some(&size) = INPUT_SIZE.get().filter(|&&s| s > 0) {
            let done = BYTES_READ.load(Ordering::Relaxed).min(size);
            text += &format!(" · {:.0}% of input", 100.0 * done as f64 / size as f64);
        }
        text += &format!(" · {}", duration(elapsed));
        let pad = self.width.saturating_sub(text.chars().count());
        self.width = text.chars().count();
        let mut err = std::io::stderr().lock();
        let _ = write!(err, "\r{text}{}", " ".repeat(pad));
        let _ = err.flush();
    }

    /// Erase the progress line, if one was drawn.
    pub fn clear(&mut self) {
        if self.width > 0 {
            let mut err = std::io::stderr().lock();
            let _ = write!(err, "\r{}\r", " ".repeat(self.width));
            let _ = err.flush();
            self.width = 0;
        }
    }
}

impl Drop for Progress {
    fn drop(&mut self) {
        self.clear();
    }
}

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

    #[test]
    fn numbers_and_times_are_formatted_for_people() {
        assert_eq!(grouped(0), "0");
        assert_eq!(grouped(999), "999");
        assert_eq!(grouped(1_000), "1,000");
        assert_eq!(grouped(14_006_183), "14,006,183");
        assert_eq!(duration(Duration::from_millis(400)), "0.40 s");
        assert_eq!(duration(Duration::from_millis(12_340)), "12.3 s");
        assert_eq!(duration(Duration::from_secs(245)), "4:05");
        assert_eq!(duration(Duration::from_secs(3723)), "1:02:03");
        assert_eq!(rate(15_000, Duration::from_secs(1)), "15,000 reads/s");
        assert_eq!(rate(19_200_000, Duration::from_secs(10)), "1.92 M reads/s");
        assert_eq!(bound(3.0), "3");
        assert_eq!(bound(2.5), "2.5");
        assert_eq!(bound(-1000.0), "-1000");
    }

    fn report() -> RunReport {
        RunReport {
            subcommand: "filter".into(),
            input: "in.bam".into(),
            format: "bam",
            reference: None,
            library: "ds",
            treatment: "none",
            compat: false,
            threshold: Some(3.0),
            upper_threshold: None,
            reads: 1_500,
            scored: 1_500,
            no_sites: 0,
            kept: Some(524),
            skipped: vec![],
            at_threshold: vec![(1.0, 1_200), (2.0, 800), (3.0, 524), (5.0, 201)],
            first_base: Some((0.398, 0.025, 372)),
            mode_notes: vec![],
            md_disagreeing: 0,
            paired: 0,
            missing_sequences: 0,
            outputs: vec!["out.bam".into()],
            elapsed: Duration::from_millis(100),
        }
    }

    #[test]
    fn summary_block_for_a_filter_run() {
        let text = report().render(Style::plain());
        let expected = format!(
            "pmdshodh filter {} · ds · no UDG · keep PMD ≥ 3
  input     in.bam (BAM) · reference bases from MD tags
  reads     1,500 read · 1,500 scored · 0 skipped
  kept      524 of 1,500 scored (34.9%)
  threshold ≥1 1,200 (80.0%) · ≥2 800 (53.3%) · ≥3 524 (34.9%) · ≥5 201 (13.4%) of scored reads
  damage    5′ C→T at the first base 39.8% ± 2.5% (372 sites)
  written   out.bam
  time      0.10 s · 15,000 reads/s",
            env!("CARGO_PKG_VERSION")
        );
        assert_eq!(text, expected);
    }

    #[test]
    fn summary_block_lists_skips_bounds_and_notes_only_when_present() {
        let mut r = report();
        r.subcommand = "score".into();
        r.kept = None;
        r.threshold = None;
        r.first_base = None;
        r.treatment = "half";
        r.library = "ss";
        r.compat = true;
        r.reference = Some("ref.fa".into());
        r.skipped = vec![("unmapped", 3), ("min_mapq", 1_200)];
        r.no_sites = 7;
        r.md_disagreeing = 2;
        r.first_base = Some((0.184, 0.00022, 3_097_363));
        assert!(
            r.render(Style::plain())
                .contains("5′ C→T at the first base 18.4% ± 0.02% (3,097,363 sites)")
        );
        r.first_base = Some((1.0, 0.0, 1));
        assert!(r.render(Style::plain()).contains("(1 site)"));
        r.first_base = None;
        let text = r.render(Style::plain());
        assert!(text.starts_with(&format!(
            "pmdshodh score {} · ss · UDG-half · --pmdtools-compat\n",
            env!("CARGO_PKG_VERSION")
        )));
        assert!(text.contains("in.bam (BAM) · reference bases from ref.fa"));
        assert!(text.contains(
            "reads     1,500 read · 1,500 scored · 1,203 skipped · 7 scored without informative sites"
        ));
        assert!(text.contains("skipped   unmapped 3 · min_mapq 1,200"));
        assert!(text.contains("note      2 scored reads have an MD tag that disagrees"));
        assert!(!text.contains("kept") && !text.contains("damage"));

        r.threshold = Some(3.0);
        r.upper_threshold = Some(10.0);
        assert!(r.render(Style::plain()).contains("keep 3 ≤ PMD < 10"));
        r.threshold = None;
        assert!(r.render(Style::plain()).contains("keep PMD < 10"));
    }

    #[test]
    fn summary_block_notes_paired_mates_and_empty_results() {
        let mut r = report();
        let text = r.render(Style::plain());
        assert!(!text.contains("paired") && !text.contains("no read"));

        r.paired = 1_200;
        assert!(r.render(Style::plain()).contains(
            "note      1,200 input reads are paired mates, not merged into one read: each mate is \
             scored on its own, so one mate can be kept without the other, and its 3′ end"
        ));
        r.paired = 1;
        r.kept = None;
        let text = r.render(Style::plain());
        assert!(text.contains("1 input read is a paired mate, not merged"));
        assert!(!text.contains("kept without"));

        r.paired = 0;
        r.kept = Some(0);
        assert!(
            r.render(Style::plain())
                .contains("note      no read passed the filter: the output has no reads")
        );
        r.scored = 0;
        r.skipped = vec![("unmapped", 1_500)];
        r.at_threshold = vec![(1.0, 0), (2.0, 0), (3.0, 0), (5.0, 0)];
        let text = r.render(Style::plain());
        assert!(text.contains("note      no read was scored: every read was skipped"));
        assert!(
            !text.contains("threshold") && !text.contains("NaN"),
            "{text}"
        );
        assert!(!text.contains("passed the filter"));
        r.reads = 0;
        r.skipped = vec![];
        assert!(
            r.render(Style::plain())
                .contains("note      the input has no reads")
        );
    }

    #[test]
    fn summary_block_notes_header_sequences_missing_from_the_reference() {
        let mut r = report();
        assert!(!r.render(Style::plain()).contains("not in the reference"));
        r.missing_sequences = 81;
        assert!(r.render(Style::plain()).contains(
            "note      81 header sequences are not in the reference; no read is mapped to them"
        ));
        r.missing_sequences = 1;
        assert!(
            r.render(Style::plain())
                .contains("1 header sequence is not in the reference; no read is mapped to it")
        );
    }

    #[test]
    fn summary_block_notes_a_damage_pattern_that_disagrees_with_the_mode() {
        let mut r = report();
        r.mode_notes = vec!["library note".into(), "treatment note".into()];
        let text = r.render(Style::plain());
        assert!(text.contains(
            "  damage    5′ C→T at the first base 39.8% ± 2.5% (372 sites)\n  \
             note      library note\n  note      treatment note\n  written"
        ));
    }

    #[test]
    fn colour_wraps_text_only_when_enabled() {
        let on = Style { color: true };
        assert_eq!(on.good("x"), "\x1b[32mx\x1b[0m");
        assert_eq!(Style::plain().good("x"), "x");
    }
}