From 997d5e7d5db6f3d409008644aebf3b119e719ff5 Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:54:09 +0100 Subject: [PATCH 1/3] feat(report): render the PGx, HLA and mitochondrial sections with og og report render now writes PharmCAT, CPIC, pypgx with the PGx copy number, CYP2D6 across the callers with step 36's verdict, HLA typing with its derived lines and the KIR genes, and the mitochondrial variants. Parity: 11 new report-render cases reach each state of these sections; all 544 items in 110 cases agree with render_report.py. The chapter-order test of port_report_core.rs now holds for any set of ported sections, and the corpus filter drops the chart figures, which the charts port owns. Bead: D1 Bead: D14 --- rust/crates/og-render/src/chapters.rs | 10 +- rust/crates/og-render/src/html.rs | 55 +- rust/crates/og-render/src/model.rs | 28 + rust/crates/og-render/src/sections/hla.rs | 344 ++++++++- rust/crates/og-render/src/sections/mito.rs | 479 +++++++++++- rust/crates/og-render/src/sections/pgx.rs | 709 +++++++++++++++++- .../og-render/tests/port_report_core.rs | 11 +- tests/parity/cases.tsv | 11 + tests/parity/gen/report-corpus.sh | 94 ++- 9 files changed, 1727 insertions(+), 14 deletions(-) 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..9420ad2d0 100644 --- a/rust/crates/og-render/src/sections/hla.rs +++ b/rust/crates/og-render/src/sections/hla.rs @@ -1 +1,343 @@ -//! 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). +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/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