From 912466da701264a07fb7a75314602e45464cebb7 Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:04:21 +0100 Subject: [PATCH 1/6] test(mito): a shifted poly-C indel and a NuMT filter without depth must show in the annotation The m.315 C>CC call is the m.310 T>TC insertion ClinVar and gnomAD list, and m.308 CC>C is m.302 AC>A; today neither meets its record. A run whose NuMT filter had no autosomal depth must say so in the table. Both tests fail on main. Bead: lin-35 Bead: lin-32 --- rust/crates/og-summary/src/mito/tests.rs | 143 +++++++++++++++++++++++ 1 file changed, 143 insertions(+) diff --git a/rust/crates/og-summary/src/mito/tests.rs b/rust/crates/og-summary/src/mito/tests.rs index fc6e18b10..6c41f95e5 100644 --- a/rust/crates/og-summary/src/mito/tests.rs +++ b/rust/crates/og-summary/src/mito/tests.rs @@ -233,3 +233,146 @@ fn verbs_and_usage() { assert_eq!(code, 2); assert!(err.contains("required: --idxstats"), "{err}"); } + +/// Writes `text` as a BGZF VCF with its `.tbi` and returns its path. +fn put_indexed(d: &Dir, name: &str, text: &str) -> String { + use std::fs::File; + use std::num::NonZero; + let p = d.path(name); + let mut w = og_io::bgzf::Writer::new(File::create(&p).unwrap(), NonZero::::MIN).unwrap(); + w.write_all(text.as_bytes()).unwrap(); + w.finish().unwrap(); + og_io::tabix_write::build(File::open(&p).unwrap()) + .unwrap() + .write_tbi(std::path::Path::new(&format!("{p}.tbi")), true) + .unwrap(); + p +} + +/// Indels in the D-loop poly-C tracts, each spelled at the other end of its +/// repeat from the known record: the rCRS has A at m.302, C at m.303-309, +/// T at m.310, C at m.311-315 and G at m.316. +const SHIFTED_VCF: &str = "##fileformat=VCFv4.2\n#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\tS\n\ +chrM\t302\t.\tAC\tACC\t.\tPASS\t.\tGT:AF\t0/1:0.2\n\ +chrM\t308\tdel\tCC\tC\t.\tPASS\t.\tGT:AF\t0/1:0.3\n\ +chrM\t315\tins\tC\tCC\t.\tPASS\t.\tGT:AF\t0/1:0.4\n\ +chrM\t16193\t.\tC\tCC\t.\tPASS\t.\tGT:AF\t0/1:0.5\n"; + +/// ClinVar and gnomAD spell them leftmost; ClinVar also spells the m.303 +/// insertion a base to the right of its exact match at m.302. +const SHIFTED_CLINVAR: &str = "##fileformat=VCFv4.2\n##fileDate=2026-11-01\n\ +#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n\ +chrM\t302\t8\tA\tAC\t.\t.\tCLNSIG=Benign;CLNREVSTAT=criteria_provided,_single_submitter;CLNDN=exact\n\ +chrM\t302\t11\tAC\tA\t.\t.\tCLNSIG=Benign;CLNREVSTAT=no_assertion_criteria_provided;CLNDN=deletion\n\ +chrM\t303\t10\tC\tCC\t.\t.\tCLNSIG=Likely_benign;CLNDN=shifted\n\ +chrM\t310\t12\tT\tTC\t.\t.\tCLNSIG=Benign;CLNREVSTAT=criteria_provided,_single_submitter;CLNDN=poly-C\n"; + +const SHIFTED_GNOMAD: &str = "##fileformat=VCFv4.2\n#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n\ +chrM\t302\t.\tAC\tA\t.\tPASS\tAF_hom=0.03;AF_het=0.1;max_hl=0.5\n\ +chrM\t310\t.\tT\tTC\t.\tPASS\tAF_hom=0.4;AF_het=0.3;max_hl=0.99\n\ +chrM\t16189\t.\tT\tTC\t.\tPASS\tAF_hom=0.05;AF_het=0.2;max_hl=0.98\n"; + +#[test] +fn annotate_matches_an_indel_anywhere_in_its_repeat() { + let d = Dir::new("mito-shifted"); + let vcf = d.put_gz("S_chrM_filtered.vcf.gz", SHIFTED_VCF); + let cv = put_indexed(&d, "clinvar_chr.vcf.gz", SHIFTED_CLINVAR); + let gn = d.put("gnomad.vcf", SHIFTED_GNOMAD); + let out = d.path("a.tsv"); + let (code, stdout, err) = run( + cli_of("annotate"), + &[ + "--vcf", + &vcf, + "--clinvar", + &cv, + "--gnomad", + &gn, + "--out", + &out, + ], + ); + assert_eq!(code, 0, "{err}"); + let rows: Vec> = d + .read("a.tsv") + .lines() + .filter(|l| !l.starts_with('#') && !l.starts_with("chrom\t")) + .map(|l| l.split('\t').map(str::to_owned).collect()) + .collect(); + let cols = |r: &Vec| -> Vec { + [1, 2, 3, 6, 9, 10].iter().map(|&k| r[k].clone()).collect() + }; + let want: [[&str; 6]; 4] = [ + // An exact spelling wins over a shifted one (m.303 C>CC). + ["302", "A", "AC", "8", "exact", "."], + // m.308 CC>C is m.302 AC>A, the deletion of one C of m.303-309. + ["308", "CC", "C", "11", "deletion", "0.03"], + // m.315 C>CC is m.310 T>TC, the m.310 poly-C insertion. + ["315", "C", "CC", "12", "poly-C", "0.4"], + // m.16193 C>CC is m.16189 T>TC, in the m.16184-16193 tract. + ["16193", "C", "CC", ".", ".", "0.05"], + ]; + assert_eq!(rows.iter().map(cols).collect::>(), want, "{stdout}"); + assert!( + stdout.ends_with(" 4 chrM alleles: 3 with a ClinVar record, 3 in gnomAD\n"), + "{stdout}" + ); +} + +#[test] +fn annotate_keeps_an_allele_off_the_rcrs_as_it_is() { + // A REF that is not the rCRS there (another chrM) is not moved: m.315 + // is C on the rCRS, so G>GG stays where it is. Moved over the rCRS it + // would read m.314 C>CG and meet that record. + let d = Dir::new("mito-off-rcrs"); + let vcf = d.put_gz( + "S_chrM_filtered.vcf.gz", + "##fileformat=VCFv4.2\n#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\tS\n\ + chrM\t315\t.\tG\tGG\t.\tPASS\t.\tGT:AF\t0/1:0.4\n", + ); + let gn = d.put( + "gnomad.vcf", + "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n\ + chrM\t314\t.\tC\tCG\t.\tPASS\tAF_hom=0.7;AF_het=0.1;max_hl=0.9\n", + ); + let out = d.path("a.tsv"); + let (code, _, err) = run( + cli_of("annotate"), + &["--vcf", &vcf, "--gnomad", &gn, "--out", &out], + ); + assert_eq!(code, 0, "{err}"); + let row = d.read("a.tsv").lines().last().unwrap().to_owned(); + assert!(row.starts_with("chrM\t315\tG\tGG\tPASS\t0.4\t"), "{row}"); + assert!(row.ends_with("\t.\t.\t."), "{row}"); +} + +#[test] +fn annotate_says_when_the_numt_filter_had_no_depth() { + let d = Dir::new("mito-numt"); + let vcf = d.put_gz("S_chrM_filtered.vcf.gz", MITO_VCF); + let out = d.path("a.tsv"); + let (code, stdout, err) = run( + cli_of("annotate"), + &["--vcf", &vcf, "--numt-default", "--out", &out], + ); + assert_eq!(code, 0, "{err}"); + let head: Vec = d + .read("a.tsv") + .lines() + .take_while(|l| l.starts_with('#')) + .map(str::to_owned) + .collect(); + assert_eq!( + head[2], + "# numt\tNuMT filter used a default depth\tno median autosomal depth was given, \ + so it ran at depth 0 and marked no call as a possible NuMT" + ); + assert!( + stdout.contains("\n numt: NuMT filter used a default depth (no median autosomal depth"), + "{stdout}" + ); + // Without the flag the table has no numt line, as before. + let (code, _, _) = run(cli_of("annotate"), &["--vcf", &vcf, "--out", &out]); + assert_eq!(code, 0); + assert!(!d.read("a.tsv").contains("# numt")); +} From 102771bceadbaca4eaabd9af904dc834c47ac3cc Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:08:15 +0100 Subject: [PATCH 2/6] fix(mito): match a chrM indel at the left end of its repeat, and name a NuMT filter without depth og mito annotate matched an indel only on its exact trimmed spelling, and looked for ClinVar's record at most one base away, so m.315 C>CC never met the m.310 T>TC record of the same poly-C insertion. When the exact spelling finds nothing, both sides are now moved to the left end of their repeat on the rCRS (embedded, GRCh38's chrM, MD5 c68f5267...) and matched there. An exact match still wins, so every table that matched before is unchanged; an allele whose REF is not the rCRS is not moved. ClinVar's chrM records are now read whole, since a record can sit anywhere in its repeat. --numt-default adds a '# numt' header line: the NuMT filter used a default depth (0) and marked nothing. Without it the table is as before. Bead: lin-35 Bead: lin-32 --- rust/crates/og-summary/src/mito.rs | 396 +++++++++++++++++++---- rust/crates/og-summary/src/mito/tests.rs | 22 +- 2 files changed, 360 insertions(+), 58 deletions(-) diff --git a/rust/crates/og-summary/src/mito.rs b/rust/crates/og-summary/src/mito.rs index d447acf9f..d0da82b04 100644 --- a/rust/crates/og-summary/src/mito.rs +++ b/rust/crates/og-summary/src/mito.rs @@ -3,8 +3,11 @@ //! - `annotate`: each ALT of the filtered chrM VCF with its ClinVar record //! (CLNSIG, CLNREVSTAT, CLNDN) and gnomAD v3.1's chrM sites (AF_hom, //! AF_het, max_hl), matched on the allele trimmed to its shortest form -//! ([`min_allele`]), so Mutect2's, ClinVar's and gnomAD's spellings of one -//! indel meet. `_chrM_annotated.tsv`. +//! ([`min_allele`]), and else on the indel moved to the left end of its +//! repeat on the rCRS ([`left_allele`]), so Mutect2's, ClinVar's and +//! gnomAD's spellings of one indel meet wherever in a homopolymer each put +//! it. `--numt-default` adds a `# numt` line: the NuMT filter had no +//! autosomal depth. `_chrM_annotated.tsv`. //! - `deletions`: eKLIPse's deletions CSV (Goudenege 2019) as a table; the //! deleted length wraps the circular origin. `_chrM_deletions.tsv`. //! - `copies`: the mtDNA copy number, 2 x (chrM reads per base) / (autosomal @@ -71,6 +74,230 @@ const CHRM_NAMES: [&str; 4] = ["chrM", "MT", "chrMT", "M"]; /// One allele as a key: (pos, ref, alt). pub type Allele = (i128, String, String); +/// The `# numt` header line of `annotate --numt-default`: the NuMT filter +/// got no median autosomal depth, ran at 0 and so marked nothing. +pub const NUMT_DEFAULT: &str = "# numt\tNuMT filter used a default depth\tno median autosomal depth \ +was given, so it ran at depth 0 and marked no call as a possible NuMT"; + +/// GRCh38's chrM, the rCRS (NC_012920.1, MD5 +/// c68f52674c9fb33aef52dcf399755519): base `p` at index `p - 1`. +const RCRS: &str = concat!( + "GATCACAGGTCTATCACCCTATTAACCACTCACGGGAGCTCTCCATGCATTTGGTATTTTCGTCTGGGGGGTATGCACGC", + "GATAGCATTGCGAGACGCTGGAGCCGGAGCACCCTATGTCGCAGTATCTGTCTTTGATTCCTGCCTCATCCTATTATTTA", + "TCGCACCTACGTTCAATATTACAGGCGAACATACTTACTAAAGTGTGTTAATTAATTAATGCTTGTAGGACATAATAATA", + "ACAATTGAATGTCTGCACAGCCACTTTCCACACAGACATCATAACAAAAAATTTCCACCAAACCCCCCCTCCCCCGCTTC", + "TGGCCACAGCACTTAAACACATCTCTGCCAAACCCCAAAAACAAAGAACCCTAACACCAGCCTAACCAGATTTCAAATTT", + "TATCTTTTGGCGGTATGCACTTTTAACAGTCACCCCCCAACTAACACATTATTTTCCCCTCCCACTCCCATACTACTAAT", + "CTCATCAATACAACCCCCGCCCATCCTACCCAGCACACACACACCGCTGCTAACCCCATACCCCGAACCAACCAAACCCC", + "AAAGACACCCCCCACAGTTTATGTAGCTTACCTCCTCAAAGCAATACACTGAAAATGTTTAGACGGGCTCACATCACCCC", + "ATAAACAAATAGGTTTGGTCCTAGCCTTTCTATTAGCTCTTAGTAAGATTACACATGCAAGCATCCCCGTTCCAGTGAGT", + "TCACCCTCTAAATCACCACGATCAAAAGGAACAAGCATCAAGCACGCAGCAATGCAGCTCAAAACGCTTAGCCTAGCCAC", + "ACCCCCACGGGAAACAGCAGTGATTAACCTTTAGCAATAAACGAAAGTTTAACTAAGCTATACTAACCCCAGGGTTGGTC", + "AATTTCGTGCCAGCCACCGCGGTCACACGATTAACCCAAGTCAATAGAAGCCGGCGTAAAGAGTGTTTTAGATCACCCCC", + "TCCCCAATAAAGCTAAAACTCACCTGAGTTGTAAAAAACTCCAGTTGACACAAAATAGACTACGAAAGTGGCTTTAACAT", + "ATCTGAACACACAATAGCTAAGACCCAAACTGGGATTAGATACCCCACTATGCTTAGCCCTAAACCTCAACAGTTAAATC", + "AACAAAACTGCTCGCCAGAACACTACGAGCCACAGCTTAAAACTCAAAGGACCTGGCGGTGCTTCATATCCCTCTAGAGG", + "AGCCTGTTCTGTAATCGATAAACCCCGATCAACCTCACCACCTCTTGCTCAGCCTATATACCGCCATCTTCAGCAAACCC", + "TGATGAAGGCTACAAAGTAAGCGCAAGTACCCACGTAAAGACGTTAGGTCAAGGTGTAGCCCATGAGGTGGCAAGAAATG", + "GGCTACATTTTCTACCCCAGAAAACTACGATAGCCCTTATGAAACTTAAGGGTCGAAGGTGGATTTAGCAGTAAACTAAG", + "AGTAGAGTGCTTAGTTGAACAGGGCCCTGAAGCGCGTACACACCGCCCGTCACCCTCCTCAAGTATACTTCAAAGGACAT", + "TTAACTAAAACCCCTACGCATTTATATAGAGGAGACAAGTCGTAACATGGTAAGTGTACTGGAAAGTGCACTTGGACGAA", + "CCAGAGTGTAGCTTAACACAAAGCACCCAACTTACACTTAGGAGATTTCAACTTAACTTGACCGCTCTGAGCTAAACCTA", + "GCCCCAAACCCACTCCACCTTACTACCAGACAACCTTAGCCAAACCATTTACCCAAATAAAGTATAGGCGATAGAAATTG", + "AAACCTGGCGCAATAGATATAGTACCGCAAGGGAAAGATGAAAAATTATAACCAAGCATAATATAGCAAGGACTAACCCC", + "TATACCTTCTGCATAATGAATTAACTAGAAATAACTTTGCAAGGAGAGCCAAAGCTAAGACCCCCGAAACCAGACGAGCT", + "ACCTAAGAACAGCTAAAAGAGCACACCCGTCTATGTAGCAAAATAGTGGGAAGATTTATAGGTAGAGGCGACAAACCTAC", + "CGAGCCTGGTGATAGCTGGTTGTCCAAGATAGAATCTTAGTTCAACTTTAAATTTGCCCACAGAACCCTCTAAATCCCCT", + "TGTAAATTTAACTGTTAGTCCAAAGAGGAACAGCTCTTTGGACACTAGGAAAAAACCTTGTAGAGAGAGTAAAAAATTTA", + "ACACCCATAGTAGGCCTAAAAGCAGCCACCAATTAAGAAAGCGTTCAAGCTCAACACCCACTACCTAAAAAATCCCAAAC", + "ATATAACTGAACTCCTCACACCCAATTGGACCAATCTATCACCCTATAGAAGAACTAATGTTAGTATAAGTAACATGAAA", + "ACATTCTCCTCCGCATAAGCCTGCGTCAGATTAAAACACTGAACTGACAATTAACAGCCCAATATCTACAATCAACCAAC", + "AAGTCATTATTACCCTCACTGTCAACCCAACACAGGCATGCTCATAAGGAAAGGTTAAAAAAAGTAAAAGGAACTCGGCA", + "AATCTTACCCCGCCTGTTTACCAAAAACATCACCTCTAGCATCACCAGTATTAGAGGCACCGCCTGCCCAGTGACACATG", + "TTTAACGGCCGCGGTACCCTAACCGTGCAAAGGTAGCATAATCACTTGTTCCTTAAATAGGGACCTGTATGAATGGCTCC", + "ACGAGGGTTCAGCTGTCTCTTACTTTTAACCAGTGAAATTGACCTGCCCGTGAAGAGGCGGGCATAACACAGCAAGACGA", + "GAAGACCCTATGGAGCTTTAATTTATTAATGCAAACAGTACCTAACAAACCCACAGGTCCTAAACTACCAAACCTGCATT", + "AAAAATTTCGGTTGGGGCGACCTCGGAGCAGAACCCAACCTCCGAGCAGTACATGCTAAGACTTCACCAGTCAAAGCGAA", + "CTACTATACTCAATTGATCCAATAACTTGACCAACGGAACAAGTTACCCTAGGGATAACAGCGCAATCCTATTCTAGAGT", + "CCATATCAACAATAGGGTTTACGACCTCGATGTTGGATCAGGACATCCCGATGGTGCAGCCGCTATTAAAGGTTCGTTTG", + "TTCAACGATTAAAGTCCTACGTGATCTGAGTTCAGACCGGAGTAATCCAGGTCGGTTTCTATCTACNTTCAAATTCCTCC", + "CTGTACGAAAGGACAAGAGAAATAAGGCCTACTTCACAAAGCGCCTTCCCCCGTAAATGATATCATCTCAACTTAGTATT", + "ATACCCACACCCACCCAAGAACAGGGTTTGTTAAGATGGCAGAGCCCGGTAATCGCATAAAACTTAAAACTTTACAGTCA", + "GAGGTTCAATTCCTCTTCTTAACAACATACCCATGGCCAACCTCCTACTCCTCATTGTACCCATTCTAATCGCAATGGCA", + "TTCCTAATGCTTACCGAACGAAAAATTCTAGGCTATATACAACTACGCAAAGGCCCCAACGTTGTAGGCCCCTACGGGCT", + "ACTACAACCCTTCGCTGACGCCATAAAACTCTTCACCAAAGAGCCCCTAAAACCCGCCACATCTACCATCACCCTCTACA", + "TCACCGCCCCGACCTTAGCTCTCACCATCGCTCTTCTACTATGAACCCCCCTCCCCATACCCAACCCCCTGGTCAACCTC", + "AACCTAGGCCTCCTATTTATTCTAGCCACCTCTAGCCTAGCCGTTTACTCAATCCTCTGATCAGGGTGAGCATCAAACTC", + "AAACTACGCCCTGATCGGCGCACTGCGAGCAGTAGCCCAAACAATCTCATATGAAGTCACCCTAGCCATCATTCTACTAT", + "CAACATTACTAATAAGTGGCTCCTTTAACCTCTCCACCCTTATCACAACACAAGAACACCTCTGATTACTCCTGCCATCA", + "TGACCCTTGGCCATAATATGATTTATCTCCACACTAGCAGAGACCAACCGAACCCCCTTCGACCTTGCCGAAGGGGAGTC", + "CGAACTAGTCTCAGGCTTCAACATCGAATACGCCGCAGGCCCCTTCGCCCTATTCTTCATAGCCGAATACACAAACATTA", + "TTATAATAAACACCCTCACCACTACAATCTTCCTAGGAACAACATATGACGCACTCTCCCCTGAACTCTACACAACATAT", + "TTTGTCACCAAGACCCTACTTCTAACCTCCCTGTTCTTATGAATTCGAACAGCATACCCCCGATTCCGCTACGACCAACT", + "CATACACCTCCTATGAAAAAACTTCCTACCACTCACCCTAGCATTACTTATATGATATGTCTCCATACCCATTACAATCT", + "CCAGCATTCCCCCTCAAACCTAAGAAATATGTCTGATAAAAGAGTTACTTTGATAGAGTAAATAATAGGAGCTTAAACCC", + "CCTTATTTCTAGGACTATGAGAATCGAACCCATCCCTGAGAATCCAAAATTCTCCGTGCCACCTATCACACCCCATCCTA", + "AAGTAAGGTCAGCTAAATAAGCTATCGGGCCCATACCCCGAAAATGTTGGTTATACCCTTCCCGTACTAATTAATCCCCT", + "GGCCCAACCCGTCATCTACTCTACCATCTTTGCAGGCACACTCATCACAGCGCTAAGCTCGCACTGATTTTTTACCTGAG", + "TAGGCCTAGAAATAAACATGCTAGCTTTTATTCCAGTTCTAACCAAAAAAATAAACCCTCGTTCCACAGAAGCTGCCATC", + "AAGTATTTCCTCACGCAAGCAACCGCATCCATAATCCTTCTAATAGCTATCCTCTTCAACAATATACTCTCCGGACAATG", + "AACCATAACCAATACTACCAATCAATACTCATCATTAATAATCATAATAGCTATAGCAATAAAACTAGGAATAGCCCCCT", + "TTCACTTCTGAGTCCCAGAGGTTACCCAAGGCACCCCTCTGACATCCGGCCTGCTTCTTCTCACATGACAAAAACTAGCC", + "CCCATCTCAATCATATACCAAATCTCTCCCTCACTAAACGTAAGCCTTCTCCTCACTCTCTCAATCTTATCCATCATAGC", + "AGGCAGTTGAGGTGGATTAAACCAAACCCAGCTACGCAAAATCTTAGCATACTCCTCAATTACCCACATAGGATGAATAA", + "TAGCAGTTCTACCGTACAACCCTAACATAACCATTCTTAATTTAACTATTTATATTATCCTAACTACTACCGCATTCCTA", + "CTACTCAACTTAAACTCCAGCACCACGACCCTACTACTATCTCGCACCTGAAACAAGCTAACATGACTAACACCCTTAAT", + "TCCATCCACCCTCCTCTCCCTAGGAGGCCTGCCCCCGCTAACCGGCTTTTTGCCCAAATGGGCCATTATCGAAGAATTCA", + "CAAAAAACAATAGCCTCATCATCCCCACCATCATAGCCACCATCACCCTCCTTAACCTCTACTTCTACCTACGCCTAATC", + "TACTCCACCTCAATCACACTACTCCCCATATCTAACAACGTAAAAATAAAATGACAGTTTGAACATACAAAACCCACCCC", + "ATTCCTCCCCACACTCATCGCCCTTACCACGCTACTCCTACCTATCTCCCCTTTTATACTAATAATCTTATAGAAATTTA", + "GGTTAAATACAGACCAAGAGCCTTCAAAGCCCTCAGTAAGTTGCAATACTTAATTTCTGTAACAGCTAAGGACTGCAAAA", + "CCCCACTCTGCATCAACTGAACGCAAATCAGCCACTTTAATTAAGCTAAGCCCTTACTAGACCAATGGGACTTAAACCCA", + "CAAACACTTAGTTAACAGCTAAGCACCCTAATCAACTGGCTTCAATCTACTTCTCCCGCCGCCGGGAAAAAAGGCGGGAG", + "AAGCCCCGGCAGGTTTGAAGCTGCTTCTTCGAATTTGCAATTCAATATGAAAATCACCTCGGAGCTGGTAAAAAGAGGCC", + "TAACCCCTGTCTTTAGATTTACAGTCCAATGCTTCACTCAGCCATTTTACCTCACCCCCACTGATGTTCGCCGACCGTTG", + "ACTATTCTCTACAAACCACAAAGACATTGGAACACTATACCTATTATTCGGCGCATGAGCTGGAGTCCTAGGCACAGCTC", + "TAAGCCTCCTTATTCGAGCCGAGCTGGGCCAGCCAGGCAACCTTCTAGGTAACGACCACATCTACAACGTTATCGTCACA", + "GCCCATGCATTTGTAATAATCTTCTTCATAGTAATACCCATCATAATCGGAGGCTTTGGCAACTGACTAGTTCCCCTAAT", + "AATCGGTGCCCCCGATATGGCGTTTCCCCGCATAAACAACATAAGCTTCTGACTCTTACCTCCCTCTCTCCTACTCCTGC", + "TCGCATCTGCTATAGTGGAGGCCGGAGCAGGAACAGGTTGAACAGTCTACCCTCCCTTAGCAGGGAACTACTCCCACCCT", + "GGAGCCTCCGTAGACCTAACCATCTTCTCCTTACACCTAGCAGGTGTCTCCTCTATCTTAGGGGCCATCAATTTCATCAC", + "AACAATTATCAATATAAAACCCCCTGCCATAACCCAATACCAAACGCCCCTCTTCGTCTGATCCGTCCTAATCACAGCAG", + "TCCTACTTCTCCTATCTCTCCCAGTCCTAGCTGCTGGCATCACTATACTACTAACAGACCGCAACCTCAACACCACCTTC", + "TTCGACCCCGCCGGAGGAGGAGACCCCATTCTATACCAACACCTATTCTGATTTTTCGGTCACCCTGAAGTTTATATTCT", + "TATCCTACCAGGCTTCGGAATAATCTCCCATATTGTAACTTACTACTCCGGAAAAAAAGAACCATTTGGATACATAGGTA", + "TGGTCTGAGCTATGATATCAATTGGCTTCCTAGGGTTTATCGTGTGAGCACACCATATATTTACAGTAGGAATAGACGTA", + "GACACACGAGCATATTTCACCTCCGCTACCATAATCATCGCTATCCCCACCGGCGTCAAAGTATTTAGCTGACTCGCCAC", + "ACTCCACGGAAGCAATATGAAATGATCTGCTGCAGTGCTCTGAGCCCTAGGATTCATCTTTCTTTTCACCGTAGGTGGCC", + "TGACTGGCATTGTATTAGCAAACTCATCACTAGACATCGTACTACACGACACGTACTACGTTGTAGCCCACTTCCACTAT", + "GTCCTATCAATAGGAGCTGTATTTGCCATCATAGGAGGCTTCATTCACTGATTTCCCCTATTCTCAGGCTACACCCTAGA", + "CCAAACCTACGCCAAAATCCATTTCACTATCATATTCATCGGCGTAAATCTAACTTTCTTCCCACAACACTTTCTCGGCC", + "TATCCGGAATGCCCCGACGTTACTCGGACTACCCCGATGCATACACCACATGAAACATCCTATCATCTGTAGGCTCATTC", + "ATTTCTCTAACAGCAGTAATATTAATAATTTTCATGATTTGAGAAGCCTTCGCTTCGAAGCGAAAAGTCCTAATAGTAGA", + "AGAACCCTCCATAAACCTGGAGTGACTATATGGATGCCCCCCACCCTACCACACATTCGAAGAACCCGTATACATAAAAT", + "CTAGACAAAAAAGGAAGGAATCGAACCCCCCAAAGCTGGTTTCAAGCCAACCCCATGGCCTCCATGACTTTTTCAAAAAG", + "GTATTAGAAAAACCATTTCATAACTTTGTCAAAGTTAAATTATAGGCTAAATCCTATATATCTTAATGGCACATGCAGCG", + "CAAGTAGGTCTACAAGACGCTACTTCCCCTATCATAGAAGAGCTTATCACCTTTCATGATCACGCCCTCATAATCATTTT", + "CCTTATCTGCTTCCTAGTCCTGTATGCCCTTTTCCTAACACTCACAACAAAACTAACTAATACTAACATCTCAGACGCTC", + "AGGAAATAGAAACCGTCTGAACTATCCTGCCCGCCATCATCCTAGTCCTCATCGCCCTCCCATCCCTACGCATCCTTTAC", + "ATAACAGACGAGGTCAACGATCCCTCCCTTACCATCAAATCAATTGGCCACCAATGGTACTGAACCTACGAGTACACCGA", + "CTACGGCGGACTAATCTTCAACTCCTACATACTTCCCCCATTATTCCTAGAACCAGGCGACCTGCGACTCCTTGACGTTG", + "ACAATCGAGTAGTACTCCCGATTGAAGCCCCCATTCGTATAATAATTACATCACAAGACGTCTTGCACTCATGAGCTGTC", + "CCCACATTAGGCTTAAAAACAGATGCAATTCCCGGACGTCTAAACCAAACCACTTTCACCGCTACACGACCGGGGGTATA", + "CTACGGTCAATGCTCTGAAATCTGTGGAGCAAACCACAGTTTCATGCCCATCGTCCTAGAATTAATTCCCCTAAAAATCT", + "TTGAAATAGGGCCCGTATTTACCCTATAGCACCCCCTCTACCCCCTCTAGAGCCCACTGTAAAGCTAACTTAGCATTAAC", + "CTTTTAAGTTAAAGATTAAGAGAACCAACACCTCTTTACAGTGAAATGCCCCAACTAAATACTACCGTATGGCCCACCAT", + "AATTACCCCCATACTCCTTACACTATTCCTCATCACCCAACTAAAAATATTAAACACAAACTACCACCTACCTCCCTCAC", + "CAAAGCCCATAAAAATAAAAAATTATAACAAACCCTGAGAACCAAAATGAACGAAAATCTGTTCGCTTCATTCATTGCCC", + "CCACAATCCTAGGCCTACCCGCCGCAGTACTGATCATTCTATTTCCCCCTCTATTGATCCCCACCTCCAAATATCTCATC", + "AACAACCGACTAATCACCACCCAACAATGACTAATCAAACTAACCTCAAAACAAATGATAACCATACACAACACTAAAGG", + "ACGAACCTGATCTCTTATACTAGTATCCTTAATCATTTTTATTGCCACAACTAACCTCCTCGGACTCCTGCCTCACTCAT", + "TTACACCAACCACCCAACTATCTATAAACCTAGCCATGGCCATCCCCTTATGAGCGGGCACAGTGATTATAGGCTTTCGC", + "TCTAAGATTAAAAATGCCCTAGCCCACTTCTTACCACAAGGCACACCTACACCCCTTATCCCCATACTAGTTATTATCGA", + "AACCATCAGCCTACTCATTCAACCAATAGCCCTGGCCGTACGCCTAACCGCTAACATTACTGCAGGCCACCTACTCATGC", + "ACCTAATTGGAAGCGCCACCCTAGCAATATCAACCATTAACCTTCCCTCTACACTTATCATCTTCACAATTCTAATTCTA", + "CTGACTATCCTAGAAATCGCTGTCGCCTTAATCCAAGCCTACGTTTTCACACTTCTAGTAAGCCTCTACCTGCACGACAA", + "CACATAATGACCCACCAATCACATGCCTATCATATAGTAAAACCCAGCCCATGACCCCTAACAGGGGCCCTCTCAGCCCT", + "CCTAATGACCTCCGGCCTAGCCATGTGATTTCACTTCCACTCCATAACGCTCCTCATACTAGGCCTACTAACCAACACAC", + "TAACCATATACCAATGATGGCGCGATGTAACACGAGAAAGCACATACCAAGGCCACCACACACCACCTGTCCAAAAAGGC", + "CTTCGATACGGGATAATCCTATTTATTACCTCAGAAGTTTTTTTCTTCGCAGGATTTTTCTGAGCCTTTTACCACTCCAG", + "CCTAGCCCCTACCCCCCAATTAGGAGGGCACTGGCCCCCAACAGGCATCACCCCGCTAAATCCCCTAGAAGTCCCACTCC", + "TAAACACATCCGTATTACTCGCATCAGGAGTATCAATCACCTGAGCTCACCATAGTCTAATAGAAAACAACCGAAACCAA", + "ATAATTCAAGCACTGCTTATTACAATTTTACTGGGTCTCTATTTTACCCTCCTACAAGCCTCAGAGTACTTCGAGTCTCC", + "CTTCACCATTTCCGACGGCATCTACGGCTCAACATTTTTTGTAGCCACAGGCTTCCACGGACTTCACGTCATTATTGGCT", + "CAACTTTCCTCACTATCTGCTTCATCCGCCAACTAATATTTCACTTTACATCCAAACATCACTTTGGCTTCGAAGCCGCC", + "GCCTGATACTGGCATTTTGTAGATGTGGTTTGACTATTTCTGTATGTCTCCATCTATTGATGAGGGTCTTACTCTTTTAG", + "TATAAATAGTACCGTTAACTTCCAATTAACTAGTTTTGACAACATTCAAAAAAGAGTAATAAACTTCGCCTTAATTTTAA", + "TAATCAACACCCTCCTAGCCTTACTACTAATAATTATTACATTTTGACTACCACAACTCAACGGCTACATAGAAAAATCC", + "ACCCCTTACGAGTGCGGCTTCGACCCTATATCCCCCGCCCGCGTCCCTTTCTCCATAAAATTCTTCTTAGTAGCTATTAC", + "CTTCTTATTATTTGATCTAGAAATTGCCCTCCTTTTACCCCTACCATGAGCCCTACAAACAACTAACCTGCCACTAATAG", + "TTATGTCATCCCTCTTATTAATCATCATCCTAGCCCTAAGTCTGGCCTATGAGTGACTACAAAAAGGATTAGACTGAACC", + "GAATTGGTATATAGTTTAAACAAAACGAATGATTTCGACTCATTAAATTATGATAATCATATTTACCAAATGCCCCTCAT", + "TTACATAAATATTATACTAGCATTTACCATCTCACTTCTAGGAATACTAGTATATCGCTCACACCTCATATCCTCCCTAC", + "TATGCCTAGAAGGAATAATACTATCGCTGTTCATTATAGCTACTCTCATAACCCTCAACACCCACTCCCTCTTAGCCAAT", + "ATTGTGCCTATTGCCATACTAGTCTTTGCCGCCTGCGAAGCAGCGGTGGGCCTAGCCCTACTAGTCTCAATCTCCAACAC", + "ATATGGCCTAGACTACGTACATAACCTAAACCTACTCCAATGCTAAAACTAATCGTCCCAACAATTATATTACTACCACT", + "GACATGACTTTCCAAAAAACACATAATTTGAATCAACACAACCACCCACAGCCTAATTATTAGCATCATCCCTCTACTAT", + "TTTTTAACCAAATCAACAACAACCTATTTAGCTGTTCCCCAACCTTTTCCTCCGACCCCCTAACAACCCCCCTCCTAATA", + "CTAACTACCTGACTCCTACCCCTCACAATCATGGCAAGCCAACGCCACTTATCCAGTGAACCACTATCACGAAAAAAACT", + "CTACCTCTCTATACTAATCTCCCTACAAATCTCCTTAATTATAACATTCACAGCCACAGAACTAATCATATTTTATATCT", + "TCTTCGAAACCACACTTATCCCCACCTTGGCTATCATCACCCGATGAGGCAACCAGCCAGAACGCCTGAACGCAGGCACA", + "TACTTCCTATTCTACACCCTAGTAGGCTCCCTTCCCCTACTCATCGCACTAATTTACACTCACAACACCCTAGGCTCACT", + "AAACATTCTACTACTCACTCTCACTGCCCAAGAACTATCAAACTCCTGAGCCAACAACTTAATATGACTAGCTTACACAA", + "TAGCTTTTATAGTAAAGATACCTCTTTACGGACTCCACTTATGACTCCCTAAAGCCCATGTCGAAGCCCCCATCGCTGGG", + "TCAATAGTACTTGCCGCAGTACTCTTAAAACTAGGCGGCTATGGTATAATACGCCTCACACTCATTCTCAACCCCCTGAC", + "AAAACACATAGCCTACCCCTTCCTTGTACTATCCCTATGAGGCATAATTATAACAAGCTCCATCTGCCTACGACAAACAG", + "ACCTAAAATCGCTCATTGCATACTCTTCAATCAGCCACATAGCCCTCGTAGTAACAGCCATTCTCATCCAAACCCCCTGA", + "AGCTTCACCGGCGCAGTCATTCTCATAATCGCCCACGGGCTTACATCCTCATTACTATTCTGCCTAGCAAACTCAAACTA", + "CGAACGCACTCACAGTCGCATCATAATCCTCTCTCAAGGACTTCAAACTCTACTCCCACTAATAGCTTTTTGATGACTTC", + "TAGCAAGCCTCGCTAACCTCGCCTTACCCCCCACTATTAACCTACTGGGAGAACTCTCTGTGCTAGTAACCACGTTCTCC", + "TGATCAAATATCACTCTCCTACTTACAGGACTCAACATACTAGTCACAGCCCTATACTCCCTCTACATATTTACCACAAC", + "ACAATGGGGCTCACTCACCCACCACATTAACAACATAAAACCCTCATTCACACGAGAAAACACCCTCATGTTCATACACC", + "TATCCCCCATTCTCCTCCTATCCCTCAACCCCGACATCATTACCGGGTTTTCCTCTTGTAAATATAGTTTAACCAAAACA", + "TCAGATTGTGAATCTGACAACAGAGGCTTACGACCCCTTATTTACCGAGAAAGCTCACAAGAACTGCTAACTCATGCCCC", + "CATGTCTAACAACATGGCTTTCTCAACTTTTAAAGGATAACAGCTATCCATTGGTCTTAGGCCCCAAAAATTTTGGTGCA", + "ACTCCAAATAAAAGTAATAACCATGCACACTACTATAACCACCCTAACCCTGACTTCCCTAATTCCCCCCATCCTTACCA", + "CCCTCGTTAACCCTAACAAAAAAAACTCATACCCCCATTATGTAAAATCCATTGTCGCATCCACCTTTATTATCAGTCTC", + "TTCCCCACAACAATATTCATGTGCCTAGACCAAGAAGTTATTATCTCGAACTGACACTGAGCCACAACCCAAACAACCCA", + "GCTCTCCCTAAGCTTCAAACTAGACTACTTCTCCATAATATTCATCCCTGTAGCATTGTTCGTTACATGGTCCATCATAG", + "AATTCTCACTGTGATATATAAACTCAGACCCAAACATTAATCAGTTCTTCAAATATCTACTCATCTTCCTAATTACCATA", + "CTAATCTTAGTTACCGCTAACAACCTATTCCAACTGTTCATCGGCTGAGAGGGCGTAGGAATTATATCCTTCTTGCTCAT", + "CAGTTGATGATACGCCCGAGCAGATGCCAACACAGCAGCCATTCAAGCAATCCTATACAACCGTATCGGCGATATCGGTT", + "TCATCCTCGCCTTAGCATGATTTATCCTACACTCCAACTCATGAGACCCACAACAAATAGCCCTTCTAAACGCTAATCCA", + "AGCCTCACCCCACTACTAGGCCTCCTCCTAGCAGCAGCAGGCAAATCAGCCCAATTAGGTCTCCACCCCTGACTCCCCTC", + "AGCCATAGAAGGCCCCACCCCAGTCTCAGCCCTACTCCACTCAAGCACTATAGTTGTAGCAGGAATCTTCTTACTCATCC", + "GCTTCCACCCCCTAGCAGAAAATAGCCCACTAATCCAAACTCTAACACTATGCTTAGGCGCTATCACCACTCTGTTCGCA", + "GCAGTCTGCGCCCTTACACAAAATGACATCAAAAAAATCGTAGCCTTCTCCACTTCAAGTCAACTAGGACTCATAATAGT", + "TACAATCGGCATCAACCAACCACACCTAGCATTCCTGCACATCTGTACCCACGCCTTCTTCAAAGCCATACTATTTATGT", + "GCTCCGGGTCCATCATCCACAACCTTAACAATGAACAAGATATTCGAAAAATAGGAGGACTACTCAAAACCATACCTCTC", + "ACTTCAACCTCCCTCACCATTGGCAGCCTAGCATTAGCAGGAATACCTTTCCTCACAGGTTTCTACTCCAAAGACCACAT", + "CATCGAAACCGCAAACATATCATACACAAACGCCTGAGCCCTATCTATTACTCTCATCGCTACCTCCCTGACAAGCGCCT", + "ATAGCACTCGAATAATTCTTCTCACCCTAACAGGTCAACCTCGCTTCCCCACCCTTACTAACATTAACGAAAATAACCCC", + "ACCCTACTAAACCCCATTAAACGCCTGGCAGCCGGAAGCCTATTCGCAGGATTTCTCATTACTAACAACATTTCCCCCGC", + "ATCCCCCTTCCAAACAACAATCCCCCTCTACCTAAAACTCACAGCCCTCGCTGTCACTTTCCTAGGACTTCTAACAGCCC", + "TAGACCTCAACTACCTAACCAACAAACTTAAAATAAAATCCCCACTATGCACATTTTATTTCTCCAACATACTCGGATTC", + "TACCCTAGCATCACACACCGCACAATCCCCTATCTAGGCCTTCTTACGAGCCAAAACCTGCCCCTACTCCTCCTAGACCT", + "AACCTGACTAGAAAAGCTATTACCTAAAACAATTTCACAGCACCAAATCTCCACCTCCATCATCACCTCAACCCAAAAAG", + "GCATAATTAAACTTTACTTCCTCTCTTTCTTCTTCCCACTCATCCTAACCCTACTCCTAATCACATAACCTATTCCCCCG", + "AGCAATCTCAATTACAATATATACACCAACAAACAATGTTCAACCAGTAACTACTACTAATCAACGCCCATAATCATACA", + "AAGCCCCCGCACCAATAGGATCCTCCCGAATCAACCCTGACCCCTCTCCTTCATAAATTATTCAGCTTCCTACACTATTA", + "AAGTTTACCACAACCACCACCCCATCATACTCTTTCACCCACAGCACCAATCCTACCTCCATCGCTAACCCCACTAAAAC", + "ACTCACCAAGACCTCAACCCCTGACCCCCATGCCTCAGGATACTCCTCAATAGCCATCGCTGTAGTATATCCAAAGACAA", + "CCATCATTCCCCCTAAATAAATTAAAAAAACTATTAAACCCATATAACCTCCCCCAAAATTCAGAATAATAACACACCCG", + "ACCACACCGCTAACAATCAATACTAAACCCCCATAAATAGGAGAAGGCTTAGAAGAAAACCCCACAAACCCCATTACTAA", + "ACCCACACTCAACAGAAACAAAGCATACATCATTATTCTCGCACGGACTACAACCACGACCAATGATATGAAAAACCATC", + "GTTGTATTTCAACTACAAGAACACCAATGACCCCAATACGCAAAACTAACCCCCTAATAAAATTAATTAACCACTCATTC", + "ATCGACCTCCCCACCCCATCCAACATCTCCGCATGATGAAACTTCGGCTCACTCCTTGGCGCCTGCCTGATCCTCCAAAT", + "CACCACAGGACTATTCCTAGCCATGCACTACTCACCAGACGCCTCAACCGCCTTTTCATCAATCGCCCACATCACTCGAG", + "ACGTAAATTATGGCTGAATCATCCGCTACCTTCACGCCAATGGCGCCTCAATATTCTTTATCTGCCTCTTCCTACACATC", + "GGGCGAGGCCTATATTACGGATCATTTCTCTACTCAGAAACCTGAAACATCGGCATTATCCTCCTGCTTGCAACTATAGC", + "AACAGCCTTCATAGGCTATGTCCTCCCGTGAGGCCAAATATCATTCTGAGGGGCCACAGTAATTACAAACTTACTATCCG", + "CCATCCCATACATTGGGACAGACCTAGTTCAATGAATCTGAGGAGGCTACTCAGTAGACAGTCCCACCCTCACACGATTC", + "TTTACCTTTCACTTCATCTTGCCCTTCATTATTGCAGCCCTAGCAACACTCCACCTCCTATTCTTGCACGAAACGGGATC", + "AAACAACCCCCTAGGAATCACCTCCCATTCCGATAAAATCACCTTCCACCCTTACTACACAATCAAAGACGCCCTCGGCT", + "TACTTCTCTTCCTTCTCTCCTTAATGACATTAACACTATTCTCACCAGACCTCCTAGGCGACCCAGACAATTATACCCTA", + "GCCAACCCCTTAAACACCCCTCCCCACATCAAGCCCGAATGATATTTCCTATTCGCCTACACAATTCTCCGATCCGTCCC", + "TAACAAACTAGGAGGCGTCCTTGCCCTATTACTATCCATCCTCATCCTAGCAATAATCCCCATCCTCCATATATCCAAAC", + "AACAAAGCATAATATTTCGCCCACTAAGCCAATCACTTTATTGACTCCTAGCCGCAGACCTCCTCATTCTAACCTGAATC", + "GGAGGACAACCAGTAAGCTACCCTTTTACCATCATTGGACAAGTAGCATCCGTACTATACTTCACAACAATCCTAATCCT", + "AATACCAACTATCTCCCTAATTGAAAACAAAATACTCAAATGGGCCTGTCCTTGTAGTATAAACTAATACACCAGTCTTG", + "TAAACCGGAGATGAAAACCTTTTTCCAAGGACAAATCAGAGAAAAAGTCTTTAACTCCACCATTAGCACCCAAAGCTAAG", + "ATTCTAATTTAAACTATTCTCTGTTCTTTCATGGGGAAGCAGATTTGGGTACCACCCAAGTATTGACTCACCCATCAACA", + "ACCGCTATGTATTTCGTACATTACTGCCAGCCACCATGAATATTGTACGGTACCATAAATACTTGACCACCTGTAGTACA", + "TAAAAACCCAATCCACATCAAAACCCCCTCCCCATGCTTACAAGCAAGTACAGCAATCAACCCTCAACTATCACACATCA", + "ACTGCAACTCCAAAGCCACCCCTCACCCACTAGGATACCAACAAACCTACCCACCCTTAACAGTACATAGTACATAAAGC", + "CATTTACCGTACATAGCACATTACAGTCAAATCCCTTCTCGTCCCCATGGATGACCCCCCTCAGATAGGGGTCCCTTGAC", + "CACCATCCTCCGTGAAATCAATATCCCGCACAAGAGTGCTACTCTCCTCGCTCCGGGCCCATAACACTTGGGGGTAGCTA", + "AAGTGAACTGTATCCGACATCTGGTTCCTACTTCAGGGTCATAAAGCCTAAATAGCCCACACGTTCCCCTTAAATAAGAC", + "ATCACGATG", +); + +/// The rCRS base at 1-based `pos`. +fn rcrs_base(pos: i128) -> Option { + let i = usize::try_from(pos - 1).ok()?; + RCRS.as_bytes().get(i).map(|b| char::from(*b)) +} + /// `(pos, ref, alt)` without the bases ref and alt share at the end and then /// at the start, one base always kept: Mutect2's `A>AC` of a two-ALT record /// `AC>A,ACC` becomes the same key as ClinVar's and gnomAD's. @@ -88,6 +315,70 @@ pub fn min_allele(mut pos: i128, r: &str, a: &str) -> Allele { (pos, r[k..].iter().collect(), a[k..].iter().collect()) } +/// [`min_allele`] of an indel moved to the left end of its repeat on the +/// rCRS (Tan, Abecasis and Kang 2015): m.315 C>CC and m.310 T>TC, one more C +/// in m.311-315, are both `(310, "T", "TC")`. An allele of equal lengths, +/// or whose REF is not the rCRS there (another chrM), stays as +/// [`min_allele`] gives it. +pub fn left_allele(pos: i128, r: &str, a: &str) -> Allele { + let (pos, r, a) = min_allele(pos, r, a); + let on_rcrs = r.chars().enumerate().all(|(k, c)| { + i128::try_from(k) + .ok() + .and_then(|k| rcrs_base(pos + k)) + .is_some_and(|b| b.eq_ignore_ascii_case(&c)) + }); + if r.chars().count() == a.chars().count() || !on_rcrs { + return (pos, r, a); + } + let (mut p, mut r, mut a): (i128, Vec, Vec) = + (pos, r.chars().collect(), a.chars().collect()); + // Drop the shared last base; when that would empty an allele, take the + // rCRS base before it first. + while r.last().is_some() && r.last() == a.last() { + if r.len() == 1 || a.len() == 1 { + let Some(b) = rcrs_base(p - 1) else { break }; + p -= 1; + r.insert(0, b); + a.insert(0, b); + } + r.pop(); + a.pop(); + } + let (r, a): (String, String) = (r.into_iter().collect(), a.into_iter().collect()); + min_allele(p, &r, &a) +} + +/// A source's columns by allele: by its exact spelling ([`min_allele`]), +/// and by its leftmost one ([`left_allele`]) for a lookup the exact one +/// misses. The last record of a key wins. +struct ByAllele { + exact: HashMap, + left: HashMap, +} + +impl ByAllele { + fn new() -> Self { + ByAllele { + exact: HashMap::new(), + left: HashMap::new(), + } + } + + fn insert(&mut self, pos: i128, r: &str, a: &str, v: V) { + self.left.insert(left_allele(pos, r, a), v.clone()); + self.exact.insert(min_allele(pos, r, a), v); + } + + /// The exact spelling's columns, else those of the same indel spelt + /// elsewhere in its repeat. + fn get(&self, pos: i128, r: &str, a: &str) -> Option<&V> { + self.exact + .get(&min_allele(pos, r, a)) + .or_else(|| self.left.get(&left_allele(pos, r, a))) + } +} + /// `{k: v}` of a VCF INFO field; a key without `=` maps to `""`. fn info_map(field: &str) -> HashMap<&str, &str> { field @@ -129,13 +420,11 @@ type ClinVar = [String; 4]; /// gnomAD's columns for an allele: AF_hom, AF_het, max_hl. type Gnomad = [String; 3]; -/// The ClinVar records of chrM at the positions `wanted`, each read and its -/// alleles keyed in the order the Python's lookups visited them: for each -/// wanted position in order, the records starting a base before, at and a -/// base after it (ClinVar's record can start a base away from Mutect2's for -/// the same indel), the last visit winning. -fn clinvar_alleles(path: &str, wanted: &[i128]) -> Result, String> { - let mut out = HashMap::new(); +/// The ClinVar records of chrM by allele, in file order. ClinVar's record of +/// an indel can sit anywhere in its repeat, so every chrM record is read +/// (a few thousand). +fn clinvar_alleles(path: &str) -> Result, String> { + let mut out = ByAllele::new(); let mut reader = tabix::Reader::open(path).map_err(|e| format!("{}: {e}", basename(path)))?; let Some(chrom) = ["chrM", "MT"] .into_iter() @@ -143,48 +432,41 @@ fn clinvar_alleles(path: &str, wanted: &[i128]) -> Result>> = HashMap::new(); - let last = wanted.iter().copied().max().unwrap_or(0) + 1; - let end = u64::try_from(last.max(1)).unwrap_or(u64::MAX).min(1 << 29); - for line in reader.query(chrom, 0, end) { + for line in reader.query(chrom, 0, 1 << 29) { let line = line.map_err(|e| format!("{}: {e}", basename(path)))?; let line = String::from_utf8_lossy(&line).into_owned(); - let f: Vec = line.split('\t').map(str::to_owned).collect(); - if f.len() < 3 || f[1].is_empty() || !f[1].bytes().all(|b| b.is_ascii_digit()) { + let c: Vec = line.split('\t').map(str::to_owned).collect(); + if c.len() < 3 || c[1].is_empty() || !c[1].bytes().all(|b| b.is_ascii_digit()) { continue; } - let pos = int(&f[1])?; - at.entry(pos).or_default().push(f); - } - for &pos in wanted { - for q in [pos - 1, pos, pos + 1] { - if q < 1 { - continue; - } - for c in at.get(&q).into_iter().flatten() { - let i = c.get(7).map(|x| info_map(x)).unwrap_or_default(); - let g = |k: &str| i.get(k).copied().unwrap_or(".").to_owned(); - let alts = c.get(4).ok_or_else(|| { - format!("{}: a record with {} fields", basename(path), c.len()) - })?; - for alt in alts.split(',') { - out.insert( - min_allele(q, &c[3], alt), - [c[2].clone(), g("CLNSIG"), g("CLNREVSTAT"), g("CLNDN")], - ); - } - } + let q = int(&c[1])?; + if q < 1 { + continue; + } + let i = c.get(7).map(|x| info_map(x)).unwrap_or_default(); + let g = |k: &str| i.get(k).copied().unwrap_or(".").to_owned(); + let alts = c + .get(4) + .ok_or_else(|| format!("{}: a record with {} fields", basename(path), c.len()))?; + for alt in alts.split(',') { + out.insert( + q, + &c[3], + alt, + [c[2].clone(), g("CLNSIG"), g("CLNREVSTAT"), g("CLNDN")], + ); } } Ok(out) } -/// The annotation table: its two `#` header lines and its rows. +/// The annotation table: its `#` header lines (ClinVar, gnomAD, and with +/// `numt_default` the [`NUMT_DEFAULT`] line) and its rows. pub fn annotate( vcf: &str, clinvar: Option<&str>, gnomad: Option<&str>, + numt_default: bool, ) -> Result<(Vec, Vec<[String; 13]>), String> { let mut head = Vec::new(); let clinvar = match clinvar { @@ -202,7 +484,7 @@ pub fn annotate( None } }; - let mut gn: HashMap = HashMap::new(); + let mut gn: ByAllele = ByAllele::new(); match gnomad { Some(g) if is_file(g) => { head.push(format!("# gnomad\t{}\t-", basename(g))); @@ -214,30 +496,24 @@ pub fn annotate( let v = |k: &str| i.get(k).copied().unwrap_or(".").to_owned(); let pos = int(&r[1])?; for alt in r[4].split(',') { - gn.insert( - min_allele(pos, &r[3], alt), - [v("AF_hom"), v("AF_het"), v("max_hl")], - ); + gn.insert(pos, &r[3], alt, [v("AF_hom"), v("AF_het"), v("max_hl")]); } } } Some(g) => head.push(format!("# gnomad\tnot annotated\tno {g}")), None => head.push("# gnomad\tnot annotated\tno gnomAD chrM file given".into()), } + if numt_default { + head.push(NUMT_DEFAULT.to_owned()); + } let recs = vcf_records(vcf)?; - let mut cv = HashMap::new(); + let mut cv = ByAllele::new(); if let Some(c) = clinvar { - let mut wanted = Vec::new(); + // A call with a bad POS stops the run before ClinVar is read. for r in recs.iter().filter(|r| r.len() >= 5) { - let pos = int(&r[1])?; - wanted.push(pos); - for a in r[4].split(',') { - wanted.push(min_allele(pos, &r[3], a).0); - } + int(&r[1])?; } - wanted.sort_unstable(); - wanted.dedup(); - cv = clinvar_alleles(c, &wanted)?; + cv = clinvar_alleles(c)?; } let dot = || ".".to_owned(); let mut rows = Vec::new(); @@ -255,11 +531,11 @@ pub fn annotate( for (k, alt) in r[4].split(',').enumerate() { let key = min_allele(pos, &r[3], alt); let [cid, sig, rev, dn] = cv - .get(&key) + .get(pos, &r[3], alt) .cloned() .unwrap_or_else(|| [dot(), dot(), dot(), dot()]); let [hom, het, hl] = gn - .get(&key) + .get(pos, &r[3], alt) .cloned() .unwrap_or_else(|| [dot(), dot(), dot()]); rows.push([ @@ -537,7 +813,7 @@ pub fn copies(idxstats: &str) -> Result { } const ANNOTATE_USAGE: &str = - "usage: og mito annotate --vcf VCF [--clinvar VCF] [--gnomad VCF] --out F\n"; + "usage: og mito annotate --vcf VCF [--clinvar VCF] [--gnomad VCF] [--numt-default] --out F\n"; const DELETIONS_USAGE: &str = "usage: og mito deletions --eklipse CSV --out F\n"; const COPIES_USAGE: &str = "usage: og mito copies --idxstats F --out F\n"; @@ -550,6 +826,7 @@ pub fn cli(verb: Option<&str>, argv: &[String], out: &mut impl Write, err: &mut Opt("--vcf", Kind::Value, true), Opt("--clinvar", Kind::Value, false), Opt("--gnomad", Kind::Value, false), + Opt("--numt-default", Kind::Flag, false), Opt("--out", Kind::Value, true), ], ), @@ -591,7 +868,12 @@ pub fn cli(verb: Option<&str>, argv: &[String], out: &mut impl Write, err: &mut match verb { "annotate" => { let some = |k: &str| a.get(k).filter(|v| !v.is_empty()); - let (head, rows) = annotate(a.text("--vcf"), some("--clinvar"), some("--gnomad"))?; + let (head, rows) = annotate( + a.text("--vcf"), + some("--clinvar"), + some("--gnomad"), + a.flag("--numt-default"), + )?; let mut body = String::new(); for h in &head { body.push_str(h); diff --git a/rust/crates/og-summary/src/mito/tests.rs b/rust/crates/og-summary/src/mito/tests.rs index 6c41f95e5..5922fc9f3 100644 --- a/rust/crates/og-summary/src/mito/tests.rs +++ b/rust/crates/og-summary/src/mito/tests.rs @@ -15,6 +15,26 @@ fn min_allele_trims_the_shared_bases() { assert_eq!(k(10, "AA", "AA"), (10, "A".into(), "A".into())); } +#[test] +fn left_allele_moves_an_indel_to_the_left_end_of_its_repeat() { + let k = |p, r: &str, a: &str| left_allele(p, r, a); + let al = |p, r: &str, a: &str| (p, r.to_owned(), a.to_owned()); + // One more C in m.311-315, spelt three ways. + assert_eq!(k(315, "C", "CC"), al(310, "T", "TC")); + assert_eq!(k(312, "CC", "CCC"), al(310, "T", "TC")); + assert_eq!(k(310, "T", "TC"), al(310, "T", "TC")); + // One C fewer in m.303-309, and one more in m.16190-16193. + assert_eq!(k(308, "CC", "C"), al(302, "AC", "A")); + assert_eq!(k(16193, "C", "CC"), al(16189, "T", "TC")); + // One CA fewer in the m.514-523 CA repeat. + assert_eq!(k(521, "ACA", "A"), al(513, "GCA", "G")); + // An SNV, a REF that is not the rCRS, and nothing before m.1 stay. + assert_eq!(k(3243, "A", "G"), al(3243, "A", "G")); + assert_eq!(k(315, "G", "GG"), al(315, "G", "GG")); + assert_eq!(k(1, "G", "GG"), al(1, "G", "GG")); + assert_eq!(k(16570, "A", "AA"), al(16570, "A", "AA")); +} + fn idxstats(chrm: u64, per_chrom: u64, m: &str) -> String { let mut s: String = (1..=22) .map(|i| format!("chr{i}\t50000000\t{per_chrom}\t0\n")) @@ -181,7 +201,7 @@ fn annotate_without_clinvar() { let d = Dir::new("mito-annot"); let vcf = d.put_gz("S_chrM_filtered.vcf.gz", MITO_VCF); let gn = d.put("gnomad.vcf", GNOMAD); - let (head, rows) = annotate(&vcf, Some(&d.path("cv.vcf.gz")), Some(&gn)).unwrap(); + let (head, rows) = annotate(&vcf, Some(&d.path("cv.vcf.gz")), Some(&gn), false).unwrap(); assert_eq!( head, [ From df8550be15d4bc517253548edf5c56c316f33e02 Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:08:15 +0100 Subject: [PATCH 3/6] fix(mito): warn and flag the table when the NuMT filter has no autosomal depth Step 20 already warned; it now passes --numt-default to og mito annotate when neither mosdepth output nor AUTOSOMAL_COVERAGE gives a depth. The Nextflow MITO_VARIANTS prints its message as a WARNING on stderr, and MITO_VARIANTS_ANNOTATE passes --numt-default when mosdepth is not in --tools, the one case where MITO_VARIANTS has no depth files. The fake-docker case checks both paths and fails on main. Bead: lin-32 --- modules/local/mito_variants/main.nf | 13 +++++++---- scripts/20-mtoolbox.sh | 8 +++++-- tests/fake-docker/bash-step-somatic-mito.sh | 24 ++++++++++++++++++--- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/modules/local/mito_variants/main.nf b/modules/local/mito_variants/main.nf index ffc8b84e6..46dcd2fcc 100644 --- a/modules/local/mito_variants/main.nf +++ b/modules/local/mito_variants/main.nf @@ -14,7 +14,8 @@ the median autosomal depth, read from MOSDEPTH's summary and global distribution with the awk of scripts/20-mtoolbox.sh. Without mosdepth in --tools the depth is 0 and the filter marks nothing, as - step 20 does without step 16b's output. + step 20 does without step 16b's output: the task warns, and + MITO_VARIANTS_ANNOTATE writes the '# numt' line that says so. MITO_VARIANTS_ANNOTATE, after it, annotates each call with its ClinVar record and gnomAD v3.1's chrM sites (og mito annotate), as step 20's @@ -93,7 +94,7 @@ process MITO_VARIANTS { echo "Median autosomal coverage: \${AUTOSOMAL_COVERAGE} (from \${DEPTH_FILES})" else AUTOSOMAL_COVERAGE=0 - echo "mosdepth is not in --tools: NuMTFilterTool runs at depth 0 and marks nothing" + echo "WARNING: mosdepth is not in --tools: NuMTFilterTool runs at the default depth 0 and marks nothing" >&2 fi gatk NuMTFilterTool \\ -R ${reference} \\ @@ -133,7 +134,9 @@ process MITO_VARIANTS { it) and gnomAD v3.1's chrM sites (AF_hom, AF_het, max_hl, from --gnomad_mito, the file GNOMAD_MITO_URL in versions.env names), into _chrM_annotated.tsv. A file not given is named in the table's header - and its columns are '.', as step 20 does without it. + and its columns are '.', as step 20 does without it. Without mosdepth in + --tools MITO_VARIANTS had no autosomal depth for the NuMT filter, so the + table gets the '# numt' line (--numt-default), as step 20 writes it. Equivalent to: stage 6 of scripts/20-mtoolbox.sh */ @@ -159,8 +162,10 @@ process MITO_VARIANTS_ANNOTATE { script: def cv = clinvar ? "--clinvar ${clinvar}" : '' def gn = gnomad ? "--gnomad ${gnomad}" : '' + // MITO_VARIANTS gets mosdepth's output exactly when mosdepth is in --tools. + def numt = (params.tools ?: '').toString().split(',').collect { it.trim() }.contains('mosdepth') ? '' : '--numt-default' """ - og mito annotate --vcf ${vcf} ${cv} ${gn} --out ${meta.id}_chrM_annotated.tsv + og mito annotate --vcf ${vcf} ${cv} ${gn} ${numt} --out ${meta.id}_chrM_annotated.tsv printf '"%s":\\n python: %s\\n' "${task.process}" "${task.container.replaceFirst(/^[^:@]+[:@]/, '')}" > versions.yml """ diff --git a/scripts/20-mtoolbox.sh b/scripts/20-mtoolbox.sh index b4baa2cf4..f41cb050d 100755 --- a/scripts/20-mtoolbox.sh +++ b/scripts/20-mtoolbox.sh @@ -11,7 +11,8 @@ # the autosomes. GATK's NuMTFilterTool marks an allele as possible_numt when # its depth is what such copies could explain at the sample's median autosomal # coverage. That median is read from step 16b's mosdepth output, or taken from -# AUTOSOMAL_COVERAGE; without either the filter marks nothing. +# AUTOSOMAL_COVERAGE; without either the filter runs at depth 0 and marks +# nothing: the step warns, and the annotation table's '# numt' line says so. # # Last, each call is annotated with its ClinVar record (clinvar/clinvar_chr.vcf.gz, # which setup.sh installs) and with gnomAD v3.1's chrM sites (GNOMAD_MITO_URL in @@ -99,6 +100,7 @@ echo "[5/8] Marking possible NuMTs..." # Median autosomal depth: the depth at which half of the chr1-22 bases are # covered at least that deep, from mosdepth's per-chromosome distribution. MOSDEPTH_PREFIX="${SAMPLE_DIR}/mosdepth/${SAMPLE}.mosdepth" +NUMT_DEFAULT= if [ -n "${AUTOSOMAL_COVERAGE:-}" ]; then echo " Median autosomal coverage: ${AUTOSOMAL_COVERAGE} (AUTOSOMAL_COVERAGE)" elif [ -s "${MOSDEPTH_PREFIX}.summary.txt" ] && [ -s "${MOSDEPTH_PREFIX}.global.dist.txt" ]; then @@ -114,8 +116,9 @@ elif [ -s "${MOSDEPTH_PREFIX}.summary.txt" ] && [ -s "${MOSDEPTH_PREFIX}.global. echo " Median autosomal coverage: ${AUTOSOMAL_COVERAGE} (from ${MOSDEPTH_PREFIX}.global.dist.txt)" else AUTOSOMAL_COVERAGE=0 + NUMT_DEFAULT=--numt-default echo " WARNING: no mosdepth output (run scripts/16b-mosdepth.sh first) and no AUTOSOMAL_COVERAGE:" - echo " the NuMT filter runs with coverage 0 and marks nothing." + echo " the NuMT filter runs with the default depth 0 and marks nothing; the annotation table says so." fi run_in --cpus 2 --memory 4g \ "$GATK_IMAGE" \ @@ -139,6 +142,7 @@ run_in --cpus 1 --memory 2g -v "${PGP_ROOT}/bin:/pgp-bin:ro" "$PYTHON_IMAGE" \ --vcf "/genome/${SAMPLE}/mito/${SAMPLE}_chrM_filtered.vcf.gz" \ --clinvar "$(cpath "$CLINVAR_VCF")" \ --gnomad "$(cpath "$GNOMAD_MITO")" \ + ${NUMT_DEFAULT:+"$NUMT_DEFAULT"} \ --out "/genome/${SAMPLE}/mito/${SAMPLE}_chrM_annotated.tsv" echo "[7/8] mtDNA copy number (chrM against autosomal reads, from the BAM index)..." diff --git a/tests/fake-docker/bash-step-somatic-mito.sh b/tests/fake-docker/bash-step-somatic-mito.sh index af177e672..f23363af8 100755 --- a/tests/fake-docker/bash-step-somatic-mito.sh +++ b/tests/fake-docker/bash-step-somatic-mito.sh @@ -8,7 +8,9 @@ # autosomal depth it reads from step 16b's mosdepth output. The Nextflow # MITO_VARIANTS runs the same filter on the same mosdepth files: its script # block, run here with a fake gatk, passes the same depth (30), and 0 when the -# workflow gives it no mosdepth files. The workflow joins MOSDEPTH's output +# workflow gives it no mosdepth files. At depth 0 the filter marks nothing: +# step 20 and the module warn, and the annotation table gets the '# numt' +# line (og mito annotate --numt-default). The workflow joins MOSDEPTH's output # in, and the module stops when mosdepth ran and the join gave it nothing # (the stub runs in nextflow.yml go through that check). # shellcheck source=../../scripts/ci/fake-docker/lib.sh @@ -95,7 +97,9 @@ docker_log_has '^run image=[^ ]*gatk.* Mutect2 ' "step 29 reused a result withou : > "$FAKE_DOCKER_LOG" run_expect 0 mito-no-depth "${SCRIPTS}/20-mtoolbox.sh" sample1 docker_log_has '^run image=[^ ]*gatk.* NuMTFilterTool .*--autosomal-coverage 0 ' "step 20 did not run NuMTFilterTool" -output_has mito-no-depth 'no mosdepth output' +output_has mito-no-depth 'WARNING: no mosdepth output' +docker_log_has '^run image=[^ ]*python.* /pgp-bin/og mito annotate .*--numt-default --out ' \ + "step 20 without a depth did not tell og mito annotate the NuMT filter used the default depth" M="${GENOME_DIR}/sample1/mosdepth" mkdir -p "$M" printf 'chrom\tlength\tbases\tmean\tmin\tmax\nchr1\t1000\t30000\t30.00\t0\t60\nchr2\t1000\t30000\t30.00\t0\t60\nchrM\t16569\t1\t1000.00\t0\t2000\ntotal\t2000\t60000\t30.00\t0\t60\n' \ @@ -113,6 +117,12 @@ run_expect 0 mito "${SCRIPTS}/20-mtoolbox.sh" sample1 docker_log_has '^run image=[^ ]*gatk.* NuMTFilterTool .*--autosomal-coverage 30 ' "step 20 did not pass the median autosomal depth (30)" docker_log_has '^run image=[^ ]*gatk.* NuMTFilterTool .*-O /genome/sample1/mito/sample1_chrM_filtered\.vcf\.gz' \ "NuMTFilterTool does not write the file the reports read" +docker_log_has '^run image=[^ ]*python.* /pgp-bin/og mito annotate ' "step 20 did not annotate its calls" +if grep -q -- '--numt-default' "$FAKE_DOCKER_LOG"; then fail "step 20 with a measured depth still said the NuMT filter used the default"; fi +: > "$FAKE_DOCKER_LOG" +AUTOSOMAL_COVERAGE=25 run_expect 0 mito-given-depth "${SCRIPTS}/20-mtoolbox.sh" sample1 +docker_log_has '^run image=[^ ]*gatk.* NuMTFilterTool .*--autosomal-coverage 25 ' "step 20 did not pass AUTOSOMAL_COVERAGE" +if grep -q -- '--numt-default' "$FAKE_DOCKER_LOG"; then fail "step 20 with AUTOSOMAL_COVERAGE still said the NuMT filter used the default"; fi # --- MITO_VARIANTS (Nextflow) ------------------------------------------------------------ # The shell of the module's script block, with its Groovy values filled in: @@ -151,7 +161,15 @@ grep -q ' FilterMutectCalls .*-O sample1_chrM_mutect2_filtered\.vcf\.gz' "${NFW} || fail "FilterMutectCalls does not write the intermediate file NuMTFilterTool reads" run_mito_module "" grep -q -- '--autosomal-coverage 0 ' <<<"${NUMT} " || fail "MITO_VARIANTS without mosdepth files did not run the filter at depth 0: ${NUMT:-no call}" -grep -q 'mosdepth is not in --tools' "${NFW}/out.txt" || fail "MITO_VARIANTS without mosdepth files does not say why the depth is 0" +grep -q 'WARNING: mosdepth is not in --tools' "${NFW}/out.txt" || fail "MITO_VARIANTS without mosdepth files does not warn why the depth is 0" +# MITO_VARIANTS_ANNOTATE passes --numt-default exactly when mosdepth is not +# in --tools, the case where MITO_VARIANTS had no depth files. +# shellcheck disable=SC2016 # the dollar signs are Groovy placeholders, matched literally +grep -qF "contains('mosdepth') ? '' : '--numt-default'" "$MOD" \ + || fail "MITO_VARIANTS_ANNOTATE does not pass --numt-default without mosdepth in --tools" +# shellcheck disable=SC2016 +grep -qF 'og mito annotate --vcf ${vcf} ${cv} ${gn} ${numt} --out' "$MOD" \ + || fail "MITO_VARIANTS_ANNOTATE does not hand og mito annotate its --numt-default" # The workflow joins MOSDEPTH's summary and distribution in, failing on a BAM # with no mosdepth row (a plain join drops that sample from MITO_VARIANTS), and # the module refuses to go on without them when mosdepth ran. From f74af7a39a5a24045e6353f9654ca8a4ca28e71a Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:08:15 +0100 Subject: [PATCH 4/6] docs(mito): say how indels are matched and what the numt line means Bead: lin-35 Bead: lin-32 --- CHANGELOG.md | 2 ++ docs/20-mtoolbox.md | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb7a130e7..9bd31d786 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,8 @@ Each release's notes. From 1.0.0 on, a major or minor release has hand-written n - `og y private-snps` drops a call as public only when a table names the same change at that position (the same two bases, either order). Another base at a known position is a new mutation and stays in the list. A VCF with more than one sample is read only when `--sample` names one of its columns, and refused otherwise; before, the scan read the first column, which could be another man's. The file's columns do not change. - HLA-B drug answers no longer depend on the order T1K prints tied alleles. T1K writes equally likely candidates of one allele comma-separated, with one quality. Step 36 (`og pgx outside-calls`) cut the whole field to two fields, so PharmCAT got whichever candidate T1K printed first: a B\*57:01/B\*58:01 tie passed as B\*57:01 in one order and B\*58:01 in the other, which flips abacavir and allopurinol guidance. Now each candidate is read. When the tie, or an allele with an expression suffix (`N` null, `Q` questionable), leaves open whether HLA-A\*31:01, HLA-B\*15:02, \*57:01 or \*58:01 is there, the gene is `indeterminate`, is not passed, and its reason reads `ambiguous: B*57:01 possible, B*58:01 possible (T1K ties *57:01 and *58:01 on one allele)`. An OptiType call that matches one candidate does not decide it. A tie or a null that names none of those alleles still passes the gene, so a sure B\*57:01 on the other copy is not lost; the reason names the stand-in. Two-field names keep their expression suffix (`*02:53N`), so a T1K null no longer agrees with OptiType's expressed `*02:53` in step 08's concordance table, which also lists tied candidates comma-separated. OptiType never calls a null, so step 36 still compares it with the names PharmCAT reads and passes such a pair; the table's note says so. `summary.json`'s `withheld_from_pharmcat` follows the same rule, and the HLA allele table reads a null form of an allele (`B*57:01:01N`) as uncertain, not present. `og summary collect` also finds `bin/hla_alleles.tsv` beside its `--assets` folder, so a binary outside the checkout run from another folder no longer reads the HLA section as unreadable. No option, file or column changes; only these values do, and every input without a tie or a suffix gives the same bytes as before. When pypgx and Cyrius agree on CYP2D6 with a passed depth check, the call is still passed whatever pypgx's `Defining_filter` and `Quality` say; a cargo test now pins that rule. The docs no longer call OptiType independent: both callers read the same alignment, so their agreement is extra evidence, not validation. - Step 20's section of `summary.json` flags the heteroplasmic calls that PASS alone does not make reliable, and keeps them counted. A call whose bases touch either end of the control region (m.1-500 or m.16000-16569, where reads that span the origin of the circular reference align poorly) gets `origin_edge`. A call under 0.10 AF, the low-level range where missed NuMT reads look like calls, gets `low_af`. Before, both read like every other PASS call, ClinVar class included. Each entry of `heteroplasmic_sites` and each row of `annotation.variants` now carries both flags, and `heteroplasmic_origin_edge` and `heteroplasmic_low_af` count the flagged calls beside the `heteroplasmic` total, with `origin_edge_regions` and `low_af_ceiling` naming the limits. No call leaves a count or a list: a PASS m.3243A>G at AF 0.07 carries `low_af` and is still in the ClinVar pathogenic count and the confirmed variants. Fields are only added; none is renamed or removed. The reports do not show the flags yet. +- Step 20 now finds the ClinVar and gnomAD record of a mitochondrial indel wherever in its repeat each side writes it. `og mito annotate` matched an indel only on the exact spelling, and looked for ClinVar's record at most one base away. An indel in a homopolymer can be written at any place in it, so one C more in the m.311-315 tract, written m.315C>CC by one side and m.310T>TC by the other, got no ClinVar class and no gnomAD frequency. When the exact spelling finds nothing, the allele is now moved to the left end of its repeat on the rCRS (GRCh38's chrM) and matched there, on both sides; an exact match still wins, and an allele whose REF is not the rCRS is not moved. `_chrM_annotated.tsv` keeps its columns and each call's own position, REF and ALT; only `.` cells can now hold a value. Part of #411 (lin-35). +- Step 20 and the Nextflow `MITO_VARIANTS` warn when the NuMT filter had no autosomal depth (no mosdepth output, and no `AUTOSOMAL_COVERAGE` in step 20), so it ran at depth 0 and marked nothing, and the annotation table says so. `og mito annotate` has a new switch, `--numt-default`, which adds a `# numt` header line that reads `NuMT filter used a default depth` to `_chrM_annotated.tsv`; step 20 and `MITO_VARIANTS_ANNOTATE` pass it in that case. A run with a depth writes the table as before. The reports do not show the line yet. Part of #411 (lin-32). - The run manifest stays whole when two scripts write it at once. `bin/write_manifest.sh` built every manifest in one shared file, `run_manifest.tsv.tmp`. When a run and a report script wrote one sample's manifest at the same time, each emptied and appended to the other's file, and the manifest that landed had rows missing or repeated. Each writer now builds its manifest in a temporary file of its own beside `run_manifest.tsv` and renames it into place, so the manifest is always one writer's whole manifest. A writer that stops early removes its temporary file. `tests/test_write_manifest.sh` runs three writers at once; its control copy with the shared name breaks the manifest. The manifest's rows and format do not change. - The delta report words each reason it gives for a changed result as a guess. The "why" of a change read `changed inputs: VEP_CACHE_RELEASE 115 -> 116`, which reads as the cause. The delta only knows which inputs changed and which inputs feed the section, so it now reads `likely from changed inputs: VEP_CACHE_RELEASE 115 -> 116`, in `_delta.json`, `.txt` and `.html`. The line for a result whose tracked inputs did not change names no cause and stays as it was. No file, field or column changes; only the text of `why`. - Step 46's LPA KIV-2 message now says what its interval covers and what the count adds up. It read `about N KIV-2 repeats in total (95% interval LOW to HIGH)`, which looked like a calibrated range for one chromosome. The interval comes only from how the depth varies between exons, read as independent, so it covers read sampling and leaves out any aligner or library bias; the count is the total over both copies of chromosome 6. The message is now `about N KIV-2 repeats in total over both copies (95% interval for read sampling, LOW to HIGH; aligner or library bias not included)`, then the class reading as before. Only the `message` value of `lpa/_lpa_kiv2.tsv` and the step's stdout line change; the columns `kiv2_copies`, `ci95_low` and `ci95_high` keep their names and values. diff --git a/docs/20-mtoolbox.md b/docs/20-mtoolbox.md index 964e5f859..7d77a456d 100644 --- a/docs/20-mtoolbox.md +++ b/docs/20-mtoolbox.md @@ -61,7 +61,7 @@ The last stage (`og mito annotate`, in `PYTHON_IMAGE`) writes `${SAMPLE}_chrM_an - ClinVar's record of the same allele from `clinvar/clinvar_chr.vcf.gz`, the ClinVar file `setup.sh` installs: `clinvar_id`, `clinvar_significance` (CLNSIG), `clinvar_review` (CLNREVSTAT) and `clinvar_condition` (CLNDN); - gnomAD v3.1's chrM sites of the same allele (`gnomad_mito/`, pinned in `versions.env`): `gnomad_af_hom` (the share of gnomAD's samples with the allele at 95% or more), `gnomad_af_het` (at 10% to 95%) and `gnomad_max_hl` (the highest level gnomAD saw). -Alleles are matched by position, REF and ALT after the bases they share are trimmed, so Mutect2's multi-allelic indels meet ClinVar's and gnomAD's single-allele records. A file that is not installed leaves its columns `.`, and the table's first lines say which. The reports list each PASS call that is heteroplasmic, or that ClinVar classes as anything but benign (a record with no classification, `not_provided`, does not count), with these columns, and count the ClinVar pathogenic or likely pathogenic ones. +Alleles are matched by position, REF and ALT after the bases they share are trimmed, so Mutect2's multi-allelic indels meet ClinVar's and gnomAD's single-allele records. An indel in a repeat can be written at any place in it, so an indel that finds no record that way is moved to the left end of its repeat on the rCRS (GRCh38's chrM) and matched there, on both sides. One C more in the m.311-315 tract then meets its record whether either side writes it m.310T>TC or m.315C>CC. An exact spelling wins over a moved one, and an allele whose REF is not the rCRS at its position is not moved. The table keeps each call's own position, REF and ALT. A file that is not installed leaves its columns `.`, and the table's first lines say which. The reports list each PASS call that is heteroplasmic, or that ClinVar classes as anything but benign (a record with no classification, `not_provided`, does not count), with these columns, and count the ClinVar pathogenic or likely pathogenic ones. Step 13's VEP annotates the main VCF, whose chrM records come from the nuclear caller: a heteroplasmic call only Mutect2 makes has no VEP record, so this step annotates its own calls. In the Nextflow pipeline `MITO_VARIANTS_ANNOTATE` does the same after `MITO_VARIANTS`, with the files of `--mito_clinvar` and `--gnomad_mito`; without them its table names each file as not given and leaves its columns `.`. @@ -100,7 +100,7 @@ The copy-number catalog (`bin/cn_loci.tsv`, [docs/cn-loci.md](cn-loci.md)) has n ### NuMTs -NuMTs are copies of mitochondrial DNA in the nuclear genome. Their reads can map to chrM and look like low-level heteroplasmy. GATK's NuMTFilterTool marks an allele `possible_numt` when its depth is no more than such copies could give at the sample's autosomal depth. The script reads the median autosomal depth from step 16b's mosdepth output (`mosdepth/.mosdepth.summary.txt` and `.global.dist.txt`), so run step 16b first; `AUTOSOMAL_COVERAGE=30` sets it by hand. Without either, the filter runs at depth 0 and marks nothing, and the step says so. The Nextflow `MITO_VARIANTS` runs the same filter with the same depth, read from `MOSDEPTH`'s output when `mosdepth` is in `--tools` (a default tool); without it, at depth 0. +NuMTs are copies of mitochondrial DNA in the nuclear genome. Their reads can map to chrM and look like low-level heteroplasmy. GATK's NuMTFilterTool marks an allele `possible_numt` when its depth is no more than such copies could give at the sample's autosomal depth. The script reads the median autosomal depth from step 16b's mosdepth output (`mosdepth/.mosdepth.summary.txt` and `.global.dist.txt`), so run step 16b first; `AUTOSOMAL_COVERAGE=30` sets it by hand. Without either, the filter runs at depth 0 and marks nothing. The step then prints a `WARNING`, and the annotation table gets a `# numt` header line that reads `NuMT filter used a default depth` (`og mito annotate --numt-default`). The Nextflow `MITO_VARIANTS` runs the same filter with the same depth, read from `MOSDEPTH`'s output when `mosdepth` is in `--tools` (a default tool). Without it the depth is 0: the task warns, and `MITO_VARIANTS_ANNOTATE` writes the same `# numt` line. The reports do not show that line yet. ## Output - `${SAMPLE}_chrM_mutect2.vcf.gz` — Raw mitochondrial variant calls From 623102583e28ea2fcb14f64eccde52ad3473f61f Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:29:40 +0100 Subject: [PATCH 5/6] fix(mito): warn on the Nextflow console when the NuMT filter has no depth A successful task's stderr stays in its work directory, so the MITO_VARIANTS WARNING never reached a Nextflow user. The workflow now says it with log.warn when mito_variants runs without mosdepth. --- CHANGELOG.md | 2 +- docs/20-mtoolbox.md | 2 +- tests/fake-docker/bash-step-somatic-mito.sh | 5 +++++ workflows/bam_analysis.nf | 9 ++++++++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bd31d786..f5670eea9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,7 +27,7 @@ Each release's notes. From 1.0.0 on, a major or minor release has hand-written n - HLA-B drug answers no longer depend on the order T1K prints tied alleles. T1K writes equally likely candidates of one allele comma-separated, with one quality. Step 36 (`og pgx outside-calls`) cut the whole field to two fields, so PharmCAT got whichever candidate T1K printed first: a B\*57:01/B\*58:01 tie passed as B\*57:01 in one order and B\*58:01 in the other, which flips abacavir and allopurinol guidance. Now each candidate is read. When the tie, or an allele with an expression suffix (`N` null, `Q` questionable), leaves open whether HLA-A\*31:01, HLA-B\*15:02, \*57:01 or \*58:01 is there, the gene is `indeterminate`, is not passed, and its reason reads `ambiguous: B*57:01 possible, B*58:01 possible (T1K ties *57:01 and *58:01 on one allele)`. An OptiType call that matches one candidate does not decide it. A tie or a null that names none of those alleles still passes the gene, so a sure B\*57:01 on the other copy is not lost; the reason names the stand-in. Two-field names keep their expression suffix (`*02:53N`), so a T1K null no longer agrees with OptiType's expressed `*02:53` in step 08's concordance table, which also lists tied candidates comma-separated. OptiType never calls a null, so step 36 still compares it with the names PharmCAT reads and passes such a pair; the table's note says so. `summary.json`'s `withheld_from_pharmcat` follows the same rule, and the HLA allele table reads a null form of an allele (`B*57:01:01N`) as uncertain, not present. `og summary collect` also finds `bin/hla_alleles.tsv` beside its `--assets` folder, so a binary outside the checkout run from another folder no longer reads the HLA section as unreadable. No option, file or column changes; only these values do, and every input without a tie or a suffix gives the same bytes as before. When pypgx and Cyrius agree on CYP2D6 with a passed depth check, the call is still passed whatever pypgx's `Defining_filter` and `Quality` say; a cargo test now pins that rule. The docs no longer call OptiType independent: both callers read the same alignment, so their agreement is extra evidence, not validation. - Step 20's section of `summary.json` flags the heteroplasmic calls that PASS alone does not make reliable, and keeps them counted. A call whose bases touch either end of the control region (m.1-500 or m.16000-16569, where reads that span the origin of the circular reference align poorly) gets `origin_edge`. A call under 0.10 AF, the low-level range where missed NuMT reads look like calls, gets `low_af`. Before, both read like every other PASS call, ClinVar class included. Each entry of `heteroplasmic_sites` and each row of `annotation.variants` now carries both flags, and `heteroplasmic_origin_edge` and `heteroplasmic_low_af` count the flagged calls beside the `heteroplasmic` total, with `origin_edge_regions` and `low_af_ceiling` naming the limits. No call leaves a count or a list: a PASS m.3243A>G at AF 0.07 carries `low_af` and is still in the ClinVar pathogenic count and the confirmed variants. Fields are only added; none is renamed or removed. The reports do not show the flags yet. - Step 20 now finds the ClinVar and gnomAD record of a mitochondrial indel wherever in its repeat each side writes it. `og mito annotate` matched an indel only on the exact spelling, and looked for ClinVar's record at most one base away. An indel in a homopolymer can be written at any place in it, so one C more in the m.311-315 tract, written m.315C>CC by one side and m.310T>TC by the other, got no ClinVar class and no gnomAD frequency. When the exact spelling finds nothing, the allele is now moved to the left end of its repeat on the rCRS (GRCh38's chrM) and matched there, on both sides; an exact match still wins, and an allele whose REF is not the rCRS is not moved. `_chrM_annotated.tsv` keeps its columns and each call's own position, REF and ALT; only `.` cells can now hold a value. Part of #411 (lin-35). -- Step 20 and the Nextflow `MITO_VARIANTS` warn when the NuMT filter had no autosomal depth (no mosdepth output, and no `AUTOSOMAL_COVERAGE` in step 20), so it ran at depth 0 and marked nothing, and the annotation table says so. `og mito annotate` has a new switch, `--numt-default`, which adds a `# numt` header line that reads `NuMT filter used a default depth` to `_chrM_annotated.tsv`; step 20 and `MITO_VARIANTS_ANNOTATE` pass it in that case. A run with a depth writes the table as before. The reports do not show the line yet. Part of #411 (lin-32). +- Step 20 and the Nextflow run (on the console, when it starts) warn when the NuMT filter had no autosomal depth (no mosdepth output, and no `AUTOSOMAL_COVERAGE` in step 20), so it ran at depth 0 and marked nothing, and the annotation table says so. `og mito annotate` has a new switch, `--numt-default`, which adds a `# numt` header line that reads `NuMT filter used a default depth` to `_chrM_annotated.tsv`; step 20 and `MITO_VARIANTS_ANNOTATE` pass it in that case. A run with a depth writes the table as before. The reports do not show the line yet. Part of #411 (lin-32). - The run manifest stays whole when two scripts write it at once. `bin/write_manifest.sh` built every manifest in one shared file, `run_manifest.tsv.tmp`. When a run and a report script wrote one sample's manifest at the same time, each emptied and appended to the other's file, and the manifest that landed had rows missing or repeated. Each writer now builds its manifest in a temporary file of its own beside `run_manifest.tsv` and renames it into place, so the manifest is always one writer's whole manifest. A writer that stops early removes its temporary file. `tests/test_write_manifest.sh` runs three writers at once; its control copy with the shared name breaks the manifest. The manifest's rows and format do not change. - The delta report words each reason it gives for a changed result as a guess. The "why" of a change read `changed inputs: VEP_CACHE_RELEASE 115 -> 116`, which reads as the cause. The delta only knows which inputs changed and which inputs feed the section, so it now reads `likely from changed inputs: VEP_CACHE_RELEASE 115 -> 116`, in `_delta.json`, `.txt` and `.html`. The line for a result whose tracked inputs did not change names no cause and stays as it was. No file, field or column changes; only the text of `why`. - Step 46's LPA KIV-2 message now says what its interval covers and what the count adds up. It read `about N KIV-2 repeats in total (95% interval LOW to HIGH)`, which looked like a calibrated range for one chromosome. The interval comes only from how the depth varies between exons, read as independent, so it covers read sampling and leaves out any aligner or library bias; the count is the total over both copies of chromosome 6. The message is now `about N KIV-2 repeats in total over both copies (95% interval for read sampling, LOW to HIGH; aligner or library bias not included)`, then the class reading as before. Only the `message` value of `lpa/_lpa_kiv2.tsv` and the step's stdout line change; the columns `kiv2_copies`, `ci95_low` and `ci95_high` keep their names and values. diff --git a/docs/20-mtoolbox.md b/docs/20-mtoolbox.md index 7d77a456d..485c2bb8c 100644 --- a/docs/20-mtoolbox.md +++ b/docs/20-mtoolbox.md @@ -100,7 +100,7 @@ The copy-number catalog (`bin/cn_loci.tsv`, [docs/cn-loci.md](cn-loci.md)) has n ### NuMTs -NuMTs are copies of mitochondrial DNA in the nuclear genome. Their reads can map to chrM and look like low-level heteroplasmy. GATK's NuMTFilterTool marks an allele `possible_numt` when its depth is no more than such copies could give at the sample's autosomal depth. The script reads the median autosomal depth from step 16b's mosdepth output (`mosdepth/.mosdepth.summary.txt` and `.global.dist.txt`), so run step 16b first; `AUTOSOMAL_COVERAGE=30` sets it by hand. Without either, the filter runs at depth 0 and marks nothing. The step then prints a `WARNING`, and the annotation table gets a `# numt` header line that reads `NuMT filter used a default depth` (`og mito annotate --numt-default`). The Nextflow `MITO_VARIANTS` runs the same filter with the same depth, read from `MOSDEPTH`'s output when `mosdepth` is in `--tools` (a default tool). Without it the depth is 0: the task warns, and `MITO_VARIANTS_ANNOTATE` writes the same `# numt` line. The reports do not show that line yet. +NuMTs are copies of mitochondrial DNA in the nuclear genome. Their reads can map to chrM and look like low-level heteroplasmy. GATK's NuMTFilterTool marks an allele `possible_numt` when its depth is no more than such copies could give at the sample's autosomal depth. The script reads the median autosomal depth from step 16b's mosdepth output (`mosdepth/.mosdepth.summary.txt` and `.global.dist.txt`), so run step 16b first; `AUTOSOMAL_COVERAGE=30` sets it by hand. Without either, the filter runs at depth 0 and marks nothing. The step then prints a `WARNING`, and the annotation table gets a `# numt` header line that reads `NuMT filter used a default depth` (`og mito annotate --numt-default`). The Nextflow `MITO_VARIANTS` runs the same filter with the same depth, read from `MOSDEPTH`'s output when `mosdepth` is in `--tools` (a default tool). Without it the depth is 0: the run prints a warning on the console when it starts, and `MITO_VARIANTS_ANNOTATE` writes the same `# numt` line. The reports do not show that line yet. ## Output - `${SAMPLE}_chrM_mutect2.vcf.gz` — Raw mitochondrial variant calls diff --git a/tests/fake-docker/bash-step-somatic-mito.sh b/tests/fake-docker/bash-step-somatic-mito.sh index f23363af8..a43bf030d 100755 --- a/tests/fake-docker/bash-step-somatic-mito.sh +++ b/tests/fake-docker/bash-step-somatic-mito.sh @@ -175,6 +175,11 @@ grep -qF 'og mito annotate --vcf ${vcf} ${cv} ${gn} ${numt} --out' "$MOD" \ # the module refuses to go on without them when mosdepth ran. grep -qF 'ch_bam.join(ch_mosdepth_depth, failOnMismatch: true)' "${REPO_ROOT}/workflows/bam_analysis.nf" \ || fail "workflows/bam_analysis.nf does not join MOSDEPTH's output into MITO_VARIANTS with failOnMismatch" +# A task's stderr stays in its work directory when the task succeeds, so the +# workflow itself says on the console that the NuMT filter has no depth. +grep -q 'log.warn "mito_variants: mosdepth is not in --tools, so the NuMT filter runs at the default depth 0' \ + "${REPO_ROOT}/workflows/bam_analysis.nf" \ + || fail "workflows/bam_analysis.nf does not warn on the console when mito_variants runs without mosdepth" [ "$(grep -c "contains('mosdepth') && !mosdepth_summary" "$MOD")" -eq 2 ] \ || fail "MITO_VARIANTS' script and stub do not both stop when mosdepth ran and its output is missing" echo "MITO_VARIANTS marks possible NuMTs at the depth step 20 uses." diff --git a/workflows/bam_analysis.nf b/workflows/bam_analysis.nf index 71214cb51..8387f79ff 100644 --- a/workflows/bam_analysis.nf +++ b/workflows/bam_analysis.nf @@ -214,7 +214,14 @@ workflow BAM_ANALYSIS { // dropping the sample from MITO_VARIANTS. // if (params.tools && params.tools.split(',').collect{it.trim()}.contains('mito_variants')) { - ch_mito_in = params.tools.split(',').collect{it.trim()}.contains('mosdepth') + def mito_has_depth = params.tools.split(',').collect{it.trim()}.contains('mosdepth') + if (!mito_has_depth) { + // The task's own WARNING goes only to its .command.err, which + // Nextflow does not show for a task that succeeds. + log.warn "mito_variants: mosdepth is not in --tools, so the NuMT filter runs at the default depth 0 " + + "and marks nothing; _chrM_annotated.tsv gets the '# numt' line that says so." + } + ch_mito_in = mito_has_depth ? ch_bam.join(ch_mosdepth_depth, failOnMismatch: true) : ch_bam.map { meta, bam, bai -> [meta, bam, bai, [], []] } MITO_VARIANTS( From 660310aa9b060fc585e51c1e2064969a49856593 Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:40:47 +0100 Subject: [PATCH 6/6] fix(mito): check an indel's whole REF against the rCRS before moving it left_allele trimmed first, so m.314 CG>CCG passed as C>CC on the rCRS and met m.310's record. The docs now say the table writes the trimmed spelling, not the moved one. --- CHANGELOG.md | 2 +- docs/20-mtoolbox.md | 2 +- rust/crates/og-summary/src/mito.rs | 4 +++- rust/crates/og-summary/src/mito/tests.rs | 3 +++ 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5670eea9..973a55070 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,7 +26,7 @@ Each release's notes. From 1.0.0 on, a major or minor release has hand-written n - `og y private-snps` drops a call as public only when a table names the same change at that position (the same two bases, either order). Another base at a known position is a new mutation and stays in the list. A VCF with more than one sample is read only when `--sample` names one of its columns, and refused otherwise; before, the scan read the first column, which could be another man's. The file's columns do not change. - HLA-B drug answers no longer depend on the order T1K prints tied alleles. T1K writes equally likely candidates of one allele comma-separated, with one quality. Step 36 (`og pgx outside-calls`) cut the whole field to two fields, so PharmCAT got whichever candidate T1K printed first: a B\*57:01/B\*58:01 tie passed as B\*57:01 in one order and B\*58:01 in the other, which flips abacavir and allopurinol guidance. Now each candidate is read. When the tie, or an allele with an expression suffix (`N` null, `Q` questionable), leaves open whether HLA-A\*31:01, HLA-B\*15:02, \*57:01 or \*58:01 is there, the gene is `indeterminate`, is not passed, and its reason reads `ambiguous: B*57:01 possible, B*58:01 possible (T1K ties *57:01 and *58:01 on one allele)`. An OptiType call that matches one candidate does not decide it. A tie or a null that names none of those alleles still passes the gene, so a sure B\*57:01 on the other copy is not lost; the reason names the stand-in. Two-field names keep their expression suffix (`*02:53N`), so a T1K null no longer agrees with OptiType's expressed `*02:53` in step 08's concordance table, which also lists tied candidates comma-separated. OptiType never calls a null, so step 36 still compares it with the names PharmCAT reads and passes such a pair; the table's note says so. `summary.json`'s `withheld_from_pharmcat` follows the same rule, and the HLA allele table reads a null form of an allele (`B*57:01:01N`) as uncertain, not present. `og summary collect` also finds `bin/hla_alleles.tsv` beside its `--assets` folder, so a binary outside the checkout run from another folder no longer reads the HLA section as unreadable. No option, file or column changes; only these values do, and every input without a tie or a suffix gives the same bytes as before. When pypgx and Cyrius agree on CYP2D6 with a passed depth check, the call is still passed whatever pypgx's `Defining_filter` and `Quality` say; a cargo test now pins that rule. The docs no longer call OptiType independent: both callers read the same alignment, so their agreement is extra evidence, not validation. - Step 20's section of `summary.json` flags the heteroplasmic calls that PASS alone does not make reliable, and keeps them counted. A call whose bases touch either end of the control region (m.1-500 or m.16000-16569, where reads that span the origin of the circular reference align poorly) gets `origin_edge`. A call under 0.10 AF, the low-level range where missed NuMT reads look like calls, gets `low_af`. Before, both read like every other PASS call, ClinVar class included. Each entry of `heteroplasmic_sites` and each row of `annotation.variants` now carries both flags, and `heteroplasmic_origin_edge` and `heteroplasmic_low_af` count the flagged calls beside the `heteroplasmic` total, with `origin_edge_regions` and `low_af_ceiling` naming the limits. No call leaves a count or a list: a PASS m.3243A>G at AF 0.07 carries `low_af` and is still in the ClinVar pathogenic count and the confirmed variants. Fields are only added; none is renamed or removed. The reports do not show the flags yet. -- Step 20 now finds the ClinVar and gnomAD record of a mitochondrial indel wherever in its repeat each side writes it. `og mito annotate` matched an indel only on the exact spelling, and looked for ClinVar's record at most one base away. An indel in a homopolymer can be written at any place in it, so one C more in the m.311-315 tract, written m.315C>CC by one side and m.310T>TC by the other, got no ClinVar class and no gnomAD frequency. When the exact spelling finds nothing, the allele is now moved to the left end of its repeat on the rCRS (GRCh38's chrM) and matched there, on both sides; an exact match still wins, and an allele whose REF is not the rCRS is not moved. `_chrM_annotated.tsv` keeps its columns and each call's own position, REF and ALT; only `.` cells can now hold a value. Part of #411 (lin-35). +- Step 20 now finds the ClinVar and gnomAD record of a mitochondrial indel wherever in its repeat each side writes it. `og mito annotate` matched an indel only on the exact spelling, and looked for ClinVar's record at most one base away. An indel in a homopolymer can be written at any place in it, so one C more in the m.311-315 tract, written m.315C>CC by one side and m.310T>TC by the other, got no ClinVar class and no gnomAD frequency. When the exact spelling finds nothing, the allele is now moved to the left end of its repeat on the rCRS (GRCh38's chrM) and matched there, on both sides; an exact match still wins, and an allele whose REF, as written, is not the rCRS is not moved. `_chrM_annotated.tsv` keeps its columns, and each row keeps the call's trimmed position, REF and ALT as before, never the moved spelling; only `.` cells can now hold a value. Part of #411 (lin-35). - Step 20 and the Nextflow run (on the console, when it starts) warn when the NuMT filter had no autosomal depth (no mosdepth output, and no `AUTOSOMAL_COVERAGE` in step 20), so it ran at depth 0 and marked nothing, and the annotation table says so. `og mito annotate` has a new switch, `--numt-default`, which adds a `# numt` header line that reads `NuMT filter used a default depth` to `_chrM_annotated.tsv`; step 20 and `MITO_VARIANTS_ANNOTATE` pass it in that case. A run with a depth writes the table as before. The reports do not show the line yet. Part of #411 (lin-32). - The run manifest stays whole when two scripts write it at once. `bin/write_manifest.sh` built every manifest in one shared file, `run_manifest.tsv.tmp`. When a run and a report script wrote one sample's manifest at the same time, each emptied and appended to the other's file, and the manifest that landed had rows missing or repeated. Each writer now builds its manifest in a temporary file of its own beside `run_manifest.tsv` and renames it into place, so the manifest is always one writer's whole manifest. A writer that stops early removes its temporary file. `tests/test_write_manifest.sh` runs three writers at once; its control copy with the shared name breaks the manifest. The manifest's rows and format do not change. - The delta report words each reason it gives for a changed result as a guess. The "why" of a change read `changed inputs: VEP_CACHE_RELEASE 115 -> 116`, which reads as the cause. The delta only knows which inputs changed and which inputs feed the section, so it now reads `likely from changed inputs: VEP_CACHE_RELEASE 115 -> 116`, in `_delta.json`, `.txt` and `.html`. The line for a result whose tracked inputs did not change names no cause and stays as it was. No file, field or column changes; only the text of `why`. diff --git a/docs/20-mtoolbox.md b/docs/20-mtoolbox.md index 485c2bb8c..1991f0008 100644 --- a/docs/20-mtoolbox.md +++ b/docs/20-mtoolbox.md @@ -61,7 +61,7 @@ The last stage (`og mito annotate`, in `PYTHON_IMAGE`) writes `${SAMPLE}_chrM_an - ClinVar's record of the same allele from `clinvar/clinvar_chr.vcf.gz`, the ClinVar file `setup.sh` installs: `clinvar_id`, `clinvar_significance` (CLNSIG), `clinvar_review` (CLNREVSTAT) and `clinvar_condition` (CLNDN); - gnomAD v3.1's chrM sites of the same allele (`gnomad_mito/`, pinned in `versions.env`): `gnomad_af_hom` (the share of gnomAD's samples with the allele at 95% or more), `gnomad_af_het` (at 10% to 95%) and `gnomad_max_hl` (the highest level gnomAD saw). -Alleles are matched by position, REF and ALT after the bases they share are trimmed, so Mutect2's multi-allelic indels meet ClinVar's and gnomAD's single-allele records. An indel in a repeat can be written at any place in it, so an indel that finds no record that way is moved to the left end of its repeat on the rCRS (GRCh38's chrM) and matched there, on both sides. One C more in the m.311-315 tract then meets its record whether either side writes it m.310T>TC or m.315C>CC. An exact spelling wins over a moved one, and an allele whose REF is not the rCRS at its position is not moved. The table keeps each call's own position, REF and ALT. A file that is not installed leaves its columns `.`, and the table's first lines say which. The reports list each PASS call that is heteroplasmic, or that ClinVar classes as anything but benign (a record with no classification, `not_provided`, does not count), with these columns, and count the ClinVar pathogenic or likely pathogenic ones. +Alleles are matched by position, REF and ALT after the bases they share are trimmed, so Mutect2's multi-allelic indels meet ClinVar's and gnomAD's single-allele records. An indel in a repeat can be written at any place in it, so an indel that finds no record that way is moved to the left end of its repeat on the rCRS (GRCh38's chrM) and matched there, on both sides. One C more in the m.311-315 tract then meets its record whether either side writes it m.310T>TC or m.315C>CC. An exact spelling wins over a moved one, and an allele whose REF, as written, is not the rCRS at its position is not moved. The table writes each call's position, REF and ALT trimmed as above (Mutect2's m.302 AC>ACC reads m.302 A>AC), never the moved spelling. A file that is not installed leaves its columns `.`, and the table's first lines say which. The reports list each PASS call that is heteroplasmic, or that ClinVar classes as anything but benign (a record with no classification, `not_provided`, does not count), with these columns, and count the ClinVar pathogenic or likely pathogenic ones. Step 13's VEP annotates the main VCF, whose chrM records come from the nuclear caller: a heteroplasmic call only Mutect2 makes has no VEP record, so this step annotates its own calls. In the Nextflow pipeline `MITO_VARIANTS_ANNOTATE` does the same after `MITO_VARIANTS`, with the files of `--mito_clinvar` and `--gnomad_mito`; without them its table names each file as not given and leaves its columns `.`. diff --git a/rust/crates/og-summary/src/mito.rs b/rust/crates/og-summary/src/mito.rs index d0da82b04..f6eed7b76 100644 --- a/rust/crates/og-summary/src/mito.rs +++ b/rust/crates/og-summary/src/mito.rs @@ -321,13 +321,15 @@ pub fn min_allele(mut pos: i128, r: &str, a: &str) -> Allele { /// or whose REF is not the rCRS there (another chrM), stays as /// [`min_allele`] gives it. pub fn left_allele(pos: i128, r: &str, a: &str) -> Allele { - let (pos, r, a) = min_allele(pos, r, a); + // The whole REF as written, before trimming: m.314 CG>CCG trims to a + // C>CC that is on the rCRS, but its G at m.315 is not. let on_rcrs = r.chars().enumerate().all(|(k, c)| { i128::try_from(k) .ok() .and_then(|k| rcrs_base(pos + k)) .is_some_and(|b| b.eq_ignore_ascii_case(&c)) }); + let (pos, r, a) = min_allele(pos, r, a); if r.chars().count() == a.chars().count() || !on_rcrs { return (pos, r, a); } diff --git a/rust/crates/og-summary/src/mito/tests.rs b/rust/crates/og-summary/src/mito/tests.rs index 5922fc9f3..7473d76ea 100644 --- a/rust/crates/og-summary/src/mito/tests.rs +++ b/rust/crates/og-summary/src/mito/tests.rs @@ -31,6 +31,9 @@ fn left_allele_moves_an_indel_to_the_left_end_of_its_repeat() { // An SNV, a REF that is not the rCRS, and nothing before m.1 stay. assert_eq!(k(3243, "A", "G"), al(3243, "A", "G")); assert_eq!(k(315, "G", "GG"), al(315, "G", "GG")); + // The whole REF is checked before trimming: m.314-315 is CC on the + // rCRS, so CG>CCG is off it even though its trimmed C>CC is not. + assert_eq!(k(314, "CG", "CCG"), al(314, "C", "CC")); assert_eq!(k(1, "G", "GG"), al(1, "G", "GG")); assert_eq!(k(16570, "A", "AA"), al(16570, "A", "AA")); }