|
| 1 | +//! `eval-germline` must normalize each variant against its OWN contig. Loading |
| 2 | +//! only the first FASTA record silently miscompared (or crashed with an opaque |
| 3 | +//! out-of-bounds error) on any multi-contig benchmark — the headline truth- |
| 4 | +//! comparison surface, and exactly what a real GIAB run looks like. |
| 5 | +
|
| 6 | +use std::path::PathBuf; |
| 7 | +use std::process::Command; |
| 8 | +use std::sync::atomic::{AtomicU64, Ordering}; |
| 9 | + |
| 10 | +fn bin() -> &'static str { |
| 11 | + env!("CARGO_BIN_EXE_rosalind") |
| 12 | +} |
| 13 | + |
| 14 | +fn unique_dir(prefix: &str) -> PathBuf { |
| 15 | + static COUNTER: AtomicU64 = AtomicU64::new(0); |
| 16 | + let n = COUNTER.fetch_add(1, Ordering::Relaxed); |
| 17 | + let nanos = std::time::SystemTime::now() |
| 18 | + .duration_since(std::time::UNIX_EPOCH) |
| 19 | + .unwrap() |
| 20 | + .as_nanos(); |
| 21 | + let d = std::env::temp_dir().join(format!("{prefix}-{nanos}-{n}")); |
| 22 | + std::fs::create_dir_all(&d).unwrap(); |
| 23 | + d |
| 24 | +} |
| 25 | + |
| 26 | +// chr1 = 20 bp, chr2 = 40 bp. |
| 27 | +const REFERENCE: &str = |
| 28 | + ">chr1\nACGTACGTACGTACGTACGT\n>chr2\nACGTACGTACGTACGTACGTACGTACGTACGTACGTACGT\n"; |
| 29 | + |
| 30 | +#[test] |
| 31 | +fn eval_germline_compares_each_variant_against_its_own_contig() { |
| 32 | + let dir = unique_dir("rosalind-eval-mc"); |
| 33 | + let fa = dir.join("ref.fa"); |
| 34 | + std::fs::write(&fa, REFERENCE).unwrap(); |
| 35 | + |
| 36 | + // A chr1 SNV (pos 5) and a chr2 SNV at pos 30 — past chr1's length (20), so |
| 37 | + // the old first-contig-only path crashed out-of-bounds on the chr2 variant. |
| 38 | + let header = "##fileformat=VCFv4.3\n#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n"; |
| 39 | + let calls = dir.join("calls.vcf"); |
| 40 | + std::fs::write( |
| 41 | + &calls, |
| 42 | + format!("{header}chr1\t5\t.\tA\tC\t50\tPASS\t.\nchr2\t30\t.\tG\tT\t50\tPASS\t.\n"), |
| 43 | + ) |
| 44 | + .unwrap(); |
| 45 | + let truth = dir.join("truth.vcf"); |
| 46 | + std::fs::write( |
| 47 | + &truth, |
| 48 | + format!("{header}chr1\t5\t.\tA\tC\t50\tPASS\t.\nchr2\t30\t.\tG\tT\t50\tPASS\t.\n"), |
| 49 | + ) |
| 50 | + .unwrap(); |
| 51 | + |
| 52 | + let out = Command::new(bin()) |
| 53 | + .args(["eval-germline", "--reference"]) |
| 54 | + .arg(&fa) |
| 55 | + .arg("--calls") |
| 56 | + .arg(&calls) |
| 57 | + .arg("--truth") |
| 58 | + .arg(&truth) |
| 59 | + .output() |
| 60 | + .unwrap(); |
| 61 | + assert!( |
| 62 | + out.status.success(), |
| 63 | + "multi-contig eval must succeed: {}", |
| 64 | + String::from_utf8_lossy(&out.stderr) |
| 65 | + ); |
| 66 | + let stdout = String::from_utf8_lossy(&out.stdout); |
| 67 | + // Both contigs' variants match truth → 2 TP, 0 FP, 0 FN. |
| 68 | + assert!( |
| 69 | + stdout.contains("tp=2"), |
| 70 | + "expected 2 true positives: {stdout}" |
| 71 | + ); |
| 72 | + assert!( |
| 73 | + stdout.contains("fp=0"), |
| 74 | + "expected 0 false positives: {stdout}" |
| 75 | + ); |
| 76 | + assert!( |
| 77 | + stdout.contains("fn=0"), |
| 78 | + "expected 0 false negatives: {stdout}" |
| 79 | + ); |
| 80 | + std::fs::remove_dir_all(&dir).ok(); |
| 81 | +} |
| 82 | + |
| 83 | +#[test] |
| 84 | +fn eval_germline_errors_clearly_on_a_contig_naming_mismatch() { |
| 85 | + let dir = unique_dir("rosalind-eval-naming"); |
| 86 | + let fa = dir.join("ref.fa"); |
| 87 | + std::fs::write(&fa, REFERENCE).unwrap(); |
| 88 | + |
| 89 | + // A variant on a contig the FASTA does not contain (Ensembl '1' vs UCSC |
| 90 | + // 'chr1') must error clearly, not silently miscompare. |
| 91 | + let header = "##fileformat=VCFv4.3\n#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n"; |
| 92 | + let calls = dir.join("calls.vcf"); |
| 93 | + std::fs::write(&calls, format!("{header}1\t5\t.\tA\tC\t50\tPASS\t.\n")).unwrap(); |
| 94 | + let truth = dir.join("truth.vcf"); |
| 95 | + std::fs::write(&truth, header).unwrap(); |
| 96 | + |
| 97 | + let out = Command::new(bin()) |
| 98 | + .args(["eval-germline", "--reference"]) |
| 99 | + .arg(&fa) |
| 100 | + .arg("--calls") |
| 101 | + .arg(&calls) |
| 102 | + .arg("--truth") |
| 103 | + .arg(&truth) |
| 104 | + .output() |
| 105 | + .unwrap(); |
| 106 | + assert!(!out.status.success(), "naming mismatch must fail"); |
| 107 | + let stderr = String::from_utf8_lossy(&out.stderr); |
| 108 | + assert!( |
| 109 | + stderr.contains("naming scheme") || stderr.contains("no matching sequence"), |
| 110 | + "expected a clear naming-mismatch error: {stderr}" |
| 111 | + ); |
| 112 | + std::fs::remove_dir_all(&dir).ok(); |
| 113 | +} |
0 commit comments