diff --git a/CHANGELOG.md b/CHANGELOG.md index 05c3388e1..bd62b3569 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Each release's notes. From 1.0.0 on, a major or minor release has hand-written n ## Unreleased +- `og report render` now writes the pharmacogenomics, HLA and mitochondrial sections of both reports: PharmCAT, the CPIC drug-gene recommendations, pypgx with the PGx copy number, CYP2D6 across the callers with step 36's verdict, HLA typing with its derived lines (coeliac DQ types, the DQB1*02 dose, the drug and disease alleles) and the KIR genes, and the mitochondrial variants with the confirmed list, the large deletions and the mtDNA copy number. No step calls the verb yet, so nothing a run writes changes. On 110 summaries (the 99 report-render parity cases and 11 new changes of one summary that reach each state of these sections) its output is byte for byte the Python's for those parts. The mitochondrial card's chart is left out until the charts are ported. The checks of `tests/test_hla_report.py` and `tests/test_mito_extended.py` are cargo tests in `rust/crates/og-render/tests/port_report_pgx.rs`, and both Python files are gone. What og does on purpose that the Python did not: + - A summary without the CYP2D6 block (`"cyp2d6"` missing or `null`) shows no CYP2D6 across the callers. The Python stopped with a KeyError or a TypeError and wrote no report. - The self-test of `scripts/ci/check-images.sh` copies the whole tree again once the list of tracked paths passes 128 KiB. It piped that list through `xargs` to `tar`, and GNU `xargs` split a list that long over two `tar` runs; the reading `tar` stopped after the first archive, so Guard's `images` job failed on a broken pipe (exit 123). One `tar` now reads the list itself (`tar --null -T -`). Nothing the check reports changes. - The phased family VCF is indexed by the Rust `og` binary. WhatsHap's step (step 38 and the Nextflow WHATSHAP_PED process) wrote `family.phased.vcf.gz.tbi` with a Python one-liner, `pysam.tabix_index`. It now runs `og vcf index -f`. The `.tbi` keeps its name and format, and its bytes, once inflated, are the ones htslib 1.24 writes (the htslib of the pysam and bcftools the pipeline pins): the same bins in the same order, the same chunks and the same linear index. Only the deflate stream can differ, as two deflate libraries differ. On a 5-million-record three-sample VCF it took 0.75 to 0.91 s and 31 MB at peak, where htslib's own `tabix` took 0.81 to 0.93 s and 19 MB. - As in htslib 1.24, a record ends at the largest of INFO/END, the REF length, the SVLEN of a ``, ``, `` or `` ALT, and a sample's LEN on a gVCF block. A line with no position to read, unsorted records, a sequence that starts again, a record past 2^29 and an empty file stop the index with a message, and nothing is written. Without `-f` it never writes over an index, even one that appears while it runs. diff --git a/bin/hla_alleles.tsv b/bin/hla_alleles.tsv index e8da1c385..076e6c38b 100644 --- a/bin/hla_alleles.tsv +++ b/bin/hla_alleles.tsv @@ -4,7 +4,7 @@ # allele: an allele group at a field boundary: B*27 matches B*27:05:02, B*57:01 matches B*57:01:01. # kind: drug (a drug reaction) or disease (a condition). association: what the study in pmids reports. # pmids: the primary papers, comma-separated PubMed ids. citation: first author, year, journal. -# tests/test_hla_report.py checks the schema. +# rust/crates/og-render/tests/port_report_pgx.rs checks the schema. allele kind association pmids citation A*31:01 drug carbamazepine hypersensitivity reactions in Europeans 21428769 McCormack 2011, N Engl J Med B*15:02 drug carbamazepine Stevens-Johnson syndrome in Han Chinese 15057820 Chung 2004, Nature diff --git a/docs/08-hla-typing.md b/docs/08-hla-typing.md index 5926f8849..029f2e707 100644 --- a/docs/08-hla-typing.md +++ b/docs/08-hla-typing.md @@ -120,7 +120,7 @@ docker run --rm \ ## What the Report Derives -The report (step 24 and the text report) lists each T1K row and then derives three things from it. The code is the HLA section of `og summary collect` (`rust/crates/og-summary/src/collect/group_c/hla.rs`); `tests/test_hla_report.py` checks every rule on synthetic T1K rows. The genotypes are unphased: T1K gives two alleles per gene, not which chromosome each sits on. +The report (step 24 and the text report) lists each T1K row and then derives three things from it. The code is the HLA section of `og summary collect` (`rust/crates/og-summary/src/collect/group_c/hla.rs`); `rust/crates/og-render/tests/port_report_pgx.rs` checks every rule on synthetic T1K rows. The genotypes are unphased: T1K gives two alleles per gene, not which chromosome each sits on. **Coeliac DQ.** From the DQA1 and DQB1 rows: diff --git a/rust/Cargo.lock b/rust/Cargo.lock index ec8e92fcc..f20baab4b 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -849,6 +849,7 @@ name = "og-render" version = "0.1.0" dependencies = [ "indexmap", + "og-dev", "og-io", "og-summary", "pycompat", diff --git a/rust/crates/og-render/Cargo.toml b/rust/crates/og-render/Cargo.toml index 5112b56a7..128a9083b 100644 --- a/rust/crates/og-render/Cargo.toml +++ b/rust/crates/og-render/Cargo.toml @@ -20,5 +20,8 @@ og-io.workspace = true og-summary.workspace = true indexmap.workspace = true +[dev-dependencies] +og-dev.workspace = true + [lints] workspace = true diff --git a/rust/crates/og-render/src/chapters.rs b/rust/crates/og-render/src/chapters.rs index 92ace64e9..71d5d08c0 100644 --- a/rust/crates/og-render/src/chapters.rs +++ b/rust/crates/og-render/src/chapters.rs @@ -181,7 +181,7 @@ pub fn writer(sid: &str) -> Option<(TextFn, HtmlFn)> { "vntyper" => None, - "mito" => None, + "mito" => Some((sections::mito::text, sections::mito::html)), "lpa" => None, @@ -197,13 +197,13 @@ pub fn writer(sid: &str) -> Option<(TextFn, HtmlFn)> { "paralogs" => None, - "pharmcat" => None, + "pharmcat" => Some((sections::pgx::pharmcat_text, sections::pgx::pharmcat_html)), - "cpic" => None, + "cpic" => Some((sections::pgx::cpic_text, sections::pgx::cpic_html)), - "cyp2d6" => None, + "cyp2d6" => Some((sections::pgx::cyp2d6_text, sections::pgx::cyp2d6_html)), - "hla" => None, + "hla" => Some((sections::hla::text, sections::hla::html)), "locityper" => None, diff --git a/rust/crates/og-render/src/html.rs b/rust/crates/og-render/src/html.rs index 43e75f02b..8085209b1 100644 --- a/rust/crates/og-render/src/html.rs +++ b/rust/crates/og-render/src/html.rs @@ -9,7 +9,9 @@ use crate::chapters::{CHAPTERS, FrameState, LIMITS, Sec, step_of, writer}; use crate::css::CSS; use crate::escape::e; -use crate::model::{Card, Colour, HtmlPart, Inline, Line, Malformed, Section, State, Summary, py}; +use crate::model::{ + Card, Colour, HtmlPart, Inline, Layout, Line, Malformed, Section, State, Summary, Table, py, +}; use crate::notes::stale_note; use crate::sections::run; @@ -70,6 +72,57 @@ fn line(out: &mut Vec, l: &Line) { out.extend(items.iter().map(|t| format!("
  • {}
  • ", e(t)))); out.push(" ".to_owned()); } + Line::Table(t) => table(out, t), + Line::Warn(t) => out.push(format!("
    {}
    ", e(t))), + Line::ViewerNote => out.push(crate::notes::VIEWER_NOTE.to_owned()), + } +} + +/// The genome viewer's address on this computer (scripts/owngenome-view.sh); +/// the part after `#` is the locus it opens at. +pub const VIEWER_URL: &str = "http://127.0.0.1:8765/"; + +/// A table's rows, each cell escaped, with the Viewer cell last when the +/// table has that column. +fn table(out: &mut Vec, t: &Table) { + let mut head: String = t + .head + .iter() + .map(|c| format!("{}", e(c))) + .collect(); + if t.viewer.is_some() { + head.push_str("Viewer"); + } + let rows: Vec = t + .rows + .iter() + .enumerate() + .map(|(i, r)| { + let mut row: String = r.iter().map(|c| format!("{}", e(c))).collect(); + if let Some(v) = &t.viewer { + row.push_str(&match v.get(i).cloned().flatten() { + Some(at) => format!( + "{0}", + e(&at) + ), + None => "".to_owned(), + }); + } + format!("{row}") + }) + .collect(); + match t.layout { + Layout::OneLine => out.push(format!( + " {head}{}
    ", + rows.concat() + )), + Layout::RowLines => { + out.push(" ".to_owned()); + out.push(format!(" {head}")); + out.push(rows.join("\n")); + out.push("
    ".to_owned()); + } } } diff --git a/rust/crates/og-render/src/model.rs b/rust/crates/og-render/src/model.rs index 54ea06364..a922cb418 100644 --- a/rust/crates/og-render/src/model.rs +++ b/rust/crates/og-render/src/model.rs @@ -325,6 +325,34 @@ pub enum Line { Para(String), /// A plain bulleted list. List(Vec), + /// A table of plain cells. + Table(Table), + /// A warning in the stale box's style, among the card's lines. + Warn(String), + /// The sentence that says how the Viewer column opens the reads. + ViewerNote, +} + +/// A table: its column titles and rows of cells, each cell escaped once. +/// With `viewer`, a last column opens each row's locus in the genome viewer +/// (`chrom:start` or `chrom:start-end`); a row with `None` gets an empty +/// cell. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Table { + pub head: Vec, + pub rows: Vec>, + pub viewer: Option>>, + pub layout: Layout, +} + +/// How a table is laid out in the page's source. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Layout { + /// The whole table on one line. + OneLine, + /// The table tags and the head row on lines of their own, the rows on + /// one line each with no indent. + RowLines, } /// One card: a title, the caveats shown under it, the notes saying its diff --git a/rust/crates/og-render/src/sections/hla.rs b/rust/crates/og-render/src/sections/hla.rs index 79b3352a3..548422b68 100644 --- a/rust/crates/og-render/src/sections/hla.rs +++ b/rust/crates/og-render/src/sections/hla.rs @@ -1 +1,344 @@ -//! The report section `hla`. Empty until its unit of the 1.0.0 plan lands. +//! HLA typing (step 08): T1K's alleles per locus with their quality, the +//! lines derived from them (the coeliac DQ types, the DQB1*02 dose and the +//! allele table of bin/hla_alleles.tsv), OptiType's second opinion on the +//! class I genes, and the KIR genes. + +use pycompat::json::Value; + +use crate::html; +use crate::model::{Card, Inline, Line, Malformed, Summary, get, is_str, need, num, py, truthy}; +use crate::notes::{HLA_DERIVED_NOTE, HLA_NOTE, KIR_NOTE}; +use crate::sections::pgx::{items, left, or_text, text_of}; +use crate::text; + +/// A quality-0 allele with more candidates than this reads "no confident +/// call" (og summary collect keeps this many of them; its own copy is +/// private, so tests/port_report_pgx.rs checks the two caps agree). +pub const MAX_CANDIDATES: f64 = 3.0; + +/// T1K's quality as a number; one that is not a number counts as -1, as in +/// step 36. +fn quality(q: &Value) -> Result { + match q { + Value::Str(s) => Ok(pycompat::num::parse_float(s).unwrap_or(-1.0)), + _ => num(Some(q)).ok_or_else(|| Malformed("an HLA quality is not a number".into())), + } +} + +/// One HLA locus: each allele with its T1K quality, and the low-confidence +/// mark. A quality-0 allele with more than [`MAX_CANDIDATES`] candidates +/// reads "no confident call"; the HFE row carries its label. +pub fn hla_text(locus: &Value) -> Result { + let p = "sections.hla.data.loci[]"; + let q = items(get(locus, "quality", p)?.filter(|v| truthy(v)), p)?; + let n = items(get(locus, "candidates", p)?.filter(|v| truthy(v)), p)?; + let mut parts = Vec::new(); + for (i, a) in items(Some(need(locus, "alleles", p)?), p)? + .iter() + .enumerate() + { + let blank = Value::Str(String::new()); + let qi = q.get(i).unwrap_or(&blank); + let has_q = !is_str(Some(qi), ""); + let many = match n.get(i) { + Some(c) => { + num(Some(c)) + .ok_or_else(|| Malformed("an HLA candidate count is not a number".into()))? + > MAX_CANDIDATES + } + None => false, + }; + if many && has_q && quality(qi)? <= 0.0 { + parts.push(format!( + "no confident call ({} candidates, quality {})", + py(&n[i])?, + py(qi)? + )); + } else { + let mut t = text_of(a, "an HLA allele")?; + if has_q { + t += &format!(" (quality {})", py(qi)?); + } + parts.push(t); + } + } + let mut txt = parts.join(" / "); + if txt.is_empty() { + txt = "no call".to_owned(); + } + if get(locus, "low_confidence", p)?.is_some_and(truthy) { + txt += "; low confidence (T1K quality 0 or below)"; + if get(locus, "withheld_from_pharmcat", p)?.is_some_and(truthy) { + txt += ", not passed to PharmCAT"; + } + } + if let Some(l) = get(locus, "label", p)?.filter(|v| truthy(v)) { + txt += &format!(" [{}]", py(l)?); + } + Ok(txt) +} + +/// The order of an allele's state in the table: present ones first. +fn rank(state: &Value) -> u8 { + match state { + Value::Str(s) => match s.as_str() { + "present" => 0, + "uncertain" => 1, + "absent" => 2, + "not_determined" => 3, + _ => 9, + }, + _ => 9, + } +} + +/// The derived HLA lines both reports print: the coeliac DQ line, the +/// DQB1*02 dose and the allele table, present ones first. +pub fn hla_derived_lines(d: Option<&Value>) -> Result, Malformed> { + let Some(d) = d.filter(|v| truthy(v)) else { + return Ok(Vec::new()); + }; + let p = "sections.hla.data.derived"; + let empty = Value::Object(Default::default()); + let c = get(d, "coeliac", p)? + .filter(|v| truthy(v)) + .unwrap_or(&empty); + let cp = "sections.hla.data.derived.coeliac"; + let mut out = Vec::new(); + if is_str(get(c, "status", cp)?, "derived") { + let mut dq25 = text_of(need(c, "dq2_5", cp)?, "the DQ2.5 state")?; + if let Some(ph) = get(c, "dq2_5_phase", cp)?.filter(|v| truthy(v)) { + dq25 += &format!(", {}", py(ph)?); + } + out.push(format!( + "Coeliac DQ: DQ2.5 {dq25}; DQ2.2 {}; DQ8 {}", + py(need(c, "dq2_2", cp)?)?, + py(need(c, "dq8", cp)?)? + )); + } else { + out.push(format!( + "Coeliac DQ: not determined ({})", + or_text(get(c, "reason", cp)?, "no DQA1 and DQB1 call")? + )); + } + let dose = match get(c, "dqb1_02_dose", cp)? { + None | Some(Value::Null) => "not determined".to_owned(), + Some(v) => py(v)?, + }; + out.push(format!("DQB1*02 copies: {dose}")); + let mut alleles: Vec<&Value> = items( + get(d, "alleles", p)?.filter(|v| truthy(v)), + "sections.hla.data.derived.alleles", + )? + .iter() + .collect(); + let ap = "sections.hla.data.derived.alleles[]"; + for a in &alleles { + need(a, "state", ap)?; + } + alleles.sort_by_key(|a| rank(need(a, "state", ap).expect("checked above"))); + for a in alleles { + let mut state = text_of(need(a, "state", ap)?, "an allele state")?.replace('_', " "); + if let Some(r) = get(a, "reason", ap)?.filter(|v| truthy(v)) { + state += &format!(" ({})", py(r)?); + } + out.push(format!( + "{} {state}: {} ({})", + py(need(a, "allele", ap)?)?, + py(need(a, "association", ap)?)?, + py(need(a, "citation", ap)?)? + )); + } + Ok(out) +} + +/// Step 08's OptiType check: (gene, line) per class I gene, or one +/// (`""`, line) with the reason the callers were not compared. +pub fn hla_optitype_lines(ot: &Value) -> Result, Malformed> { + let p = "sections.hla.data.optitype"; + let genes = need(ot, "genes", p)?; + if !truthy(genes) { + return Ok(vec![( + String::new(), + format!("not compared: {}", py(need(ot, "reason", p)?)?), + )]); + } + let gp = "sections.hla.data.optitype.genes[]"; + let mut out = Vec::new(); + for g in items(Some(genes), p)? { + let agree = need(g, "agree", gp)?; + let word = match agree { + Value::Str(s) if s == "yes" => "agree".to_owned(), + Value::Str(s) if s == "no" => "disagree".to_owned(), + _ => or_text(Some(agree), "not compared")?, + }; + let mut line = format!( + "{word}: T1K {}, OptiType {}", + py(need(g, "t1k", gp)?)?, + py(need(g, "optitype", gp)?)? + ); + let note = need(g, "note", gp)?; + if !(is_str(Some(note), "") || is_str(Some(note), "-")) { + line += &format!(" ({})", py(note)?); + } + out.push((py(need(g, "gene", gp)?)?, line)); + } + Ok(out) +} + +/// Step 08's KIR genes: (gene, alleles) per typed gene, then (`""`, line) +/// for the genes with no allele, or the reason nothing was typed. +pub fn kir_lines(kir: &Value) -> Result, Malformed> { + let p = "sections.hla.data.kir"; + let reason = need(kir, "reason", p)?; + if truthy(reason) { + return Ok(vec![(String::new(), py(reason)?)]); + } + let gp = "sections.hla.data.kir.typed[]"; + let mut out = Vec::new(); + for g in items(Some(need(kir, "typed", p)?), p)? { + let alleles = items(Some(need(g, "alleles", gp)?), gp)?; + let quals = items(Some(need(g, "quality", gp)?), gp)?; + let mut parts = Vec::new(); + for (a, q) in alleles.iter().zip(quals) { + let mut t = text_of(a, "a KIR allele")?; + if !is_str(Some(q), "") { + t += &format!(" (quality {})", py(q)?); + } + parts.push(t); + } + out.push((py(need(g, "gene", gp)?)?, parts.join(" / "))); + } + let not_called = need(kir, "not_called", p)?; + if truthy(not_called) { + let names: Result, Malformed> = items(Some(not_called), p)? + .iter() + .map(|x| text_of(x, "a KIR gene")) + .collect(); + out.push(( + String::new(), + format!( + "No allele called (absent, or too few reads): {}", + names?.join(", ") + ), + )); + } + Ok(out) +} + +/// ` GENE line`, or the line alone without a gene. +fn gene_line(g: &str, line: &str) -> String { + if g.is_empty() { + format!(" {line}") + } else { + format!(" {} {line}", pycompat::fmt::ljust(g, 8)) + } +} + +fn database(s: &Summary) -> Result { + or_text(Some(s.need2("databases", "hla_database")?), "unknown") +} + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "hla", Some("HLA Typing (T1K)"))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if head.ran { + let sec = s.section("hla"); + for l in items(Some(sec.need("loci")?), "sections.hla.data.loci")? { + w.push(format!( + " {} {}", + left( + need(l, "gene", "sections.hla.data.loci[]")?, + 8, + "an HLA gene" + )?, + hla_text(l)? + )); + } + let derived = sec.get("derived")?; + w.extend( + hla_derived_lines(derived)? + .into_iter() + .map(|l| format!(" {l}")), + ); + if derived.is_some_and(truthy) { + w.push(format!(" {HLA_DERIVED_NOTE}")); + } + w.push(format!(" HLA database: {}", database(s)?)); + w.push(format!(" {HLA_NOTE}")); + if let Some(ot) = sec.get("optitype")?.filter(|v| **v != Value::Null) { + w.push(" OptiType check (HLA_QC=true, two fields):".to_owned()); + for (g, line) in hla_optitype_lines(ot)? { + w.push(gene_line(&g, &line)); + } + } + if let Some(kir) = sec.get("kir")?.filter(|v| **v != Value::Null) { + w.push(" KIR genes (T1K, KIR=true):".to_owned()); + for (g, line) in kir_lines(kir)? { + w.push(gene_line(&g, &line)); + } + w.push(format!(" {KIR_NOTE}")); + } + w.push(String::new()); + } + Ok(w) +} + +fn stat(label: &str, value: String) -> Line { + Line::Stat(label.to_owned(), Inline::Text(value)) +} + +/// The HTML report's card. +pub fn html(s: &Summary) -> Result { + let sec = s.section("hla"); + let mut body = Vec::new(); + let mut caveats = Vec::new(); + for l in items(sec.get("loci")?, "sections.hla.data.loci")? { + body.push(stat( + &py(need(l, "gene", "sections.hla.data.loci[]")?)?, + hla_text(l)?, + )); + } + let derived = sec.get("derived")?.filter(|v| truthy(v)); + if derived.is_some() { + body.extend(hla_derived_lines(derived)?.into_iter().map(Line::Note)); + caveats.push(HLA_DERIVED_NOTE); + } + body.push(stat("HLA database", database(s)?)); + caveats.push(HLA_NOTE); + if let Some(ot) = sec.get("optitype")?.filter(|v| **v != Value::Null) { + for (g, line) in hla_optitype_lines(ot)? { + body.push(if g.is_empty() { + stat("OptiType check", line) + } else { + stat(&format!("{g}: T1K vs OptiType"), line) + }); + } + } + if let Some(kir) = sec.get("kir")?.filter(|v| **v != Value::Null) { + for (g, line) in kir_lines(kir)? { + body.push(if g.is_empty() { + Line::Note(line) + } else { + stat(&g, line) + }); + } + caveats.push(KIR_NOTE); + } + let card = html::card( + &sec, + Card { + title: "HLA Typing".into(), + caveats, + body, + ..Card::default() + }, + )?; + Ok(crate::model::HtmlPart { + cards: vec![card], + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/mito.rs b/rust/crates/og-render/src/sections/mito.rs index e6f32d585..2d66d99b0 100644 --- a/rust/crates/og-render/src/sections/mito.rs +++ b/rust/crates/og-render/src/sections/mito.rs @@ -1 +1,478 @@ -//! The report section `mito`. Empty until its unit of the 1.0.0 plan lands. +//! Mitochondrial variants (step 20): the Mutect2 chrM calls with the D-loop +//! poly-C tracts, ClinVar and gnomAD on each call, the confirmed disease +//! variants of assets/mito_confirmed.tsv, eKLIPse's large deletions and the +//! mtDNA copies per cell. + +use pycompat::json::Value; + +use crate::html; +use crate::model::{ + Card, Colour, HtmlPart, Inline, Layout, Line, Malformed, Section, Summary, Table, get, is_str, + need, num, py, truthy, +}; +use crate::notes::{MITO_COPIES_NOTE, MITO_DELETION_NOTE, MITO_NOTE, MITO_POLYC_NOTE}; +use crate::sections::pgx::{items, or_none, or_text, text_of}; +use crate::text; + +/// `"."` for a value that is missing, empty or `.`, else its text. +fn val(x: Option<&Value>) -> Result { + match x { + None | Some(Value::Null) => Ok(".".into()), + Some(Value::Str(s)) if s.is_empty() || s == "." => Ok(".".into()), + Some(v) => py(v), + } +} + +/// One annotated chrM call as a line. +pub fn mito_variant_text(v: &Value) -> Result { + let p = "sections.mito.data.annotation.variants[]"; + let g = |k: &str| get(v, k, p); + let cv = val(g("clinvar_significance")?)?; + let cv = if cv == "." { + "not in ClinVar".to_owned() + } else { + format!( + "ClinVar {cv} ({}; {})", + val(g("clinvar_review")?)?, + val(g("clinvar_condition")?)? + ) + }; + let gn = if val(g("gnomad_af_hom")?)? == "." { + "not in gnomAD".to_owned() + } else { + format!( + "gnomAD AF hom {}, het {}", + py(need(v, "gnomad_af_hom", p)?)?, + val(g("gnomad_af_het")?)? + ) + }; + let polyc = if g("polyc")?.is_some_and(truthy) { + " poly-C tract" + } else { + "" + }; + Ok(format!( + "chrM:{} {}>{} AF {} {cv} {gn}{polyc}", + py(need(v, "pos", p)?)?, + py(need(v, "ref", p)?)?, + py(need(v, "alt", p)?)?, + val(g("af")?)? + )) +} + +/// The annotation block, `{}` when the summary has none. +fn annotation<'a>(d: &Section<'a>) -> Result, Malformed> { + Ok(d.get("annotation")?.filter(|v| truthy(v))) +} + +/// Step 20's lines under the counts, the same in both reports. +pub fn mito_lines(d: &Section<'_>) -> Result, Malformed> { + let mut lines = Vec::new(); + let pc = items( + d.get("polyc_heteroplasmic")?.filter(|v| truthy(v)), + "sections.mito.data.polyc_heteroplasmic", + )?; + if d.get("polyc_heteroplasmic")?.is_some() { + let tracts: Result, Malformed> = items( + d.get("polyc_tracts")?.filter(|v| truthy(v)), + "sections.mito.data.polyc_tracts", + )? + .iter() + .map(|t| text_of(t, "a poly-C tract")) + .collect(); + lines.push(format!( + "Length heteroplasmy in the D-loop poly-C tracts ({}; common, usually technical): {}", + tracts?.join(", "), + pc.len() + )); + for x in pc { + lines.push(format!(" {}", polyc_text(x)?)); + } + } + if let Some(a) = annotation(d)? { + let ap = "sections.mito.data.annotation"; + if let Some(src) = get(a, "source", ap)?.filter(|v| truthy(v)) { + lines.push(format!( + "Annotation ({}): ClinVar {}; gnomAD {}", + py(src)?, + or_text(get(a, "clinvar", ap)?, "unknown")?, + or_text(get(a, "gnomad", ap)?, "unknown")? + )); + lines.push(format!( + "ClinVar pathogenic or likely pathogenic: {}", + get(a, "clinvar_plp", ap)?.map_or(Ok("0".into()), py)? + )); + let vs = get(a, "variants", ap)?.filter(|v| truthy(v)); + if vs.is_some() { + lines.push( + "Heteroplasmic, or in ClinVar with a class other than benign:".to_owned(), + ); + for v in items(vs, ap)? { + lines.push(format!(" {}", mito_variant_text(v)?)); + } + } + } else if let Some(n) = get(a, "note", ap)?.filter(|v| truthy(v)) { + lines.push(format!("ClinVar and gnomAD: not annotated ({})", py(n)?)); + } + } + lines.extend(mito_extended_lines(d)?); + Ok(lines) +} + +/// One poly-C length heteroplasmy: `variant AF af`. +fn polyc_text(x: &Value) -> Result { + let p = "sections.mito.data.polyc_heteroplasmic[]"; + Ok(format!( + "{} AF {}", + py(need(x, "variant", p)?)?, + py(need(x, "af", p)?)? + )) +} + +/// One confirmed disease variant. +pub fn mito_confirmed_text(v: &Value) -> Result { + let p = "sections.mito.data.confirmed[]"; + let pmids = text_of(need(v, "pmids", p)?, "a confirmed variant's PubMed ids")?; + Ok(format!( + "{} ({}) AF {} filter {} {}; ClinVar {}; PMID {}. {}", + py(need(v, "hgvs", p)?)?, + py(need(v, "gene", p)?)?, + or_text(get(v, "af", p)?, ".")?, + or_text(get(v, "filter", p)?, ".")?, + py(need(v, "disease", p)?)?, + py(need(v, "clinvar_id", p)?)?, + pmids.replace(';', ", "), + py(need(v, "note", p)?)? + )) +} + +/// `x + d` as Python adds a number to an int or a float. +fn plus(x: &Value, d: i128) -> Result { + match x { + Value::Int(i) => Ok((i + d).to_string()), + Value::Bool(b) => Ok((i128::from(*b) + d).to_string()), + Value::Float(f) => py(&Value::Float(f + d as f64)), + _ => Err(Malformed("a deletion breakpoint is not a number".into())), + } +} + +/// One large deletion: its span (across the origin when the 3' breakpoint +/// comes first), its level, and the confirmed deletion it matches. +pub fn mito_deletion_text(r: &Value) -> Result { + let p = "sections.mito.data.deletions.listed[]"; + let (b5, b3) = (need(r, "bkp5", p)?, need(r, "bkp3", p)?); + let (n5, n3) = match (num(Some(b5)), num(Some(b3))) { + (Some(a), Some(b)) => (a, b), + _ => return Err(Malformed("a deletion breakpoint is not a number".into())), + }; + let span = if n3 > n5 { + format!( + "m.{}_{}del ({} bp)", + plus(b5, 1)?, + plus(b3, -1)?, + py(need(r, "length", p)?)? + ) + } else { + format!( + "m.{} to m.{} across the origin ({} bp)", + plus(b5, 1)?, + plus(b3, -1)?, + or_none(get(r, "length", p)?)? + ) + }; + let mut out = format!("{span} level {}%", py(need(r, "level_pct", p)?)?); + if let Some(k) = get(r, "known", p)?.filter(|v| truthy(v)) { + let kp = "sections.mito.data.deletions.listed[].known"; + let pmids = text_of(need(k, "pmids", kp)?, "a deletion's PubMed ids")?; + out += &format!( + " {}; ClinVar {} ({}); PMID {}", + py(need(k, "disease", kp)?)?, + py(need(k, "clinvar_id", kp)?)?, + py(need(k, "hgvs", kp)?)?, + pmids.replace(';', ", ") + ); + } + Ok(out) +} + +/// The copy-number line: the copies per cell, or why there are none. +pub fn mito_copies_text(c: &Value) -> Result { + let p = "sections.mito.data.copies"; + if is_str(get(c, "status", p)?, "called") { + return Ok(format!( + "mtDNA copy number: {} per cell (chrM depth {} times the autosomal depth)", + py(need(c, "mtdna_copies", p)?)?, + py(need(c, "depth_ratio", p)?)? + )); + } + let reason = get(c, "reason", p)?; + let why = if is_str(reason, "low_autosomal_depth") { + "too few autosomal reads: a sliced BAM, a panel or a shallow genome".to_owned() + } else if is_str(reason, "no_chrM_reads") { + "no chrM reads in the BAM".to_owned() + } else { + or_text(reason, "unknown")? + }; + Ok(format!("mtDNA copy number: not assessed ({why})")) +} + +/// The listed deletions and how many of them the confirmed list names. +fn listed(dl: &Value) -> Result<(&[Value], usize), Malformed> { + let rows = items( + Some(need(dl, "listed", "sections.mito.data.deletions")?), + "sections.mito.data.deletions.listed", + )?; + let mut named = 0; + for r in rows { + if get(r, "known", "sections.mito.data.deletions.listed[]")?.is_some_and(truthy) { + named += 1; + } + } + Ok((rows, named)) +} + +/// Step 20's confirmed variants, large deletions and copy number, the same +/// in both reports. +pub fn mito_extended_lines(d: &Section<'_>) -> Result, Malformed> { + let mut lines = Vec::new(); + if let Some(conf) = d.get("confirmed")? { + let conf = items(Some(conf), "sections.mito.data.confirmed")?; + lines.push(format!( + "Confirmed disease variants (assets/mito_confirmed.tsv): {}", + conf.len() + )); + for v in conf { + lines.push(format!(" {}", mito_confirmed_text(v)?)); + } + } + if let Some(n) = d.get("confirmed_note")?.filter(|v| truthy(v)) { + lines.push(format!( + "Confirmed disease variants: not checked ({})", + py(n)? + )); + } + if let Some(dl) = d.get("deletions")?.filter(|v| truthy(v)) { + let (rows, named) = listed(dl)?; + let dp = "sections.mito.data.deletions"; + lines.push(format!( + "Large deletions (eKLIPse): {}, of them {named} named in assets/mito_confirmed.tsv; listed: named \ + ones and any at {}% or more", + py(need(dl, "total", dp)?)?, + py(need(dl, "list_floor_pct", dp)?)? + )); + for r in rows { + lines.push(format!(" {}", mito_deletion_text(r)?)); + } + lines.push(MITO_DELETION_NOTE.to_owned()); + } else if let Some(n) = d.get("deletions_note")?.filter(|v| truthy(v)) { + lines.push(format!("Large deletions: not looked for ({})", py(n)?)); + } + if let Some(c) = d.get("copies")?.filter(|v| truthy(v)) { + lines.push(mito_copies_text(c)?); + if is_str(get(c, "status", "sections.mito.data.copies")?, "called") { + lines.push(MITO_COPIES_NOTE.to_owned()); + } + } else if let Some(n) = d.get("copies_note")?.filter(|v| truthy(v)) { + lines.push(format!("mtDNA copy number: not counted ({})", py(n)?)); + } + Ok(lines) +} + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "mito", Some("Mitochondrial Variants (Mutect2)"))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if head.ran { + let d = s.section("mito"); + w.push(format!(" PASS variants: {}", py(d.need("pass")?)?)); + w.push(format!( + " Heteroplasmic (AF 0.05 to 0.95, outside the D-loop poly-C tracts): {}", + py(d.need("heteroplasmic")?)? + )); + w.extend(mito_lines(&d)?.into_iter().map(|l| format!(" {l}"))); + if d.get("polyc_heteroplasmic")?.is_some_and(truthy) { + w.push(format!(" {MITO_POLYC_NOTE}")); + } + w.push(format!(" {MITO_NOTE}")); + w.push(String::new()); + } + Ok(w) +} + +fn stat(label: &str, value: Inline) -> Line { + Line::Stat(label.to_owned(), value) +} + +fn count(n: String, any: bool) -> Inline { + Inline::Badge(n, if any { Colour::Yellow } else { Colour::Gray }) +} + +/// The HTML report's card: full width when it holds the table of annotated +/// calls. +pub fn html(s: &Summary) -> Result { + let mi = s.section("mito"); + let empty = Value::Object(Default::default()); + let ma = annotation(&mi)?.unwrap_or(&empty); + let ap = "sections.mito.data.annotation"; + let pc = items( + mi.get("polyc_heteroplasmic")?.filter(|v| truthy(v)), + "sections.mito.data.polyc_heteroplasmic", + )?; + let mut caveats = Vec::new(); + let mut body = vec![ + stat( + "chrM variants (PASS)", + Inline::Text(or_none(mi.get("pass")?)?), + ), + stat( + "Heteroplasmic (AF 0.05 to 0.95, outside the D-loop poly-C tracts)", + Inline::Text(or_none(mi.get("heteroplasmic")?)?), + ), + stat( + "Length heteroplasmy in the D-loop poly-C tracts", + Inline::Text(pc.len().to_string()), + ), + ]; + for x in pc { + body.push(Line::Note(polyc_text(x)?)); + } + if !pc.is_empty() { + caveats.push(MITO_POLYC_NOTE); + } + let variants = get(ma, "variants", ap)?.filter(|v| truthy(v)); + if get(ma, "source", ap)?.is_some_and(truthy) { + let plp = get(ma, "clinvar_plp", ap)?; + body.push(stat( + "ClinVar pathogenic or likely pathogenic", + count(plp.map_or(Ok("0".into()), py)?, plp.is_some_and(truthy)), + )); + if let Some(vs) = variants { + let vp = "sections.mito.data.annotation.variants[]"; + let mut rows = Vec::new(); + let mut at = Vec::new(); + for v in items(Some(vs), ap)? { + let g = |k: &str| -> Result { or_none(get(v, k, vp)?) }; + let pos = need(v, "pos", vp)?; + rows.push(vec![ + format!( + "chrM:{} {}>{}", + py(pos)?, + py(need(v, "ref", vp)?)?, + py(need(v, "alt", vp)?)? + ), + g("af")?, + g("clinvar_significance")?, + g("clinvar_review")?, + g("clinvar_condition")?, + g("gnomad_af_hom")?, + g("gnomad_af_het")?, + if get(v, "polyc", vp)?.is_some_and(truthy) { + "yes".into() + } else { + String::new() + }, + ]); + at.push(if truthy(pos) { + Some(format!("chrM:{}", py(pos)?)) + } else { + None + }); + } + body.push(Line::Table(Table { + head: [ + "Variant", + "AF", + "ClinVar", + "Review", + "Condition", + "gnomAD AF hom", + "gnomAD AF het", + "Poly-C tract", + ] + .map(str::to_owned) + .to_vec(), + rows, + viewer: Some(at), + layout: Layout::OneLine, + })); + body.push(Line::ViewerNote); + } + body.push(Line::Note(format!( + "ClinVar {}; gnomAD {}", + or_text(get(ma, "clinvar", ap)?, "unknown")?, + or_text(get(ma, "gnomad", ap)?, "unknown")? + ))); + } else if let Some(n) = get(ma, "note", ap)?.filter(|v| truthy(v)) { + body.push(Line::Note(format!( + "ClinVar and gnomAD: not annotated ({})", + py(n)? + ))); + } + if let Some(conf) = mi.get("confirmed")? { + let conf = items(Some(conf), "sections.mito.data.confirmed")?; + body.push(stat( + "Confirmed disease variants", + count(conf.len().to_string(), !conf.is_empty()), + )); + for v in conf { + body.push(Line::Note(mito_confirmed_text(v)?)); + } + } + if let Some(dl) = mi.get("deletions")?.filter(|v| truthy(v)) { + let (rows, named) = listed(dl)?; + body.push(stat( + "Large deletions (eKLIPse)", + Inline::Text(py(need(dl, "total", "sections.mito.data.deletions")?)?), + )); + body.push(stat( + "Named in the confirmed list", + count(named.to_string(), named > 0), + )); + for r in rows { + body.push(Line::Note(mito_deletion_text(r)?)); + } + caveats.push(MITO_DELETION_NOTE); + } else if let Some(n) = mi.get("deletions_note")?.filter(|v| truthy(v)) { + body.push(Line::Note(format!( + "Large deletions: not looked for ({})", + py(n)? + ))); + } + if let Some(mc) = mi.get("copies")?.filter(|v| truthy(v)) { + let called = is_str(get(mc, "status", "sections.mito.data.copies")?, "called"); + body.push(stat( + "mtDNA copies per cell", + Inline::Text(if called { + py(need(mc, "mtdna_copies", "sections.mito.data.copies")?)? + } else { + "not assessed".into() + }), + )); + body.push(Line::Note(mito_copies_text(mc)?)); + if called { + caveats.push(MITO_COPIES_NOTE); + } + } else if let Some(n) = mi.get("copies_note")?.filter(|v| truthy(v)) { + body.push(Line::Note(format!( + "mtDNA copy number: not counted ({})", + py(n)? + ))); + } + caveats.push(MITO_NOTE); + let card = html::card( + &mi, + Card { + title: "Mitochondrial Analysis".into(), + full: variants.is_some(), + caveats, + body, + ..Card::default() + }, + )?; + Ok(HtmlPart { + cards: vec![card], + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/pgx.rs b/rust/crates/og-render/src/sections/pgx.rs index 0591b1e47..18f5ae8a9 100644 --- a/rust/crates/og-render/src/sections/pgx.rs +++ b/rust/crates/og-render/src/sections/pgx.rs @@ -1 +1,708 @@ -//! The report section `pgx`. Empty until its unit of the 1.0.0 plan lands. +//! The medicines sections: PharmCAT (step 07), the CPIC recommendations +//! (step 27), and pypgx (step 32) with Cyrius (step 21) and step 36's +//! CYP2D6 verdict, which both reports show in one section (`cyp2d6`). + +use pycompat::json::Value; +use pycompat::text::{lower, slice, upper}; + +use crate::model::{ + Card, Colour, HtmlPart, Inline, Layout, Line, Malformed, Section, State, Summary, Table, + contains, get, is_str, need, py, truthy, +}; +use crate::notes::{CPIC_NOTE, PGX_CALLERS_NOTE, PHARMCAT_NOTE, stale_note}; +use crate::{html, text}; + +/// `f"{v: Result { + let s = match v { + Value::Str(_) | Value::Int(_) | Value::Float(_) => py(v)?, + Value::Bool(b) => u8::from(*b).to_string(), + _ => return Err(Malformed(format!("{path} cannot be formatted"))), + }; + Ok(pycompat::fmt::ljust(&s, width)) +} + +/// `v` as a text, or the path of a value that is not one (where Python +/// joins or concatenates strings). +pub fn text_of(v: &Value, path: &str) -> Result { + match v { + Value::Str(s) => Ok(s.clone()), + _ => Err(Malformed(format!("{path} is not a text"))), + } +} + +/// `v or default`, as text. +pub fn or_text(v: Option<&Value>, default: &str) -> Result { + match v.filter(|v| truthy(v)) { + Some(v) => py(v), + None => Ok(default.to_owned()), + } +} + +/// `d.get(key)` as Python's f-string prints it: `None` for a missing key. +pub fn or_none(v: Option<&Value>) -> Result { + v.map_or(Ok("None".to_owned()), py) +} + +/// The items of a list value (`[]` for a missing key or `null`). +pub fn items<'a>(v: Option<&'a Value>, path: &str) -> Result<&'a [Value], Malformed> { + match v { + None | Some(Value::Null) => Ok(&[]), + Some(Value::Array(a)) => Ok(a), + Some(_) => Err(Malformed(format!("{path} is not a list"))), + } +} + +/// `str.capitalize()`: the first character upper case, the rest lower. +fn capitalize(s: &str) -> String { + let mut c = s.chars(); + match c.next() { + Some(f) => upper(&f.to_string()) + &lower(c.as_str()), + None => String::new(), + } +} + +/// `os.path.join(os.path.dirname(path), name)`, on `/` paths. +fn beside(path: &str, name: &str) -> String { + let head = path.rfind('/').map_or("", |i| &path[..=i]); + let dir = if !head.is_empty() && head.chars().any(|c| c != '/') { + head.trim_end_matches('/') + } else { + head + }; + if name.starts_with('/') { + name.to_owned() + } else if dir.is_empty() || dir.ends_with('/') { + format!("{dir}{name}") + } else { + format!("{dir}/{name}") + } +} + +fn stat(label: &str, value: Inline) -> Line { + Line::Stat(label.to_owned(), value) +} + +fn sample(s: &Summary) -> Result { + text_of(s.need("sample")?, "summary.sample") +} + +/// `_cpic_recommendations.txt` beside the CPIC report. +fn cpic_file(s: &Summary, sec: &Section<'_>) -> Result { + let src = sec + .source + .ok_or_else(|| Malformed("sections.cpic has no source".into()))?; + let src = text_of(src, "sections.cpic.source")?; + Ok(beside( + &src, + &format!("{}_cpic_recommendations.txt", sample(s)?), + )) +} + +/// The step ran and the summary names its file. +fn ran_with_source(sec: &Section<'_>) -> bool { + sec.state.ran() && sec.source.is_some_and(truthy) +} + +// --- PharmCAT ---------------------------------------------------------------------- + +/// The text report's PharmCAT block. +pub fn pharmcat_text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "pharmcat", Some("Pharmacogenomics (PharmCAT)"))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if head.ran { + let sec = s.section("pharmcat"); + w.push(format!( + " Report: {} (PharmCAT {})", + or_none(sec.source)?, + or_text(sec.get("version")?, "version unknown")? + )); + if sec.get("html_report")?.is_some_and(truthy) { + w.push(" HTML report beside it (open in a browser for full details)".to_owned()); + } + w.push(format!(" {PHARMCAT_NOTE}")); + w.push(String::new()); + } + Ok(w) +} + +/// The HTML report's PharmCAT card, and the link to PharmCAT's own HTML +/// report beside it. +pub fn pharmcat_html(s: &Summary) -> Result { + let sec = s.section("pharmcat"); + // Never green: a report that was written is not a finding. + let mut body = vec![stat( + "PharmCAT report", + if sec.state.ran() { + Inline::Badge("Complete".into(), Colour::State) + } else { + Inline::Badge("Not run".into(), Colour::Gray) + }, + )]; + if let Some(v) = sec.get("version")?.filter(|v| truthy(v)) { + body.push(stat( + "PharmCAT version", + Inline::Text(text_of(v, "sections.pharmcat.data.version")?), + )); + } + let pg = s.section("pypgx"); + if pg.state.ran() && contains(pg.data, "comparison") { + let c = need( + pg.need("comparison")?, + "conflicts", + "sections.pypgx.data.comparison", + )?; + body.push(stat("PharmCAT vs pypgx conflicts", Inline::Text(py(c)?))); + } + let card = html::card( + &sec, + Card { + title: "Pharmacogenomics".into(), + caveats: vec![PHARMCAT_NOTE], + body, + ..Card::default() + }, + )?; + let mut files = Vec::new(); + if ran_with_source(&sec) && sec.get("html_report")?.is_some_and(truthy) { + let src = text_of(sec.source.expect("checked"), "sections.pharmcat.source")?; + files.push(( + format!("{}.html", slice(&src, None, Some(-5))), + "PharmCAT HTML report".to_owned(), + )); + } + Ok(HtmlPart { + cards: vec![card], + files, + }) +} + +// --- CPIC -------------------------------------------------------------------------- + +/// The genes CPIC lists as non-normal or with more than one possible +/// result: (gene, diplotype, phenotype). +fn unusual<'a>(sec: &Section<'a>) -> Result, Malformed> { + let mut out = Vec::new(); + for (i, g) in items(Some(sec.need("genes")?), "sections.cpic.data.genes")? + .iter() + .enumerate() + { + let p = format!("sections.cpic.data.genes[{i}]"); + let st = need(g, "status", &p)?; + if is_str(Some(st), "non-normal") || is_str(Some(st), "ambiguous") { + out.push([ + need(g, "gene", &p)?, + need(g, "diplotype", &p)?, + need(g, "phenotype", &p)?, + ]); + } + } + Ok(out) +} + +/// The text report's CPIC block. +pub fn cpic_text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "cpic", Some("CPIC Drug-Gene Recommendations"))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if head.ran { + let sec = s.section("cpic"); + if truthy(sec.need("parse_failed")?) { + w.push(" The PharmCAT report could not be parsed: see the CPIC report.".to_owned()); + } + let or0 = |k: &str| -> Result { sec.get(k)?.map_or(Ok("0".into()), py) }; + w.push(format!( + " Genes with a non-normal phenotype: {} More than one possible result: {} Not called: {} \ + Called without a function phenotype (no drug guidance): {}", + py(sec.need("non_normal")?)?, + or0("ambiguous")?, + py(sec.need("not_called")?)?, + or0("unclassified")? + )); + for [g, d, p] in unusual(&sec)? { + w.push(format!( + " {} {} {}", + left(g, 10, "a CPIC gene")?, + left(d, 28, "a CPIC diplotype")?, + py(p)? + )); + } + for warn in items(Some(sec.need("warnings")?), "sections.cpic.data.warnings")? { + w.push(format!(" WARNING: {}", py(warn)?)); + } + w.push(format!(" Drugs per gene: {}", cpic_file(s, &sec)?)); + w.push(format!(" {CPIC_NOTE}")); + w.push(String::new()); + } + Ok(w) +} + +/// The HTML report's CPIC cards: the counts, then the genes with a +/// non-normal or unresolved phenotype; and the link to the recommendations +/// file. +pub fn cpic_html(s: &Summary) -> Result { + let sec = s.section("cpic"); + let smp = sample(s)?; + let mut body = vec![stat( + "Status", + if sec.state == State::Stale { + Inline::Badge("Stale".into(), Colour::Yellow) + } else { + Inline::Badge("Complete".into(), Colour::Green) + }, + )]; + if sec.get("parse_failed")?.is_some_and(truthy) { + body.push(stat( + "PharmCAT report", + Inline::Badge("could not be parsed".into(), Colour::Red), + )); + } else { + for (label, k) in [ + ("Genes with a non-normal phenotype", "non_normal"), + ("Genes with more than one possible result", "ambiguous"), + ("Genes not called", "not_called"), + ( + "Genes called without a function phenotype (no drug guidance)", + "unclassified", + ), + ] { + let v = sec.get(k)?.map_or(Ok("0".into()), py)?; + body.push(stat(label, Inline::Text(v))); + } + } + body.push(stat( + "Tip", + Inline::Quiet(format!( + "Recommendations per gene: cpic/{smp}_cpic_recommendations.txt" + )), + )); + let mut cards = vec![html::card( + &sec, + Card { + title: "CPIC Drug Recommendations".into(), + caveats: vec![CPIC_NOTE], + body, + ..Card::default() + }, + )?]; + if sec.state.ran() + && (truthy(sec.need("non_normal")?) || sec.get("ambiguous")?.is_some_and(truthy)) + { + let mut rows = Vec::new(); + for [g, d, p] in unusual(&sec)? { + rows.push(vec![ + text_of(g, "a CPIC gene")?, + text_of(d, "a CPIC diplotype")?, + text_of(p, "a CPIC phenotype")?, + ]); + } + let mut body = vec![Line::Table(Table { + head: vec!["Gene".into(), "Diplotype".into(), "Phenotype".into()], + rows, + viewer: None, + layout: Layout::RowLines, + })]; + for x in items(Some(sec.need("warnings")?), "sections.cpic.data.warnings")? { + body.push(Line::Warn(format!( + "WARNING: {}", + text_of(x, "a CPIC warning")? + ))); + } + body.push(Line::Para(format!( + "Drugs for each gene: cpic/{smp}_cpic_recommendations.txt" + ))); + cards.push(html::card( + &sec, + Card { + title: "Genes With a Non-Normal or Unresolved Phenotype (CPIC)".into(), + full: true, + body, + ..Card::default() + }, + )?); + } + let mut files = Vec::new(); + if ran_with_source(&sec) { + files.push(( + cpic_file(s, &sec)?, + "CPIC recommendations per gene (text)".to_owned(), + )); + } + Ok(HtmlPart { cards, files }) +} + +// --- pypgx, Cyrius and CYP2D6 ------------------------------------------------------ + +/// Why step 32 could not call a gene on this reference. +fn not_callable_why(g: &Value) -> &'static str { + if is_str(Some(g), "GSTT1") { + "GRCh38 has it only on the ALT contig chr22_KI270879v1_alt, which a reference without ALT \ + contigs lacks" + } else { + "see docs/32-pypgx.md" + } +} + +/// Step 32's lines below its counts, the same in both reports: the genes +/// this reference cannot hold, the calls resting on a low-GQ variant, and +/// the CYP2D6 depth check with its MAPQ shares. +pub fn pypgx_lines(d: &Section<'_>) -> Result, Malformed> { + let mut lines = Vec::new(); + for g in items(d.get("not_callable")?, "sections.pypgx.data.not_callable")? { + lines.push(format!( + "Not assessable on this reference: {} ({})", + py(g)?, + not_callable_why(g) + )); + } + for (i, x) in items(d.get("low_gq")?, "sections.pypgx.data.low_gq")? + .iter() + .enumerate() + { + let p = format!("sections.pypgx.data.low_gq[{i}]"); + lines.push(format!( + "Low-confidence: {} {} rests on {}: check the reads", + py(need(x, "gene", &p)?)?, + py(need(x, "diplotype", &p)?)?, + or_text(Some(need(x, "weakest", &p)?), "a low-GQ variant")? + )); + } + if let Some(dc) = d.get("depth_check")?.filter(|v| truthy(v)) { + let p = "sections.pypgx.data.depth_check"; + let share = |k: &str| -> Result { + get(dc, k, p)?.map_or(Ok("not measured".into()), py) + }; + lines.push(format!( + "CYP2D6 depth check: {}; share of the depth at MAPQ >= 1 / >= 20: Cyrius's region with REP6 {} / {}, \ + gene body without REP6 {} / {}", + or_text(get(dc, "status", p)?, "unreadable")?, + share("gene_mapq1_share")?, + share("gene_mapq20_share")?, + share("body_mapq1_share")?, + share("body_mapq20_share")? + )); + } + Ok(lines) +} + +/// (gene, copy number) rows, the same in both reports: pypgx's call from +/// read depth for each BAM-mode gene of assets/pgx_genes.tsv. +pub fn pgx_copy_number(s: &Summary) -> Result, Malformed> { + let pg = s.section("pypgx"); + match pg.get("cnv_call")?.filter(|v| truthy(v)) { + None => Ok(Vec::new()), + Some(Value::Object(o)) => o + .iter() + .map(|(g, v)| Ok((g.clone(), format!("{} (pypgx, read depth)", py(v)?)))) + .collect(), + Some(_) => Err(Malformed( + "sections.pypgx.data.cnv_call is not an object".into(), + )), + } +} + +/// The summary's CYP2D6 block (calls, agreement, step 36's consensus, the +/// probable call), or `None` for a summary without one (a part collected +/// with `--only`), which shows no CYP2D6 across the callers. +fn block(s: &Summary) -> Result, Malformed> { + match s.get("cyp2d6") { + None | Some(Value::Null) => Ok(None), + Some(c @ Value::Object(_)) => Ok(Some(c)), + Some(_) => Err(Malformed("summary.cyp2d6 is not an object".into())), + } +} + +/// The callers' CYP2D6 calls, in the summary's order. +fn calls(c: &Value) -> Result, Malformed> { + match need(c, "calls", "summary.cyp2d6")? { + Value::Object(o) => Ok(o.iter().collect()), + _ => Err(Malformed("summary.cyp2d6.calls is not an object".into())), + } +} + +/// The column label of a caller: PharmCAT calls no CYP2D6 from a VCF, it +/// shows the call step 36 passed to it. +fn caller_label(k: &str) -> &str { + if k == "PharmCAT" { + "PharmCAT (step 36's call)" + } else { + k + } +} + +/// Step 36's CYP2D6 verdict as one line, or `""` without one. +pub fn cyp2d6_verdict(c: &Value) -> Result { + let Some(v) = get(c, "consensus", "summary.cyp2d6")?.filter(|v| truthy(v)) else { + return Ok(String::new()); + }; + let p = "summary.cyp2d6.consensus"; + let passed = get(v, "passed_to_pharmcat", p)?.is_some_and(truthy); + let where_ = if passed { + "passed to PharmCAT" + } else { + "not passed to PharmCAT" + }; + Ok(format!( + "{}, {where_} ({})", + or_text(get(v, "result", p)?, "?")?, + or_text(get(v, "reason", p)?, "no reason given")? + )) +} + +/// The probable CYP2D6 call as one line. +fn probable_text(p: &Value) -> Result { + let path = "summary.cyp2d6.probable"; + let pheno = match get(p, "phenotype", path)?.filter(|v| truthy(v)) { + Some(x) => format!(", {}", py(x)?), + None => String::new(), + }; + Ok(format!( + "{}{pheno}; not passed to PharmCAT, so no drug guidance rests on it", + or_none(get(p, "diplotype", path)?)? + )) +} + +/// The probable call's label and line, when the summary has one. +fn probable(c: &Value) -> Result, Malformed> { + let Some(p) = get(c, "probable", "summary.cyp2d6")?.filter(|v| truthy(v)) else { + return Ok(None); + }; + let label = text_of( + need(p, "label", "summary.cyp2d6.probable")?, + "summary.cyp2d6.probable.label", + )?; + Ok(Some((capitalize(&label), probable_text(p)?))) +} + +/// The text report's block of the section: pypgx, its copy numbers, then +/// CYP2D6 across the callers. +pub fn cyp2d6_text(s: &Summary) -> Result, Malformed> { + let mut w = Vec::new(); + if let Some(head) = text::head(s, "pypgx", Some("pypgx Pharmacogenomics"))? { + w.extend(head.lines); + if head.ran { + let d = s.section("pypgx"); + w.push(format!( + " Genes called: {}/{}", + py(d.need("genes_called")?)?, + py(d.need("genes_total")?)? + )); + w.push(format!(" CYP2D6 diplotype: {}", py(d.need("cyp2d6")?)?)); + if contains(d.data, "comparison") { + let c = d.need("comparison")?; + let p = "sections.pypgx.data.comparison"; + w.push(format!( + " PharmCAT vs pypgx conflicts: {}", + py(need(c, "conflicts", p)?)? + )); + let one = need(c, "one_tool_only", p)?; + if truthy(one) { + w.push(format!( + " Scope differences (only one tool called): {}", + py(one)? + )); + } + } + w.extend(pypgx_lines(&d)?.into_iter().map(|l| format!(" {l}"))); + w.push(String::new()); + } + } + let cn = pgx_copy_number(s)?; + if !cn.is_empty() { + w.push("## PGx copy number".to_owned()); + w.push("---".to_owned()); + for (g, v) in cn { + w.push(format!(" {} {v}", pycompat::fmt::ljust(&g, 8))); + } + w.push(String::new()); + } + let Some(c) = block(s)? else { + return Ok(w); + }; + let calls = calls(c)?; + let verdict = cyp2d6_verdict(c)?; + if calls.iter().any(|(_, v)| truthy(v)) || !verdict.is_empty() { + w.push("## CYP2D6 across callers".to_owned()); + w.push("---".to_owned()); + // The consensus change is filed under pypgx. + if let Some(line) = text::new_since(s, &["pypgx", "cyrius"])? { + w.push(line); + } + for (k, v) in &calls { + w.push(format!( + " {} {}", + pycompat::fmt::ljust(caller_label(k), 26), + or_text(Some(v), "not run")? + )); + } + let agree = need(c, "agree", "summary.cyp2d6")?; + w.push(format!( + " pypgx and Cyrius agree: {}", + if truthy(agree) { + "yes" + } else if *agree == Value::Bool(false) { + "NO, review before acting on CYP2D6" + } else { + "fewer than two usable calls" + } + )); + if !verdict.is_empty() { + w.push(format!(" Step 36: {verdict}")); + } + if let Some((label, line)) = probable(c)? { + w.push(format!(" {label}: {line}")); + } + w.push(format!(" {PGX_CALLERS_NOTE}")); + w.push(String::new()); + } + Ok(w) +} + +/// The HTML report's cards of the section: pypgx (when its step ran or +/// failed), its copy numbers, and CYP2D6 across the callers, which is +/// always there. +pub fn cyp2d6_html(s: &Summary) -> Result { + let mut cards = Vec::new(); + let pd = s.section("pypgx"); + if pd.state != State::Missing { + let mut body = vec![ + stat( + "Genes called", + Inline::Text(format!( + "{}/{}", + or_none(pd.get("genes_called")?)?, + or_none(pd.get("genes_total")?)? + )), + ), + stat( + "CYP2D6 diplotype", + Inline::Text(or_text(pd.get("cyp2d6")?, "not called")?), + ), + ]; + body.extend(pypgx_lines(&pd)?.into_iter().map(Line::Para)); + cards.push(html::card( + &pd, + Card { + title: "pypgx Pharmacogenomics".into(), + body, + ..Card::default() + }, + )?); + } + let cn = pgx_copy_number(s)?; + if !cn.is_empty() { + cards.push(Card { + title: "PGx Copy Number".into(), + body: cn + .into_iter() + .map(|(g, v)| stat(&g, Inline::Text(v))) + .collect(), + ..Card::default() + }); + } + let Some(c) = block(s)? else { + return Ok(HtmlPart { + cards, + files: Vec::new(), + }); + }; + let mut body: Vec = Vec::new(); + for (k, v) in calls(c)? { + body.push(stat( + caller_label(k), + Inline::Text(or_text(Some(v), "not run")?), + )); + } + let agree = need(c, "agree", "summary.cyp2d6")?; + body.push(stat( + "pypgx and Cyrius agree", + if truthy(agree) { + Inline::Badge("yes".into(), Colour::Green) + } else if *agree == Value::Bool(false) { + Inline::Badge("no".into(), Colour::Red) + } else { + Inline::Badge("fewer than two usable calls".into(), Colour::Gray) + }, + )); + if let Some(cons) = get(c, "consensus", "summary.cyp2d6")?.filter(|v| truthy(v)) { + let p = "summary.cyp2d6.consensus"; + let passed = get(cons, "passed_to_pharmcat", p)?.is_some_and(truthy); + body.push(stat( + "Step 36", + Inline::Badge( + or_text(get(cons, "result", p)?, "?")?, + if passed { + Colour::State + } else { + Colour::Yellow + }, + ), + )); + body.push(Line::Note(format!( + "{}{}", + if passed { + "Passed to PharmCAT: " + } else { + "Not passed to PharmCAT: " + }, + or_text(get(cons, "reason", p)?, "no reason given")? + ))); + } + if let Some((label, line)) = probable(c)? { + body.push(stat(&label, Inline::Text(line))); + } + let mut stale = Vec::new(); + for k in ["cpic", "pypgx", "cyrius"] { + let sec = s.section(k); + if matches!(sec.state, State::Stale | State::Failed) { + stale.push(format!( + "{}: {}", + text_of(sec.title, &format!("sections.{k}.title"))?, + stale_note(&sec)? + )); + } + } + cards.push(Card { + title: "CYP2D6 Across Callers".into(), + caveats: vec![PGX_CALLERS_NOTE], + stale, + body, + ..Card::default() + }); + Ok(HtmlPart { + cards, + files: Vec::new(), + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn paths_join_as_os_path_does() { + assert_eq!(beside("cpic/S_cpic.json", "S_x.txt"), "cpic/S_x.txt"); + assert_eq!(beside("S_cpic.json", "S_x.txt"), "S_x.txt"); + assert_eq!(beside("/r.json", "x"), "/x"); + assert_eq!(beside("a//b", "x"), "a/x"); + assert_eq!(beside("a/b", "/abs"), "/abs"); + } + + #[test] + fn capitalize_lowers_the_rest() { + assert_eq!(capitalize("probable CYP2D6 call"), "Probable cyp2d6 call"); + assert_eq!(capitalize(""), ""); + } + + #[test] + fn left_pads_as_format_does() { + assert_eq!(left(&Value::Str("ab".into()), 4, "x").unwrap(), "ab "); + assert_eq!(left(&Value::Bool(true), 3, "x").unwrap(), "1 "); + assert!(left(&Value::Null, 3, "x").is_err()); + } +} diff --git a/rust/crates/og-render/tests/port_report_core.rs b/rust/crates/og-render/tests/port_report_core.rs index df225fe2a..f4d288ee1 100644 --- a/rust/crates/og-render/tests/port_report_core.rs +++ b/rust/crates/og-render/tests/port_report_core.rs @@ -254,9 +254,14 @@ fn both_reports_follow_the_chapter_order() { .flat_map(|c| c.sections.iter().map(|s| s.id)) .collect(); let shown = shown_sections(&page); - assert_eq!(shown, ["qc", "variants", "not-run", "not-assessed"]); + // The sections ported so far, in chapter order, then the limits chapter. + let n = shown.len(); + assert!( + n >= 4 && shown[..2] == ["qc", "variants"] && shown[n - 2..] == ["not-run", "not-assessed"], + "{shown:?}" + ); let in_order: Vec<&str> = sids.iter().copied().filter(|s| shown.contains(s)).collect(); - assert_eq!(&shown[..2], in_order.as_slice()); + assert_eq!(&shown[..n - 2], in_order.as_slice()); assert_eq!(text_chapters(&txt), ["QUALITY"]); let q = txt.split("# 1. QUALITY").nth(1).unwrap_or(""); assert!(q.find("## Quality control") < q.find("## Variant Calling (DeepVariant)")); @@ -266,7 +271,7 @@ fn both_reports_follow_the_chapter_order() { .replace("id=\"qc\"", "id=\"tmp\"") .replace("id=\"variants\"", "id=\"qc\"") .replace("id=\"tmp\"", "id=\"variants\""); - assert_ne!(shown_sections(&swapped)[..2], in_order[..]); + assert_ne!(shown_sections(&swapped)[..n - 2], in_order[..]); } #[test] diff --git a/rust/crates/og-render/tests/port_report_pgx.rs b/rust/crates/og-render/tests/port_report_pgx.rs new file mode 100644 index 000000000..1a2edb453 --- /dev/null +++ b/rust/crates/og-render/tests/port_report_pgx.rs @@ -0,0 +1,1006 @@ +//! The PGx, HLA and mitochondrial sections of the reports (og report +//! render), and the summary data they show (og summary collect), on +//! synthetic inputs. These are the checks of the old +//! tests/test_hla_report.py and tests/test_mito_extended.py, and the rules +//! of the CYP2D6 verdict line and the KIR block. +//! +//! Each detector is shown red: the table checks on a planted bad table, the +//! derivations on a swapped allele, the report lines on the summary without +//! the input that makes them. + +use std::fs; +use std::path::{Path, PathBuf}; + +use og_render::model::Summary; +use og_render::sections::{hla, pgx}; +use og_render::{html, text}; +use og_summary::collect::{Group, Input, post}; +use pycompat::json::{Dump, Value, dumps, loads}; + +fn root() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../..") +} + +/// A fresh scratch folder for one test. +fn scratch(name: &str) -> PathBuf { + let d = std::env::temp_dir().join(format!("og-render-pgx-{}-{name}", std::process::id())); + let _ = fs::remove_dir_all(&d); + fs::create_dir_all(&d).expect("scratch folder"); + d +} + +/// og summary collect on one sample folder, every section, with ASSETS. +fn collect_with(sample: &str, dir: &Path, assets: &Path) -> String { + let input = Input { + sample: sample.to_owned(), + sample_dir: dir.display().to_string(), + declared_sex: None, + assets: assets.display().to_string(), + group: Group::All, + tables: root().join("bin").display().to_string(), + }; + let v = post::collect(&input).expect("a summary"); + dumps( + &v, + &Dump { + indent: Some(1), + ..Dump::default() + }, + ) + .expect("json") +} + +fn collect(sample: &str, dir: &Path) -> String { + collect_with(sample, dir, &root().join("assets")) +} + +fn reports(json: &str) -> (String, String) { + let s = Summary::parse(json).expect("a summary"); + ( + text::report(&s).expect("the text report"), + html::report(&s).expect("the HTML report"), + ) +} + +/// `doc[a][b]...`, or `Null` where a key is missing. +fn at<'a>(doc: &'a Value, path: &[&str]) -> &'a Value { + let mut v = doc; + for k in path { + v = match v { + Value::Object(o) => o.get(*k).unwrap_or(&Value::Null), + _ => &Value::Null, + }; + } + v +} + +fn s(v: &Value) -> &str { + match v { + Value::Str(s) => s, + _ => "", + } +} + +/// The errors og dev schema finds in a summary. +fn schema_errors(json: &str) -> Vec { + let schema = loads( + &fs::read_to_string(root().join("tests/schema/summary.schema.json")).expect("schema"), + ) + .expect("schema json"); + og_dev::schema::validate(&schema, &loads(json).expect("summary json")).expect("a schema") +} + +// --- HLA ------------------------------------------------------------------------------ + +const COLUMNS: [&str; 5] = ["allele", "kind", "association", "pmids", "citation"]; +const REQUIRED: [&str; 14] = [ + "A*31:01", + "B*15:02", + "B*57:01", + "B*58:01", + "B*27", + "C*06:02", + "DQB1*06:02", + "DRB1*15:01", + "DRB1*01:01", + "DRB1*04:01", + "DRB1*04:04", + "DRB1*04:05", + "DRB1*04:08", + "DRB1*14:02", +]; + +/// `[A-Z]+[0-9]?\*[0-9]{2}(:[0-9]{2,3})?`: an allele group at a field +/// boundary. +fn allele_group(a: &str) -> bool { + let Some((gene, fields)) = a.split_once('*') else { + return false; + }; + let letters = gene.trim_end_matches(|c: char| c.is_ascii_digit()); + let digits = gene.len() - letters.len(); + let f: Vec<&str> = fields.split(':').collect(); + !letters.is_empty() + && letters.chars().all(|c| c.is_ascii_uppercase()) + && digits <= 1 + && f[0].len() == 2 + && f.iter() + .all(|x| !x.is_empty() && x.chars().all(|c| c.is_ascii_digit())) + && match f.len() { + 1 => true, + 2 => (2..=3).contains(&f[1].len()), + _ => false, + } +} + +/// Every schema problem of an allele table. +fn table_errors(text: &str) -> Vec { + let (mut errs, mut head, mut seen) = (Vec::new(), None::>, Vec::::new()); + for (n, line) in text.lines().enumerate().map(|(i, l)| (i + 1, l)) { + if line.starts_with('#') || line.trim().is_empty() { + continue; + } + let c: Vec<&str> = line.split('\t').collect(); + if head.is_none() { + if c != COLUMNS { + errs.push(format!("header {c:?}")); + } + head = Some(c.iter().map(|x| (*x).to_owned()).collect()); + continue; + } + if c.len() != COLUMNS.len() { + errs.push(format!("line {n}: {} columns", c.len())); + continue; + } + let (allele, kind, assoc, pmids, cite) = (c[0], c[1], c[2], c[3], c[4]); + if !allele_group(allele) { + errs.push(format!("line {n}: allele {allele:?}")); + } + if !matches!(kind, "drug" | "disease") { + errs.push(format!("line {n}: kind {kind:?}")); + } + if assoc.trim().is_empty() { + errs.push(format!("line {n}: no association")); + } + if pmids.is_empty() + || !pmids + .split(',') + .all(|p| !p.is_empty() && p.chars().all(|c| c.is_ascii_digit())) + { + errs.push(format!("line {n}: pmids {pmids:?}")); + } + if cite.trim().is_empty() { + errs.push(format!("line {n}: no citation")); + } + if seen.iter().any(|x| x == allele) { + errs.push(format!("line {n}: duplicate {allele}")); + } + seen.push(allele.to_owned()); + } + errs.extend( + REQUIRED + .iter() + .filter(|a| !seen.iter().any(|x| x == *a)) + .map(|a| format!("missing {a}")), + ); + errs +} + +/// One allele of a T1K row: (allele, quality), or `None` for T1K's +/// one-allele row. +type Pair<'a> = Option<(&'a str, i32)>; + +/// T1K's genotype TSV. +fn t1k(rows: &[(&str, &str, i32, Pair<'_>)]) -> String { + rows.iter() + .map(|(gene, a1, q1, two)| match two { + None => format!("{gene}\t1\t{a1}\t30\t{q1}\t.\t0\t-1\n"), + Some((a2, q2)) => format!("{gene}\t2\t{a1}\t30\t{q1}\t{a2}\t20\t{q2}\n"), + }) + .collect() +} + +/// The summary of a sample folder that holds only T1K's genotype TSV. +fn hla_summary(work: &Path, name: &str, rows: &[(&str, &str, i32, Pair<'_>)]) -> String { + let d = work.join(name).join("S"); + fs::create_dir_all(d.join("hla_t1k")).expect("folder"); + fs::write(d.join("hla_t1k/S_hla_genotype.tsv"), t1k(rows)).expect("t1k"); + collect("S", &d) +} + +fn coeliac(json: &str) -> Value { + at( + &loads(json).expect("json"), + &["sections", "hla", "data", "derived", "coeliac"], + ) + .clone() +} + +fn state_of(json: &str, allele: &str) -> String { + let v = loads(json).expect("json"); + let Value::Array(rows) = at(&v, &["sections", "hla", "data", "derived", "alleles"]) else { + panic!("no allele table"); + }; + rows.iter() + .find(|r| s(at(r, &["allele"])) == allele) + .map(|r| s(at(r, &["state"])).to_owned()) + .unwrap_or_default() +} + +#[test] +fn the_hla_allele_table_passes_its_schema_and_each_planted_problem_is_caught() { + let good = fs::read_to_string(root().join("bin/hla_alleles.tsv")).expect("table"); + assert_eq!(table_errors(&good), Vec::::new()); + for (what, bad) in [ + ("a wrong kind", good.replacen("\tdrug\t", "\tdrugs\t", 1)), + ( + "a PubMed id that is not a number", + good.replace("\t21428769\t", "\tPMID21428769\t"), + ), + ( + "a duplicate allele", + format!("{good}B*27\tdisease\tx\t1\ty\n"), + ), + ( + "a missing required allele", + good.replace("C*06:02\t", "C*07:02\t"), + ), + ( + "a three-field allele", + good.replace("B*58:01\t", "B*58:01:01\t"), + ), + ] { + assert!(!table_errors(&bad).is_empty(), "not caught: {what}"); + } + // og summary collect reads the same rows, in order. + let work = scratch("table"); + let json = hla_summary( + &work, + "table", + &[("HLA-B", "B*57:01:01", 40, Some(("B*08:01:01", 40)))], + ); + let v = loads(&json).expect("json"); + let Value::Array(rows) = at(&v, &["sections", "hla", "data", "derived", "alleles"]) else { + panic!("no allele table"); + }; + let got: Vec<&str> = rows.iter().map(|r| s(at(r, &["allele"]))).collect(); + let want: Vec<&str> = good + .lines() + .filter(|l| !l.starts_with('#') && !l.trim().is_empty()) + .skip(1) + .map(|l| l.split('\t').next().unwrap_or("")) + .collect(); + assert_eq!(got, want); + let _ = fs::remove_dir_all(work); +} + +#[test] +fn the_coeliac_dq_types_and_the_dqb1_02_dose_follow_the_alleles() { + let work = scratch("coeliac"); + let cis = hla_summary( + &work, + "cis", + &[ + ("HLA-DQA1", "DQA1*05:01:01", 40, Some(("DQA1*01:01:01", 40))), + ("HLA-DQB1", "DQB1*02:01:01", 40, Some(("DQB1*05:01:01", 40))), + ("HLA-DRB1", "DRB1*03:01:01", 40, Some(("DRB1*01:01:01", 40))), + ("HLA-B", "B*57:01:01", 40, Some(("B*08:01:01", 40))), + ], + ); + let c = coeliac(&cis); + assert_eq!(s(at(&c, &["status"])), "derived"); + assert_eq!(s(at(&c, &["dq2_5"])), "present"); + assert!( + s(at(&c, &["dq2_5_phase"])).starts_with("likely cis"), + "{c:?}" + ); + assert_eq!( + (s(at(&c, &["dq2_2"])), s(at(&c, &["dq8"]))), + ("absent", "absent") + ); + assert_eq!(at(&c, &["dqb1_02_dose"]), &Value::Int(1)); + assert_eq!(schema_errors(&cis), Vec::::new()); + let trans = coeliac(&hla_summary( + &work, + "trans", + &[ + ("HLA-DQA1", "DQA1*05:05:01", 40, Some(("DQA1*02:01:01", 40))), + ("HLA-DQB1", "DQB1*03:01:01", 40, Some(("DQB1*02:02:01", 40))), + ("HLA-DRB1", "DRB1*11:01:01", 40, Some(("DRB1*07:01:01", 40))), + ], + )); + assert_eq!(s(at(&trans, &["dq2_5"])), "present"); + assert!(s(at(&trans, &["dq2_5_phase"])).starts_with("possible trans")); + assert_eq!(s(at(&trans, &["dq2_2"])), "present"); + let dq22 = coeliac(&hla_summary( + &work, + "dq22", + &[ + ("HLA-DQA1", "DQA1*02:01:01", 40, Some(("DQA1*01:02:01", 40))), + ("HLA-DQB1", "DQB1*02:02:01", 40, Some(("DQB1*06:02:01", 40))), + ], + )); + assert_eq!( + ( + s(at(&dq22, &["dq2_5"])), + s(at(&dq22, &["dq2_2"])), + s(at(&dq22, &["dq8"])) + ), + ("absent", "present", "absent") + ); + assert_eq!(at(&dq22, &["dq2_5_phase"]), &Value::Null); + let dq8 = coeliac(&hla_summary( + &work, + "dq8", + &[ + ("HLA-DQA1", "DQA1*03:01:01", 40, Some(("DQA1*01:01:01", 40))), + ("HLA-DQB1", "DQB1*03:02:01", 40, Some(("DQB1*05:01:01", 40))), + ], + )); + assert_eq!( + (s(at(&dq8, &["dq8"])), s(at(&dq8, &["dq2_5"]))), + ("present", "absent") + ); + assert_eq!(at(&dq8, &["dqb1_02_dose"]), &Value::Int(0)); + // Red control: DQA1*05:01 swapped for DQA1*01:01, so no DQ2.5. + let red = coeliac(&hla_summary( + &work, + "red", + &[ + ("HLA-DQA1", "DQA1*01:01:01", 40, Some(("DQA1*01:02:01", 40))), + ("HLA-DQB1", "DQB1*02:01:01", 40, Some(("DQB1*05:01:01", 40))), + ("HLA-DRB1", "DRB1*03:01:01", 40, Some(("DRB1*01:01:01", 40))), + ], + )); + assert_eq!(s(at(&red, &["dq2_5"])), "absent"); + assert_eq!(at(&red, &["dq2_5_phase"]), &Value::Null); + let low = coeliac(&hla_summary( + &work, + "low", + &[ + ("HLA-DQA1", "DQA1*05:01:01", 0, Some(("DQA1*01:01:01", 40))), + ("HLA-DQB1", "DQB1*02:01:01", 40, Some(("DQB1*05:01:01", 40))), + ], + )); + assert_eq!(s(at(&low, &["status"])), "not_determined"); + assert_eq!(at(&low, &["dq2_5"]), &Value::Null); + assert!(s(at(&low, &["reason"])).contains("HLA-DQA1 low confidence")); + assert_eq!(at(&low, &["dqb1_02_dose"]), &Value::Int(1)); + let two = coeliac(&hla_summary( + &work, + "two", + &[("HLA-DQB1", "DQB1*02:01:01", 40, Some(("DQB1*02:02:01", 40)))], + )); + assert_eq!(at(&two, &["dqb1_02_dose"]), &Value::Int(2)); + let hom = coeliac(&hla_summary( + &work, + "hom", + &[("HLA-DQB1", "DQB1*02:01:01", 40, None)], + )); + assert_eq!( + at(&hom, &["dqb1_02_dose"]), + &Value::Int(2), + "T1K's one homozygous allele" + ); + assert_eq!(s(at(&hom, &["reason"])), "HLA-DQA1 not typed"); + let _ = fs::remove_dir_all(work); +} + +#[test] +fn the_allele_table_reads_present_absent_uncertain_and_not_determined() { + let work = scratch("alleles"); + let cis = hla_summary( + &work, + "cis", + &[("HLA-B", "B*57:01:01", 40, Some(("B*08:01:01", 40)))], + ); + assert_eq!(state_of(&cis, "B*57:01"), "present"); + let b7 = hla_summary( + &work, + "b7", + &[("HLA-B", "B*07:02:01", 40, Some(("B*08:01:01", 40)))], + ); + assert_eq!(state_of(&b7, "B*57:01"), "absent"); + assert_eq!(state_of(&b7, "B*58:01"), "absent"); + let grp = hla_summary( + &work, + "b27", + &[ + ( + "HLA-B", + "B*27:05:02", + 40, + Some(("B*57:03:01,B*57:01:01", 40)), + ), + ("HLA-C", "C*06:02:01", 0, Some(("C*07:01:01", 40))), + ], + ); + assert_eq!(state_of(&grp, "B*27"), "present"); + assert_eq!(state_of(&grp, "B*57:01"), "uncertain"); + assert_eq!(state_of(&grp, "C*06:02"), "not_determined"); + assert_eq!(state_of(&grp, "DQB1*06:02"), "not_determined"); + let _ = fs::remove_dir_all(work); +} + +#[test] +fn both_reports_show_the_hla_rows_the_derived_lines_and_the_note() { + let work = scratch("hla-report"); + let many: Vec = (1..=12).map(|i| format!("DRB5*01:{i:02}")).collect(); + let many = many.join(","); + let cap = hla_summary( + &work, + "cap", + &[ + ("HLA-DRB5", &many, 0, None), + ("HLA-HFE", "HFE*001:01", 40, Some(("HFE*002:01", 40))), + ("HLA-DQA1", "DQA1*05:01:01", 40, Some(("DQA1*01:01:01", 40))), + ("HLA-DQB1", "DQB1*02:01:01", 40, Some(("DQB1*05:01:01", 40))), + ("HLA-B", "B*57:01:01", 40, Some(("B*08:01:01", 40))), + ], + ); + let v = loads(&cap).expect("json"); + let Value::Array(loci) = at(&v, &["sections", "hla", "data", "loci"]) else { + panic!("no loci"); + }; + let drb5 = loci + .iter() + .find(|l| s(at(l, &["gene"])) == "HLA-DRB5") + .expect("DRB5"); + assert_eq!( + at(drb5, &["alleles"]), + &Value::Array(vec![Value::Str("DRB5*01:01,DRB5*01:02,DRB5*01:03".into())]) + ); + assert_eq!( + at(drb5, &["candidates"]), + &Value::Array(vec![Value::Int(12)]) + ); + let hfe = loci + .iter() + .find(|l| s(at(l, &["gene"])) == "HLA-HFE") + .expect("HFE"); + assert!( + s(at(hfe, &["label"])).starts_with("T1K sequence allele, not the HFE C282Y/H63D genotype") + ); + assert_eq!(schema_errors(&cap), Vec::::new()); + let (txt, page) = reports(&cap); + assert!( + txt.contains("HLA-DRB5 no confident call (12 candidates, quality 0); low confidence"), + "{txt}" + ); + assert!(!txt.contains("DRB5*01:04") && !page.contains("DRB5*01:04")); + assert!(page.contains("no confident call (12 candidates, quality 0)")); + assert_eq!(txt.matches("not the HFE C282Y/H63D genotype").count(), 1); + assert_eq!(page.matches("not the HFE C282Y/H63D genotype").count(), 1); + let want = [ + "Coeliac DQ: DQ2.5 present, phase unknown (HLA-DRB1 not typed); DQ2.2 absent; DQ8 absent", + "DQB1*02 copies: 1", + "B*57:01 present: abacavir hypersensitivity; flucloxacillin liver injury", + "C*06:02 not determined (HLA-C not typed)", + ]; + for w in want { + assert!(txt.contains(w), "text: {w}"); + assert!( + page.contains(&og_render::escape::e(w).into_owned()), + "HTML: {w}" + ); + } + assert!(txt.contains("not a diagnosis")); + let note = og_render::notes::HLA_DERIVED_NOTE; + assert!(page.contains(&format!( + "

    {}

    ", + og_render::escape::e(note) + ))); + // No clinical advice in the derived lines or the note. + let advice = |t: &str| { + let t = t.to_lowercase(); + ["you should", "you must", "consult", "treat"] + .iter() + .any(|w| t.contains(w)) + }; + let derived = hla::hla_derived_lines(Some(at(&v, &["sections", "hla", "data", "derived"]))) + .expect("lines"); + assert!(!advice(&format!("{} {note}", derived.join(" ")))); + assert!( + advice("you should consult"), + "red control of the advice check" + ); + // Red control: without T1K's table the section is not run, and neither + // report prints a derived line. + let none = collect("S", &work.join("none")); + let (txt, page) = reports(&none); + assert!(!txt.contains("Coeliac DQ") && !page.contains("Coeliac DQ")); + let _ = fs::remove_dir_all(work); +} + +/// The KIR block of the text report, as one string. +fn kir_block(txt: &str) -> String { + let start = txt + .find(" KIR genes (T1K, KIR=true):") + .expect("a KIR block"); + let rest = &txt[start..]; + let end = rest.find("\n\n").unwrap_or(rest.len()); + rest[..end].to_owned() +} + +#[test] +fn the_kir_block_lists_each_typed_gene_then_the_genes_with_no_allele() { + let kir = loads( + r#"{"reason": "", "typed": [{"gene": "KIR2DL1", "alleles": ["KIR2DL1*001", "KIR2DL1*002"], + "quality": ["30", ""]}, {"gene": "KIR3DL2", "alleles": ["KIR3DL2*007"], "quality": [12, "x"]}], + "not_called": ["KIR2DS1", "KIR3DS1"]}"#, + ) + .expect("json"); + assert_eq!( + hla::kir_lines(&kir).expect("lines"), + [ + ( + "KIR2DL1".to_owned(), + "KIR2DL1*001 (quality 30) / KIR2DL1*002".to_owned() + ), + ("KIR3DL2".to_owned(), "KIR3DL2*007 (quality 12)".to_owned()), + ( + String::new(), + "No allele called (absent, or too few reads): KIR2DS1, KIR3DS1".to_owned() + ), + ] + ); + let why = loads(r#"{"reason": "KIR=false", "typed": [], "not_called": ["X"]}"#).expect("json"); + assert_eq!( + hla::kir_lines(&why).expect("lines"), + [(String::new(), "KIR=false".to_owned())] + ); + let summary = synthetic(&format!( + r#""hla": {{"title": "HLA", "state": "ok", "source": "x", "file_date": null, "note": null, + "data": {{"loci": [], "kir": {}}}}}"#, + dumps(&kir, &Dump::default()).expect("json") + )); + let (txt, page) = reports(&summary); + assert_eq!( + kir_block(&txt), + " KIR genes (T1K, KIR=true):\n KIR2DL1 KIR2DL1*001 (quality 30) / KIR2DL1*002\n \ + KIR3DL2 KIR3DL2*007 (quality 12)\n No allele called (absent, or too few reads): KIR2DS1, \ + KIR3DS1\n KIR genotypes as T1K typed them. No step interprets them: they matter for transplant \ + matching and some immune conditions, not for drug guidance." + ); + assert!(page.contains( + "KIR2DL1KIR2DL1*001 (quality 30) / KIR2DL1*002" + )); + assert!(page.contains( + "

    No allele called (absent, or too few reads): KIR2DS1, KIR3DS1

    " + )); +} + +// --- CYP2D6 --------------------------------------------------------------------------- + +/// A summary with the frame's keys and the sections and top-level keys of +/// `sections` (a JSON object body). +fn synthetic(sections: &str) -> String { + synthetic_with(sections, r#""cyp2d6": null"#) +} + +fn synthetic_with(sections: &str, top: &str) -> String { + format!( + r#"{{"schema_version": 2, "sample": "S1", "generated_utc": "2026-11-06 12:00 UTC", + "run": {{"started_utc": null, "declared_sex": null}}, + "databases": {{"clinvar_release": null, "cpsr_clinvar_release": null, "hla_database": "IPD-IMGT/HLA 3.60"}}, + "manifest": [], "sections": {{{sections}}}, {top}, "not_run": [], "not_assessed": []}}"# + ) +} + +#[test] +fn the_cyp2d6_verdict_line_says_where_the_call_went_and_why() { + let c = + |cons: &str| loads(&format!(r#"{{"calls": {{}}, "consensus": {cons}}}"#)).expect("json"); + assert_eq!( + pgx::cyp2d6_verdict(&c( + r#"{"result": "*1/*2", "passed_to_pharmcat": true, "reason": "pypgx and Cyrius agree"}"# + )) + .expect("line"), + "*1/*2, passed to PharmCAT (pypgx and Cyrius agree)" + ); + assert_eq!( + pgx::cyp2d6_verdict(&c(r#"{"result": null, "passed_to_pharmcat": false}"#)).expect("line"), + "?, not passed to PharmCAT (no reason given)" + ); + assert_eq!(pgx::cyp2d6_verdict(&c("null")).expect("line"), ""); + let top = r#""cyp2d6": {"calls": {"PharmCAT": "*1/*4", "pypgx": "*1/*2", "Cyrius": null}, + "agree": false, "consensus": {"result": "*1/*4", "passed_to_pharmcat": false, + "reason": "the callers disagree"}, "probable": null}"#; + let (txt, page) = reports(&synthetic_with("", top)); + assert!( + txt.contains( + " PharmCAT (step 36's call) *1/*4\n pypgx *1/*2\n Cyrius not run\n \ + pypgx and Cyrius agree: NO, review before acting on CYP2D6\n \ + Step 36: *1/*4, not passed to PharmCAT (the callers disagree)\n" + ), + "{txt}" + ); + assert!(page.contains( + "Step 36*1/*4" + )); + assert!(page.contains("

    Not passed to PharmCAT: the callers disagree

    ")); + // Red control: with no call and no verdict the text report has no block. + let empty = r#""cyp2d6": {"calls": {"PharmCAT": null}, "agree": null, "consensus": null, "probable": null}"#; + let (txt, _) = reports(&synthetic_with("", empty)); + assert!(!txt.contains("CYP2D6 across callers")); + // A summary without the block (collected by part) shows none. + let (txt, page) = reports(&synthetic("")); + assert!(!txt.contains("CYP2D6 across callers") && !page.contains("CYP2D6 Across Callers")); +} + +// --- mitochondrial variants -------------------------------------------------------- + +const EK_HEAD: &str = "\"Title\";\"5' breakpoint\";\"3' breakpoint\";\"Freq\";\"Freq For\";\"Freq Rev\";\ +\"5' Blast\";\"3' Blast\";\"5' Depth\";\"3' Depth\";\"Repetition\"\n"; + +fn run_mito(verb: &str, args: &[&str]) -> u8 { + let args: Vec = args.iter().map(|a| (*a).to_owned()).collect(); + og_summary::mito::cli(Some(verb), &args, &mut Vec::new(), &mut Vec::new()) +} + +/// A sample folder with a chrM VCF of (pos, ref, alt, AF), and eKLIPse's +/// deletions and the idxstats when given. +fn mito_dir( + work: &Path, + name: &str, + records: &[(u32, &str, &str, &str)], + deletions: Option<&[(u32, u32, f64)]>, + idxstats: Option, +) -> PathBuf { + let d = work.join(name); + fs::create_dir_all(d.join("mito")).expect("folder"); + let mut vcf = "##fileformat=VCFv4.2\n##contig=\n\ + #CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\tS\n" + .to_owned(); + for (pos, r, a, af) in records { + vcf += &format!("chrM\t{pos}\t.\t{r}\t{a}\t.\tPASS\t.\tGT:AF\t0/1:{af}\n"); + } + fs::write(d.join(format!("mito/{name}_chrM_filtered.vcf.gz")), vcf).expect("vcf"); + if let Some(rows) = deletions { + let mut csv = EK_HEAD.to_owned(); + for (b5, b3, lvl) in rows { + let v = lvl.to_string().replace('.', ","); + csv += &format!( + "\"{name}\";\"{b5}\";\"{b3}\";\"{v}\";\"{v}\";\"{v}\";\"12\";\"14\";\"390.0\";\"402.0\";\"none\"\n" + ); + } + let p = work.join(format!("{name}_eklipse.csv")); + fs::write(&p, csv).expect("csv"); + let out = d.join(format!("mito/{name}_chrM_deletions.tsv")); + assert_eq!( + run_mito( + "deletions", + &[ + "--eklipse", + &p.display().to_string(), + "--out", + &out.display().to_string() + ] + ), + 0 + ); + } + if let Some(text) = idxstats { + let p = work.join(format!("{name}.idxstats")); + fs::write(&p, text).expect("idxstats"); + let out = d.join(format!("mito/{name}_mtdna_copies.tsv")); + assert_eq!( + run_mito( + "copies", + &[ + "--idxstats", + &p.display().to_string(), + "--out", + &out.display().to_string() + ] + ), + 0 + ); + } + d +} + +fn mito_data(json: &str) -> Value { + at(&loads(json).expect("json"), &["sections", "mito", "data"]).clone() +} + +fn idxstats(chrm: u64, per_chrom: u64) -> String { + let mut s: String = (1..=22) + .map(|i| format!("chr{i}\t50000000\t{per_chrom}\t0\n")) + .collect(); + s += &format!("chrX\t156040895\t{per_chrom}\t0\nchrM\t16569\t{chrm}\t0\n*\t0\t0\t500\n"); + s +} + +#[test] +fn the_mito_list_has_its_columns_an_id_on_every_row_and_its_first_rows() { + let text = fs::read_to_string(root().join("assets/mito_confirmed.tsv")).expect("list"); + let errors = |t: &str| -> Vec { + let mut rows = t.lines().filter(|l| !l.starts_with('#')); + let head = rows.next().unwrap_or(""); + let cols = [ + "kind", + "pos", + "end", + "ref", + "alt", + "hgvs", + "gene", + "disease", + "clinvar_id", + "pmids", + "heteroplasmy_note", + ]; + if head.split('\t').collect::>() != cols { + return vec!["header".into()]; + } + let digits = |x: &str| !x.is_empty() && x.chars().all(|c| c.is_ascii_digit()); + let mut out = Vec::new(); + for r in rows { + let c: Vec<&str> = r.split('\t').collect(); + if c.len() != cols.len() { + out.push(format!("{r}: columns")); + continue; + } + let (cv, pm) = (!matches!(c[8], "" | "."), !matches!(c[9], "" | ".")); + if !cv && !pm { + out.push(format!("{}: neither a ClinVar id nor a PubMed id", c[5])); + } + if cv && !digits(c[8]) { + out.push(format!("{}: ClinVar id", c[5])); + } + if pm && !c[9].split(';').all(digits) { + out.push(format!("{}: PubMed ids", c[5])); + } + if !matches!(c[0], "snv" | "deletion") { + out.push(format!("{}: kind", c[5])); + } else { + let (p, e) = (c[1].parse::().ok(), c[2].parse::().ok()); + if !matches!((p, e), (Some(p), Some(e)) if 1 <= p && p <= e && e <= 16569) { + out.push(format!("{}: positions", c[5])); + } + } + } + out + }; + assert_eq!(errors(&text), Vec::::new()); + for want in [ + "m.3243A>G", + "m.8344A>G", + "m.11778G>A", + "m.3460G>A", + "m.14484T>C", + ] { + assert!(text.contains(&format!("\t{want}\t")), "{want}"); + } + assert!( + text.lines() + .any(|l| l.starts_with("deletion\t8470\t13446\t")) + ); + let bad = + format!("{text}snv\t3271\t3271\tT\tC\tm.3271T>C\tMT-TL1\tMELAS\t.\t.\tno id at all\n"); + assert!( + errors(&bad) + .iter() + .any(|e| e.contains("neither a ClinVar id nor a PubMed id")), + "red control: a planted row with neither id" + ); +} + +#[test] +fn a_confirmed_variant_shows_with_its_level_in_the_summary_and_both_reports() { + let work = scratch("confirmed"); + let d = mito_dir( + &work, + "S3243", + &[ + (73, "A", "G", "1.00"), + (3243, "A", "G", "0.12"), + (16519, "T", "C", "0.998"), + ], + None, + None, + ); + let json = collect("S3243", &d); + let data = mito_data(&json); + let Value::Array(conf) = at(&data, &["confirmed"]) else { + panic!("no confirmed list: {data:?}"); + }; + assert_eq!(conf.len(), 1); + assert_eq!(s(at(&conf[0], &["hgvs"])), "m.3243A>G"); + assert_eq!(s(at(&conf[0], &["af"])), "0.12"); + let (txt, page) = reports(&json); + assert!( + txt.contains("m.3243A>G (MT-TL1) AF 0.12 filter PASS MELAS"), + "{txt}" + ); + assert!(page.contains( + "Confirmed disease variants
    1<" + )); + assert!(page.contains("m.3243A>G (MT-TL1) AF 0.12")); + // Red control: the same VCF without m.3243A>G lists none. + let d = mito_dir( + &work, + "Snone", + &[(73, "A", "G", "1.00"), (16519, "T", "C", "0.998")], + None, + None, + ); + let json = collect("Snone", &d); + assert_eq!( + at(&mito_data(&json), &["confirmed"]), + &Value::Array(Vec::new()) + ); + let (txt, _) = reports(&json); + assert!(txt.contains("Confirmed disease variants (assets/mito_confirmed.tsv): 0")); + // Without the list there is no count, only why. + let no_assets = work.join("no-assets"); + fs::create_dir_all(&no_assets).expect("folder"); + let json = collect_with("Snone", &d, &no_assets); + let data = mito_data(&json); + assert_eq!(at(&data, &["confirmed"]), &Value::Null); + assert!(s(at(&data, &["confirmed_note"])).contains("mito_confirmed.tsv is not in")); + let (txt, page) = reports(&json); + assert!(!txt.contains("Confirmed disease variants (assets")); + assert!(!page.contains("Confirmed disease variants")); + assert!(txt.contains("Confirmed disease variants: not checked")); + let _ = fs::remove_dir_all(work); +} + +#[test] +fn the_copy_number_shows_in_both_reports_or_why_it_is_missing() { + let work = scratch("copies"); + let per = 200 * 50_000; + let chrm = (100.0 * 16569.0 * per as f64 / 50_000_000.0).round() as u64; + let d = mito_dir(&work, "Snocn", &[(73, "A", "G", "1.00")], None, None); + let json = collect("Snocn", &d); + let data = mito_data(&json); + assert_eq!(at(&data, &["copies"]), &Value::Null); + assert!(s(at(&data, &["copies_note"])).contains("mtdna_copies.tsv")); + let (txt, page) = reports(&json); + assert!(txt.contains("mtDNA copy number: not counted (")); + assert!(page.contains("mtDNA copy number: not counted (")); + let d = mito_dir( + &work, + "Scn", + &[(73, "A", "G", "1.00")], + None, + Some(idxstats(chrm, per)), + ); + let (txt, page) = reports(&collect("Scn", &d)); + let n = |t: &str, lead: &str| -> f64 { + let at = t.find(lead).expect(lead) + lead.len(); + t[at..] + .split(|c: char| !(c.is_ascii_digit() || c == '.')) + .next() + .and_then(|x| x.parse().ok()) + .unwrap_or(0.0) + }; + let got = n(&txt, "mtDNA copy number: "); + assert!((200.0..210.0).contains(&got), "{got}"); + let got = n(&page, "mtDNA copies per cell"); + assert!((200.0..210.0).contains(&got), "{got}"); + let _ = fs::remove_dir_all(work); +} + +#[test] +fn the_large_deletions_are_counted_listed_and_named() { + let work = scratch("deletions"); + let d = mito_dir( + &work, + "Sdel", + &[(73, "A", "G", "1.00")], + Some(&[ + (8469, 13447, 10.2), + (8482, 13460, 2.5), + (9000, 14000, 3.1), + (5000, 7000, 0.4), + ]), + None, + ); + let json = collect("Sdel", &d); + let dl = at(&mito_data(&json), &["deletions"]).clone(); + let Value::Array(listed) = at(&dl, &["listed"]) else { + panic!("no deletions: {dl:?}"); + }; + let num = |v: &Value| match v { + Value::Int(i) => *i, + _ => -1, + }; + let named: Vec<(i128, i128)> = listed + .iter() + .filter(|r| !matches!(at(r, &["known"]), Value::Null)) + .map(|r| (num(at(r, &["bkp5"])), num(at(r, &["bkp3"])))) + .collect(); + assert_eq!(named, [(8469, 13447), (8482, 13460)]); + assert!( + listed + .iter() + .any(|r| num(at(r, &["bkp5"])) == 9000 && matches!(at(r, &["known"]), Value::Null)) + ); + assert_eq!((at(&dl, &["total"]), listed.len()), (&Value::Int(4), 3)); + let (txt, page) = reports(&json); + assert!(page.contains(">Large deletions (eKLIPse)4<")); + assert!(page.contains( + "Named in the confirmed list2<" + )); + let line = txt + .lines() + .find(|l| l.contains("m.8470_13446del (4977 bp) level 10.2%")) + .expect("the common deletion"); + assert!( + line.contains("The common 4977 bp deletion") && line.contains("ClinVar 430680"), + "{line}" + ); + assert!(txt.contains("often found in muscle and not in blood")); + // Red control: a table with no deletion lists none. + let d = mito_dir(&work, "Snodel", &[(73, "A", "G", "1.00")], Some(&[]), None); + let dl = at(&mito_data(&collect("Snodel", &d)), &["deletions"]).clone(); + assert_eq!(at(&dl, &["total"]), &Value::Int(0)); + assert_eq!(at(&dl, &["listed"]), &Value::Array(Vec::new())); + let _ = fs::remove_dir_all(work); +} + +#[test] +fn the_eklipse_image_is_pinned_by_digest_and_names_its_upstream_commit() { + let env = fs::read_to_string(root().join("versions.env")).expect("versions.env"); + let line = env + .lines() + .find(|l| l.starts_with("EKLIPSE_IMAGE=")) + .expect("EKLIPSE_IMAGE"); + let image = line + .strip_prefix("EKLIPSE_IMAGE=\"") + .and_then(|r| r.split('"').next()) + .unwrap_or(""); + let (name, digest) = image.split_once("@sha256:").unwrap_or(("", "")); + assert!(name.contains(':') && digest.len() == 64, "{line}"); + assert!( + digest + .chars() + .all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase()) + ); + assert!( + line.contains("3606cb2edac983d2623ddc667b49206c3d01373c"), + "{line}" + ); +} + +/// og summary collect and og-render each hold the candidate cap +/// (`MAX_CANDIDATES`). The collect one is private, so this checks the rule +/// they share instead of the two numbers: at quality 0, the report says "no +/// confident call" exactly when collect cut the candidates down. A change to +/// one cap and not the other makes one count of candidates disagree. +#[test] +fn the_report_withholds_an_allele_exactly_when_collect_cut_its_candidates() { + let work = scratch("hla-cap"); + let mut seen = (false, false); + for n in 1..=6 { + let cands: Vec = (1..=n).map(|i| format!("A*01:{i:02}")).collect(); + let cands = cands.join(","); + let json = hla_summary(&work, &format!("n{n}"), &[("HLA-A", &cands, 0, None)]); + let v = loads(&json).expect("json"); + let Value::Array(loci) = at(&v, &["sections", "hla", "data", "loci"]) else { + panic!("no loci"); + }; + let a = loci + .iter() + .find(|l| s(at(l, &["gene"])) == "HLA-A") + .expect("HLA-A"); + let Value::Array(kept) = at(a, &["alleles"]) else { + panic!("no alleles"); + }; + let cut = s(&kept[0]) != cands; + let withheld = hla::hla_text(a) + .expect("the HLA line") + .contains("no confident call"); + assert_eq!( + cut, withheld, + "{n} candidates: collect cut them {cut}, the report withholds {withheld}" + ); + if cut { + seen.0 = true; + } else { + seen.1 = true; + } + } + assert_eq!(seen, (true, true), "1 to 6 candidates must cross the cap"); +} diff --git a/tests/parity/cases.tsv b/tests/parity/cases.tsv index 3e7e16214..27ba71458 100644 --- a/tests/parity/cases.tsv +++ b/tests/parity/cases.tsv @@ -1255,3 +1255,14 @@ report-render m-variants-missing gen:report-corpus.sh --summary m-variants-missi report-render m-variants-ok gen:report-corpus.sh --summary m-variants-ok.json -o m-variants-ok.txt -o m-variants-ok.html m-variants-ok.txt,m-variants-ok.html bytes PYTHON_IMAGE report-render m-variants-stale gen:report-corpus.sh --summary m-variants-stale.json -o m-variants-stale.txt -o m-variants-stale.html m-variants-stale.txt,m-variants-stale.html bytes PYTHON_IMAGE report-render m-variants-unreadable gen:report-corpus.sh --summary m-variants-unreadable.json -o m-variants-unreadable.txt -o m-variants-unreadable.html m-variants-unreadable.txt,m-variants-unreadable.html bytes PYTHON_IMAGE +report-render m-pgx-ok gen:report-corpus.sh --summary m-pgx-ok.json -o m-pgx-ok.txt -o m-pgx-ok.html m-pgx-ok.txt,m-pgx-ok.html bytes PYTHON_IMAGE +report-render m-pgx-stale gen:report-corpus.sh --summary m-pgx-stale.json -o m-pgx-stale.txt -o m-pgx-stale.html m-pgx-stale.txt,m-pgx-stale.html bytes PYTHON_IMAGE +report-render m-pgx-states gen:report-corpus.sh --summary m-pgx-states.json -o m-pgx-states.txt -o m-pgx-states.html m-pgx-states.txt,m-pgx-states.html bytes PYTHON_IMAGE +report-render m-pgx-calls gen:report-corpus.sh --summary m-pgx-calls.json -o m-pgx-calls.txt -o m-pgx-calls.html m-pgx-calls.txt,m-pgx-calls.html bytes PYTHON_IMAGE +report-render m-hla-ok gen:report-corpus.sh --summary m-hla-ok.json -o m-hla-ok.txt -o m-hla-ok.html m-hla-ok.txt,m-hla-ok.html bytes PYTHON_IMAGE +report-render m-hla-none gen:report-corpus.sh --summary m-hla-none.json -o m-hla-none.txt -o m-hla-none.html m-hla-none.txt,m-hla-none.html bytes PYTHON_IMAGE +report-render m-hla-failed gen:report-corpus.sh --summary m-hla-failed.json -o m-hla-failed.txt -o m-hla-failed.html m-hla-failed.txt,m-hla-failed.html bytes PYTHON_IMAGE +report-render m-mito-ok gen:report-corpus.sh --summary m-mito-ok.json -o m-mito-ok.txt -o m-mito-ok.html m-mito-ok.txt,m-mito-ok.html bytes PYTHON_IMAGE +report-render m-mito-notes gen:report-corpus.sh --summary m-mito-notes.json -o m-mito-notes.txt -o m-mito-notes.html m-mito-notes.txt,m-mito-notes.html bytes PYTHON_IMAGE +report-render m-mito-copies gen:report-corpus.sh --summary m-mito-copies.json -o m-mito-copies.txt -o m-mito-copies.html m-mito-copies.txt,m-mito-copies.html bytes PYTHON_IMAGE +report-render m-mito-failed gen:report-corpus.sh --summary m-mito-failed.json -o m-mito-failed.txt -o m-mito-failed.html m-mito-failed.txt,m-mito-failed.html bytes PYTHON_IMAGE diff --git a/tests/parity/gen/report-corpus.sh b/tests/parity/gen/report-corpus.sh index d16728bc9..69cf5fe73 100644 --- a/tests/parity/gen/report-corpus.sh +++ b/tests/parity/gen/report-corpus.sh @@ -114,6 +114,89 @@ mut sex-agree '.run.declared_sex = "male" | .sections.sex_check.state = "ok" | . mut counts '.sections.variants.state = "ok" | .sections.variants.data = {"total": 4.5, "pass": true, "snps": "12", "indels": null} | .sections.coverage.state = "ok" | .sections.coverage.data.mean_depth = 31' mut counts-float '.sections.coverage.state = "ok" | .sections.coverage.data.mean_depth = 30.05' +# PGx, HLA and mitochondrial variants (port-report-pgx): each section in each +# state, the rows each writer prints, and texts HTML escapes. +pgx='.sections.pharmcat = {"title": "PharmCAT", "state": "ok", "source": "pharmcat/S.report.json", "file_date": null, + "note": null, "data": {"version": "3.4.0", "html_report": true}} + | .sections.cpic = {"title": "CPIC ", "state": "ok", "source": "cpic/S_phenotypes.tsv", "file_date": null, + "note": null, "data": {"genes": [{"gene": "CYP2D6", "diplotype": "*1/*4", "phenotype": "Intermediate ", + "status": "non-normal"}, {"gene": "CYP2C19", "diplotype": "*1/*17 & *2", "phenotype": "Rapid", "status": "ambiguous"}, + {"gene": "DPYD", "diplotype": null, "phenotype": null, "status": null}], "non_normal": 1, "ambiguous": 1, + "not_called": 2, "unclassified": 1, "parse_failed": false, "warnings": ["two & more"]}} + | .sections.pypgx = {"title": "pypgx", "state": "ok", "source": "pypgx/S.tsv", "file_date": null, "note": null, + "data": {"genes_total": 6, "genes_called": 3, "cyp2d6": "*1/*2", "cnv_call": {"CYP2D6": "2", "UGT2B17": "0 "}, + "not_callable": ["GSTT1", "CYP2E1"], "low_gq": [{"gene": "UGT1A1", "diplotype": "*1/*28", "weakest": ""}], + "depth_check": {"status": null, "gene_mapq1_share": "0.99", "body_mapq20_share": null}, + "comparison": {"conflicts": 2, "one_tool_only": 0}}} + | .sections.cyrius = {"title": "Cyrius", "state": "ok", "source": "cyrius/S.tsv", "file_date": null, "note": null, + "data": {"genotype": "*1/*2"}} + | .cyp2d6 = {"calls": {"PharmCAT": "*1/*4", "pypgx": "*1/*2", "Cyrius": null}, "agree": false, + "consensus": {"result": null, "passed_to_pharmcat": false, "reason": null}, "probable": {"diplotype": "*1/*2", + "phenotype": "Normal Metabolizer", "label": "probable CYP2D6 call (ONE caller)"}}' +mut pgx-ok "$pgx" +mut pgx-stale "$pgx"' | .sections.cpic.state = "stale" | .sections.cpic.note = "from <2020>" + | .sections.pypgx.state = "failed" | .sections.pypgx.note = "exit 1" | .sections.pypgx.data = {} + | .sections.cyrius.state = "stale" | .sections.cyrius.note = "old" | .sections.pharmcat.data = {} + | .cyp2d6.agree = null | .cyp2d6.consensus = {"result": "*1/*1", "passed_to_pharmcat": true, "reason": "agree"} + | .cyp2d6.probable = null | .new_since = {"previous_utc": "2026-01-01", "sections": ["cyrius", "cpic"]}' +mut pgx-states "$pgx"' | .sections.pharmcat.state = "unreadable" | .sections.pharmcat.note = "bad json" + | .sections.cpic.data.parse_failed = true | .sections.cpic.data.non_normal = 0 | .sections.cpic.data.ambiguous = 0 + | .sections.cpic.source = "S_phenotypes.tsv" | .sections.pypgx.state = "missing" | .sections.pypgx.data = {} + | .cyp2d6.calls = {"PharmCAT": null, "pypgx": null, "Cyrius": null} | .cyp2d6.consensus = null | .cyp2d6.probable = null' +mut pgx-calls "$pgx"' | .sections.pharmcat.state = "failed" | .sections.pharmcat.note = "exit 2" + | .sections.cpic.state = "missing" | .sections.pypgx.data.comparison.one_tool_only = 3 + | .sections.pypgx.data.depth_check = {"status": "low", "gene_mapq1_share": "0.5", "gene_mapq20_share": "0.4", + "body_mapq1_share": "0.9", "body_mapq20_share": "0.8"} | .cyp2d6.agree = true + | .cyp2d6.calls = {"PharmCAT": null, "pypgx": "*1/*2", "Cyrius": "*1/*2"} + | .cyp2d6.consensus = {"result": "*1/*2", "passed_to_pharmcat": true, "reason": "pypgx & Cyrius agree"}' +hla='.databases.hla_database = "IPD-IMGT/HLA 3.60" | .sections.hla = {"title": "HLA", "state": "ok", + "source": "hla_t1k/S_hla_genotype.tsv", "file_date": null, "note": null, "data": {"loci": [ + {"gene": "HLA-A", "alleles": ["A*02:01", "A*31:01"], "quality": ["40", "12"]}, + {"gene": "HLA-DRB5", "alleles": ["DRB5*01:01,DRB5*01:02,DRB5*01:03"], "quality": ["0"], "candidates": [12], + "low_confidence": true, "withheld_from_pharmcat": true}, + {"gene": "HLA-B", "alleles": ["B*57:01", "B*08:01"], "quality": [0, "x"], "low_confidence": true}, + {"gene": "HLA-HFE", "alleles": ["HFE*001:01", "HFE*002:01"], "quality": ["40"], "label": "a