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
//! Mode check: compare the damage pattern with the chosen `--library` and `--treatment` (roadmap
//! milestone 16, issue #96). A disagreement gives a note on stderr and `mode_check` in the JSON run
//! summary. The mode is never changed and the library type is never inferred, so scores and kept
//! reads do not depend on this check.
//!
//! The rates come from [`PatternCounts`], which are the same in every mode.

use serde_json::{Value, json};

use crate::damage::{PatternCounts, SiteCount};
use crate::sites::{Library, Treatment};

// The cutoffs below were set on real libraries (milestone 16, #96): untreated, UDG-half and full-UDG
// libraries, double- and single-stranded, and modern DNA, which must give no call.

/// Fewest sites behind every rate a call uses.
pub const MIN_SITES: u64 = 500;
/// A call needs a two-proportion z statistic of at least this.
const MIN_Z: f64 = 5.0;
/// Library: the larger 3′ rate (C→T or G→A) must exceed the smaller by this much…
const LIBRARY_MIN_DIFFERENCE: f64 = 0.005;
/// …and be at least this many times as large.
const LIBRARY_MIN_RATIO: f64 = 2.0;
/// Treatment: 5′ non-CpG C→T at the first base above the background, for half or none.
const DAMAGED_MIN_EXCESS: f64 = 0.04;
/// Excess at the second base over that at the first: below this is UDG-half…
const HALF_MAX_RATIO: f64 = 0.15;
/// …and above this untreated.
const NONE_MIN_RATIO: f64 = 0.35;
/// Full UDG: first-base non-CpG excess below this, and CpG C→T at the first base above the non-CpG
/// rate by at least `FULL_MIN_CPG_DIFFERENCE` and `FULL_MIN_CPG_RATIO` times.
const FULL_MAX_EXCESS: f64 = 0.02;
const FULL_MIN_CPG_DIFFERENCE: f64 = 0.02;
const FULL_MIN_CPG_RATIO: f64 = 3.0;

/// Two-proportion z statistic of `a`'s rate over `b`'s, with binomial standard errors. Both need
/// sites. Equal rates of 0 or 1 give NaN, which passes no cutoff.
fn z(a: SiteCount, b: SiteCount) -> f64 {
    let (x, y) = (a.rate().unwrap_or(f64::NAN), b.rate().unwrap_or(f64::NAN));
    let se = (x * (1.0 - x) / a.sites as f64 + y * (1.0 - y) / b.sites as f64).sqrt();
    (x - y) / se
}

/// One library test: single-stranded when C→T exceeds G→A at the 3′ end, double-stranded in the
/// mirror case, otherwise no call.
fn library_test(c_to_t: SiteCount, g_to_a: SiteCount) -> Option<Library> {
    let (Some(x), Some(y)) = (c_to_t.rate(), g_to_a.rate()) else {
        return None;
    };
    if c_to_t.sites < MIN_SITES || g_to_a.sites < MIN_SITES {
        return None;
    }
    let z = z(c_to_t, g_to_a);
    if x - y >= LIBRARY_MIN_DIFFERENCE && x >= LIBRARY_MIN_RATIO * y && z >= MIN_Z {
        Some(Library::SingleStranded)
    } else if y - x >= LIBRARY_MIN_DIFFERENCE && y >= LIBRARY_MIN_RATIO * x && -z >= MIN_Z {
        Some(Library::DoubleStranded)
    } else {
        None
    }
}

/// The library pattern and whether the CpG test decided it. Test A (all 3′ terminal bases) decides
/// when it calls; otherwise test B (3′ CpG). Opposite calls give no call.
pub fn library_pattern(
    terminal: (SiteCount, SiteCount),
    cpg: (SiteCount, SiteCount),
) -> (Option<Library>, bool) {
    match (
        library_test(terminal.0, terminal.1),
        library_test(cpg.0, cpg.1),
    ) {
        (Some(a), Some(b)) if a != b => (None, false),
        (Some(a), _) => (Some(a), false),
        (None, b) => (b, b.is_some()),
    }
}

/// The treatment pattern from 5′ non-CpG C→T at the first and second base, the non-CpG
/// background, and CpG C→T at the first base. Low damage alone is not full UDG: that needs CpG
/// damage well above the rest.
pub fn treatment_pattern(
    first: SiteCount,
    second: SiteCount,
    background: SiteCount,
    first_cpg: SiteCount,
) -> Option<Treatment> {
    if [first, second, background]
        .iter()
        .any(|c| c.sites < MIN_SITES)
    {
        return None;
    }
    let (p0, p1, b) = (first.rate()?, second.rate()?, background.rate()?);
    let (e0, e1) = (p0 - b, p1 - b);
    if e0 >= DAMAGED_MIN_EXCESS {
        let r = e1 / e0;
        return if r < HALF_MAX_RATIO {
            Some(Treatment::Half)
        } else if r > NONE_MIN_RATIO {
            Some(Treatment::None)
        } else {
            None
        };
    }
    let c0 = first_cpg.rate()?;
    (e0 < FULL_MAX_EXCESS
        && first_cpg.sites >= MIN_SITES
        && c0 - p0 >= FULL_MIN_CPG_DIFFERENCE
        && c0 >= FULL_MIN_CPG_RATIO * p0
        && z(first_cpg, first) >= MIN_Z)
        .then_some(Treatment::Full)
}

fn library_name(l: Library) -> &'static str {
    match l {
        Library::DoubleStranded => "ds",
        Library::SingleStranded => "ss",
    }
}

fn treatment_name(t: Treatment) -> &'static str {
    match t {
        Treatment::None => "none",
        Treatment::Half => "half",
        Treatment::Full => "full",
    }
}

/// A rate in percent for the notes: 0.178 → "17.8%".
fn percent(c: SiteCount) -> String {
    format!("{:.1}%", 100.0 * c.rate().unwrap_or(0.0))
}

/// The patterns found in one run, with the mode the reads were scored in.
pub struct ModeCheck {
    counts: PatternCounts,
    library: Library,
    treatment: Treatment,
    pub library_pattern: Option<Library>,
    /// The library pattern was decided by the 3′ CpG test (B).
    pub library_by_cpg: bool,
    pub treatment_pattern: Option<Treatment>,
}

impl ModeCheck {
    pub fn new(counts: &PatternCounts, library: Library, treatment: Treatment) -> ModeCheck {
        let (library_pattern, library_by_cpg) = library_pattern(
            (counts.three_prime_c, counts.three_prime_g),
            (counts.three_prime_cpg_c, counts.three_prime_cpg_g),
        );
        let treatment_pattern = treatment_pattern(
            counts.five_prime_first,
            counts.five_prime_second,
            counts.five_prime_background,
            counts.five_prime_cpg,
        );
        ModeCheck {
            counts: *counts,
            library,
            treatment,
            library_pattern,
            library_by_cpg,
            treatment_pattern,
        }
    }

    /// `None` without a call.
    pub fn library_agrees(&self) -> Option<bool> {
        self.library_pattern.map(|p| p == self.library)
    }

    pub fn treatment_agrees(&self) -> Option<bool> {
        self.treatment_pattern.map(|p| p == self.treatment)
    }

    /// The `mode_check` object of the JSON run summary. Rates are null without sites.
    pub fn to_json(&self) -> Value {
        let c = &self.counts;
        json!({
            "library": {
                "pattern": self.library_pattern.map(library_name),
                "agrees": self.library_agrees(),
                "three_prime_c_to_t": c.three_prime_c.rate(),
                "three_prime_g_to_a": c.three_prime_g.rate(),
                "c_sites": c.three_prime_c.sites,
                "g_sites": c.three_prime_g.sites,
                "cpg_c_to_t": c.three_prime_cpg_c.rate(),
                "cpg_g_to_a": c.three_prime_cpg_g.rate(),
                // Each base of the dinucleotide has its own quality filter; the smaller count.
                "cpg_sites": c.three_prime_cpg_c.sites.min(c.three_prime_cpg_g.sites),
            },
            "treatment": {
                "pattern": self.treatment_pattern.map(treatment_name),
                "agrees": self.treatment_agrees(),
                "first_base": c.five_prime_first.rate(),
                "second_base": c.five_prime_second.rate(),
                "background": c.five_prime_background.rate(),
                "sites": c.five_prime_first.sites,
                "first_base_cpg": c.five_prime_cpg.rate(),
                "cpg_sites": c.five_prime_cpg.sites,
            },
        })
    }

    /// One note per disagreement, for the summary block on stderr.
    pub fn notes(&self) -> Vec<String> {
        let c = &self.counts;
        let mut notes = Vec::new();
        if let (Some(pattern), Some(false)) = (self.library_pattern, self.library_agrees()) {
            let (ct, ga, place) = if self.library_by_cpg {
                (
                    c.three_prime_cpg_c,
                    c.three_prime_cpg_g,
                    "at CpG sites the 3′ ends show",
                )
            } else {
                (c.three_prime_c, c.three_prime_g, "the 3′ ends show")
            };
            let (more, less, kind) = match pattern {
                Library::SingleStranded => (
                    format!("C→T ({})", percent(ct)),
                    format!("G→A ({})", percent(ga)),
                    "single",
                ),
                Library::DoubleStranded => (
                    format!("G→A ({})", percent(ga)),
                    format!("C→T ({})", percent(ct)),
                    "double",
                ),
            };
            notes.push(format!(
                "{place} {more} more than {less}, as {kind}-stranded libraries do; the reads were \
                 scored as --library {}",
                library_name(self.library)
            ));
        }
        if let (Some(pattern), Some(false)) = (self.treatment_pattern, self.treatment_agrees()) {
            let (p0, p1) = (percent(c.five_prime_first), percent(c.five_prime_second));
            let seen = match pattern {
                Treatment::Full => format!(
                    "5′ C→T at the first base is {} at CpG sites but {p0} elsewhere, as after full \
                     UDG treatment",
                    percent(c.five_prime_cpg)
                ),
                Treatment::Half => format!(
                    "5′ C→T outside CpG sites is {p0} at the first base but {p1} at the second, as \
                     after UDG-half treatment"
                ),
                Treatment::None => format!(
                    "5′ C→T outside CpG sites is {p0} at the first base and {p1} at the second, as \
                     in untreated libraries"
                ),
            };
            notes.push(format!(
                "{seen}; the reads were scored as --treatment {}",
                treatment_name(self.treatment)
            ));
        }
        notes
    }
}

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

    /// `rate` of `n` sites, rounded to whole sites.
    fn count(rate: f64, n: u64) -> SiteCount {
        SiteCount {
            sites: n,
            changed: (rate * n as f64).round() as u64,
        }
    }

    /// Counts in the tens of thousands, the low end of the real libraries.
    const N: u64 = 20_000;

    fn treatment(p0: f64, p1: f64, b: f64, c0: f64) -> Option<Treatment> {
        treatment_pattern(count(p0, N), count(p1, N), count(b, N), count(c0, N))
    }

    fn library(ct: f64, ga: f64, cpg_ct: f64, cpg_ga: f64) -> Option<Library> {
        library_pattern(
            (count(ct, N), count(ga, N)),
            (count(cpg_ct, N), count(cpg_ga, N)),
        )
        .0
    }

    /// 5′ rates measured on real libraries (#96): p0, p1, background, CpG at the first base.
    #[test]
    fn treatment_of_real_libraries() {
        use Treatment as T;
        for (name, [p0, p1, b, c0], want) in [
            (
                "untreated ds BER11_NOuser",
                [0.181, 0.120, 0.033, 0.197],
                Some(T::None),
            ),
            (
                "untreated ss UKY001",
                [0.258, 0.149, 0.048, 0.194],
                Some(T::None),
            ),
            (
                "untreated ss wolf",
                [0.177, 0.107, 0.037, 0.206],
                Some(T::None),
            ),
            (
                "half ss I19359",
                [0.225, 0.0036, 0.0021, 0.297],
                Some(T::Half),
            ),
            (
                "full ds BER11_USER",
                [0.0019, 0.0018, 0.0015, 0.062],
                Some(T::Full),
            ),
            (
                "full ds Motala3",
                [0.0037, 0.0019, 0.0010, 0.197],
                Some(T::Full),
            ),
            ("JK2067", [0.0096, 0.0030, 0.0010, 0.0165], None),
            ("modern NA12878", [0.0012, 0.0013, 0.0016, 0.003], None),
        ] {
            assert_eq!(treatment(p0, p1, b, c0), want, "{name}");
        }
        // ZKU002 has few sites: about 6,600 non-CpG at the first base and 535 CpG.
        let zku = |n: u64, cpg: u64| {
            treatment_pattern(
                count(0.18, n),
                count(0.11, n),
                count(0.035, 5 * n),
                count(0.2, cpg),
            )
        };
        assert_eq!(zku(6_600, 535), Some(T::None));
    }

    /// 3′ rates measured on real libraries (#96): test A (all contexts), then test B (CpG).
    #[test]
    fn library_of_real_libraries() {
        use Library::*;
        let no_b = (0.0, 0.0);
        for (name, a, b, want) in [
            ("ss UKY001", (0.178, 0.013), no_b, Some(SingleStranded)),
            ("ss I19359", (0.20, 0.004), no_b, Some(SingleStranded)),
            (
                "ds BER11_NOuser",
                (0.011, 0.126),
                no_b,
                Some(DoubleStranded),
            ),
            ("ds JK2067", (0.0037, 0.0129), no_b, Some(DoubleStranded)),
            ("ds Motala3", (0.0021, 0.0138), no_b, Some(DoubleStranded)),
            (
                "BER11_USER",
                (0.0023, 0.0047),
                (0.0097, 0.0426),
                Some(DoubleStranded),
            ),
            ("NA12878", (0.0015, 0.0009), (0.0026, 0.0034), None),
        ] {
            assert_eq!(library(a.0, a.1, b.0, b.1), want, "{name}");
        }
        // Test A makes no call on BER11_USER; B decides.
        assert_eq!(library_test(count(0.0023, N), count(0.0047, N)), None);
        let (pattern, by_cpg) = library_pattern(
            (count(0.0023, N), count(0.0047, N)),
            (count(0.0097, N), count(0.0426, N)),
        );
        assert_eq!((pattern, by_cpg), (Some(DoubleStranded), true));
    }

    #[test]
    fn library_boundaries() {
        use Library::*;
        let n = 1_000_000; // z is far above 5 here, so only the rate cutoffs decide.
        let test = |x: f64, y: f64| library_test(count(x, n), count(y, n));
        // Difference: 0.0051 calls, 0.0049 does not (ratio well above 2).
        assert_eq!(test(0.0051, 0.0), Some(SingleStranded));
        assert_eq!(test(0.0049, 0.0), None);
        assert_eq!(test(0.0, 0.0051), Some(DoubleStranded));
        // Ratio: 2.02 calls, 1.98 does not (difference well above 0.005).
        assert_eq!(test(0.202, 0.1), Some(SingleStranded));
        assert_eq!(test(0.198, 0.1), None);
        assert_eq!(test(0.1, 0.202), Some(DoubleStranded));
        // z: rates that pass both cutoffs give no call on too few sites.
        let few = |n: u64| library_test(count(0.03, n), count(0.01, n));
        assert_eq!(few(20_000), Some(SingleStranded));
        assert_eq!(few(1_000), None); // z ≈ 4.5
        // Under MIN_SITES on either side: no call, however clear the rates.
        assert_eq!(library_test(count(0.5, 499), count(0.0, n)), None);
        assert_eq!(library_test(count(0.5, n), count(0.0, 499)), None);
        assert_eq!(
            library_test(count(0.5, 500), count(0.0, 500)),
            Some(SingleStranded)
        );
        assert_eq!(
            library_test(SiteCount::default(), SiteCount::default()),
            None
        );
    }

    #[test]
    fn library_tests_combine() {
        use Library::*;
        let ss = (count(0.2, N), count(0.01, N));
        let ds = (count(0.01, N), count(0.2, N));
        let none = (count(0.002, N), count(0.002, N));
        assert_eq!(library_pattern(ss, none), (Some(SingleStranded), false));
        assert_eq!(library_pattern(ss, ss), (Some(SingleStranded), false));
        assert_eq!(library_pattern(none, ds), (Some(DoubleStranded), true));
        assert_eq!(library_pattern(none, none), (None, false));
        // A and B call opposite things: no call.
        assert_eq!(library_pattern(ss, ds), (None, false));
        assert_eq!(library_pattern(ds, ss), (None, false));
    }

    #[test]
    fn treatment_boundaries() {
        use Treatment as T;
        let t = |p0: f64, p1: f64, b: f64, c0: f64| {
            let n = 1_000_000;
            treatment_pattern(count(p0, n), count(p1, n), count(b, n), count(c0, n))
        };
        // Excess at the first base: 0.041 is damaged, 0.039 is not (and not full: no CpG excess).
        assert_eq!(t(0.051, 0.03, 0.01, 0.05), Some(T::None));
        assert_eq!(t(0.049, 0.03, 0.01, 0.05), None);
        // Ratio of excesses: 0.14 is half, 0.16 to 0.34 no call, 0.36 untreated.
        assert_eq!(t(0.11, 0.024, 0.01, 0.1), Some(T::Half));
        assert_eq!(t(0.11, 0.026, 0.01, 0.1), None);
        assert_eq!(t(0.11, 0.044, 0.01, 0.1), None);
        assert_eq!(t(0.11, 0.046, 0.01, 0.1), Some(T::None));
        // Full: first-base excess 0.019 passes, 0.021 does not.
        assert_eq!(t(0.02, 0.0, 0.001, 0.1), Some(T::Full));
        assert_eq!(t(0.022, 0.0, 0.001, 0.1), None);
        // CpG excess 0.021 passes, 0.019 does not; CpG at least 3 times the rest.
        assert_eq!(t(0.005, 0.005, 0.005, 0.026), Some(T::Full));
        assert_eq!(t(0.005, 0.005, 0.005, 0.024), None);
        assert_eq!(t(0.015, 0.015, 0.015, 0.046), Some(T::Full));
        assert_eq!(t(0.015, 0.015, 0.015, 0.044), None);
        // Low damage alone is not full.
        assert_eq!(t(0.001, 0.001, 0.001, 0.001), None);
    }

    #[test]
    fn treatment_needs_min_sites() {
        use Treatment as T;
        let c = |rate: f64, n: u64| count(rate, n);
        let half = |n0: u64, n1: u64, nb: u64| {
            treatment_pattern(c(0.225, n0), c(0.0036, n1), c(0.0021, nb), c(0.297, 0))
        };
        assert_eq!(
            half(N, N, N),
            Some(T::Half),
            "CpG sites are not needed for half"
        );
        assert_eq!(half(499, N, N), None);
        assert_eq!(half(N, 499, N), None);
        assert_eq!(half(N, N, 499), None);
        let full =
            |ncpg: u64| treatment_pattern(c(0.0037, N), c(0.0019, N), c(0.001, N), c(0.197, ncpg));
        assert_eq!(full(500), Some(T::Full));
        assert_eq!(full(499), None);
        // z of CpG over non-CpG under 5: no call.
        let weak = treatment_pattern(c(0.01, 600), c(0.01, N), c(0.01, N), c(0.04, 600));
        assert_eq!(weak, None);
    }

    fn counts(ct: f64, ga: f64, p0: f64, p1: f64, b: f64, c0: f64) -> PatternCounts {
        PatternCounts {
            three_prime_c: count(ct, N),
            three_prime_g: count(ga, N),
            five_prime_first: count(p0, N),
            five_prime_second: count(p1, N),
            five_prime_background: count(b, 5 * N),
            five_prime_cpg: count(c0, N),
            ..PatternCounts::default()
        }
    }

    /// #102: `mode_check.*.pattern` has a fixed set of names in `format_version` 5; a new one
    /// needs a new version (README, "Compatibility of the JSON run summary").
    #[test]
    fn pattern_names_are_frozen() {
        let libraries = [Library::DoubleStranded, Library::SingleStranded].map(library_name);
        assert_eq!(libraries, ["ds", "ss"]);
        let treatments = [Treatment::None, Treatment::Half, Treatment::Full].map(treatment_name);
        assert_eq!(treatments, ["none", "half", "full"]);
    }

    #[test]
    fn notes_and_json_for_disagreements() {
        // UKY001: untreated single-stranded, scored as ds.
        let uky = counts(0.178, 0.013, 0.258, 0.149, 0.048, 0.194);
        let m = ModeCheck::new(&uky, Library::DoubleStranded, Treatment::None);
        assert_eq!(
            m.notes(),
            [
                "the 3′ ends show C→T (17.8%) more than G→A (1.3%), as single-stranded libraries \
              do; the reads were scored as --library ds"
            ]
        );
        let j = m.to_json();
        assert_eq!(j["library"]["pattern"], "ss");
        assert_eq!(j["library"]["agrees"], false);
        assert_eq!(j["library"]["c_sites"], N);
        assert_eq!(j["library"]["cpg_c_to_t"], Value::Null);
        assert_eq!(j["library"]["cpg_sites"], 0);
        assert_eq!(j["treatment"]["pattern"], "none");
        assert_eq!(j["treatment"]["agrees"], true);
        assert_eq!(j["treatment"]["sites"], N);
        assert_eq!(j["treatment"]["background"], 0.048);
        assert!(
            ModeCheck::new(&uky, Library::SingleStranded, Treatment::None)
                .notes()
                .is_empty()
        );

        // BER11_USER: full UDG, ds by the CpG test, scored as ss and untreated.
        let mut user = counts(0.0023, 0.0047, 0.0019, 0.0018, 0.0015, 0.062);
        user.three_prime_cpg_c = count(0.0097, N);
        user.three_prime_cpg_g = count(0.0426, N);
        let m = ModeCheck::new(&user, Library::SingleStranded, Treatment::None);
        assert_eq!(
            m.notes(),
            [
                "at CpG sites the 3′ ends show G→A (4.3%) more than C→T (1.0%), as \
                 double-stranded libraries do; the reads were scored as --library ss",
                "5′ C→T at the first base is 6.2% at CpG sites but 0.2% elsewhere, as after full \
                 UDG treatment; the reads were scored as --treatment none",
            ]
        );
        assert_eq!(m.to_json()["library"]["cpg_sites"], N);

        // I19359 scored as untreated, BER11_NOuser scored as UDG-half.
        let half = counts(0.20, 0.004, 0.225, 0.0036, 0.0021, 0.297);
        let m = ModeCheck::new(&half, Library::SingleStranded, Treatment::None);
        assert_eq!(
            m.notes(),
            [
                "5′ C→T outside CpG sites is 22.5% at the first base but 0.4% at the second, as \
              after UDG-half treatment; the reads were scored as --treatment none"
            ]
        );
        let none = counts(0.011, 0.126, 0.181, 0.120, 0.033, 0.197);
        let m = ModeCheck::new(&none, Library::DoubleStranded, Treatment::Half);
        assert_eq!(
            m.notes(),
            [
                "5′ C→T outside CpG sites is 18.1% at the first base and 12.0% at the second, as in \
              untreated libraries; the reads were scored as --treatment half"
            ]
        );

        // No sites at all: no call, rates null, no note.
        let m = ModeCheck::new(
            &PatternCounts::default(),
            Library::DoubleStranded,
            Treatment::None,
        );
        assert!(m.notes().is_empty());
        let j = m.to_json();
        for side in ["library", "treatment"] {
            assert_eq!(j[side]["pattern"], Value::Null);
            assert_eq!(j[side]["agrees"], Value::Null);
        }
        assert_eq!(j["library"]["three_prime_c_to_t"], Value::Null);
        assert_eq!(j["treatment"]["first_base"], Value::Null);
        assert_eq!(j["treatment"]["background"], Value::Null);
        assert_eq!(j["treatment"]["sites"], 0);
    }
}