use std::io::{IsTerminal, Read, Write};
use std::sync::OnceLock;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};
static BYTES_READ: AtomicU64 = AtomicU64::new(0);
static INPUT_SIZE: OnceLock<u64> = OnceLock::new();
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)
}
}
pub fn set_input_size(bytes: u64) {
let _ = INPUT_SIZE.set(bytes);
}
#[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");
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)
}
}
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
}
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}")
}
}
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))
}
}
fn bound(x: f64) -> String {
let s = format!("{x}");
s.strip_suffix(".0").map_or(s.clone(), str::to_string)
}
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>,
pub skipped: Vec<(&'static str, u64)>,
pub first_base: Option<(f64, f64, u64)>,
pub md_disagreeing: u64,
pub paired: u64,
pub missing_sequences: usize,
pub outputs: Vec<String>,
pub elapsed: Duration,
}
impl RunReport {
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 let Some((rate, se, n)) = self.first_base {
row(
"damage",
format!(
"5′ C→T at the first base {:.1}% ± {:.*}% ({} site{})",
100.0 * rate,
if 100.0 * se < 0.05 { 2 } else { 1 },
100.0 * se,
grouped(n),
if n == 1 { "" } else { "s" }
),
);
}
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")
}
}
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()
}
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();
}
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![],
first_base: Some((0.398, 0.025, 372)),
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%)
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)];
let text = r.render(Style::plain());
assert!(text.contains("note no read was scored: every read was skipped"));
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 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");
}
}