From a8f3b4e701e3006e6b9798ce1938242858fe39f8 Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:36:45 +0100 Subject: [PATCH 01/29] feat(report): write the ancestry, haplogroup and PRS sections in Rust og report render now writes the fine-grained ancestry, mitochondrial haplogroup, Y chromosome (Yleaf, the second placement, Y-STR), archaic, ancient and polygenic score sections, byte for byte as bin/render_report.py does. A section lays out its tables and pictures in a Raw line, escaping each text it puts in. The report-render parity cases gain 14 summaries made from case c-full (every state, a planted escape in the haplogroup links, the painting and its legend, the raw and sex-specific scores, numbers where the summary writes text), and the oracle filter drops the charts, which og report charts will write. Bead: D4 Bead: D5 Bead: D8 --- rust/crates/og-render/src/chapters.rs | 15 +- rust/crates/og-render/src/html.rs | 1 + rust/crates/og-render/src/lib.rs | 6 +- rust/crates/og-render/src/model.rs | 10 +- .../crates/og-render/src/sections/ancestry.rs | 176 +++++++++- .../og-render/src/sections/ancestry_fine.rs | 286 +++++++++++++++- rust/crates/og-render/src/sections/ancient.rs | 132 ++++++- rust/crates/og-render/src/sections/archaic.rs | 91 ++++- .../og-render/src/sections/haplogroups.rs | 201 ++++++++++- rust/crates/og-render/src/sections/prs.rs | 324 +++++++++++++++++- rust/crates/og-render/src/sections/y_tree.rs | 142 +++++++- rust/crates/og-render/src/sections/ystr.rs | 102 +++++- tests/parity/cases.tsv | 16 + tests/parity/gen/report-corpus.sh | 80 ++++- 14 files changed, 1555 insertions(+), 27 deletions(-) diff --git a/rust/crates/og-render/src/chapters.rs b/rust/crates/og-render/src/chapters.rs index 92ace64e9..5367904e9 100644 --- a/rust/crates/og-render/src/chapters.rs +++ b/rust/crates/og-render/src/chapters.rs @@ -207,21 +207,24 @@ pub fn writer(sid: &str) -> Option<(TextFn, HtmlFn)> { "locityper" => None, - "ancestry-fine" => None, + "ancestry-fine" => Some((sections::ancestry_fine::text, sections::ancestry_fine::html)), - "mt-haplogroup" => None, + "mt-haplogroup" => Some((sections::haplogroups::text, sections::haplogroups::html)), - "y-chromosome" => None, + "y-chromosome" => Some(( + sections::haplogroups::y_chromosome_text, + sections::haplogroups::y_chromosome_html, + )), - "archaic" => None, + "archaic" => Some((sections::archaic::text, sections::archaic::html)), - "ancient" => None, + "ancient" => Some((sections::ancient::text, sections::ancient::html)), "roh" => None, "traits-panel" => None, - "prs" => None, + "prs" => Some((sections::prs::text, sections::prs::html)), "bloodtype" => None, diff --git a/rust/crates/og-render/src/html.rs b/rust/crates/og-render/src/html.rs index 43e75f02b..34883da28 100644 --- a/rust/crates/og-render/src/html.rs +++ b/rust/crates/og-render/src/html.rs @@ -70,6 +70,7 @@ fn line(out: &mut Vec, l: &Line) { out.extend(items.iter().map(|t| format!("
  • {}
  • ", e(t)))); out.push(" ".to_owned()); } + Line::Raw(t) => out.push(t.clone()), } } diff --git a/rust/crates/og-render/src/lib.rs b/rust/crates/og-render/src/lib.rs index 839f2be52..83e46ab58 100644 --- a/rust/crates/og-render/src/lib.rs +++ b/rust/crates/og-render/src/lib.rs @@ -10,9 +10,9 @@ //! [`css`] the page's style. //! //! Ported so far: the frame (chapters, contents, section frames, the run -//! facts, what the report leaves out) and the quality-control and variant -//! sections. A section not ported yet is left out of both reports; the index -//! and charts verbs say they are not ported and exit 2. +//! facts, what the report leaves out) and the sections +//! [`chapters::writer`] names. A section not ported yet is left out of both +//! reports; the index and charts verbs say they are not ported and exit 2. pub mod chapters; pub mod charts; diff --git a/rust/crates/og-render/src/model.rs b/rust/crates/og-render/src/model.rs index 54ea06364..71093922f 100644 --- a/rust/crates/og-render/src/model.rs +++ b/rust/crates/og-render/src/model.rs @@ -3,8 +3,10 @@ //! `==`), and the document the section modules build from it, which the //! text and HTML writers lay out. //! -//! A section module never writes markup. It returns cards of typed lines; -//! every text in them is escaped once, by the HTML writer. +//! A section module returns cards of typed lines; every text in them is +//! escaped once, by the HTML writer. The one exception is [`Line::Raw`]: +//! a table or picture the section lays out itself, escaping each text it +//! puts in. use std::fmt; @@ -325,6 +327,10 @@ pub enum Line { Para(String), /// A plain bulleted list. List(Vec), + /// Markup a section built itself, each text in it escaped with + /// [`crate::escape::e`]: a table, a picture or a link whose layout the + /// report fixes byte for byte. + Raw(String), } /// One card: a title, the caveats shown under it, the notes saying its diff --git a/rust/crates/og-render/src/sections/ancestry.rs b/rust/crates/og-render/src/sections/ancestry.rs index f882faadb..f091cf9e9 100644 --- a/rust/crates/og-render/src/sections/ancestry.rs +++ b/rust/crates/og-render/src/sections/ancestry.rs @@ -1,2 +1,174 @@ -//! The report section `ancestry`. Empty until its unit of the 1.0.0 plan -//! lands. +//! What the ancestry chapter's sections share: reading a row of the +//! summary the way the Python report did (`r["key"]`, `f"{v:<35}"`, a list +//! walked with `for`), the percentages of the fine-grained ancestry, and the +//! links where a haplogroup can be explored further (steps 12 and 37). +//! +//! Used by the fine-grained ancestry, haplogroup, Y chromosome, archaic, +//! ancient and polygenic score sections. + +use pycompat::fmt::{fixed, ljust, rjust}; +use pycompat::json::Value; + +use crate::escape::e; +use crate::html::offsite_link; +use crate::model::{Line, Malformed, get, need, num, py, truthy}; +use crate::notes::EXPLORE_NOTE; + +/// Every explore address but the docs one carries the haplogroup in it. +pub const EXPLORE_DOCS: &str = "https://github.com/GeiserX/OwnGenome/blob/main/docs/"; + +/// The mark of an address that leaves this computer with the haplogroup in it. +pub const OFFSITE_HAPLOGROUP: &str = "opens a website; the address carries the haplogroup"; + +/// `str(v)` of an optional value: `None` when it is absent. +pub fn py_or(v: Option<&Value>, default: &str) -> Result { + v.map_or(Ok(default.to_owned()), py) +} + +/// `d.get(key, default)` as text: the default only when the key is absent. +pub fn get_text(d: &Value, key: &str, default: &str, path: &str) -> Result { + py_or(get(d, key, path)?, default) +} + +/// `str(d[key])`. +pub fn need_text(d: &Value, key: &str, path: &str) -> Result { + py(need(d, key, path)?) +} + +/// The items `for x in v` walks: a list's items, a string's characters, a +/// dict's keys. `None` and numbers cannot be walked. +pub fn items(v: &Value, path: &str) -> Result, Malformed> { + match v { + Value::Array(a) => Ok(a.clone()), + Value::Str(s) => Ok(s.chars().map(|c| Value::Str(c.to_string())).collect()), + Value::Object(o) => Ok(o.keys().map(|k| Value::Str(k.clone())).collect()), + _ => Err(Malformed(format!("{path} is not a list"))), + } +} + +/// `d.get(key, [])` walked with `for`: nothing when the key is absent. +pub fn list(d: &Value, key: &str, path: &str) -> Result, Malformed> { + match get(d, key, path)? { + None => Ok(Vec::new()), + Some(v) => items(v, &format!("{path}.{key}")), + } +} + +/// `len(v)`. +pub fn py_len(v: &Value, path: &str) -> Result { + match v { + Value::Array(a) => Ok(a.len()), + Value::Str(s) => Ok(s.chars().count()), + Value::Object(o) => Ok(o.len()), + _ => Err(Malformed(format!("{path} has no length"))), + } +} + +/// Which side `f"{v:N}"` (right) pads. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Align { + Left, + Right, +} + +/// `f"{v:N}"`: a string or a number padded to `width` +/// characters. A bool formats as the number it is, as in Python; `None`, a +/// list or a dict cannot take a width. +pub fn align(v: &Value, width: usize, side: Align, path: &str) -> Result { + let text = match v { + Value::Str(s) => s.clone(), + Value::Int(i) => i.to_string(), + Value::Float(_) => py(v)?, + Value::Bool(b) => u8::from(*b).to_string(), + _ => return Err(Malformed(format!("{path} cannot be padded"))), + }; + Ok(match side { + Align::Left => ljust(&text, width), + Align::Right => rjust(&text, width), + }) +} + +/// `f"{d[key]:N}"`. +pub fn need_align( + d: &Value, + key: &str, + width: usize, + side: Align, + path: &str, +) -> Result { + align(need(d, key, path)?, width, side, &format!("{path}.{key}")) +} + +/// A number as Python compares it with `>=`; anything else is an error. +pub fn number(v: &Value, path: &str) -> Result { + num(Some(v)).ok_or_else(|| Malformed(format!("{path} is not a number"))) +} + +/// A fraction as a whole percentage (`95%`), `.` for none. +pub fn share(v: Option<&Value>, path: &str) -> Result { + match v { + None | Some(Value::Null) => Ok(".".to_owned()), + Some(x) => Ok(format!("{}%", fixed(100.0 * number(x, path)?, 0))), + } +} + +/// A stat line whose value is markup built here. +pub fn stat_raw(label: &str, value: &str) -> Line { + Line::Raw(format!( + "
    {}{value}
    ", + e(label) + )) +} + +/// One explore entry of a haplogroup section: its name and address. +fn explore(d: &Value, path: &str) -> Result, Malformed> { + let at = format!("{path}.explore[]"); + list(d, "explore", path)? + .iter() + .map(|x| Ok((need_text(x, "name", &at)?, need(x, "url", &at)?.clone()))) + .collect() +} + +/// The text report's explore lines: `name: url` for each link, then the +/// note that nothing is uploaded; nothing when there is no link. +pub fn explore_lines(d: &Value, path: &str) -> Result, Malformed> { + let mut out = Vec::new(); + for (name, url) in explore(d, path)? { + out.push(format!("{name}: {}", py(&url)?)); + } + if !out.is_empty() { + out.push(EXPLORE_NOTE.to_owned()); + } + Ok(out) +} + +/// The HTML report's explore lines: one stat per link, each marked as +/// leaving this computer, and whether the card shows the explore caveat. +/// The address is escaped, so a branch name cannot open a tag or end the +/// attribute. +pub fn explore_html(d: &Value, path: &str) -> Result<(Vec, bool), Malformed> { + let mut out = Vec::new(); + for (name, url) in explore(d, path)? { + let Value::Str(url) = url else { + return Err(Malformed(format!("{path}.explore[].url is not a text"))); + }; + let why = if url.starts_with(EXPLORE_DOCS) { + crate::html::OFFSITE + } else { + OFFSITE_HAPLOGROUP + }; + out.push(stat_raw(&name, &offsite_link(&url, &url, why))); + } + let note = !out.is_empty(); + Ok((out, note)) +} + +/// `d.get(key, ".") not in (".", "")`: a value worth a line. +pub fn shown<'a>(d: &'a Value, key: &str, path: &str) -> Result, Malformed> { + Ok(get(d, key, path)?.filter(|v| !matches!(v, Value::Str(s) if s.is_empty() || s == "."))) +} + +/// `bool(d.get(key))`. +pub fn has(d: &Value, key: &str, path: &str) -> Result { + Ok(get(d, key, path)?.is_some_and(truthy)) +} diff --git a/rust/crates/og-render/src/sections/ancestry_fine.rs b/rust/crates/og-render/src/sections/ancestry_fine.rs index d87e67915..67b9c2134 100644 --- a/rust/crates/og-render/src/sections/ancestry_fine.rs +++ b/rust/crates/og-render/src/sections/ancestry_fine.rs @@ -1,2 +1,284 @@ -//! The report section `ancestry_fine`. Empty until its unit of the 1.0.0 -//! plan lands. +//! Fine-grained ancestry (step 26b): the continental and regional groups +//! with their bootstrap ranges, the groups the model leaves out, the share +//! of the genome each group paints, and the chromosome painting with its +//! legend. + +use pycompat::json::Value; + +use crate::escape::e; +use crate::model::{ + Card, HtmlPart, Inline, Line, Malformed, State, Summary, get, need, py, truthy, +}; +use crate::notes::ANCESTRY_FINE_NOTE; +use crate::sections::ancestry::{get_text, has, list, need_text, number, share}; +use crate::sections::haplogroups::shows_body; +use crate::{html, text}; + +const PATH: &str = "sections.ancestry_fine.data"; + +/// The two levels the model reports, with their headings. +pub const LEVELS: [(&str, &str); 2] = [ + ("continent", "Continental groups"), + ("region", "Regional groups"), +]; + +/// The groups of one level worth a line: every continental group of 1% or +/// more, a regional one of 1% or more or whose bootstrap range reaches 5%. +pub fn rows(d: &Value, level: &str) -> Result, Malformed> { + let at = format!("{PATH}.{level}[]"); + let mut out = Vec::new(); + for r in list(d, level, PATH)? { + let f = need(&r, "fraction", &at)?; + if matches!(f, Value::Null) { + continue; + } + let keep = number(f, &format!("{at}.fraction"))? >= 0.01 + || (level == "region" + && match get(&r, "high", &at)?.filter(|v| truthy(v)) { + Some(h) => number(h, &format!("{at}.high"))? >= 0.05, + None => false, + }); + if keep { + out.push(r); + } + } + Ok(out) +} + +/// ` (93% to 97%)`: the bootstrap range of a row, when it has one. +pub fn spread(r: &Value, at: &str) -> Result { + match get(r, "low", at)? { + None | Some(Value::Null) => Ok(String::new()), + Some(low) => Ok(format!( + " ({} to {})", + share(Some(low), at)?, + share(Some(need(r, "high", at)?), at)? + )), + } +} + +/// `(.*) \((.*) group\)` matched against the whole name: the group and its +/// kind (`Oceania (continental group)`), or the name alone. +fn name_and_kind(group: &str) -> (&str, &str) { + let Some(inner) = group.strip_suffix(" group)") else { + return (group, ""); + }; + match inner.rfind(" (") { + Some(i) if !inner.contains('\n') => (&inner[..i], &inner[i + 2..]), + _ => (group, ""), + } +} + +/// The groups the model leaves out, as one sentence, or "". +pub fn left_out(d: &Value) -> Result { + if !has(d, "left_out", PATH)? { + return Ok(String::new()); + } + let at = format!("{PATH}.left_out[]"); + // One row per level ("Oceania (continental group)"); a group both + // levels leave out reads as one name. + let mut kinds: Vec<(String, Vec)> = Vec::new(); + for r in list(d, "left_out", PATH)? { + let Value::Str(group) = need(&r, "group", &at)? else { + return Err(Malformed(format!("{at}.group is not a text"))); + }; + let (name, kind) = name_and_kind(group); + let i = match kinds.iter().position(|(n, _)| n == name) { + Some(i) => i, + None => { + kinds.push((name.to_owned(), Vec::new())); + kinds.len() - 1 + } + }; + if !kind.is_empty() && !kinds[i].1.iter().any(|k| k == kind) { + kinds[i].1.push(kind.to_owned()); + } + } + let names: Vec = kinds + .iter() + .map(|(n, k)| { + if k.is_empty() { + n.clone() + } else { + format!("{n} ({} group)", k.join(" and ")) + } + }) + .collect(); + Ok(format!( + "Left out of the model: {}. The panel has fewer than {} unrelated reference people for \ + it, so its ancestry is spread over the groups shown.", + names.join(", "), + get_text(d, "min_references", "10", PATH)? + )) +} + +/// The painted shares, or why there are none, and the chromosomes the +/// painting failed on. +pub fn painted(d: &Value) -> Result { + let codes = list(d, "not_painted", PATH)? + .iter() + .map( + |r| match need(r, "code", &format!("{PATH}.not_painted[]"))? { + Value::Str(c) => Ok(c.clone()), + _ => Err(Malformed(format!( + "{PATH}.not_painted[].code is not a text" + ))), + }, + ) + .collect::, _>>()?; + let failed = codes.join(", "); + let mut text = if has(d, "painting", PATH)? { + let at = format!("{PATH}.painting[]"); + list(d, "painting", PATH)? + .iter() + .map(|r| { + Ok(format!( + "{} {}", + need_text(r, "group", &at)?, + share(Some(need(r, "fraction", &at)?), &at)? + )) + }) + .collect::, Malformed>>()? + .join(", ") + } else if failed.is_empty() { + "none (no chromosome long enough)".to_owned() + } else { + "none".to_owned() + }; + if !failed.is_empty() { + text += &format!("; not painted: RFMix failed on {failed}"); + } + Ok(text) +} + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head( + s, + "ancestry_fine", + Some("Fine-Grained Ancestry (ADMIXTURE, RFMix)"), + )? + else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if !head.ran { + return Ok(w); + } + let d = s.section("ancestry_fine").data; + for (level, title) in LEVELS { + let rs = rows(d, level)?; + if rs.is_empty() { + continue; + } + w.push(format!(" {title} (fraction, bootstrap range):")); + let at = format!("{PATH}.{level}[]"); + for r in &rs { + let group = crate::sections::ancestry::need_align( + r, + "group", + 50, + crate::sections::ancestry::Align::Left, + &at, + )?; + let frac = share(Some(need(r, "fraction", &at)?), &at)?; + w.push(format!( + " {group} {}{}", + pycompat::fmt::rjust(&frac, 5), + spread(r, &at)? + )); + } + } + let lo = left_out(d)?; + if !lo.is_empty() { + w.push(format!(" {lo}")); + } + let lead = if has(d, "painting", PATH)? { + " Chromosome painting, share of the painted genome: " + } else { + " Chromosome painting: " + }; + w.push(format!("{lead}{}", painted(d)?)); + w.push(format!(" {ANCESTRY_FINE_NOTE}")); + w.push(String::new()); + Ok(w) +} + +/// The HTML report's card, when step 26b has a result: one table per +/// level, the painted shares, and the painting with its legend. +pub fn html(s: &Summary) -> Result { + let sec = s.section("ancestry_fine"); + if sec.state == State::Missing { + return Ok(HtmlPart::default()); + } + let mut c = Card { + title: "Fine-Grained Ancestry".to_owned(), + full: true, + ..Card::default() + }; + if shows_body(&sec) { + let d = sec.data; + for (level, title) in LEVELS { + let at = format!("{PATH}.{level}[]"); + let mut rows_html = String::new(); + for r in rows(d, level)? { + let range = spread(&r, &at)?; + let range = range.trim_matches([' ', '(', ')']); + rows_html += &format!( + "{}{}{}\n", + e(&need_text(&r, "group", &at)?), + e(&share(Some(need(&r, "fraction", &at)?), &at)?), + e(if range.is_empty() { "." } else { range }) + ); + } + if !rows_html.is_empty() { + c.body.extend([ + Line::Raw(format!("

    {}

    ", e(title))), + Line::Raw(" ".to_owned()), + Line::Raw( + " " + .to_owned(), + ), + Line::Raw(rows_html.trim_end_matches('\n').to_owned()), + Line::Raw("
    GroupFractionBootstrap range
    ".to_owned()), + ]); + } + } + let lo = left_out(d)?; + if !lo.is_empty() { + c.body.push(Line::Para(lo)); + } + c.body.push(Line::Stat( + "Painted genome".to_owned(), + Inline::Text(painted(d)?), + )); + if let Some(png) = get(d, "painting_png", PATH)?.filter(|v| truthy(v)) { + c.body.push(Line::Raw(format!( + " \"Chromosome", + e(&py(png)?) + ))); + let at = format!("{PATH}.legend[]"); + let keys = list(d, "legend", PATH)? + .iter() + .map(|x| { + Ok(format!( + "■ {}", + e(&need_text(x, "colour", &at)?), + e(&need_text(x, "group", &at)?) + )) + }) + .collect::, Malformed>>()?; + c.body.push(Line::Raw(format!( + "

    {} ■ not painted

    ", + keys.join(" ") + ))); + } + c.caveats.push(ANCESTRY_FINE_NOTE); + } + Ok(HtmlPart { + cards: vec![html::card(&sec, c)?], + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/ancient.rs b/rust/crates/og-render/src/sections/ancient.rs index 0bcd3409a..d6000869a 100644 --- a/rust/crates/og-render/src/sections/ancient.rs +++ b/rust/crates/og-render/src/sections/ancient.rs @@ -1,2 +1,130 @@ -//! The report section `ancient`. Empty until its unit of the 1.0.0 plan -//! lands. +//! Ancient populations (step 44): where the genome falls on a PCA of +//! present-day West Eurasians, and the ancient groups of the AADR nearest +//! to it there, with the plot. + +use pycompat::json::Value; + +use crate::escape::e; +use crate::model::{Card, HtmlPart, Line, Malformed, State, Summary, get, py, truthy}; +use crate::notes::ANCIENT_NOTE; +use crate::sections::ancestry::{Align, get_text, need_align, need_text}; +use crate::{html, text}; + +const PATH: &str = "sections.ancient.data"; +const ROW: &str = "sections.ancient.data.nearest[]"; + +/// `(d.get("nearest") or [])[:10]`: the ten nearest groups. +fn nearest(d: &Value) -> Result, Malformed> { + match get(d, "nearest", PATH)?.filter(|v| truthy(v)) { + None => Ok(Vec::new()), + Some(Value::Array(a)) => Ok(a.iter().take(10).collect()), + Some(_) => Err(Malformed(format!("{PATH}.nearest is not a list"))), + } +} + +/// Step 44's lines, the same in both reports: what the PCA was built from, +/// then the nearest groups. +pub fn lines(d: &Value) -> Result, Malformed> { + let g = |k: &str| get_text(d, k, ".", PATH); + let mut out = vec![ + format!( + "AADR {}: {} of {} sites used; axes from {} present-day West Eurasians, {} ancient \ + people in {} groups projected.", + g("release")?, + g("sites_used")?, + g("panel_sites")?, + g("present_day_people")?, + g("ancient_people")?, + g("ancient_groups")? + ), + "Nearest ancient groups:".to_owned(), + ]; + for r in nearest(d)? { + let n = |k: &str| need_text(r, k, ROW); + out.push(format!( + " {}. {} ({}, {}; {} people)", + need_align(r, "rank", 2, Align::Right, ROW)?, + n("group")?, + n("dates")?, + n("country")?, + n("n")? + )); + } + Ok(out) +} + +/// The text report's block, with the plot's file name. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head( + s, + "ancient", + Some("Ancient Populations (AADR PCA, step 44)"), + )? + else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if head.ran { + w.extend( + lines(s.section("ancient").data)? + .iter() + .map(|l| format!(" {l}")), + ); + w.push(format!( + " {ANCIENT_NOTE} Plot: ancient/{}_ancient_pca.svg", + py(s.need("sample")?)? + )); + w.push(String::new()); + } + Ok(w) +} + +/// The HTML report's card: the nearest groups as a table, and the plot as +/// an image (shown, never run as part of the page). +pub fn html(s: &Summary) -> Result { + let sec = s.section("ancient"); + if sec.state == State::Missing { + return Ok(HtmlPart::default()); + } + let mut c = Card { + title: "Ancient Populations".to_owned(), + full: true, + ..Card::default() + }; + if crate::sections::haplogroups::shows_body(&sec) { + let d = sec.data; + let first = lines(d)?.swap_remove(0); + let mut rows = String::new(); + for r in nearest(d)? { + rows += ""; + for k in ["rank", "group", "dates", "country", "locality", "n"] { + rows += &format!("{}", e(&need_text(r, k, ROW)?)); + } + rows += "\n"; + } + c.body = vec![ + Line::Para(first), + Line::Raw(" ".to_owned()), + Line::Raw( + " \ + " + .to_owned(), + ), + Line::Raw(rows.trim_end_matches('\n').to_owned()), + Line::Raw("
    #Ancient groupDatesCountryPlacePeople
    ".to_owned()), + ]; + if let Some(svg) = get(d, "plot_svg", PATH)?.filter(|v| truthy(v)) { + c.body.push(Line::Raw(format!( + " \"PC1", + e(&py(svg)?) + ))); + } + c.caveats.push(ANCIENT_NOTE); + } + Ok(HtmlPart { + cards: vec![html::card(&sec, c)?], + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/archaic.rs b/rust/crates/og-render/src/sections/archaic.rs index 4295ec431..42cc364a2 100644 --- a/rust/crates/og-render/src/sections/archaic.rs +++ b/rust/crates/og-render/src/sections/archaic.rs @@ -1,2 +1,89 @@ -//! The report section `archaic`. Empty until its unit of the 1.0.0 plan -//! lands. +//! Archaic ancestry (step 43, IBDmix): the share of the analysed autosomes +//! in Neanderthal and Denisovan segments, and the longest segments. + +use pycompat::fmt::fixed; +use pycompat::json::Value; + +use crate::model::{Card, Inline, Line, Malformed, State, Summary, need, py}; +use crate::notes::ARCHAIC_NOTE; +use crate::sections::ancestry::{get_text, has, items, need_text, number}; +use crate::{html, text}; + +const PATH: &str = "sections.archaic.data"; + +/// A length in base pairs as megabases (`50.0 Mb`). +fn mb(d: &Value, key: &str) -> Result { + let bp = number(need(d, key, PATH)?, &format!("{PATH}.{key}"))?; + Ok(format!("{} Mb", fixed(bp / 1e6, 1))) +} + +/// Step 43's lines, the same in both reports. +pub fn lines(d: &Value) -> Result, Malformed> { + let n = |k: &str| need_text(d, k, PATH); + let mut out = Vec::new(); + for (name, key) in [("Neanderthal", "neanderthal"), ("Denisovan", "denisovan")] { + out.push(format!( + "{name}: {}% ({} in {} segments)", + n(&format!("{key}_percent"))?, + mb(d, &format!("{key}_bp"))?, + n(&format!("{key}_segments"))? + )); + } + out.push(format!( + "Analysed: {} of the autosomes; modern frequencies: 1000 Genomes {}", + mb(d, "analysed_bp")?, + get_text(d, "population", ".", PATH)? + )); + if has(d, "longest", PATH)? { + out.push("Longest segments:".to_owned()); + for x in items(need(d, "longest", PATH)?, &format!("{PATH}.longest"))? { + out.push(format!(" {}", py(&x)?)); + } + } + Ok(out) +} + +/// The text report's block. It ends without a blank line, as the Python +/// report's did. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "archaic", Some("Archaic Ancestry (IBDmix, step 43)"))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if head.ran { + let d = s.section("archaic").data; + w.extend(lines(d)?.iter().map(|l| format!(" {l}"))); + w.push(format!(" {ARCHAIC_NOTE}")); + } + Ok(w) +} + +/// The HTML report's card, when step 43 has a result. +pub fn html(s: &Summary) -> Result { + let sec = s.section("archaic"); + if sec.state == State::Missing { + return Ok(crate::model::HtmlPart::default()); + } + let mut c = Card { + title: "Archaic Ancestry (Neanderthal, Denisovan)".to_owned(), + ..Card::default() + }; + if sec.state.ran() { + let d = sec.data; + for (label, key) in [ + ("Neanderthal", "neanderthal_percent"), + ("Denisovan", "denisovan_percent"), + ] { + c.body.push(Line::Stat( + label.to_owned(), + Inline::Text(format!("{}%", need_text(d, key, PATH)?)), + )); + } + c.body.extend(lines(d)?.into_iter().map(Line::Para)); + c.caveats.push(ARCHAIC_NOTE); + } + Ok(crate::model::HtmlPart { + cards: vec![html::card(&sec, c)?], + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/haplogroups.rs b/rust/crates/og-render/src/sections/haplogroups.rs index 8f28f8f32..94fdad73e 100644 --- a/rust/crates/og-render/src/sections/haplogroups.rs +++ b/rust/crates/og-render/src/sections/haplogroups.rs @@ -1,2 +1,199 @@ -//! The report section `haplogroups`. Empty until its unit of the 1.0.0 plan -//! lands. +//! The haplogroups: the mitochondrial one with haplocheck's contamination +//! verdict (step 12), and the Y chromosome section, which shows Yleaf's +//! haplogroup (step 37), the second placement on the current public tree +//! ([`crate::sections::y_tree`]) and the Y-STR markers +//! ([`crate::sections::ystr`]). Each haplogroup carries the links where it +//! can be explored further. + +use pycompat::json::Value; + +use crate::model::{Card, Colour, HtmlPart, Inline, Line, Malformed, Section, State, Summary, py}; +use crate::notes::EXPLORE_NOTE; +use crate::sections::ancestry::{explore_html, explore_lines, get_text, need_text}; +use crate::sections::{y_tree, ystr}; +use crate::{html, text}; + +/// The Status badge of a section that ran: Stale for an earlier run's +/// result, else Complete. +pub fn done_badge(sec: &Section<'_>) -> Inline { + if sec.state == State::Stale { + Inline::Badge("Stale".to_owned(), Colour::Yellow) + } else { + Inline::Badge("Complete".to_owned(), Colour::Green) + } +} + +/// Whether a card's lines are shown: the card says Not run, Failed or +/// Unreadable in their place otherwise. +pub fn shows_body(sec: &Section<'_>) -> bool { + !matches!( + sec.state, + State::Missing | State::Failed | State::Unreadable + ) +} + +fn path(sec: &Section<'_>) -> String { + format!("sections.{}.data", sec.key) +} + +/// haplocheck's verdict: whether the reads hold two mtDNA haplogroups. +pub fn contamination_text(sec: &Section<'_>) -> Result { + let Some(status) = sec.get("contamination_status")? else { + return Ok("not checked (needs step 20's Mutect2 chrM calls)".to_owned()); + }; + let word = match status { + Value::Str(s) => match s.as_str() { + "YES" => "YES, two mtDNA haplogroups in the reads (another person's DNA?)".to_owned(), + "NO" => "no".to_owned(), + "ND" => "not determined".to_owned(), + _ => s.clone(), + }, + Value::Array(_) | Value::Object(_) => { + return Err(Malformed(format!( + "{}.contamination_status is not a value", + path(sec) + ))); + } + v => py(v)?, + }; + let level = get_text(sec.data, "contamination_level", ".", &path(sec))?; + Ok(format!("{word} (level {level})")) +} + +/// The text report's mitochondrial block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "haplogroup", Some("Mitochondrial Haplogroup"))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if head.ran { + let sec = s.section("haplogroup"); + let p = path(&sec); + w.push(format!( + " Haplogroup: {}", + need_text(sec.data, "haplogroup", &p)? + )); + w.push(format!( + " Contamination (haplocheck): {}", + contamination_text(&sec)? + )); + w.extend( + explore_lines(sec.data, &p)? + .iter() + .map(|l| format!(" {l}")), + ); + w.push(String::new()); + } + Ok(w) +} + +/// The HTML report's mitochondrial card. It is always there, so a run +/// without step 12 says so. +pub fn html(s: &Summary) -> Result { + let sec = s.section("haplogroup"); + let mut c = Card { + title: "Mitochondrial Haplogroup".to_owned(), + ..Card::default() + }; + if shows_body(&sec) { + let p = path(&sec); + c.body = vec![ + Line::Stat("Status".to_owned(), done_badge(&sec)), + Line::Stat( + "Haplogroup".to_owned(), + Inline::Text(get_text(sec.data, "haplogroup", ".", &p)?), + ), + Line::Stat( + "Contamination (haplocheck)".to_owned(), + Inline::Text(contamination_text(&sec)?), + ), + ]; + let (links, note) = explore_html(sec.data, &p)?; + c.body.extend(links); + if note { + c.caveats.push(EXPLORE_NOTE); + } + } + Ok(HtmlPart { + cards: vec![html::card(&sec, c)?], + files: Vec::new(), + }) +} + +/// The text report's block of Yleaf's haplogroup. +fn y_text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "y_haplogroup", Some("Y-Chromosome Haplogroup (Yleaf)"))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if head.ran { + let sec = s.section("y_haplogroup"); + let p = path(&sec); + let n = |k: &str| need_text(sec.data, k, &p); + w.push(format!( + " Haplogroup: {} ({} markers, QC-score {})", + n("haplogroup")?, + n("valid_markers")?, + n("qc_score")? + )); + w.extend( + explore_lines(sec.data, &p)? + .iter() + .map(|l| format!(" {l}")), + ); + w.push(String::new()); + } + Ok(w) +} + +/// The HTML report's card of Yleaf's haplogroup, when step 37 has a result. +fn y_card(s: &Summary) -> Result, Malformed> { + let sec = s.section("y_haplogroup"); + if sec.state == State::Missing { + return Ok(None); + } + let mut c = Card { + title: "Y-Chromosome Haplogroup".to_owned(), + ..Card::default() + }; + if shows_body(&sec) { + let p = path(&sec); + for (label, key) in [ + ("Haplogroup", "haplogroup"), + ("Markers", "valid_markers"), + ("QC-score", "qc_score"), + ] { + c.body.push(Line::Stat( + label.to_owned(), + Inline::Text(get_text(sec.data, key, ".", &p)?), + )); + } + let (links, note) = explore_html(sec.data, &p)?; + c.body.extend(links); + if note { + c.caveats.push(EXPLORE_NOTE); + } + } + Ok(Some(html::card(&sec, c)?)) +} + +/// The text report's Y chromosome blocks: Yleaf, the second placement, +/// the Y-STR markers. +pub fn y_chromosome_text(s: &Summary) -> Result, Malformed> { + let mut w = y_text(s)?; + w.extend(y_tree::text(s)?); + w.extend(ystr::text(s)?); + Ok(w) +} + +/// The HTML report's Y chromosome cards, each when its step has a result. +pub fn y_chromosome_html(s: &Summary) -> Result { + let cards = [y_card(s)?, y_tree::card(s)?, ystr::card(s)?] + .into_iter() + .flatten() + .collect(); + Ok(HtmlPart { + cards, + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/prs.rs b/rust/crates/og-render/src/sections/prs.rs index 3d832baa8..c9faba465 100644 --- a/rust/crates/og-render/src/sections/prs.rs +++ b/rust/crates/og-render/src/sections/prs.rs @@ -1 +1,323 @@ -//! The report section `prs`. Empty until its unit of the 1.0.0 plan lands. +//! Polygenic risk scores (step 25, pgsc_calc): each score with the variants +//! it matched, its percentile among the most similar reference group when +//! the ancestry panel is installed, the effect a published evaluation +//! measured in that group, and the line that says what the numbers can be +//! compared with: the ancestry caveat, the chrX rows left out and the +//! sex-specific scores when no sex was declared. +//! +//! The scores are shown as the summary lists them. + +use pycompat::json::Value; + +use crate::escape::e; +use crate::model::{Card, HtmlPart, Line, Malformed, Summary, get, is_str, need, py, truthy}; +use crate::notes::PRS_ANCESTRY_NOTE; +use crate::sections::ancestry::{Align, items, need_align, need_text, py_or}; +use crate::{html, text}; + +const PATH: &str = "sections.prs.data"; +const ROW: &str = "sections.prs.data.scores[]"; + +/// What a score of the other sex says in place of a percentile. +pub const NOT_APPLICABLE: &str = "not applicable to the declared sex"; + +/// Part of the line under the scores, so not a caveat of its own. +pub const EFFECT_TEXT: &str = "Effect: the odds ratio (OR) or hazard ratio (HR) a published \ + evaluation of the score measured in people of the same reference group, in that \ + evaluation's unit (its PGS Catalog PPM page says which). It describes the score in that \ + group, not your own risk, and is shown only for a group the score was evaluated in."; + +fn field<'a>(r: &'a Value, key: &str) -> Result, Malformed> { + get(r, key, ROW) +} + +/// `r.get(key)` is true. +fn on<'a>(r: &'a Value, key: &str) -> Result, Malformed> { + Ok(field(r, key)?.filter(|v| truthy(v))) +} + +fn other_sex(r: &Value) -> Result { + Ok(is_str(field(r, "sex_check")?, NOT_APPLICABLE)) +} + +/// The scores of the summary. +fn scores(d: &Value) -> Result, Malformed> { + items(need(d, "scores", PATH)?, &format!("{PATH}.scores")) +} + +/// The percentile cell: the percentile with its group, the note for a score +/// of the other sex, or "" when there is none. +pub fn percentile(r: &Value) -> Result { + if other_sex(r)? { + let sex = py_or(on(r, "applies_to")?, "sex")?; + return Ok(format!("{NOT_APPLICABLE} ({sex}-specific score)")); + } + match on(r, "percentile")? { + Some(p) => Ok(format!( + "{} ({})", + py(p)?, + py_or(on(r, "group")?, "group unknown")? + )), + None => Ok(String::new()), + } +} + +/// `", ".join(v)`: the items of a list of texts, or a text's characters. +fn join(v: &Value, at: &str) -> Result { + let parts = items(v, at)? + .into_iter() + .map(|x| match x { + Value::Str(s) => Ok(s), + _ => Err(Malformed(format!("{at} holds an item that is not a text"))), + }) + .collect::, _>>()?; + Ok(parts.join(", ")) +} + +/// The effect cell: the odds or hazard ratio the PGS Catalog records for +/// the score in the sample's group, a line saying the score has none for +/// that group, or "" when the sample has no group, the score does not apply +/// to the declared sex, or the table names no evaluations. The bool says +/// whether the cell is the ratio itself. +pub fn effect(r: &Value) -> Result<(String, bool), Malformed> { + let evaluated = match field(r, "evaluated_in")? { + None | Some(Value::Null) => return Ok((String::new(), false)), + Some(v) => v, + }; + if on(r, "percentile")?.is_some() && on(r, "group")?.is_some() && !other_sex(r)? { + if let Some(x) = on(r, "effect")? { + return Ok((py(x)?, true)); + } + let group = py(need(r, "group", ROW)?)?; + let place = if truthy(evaluated) { + format!( + "; evaluated in {}", + join(evaluated, &format!("{ROW}.evaluated_in"))? + ) + } else { + String::new() + }; + return Ok((format!("no published OR or HR for {group}{place}"), false)); + } + Ok((String::new(), false)) +} + +/// The chrX rows left out of the scores because no sex was given. +fn chrx_note(rs: &[Value]) -> Result { + let mut left = Vec::new(); + for r in rs { + let Some(c) = on(r, "chrx")? else { continue }; + let Value::Str(c) = c else { + return Err(Malformed(format!("{ROW}.chrx is not a text"))); + }; + if c.ends_with("left out") { + let first = pycompat::text::split(c.as_str(), None, None) + .first() + .copied() + .unwrap_or_default() + .to_owned(); + left.push(format!("{} ({first})", need_text(r, "pgs_id", ROW)?)); + } + } + if left.is_empty() { + return Ok(String::new()); + } + Ok(format!( + " Rows on chrX were left out, because the sample's sex was not given: {}. Give the sex \ + to score them (docs/25-prs.md).", + left.join(", ") + )) +} + +/// The sex-specific scores shown for any sex, when no sex was declared. +fn sex_note(rs: &[Value]) -> Result { + let mut named = Vec::new(); + for r in rs { + if is_str(field(r, "sex_check")?, "no sex declared") + && let Some(a) = on(r, "applies_to")? + { + named.push(format!("{} ({})", need_text(r, "pgs_id", ROW)?, py(a)?)); + } + } + if named.is_empty() { + return Ok(String::new()); + } + Ok(format!( + " No sex was declared, so these sex-specific scores are shown for any sex: {}. Give the \ + sex to mark a score of the other sex not applicable (docs/25-prs.md).", + named.join(", ") + )) +} + +/// Whether the summary's table names evaluations (its Evaluated_In column). +fn names_evaluations(rs: &[Value]) -> Result { + for r in rs { + if !matches!(field(r, "evaluated_in")?, None | Some(Value::Null)) { + return Ok(true); + } + } + Ok(false) +} + +/// The line under the scores, in both reports: what the numbers can be +/// compared with. +pub fn note(d: &Value) -> Result { + let rs = scores(d)?; + let tail = chrx_note(&rs)? + &sex_note(&rs)?; + if !get(d, "adjusted", PATH)?.is_some_and(truthy) { + return Ok(format!( + "Raw score only: no ancestry reference panel was installed, so there is no percentile \ + and the sum cannot be compared with anyone (scripts/setup.sh --ancestry-panel adds \ + it). {PRS_ANCESTRY_NOTE} See docs/25-prs.md.{tail}" + )); + } + let empty = Value::Object(indexmap::IndexMap::new()); + let anc = get(d, "ancestry", PATH)? + .filter(|v| truthy(v)) + .unwrap_or(&empty); + let at = format!("{PATH}.ancestry"); + let mut group = match get(anc, "population", &at)?.filter(|v| truthy(v)) { + Some(p) => py(p)?, + None => { + let mut g = String::new(); + for r in &rs { + if let Some(x) = on(r, "group")? { + g = py(x)?; + break; + } + } + g + } + }; + if group.is_empty() { + group = "most similar".to_owned(); + } + let panel = match get(anc, "panel", &at)?.filter(|v| truthy(v)) { + Some(p) => format!(" of the {} reference panel", py(p)?), + None => " of the reference panel".to_owned(), + }; + let low = if is_str(get(anc, "low_confidence", &at)?, "True") { + " The ancestry match is low-confidence: read the percentile with care." + } else { + "" + }; + let eff = if names_evaluations(&rs)? { + format!(" {EFFECT_TEXT}") + } else { + String::new() + }; + Ok(format!( + "Percentile: where the score falls among the {group} samples{panel}, the group whose \ + genetic ancestry is most similar to this sample's (pgsc_calc).{low} A percentile is not \ + a risk.{eff} {PRS_ANCESTRY_NOTE} See docs/25-prs.md.{tail}" + )) +} + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "prs", Some("Polygenic Risk Scores"))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if !head.ran { + return Ok(w); + } + let d = s.section("prs").data; + for r in scores(d)? { + let condition = need_align(&r, "condition", 35, Align::Left, ROW)?; + let n = |k: &str| need_text(&r, k, ROW); + let line = format!( + " {condition} {} ({}/{} variants matched) {}", + n("score")?, + n("matched")?, + n("total")?, + n("pgs_id")? + ); + let pct = percentile(&r)?; + let pct = if other_sex(&r)? { + format!(" {pct}") + } else if pct.is_empty() { + String::new() + } else { + format!(" percentile {pct}") + }; + let (e, is_ratio) = effect(&r)?; + let eff = if e.is_empty() { + String::new() + } else if is_ratio { + format!(" effect {e}") + } else { + format!(" {e}") + }; + w.push(format!("{line}{pct}{eff}")); + } + w.push(format!(" {}", note(d)?)); + w.push(String::new()); + Ok(w) +} + +/// The HTML report's card, when step 25 has a result: one row per score, +/// with the effect column when the table names evaluations. +pub fn html(s: &Summary) -> Result { + let sec = s.section("prs"); + if !sec.state.ran() { + return Ok(HtmlPart::default()); + } + let d = sec.data; + let rs = scores(d)?; + let adjusted = get(d, "adjusted", PATH)?.is_some_and(truthy); + let show_effect = adjusted && names_evaluations(&rs)?; + let mut rows = String::new(); + for x in &rs { + let n = |k: &str| need_text(x, k, ROW).map(|t| e(&t).into_owned()); + let pct = percentile(x)?; + rows += &format!( + "{}{}{}{}/{}{}", + n("condition")?, + n("pgs_id")?, + n("score")?, + n("matched")?, + n("total")?, + e(if pct.is_empty() { + "raw score only" + } else { + &pct + }) + ); + if show_effect { + rows += &format!("{}", e(&effect(x)?.0)); + } + rows += "\n"; + } + let effect_th = if show_effect { + "Effect in your group" + } else { + "" + }; + let card = Card { + title: format!( + "Polygenic Risk Scores{}", + if adjusted { + "" + } else { + " (raw, not percentiles)" + } + ), + full: true, + body: vec![ + Line::Raw(" ".to_owned()), + Line::Raw(format!( + " \ + {effect_th}" + )), + Line::Raw(rows.trim_end_matches('\n').to_owned()), + Line::Raw("
    ConditionPGSScoreVariants matchedPercentile
    ".to_owned()), + Line::Para(note(d)?), + ], + ..Card::default() + }; + Ok(HtmlPart { + cards: vec![html::card(&sec, card)?], + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/y_tree.rs b/rust/crates/og-render/src/sections/y_tree.rs index 892584a1c..da59d9f0e 100644 --- a/rust/crates/og-render/src/sections/y_tree.rs +++ b/rust/crates/og-render/src/sections/y_tree.rs @@ -1,2 +1,140 @@ -//! The report section `y_tree`. Empty until its unit of the 1.0.0 plan -//! lands. +//! The second Y placement (step 37, `og y tree-place`): the branch on a +//! public tree release newer than Yleaf's, the SNP counts on the path to +//! it, and one row per level of the walk. + +use crate::escape::e; +use crate::model::{Card, Inline, Line, Malformed, State, Summary}; +use crate::notes::{EXPLORE_NOTE, Y_TREE_NOTE}; +use crate::sections::ancestry::{ + Align, explore_html, get_text, list, need_align, need_text, py_or, shown, +}; +use crate::{html, text}; + +const PATH: &str = "sections.y_tree.data"; +const ROW: &str = "sections.y_tree.data.path[]"; + +/// The two lists of ancestral calls a reader should see, with their labels. +const ANCESTRAL: [(&str, &str); 2] = [ + ( + "Ancestral inside the terminal branch", + "ancestral_in_terminal", + ), + ( + "Ancestral on the path, outvoted by the SNPs below it", + "outvoted_ancestral", + ), +]; + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "y_tree", Some("Y Placement on the Current Public Tree"))? + else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if !head.ran { + return Ok(w); + } + let d = s.section("y_tree").data; + let n = |k: &str| need_text(d, k, PATH); + w.push(format!( + " Branch: {} (level {}, {})", + n("haplogroup")?, + n("level")?, + n("tree")? + )); + w.push(format!( + " Path SNPs: {} derived, {} ancestral, {} no call; {} SNPs called at depth {} or more", + n("path_derived")?, + n("path_ancestral")?, + n("path_no_call")?, + n("snps_called")?, + n("min_depth")? + )); + for (label, key) in ANCESTRAL { + if shown(d, key, PATH)?.is_some() { + w.push(format!(" {label}: {}", n(key)?)); + } + } + for x in list(d, "path", PATH)? { + let a = |k: &str, width: usize, side: Align| need_align(&x, k, width, side, ROW); + w.push(format!( + " {} {} {}D {}A {}N", + a("Level", 3, Align::Right)?, + a("Branch", 22, Align::Left)?, + a("Derived", 3, Align::Right)?, + a("Ancestral", 3, Align::Right)?, + a("No_call", 4, Align::Right)? + )); + } + w.push(format!(" {Y_TREE_NOTE}")); + w.push(String::new()); + Ok(w) +} + +/// The HTML report's card, when step 37 placed the sample on the tree. +pub fn card(s: &Summary) -> Result, Malformed> { + let sec = s.section("y_tree"); + if sec.state == State::Missing { + return Ok(None); + } + let mut c = Card { + title: "Y Placement on the Current Public Tree".to_owned(), + full: true, + ..Card::default() + }; + if sec.state.ran() { + let d = sec.data; + let g = |k: &str| get_text(d, k, ".", PATH); + for (label, key) in [ + ("Branch", "haplogroup"), + ("Level", "level"), + ("Tree", "tree"), + ] { + c.body + .push(Line::Stat(label.to_owned(), Inline::Text(g(key)?))); + } + c.body.push(Line::Para(format!( + "Path SNPs: {} derived, {} ancestral, {} no call; {} SNPs called at depth {} or more", + g("path_derived")?, + g("path_ancestral")?, + g("path_no_call")?, + g("snps_called")?, + g("min_depth")? + ))); + for (label, key) in ANCESTRAL { + if let Some(v) = shown(d, key, PATH)? { + c.body + .push(Line::Para(format!("{label}: {}", py_or(Some(v), "")?))); + } + } + let mut rows = String::new(); + for x in list(d, "path", PATH)? { + rows += ""; + for k in [ + "Level", + "Branch", + "Derived", + "Ancestral", + "No_call", + "Ancestral_SNPs", + ] { + rows += &format!("{}", e(&need_text(&x, k, ROW)?)); + } + rows += ""; + } + if !rows.is_empty() { + c.body.push(Line::Raw(format!( + " \ + {rows}
    LevelBranchDerivedAncestralNo callAncestral SNPs
    " + ))); + } + let (links, note) = explore_html(d, PATH)?; + c.body.extend(links); + if note { + c.caveats.push(EXPLORE_NOTE); + } + c.caveats.push(Y_TREE_NOTE); + } + Ok(Some(html::card(&sec, c)?)) +} diff --git a/rust/crates/og-render/src/sections/ystr.rs b/rust/crates/og-render/src/sections/ystr.rs index 7e7922e39..91019f76c 100644 --- a/rust/crates/og-render/src/sections/ystr.rs +++ b/rust/crates/og-render/src/sections/ystr.rs @@ -1 +1,101 @@ -//! The report section `ystr`. Empty until its unit of the 1.0.0 plan lands. +//! The Y-STR markers (step 45, HipSTR): the repeat count of each marker +//! called, in FamilyTreeDNA's Y-111 order and panels, so a count lines up +//! with FamilyTreeDNA results and surname-project tables. + +use pycompat::json::Value; + +use crate::escape::e; +use crate::model::{Card, Line, Malformed, State, Summary, need, truthy}; +use crate::notes::YSTR_NOTE; +use crate::sections::ancestry::{Align, items, need_align, need_text, py_len}; +use crate::{html, text}; + +const PATH: &str = "sections.y_str.data"; +const ROW: &str = "sections.y_str.data.markers[]"; + +/// How many markers were called, and with what confidence. +pub fn counts(d: &Value) -> Result { + let n = |k: &str| need_text(d, k, PATH); + let high = n("high")?; + let low = n("low")?; + let all = py_len(need(d, "markers", PATH)?, &format!("{PATH}.markers"))?; + Ok(format!( + "Markers called: {high} high and {low} low confidence of {all}; {} no call, {} not \ + callable from short reads", + n("no_call")?, + n("not_callable")? + )) +} + +/// The markers with a count, in the order the summary lists them (the +/// panel's order). +fn called(markers: &Value) -> Result, Malformed> { + let mut out = Vec::new(); + for x in items(markers, &format!("{PATH}.markers"))? { + if truthy(need(&x, "count", ROW)?) { + out.push(x); + } + } + Ok(out) +} + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "y_str", Some("Y-STR Markers (HipSTR)"))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if !head.ran { + return Ok(w); + } + let d = s.section("y_str").data; + w.push(format!(" {}", counts(d)?)); + for x in called(need(d, "markers", PATH)?)? { + w.push(format!( + " {} {} {} ({})", + need_align(&x, "panel", 6, Align::Left, ROW)?, + need_align(&x, "marker", 13, Align::Left, ROW)?, + need_align(&x, "count", 6, Align::Left, ROW)?, + need_text(&x, "confidence", ROW)? + )); + } + w.push(format!(" {YSTR_NOTE}")); + w.push(String::new()); + Ok(w) +} + +/// The HTML report's card, when step 45 has a result. +pub fn card(s: &Summary) -> Result, Malformed> { + let sec = s.section("y_str"); + if sec.state == State::Missing { + return Ok(None); + } + let mut c = Card { + title: "Y-STR Markers (HipSTR)".to_owned(), + full: true, + ..Card::default() + }; + if sec.state.ran() { + let d = sec.data; + let markers = crate::model::get(d, "markers", PATH)?; + let mut rows = String::new(); + if let Some(m) = markers { + for x in called(m)? { + rows += ""; + for k in ["panel", "marker", "count", "confidence"] { + rows += &format!("{}", e(&need_text(&x, k, ROW)?)); + } + rows += ""; + } + } + c.body = vec![ + Line::Para(counts(d)?), + Line::Raw(format!( + " \ + {rows}
    PanelMarkerRepeatsConfidence
    " + )), + ]; + c.caveats.push(YSTR_NOTE); + } + Ok(Some(html::card(&sec, c)?)) +} diff --git a/tests/parity/cases.tsv b/tests/parity/cases.tsv index 3e7e16214..eb96712b2 100644 --- a/tests/parity/cases.tsv +++ b/tests/parity/cases.tsv @@ -1255,3 +1255,19 @@ 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, the ancestry chapter and the PRS card: the a-* summaries of gen/report-corpus.sh. a-numbers is text-only: +# the Python's HTML report cannot render a number where the summary writes text. +report-render a-full gen:report-corpus.sh --summary a-full.json -o a-full.txt -o a-full.html a-full.txt,a-full.html bytes PYTHON_IMAGE +report-render a-stale gen:report-corpus.sh --summary a-stale.json -o a-stale.txt -o a-stale.html a-stale.txt,a-stale.html bytes PYTHON_IMAGE +report-render a-failed gen:report-corpus.sh --summary a-failed.json -o a-failed.txt -o a-failed.html a-failed.txt,a-failed.html bytes PYTHON_IMAGE +report-render a-unreadable gen:report-corpus.sh --summary a-unreadable.json -o a-unreadable.txt -o a-unreadable.html a-unreadable.txt,a-unreadable.html bytes PYTHON_IMAGE +report-render a-missing gen:report-corpus.sh --summary a-missing.json -o a-missing.txt -o a-missing.html a-missing.txt,a-missing.html bytes PYTHON_IMAGE +report-render a-partial gen:report-corpus.sh --summary a-partial.json -o a-partial.txt -o a-partial.html a-partial.txt,a-partial.html bytes PYTHON_IMAGE +report-render a-escape gen:report-corpus.sh --summary a-escape.json -o a-escape.txt -o a-escape.html a-escape.txt,a-escape.html bytes PYTHON_IMAGE +report-render a-painting gen:report-corpus.sh --summary a-painting.json -o a-painting.txt -o a-painting.html a-painting.txt,a-painting.html bytes PYTHON_IMAGE +report-render a-no-painting gen:report-corpus.sh --summary a-no-painting.json -o a-no-painting.txt -o a-no-painting.html a-no-painting.txt,a-no-painting.html bytes PYTHON_IMAGE +report-render a-prs-raw gen:report-corpus.sh --summary a-prs-raw.json -o a-prs-raw.txt -o a-prs-raw.html a-prs-raw.txt,a-prs-raw.html bytes PYTHON_IMAGE +report-render a-prs-sex gen:report-corpus.sh --summary a-prs-sex.json -o a-prs-sex.txt -o a-prs-sex.html a-prs-sex.txt,a-prs-sex.html bytes PYTHON_IMAGE +report-render a-y-calls gen:report-corpus.sh --summary a-y-calls.json -o a-y-calls.txt -o a-y-calls.html a-y-calls.txt,a-y-calls.html bytes PYTHON_IMAGE +report-render a-since gen:report-corpus.sh --summary a-since.json -o a-since.txt -o a-since.html a-since.txt,a-since.html bytes PYTHON_IMAGE +report-render a-numbers gen:report-corpus.sh --summary a-numbers.json -o a-numbers.txt a-numbers.txt bytes PYTHON_IMAGE diff --git a/tests/parity/gen/report-corpus.sh b/tests/parity/gen/report-corpus.sh index d16728bc9..bcddd670e 100644 --- a/tests/parity/gen/report-corpus.sh +++ b/tests/parity/gen/report-corpus.sh @@ -12,6 +12,8 @@ # re-check, the manifest with and without a release, a # sample name with characters HTML escapes and characters # outside ASCII, counts missing or of another type. +# a-.json case c-full's summary changed one way each, for the +# ancestry chapter and the PRS card (see that block below). # py/ holds the modules render_report.py imports, as they were at the # oracle's commit (tests/parity/oracle.ref, report-render). # @@ -116,6 +118,73 @@ mut counts '.sections.variants.state = "ok" | .sections.variants.data = {"total" mut counts-float '.sections.coverage.state = "ok" | .sections.coverage.data.mean_depth = 30.05' rm -f "$base" +# --- the ancestry chapter and the PRS card, from case c-full ----------------------- +# a-.json: c-full without its one PRS row that has no numbers (the +# Python's HTML report cannot render it), changed one way each: every state, +# a planted escape in the haplogroup links and names, a painting with its +# legend and the groups left out, the raw and the sex-specific scores, the Y +# placement's ancestral calls, new_since, and numbers where the summary +# writes text (a text-only case: the Python's HTML report cannot render it). +[ -s c-full.json ] || { echo "report-corpus.sh: og summary collect wrote no c-full.json" >&2; exit 1; } +abase=abase.tmp +jq -c '.sections.prs.data.scores |= map(select(.score != null))' c-full.json > "$abase" +amut() { jq -c "$2" "$abase" > "a-$1.json"; } +akeys='("ancestry_fine", "haplogroup", "y_haplogroup", "y_tree", "y_str", "archaic", "ancient", "prs")' +amut full '.' +for st in stale failed unreadable missing partial; do + amut "$st" "reduce $akeys as \$k (.; .sections[\$k].state = \"$st\" | .sections[\$k].note = \"why: &'\\\"\")" +done +amut escape '.sections.haplogroup.data.haplogroup = "H&\"x'"'"'" + | .sections.haplogroup.data.explore[0].url = "https://discover.familytreedna.com/mtdna/\">" + | .sections.haplogroup.data.explore[1].name = "YFull & co" + | .sections.haplogroup.data.contamination_status = "YES" | .sections.haplogroup.data.contamination_level = "0.4<" + | .sections.y_haplogroup.data.explore[1].url = "javascript:alert(\"x\")" + | .sections.y_tree.data.path[0].Branch = "" | .sections.y_tree.data.path[0].Ancestral_SNPs = "M1&M2" + | .sections.y_str.data.markers[0].marker = "DYS<393>" + | .sections.ancient.data.nearest[0].group = "Spain_" | .sections.ancient.data.nearest[0].locality = "A & B" + | .sections.ancestry_fine.data.continent[0].group = "West " + | .sections.ancestry_fine.data.legend[0].colour = "red\">" | .sections.ancestry_fine.data.legend[1].group = "W" + | .sections.archaic.data.longest = ["chr1:1-2 "] + | .sections.prs.data.scores[0].condition = "CD & \"x\"" | .sections.prs.data.scores[0].effect = "HR <1.5>"' +amut painting '.sections.ancestry_fine.data.painting = [{"code": "AFR", "group": "Africa", "fraction": 0.3}, + {"code": "WEA", "group": "West Eurasia", "fraction": 0.7}] + | .sections.ancestry_fine.data.not_painted = [] + | .sections.ancestry_fine.data.left_out = [{"group": "Oceania (continental group)"}, {"group": "Oceania (regional group)"}, + {"group": "Papua (regional group)"}, {"group": "Odd (name) (x group)"}, {"group": "Plain"}] + | .sections.ancestry_fine.data.min_references = 12 + | .sections.ancestry_fine.data.continent += [{"group": "Small", "fraction": 0.009, "low": 0.001, "high": 0.02}] + | .sections.ancestry_fine.data.region += [{"group": "Wide", "fraction": 0.005, "low": 0.0, "high": 0.06}, + {"group": "Narrow", "fraction": 0.005, "low": 0.0, "high": 0.02}, {"group": "Unfit", "fraction": null, "low": null, "high": null}]' +amut no-painting '.sections.ancestry_fine.data.painting = [] | .sections.ancestry_fine.data.not_painted = [] + | .sections.ancestry_fine.data.left_out = [] | .sections.ancestry_fine.data.painting_png = ""' +amut prs-raw '.sections.prs.data.adjusted = false | del(.sections.prs.data.ancestry) + | .sections.prs.data.scores |= map(.percentile = "" | .evaluated_in = null)' +amut prs-sex '.sections.prs.data.ancestry = {"population": "", "panel": "", "low_confidence": "True"} + | .sections.prs.data.scores = [ + {"condition": "Prostate", "pgs_id": "PGS2", "score": "1", "matched": "5", "total": "6", "chrx": "0", + "applies_to": "male", "sex_check": "not applicable to the declared sex", "percentile": "", "group": "", + "evaluated_in": ["EUR"], "effect": ""}, + {"condition": "Breast", "pgs_id": "PGS3", "score": "2", "matched": "7", "total": "9", "chrx": "3 left out", + "applies_to": "female", "sex_check": "no sex declared", "percentile": "40", "group": "AFR", + "evaluated_in": [], "effect": ""}, + {"condition": "Height", "pgs_id": "PGS4", "score": "3", "matched": "1", "total": "2", "chrx": "", + "applies_to": "", "sex_check": "", "percentile": "60", "group": "AFR", "evaluated_in": ["EUR", "AFR"], + "effect": ""}]' +amut y-calls '.sections.y_tree.data.ancestral_in_terminal = "M1,M2" | .sections.y_tree.data.outvoted_ancestral = null + | .sections.y_tree.data.path = [] | .sections.y_str.data.markers |= map(.count = "") + | .sections.ancient.data.nearest = [] | .sections.ancient.data.plot_svg = "" + | .sections.archaic.data.longest = [] + | .sections.haplogroup.data.explore = [] | del(.sections.haplogroup.data.contamination_status) + | .sections.y_haplogroup.data.explore = []' +amut since '.new_since = {"previous_utc": "2026-01-01T00:00:00Z", "sections": ["y_str", "prs", "haplogroup"]}' +amut numbers '.sections.y_str.data.markers[0].count = 13 | .sections.y_str.data.markers[1].panel = true + | .sections.ancient.data.nearest[0].rank = 1 | .sections.ancient.data.nearest[1].n = 5.5 + | .sections.y_tree.data.path[0].Level = 1 | .sections.y_tree.data.path[1].Derived = 2.0 + | .sections.archaic.data.neanderthal_bp = 51234567.0 | .sections.archaic.data.denisovan_bp = true + | .sections.prs.data.scores[0].condition = 7 | .sections.prs.data.scores[1].score = 0.25 + | .sections.ancestry_fine.data.continent[1].fraction = 1' +rm -f "$abase" + # --- the oracle ----------------------------------------------------------------------- cat > oracle.sh << 'ORACLE' #!/bin/sh @@ -132,6 +201,7 @@ PYTHONPATH=$d python3 "$d/render_report.py" "$@" rm -rf "$d" # The sections og report render writes; the others are left out. PORTED=" qc variants not-run not-assessed " +PORTED="${PORTED}ancestry-fine mt-haplogroup y-chromosome archaic ancient prs " while [ $# -gt 0 ]; do case "$1" in -o) @@ -148,8 +218,9 @@ done ORACLE # html.awk: drops each section not in keep (its line in the contents and its -# frame), then each chapter with no section left (its contents entry and its -# body), and the gene and rsID appendix. +# frame) and every chart (og report charts will write them), then each +# chapter with no section left (its contents entry and its body), and the +# gene and rsID appendix. cat > html.awk << 'AWK' function wanted(line, id) { if (!match(line, /id="[^"]*"/) && !match(line, /href="#[^"]*"/)) return 1 @@ -164,6 +235,7 @@ END { for (i = 1; i <= n; i++) { l = lines[i] if (l ~ /^
  • /) continue if (l ~ /^
    ") i++ continue @@ -194,6 +266,10 @@ cat > text.awk << 'AWK' BEGIN { rule = sprintf("%80s", ""); gsub(/ /, "#", rule) title["## Quality control"] = "qc"; title["## Variant Calling (DeepVariant)"] = "variants" + title["## Fine-Grained Ancestry (ADMIXTURE, RFMix)"] = "ancestry-fine"; title["## Mitochondrial Haplogroup"] = "mt-haplogroup" + title["## Y-Chromosome Haplogroup (Yleaf)"] = "y-chromosome"; title["## Y Placement on the Current Public Tree"] = "y-chromosome" + title["## Y-STR Markers (HipSTR)"] = "y-chromosome"; title["## Archaic Ancestry (IBDmix, step 43)"] = "archaic" + title["## Ancient Populations (AADR PCA, step 44)"] = "ancient"; title["## Polygenic Risk Scores"] = "prs" } { lines[++n] = $0 } END { From 835905ffccd2c3a04ef5277d9f6207db46cb7146 Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:37:17 +0100 Subject: [PATCH 02/29] feat(report): write the traits, copy-number and repeat sections in Rust og report render now writes five more sections of both reports from summary.json: the traits panel (with the genotype rules, APOL1's G2 beside its rule and the APOE lines behind their warning), the blood groups, Lp(a) (the KIV-2 count, the null allele and the two risk variants), the paralog genes with Parascopy and step 35's depth counts (HBA, CYP21A2, the dosage loci, the copy-number intervals), and the repeat expansions. A card line can now hold markup the section built itself (Line::Markup): the tables and the folded APOE part, every text in them escaped. The repeat caveat points at docs/results/structural-and-repeats.md, as the Python report does since the results docs were split. The core test checks the order of whatever sections are shown, not a fixed list. Bead: D1 Bead: D3 Bead: D15 --- rust/crates/og-render/src/chapters.rs | 10 +- rust/crates/og-render/src/html.rs | 1 + rust/crates/og-render/src/model.rs | 4 + rust/crates/og-render/src/notes.rs | 2 +- rust/crates/og-render/src/sections/apoe.rs | 57 ++- .../og-render/src/sections/bloodtype.rs | 102 +++- .../og-render/src/sections/cn_intervals.rs | 152 +++++- rust/crates/og-render/src/sections/cyp21a2.rs | 48 +- rust/crates/og-render/src/sections/dosage.rs | 61 ++- rust/crates/og-render/src/sections/g2.rs | 90 +++- rust/crates/og-render/src/sections/hba.rs | 26 +- rust/crates/og-render/src/sections/lpa.rs | 140 ++++- .../crates/og-render/src/sections/paralogs.rs | 320 +++++++++++- .../og-render/src/sections/parascopy.rs | 148 +++++- rust/crates/og-render/src/sections/rfc1.rs | 20 +- rust/crates/og-render/src/sections/smn1.rs | 64 ++- rust/crates/og-render/src/sections/str.rs | 477 +++++++++++++++++- rust/crates/og-render/src/sections/traits.rs | 253 +++++++++- .../og-render/tests/port_report_core.rs | 13 +- 19 files changed, 1957 insertions(+), 31 deletions(-) diff --git a/rust/crates/og-render/src/chapters.rs b/rust/crates/og-render/src/chapters.rs index 92ace64e9..9ae9fd637 100644 --- a/rust/crates/og-render/src/chapters.rs +++ b/rust/crates/og-render/src/chapters.rs @@ -177,13 +177,13 @@ pub fn writer(sid: &str) -> Option<(TextFn, HtmlFn)> { "mei" => None, - "expansions" => None, + "expansions" => Some((sections::str::text, sections::str::html)), "vntyper" => None, "mito" => None, - "lpa" => None, + "lpa" => Some((sections::lpa::text, sections::lpa::html)), "chip" => None, @@ -195,7 +195,7 @@ pub fn writer(sid: &str) -> Option<(TextFn, HtmlFn)> { "couple" => None, - "paralogs" => None, + "paralogs" => Some((sections::paralogs::text, sections::paralogs::html)), "pharmcat" => None, @@ -219,11 +219,11 @@ pub fn writer(sid: &str) -> Option<(TextFn, HtmlFn)> { "roh" => None, - "traits-panel" => None, + "traits-panel" => Some((sections::traits::text, sections::traits::html)), "prs" => None, - "bloodtype" => None, + "bloodtype" => Some((sections::bloodtype::text, sections::bloodtype::html)), "telomere" => None, diff --git a/rust/crates/og-render/src/html.rs b/rust/crates/og-render/src/html.rs index 43e75f02b..0e2eee439 100644 --- a/rust/crates/og-render/src/html.rs +++ b/rust/crates/og-render/src/html.rs @@ -70,6 +70,7 @@ fn line(out: &mut Vec, l: &Line) { out.extend(items.iter().map(|t| format!("
  • {}
  • ", e(t)))); out.push(" ".to_owned()); } + Line::Markup(m) => out.push(m.clone()), } } diff --git a/rust/crates/og-render/src/model.rs b/rust/crates/og-render/src/model.rs index 54ea06364..cb29f047e 100644 --- a/rust/crates/og-render/src/model.rs +++ b/rust/crates/og-render/src/model.rs @@ -325,6 +325,10 @@ pub enum Line { Para(String), /// A plain bulleted list. List(Vec), + /// Markup a section built itself (a table, a heading or a folded part + /// inside the card), written as it is: every text in it is already + /// escaped with [`crate::escape::e`]. + Markup(String), } /// One card: a title, the caveats shown under it, the notes saying its diff --git a/rust/crates/og-render/src/notes.rs b/rust/crates/og-render/src/notes.rs index e94157408..e04bc59f7 100644 --- a/rust/crates/og-render/src/notes.rs +++ b/rust/crates/og-render/src/notes.rs @@ -17,7 +17,7 @@ pub const CALLABILITY_NOTE: &str = "A gene listed as not read could not be read pub const HLA_NOTE: &str = "HLA typing from short-read WGS is approximate. If either HLA-A or HLA-B allele has a \ T1K quality of 0 or less, that gene is not passed to PharmCAT (step 36)."; pub const EH_NOTE: &str = "Repeat-expansion calls from short reads can be wrong at some loci, and a long expansion \ - cannot be sized. Disease thresholds: docs/interpreting-results.md."; + cannot be sized. Disease thresholds: docs/results/structural-and-repeats.md."; pub const TELOMERE_NOTE: &str = "Relative telomere content (intratelomeric reads per million reads with 48-52% GC), not \ a telomere length: compare it only with samples of a similar age sequenced on the same \ platform (docs/10-telomere-analysis.md)."; diff --git a/rust/crates/og-render/src/sections/apoe.rs b/rust/crates/og-render/src/sections/apoe.rs index de95c5197..8d5947aef 100644 --- a/rust/crates/og-render/src/sections/apoe.rs +++ b/rust/crates/og-render/src/sections/apoe.rs @@ -1 +1,56 @@ -//! The report section `apoe`. Empty until its unit of the 1.0.0 plan lands. +//! APOE, the Alzheimer's risk factor: the report keeps its lines apart and +//! last, behind a warning, because many people choose not to learn them. +//! The text report prints them after the warning; the HTML report folds +//! them away until opened. + +use pycompat::json::Value; + +use crate::model::{Malformed, need, py}; + +/// The warning before the APOE lines. +pub const APOE_WARNING: &str = "APOE and Alzheimer's disease: a risk factor, not a diagnosis. Many \ + people choose not to learn their APOE result; docs/42-traits.md explains it."; + +/// The text report's APOE lines, the warning first; none without APOE rows. +pub fn text_lines(rows: &[Value]) -> Result, Malformed> { + if rows.is_empty() { + return Ok(Vec::new()); + } + let mut out = vec![format!(" {APOE_WARNING}")]; + for r in rows { + let f = |k: &str| need(r, k, "sections.traits.data.apoe[]").and_then(py); + out.push(format!( + " {} ({}): {}. {}", + f("trait")?, + f("rsid")?, + f("genotype")?, + f("reading")? + )); + } + Ok(out) +} + +/// The HTML report's folded APOE table: `head` is the traits table's +/// header row, `row` writes one row as the traits table does. +pub fn html_lines( + rows: &[Value], + head: &str, + row: impl Fn(&Value) -> Result, +) -> Result, Malformed> { + if rows.is_empty() { + return Ok(Vec::new()); + } + let mut out = vec![ + format!( + "
    {} Open to show it.", + crate::escape::e(APOE_WARNING) + ), + " ".to_owned(), + head.to_owned(), + ]; + for r in rows { + out.push(format!(" {}", row(r)?)); + } + out.push("
    ".to_owned()); + Ok(out) +} diff --git a/rust/crates/og-render/src/sections/bloodtype.rs b/rust/crates/og-render/src/sections/bloodtype.rs index afc1017a2..f1727357c 100644 --- a/rust/crates/og-render/src/sections/bloodtype.rs +++ b/rust/crates/og-render/src/sections/bloodtype.rs @@ -1,2 +1,100 @@ -//! The report section `bloodtype`. Empty until its unit of the 1.0.0 plan -//! lands. +//! Step 41's blood groups, predicted from the genome: one row per system +//! (ABO, RhD and the others the marker table defines), with its genotype and +//! confidence. The markers' sources and licences are in the header of +//! `assets/bloodtype_markers.tsv`. + +use pycompat::fmt::ljust; +use pycompat::json::Value; + +use crate::escape::e; +use crate::html; +use crate::model::{Card, HtmlPart, Inline, Line, Malformed, Summary, need, py}; +use crate::notes::BLOODTYPE_NOTE; +use crate::sections::paralogs::shows_body; +use crate::text; + +const PATH: &str = "sections.bloodtype.data.systems[]"; + +/// The systems, or an error when the summary holds something else. +fn systems(v: &Value) -> Result<&[Value], Malformed> { + match v { + Value::Array(a) => Ok(a), + _ => Err(Malformed( + "sections.bloodtype.data.systems is not a list".into(), + )), + } +} + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head( + s, + "bloodtype", + Some("Blood Groups (predicted from the genome)"), + )? + else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if head.ran { + let d = s.section("bloodtype"); + for x in systems(d.need("systems")?)? { + let f = |k: &str| need(x, k, PATH).and_then(py); + w.push(format!( + " {} {} {} ({})", + ljust(&f("system")?, 9), + ljust(&f("phenotype")?, 34), + ljust(&f("genotype")?, 28), + f("confidence")? + )); + } + w.push(format!(" {BLOODTYPE_NOTE}")); + w.push(String::new()); + } + Ok(w) +} + +/// The HTML report's card, when step 41 is in the summary. +pub fn html(s: &Summary) -> Result { + let d = s.section("bloodtype"); + if d.state == crate::model::State::Missing { + return Ok(HtmlPart::default()); + } + let mut body = Vec::new(); + let mut caveats = Vec::new(); + if shows_body(&d) { + let dot = |k: &str| d.get(k).and_then(|v| v.map_or(Ok(".".to_owned()), py)); + body.push(Line::Stat("ABO".to_owned(), Inline::Text(dot("abo")?))); + body.push(Line::Stat("RhD".to_owned(), Inline::Text(dot("rhd")?))); + let mut t = " \ + " + .to_owned(); + for x in d.get("systems")?.map_or(Ok(&[][..]), systems)? { + let f = |k: &str| need(x, k, PATH).and_then(py); + t += &format!( + "", + e(&f("system")?), + e(&f("phenotype")?), + e(&f("genotype")?), + e(&f("confidence")?) + ); + } + t += "
    SystemPredicted phenotypeGenotypeConfidence
    {}{}{}{}
    "; + body.push(Line::Markup(t)); + caveats.push(BLOODTYPE_NOTE); + } + let card = html::card( + &d, + Card { + title: "Blood Groups (predicted)".to_owned(), + full: true, + caveats, + body, + ..Card::default() + }, + )?; + Ok(HtmlPart { + cards: vec![card], + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/cn_intervals.rs b/rust/crates/og-render/src/sections/cn_intervals.rs index 11bb963e1..73df5a712 100644 --- a/rust/crates/og-render/src/sections/cn_intervals.rs +++ b/rust/crates/og-render/src/sections/cn_intervals.rs @@ -1,2 +1,150 @@ -//! The report section `cn_intervals`. Empty until its unit of the 1.0.0 -//! plan lands. +//! Step 35's copy-number intervals from read depth: ClinGen's recurrent CNV +//! regions, carrier deletion alleles and the AZF regions. One summary line, +//! then the rows with a deletion or a duplication. + +use pycompat::json::Value; + +use crate::escape::e; +use crate::model::{Inline, Line, Malformed, get, need, py, truthy}; + +/// The label of the summary line. +pub const LABEL: &str = + "Copy-number intervals (ClinGen recurrent regions, carrier deletions, AZF; from depth)"; + +const PATH: &str = "sections.parascopy.data.cn_intervals"; + +/// `(iv.get(key) or default)` for a dict or a list. +fn or_empty<'a>(iv: &'a Value, key: &str) -> Result, Malformed> { + Ok(get(iv, key, PATH)?.filter(|v| truthy(v))) +} + +/// The rows with a change. +fn changes(iv: Option<&Value>) -> Result<&[Value], Malformed> { + let Some(iv) = iv.filter(|v| truthy(v)) else { + return Ok(&[]); + }; + match or_empty(iv, "changes")? { + None => Ok(&[]), + Some(Value::Array(a)) => Ok(a), + Some(_) => Err(Malformed(format!("{PATH}.changes is not a list"))), + } +} + +/// The interval table in one line: rows by status and how many changed. +pub fn summary(iv: Option<&Value>) -> Result { + let Some(iv) = iv.filter(|v| !matches!(v, Value::Null)) else { + return Ok(format!( + "{LABEL}: not assessed (step 35's _cn_intervals.tsv is not in this run's output)" + )); + }; + let by = or_empty(iv, "by_status")?; + let mut counts: Vec<(String, String)> = Vec::new(); + if let Some(by) = by { + let Value::Object(o) = by else { + return Err(Malformed(format!("{PATH}.by_status is not an object"))); + }; + for (k, n) in o { + counts.push((k.clone(), py(n)?)); + } + } + counts.sort(); + let mut counts: String = counts + .iter() + .map(|(k, n)| format!("{n} {k}")) + .collect::>() + .join(", "); + if counts.is_empty() { + counts = "no row".to_owned(); + } + let n = changes(Some(iv))?.len(); + let called = match by { + Some(by) => get(by, "called", PATH)?, + None => None, + }; + let end = if n > 0 { + format!("{n} with a deletion or a duplication") + } else if let Some(k) = called.filter(|k| truthy(k)) { + format!( + "no deletion or duplication among the {} called rows", + py(k)? + ) + } else { + "none called".to_owned() + }; + let rows = get(iv, "rows", PATH)?.map_or(Ok("0".to_owned()), py)?; + Ok(format!("{LABEL}: {rows} rows ({counts}); {end}")) +} + +/// `chrom:start-end` (1-based) of an interval row of the table (BED). +pub fn place(r: &Value) -> Result { + let at = |k: &str| get(r, k, "an interval row"); + let dot = |k: &str| at(k).and_then(|v| v.map_or(Ok(".".to_owned()), py)); + let b = dot("start")?; + let start = if pycompat::text::isdigit(&b) { + let n = pycompat::num::parse_int(&b) + .map_err(|_| Malformed(format!("an interval row's start {b} is not a number")))?; + (n + 1).to_string() + } else { + b + }; + Ok(format!("{}:{start}-{}", dot("chrom")?, dot("end")?)) +} + +/// `str.capitalize()`: the first character upper case, the rest lower. +fn capitalize(s: &str) -> String { + let mut c = s.chars(); + match c.next() { + Some(first) => { + first.to_uppercase().collect::() + &pycompat::text::lower(c.as_str()) + } + None => String::new(), + } +} + +/// The text report's lines: the summary, then one line per row with a +/// change. +pub fn lines(iv: Option<&Value>) -> Result, Malformed> { + let mut out = vec![summary(iv)?]; + for r in changes(iv)? { + let f = |k: &str| need(r, k, "an interval row").and_then(py); + out.push(format!( + " {}: {} ({}, {}), {} of {} copies", + capitalize(&f("change")?), + f("name")?, + f("locus")?, + place(r)?, + f("copies")?, + f("expected")? + )); + } + Ok(out) +} + +/// The HTML card's lines: the summary as a stat, then a table of the rows +/// with a change. +pub fn html(iv: Option<&Value>) -> Result, Malformed> { + let s = summary(iv)?; + let (label, value) = s.split_once(": ").unwrap_or((&s, "")); + let mut out = vec![Line::Stat(label.to_owned(), Inline::Text(value.to_owned()))]; + let rows = changes(iv)?; + if !rows.is_empty() { + let mut t = " \ + " + .to_owned(); + for r in rows { + let f = |k: &str| need(r, k, "an interval row").and_then(py); + t += &format!( + "", + e(&f("name")?), + e(&f("locus")?), + e(&place(r)?), + e(&f("copies")?), + e(&f("expected")?), + e(&f("change")?) + ); + } + t += "
    RegionRowPlaceCopiesChange
    {}{}{}{} of {}{}
    "; + out.push(Line::Markup(t)); + } + Ok(out) +} diff --git a/rust/crates/og-render/src/sections/cyp21a2.rs b/rust/crates/og-render/src/sections/cyp21a2.rs index 3699dbddb..27456d9ca 100644 --- a/rust/crates/og-render/src/sections/cyp21a2.rs +++ b/rust/crates/og-render/src/sections/cyp21a2.rs @@ -1,2 +1,46 @@ -//! The report section `cyp21a2`. Empty until its unit of the 1.0.0 plan -//! lands. +//! Step 35's CYP21A2 call (congenital adrenal hyperplasia carriers): the +//! copies from depth and the six sites where CYP21A2 differs from its +//! pseudogene CYP21A1P, one line in both reports. + +use pycompat::json::Value; + +use crate::model::{Malformed, get, is_str, py, truthy}; + +const LABEL: &str = "CYP21A2 (RCCX module, from depth and six PSV sites)"; + +/// The CYP21A2 line; not assessed only when the table says so or is not +/// there. +pub fn cyp21a2_line(c: Option<&Value>) -> Result { + let Some(c) = c.filter(|c| truthy(c)) else { + return Ok(format!( + "{LABEL}: not assessed (step 35's _cyp21a2_psv.tsv is not in this run's output)" + )); + }; + let at = |k: &str| get(c, k, "sections.parascopy.data.cyp21a2"); + let or = |k: &str| at(k).and_then(|v| v.map_or(Ok(".".to_owned()), py)); + if !is_str(at("status")?, "called") { + let why = match at("message")? + .filter(|v| truthy(v)) + .or(at("reason")?.filter(|v| truthy(v))) + { + Some(v) => py(v)?, + None => ".".to_owned(), + }; + let why = why.strip_prefix("not assessed: ").unwrap_or(&why); + return Ok(format!("{LABEL}: not assessed ({why})")); + } + let mut value = format!( + "{} CYP21A2 copies, module total {} of {}, finding {}", + or("cyp21a2_copies")?, + or("module_total")?, + or("expected_total")?, + or("finding")? + ); + if let Some(v) = at("conversion_sites")?.filter(|v| !is_str(Some(v), ".")) { + value += &format!(" at {}", py(v)?); + } + if let Some(v) = at("reading")?.filter(|v| !is_str(Some(v), ".")) { + value += &format!(". Reading: {}", py(v)?); + } + Ok(format!("{LABEL}: {value}")) +} diff --git a/rust/crates/og-render/src/sections/dosage.rs b/rust/crates/og-render/src/sections/dosage.rs index 49d9dbfa1..bb51531e2 100644 --- a/rust/crates/og-render/src/sections/dosage.rs +++ b/rust/crates/og-render/src/sections/dosage.rs @@ -1,2 +1,59 @@ -//! The report section `dosage`. Empty until its unit of the 1.0.0 plan -//! lands. +//! Step 35's gene-dosage loci from read depth (HP, LCE3B/LCE3C, +//! CCL3L1/CCL3L3), one line each in both reports. + +use pycompat::json::Value; + +use crate::model::{Malformed, is_str, need, py, truthy}; + +/// The display name of a dosage locus. +fn name(locus: &str) -> &str { + match locus { + "HP" => "HP (haptoglobin)", + "LCE3BC" => "LCE3B/LCE3C", + "CCL3L1" => "CCL3L1/CCL3L3", + other => other, + } +} + +/// One line per dosage row, or one line saying why there is none. +pub fn dosage_lines(rows: Option<&Value>) -> Result, Malformed> { + let rows = match rows { + None | Some(Value::Null) => { + return Ok(vec![ + "Gene-dosage loci (HP, LCE3B/LCE3C, CCL3L1): not assessed (step 35's \ + _cn_dosage.tsv is not in this run's output)" + .to_owned(), + ]); + } + Some(r) if !truthy(r) => { + return Ok(vec![ + "Gene-dosage loci (HP, LCE3B/LCE3C, CCL3L1): not assessed (step 35's dosage \ + table has no row)" + .to_owned(), + ]); + } + Some(Value::Array(r)) => r, + Some(_) => { + return Err(Malformed( + "sections.parascopy.data.dosage is not a list".into(), + )); + } + }; + let path = "sections.parascopy.data.dosage[]"; + let mut out = Vec::new(); + for r in rows { + let f = |k: &str| need(r, k, path); + let label = format!("{} copies (from depth)", name(&py(f("locus")?)?)); + let value = if is_str(Some(f("status")?), "called") { + let mut v = format!("{} (estimate {})", py(f("copies")?)?, py(f("estimate")?)?); + if !is_str(Some(f("reading")?), ".") { + v += &format!(". Reading: {}", py(f("reading")?)?); + } + v + } else { + format!("{} ({})", py(f("status")?)?, py(f("reason")?)?) + }; + out.push(format!("{label}: {value}")); + } + Ok(out) +} diff --git a/rust/crates/og-render/src/sections/g2.rs b/rust/crates/og-render/src/sections/g2.rs index 6f24ec5e0..c4a075865 100644 --- a/rust/crates/og-render/src/sections/g2.rs +++ b/rust/crates/og-render/src/sections/g2.rs @@ -1 +1,89 @@ -//! The report section `g2`. Empty until its unit of the 1.0.0 plan lands. +//! A genotype rule's result (several sites read together), and APOL1's G2 +//! beside it: G2 (rs71785313) is read by its own helper, so the rule's +//! G0/G0 or G0/G1 says nothing of it, and the report prints the combined +//! answer of `summary.json` (`traits.rules[APOL1].g2`) under the rule. + +use pycompat::json::Value; + +use crate::escape::e; +use crate::model::{Malformed, get, is_str, need, py, truthy}; + +const PATH: &str = "sections.traits.data.rules[]"; + +/// How a rule's sites were phased, in words; `None` for a phase with no +/// words. +fn phase_text(phase: &Value) -> Option<&'static str> { + let Value::Str(p) = phase else { return None }; + Some(match p.as_str() { + "read_backed" => "phased from the reads", + "not_needed" => "no phase needed", + "not_used" => "phase not used", + "unphased" => "not phased", + _ => return None, + }) +} + +/// A rule's result: its label when called, else its status; and how it was +/// phased. +pub fn rule_result(r: &Value) -> Result { + let st = need(r, "status", PATH)?; + let res = if is_str(Some(st), "called") { + py(need(r, "result", PATH)?)? + } else { + py(st)?.replace('_', " ") + }; + Ok(match phase_text(need(r, "phase", PATH)?) { + Some(ph) => format!("{res} ({ph})"), + None => res, + }) +} + +/// The rule's G2 object, when it has one (APOL1 only). +fn g2(r: &Value) -> Result, Malformed> { + Ok(get(r, "g2", PATH)?.filter(|v| truthy(v))) +} + +/// The APOL1 rule's result with G2, or `None` for any other rule. +pub fn g2_result(r: &Value) -> Result, Malformed> { + let Some(g) = g2(r)? else { return Ok(None) }; + let res = need(g, "result", "the rule's g2")?; + Ok(Some(if is_str(Some(res), ".") { + "not read".to_owned() + } else { + py(res)? + })) +} + +/// The text report's G2 line under the APOL1 rule. +pub fn g2_text(r: &Value) -> Result, Malformed> { + let Some(g) = g2(r)? else { return Ok(None) }; + let f = |k: &str| need(g, k, "the rule's g2").and_then(py); + let res = g2_result(r)?.unwrap_or_default(); + Ok(Some(format!( + " With G2 (rs71785313): {res}. {} {}", + f("text")?, + f("reading")? + ))) +} + +/// The G2 result under the rule's result in the HTML table. +pub fn g2_result_html(r: &Value) -> Result { + Ok(match g2_result(r)?.filter(|x| !x.is_empty()) { + Some(res) => format!("
    With G2: {}", e(&res)), + None => String::new(), + }) +} + +/// The G2 reading and its explanation under the rule's reading in the HTML +/// table. +pub fn g2_reading_html(r: &Value) -> Result { + let Some(g) = g2(r)? else { + return Ok(String::new()); + }; + let f = |k: &str| need(g, k, "the rule's g2").and_then(py); + Ok(format!( + "
    With G2: {}
    {}", + e(&f("reading")?), + e(&f("text")?) + )) +} diff --git a/rust/crates/og-render/src/sections/hba.rs b/rust/crates/og-render/src/sections/hba.rs index d3f347f6a..fdcd0f9ee 100644 --- a/rust/crates/og-render/src/sections/hba.rs +++ b/rust/crates/og-render/src/sections/hba.rs @@ -1 +1,25 @@ -//! The report section `hba`. Empty until its unit of the 1.0.0 plan lands. +//! Step 35's HBA1 and HBA2 count from read depth (alpha-thalassaemia), one +//! line in both reports. + +use pycompat::json::Value; + +use crate::model::{Malformed, get, is_str, py}; + +/// `HBA1 and HBA2 together (alpha-globin genes, from depth): ...`. +pub fn hba_line(h: &Value) -> Result { + let at = |k: &str| get(h, k, "sections.parascopy.data.hba"); + let or = |k: &str, d: &str| at(k).and_then(|v| v.map_or(Ok(d.to_owned()), py)); + let value = if is_str(at("status")?, "called") { + format!( + "{} ({}; estimate {})", + or("alpha_globin_genes", ".")?, + or("message", ".")?, + or("estimate", ".")? + ) + } else { + or("message", "not assessed")? + }; + Ok(format!( + "HBA1 and HBA2 together (alpha-globin genes, from depth): {value}" + )) +} diff --git a/rust/crates/og-render/src/sections/lpa.rs b/rust/crates/og-render/src/sections/lpa.rs index 087b3e19b..cef768bba 100644 --- a/rust/crates/og-render/src/sections/lpa.rs +++ b/rust/crates/og-render/src/sections/lpa.rs @@ -1 +1,139 @@ -//! The report section `lpa`. Empty until its unit of the 1.0.0 plan lands. +//! Step 46's LPA KIV-2 repeat count with its 95% interval, the KIV-2 +//! 4925G>A null allele, step 42's two LPA risk variants, the flag and the +//! advice: the same lines in both reports. + +use pycompat::json::Value; + +use crate::html; +use crate::model::{ + Card, Colour, HtmlPart, Inline, Line, Malformed, Section, State, Summary, get, is_str, need, + py, py_eq, truthy, +}; +use crate::notes::LPA_NOTE; +use crate::sections::paralogs::shows_body; +use crate::sections::smn1::marker_state; +use crate::text; + +/// What a null allele means for the level, and how to measure it. +pub const LPA_NULL_CAVEAT: &str = "A null allele lowers the Lp(a) made from the copies it sits on; \ + the Lp(a) blood test is the measure."; + +/// The lines of the text section and of the card. +pub fn lines(d: &Section<'_>) -> Result, Malformed> { + let dot = |k: &str| d.get(k).and_then(|v| v.map_or(Ok(".".to_owned()), py)); + let need = |k: &str| d.need(k).and_then(py); + let mut out = vec![if is_str(d.get("status")?, "estimated") { + format!( + "KIV-2 repeats (both chromosomes): {} (95% interval {} to {}), {}", + need("kiv2_copies")?, + need("ci95_low")?, + need("ci95_high")?, + need("class")? + ) + } else { + format!("KIV-2 repeats: {}", dot("status")?) + }]; + out.push(format!(" {}", dot("message")?)); + if let Some(na) = d.get("null_allele")?.filter(|v| truthy(v)) { + let path = "sections.lpa.data.null_allele"; + let rsid = match get(na, "rsid", path)?.filter(|v| truthy(v)) { + Some(v) => py(v)?, + None => ".".to_owned(), + }; + let rsid = rsid.split(' ').next().unwrap_or_default(); + out.push(format!( + "KIV-2 4925G>A null allele ({rsid}): {}", + marker_state(na)? + )); + out.push(format!( + " {}", + get(na, "message", path)?.map_or(Ok(".".to_owned()), py)? + )); + out.push(format!(" {LPA_NULL_CAVEAT}")); + } + let variants: &[Value] = match d.get("variants")? { + None => &[], + Some(Value::Array(a)) => a, + Some(_) => return Err(Malformed("sections.lpa.data.variants is not a list".into())), + }; + for v in variants { + let path = "sections.lpa.data.variants[]"; + let n = need_v(v, "risk_alleles", path)?; + let count = if matches!(n, Value::Null) { + "not read".to_owned() + } else { + let s = if py_eq(n, &Value::Int(1)) { "" } else { "s" }; + format!("{} risk allele{s}", py(n)?) + }; + out.push(format!( + "{}: {} ({count})", + py(need_v(v, "rsid", path)?)?, + py(need_v(v, "genotype", path)?)? + )); + } + if !d.get("variants")?.is_some_and(truthy) { + out.push( + "rs10455872 and rs3798220: not read (step 42, the traits panel, did not run)" + .to_owned(), + ); + } + out.push(dot("flag_text")?); + out.push(dot("advice")?); + Ok(out) +} + +fn need_v<'a>(v: &'a Value, k: &str, path: &str) -> Result<&'a Value, Malformed> { + need(v, k, path) +} + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head( + s, + "lpa", + Some("Lp(a): LPA KIV-2 Repeat Count and Risk Variants"), + )? + else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if head.ran { + w.extend(lines(&s.section("lpa"))?.iter().map(|l| format!(" {l}"))); + w.push(format!(" {LPA_NOTE}")); + w.push(String::new()); + } + Ok(w) +} + +/// The HTML report's card, when step 46 is in the summary. +pub fn html(s: &Summary) -> Result { + let d = s.section("lpa"); + if d.state == State::Missing { + return Ok(HtmlPart::default()); + } + let mut body = Vec::new(); + let mut caveats = Vec::new(); + if shows_body(&d) { + if is_str(d.get("flag")?, "high") { + body.push(Line::Stat( + "Genetic sign of high Lp(a)".to_owned(), + Inline::Badge("Yes".to_owned(), Colour::Yellow), + )); + } + body.extend(lines(&d)?.into_iter().map(Line::Para)); + caveats.push(LPA_NOTE); + } + let card = html::card( + &d, + Card { + title: "Lp(a): LPA KIV-2 Repeat Count".to_owned(), + caveats, + body, + ..Card::default() + }, + )?; + Ok(HtmlPart { + cards: vec![card], + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/paralogs.rs b/rust/crates/og-render/src/sections/paralogs.rs index fffdfd985..8c39ac6ae 100644 --- a/rust/crates/og-render/src/sections/paralogs.rs +++ b/rust/crates/og-render/src/sections/paralogs.rs @@ -1,2 +1,318 @@ -//! The report section `paralogs`. Empty until its unit of the 1.0.0 plan -//! lands. +//! The section "Paralog genes and SMN1": Paraphase (step 35b, PacBio HiFi) +//! beside Parascopy and the depth counts of step 35 (short reads), and a +//! "not checked" card when neither left a result, so a report without them +//! never reads as normal. + +use pycompat::json::Value; + +use crate::escape::e; +use crate::html; +use crate::model::{ + Card, Colour, HtmlPart, Inline, Line, Malformed, Section, State, Summary, get, is_str, need, + py, truthy, +}; +use crate::notes::{PARALOG_NOTE, PARASCOPY_NOTE}; +use crate::sections::{parascopy, smn1::smn1_text}; +use crate::text; + +const PATH: &str = "sections.paralogs.data"; + +/// The genes Paraphase called (`d.get("genes", [])`). +fn genes<'a>(d: &Section<'a>) -> Result<&'a [Value], Malformed> { + match d.get("genes")? { + None => Ok(&[]), + Some(Value::Array(a)) => Ok(a), + Some(_) => Err(Malformed(format!("{PATH}.genes is not a list"))), + } +} + +/// `sum(d.get("variants", {}).values())`, as Python prints the sum. +fn variants(d: &Section<'_>) -> Result { + let Some(v) = d.get("variants")? else { + return Ok("0".to_owned()); + }; + let Value::Object(o) = v else { + return Err(Malformed(format!("{PATH}.variants is not an object"))); + }; + let (mut whole, mut float, mut is_float) = (0_i128, 0.0_f64, false); + for x in o.values() { + match x { + Value::Int(i) => { + whole += *i; + float += *i as f64; + } + Value::Bool(b) => { + whole += i128::from(*b); + float += f64::from(u8::from(*b)); + } + Value::Float(f) => { + is_float = true; + float += f; + } + _ => { + return Err(Malformed(format!( + "{PATH}.variants holds a value that is not a number" + ))); + } + } + } + Ok(if is_float { + pycompat::fmt::repr(float) + } else { + whole.to_string() + }) +} + +/// The text report's lines of Paraphase's result. +fn paraphase_lines(d: &Section<'_>) -> Result, Malformed> { + let path = format!("{PATH}.genes[]"); + let mut out = Vec::new(); + for g in genes(d)? { + let f = |k: &str| need(g, k, &path).and_then(py); + let detail = need(g, "detail", &path)?; + let tail = if is_str(Some(detail), "") || is_str(Some(detail), ".") { + String::new() + } else { + format!("; {}", py(detail)?) + }; + out.push(format!( + "{}: {} ({}){tail}", + f("gene")?, + f("copy_number")?, + f("status")? + )); + } + if let Some(smn) = smn1(d)? { + out.push(format!("SMN1 by both tools: {smn}")); + } + if let Some(n) = d.get("other_regions")?.filter(|v| truthy(v)) { + out.push(format!( + "Other Paraphase regions: {} ({} with a call)", + py(n)?, + py(d.need("other_regions_called")?)? + )); + } + out.push(format!("Variants on phased haplotypes: {}", variants(d)?)); + Ok(out) +} + +/// `smn1_text(d.get("smn1") or {})`. +fn smn1(d: &Section<'_>) -> Result, Malformed> { + match d.get("smn1")?.filter(|v| truthy(v)) { + Some(m) => smn1_text(m), + None => Ok(None), + } +} + +/// A key's state as the summary writes it. +fn raw_state(s: &Summary, key: &str) -> String { + let sec = s.section(key); + match sec.state { + State::Ok => "ok".to_owned(), + State::Stale => "stale".to_owned(), + State::Failed => "failed".to_owned(), + State::Unreadable => "unreadable".to_owned(), + State::Missing => "missing".to_owned(), + State::Other => s + .get("sections") + .and_then(|x| get(x, key, "sections").ok().flatten()) + .and_then(|x| get(x, "state", "sections").ok().flatten()) + .and_then(|v| py(v).ok()) + .unwrap_or_default(), + } +} + +/// `Paralog copy number: not checked (...)` when neither step 35 nor step +/// 35b left a result. +pub fn unchecked(s: &Summary) -> Option { + let states = [ + ("parascopy", "35", s.section("parascopy").state), + ("paralogs", "35b", s.section("paralogs").state), + ]; + if states.iter().any(|(_, _, st)| st.ran()) { + return None; + } + let why = if states.iter().all(|(_, _, st)| *st == State::Missing) { + "neither step 35 nor step 35b ran".to_owned() + } else { + states + .iter() + .map(|(k, step, st)| { + let word = match st { + State::Missing => "not run".to_owned(), + State::Failed => "failed".to_owned(), + State::Unreadable => "unreadable".to_owned(), + _ => raw_state(s, k), + }; + format!("step {step} {word}") + }) + .collect::>() + .join(", ") + }; + Some(format!("Paralog copy number: not checked ({why})")) +} + +/// The text report's block: Paraphase, Parascopy, then the "not checked" +/// block. +pub fn text(s: &Summary) -> Result, Malformed> { + let mut w = Vec::new(); + if let Some(head) = text::head( + s, + "paralogs", + Some("Paralog Genes (Paraphase, PacBio HiFi)"), + )? { + w.extend(head.lines); + if head.ran { + let d = s.section("paralogs"); + w.extend(paraphase_lines(&d)?.iter().map(|l| format!(" {l}"))); + w.push(format!(" {PARALOG_NOTE}")); + w.push(String::new()); + } + } + if let Some(head) = text::head( + s, + "parascopy", + Some("SMN1/SMN2 Copy Number (Parascopy, short reads)"), + )? { + w.extend(head.lines); + if head.ran { + let d = s.section("parascopy"); + w.extend(parascopy::text_lines(&d)?.iter().map(|l| format!(" {l}"))); + w.push(format!(" {PARASCOPY_NOTE}")); + w.push(String::new()); + } + } + if let Some(why) = unchecked(s) { + w.push("## Paralog Copy Number".to_owned()); + w.push("---".to_owned()); + w.push(format!(" {why}")); + w.push(String::new()); + } + Ok(w) +} + +/// The Status badge of a section that ran: Complete, or Stale for a result +/// from an earlier run. +pub fn done_badge(sec: &Section<'_>) -> Inline { + if sec.state == State::Stale { + Inline::Badge("Stale".to_owned(), Colour::Yellow) + } else { + Inline::Badge("Complete".to_owned(), Colour::Green) + } +} + +/// Whether a card shows its own lines: the step ran, or its state is one no +/// collector writes. +pub fn shows_body(sec: &Section<'_>) -> bool { + !matches!( + sec.state, + State::Missing | State::Failed | State::Unreadable + ) +} + +/// Paraphase's card. +fn paraphase_card(d: &Section<'_>) -> Result { + let mut body = vec![Line::Stat("Status".to_owned(), done_badge(d))]; + let mut caveats = Vec::new(); + if shows_body(d) { + let path = format!("{PATH}.genes[]"); + let mut rows = String::new(); + for g in genes(d)? { + let f = |k: &str| need(g, k, &path).and_then(py); + rows += &format!( + "{}{}{}{}", + e(&f("gene")?), + e(&f("copy_number")?), + e(&f("status")?), + e(&f("detail")?) + ); + } + if !rows.is_empty() { + body.push(Line::Markup(format!( + " {rows}
    GeneCopy numberCallDetail
    " + ))); + } + if let Some(smn) = smn1(d)? { + body.push(Line::Stat( + "SMN1 by both tools".to_owned(), + Inline::Text(smn), + )); + } + if let Some(n) = d.get("other_regions")?.filter(|v| truthy(v)) { + body.push(Line::Stat( + "Other Paraphase regions".to_owned(), + Inline::Text(format!( + "{} ({} with a call)", + py(n)?, + py(d.need("other_regions_called")?)? + )), + )); + } + body.push(Line::Stat( + "Variants on phased haplotypes".to_owned(), + Inline::Text(variants(d)?), + )); + caveats.push(PARALOG_NOTE); + } + html::card( + d, + Card { + title: "Paralog Genes (Paraphase)".to_owned(), + full: true, + caveats, + body, + ..Card::default() + }, + ) +} + +/// Parascopy's card. +fn parascopy_card(d: &Section<'_>) -> Result { + let mut body = vec![Line::Stat("Status".to_owned(), done_badge(d))]; + let mut caveats = Vec::new(); + if shows_body(d) { + let (lines, loci_caveats) = parascopy::html_lines(d)?; + body.extend(lines); + caveats.extend(loci_caveats); + caveats.push(PARASCOPY_NOTE); + } + html::card( + d, + Card { + title: "SMN1/SMN2 Copy Number (Parascopy)".to_owned(), + caveats, + body, + ..Card::default() + }, + ) +} + +/// The HTML report's cards. +pub fn html(s: &Summary) -> Result { + let mut cards = Vec::new(); + let pg = s.section("paralogs"); + if pg.state != State::Missing { + cards.push(paraphase_card(&pg)?); + } + let pc = s.section("parascopy"); + if pc.state != State::Missing { + cards.push(parascopy_card(&pc)?); + } + if let Some(why) = unchecked(s) { + cards.push(Card { + title: "Paralog Copy Number".to_owned(), + body: vec![ + Line::Stat( + "Status".to_owned(), + Inline::Badge("Not checked".to_owned(), Colour::Gray), + ), + Line::Note(why), + ], + ..Card::default() + }); + } + Ok(HtmlPart { + cards, + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/parascopy.rs b/rust/crates/og-render/src/sections/parascopy.rs index e688a9b01..98a975893 100644 --- a/rust/crates/og-render/src/sections/parascopy.rs +++ b/rust/crates/og-render/src/sections/parascopy.rs @@ -1,2 +1,146 @@ -//! The report section `parascopy`. Empty until its unit of the 1.0.0 plan -//! lands. +//! Step 35 (Parascopy, short reads): SMN1 and SMN2 with the SMN1 marker, +//! the other loci of its catalog, and the depth counts it writes beside them +//! (HBA, CYP21A2, the dosage loci, the copy-number intervals). + +use pycompat::json::Value; + +use crate::escape::e; +use crate::model::{Line, Malformed, Section, get, is_str, need, py, truthy}; +use crate::notes::PARASCOPY_LOCI_NOTE; +use crate::sections::smn1::{SMN1_MARKER_CAVEAT, marker_state}; +use crate::sections::{cn_intervals, cyp21a2::cyp21a2_line, dosage::dosage_lines, hba::hba_line}; + +const PATH: &str = "sections.parascopy.data"; + +/// `(d.get(key) or {})`. +fn dict_or_empty<'a>(d: &Section<'a>, key: &str, empty: &'a Value) -> Result<&'a Value, Malformed> { + Ok(d.get(key)?.filter(|v| truthy(v)).unwrap_or(empty)) +} + +/// `d.get(key, ".")` as text. +fn dot(v: &Value, key: &str, path: &str) -> Result { + get(v, key, path)?.map_or(Ok(".".to_owned()), py) +} + +/// Step 35's lines, the same in both reports (the HTML card splits each at +/// its first `": "` into a label and a value). +pub fn lines(d: &Section<'_>) -> Result, Malformed> { + let empty = Value::Object(Default::default()); + let smn1 = dict_or_empty(d, "smn1", &empty)?; + let smn2 = dict_or_empty(d, "smn2", &empty)?; + let mut out = vec![format!( + "SMN1 copies: {}", + dot(smn1, "text", &format!("{PATH}.smn1"))? + )]; + if let Some(mk) = d.get("smn1_marker")?.filter(|v| truthy(v)) { + let path = format!("{PATH}.smn1_marker"); + let rsid = match get(mk, "rsid", &path)?.filter(|v| truthy(v)) { + Some(v) => py(v)?, + None => ".".to_owned(), + }; + out.push(format!( + "SMN1 g.27134T>G silent-carrier marker ({rsid}): {}", + marker_state(mk)? + )); + out.push(format!( + "SMN1 marker reading: {}", + dot(mk, "message", &path)? + )); + out.push(format!("SMN1 marker caveat: {SMN1_MARKER_CAVEAT}")); + } + out.push(format!( + "SMN2 copies: {}", + dot(smn2, "text", &format!("{PATH}.smn2"))? + )); + let f = |k: &str| d.get(k).and_then(|v| v.map_or(Ok(".".to_owned()), py)); + out.push(format!( + "SMN1 and SMN2 together (agCN) at exon 7: {} (quality {}, filter {})", + f("agcn")?, + f("agcn_qual")?, + f("agcn_filter")? + )); + out.push(format!("Reading: {}", f("reading")?)); + if let Some(h) = d.get("hba")?.filter(|v| truthy(v)) { + out.push(hba_line(h)?); + } + out.push(cyp21a2_line(d.get("cyp21a2")?)?); + out.extend(dosage_lines(d.get("dosage")?)?); + Ok(out) +} + +/// The other loci of the catalog (`d.get("loci") or []`). +fn loci<'a>(d: &Section<'a>) -> Result<&'a [Value], Malformed> { + match d.get("loci")?.filter(|v| truthy(v)) { + None => Ok(&[]), + Some(Value::Array(a)) => Ok(a), + Some(_) => Err(Malformed(format!("{PATH}.loci is not a list"))), + } +} + +/// `genes` with a space after each comma. +fn genes(x: &Value) -> Result { + Ok(py(need(x, "genes", &format!("{PATH}.loci[]"))?)?.replace(',', ", ")) +} + +/// One line per catalog locus other than SMN1, the same in both reports. +pub fn locus_lines(d: &Section<'_>) -> Result, Malformed> { + let path = format!("{PATH}.loci[]"); + let mut out = Vec::new(); + for x in loci(d)? { + let mut line = format!("{} (agCN): {}", genes(x)?, py(need(x, "text", &path)?)?); + if is_str(get(x, "status", &path)?, "called") + && let Some(r) = get(x, "reading", &path)?.filter(|r| !is_str(Some(r), ".")) + { + line += &format!(". Reading: {}", py(r)?); + } + out.push(line); + } + Ok(out) +} + +/// The text report's block, after its head. +pub fn text_lines(d: &Section<'_>) -> Result, Malformed> { + let mut out = lines(d)?; + out.extend(locus_lines(d)?); + out.extend(cn_intervals::lines(d.get("cn_intervals")?)?); + Ok(out) +} + +/// The HTML card's lines after its status: the step's lines as label and +/// value, the table of the other loci with its caveat, the intervals. +pub fn html_lines(d: &Section<'_>) -> Result<(Vec, Vec<&'static str>), Malformed> { + let mut body = Vec::new(); + let mut caveats = Vec::new(); + for line in lines(d)? { + let (label, value) = line.split_once(": ").unwrap_or((&line, "")); + body.push(Line::Stat( + label.to_owned(), + crate::model::Inline::Text(value.to_owned()), + )); + } + let loci = loci(d)?; + if !loci.is_empty() { + let path = format!("{PATH}.loci[]"); + let mut t = + " " + .to_owned(); + for x in loci { + let reading = if is_str(get(x, "status", &path)?, "called") { + py(need(x, "reading", &path)?)? + } else { + ".".to_owned() + }; + t += &format!( + "", + e(&genes(x)?), + e(&py(need(x, "text", &path)?)?), + e(&reading) + ); + } + t += "
    GenesCopies together (agCN)Reading
    {}{}{}
    "; + body.push(Line::Markup(t)); + caveats.push(PARASCOPY_LOCI_NOTE); + } + body.extend(cn_intervals::html(d.get("cn_intervals")?)?); + Ok((body, caveats)) +} diff --git a/rust/crates/og-render/src/sections/rfc1.rs b/rust/crates/og-render/src/sections/rfc1.rs index aff217dcf..aceaf96ac 100644 --- a/rust/crates/og-render/src/sections/rfc1.rs +++ b/rust/crates/og-render/src/sections/rfc1.rs @@ -1 +1,19 @@ -//! The report section `rfc1`. Empty until its unit of the 1.0.0 plan lands. +//! Step 9b's motif count of a flagged repeat locus: RFC1's AARRG unit, and +//! any other locus whose motif decides more than its length. + +use pycompat::json::Value; + +use crate::model::{Malformed, get, py}; + +/// ` motif count: : `, as both reports print it. +pub fn motif_text(locus: &str, m: &Value) -> Result { + let at = |k: &str| get(m, k, "the motif count"); + let verdict = at("verdict")?.map_or(Ok(".".to_owned()), py)?; + let note = at("note")?.map_or(Ok(String::new()), py)?; + Ok(format!("{locus} motif count: {verdict}: {note}")) +} + +/// RFC1's motif count. +pub fn rfc1_text(m: &Value) -> Result { + motif_text("RFC1", m) +} diff --git a/rust/crates/og-render/src/sections/smn1.rs b/rust/crates/og-render/src/sections/smn1.rs index eb4742abe..10abfcbdc 100644 --- a/rust/crates/og-render/src/sections/smn1.rs +++ b/rust/crates/og-render/src/sections/smn1.rs @@ -1 +1,63 @@ -//! The report section `smn1`. Empty until its unit of the 1.0.0 plan lands. +//! SMN1 read two ways: the copy-aware marker (g.27134T>G, the silent +//! carrier sign, and LPA's KIV-2 4925G>A uses the same reading), and +//! Paraphase's count beside Parascopy's. + +use pycompat::json::Value; + +use crate::model::{Malformed, get, is_str, need, py, truthy}; + +/// What the SMN1 marker can and cannot say. +pub const SMN1_MARKER_CAVEAT: &str = "The marker raises the chance that two SMN1 copies sit on one \ + chromosome (2+0, a silent carrier); it does not prove 2+0, and a carrier test in a clinical \ + laboratory confirms it."; + +/// A copy-aware marker's state in a few words: present or uncertain with +/// the reads and the copies that carry it, not seen with the reads, too +/// little depth, or not assessed. +pub fn marker_state(m: &Value) -> Result { + let at = |k: &str| get(m, k, "the marker"); + let st = match at("status")?.filter(|v| truthy(v)) { + Some(v) => py(v)?, + None => "not assessed".to_owned(), + }; + if st == "not assessed" { + return Ok(st); + } + let dot = |k: &str| at(k).and_then(|v| v.map_or(Ok(".".to_owned()), py)); + let mut out = format!( + "{st}, {} of {} reads carry it", + dot("alt_reads")?, + dot("depth")? + ); + if let Some(c) = at("copies")? + && !matches!(c, Value::Null) + && !is_str(Some(c), "") + && !is_str(Some(c), ".") + { + out += &format!(", about {} copies", py(c)?); + } + Ok(out) +} + +/// SMN1 from Paraphase beside Parascopy (step 35), with the verdict; `None` +/// when Parascopy has no SMN1 count. +pub fn smn1_text(m: &Value) -> Result, Malformed> { + let at = |k: &str| get(m, k, "sections.paralogs.data.smn1"); + let Some(pc) = at("parascopy")?.filter(|v| !matches!(v, Value::Null)) else { + return Ok(None); + }; + let pp = match at("paraphase")?.filter(|v| !matches!(v, Value::Null)) { + Some(v) => py(v)?, + None => "no call".to_owned(), + }; + let agree = at("agree")?; + let verdict = if is_str(agree, "agree") { + "the two agree" + } else if is_str(agree, "disagree") { + "they DISAGREE: check both before using either" + } else { + "no comparison: one of them has no call" + }; + let pc = py(need(pc, "text", "sections.paralogs.data.smn1.parascopy")?)?; + Ok(Some(format!("Paraphase {pp}, Parascopy {pc}: {verdict}"))) +} diff --git a/rust/crates/og-render/src/sections/str.rs b/rust/crates/og-render/src/sections/str.rs index 047c968c6..969e3a939 100644 --- a/rust/crates/og-render/src/sections/str.rs +++ b/rust/crates/og-render/src/sections/str.rs @@ -1 +1,476 @@ -//! The report section `str`. Empty until its unit of the 1.0.0 plan lands. +//! Step 09's repeat expansions (ExpansionHunter) with Stranger's status +//! (step 9b): the loci outside the normal range, the loci it does not +//! grade, the readings of the repeats STRchive leaves out, and every locus +//! with its count, status and notes, the key loci first. + +use pycompat::fmt::ljust; +use pycompat::json::Value; + +use crate::escape::e; +use crate::html; +use crate::model::{ + Card, Colour, HtmlPart, Inline, Line, Malformed, Summary, get, is_str, need, py, truthy, +}; +use crate::notes::{EH_NOTE, NOT_GRADED_NOTE, RFC1_NOTE}; +use crate::sections::paralogs::{done_badge, shows_body}; +use crate::sections::rfc1::{motif_text, rfc1_text}; +use crate::text; + +const PATH: &str = "sections.expansions.data"; + +/// What the report says of an allele that was not sized from spanning +/// reads. +fn sizing_text(sizing: &str) -> Option<&'static str> { + Some(match sizing { + "flanking_only" => "size not determined (flanking reads only)", + "in_repeat" => { + "longer than a read (in-repeat reads only): shown as at least the interval's lower end" + } + "low_confidence" => { + "low confidence (fewer than 2 spanning reads, or the interval reaches the disease range)" + } + "no_call" => "no call", + _ => return None, + }) +} + +/// `l.get(key)`. +fn at<'a>(l: &'a Value, key: &str) -> Result, Malformed> { + get(l, key, &format!("{PATH}.loci[]")) +} + +/// `(l.get(key) or "")` as text. +fn text_or_empty(l: &Value, key: &str) -> Result { + match at(l, key)?.filter(|v| truthy(v)) { + Some(v) => py(v), + None => Ok(String::new()), + } +} + +/// `d.get(key) or []`. +fn list<'a>(d: &'a Value, key: &str) -> Result<&'a [Value], Malformed> { + match get(d, key, PATH)?.filter(|v| truthy(v)) { + None => Ok(&[]), + Some(Value::Array(a)) => Ok(a), + Some(_) => Err(Malformed(format!("{PATH}.{key} is not a list"))), + } +} + +/// The repeat count of a locus: an in-repeat allele as at least the lower +/// end of its interval, or `not sized` without one. +pub fn count(l: &Value) -> Result { + let counts = py(need(l, "repeat_count", &format!("{PATH}.loci[]"))?)?; + let counts: Vec<&str> = counts.split('/').collect(); + let sizing = text_or_empty(l, "allele_sizing")?; + let sizing: Vec<&str> = sizing.split('/').collect(); + let cis = text_or_empty(l, "ci")?; + let cis: Vec<&str> = cis.split('/').collect(); + let mut out = Vec::new(); + for (i, c) in counts.iter().enumerate() { + let z = sizing.get(i).copied().unwrap_or(""); + let ci = if cis.len() == counts.len() { + cis[i] + } else if cis.len() == 1 { + cis[0] + } else { + "" + }; + let lo = ci.split('-').next().unwrap_or(""); + out.push(if z == "in_repeat" { + if pycompat::text::isdigit(lo) { + format!(">={lo}") + } else { + "not sized".to_owned() + } + } else { + (*c).to_owned() + }); + } + Ok(out.join("/")) +} + +/// Stranger's status, never `normal` for a locus that was not sized. +pub fn status(l: &Value) -> Result { + let st = at(l, "status")?.filter(|v| truthy(v)); + if st.is_none() && at(l, "not_graded")?.is_some_and(truthy) { + return Ok("not graded".to_owned()); + } + if is_str(st, "normal") + && !is_str(at(l, "sizing")?, "sized") + && !is_str(at(l, "sizing")?, "unknown") + { + return Ok("not sized".to_owned()); + } + st.map_or(Ok(String::new()), py) +} + +/// What the report says beside a locus: sizing, the catalog's normal +/// maximum, an intermediate range, no thresholds, the motif count. +pub fn notes(l: &Value) -> Result { + let mut out = Vec::new(); + if let Some(Value::Str(z)) = at(l, "sizing")? + && let Some(t) = sizing_text(z) + { + let ci = at(l, "ci")?; + let shown = match ci { + None | Some(Value::Null) => false, + Some(v) => !is_str(Some(v), "") && !is_str(Some(v), "."), + }; + out.push(if shown { + format!("{t}, interval {}", py(ci.expect("checked above"))?) + } else { + t.to_owned() + }); + } + if let Some(m) = at(l, "normal_max")?.filter(|v| truthy(v)) { + out.push(format!("normal max {}", py(m)?)); + } + if let Some(z) = at(l, "zone")?.filter(|v| truthy(v)) { + let path = format!("{PATH}.loci[]"); + out.push(format!( + "{}: {}; {}", + py(z)?, + py(need(l, "zone_label", &path)?)?, + py(need(l, "zone_citation", &path)?)? + )); + } + if let Some(n) = at(l, "not_graded")?.filter(|v| truthy(v)) { + out.push(format!("no thresholds in the catalog ({})", py(n)?)); + } + if let Some(m) = at(l, "motif")?.filter(|v| truthy(v)) { + let locus = py(need(l, "locus", &format!("{PATH}.loci[]"))?)?; + out.push(motif_text(&locus, m)?); + } + Ok(out.join("; ")) +} + +/// `(locus, count, status, notes)` for every locus, the key loci first; a +/// key locus missing from the output says so. +pub fn rows(d: &Value) -> Result, Malformed> { + let mut rows = Vec::new(); + for l in list(d, "loci")? { + rows.push([ + py(need(l, "locus", &format!("{PATH}.loci[]"))?)?, + count(l)?, + status(l)?, + notes(l)?, + ]); + } + let mut out = Vec::new(); + for k in list(d, "key_loci")? { + let locus = need(k, "locus", &format!("{PATH}.key_loci[]"))?; + let name = py(locus)?; + // `k["locus"] not in have`: compared by value, as Python compares. + let have = list(d, "loci")?.iter().any(|l| { + get(l, "locus", PATH) + .ok() + .flatten() + .is_some_and(|x| crate::model::py_eq(x, locus)) + }); + if !have { + out.push([ + name, + "not in output".to_owned(), + String::new(), + String::new(), + ]); + } + } + out.extend(rows); + Ok(out) +} + +/// The first line of one reading of the repeats STRchive leaves out (CFTR, +/// UGT1A1, 5-HTTLPR). +fn reading_head(r: &Value) -> Result { + let path = format!("{PATH}.readings[]"); + let f = |k: &str| need(r, k, &path).and_then(py); + Ok(format!( + "{} {}: {}", + f("locus")?, + f("repeat")?, + reading_what(r)? + )) +} + +/// A reading's genotype when it was read, else its status in words. +fn reading_what(r: &Value) -> Result { + let path = format!("{PATH}.readings[]"); + let st = need(r, "status", &path)?; + if is_str(Some(st), "read") { + py(need(r, "genotype", &path)?) + } else { + Ok(py(st)?.replace('_', " ")) + } +} + +/// The joined items of a list or the keys of a dict, as `", ".join(x)`. +fn joined(v: &Value, key: &str) -> Result { + match v { + Value::Array(a) => Ok(a.iter().map(py).collect::, _>>()?.join(", ")), + Value::Object(o) => Ok(o.keys().cloned().collect::>().join(", ")), + Value::Str(s) => Ok(s.chars().map(String::from).collect::>().join(", ")), + _ => Err(Malformed(format!("{PATH}.{key} is not a list"))), + } +} + +/// `len(x)` of a list, a dict or a string. +fn len(v: &Value, key: &str) -> Result { + match v { + Value::Array(a) => Ok(a.len()), + Value::Object(o) => Ok(o.len()), + Value::Str(s) => Ok(s.chars().count()), + _ => Err(Malformed(format!("{PATH}.{key} has no length"))), + } +} + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "expansions", Some("Repeat Expansions (ExpansionHunter)"))? + else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if !head.ran { + return Ok(w); + } + let sec = s.section("expansions"); + let d = sec.data; + let g = |k: &str| get(d, k, PATH); + w.push(format!(" Loci in output: {}", py(sec.need("records")?)?)); + let flagged = list(d, "flagged")?; + if g("stranger_outdated")?.is_some_and(truthy) { + w.push( + " Stranger's result is older than ExpansionHunter's: rerun step 9b. No locus is \ + marked as normal or expanded." + .to_owned(), + ); + } else if !g("stranger")?.is_some_and(truthy) { + w.push( + " Stranger (step 9b) did not run: no locus is marked as normal or expanded." + .to_owned(), + ); + } else if !flagged.is_empty() { + w.push(format!( + " Outside the normal range (Stranger): {}", + len(sec.need("flagged")?, "flagged")? + )); + let loci = list(d, "loci")?; + for l in flagged { + let path = format!("{PATH}.flagged[]"); + let locus = need(l, "locus", &path)?; + // `by.get(l["locus"], l)`: the full locus row when there is one; + // the last row of a locus listed twice, as a dict keeps it. + let full = loci + .iter() + .rev() + .find(|x| { + get(x, "locus", PATH) + .ok() + .flatten() + .is_some_and(|y| crate::model::py_eq(y, locus)) + }) + .unwrap_or(l); + w.push(format!( + " {} {} {}", + ljust(&py(locus)?, 8), + count(full)?, + py(need(l, "status", &path)?)? + )); + if let Some(m) = get(l, "rfc1_motif", &path)?.filter(|v| truthy(v)) { + w.push(format!(" {}", rfc1_text(m)?)); + } + if let Some(m) = get(l, "motif", &path)?.filter(|v| truthy(v)) { + w.push(format!(" {}", motif_text(&py(locus)?, m)?)); + } + } + } else { + w.push(" Outside the normal range (Stranger): none".to_owned()); + } + if let Some(ng) = g("not_graded")?.filter(|v| truthy(v)) { + let Value::Object(o) = ng else { + return Err(Malformed(format!("{PATH}.not_graded is not an object"))); + }; + let items: Vec = o + .iter() + .map(|(k, v)| py(v).map(|v| format!("{k}: {v}"))) + .collect::>()?; + w.push(format!( + " Not graded (no thresholds in the catalog): {} ({})", + o.len(), + items.join("; ") + )); + w.push(format!(" {NOT_GRADED_NOTE}")); + } + for r in list(d, "readings")? { + let path = format!("{PATH}.readings[]"); + w.push(format!(" {}", reading_head(r)?)); + w.push(format!( + " {} ({})", + py(need(r, "reading", &path)?)?, + py(need(r, "source", &path)?)? + )); + } + if g("stranger")?.is_some_and(truthy) + && let Some(n) = g("no_status")?.filter(|v| truthy(v)) + { + w.push(format!( + " Loci without a Stranger status (not in its catalog): {}", + py(n)? + )); + } + if let Some(u) = g("unsized")?.filter(|v| truthy(v)) { + w.push(format!( + " Not sized from short reads, so not counted as normal: {} ({})", + len(u, "unsized")?, + joined(u, "unsized")? + )); + } + if let Some(i) = g("intermediate")?.filter(|v| truthy(v)) { + w.push(format!( + " In an intermediate range (bin/str_intermediate_ranges.tsv): {}", + joined(i, "intermediate")? + )); + } + let rows = rows(d)?; + if !rows.is_empty() { + w.push(" Every locus (key loci first): repeat count, Stranger status, notes".to_owned()); + for [locus, count, status, notes] in &rows { + let status = if status.is_empty() { "-" } else { status }; + let tail = if notes.is_empty() { + String::new() + } else { + format!(" {notes}") + }; + w.push(format!( + " {} {} {status}{tail}", + ljust(locus, 8), + ljust(count, 10) + )); + } + } + if flagged.iter().any(|l| { + get(l, "rfc1_motif", PATH) + .ok() + .flatten() + .is_some_and(truthy) + }) { + w.push(format!(" {RFC1_NOTE}")); + } + w.push(format!(" {EH_NOTE}")); + w.push(String::new()); + Ok(w) +} + +/// The HTML report's card, shown on every report: "Not run" when step 09 +/// is not in the summary. +pub fn html(s: &Summary) -> Result { + let sec = s.section("expansions"); + let mut body = vec![Line::Stat("Status".to_owned(), done_badge(&sec))]; + let mut caveats = Vec::new(); + if shows_body(&sec) { + let d = sec.data; + let g = |k: &str| get(d, k, PATH); + let label = "Outside the normal range (Stranger)".to_owned(); + let flagged = list(d, "flagged")?; + body.push(if g("stranger_outdated")?.is_some_and(truthy) { + Line::Stat( + label, + Inline::Badge( + "older than ExpansionHunter's result: rerun step 9b".to_owned(), + Colour::Yellow, + ), + ) + } else if !g("stranger")?.is_some_and(truthy) { + Line::Stat(label, Inline::Badge("not run".to_owned(), Colour::Gray)) + } else { + let colour = if flagged.is_empty() { + Colour::Gray + } else { + Colour::Yellow + }; + Line::Stat(label, Inline::Badge(flagged.len().to_string(), colour)) + }); + if let Some(u) = g("unsized")?.filter(|v| truthy(v)) { + body.push(Line::Stat( + "Not sized from short reads (not counted as normal)".to_owned(), + Inline::Badge(len(u, "unsized")?.to_string(), Colour::Yellow), + )); + } + if let Some(i) = g("intermediate")?.filter(|v| truthy(v)) { + body.push(Line::Stat( + "In an intermediate range".to_owned(), + Inline::Badge(joined(i, "intermediate")?, Colour::Yellow), + )); + } + if let Some(n) = g("not_graded")?.filter(|v| truthy(v)) { + body.push(Line::Stat( + "Not graded (no thresholds in the catalog)".to_owned(), + Inline::Badge(joined(n, "not_graded")?, Colour::Gray), + )); + caveats.push(NOT_GRADED_NOTE); + } + for r in list(d, "readings")? { + let path = format!("{PATH}.readings[]"); + let colour = if get(r, "five_t", &path)?.is_some_and(truthy) + || is_str(get(r, "agreement", &path)?, "differs") + { + Colour::Yellow + } else { + Colour::Gray + }; + body.push(Line::Stat( + format!( + "{} {}", + py(need(r, "locus", &path)?)?, + py(need(r, "repeat", &path)?)? + ), + Inline::Badge(reading_what(r)?, colour), + )); + body.push(Line::Note(format!( + "{} ({})", + py(need(r, "reading", &path)?)?, + py(need(r, "source", &path)?)? + ))); + } + for l in flagged { + let path = format!("{PATH}.flagged[]"); + if let Some(m) = get(l, "rfc1_motif", &path)?.filter(|v| truthy(v)) { + body.push(Line::Note(rfc1_text(m)?)); + caveats.push(RFC1_NOTE); + } + if let Some(m) = get(l, "motif", &path)?.filter(|v| truthy(v)) { + body.push(Line::Note(motif_text(&py(need(l, "locus", &path)?)?, m)?)); + } + } + let rows = rows(d)?; + if !rows.is_empty() { + let mut t = " \ + " + .to_owned(); + for r in &rows { + t += ""; + for x in r { + t += &format!("", e(x)); + } + t += ""; + } + t += "
    LocusRepeat CountStrangerNotes
    {}
    "; + body.push(Line::Markup(t)); + } + caveats.push(EH_NOTE); + } + let card = html::card( + &sec, + Card { + title: "Repeat Expansions".to_owned(), + caveats, + body, + ..Card::default() + }, + )?; + Ok(HtmlPart { + cards: vec![card], + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/traits.rs b/rust/crates/og-render/src/sections/traits.rs index a087281ea..e616136d6 100644 --- a/rust/crates/og-render/src/sections/traits.rs +++ b/rust/crates/og-render/src/sections/traits.rs @@ -1,2 +1,251 @@ -//! The report section `traits`. Empty until its unit of the 1.0.0 plan -//! lands. +//! Step 42's traits panel: one row per catalog site with its genotype and +//! the cited reading, the genotype rules that read several sites together +//! (APOL1 with its G2, SERPINA1, HFE and the others), and the APOE lines +//! kept apart behind a warning. + +use pycompat::json::Value; + +use crate::escape::e; +use crate::html; +use crate::model::{ + Card, Colour, HtmlPart, Inline, Line, Malformed, Section, Summary, is_str, need, py, truthy, +}; +use crate::notes::TRAITS_NOTE; +use crate::sections::{apoe, g2}; +use crate::text; + +const ROWS: &str = "sections.traits.data.rows[]"; +const RULES: &str = "sections.traits.data.rules[]"; + +/// The small grey line under a cell's main text. +const SMALL: &str = "
    "; + +/// The header row of the traits table and of the folded APOE table. +const HEAD: &str = + " TraitGeneGenotypeReadingSource"; + +/// A list in the section's data, `[]` when it is absent or empty. +fn list<'a>(d: &Section<'a>, key: &str, need_it: bool) -> Result<&'a [Value], Malformed> { + let v = if need_it { + Some(d.need(key)?) + } else { + d.get(key)?.filter(|v| truthy(v)) + }; + match v { + None => Ok(&[]), + Some(Value::Array(a)) => Ok(a), + Some(_) => Err(Malformed(format!( + "sections.traits.data.{key} is not a list" + ))), + } +} + +/// ` [ancestry]` for a row whose reading depends on ancestry. +fn ancestry(r: &Value, path: &str) -> Result { + Ok(is_str(Some(need(r, "ancestry", path)?), "yes")) +} + +/// The text report's lines (before the closing blank line). +pub fn text_lines(d: &Section<'_>, sample: &str) -> Result, Malformed> { + let n = |k: &str| d.need(k).and_then(py); + let mut out = vec![format!( + " Read: {} No genotype at the position: {} Other allele: {}", + n("called")?, + n("not_covered")?, + n("other_allele")? + )]; + let rows = list(d, "rows", true)?; + let mut missing = Vec::new(); + for r in rows { + let f = |k: &str| need(r, k, ROWS).and_then(py); + if is_str(Some(need(r, "status", ROWS)?), "not_covered") { + missing.push(f("rsid")?); + continue; + } + let mark = if ancestry(r, ROWS)? { + " [ancestry]" + } else { + "" + }; + out.push(format!( + " {} ({} {}): {}{mark}. {} (PMID {})", + f("trait")?, + f("gene")?, + f("rsid")?, + f("genotype")?, + f("reading")?, + f("pmids")? + )); + } + if !missing.is_empty() { + out.push(format!( + " Not read (no genotype in the gVCF): {}", + missing.join(", ") + )); + } + out.push(format!( + " {TRAITS_NOTE} Notes per row: traits/{sample}_traits.tsv" + )); + let rules = list(d, "rules", false)?; + if !rules.is_empty() { + out.push(format!( + " Genotype rules (several sites read together; traits/{sample}_genotype_rules.tsv):" + )); + for r in rules { + let f = |k: &str| need(r, k, RULES).and_then(py); + out.push(format!( + " {} ({} {}): {}. {} (PMID {})", + f("trait")?, + f("gene")?, + f("rsids")?.replace(',', ", "), + g2::rule_result(r)?, + f("reading")?, + f("pmids")? + )); + if let Some(line) = g2::g2_text(r)? { + out.push(line); + } + } + } + out.extend(apoe::text_lines(list(d, "apoe", false)?)?); + Ok(out) +} + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "traits", Some("Traits Panel (step 42)"))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if head.ran { + let sample = py(s.need("sample")?)?; + w.extend(text_lines(&s.section("traits"), &sample)?); + w.push(String::new()); + } + Ok(w) +} + +/// The ancestry badge after a genotype or a result, when the row has one. +fn ancestry_badge(r: &Value, path: &str) -> Result { + Ok(if ancestry(r, path)? { + format!(" {}", html::badge("ancestry", Colour::Yellow)) + } else { + String::new() + }) +} + +/// One row of the traits table (and of the folded APOE table). +fn row(r: &Value) -> Result { + let f = |k: &str| need(r, k, ROWS).and_then(py); + Ok(format!( + "{}{}{SMALL}{}{}{}{}{SMALL}{}\ + PMID {}", + e(&f("trait")?), + e(&f("gene")?), + e(&f("rsid")?), + e(&f("genotype")?), + ancestry_badge(r, ROWS)?, + e(&f("reading")?), + e(&f("note")?), + e(&f("pmids")?) + )) +} + +/// The genotype rules table, none without rules. +fn rules_html(d: &Section<'_>, sample: &str) -> Result, Malformed> { + let rules = list(d, "rules", false)?; + if rules.is_empty() { + return Ok(Vec::new()); + } + let mut out = vec![ + Line::Markup("

    Genotype rules (several sites read together)

    ".to_owned()), + Line::Markup(" ".to_owned()), + Line::Markup( + " \ + " + .to_owned(), + ), + ]; + for r in rules { + let f = |k: &str| need(r, k, RULES).and_then(py); + out.push(Line::Markup(format!( + " \ + ", + e(&f("trait")?), + e(&f("gene")?), + e(&f("rsids")?.replace(',', ", ")), + e(&f("genotypes")?.replace(',', ", ")), + e(&g2::rule_result(r)?), + ancestry_badge(r, RULES)?, + g2::g2_result_html(r)?, + e(&f("reading")?), + g2::g2_reading_html(r)?, + e(&f("note")?), + e(&f("pmids")?) + ))); + } + out.push(Line::Markup("
    TraitGene and sitesGenotypesResultReadingSource
    {}{}{SMALL}{}{}{}{}{}{}{}{SMALL}{}PMID {}
    ".to_owned())); + out.push(Line::Note(format!( + "Every rule line: traits/{sample}_genotype_rules.tsv" + ))); + Ok(out) +} + +/// The traits card's lines: the counts, one table row per site read or not +/// read, the rules, and the APOE lines folded away behind a warning. +pub fn html_lines(d: &Section<'_>, sample: &str) -> Result, Malformed> { + let n = |k: &str| d.need(k).and_then(py); + let mut out = vec![ + Line::Stat( + "Read / no genotype / other allele".to_owned(), + Inline::Text(format!( + "{} / {} / {}", + n("called")?, + n("not_covered")?, + n("other_allele")? + )), + ), + Line::Markup(" ".to_owned()), + Line::Markup(HEAD.to_owned()), + ]; + for r in list(d, "rows", true)? { + out.push(Line::Markup(format!(" {}", row(r)?))); + } + out.push(Line::Markup("
    ".to_owned())); + out.extend(rules_html(d, sample)?); + out.extend( + apoe::html_lines(list(d, "apoe", false)?, HEAD, row)? + .into_iter() + .map(Line::Markup), + ); + out.push(Line::Note(format!( + "Every row with its note: traits/{sample}_traits.tsv" + ))); + Ok(out) +} + +/// The HTML report's card, shown on every report: "Not run" when step 42 +/// is not in the summary. +pub fn html(s: &Summary) -> Result { + let sec = s.section("traits"); + let (body, caveats) = if sec.state.ran() { + let sample = py(s.need("sample")?)?; + (html_lines(&sec, &sample)?, vec![TRAITS_NOTE]) + } else { + (Vec::new(), Vec::new()) + }; + let card = html::card( + &sec, + Card { + title: "Traits".to_owned(), + full: true, + caveats, + body, + ..Card::default() + }, + )?; + Ok(HtmlPart { + cards: vec![card], + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/tests/port_report_core.rs b/rust/crates/og-render/tests/port_report_core.rs index df225fe2a..8af5502ee 100644 --- a/rust/crates/og-render/tests/port_report_core.rs +++ b/rust/crates/og-render/tests/port_report_core.rs @@ -254,10 +254,15 @@ 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"]); + let results = shown.len() - 2; + assert_eq!(&shown[..2], ["qc", "variants"]); + assert_eq!(&shown[results..], ["not-run", "not-assessed"]); let in_order: Vec<&str> = sids.iter().copied().filter(|s| shown.contains(s)).collect(); - assert_eq!(&shown[..2], in_order.as_slice()); - assert_eq!(text_chapters(&txt), ["QUALITY"]); + assert_eq!(&shown[..results], in_order.as_slice()); + assert_eq!( + text_chapters(&txt).first().map(String::as_str), + Some("QUALITY") + ); let q = txt.split("# 1. QUALITY").nth(1).unwrap_or(""); assert!(q.find("## Quality control") < q.find("## Variant Calling (DeepVariant)")); assert!(q.contains("## 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)[..results], in_order[..]); } #[test] From c401572f16529e8abcc3e36a67c914af308b33dc Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:37:23 +0100 Subject: [PATCH 03/29] test(parity): compare the five new report sections with the Python, every branch The report-render oracle keeps the expansions, Lp(a), paralogs, traits and blood group sections now, and runs main's render_report.py (the commit whose repeat caveat links the split results docs). Seven new summaries drive each branch of those sections in each state: flagged and unsized repeats, RFC1's motif, readings, Stranger off or outdated, the Lp(a) null allele and unread variants, stale and failed steps, a state no collector writes, and a sample name HTML must escape. Bead: D1 Bead: D14 --- tests/parity/cases.tsv | 8 +++ tests/parity/gen/report-corpus.sh | 116 +++++++++++++++++++++++++++++- tests/parity/oracle.ref | 2 +- 3 files changed, 123 insertions(+), 3 deletions(-) diff --git a/tests/parity/cases.tsv b/tests/parity/cases.tsv index 3e7e16214..909873f5c 100644 --- a/tests/parity/cases.tsv +++ b/tests/parity/cases.tsv @@ -1255,3 +1255,11 @@ 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, the traits, copy-number and repeat sections: every branch of each in each state (m-genome-*). +report-render m-genome-ok gen:report-corpus.sh --summary m-genome-ok.json -o m-genome-ok.txt -o m-genome-ok.html m-genome-ok.txt,m-genome-ok.html bytes PYTHON_IMAGE +report-render m-genome-stale gen:report-corpus.sh --summary m-genome-stale.json -o m-genome-stale.txt -o m-genome-stale.html m-genome-stale.txt,m-genome-stale.html bytes PYTHON_IMAGE +report-render m-genome-failed gen:report-corpus.sh --summary m-genome-failed.json -o m-genome-failed.txt -o m-genome-failed.html m-genome-failed.txt,m-genome-failed.html bytes PYTHON_IMAGE +report-render m-genome-other gen:report-corpus.sh --summary m-genome-other.json -o m-genome-other.txt -o m-genome-other.html m-genome-other.txt,m-genome-other.html bytes PYTHON_IMAGE +report-render m-genome-stranger gen:report-corpus.sh --summary m-genome-stranger.json -o m-genome-stranger.txt -o m-genome-stranger.html m-genome-stranger.txt,m-genome-stranger.html bytes PYTHON_IMAGE +report-render m-genome-outdated gen:report-corpus.sh --summary m-genome-outdated.json -o m-genome-outdated.txt -o m-genome-outdated.html m-genome-outdated.txt,m-genome-outdated.html bytes PYTHON_IMAGE +report-render m-genome-no-stranger gen:report-corpus.sh --summary m-genome-no-stranger.json -o m-genome-no-stranger.txt -o m-genome-no-stranger.html m-genome-no-stranger.txt,m-genome-no-stranger.html bytes PYTHON_IMAGE diff --git a/tests/parity/gen/report-corpus.sh b/tests/parity/gen/report-corpus.sh index d16728bc9..c77fae204 100644 --- a/tests/parity/gen/report-corpus.sh +++ b/tests/parity/gen/report-corpus.sh @@ -25,7 +25,7 @@ set -euo pipefail export LC_ALL=C TZ=UTC here=$(cd "$(dirname "$0")" && pwd) root=$(cd "${here}/../../.." && pwd) -ORACLE_SHA=1ec630690e63796b4410d23ec65768200adb8dc0 +ORACLE_SHA=65095e975bea01c74ed92a4f1b8c1bd00d05caaa og=${OG:-${root}/rust/target/release/og} # --- the oracle's modules ------------------------------------------------------------ @@ -114,6 +114,111 @@ 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' +# The sections of the traits, copy-number and repeat unit: every branch of +# each, in each state (the summary-collect folders never hold an +# ExpansionHunter result, and their blood groups render in text only). +sec() { printf '.sections.%s = {"title": "%s", "state": "%s", "note": %s, "file_date": "2021-03-04 05:06 UTC", "source": null, "data": %s}' "$1" "$1" "$2" "$3" "$4"; } +eh='{"records": 6, "stranger": true, "no_status": 2, "unsized": ["DMPK", "RFC1"], "intermediate": ["HTT"], + "not_graded": {"MUC1": "reference >300 bp", "CEL": ""}, + "flagged": [{"locus": "RFC1", "status": "pathogenic", "rfc1_motif": {"verdict": "expanded", "note": "AAGGG & "}}, + {"locus": "HTT", "status": "premutation", "motif": {"verdict": "pure", "note": "CAG"}}, + {"locus": "ATXN1", "status": "full_mutation", "repeat_count": "60"}, + {"locus": "FXN", "status": "pathogenic", "repeat_count": "900/12", "allele_sizing": "in_repeat/spanning", "ci": "700-1000", "rfc1_motif": {"verdict": "x"}}], + "loci": [{"locus": "HTT", "repeat_count": "17/40", "allele_sizing": "spanning/in_repeat", "ci": "17-17/38-45", + "status": "premutation", "sizing": "in_repeat", "normal_max": 26, "zone": "reduced penetrance", + "zone_label": "36 to 39 repeats", "zone_citation": "PMID 1", "motif": {"verdict": "pure", "note": "CAG"}}, + {"locus": "RFC1", "repeat_count": "11/11", "allele_sizing": "in_repeat/in_repeat", "ci": "x-1", "status": "normal", + "sizing": "flanking_only", "normal_max": null}, + {"locus": "DMPK", "repeat_count": "5/7", "allele_sizing": "in_repeat", "ci": "12-20", "status": "normal", + "sizing": "low_confidence"}, + {"locus": "MUC1", "repeat_count": ".", "status": "", "not_graded": "reference >300 bp", "sizing": "no_call", "ci": "."}, + {"locus": "AR", "repeat_count": "22", "status": "normal", "sizing": "sized", "ci": ""}, + {"locus": "HTT", "repeat_count": "18/41", "status": "normal", "sizing": "unknown"}], + "key_loci": [{"locus": "C9ORF72"}, {"locus": "HTT"}, {"locus": "FMR1"}], + "readings": [{"locus": "CFTR", "repeat": "TG", "genotype": "11/12", "status": "read", "reading": "a reading", + "source": "PMID 2", "five_t": true}, + {"locus": "UGT1A1", "repeat": "TA", "genotype": ".", "status": "not_read", "reading": "r2", "source": "s2", + "agreement": "differs"}, + {"locus": "SLC6A4", "repeat": "5-HTTLPR", "genotype": "L/S", "status": "read", "reading": "r3", "source": "s3"}]}' +lpa='{"status": "too little depth", "message": "8.0x over the exons", "advice": "Measure Lp(a) once.", + "flag": "none", "flag_text": "No genetic sign.", + "variants": [{"rsid": "rs10455872", "genotype": "./.", "status": "no_call", "risk_alleles": null}, + {"rsid": "rs3798220", "genotype": "C/C", "status": "called", "risk_alleles": 2}, + {"rsid": "rs0", "genotype": "T/C", "status": "called", "risk_alleles": 1.0}]}' +lpa_null='{"status": "estimated", "kiv2_copies": "19.8", "ci95_low": "17.9", "ci95_high": "22.0", "class": "low", + "message": "about 20", "advice": ".", "flag": "high", "flag_text": "Genetic signs.", "variants": [], + "null_allele": {"rsid": "", "status": "uncertain", "alt_reads": "2", "depth": "150", "copies": "0.4", "message": "m"}}' +blood='{"abo": "AB & \"O\"", "rhd": "RhD négatif", + "systems": [{"system": "ABO", "phenotype": "AB", "genotype": "A1/B", "confidence": "high"}, + {"system": "Kell", "phenotype": "K-k+ é", "genotype": "KEL*02/KEL*02", "confidence": "medium"}, + {"system": "a-very-long-system", "phenotype": "a phenotype longer than thirty-four characters", "genotype": "KEL*01.07", + "confidence": "low"}]}' +traits='{"called": 3, "not_covered": 2, "other_allele": 1, + "rows": [{"rsid": "rs4988235", "gene": "MCM6", "trait": "Lactase ", "genotype": "A/G", "status": "called", + "reading": "Likely persistent", "ancestry": "yes", "note": "European & others", "pmids": "1,2"}, + {"rsid": "rs1", "gene": "G1", "trait": "t1", "genotype": ".", "status": "not_covered", "reading": ".", + "ancestry": "no", "note": ".", "pmids": "3"}, + {"rsid": "rs2", "gene": "G2", "trait": "t2", "genotype": "./.", "status": "not_covered", "reading": ".", + "ancestry": "no", "note": ".", "pmids": "4"}, + {"rsid": "rs3", "gene": "G3", "trait": "t3", "genotype": "C/T", "status": "other_allele", "reading": "other", + "ancestry": "no", "note": "n", "pmids": "5"}], + "rules": [{"rule": "APOL1", "gene": "APOL1", "trait": "Kidney disease", "rsids": "rs73885319,rs60910145", + "genotypes": "A/G,T/G", "phase": "read_backed", "result": "G0/G1", "status": "called", "reading": "one G1", + "ancestry": "yes", "note": "African", "pmids": "6", + "g2": {"result": "G1/G2", "text": "G2 read from the VCF.", "reading": "two risk variants "}}, + {"rule": "HFE", "gene": "HFE", "trait": "Iron", "rsids": "rs1800562,rs1799945", "genotypes": "G/G,C/C", + "phase": "not_needed", "result": "wt/wt", "status": "called", "reading": "no variant", "ancestry": "no", + "note": ".", "pmids": "7"}, + {"rule": "GC", "gene": "GC", "trait": "Vitamin D", "rsids": "rs7041", "genotypes": ".", "phase": "unphased", + "result": ".", "status": "not_covered", "reading": ".", "ancestry": "no", "note": ".", "pmids": "8", + "g2": {"result": ".", "text": "not read", "reading": "r"}}, + {"rule": "MAPT", "gene": "MAPT", "trait": "t", "rsids": "rs1", "genotypes": "A/G", "phase": "other", + "result": "H1/H2", "status": "called", "reading": "r", "ancestry": "no", "note": "n", "pmids": "9"}], + "apoe": [{"rsid": "rs429358+rs7412", "gene": "APOE", "trait": "APOE e4", "genotype": "e3/e4", "status": "called", + "reading": "one e4", "ancestry": "yes", "note": "risk factor", "pmids": "10"}]}' +paraphase='{"genes": [{"gene": "SMN1", "copy_number": "2", "status": "called", "detail": "."}, + {"gene": "PMS2", "copy_number": "no call", "status": "no_call", "detail": "one haplotype "}, + {"gene": "HBA1", "copy_number": "1", "status": "called", "detail": ""}], + "other_regions": 0, "other_regions_called": 0, "variants": {"smn1": 1.5, "hba": 2}, + "smn1": {"paraphase": null, "parascopy": {"text": "2 (quality 30)"}, "agree": null}}' +parascopy='{"smn1": {"text": "2 (quality 30, filter PASS)"}, "smn2": {}, "agcn": "4", "agcn_qual": "40", + "agcn_filter": "PASS", "reading": "two SMN1 copies", + "smn1_marker": {"rsid": null, "status": "present", "alt_reads": "10", "depth": "40", "copies": "1.0", + "message": "a higher chance of 2+0"}, + "hba": {"status": "not assessed", "message": "not assessed: flank depth under 8x"}, + "cyp21a2": {"status": "called", "cyp21a2_copies": "2", "module_total": "4", "expected_total": "4", + "finding": "conversion", "conversion_sites": "In2G", "reading": "a possible conversion"}, + "dosage": [{"locus": "HP", "status": "called", "copies": "3", "estimate": "3.02", "reading": "."}, + {"locus": "CCL3L1", "status": "called", "copies": "5", "estimate": "4.9", "reading": "more copies"}, + {"locus": "LCE3BC", "status": "not assessed", "copies": ".", "estimate": ".", "reason": "low_control_depth", + "reading": "."}], + "cn_intervals": {"rows": 4, "by_status": {"not assessed": 1, "called": 3}, "changes": []}, + "loci": [{"genes": "AMY1A,AMY1B,AMY1C", "text": "6 (quality 25)", "status": "called", "reading": "six copies"}, + {"genes": "C4A,C4B", "text": "not assessed (no read)", "status": "not assessed", "reading": "r"}, + {"genes": "CFH", "text": "4", "status": "called", "reading": "."}]}' +parascopy_b='{"smn1": {"text": "1"}, "cyp21a2": {"status": "not assessed", "reason": "psv_low_depth"}, + "dosage": [], "cn_intervals": {"rows": 2, "by_status": {}, "changes": null}}' +parascopy_c='{"cyp21a2": {"status": "not assessed", "message": "", "reason": ""}, "cn_intervals": {"rows": 3, + "by_status": {"not assessed": 3}}, "hba": {"status": "called", "alpha_globin_genes": "4"}}' +mut genome-ok "$(sec expansions ok null "$eh") | $(sec lpa ok null "$lpa") | $(sec bloodtype ok null "$blood") + | $(sec traits ok null "$traits") | $(sec paralogs ok null "$paraphase") | $(sec parascopy ok null "$parascopy")" +mut genome-stale "$(sec expansions stale '"from 2020"' "$eh") | $(sec lpa stale '"from <2020>"' "$lpa_null") + | $(sec bloodtype stale '"old"' "$blood") | $(sec traits stale '"old"' "$traits") + | $(sec paralogs stale '"old"' "$paraphase") | $(sec parascopy stale '"old"' "$parascopy_b") + | .sample = \"A&B \" | .new_since = {\"previous_utc\": \"2026-01-01T00:00:00Z\", + \"sections\": [\"traits\", \"parascopy\", \"expansions\", \"lpa\", \"bloodtype\"]}" +mut genome-failed "$(sec expansions failed '"exit 1"' '{}') | $(sec lpa failed '"exit 2"' '{}') + | $(sec bloodtype unreadable '"bad table"' '{}') | $(sec traits failed '"exit 3"' '{}') + | $(sec paralogs failed '"exit 4"' '{}') | $(sec parascopy unreadable '"bad"' '{}')" +mut genome-other "$(sec expansions weird null '{}') | $(sec traits weird null '{}') | $(sec parascopy weird null "$parascopy_c") + | $(sec paralogs missing null '{}')" +mut genome-stranger "$(sec expansions ok null '{"records": 0, "stranger": true, "flagged": [], "loci": []}') + | $(sec parascopy failed '"exit 1"' '{}')" +mut genome-outdated "$(sec expansions ok null '{"records": 1, "stranger": true, "stranger_outdated": true, + "loci": [{"locus": "AR", "repeat_count": "22"}]}') | $(sec parascopy ok null "$parascopy_c")" +mut genome-no-stranger "$(sec expansions ok null '{"records": 2, "stranger": false, "no_status": 2, + "not_graded": {}, "unsized": [], "intermediate": []}') | $(sec traits ok null '{"called": 0, "not_covered": 0, + "other_allele": 0, "rows": [], "rules": [], "apoe": []}')" rm -f "$base" # --- the oracle ----------------------------------------------------------------------- @@ -131,7 +236,7 @@ cp "$prog" "$d/render_report.py" PYTHONPATH=$d python3 "$d/render_report.py" "$@" rm -rf "$d" # The sections og report render writes; the others are left out. -PORTED=" qc variants not-run not-assessed " +PORTED=" qc variants not-run not-assessed expansions lpa paralogs traits-panel bloodtype " while [ $# -gt 0 ]; do case "$1" in -o) @@ -194,6 +299,13 @@ cat > text.awk << 'AWK' BEGIN { rule = sprintf("%80s", ""); gsub(/ /, "#", rule) title["## Quality control"] = "qc"; title["## Variant Calling (DeepVariant)"] = "variants" + title["## Repeat Expansions (ExpansionHunter)"] = "expansions" + title["## Lp(a): LPA KIV-2 Repeat Count and Risk Variants"] = "lpa" + title["## Paralog Genes (Paraphase, PacBio HiFi)"] = "paralogs" + title["## SMN1/SMN2 Copy Number (Parascopy, short reads)"] = "paralogs" + title["## Paralog Copy Number"] = "paralogs" + title["## Traits Panel (step 42)"] = "traits-panel" + title["## Blood Groups (predicted from the genome)"] = "bloodtype" } { lines[++n] = $0 } END { diff --git a/tests/parity/oracle.ref b/tests/parity/oracle.ref index 8573fcfb9..28aee2d34 100644 --- a/tests/parity/oracle.ref +++ b/tests/parity/oracle.ref @@ -97,4 +97,4 @@ traits 669a8196cf4c377200f9b4af9c235ef85fb774a1 file:bin/traits_report.py bash . roh-summary 7c1470bc50933f48a1a0dde3c0e0b2b87bc48718 file:scripts/11-roh-analysis.sh sh ./oracle.sh roh summary couple-screen d4d3d4b629fc77a058bbbbd302c75bbfae1a01c2 file:bin/couple_report.py sh -c 'exec sh ./oracle.sh "$0" screen "$@"' couple screen couple-genes d4d3d4b629fc77a058bbbbd302c75bbfae1a01c2 file:bin/couple_report.py sh -c 'exec sh ./oracle.sh "$0" genes "$@"' couple genes -report-render 1ec630690e63796b4410d23ec65768200adb8dc0 file:bin/render_report.py sh ./oracle.sh report render +report-render 65095e975bea01c74ed92a4f1b8c1bd00d05caaa file:bin/render_report.py sh ./oracle.sh report render From 4506b43cb2dd90b6a586bb7947440fd43f547502 Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:41:26 +0100 Subject: [PATCH 04/29] feat(report): write the clinical sections of both reports in Rust og report render now writes callability, the ClinVar screen with its hits, cDNA marks and deep intronic sites, the clinical filter with the ACMG SF tier, CPSR, slivar's compound-het phase, the carrier list with the two-hit genes and the couple screen, one module per section. The AnnotSV block is written by annotsv.rs for the structural-variants section to join with the SV callers' counts. A card line can now be markup the section built (Line::Html), for the findings tables and their Viewer cells. The parity corpus keeps these sections of the Python's reports and drops the chart figures, which og report charts will write. Fifteen changes of the cb-full summary cover the states, the couple screen, more than 50 hits, the CPSR and SF variants and escaping: 564 items in 114 cases agree byte for byte. Bead: D1 Bead: D2 Bead: D11 Bead: D14 --- rust/crates/og-render/src/chapters.rs | 14 +- rust/crates/og-render/src/html.rs | 1 + rust/crates/og-render/src/model.rs | 3 + rust/crates/og-render/src/sections/acmg_sf.rs | 331 +++++++- rust/crates/og-render/src/sections/annotsv.rs | 280 ++++++- .../og-render/src/sections/callability.rs | 122 ++- rust/crates/og-render/src/sections/carrier.rs | 109 ++- rust/crates/og-render/src/sections/cdna.rs | 137 +++- rust/crates/og-render/src/sections/clinvar.rs | 721 +++++++++++++++++- rust/crates/og-render/src/sections/comphet.rs | 179 ++++- rust/crates/og-render/src/sections/couple.rs | 78 +- rust/crates/og-render/src/sections/cpsr.rs | 204 ++++- .../og-render/src/sections/deep_intronic.rs | 55 +- .../crates/og-render/src/sections/two_hits.rs | 73 +- tests/parity/cases.tsv | 15 + tests/parity/gen/report-corpus.sh | 50 +- 16 files changed, 2344 insertions(+), 28 deletions(-) diff --git a/rust/crates/og-render/src/chapters.rs b/rust/crates/og-render/src/chapters.rs index 92ace64e9..e06706e6e 100644 --- a/rust/crates/og-render/src/chapters.rs +++ b/rust/crates/og-render/src/chapters.rs @@ -159,17 +159,17 @@ pub fn writer(sid: &str) -> Option<(TextFn, HtmlFn)> { match sid { "qc" => Some((sections::qc::text, sections::qc::html)), - "callability" => None, + "callability" => Some((sections::callability::text, sections::callability::html)), "variants" => Some((sections::variants::text, sections::variants::html)), - "clinvar" => None, + "clinvar" => Some((sections::clinvar::text, sections::clinvar::html)), - "clinical-filter" => None, + "clinical-filter" => Some((sections::acmg_sf::text, sections::acmg_sf::html)), - "cpsr" => None, + "cpsr" => Some((sections::cpsr::text, sections::cpsr::html)), - "slivar" => None, + "slivar" => Some((sections::comphet::text, sections::comphet::html)), "exomiser" => None, @@ -191,9 +191,9 @@ pub fn writer(sid: &str) -> Option<(TextFn, HtmlFn)> { "mosaic-mca" => None, - "carrier-list" => None, + "carrier-list" => Some((sections::carrier::text, sections::carrier::html)), - "couple" => None, + "couple" => Some((sections::couple::text, sections::couple::html)), "paralogs" => None, diff --git a/rust/crates/og-render/src/html.rs b/rust/crates/og-render/src/html.rs index 43e75f02b..feffb87c3 100644 --- a/rust/crates/og-render/src/html.rs +++ b/rust/crates/og-render/src/html.rs @@ -70,6 +70,7 @@ fn line(out: &mut Vec, l: &Line) { out.extend(items.iter().map(|t| format!("
  • {}
  • ", e(t)))); out.push(" ".to_owned()); } + Line::Html(t) => out.push(t.clone()), } } diff --git a/rust/crates/og-render/src/model.rs b/rust/crates/og-render/src/model.rs index 54ea06364..f12624273 100644 --- a/rust/crates/og-render/src/model.rs +++ b/rust/crates/og-render/src/model.rs @@ -325,6 +325,9 @@ pub enum Line { Para(String), /// A plain bulleted list. List(Vec), + /// A line of markup the section built itself, every text in it already + /// escaped: a findings table, its Viewer links. Written as it is. + Html(String), } /// One card: a title, the caveats shown under it, the notes saying its diff --git a/rust/crates/og-render/src/sections/acmg_sf.rs b/rust/crates/og-render/src/sections/acmg_sf.rs index 70669f6e2..733ebfb6c 100644 --- a/rust/crates/og-render/src/sections/acmg_sf.rs +++ b/rust/crates/og-render/src/sections/acmg_sf.rs @@ -1,2 +1,329 @@ -//! The report section `acmg_sf`. Empty until its unit of the 1.0.0 plan -//! lands. +//! Step 23's clinical filter and the ACMG secondary-findings tier: the +//! counts of the filtered variants, then each SF gene's verdict by its +//! reporting rule (`assets/acmg_sf.tsv`): the ClinVar P/LP hits, the rare +//! HIGH-impact records and the class 4/5 SVs it reports, and the variants +//! it does not report, each with the reason. Every phrase comes from the +//! summary as `og summary collect` wrote it. + +use pycompat::fmt::ljust; + +use crate::escape::e; +use crate::model::{Card, HtmlPart, Inline, Line, Malformed, Summary}; +use crate::notes::{ACMG_NOTE, VIEWER_NOTE}; +use crate::sections::clinvar::{D, VIEWER_TH, viewer_td_of}; +use crate::{html, text}; + +/// One row of the SF table: gene, variant, genotype, evidence. +pub type Row = (String, String, String, String); + +/// The HIGH-impact records counted as rare: neither a weak call nor of +/// unknown frequency (a summary written before those marks counts every +/// record). +pub fn high_rare(sf: &D<'_>) -> Result { + let all = sf.at("high_impact")?.len()?; + match sf.get("high_impact_rare")? { + Some(n) => n.text(), + None => Ok(all.to_string()), + } +} + +/// The HIGH-impact records listed but not counted as rare. +pub fn high_apart(sf: &D<'_>) -> Result { + let mut parts = Vec::new(); + if let Some(n) = sf.opt("high_impact_weak")? { + parts.push(format!("{} weak call(s)", n.text()?)); + } + if let Some(n) = sf.opt("high_impact_frequency_unknown")? { + parts.push(format!("{} of unknown frequency", n.text()?)); + } + Ok(if parts.is_empty() { + String::new() + } else { + format!(" (listed apart: {})", parts.join(", ")) + }) +} + +/// The marks of one HIGH-impact record (unknown frequency, weak call), each +/// after `lead`; `""` for a rare record with a sound call. +pub fn high_marks(x: &D<'_>, lead: &str) -> Result { + let mut marks = Vec::new(); + if x.flag("frequency_unknown")? { + marks.push("frequency unknown, not counted as rare".to_owned()); + } + if x.flag("weak_call")? { + marks.push(format!( + "weak call ({}): check the reads", + x.s_or_none("call_note")? + )); + } + if marks.is_empty() { + return Ok(String::new()); + } + let close = if lead.ends_with('(') { ")" } else { "" }; + Ok(format!("{lead}{}{close}", marks.join("; "))) +} + +/// The severity, rule and two-hit notes of a row. +fn extra(x: &D<'_>) -> Result { + let mut out = String::new(); + for k in ["severity", "rule_note", "two_hit"] { + if let Some(v) = x.opt(k)? { + out += &format!("; {}", v.text()?); + } + } + Ok(out) +} + +/// The secondary findings as rows, the same in both reports: ClinVar hits, +/// rare HIGH-impact records, then class 4 and 5 SVs. +pub fn rows(sf: &D<'_>) -> Result, Malformed> { + let mut out = Vec::new(); + for x in sf.at("clinvar_hits")?.items()? { + let low = if x.flag("low_confidence")? { + "; low-confidence call: check the reads" + } else { + "" + }; + out.push(( + x.s("gene")?, + x.s("variant")?, + x.s("genotype")?, + format!( + "ClinVar {}{low}{}", + x.at("significance")?.string()?, + extra(&x)? + ), + )); + } + for x in sf.at("high_impact")?.items()? { + out.push(( + x.s("gene")?, + x.s("variant")?, + x.s("genotype")?, + format!( + "{}{}{}", + x.at("consequence")?.string()?, + high_marks(&x, "; ")?, + extra(&x)? + ), + )); + } + if let Some(sv) = sf.get("structural")? { + for x in sv.items()? { + let support = x.at("support")?; + let support = if support.is_none() { + "caller support not known (no step 22 consensus)".to_owned() + } else if support.is_num(1) { + format!("single caller ({})", x.s("caller")?) + } else { + format!("{} callers", support.text()?) + }; + let via = match x.opt("via")? { + Some(v) => format!("; through {}", v.text()?), + None => String::new(), + }; + out.push(( + x.s("gene")?, + x.s("variant")?, + ".".to_owned(), + format!( + "AnnotSV class {}, {support}{via}{}", + x.s("acmg_class")?, + extra(&x)? + ), + )); + } + } + Ok(out) +} + +/// The variants in SF genes the gene's rule does not report, or why no +/// rule could be applied. +pub fn apart_lines(sf: &D<'_>) -> Result, Malformed> { + if sf.flag("unavailable")? { + return Ok(vec![format!( + "Secondary findings: not assessed, {}", + sf.s("unavailable")? + )]); + } + let mut out = Vec::new(); + if let Some(nr) = sf.get("not_reported")? { + for x in nr.items()? { + let gt = match x.opt("genotype")? { + Some(g) => g.text()?, + None => ".".to_owned(), + }; + out.push(format!( + "{} {} {gt} ({}): not reported, {}", + x.s("gene")?, + x.s("variant")?, + x.s("source")?, + x.s("reason")? + )); + } + } + if let Some(n) = sf.opt("two_hit_note")? { + out.push(n.text()?); + } + Ok(out) +} + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "clinical", Some("Clinical Variant Filter"))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if !head.ran { + return Ok(w); + } + let d = D::of(&s.section("clinical")); + let genes = if d.has("genes") { + format!(" in {} genes", d.s("genes")?) + } else { + String::new() + }; + w.push(format!(" Clinical variants: {}{genes}", d.s("variants")?)); + if let Some(by) = d.opt("by_impact")? { + let mut pairs = by.pairs()?; + pairs.sort_by(|a, b| a.0.cmp(b.0)); + let parts: Vec = pairs + .iter() + .map(|(k, v)| Ok(format!("{k} {}", v.text()?))) + .collect::>()?; + w.push(format!(" By impact: {}", parts.join(", "))); + } + if d.has("frequency_unknown") { + w.push(format!( + " Frequency unknown (not counted as rare): {}", + d.s("frequency_unknown")? + )); + } + if d.has("weak_calls") { + w.push(format!( + " Weak genotype calls (check the reads): {}", + d.s("weak_calls")? + )); + } + if let Some(sf) = d.opt("acmg_sf")? { + if sf.flag("unavailable")? { + w.push(format!(" {}", apart_lines(&sf)?[0])); + } else { + let mut line = format!( + " Secondary-findings genes ({}, {} genes): {} ClinVar P/LP, {} rare HIGH impact{}", + sf.s("version")?, + sf.s_or("genes_on_list", "?")?, + sf.at("clinvar_hits")?.len()?, + high_rare(&sf)?, + high_apart(&sf)? + ); + if sf.flag("structural_checked")? { + line += &format!(", {} class 4/5 SV(s)", sf.at("structural")?.len()?); + } + if let Some(nr) = sf.opt("not_reported")? { + line += &format!(", {} not reported by the gene's rule", nr.len()?); + } + w.push(line); + for (g, v, z, ev) in rows(&sf)? { + w.push(format!(" {} {v} {z} {ev}", ljust(&g, 8))); + } + for line in apart_lines(&sf)? { + w.push(format!(" {line}")); + } + w.push(format!(" {ACMG_NOTE}")); + } + } + w.push(String::new()); + Ok(w) +} + +/// The counts card, always there; a stat's missing count prints `None`. +fn counts_card(d: &D<'_>) -> Result { + let stat = |label: String, v: String| Line::Stat(label, Inline::Text(v)); + let mut body = vec![stat( + "Total interesting variants".to_owned(), + d.s_or_none("variants")?, + )]; + if d.has("genes") { + body.push(stat("Genes".to_owned(), d.s("genes")?)); + } + if let Some(by) = d.opt("by_impact")? { + let mut pairs = by.pairs()?; + pairs.sort_by(|a, b| a.0.cmp(b.0)); + for (k, v) in pairs { + body.push(stat(format!("{k} impact"), v.text()?)); + } + } + if d.has("frequency_unknown") { + body.push(stat( + "Frequency unknown (not rare)".to_owned(), + d.s("frequency_unknown")?, + )); + } + if d.has("weak_calls") { + body.push(stat("Weak genotype calls".to_owned(), d.s("weak_calls")?)); + } + Ok(Card { + title: "Clinical Variant Filter".to_owned(), + body, + ..Card::default() + }) +} + +/// The SF card, when the tier lists anything or says why it could not. +fn sf_card(sf: &D<'_>) -> Result, Malformed> { + let shown = sf.at("clinvar_hits")?.truthy() + || sf.at("high_impact")?.truthy() + || sf.flag("structural")? + || sf.flag("not_reported")? + || sf.flag("unavailable")?; + if !shown { + return Ok(None); + } + let mut rows_html = Vec::new(); + for (g, v, z, ev) in rows(sf)? { + rows_html.push(format!( + "{}{}{}{}{}", + e(&g), + e(&v), + e(&z), + e(&ev), + viewer_td_of(&v)? + )); + } + let mut body = vec![ + Line::Html(" ".to_owned()), + Line::Html(format!( + " {VIEWER_TH}" + )), + Line::Html(rows_html.join("\n")), + Line::Html("
    GeneVariantGenotypeEvidence
    ".to_owned()), + ]; + body.extend(apart_lines(sf)?.into_iter().map(Line::Para)); + if !rows_html.is_empty() { + body.push(Line::Html(VIEWER_NOTE.to_owned())); + } + Ok(Some(Card { + title: format!("Secondary-Findings Genes ({})", sf.s("version")?), + full: true, + caveats: vec![ACMG_NOTE], + body, + ..Card::default() + })) +} + +/// The HTML report's cards: the counts, then the SF table. +pub fn html(s: &Summary) -> Result { + let sec = s.section("clinical"); + let d = D::of(&sec); + let mut cards = vec![html::card(&sec, counts_card(&d)?)?]; + if let Some(sf) = d.opt("acmg_sf")? + && let Some(card) = sf_card(&sf)? + { + cards.push(html::card(&sec, card)?); + } + Ok(HtmlPart { + cards, + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/annotsv.rs b/rust/crates/og-render/src/sections/annotsv.rs index b9d5acde1..3da0b1e38 100644 --- a/rust/crates/og-render/src/sections/annotsv.rs +++ b/rust/crates/og-render/src/sections/annotsv.rs @@ -1,2 +1,278 @@ -//! The report section `annotsv`. Empty until its unit of the 1.0.0 plan -//! lands. +//! Step 05's AnnotSV class 4 and 5 SVs, with how many callers saw each +//! (step 22's consensus), the exon-edge and benign-set checks, and the +//! calls step 05's rules moved to class 3 with the reason. +//! +//! It is one block of the structural-variants section, beside the SV +//! callers' counts (`sv_callers`): [`text`] and [`html`] write the AnnotSV +//! block and card, and the section's writer puts them after the callers'. + +use pycompat::fmt::{general, ljust}; + +use crate::escape::e; +use crate::model::{Card, Colour, HtmlPart, Inline, Line, Malformed, State, Summary}; +use crate::notes::{ANNOTSV_NOTE, VIEWER_NOTE}; +use crate::sections::clinvar::{D, VIEWER_TH, locus_text, viewer_td_of}; +use crate::{html, text}; + +const TITLE: &str = "Structural Variants, Class 4 and 5 (AnnotSV)"; + +/// How many callers saw the SV. +pub fn support_text(x: &D<'_>) -> Result { + let s = x.at("support")?; + if s.is_none() { + return Ok("not known (no step 22 consensus)".to_owned()); + } + if s.is_num(1) { + return Ok(format!("single caller ({})", x.s("caller")?)); + } + let callers = x.at("support_callers")?.items()?; + let names: Vec<&str> = callers.iter().map(D::string).collect::>()?; + Ok(format!("{} callers ({})", s.text()?, names.join(", "))) +} + +/// The first three genes, and how many more. +pub fn genes_text(x: &D<'_>) -> Result { + let g = x.at("genes")?; + if !g.truthy() { + return Ok("no gene".to_owned()); + } + let items = g.items()?; + let first: Vec<&str> = items + .iter() + .take(3) + .map(D::string) + .collect::>()?; + let mut out = first.join(", "); + if items.len() > 3 { + out += &format!(" and {} more", items.len() - 3); + } + Ok(out) +} + +/// The exon-edge, benign-set and rule flags of one class 4-5 SV. +pub fn flags(x: &D<'_>) -> Result, Malformed> { + let mut out = Vec::new(); + let edge = x.at("exon_edge")?; + if edge.is("yes") { + out.push(format!( + "on exon edges: {}", + x.at("exon_edge_detail")?.string()? + )); + } else if edge.is("not checked") { + out.push("exon edges not checked".to_owned()); + } + let af = x.at("population_af")?; + if !af.is_none() { + let mut f = format!("benign-set AF {}", general(af.num()?, 6)); + let src = x.at("population_source")?; + if src.truthy() { + f += &format!(" ({})", src.text()?); + } + if x.at("common")?.truthy() { + f += ", common"; + } + out.push(f); + } + if let Some(detail) = x.opt("rule_detail")? { + let class = x.s("acmg_class")?; + if class == "4" || class == "5" { + out.push(format!("rule {}: {}", x.s("rule")?, detail.text()?)); + } + } + Ok(out) +} + +/// `v or default`, as text. +fn or_text(x: &D<'_>, key: &str, default: &str) -> Result { + let v = x.at(key)?; + if v.truthy() { + v.text() + } else { + Ok(default.to_owned()) + } +} + +/// One SV step 05's rules moved to class 3: where, from which class, and +/// why. +pub fn moved_text(x: &D<'_>) -> Result { + Ok(format!( + "{} {} {} AnnotSV class {} -> {} ({}: {})", + x.s("type")?, + x.s("sv")?, + genes_text(x)?, + x.s("annotsv_class")?, + x.s("acmg_class")?, + or_text(x, "rule", "rule not named")?, + or_text(x, "rule_detail", "no detail")? + )) +} + +/// The class 4-5 lines, the same in both reports. +pub fn lines(d: &D<'_>) -> Result, Malformed> { + let count = d.at("count")?; + let mut head = format!("Class 4-5 SVs: {}", count.text()?); + if count.truthy() { + let consensus = d.at("consensus")?.truthy(); + let single = if consensus { + d.s("single_caller")? + } else { + "not known".to_owned() + }; + let edge = if d.at("exon_edge_checked")?.truthy() { + d.s("exon_edge")? + } else { + "not checked".to_owned() + }; + head += &format!( + " (single caller: {single}; deletions on exon edges: {edge}; common: {})", + d.s("common")? + ); + } + let mut out = vec![head]; + if let Some(down) = d.opt("downgraded")? { + out.push(format!( + "Moved to class 3 by step 05's rules, not counted above: {}", + down.len()? + )); + } + if count.truthy() && !d.at("consensus")?.truthy() { + out.push(match d.opt("consensus_note")? { + Some(n) => n.text()?, + None => "Caller support: step 22's consensus did not run, so how many callers saw each SV is \ + not known." + .to_owned(), + }); + } + Ok(out) +} + +/// The AnnotSV block of the text report. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "annotsv", Some(TITLE))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if !head.ran { + return Ok(w); + } + let d = D::of(&s.section("annotsv")); + w.extend(lines(&d)?.iter().map(|l| format!(" {l}"))); + for x in d.at("rows")?.items()? { + w.push(format!( + " {} {} class {} {} support: {}", + ljust(&x.s("type")?, 4), + x.s("sv")?, + x.s("acmg_class")?, + genes_text(&x)?, + support_text(&x)? + )); + for f in flags(&x)? { + w.push(format!(" {f}")); + } + } + let down = d.opt("downgraded")?; + if let Some(down) = &down { + w.push( + " Moved to class 3 by step 05's rules (from AnnotSV's class, with the reason):" + .to_owned(), + ); + for x in down.items()? { + w.push(format!(" {}", moved_text(&x)?)); + } + } + if d.at("count")?.truthy() || down.is_some() { + w.push(format!(" {ANNOTSV_NOTE}")); + } + w.push(String::new()); + Ok(w) +} + +/// `type sv` of a row, as the tables show it. +fn sv_cell(x: &D<'_>) -> Result { + Ok(format!( + "{} {}", + x.at("type")?.string()?, + x.at("sv")?.string()? + )) +} + +/// The AnnotSV card of the HTML report, when the step ran or failed. +pub fn html(s: &Summary) -> Result { + let sec = s.section("annotsv"); + if sec.state == State::Missing { + return Ok(HtmlPart::default()); + } + let d = D::of(&sec); + let done = if sec.state == State::Stale { + Inline::Badge("Stale".to_owned(), Colour::Yellow) + } else { + Inline::Badge("Complete".to_owned(), Colour::Green) + }; + let mut body = vec![Line::Stat("Status".to_owned(), done)]; + if d.truthy() { + body.extend(lines(&d)?.into_iter().map(Line::Note)); + } + let rows = d.opt("rows")?; + if let Some(rows) = &rows { + let mut t = format!( + " {VIEWER_TH}" + ); + for x in rows.items()? { + let checks = flags(&x)?.join("; "); + t += &format!( + "{}", + e(&sv_cell(&x)?), + e(&x.s("acmg_class")?), + e(&genes_text(&x)?), + e(&support_text(&x)?), + e(if checks.is_empty() { "none" } else { &checks }), + viewer_td_of(&locus_text(&x.at("sv")?)?)? + ); + } + t += "
    SVClassGenesCallersChecks
    {}{}{}{}{}
    "; + body.push(Line::Html(t)); + } + let down = d.opt("downgraded")?; + if let Some(down) = &down { + body.push(Line::Html( + "

    Moved to class 3 by step 05's rules

    ".to_owned(), + )); + let mut t = format!( + " \ + {VIEWER_TH}" + ); + for x in down.items()? { + t += &format!( + "{}", + e(&sv_cell(&x)?), + e(&x.s("annotsv_class")?), + e(&x.s("acmg_class")?), + e(&genes_text(&x)?), + e(&or_text(&x, "rule", "not named")?), + e(&or_text(&x, "rule_detail", "no detail")?), + viewer_td_of(&locus_text(&x.at("sv")?)?)? + ); + } + t += "
    SVAnnotSV classClassGenesRuleWhy
    {}{}{}{}{}{}
    "; + body.push(Line::Html(t)); + } + let mut caveats = Vec::new(); + if rows.is_some() || down.is_some() { + body.push(Line::Html(VIEWER_NOTE.to_owned())); + caveats.push(ANNOTSV_NOTE); + } + let card = html::card( + &sec, + Card { + title: TITLE.to_owned(), + full: true, + caveats, + body, + ..Card::default() + }, + )?; + Ok(HtmlPart { + cards: vec![card], + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/callability.rs b/rust/crates/og-render/src/sections/callability.rs index b91918804..41208ca03 100644 --- a/rust/crates/og-render/src/sections/callability.rs +++ b/rust/crates/og-render/src/sections/callability.rs @@ -1,2 +1,120 @@ -//! The report section `callability`. Empty until its unit of the 1.0.0 plan -//! lands. +//! Step 16b's gene callability: how many of the hard-to-read genes of +//! `assets/callability_genes.tsv` the reads covered, and which were not +//! read, apart from the ones a step of their own calls. + +use pycompat::fmt::fixed; + +use crate::model::{Card, Colour, HtmlPart, Inline, Line, Malformed, State, Summary}; +use crate::notes::CALLABILITY_NOTE; +use crate::sections::clinvar::D; +use crate::{html, text}; + +const TITLE: &str = "Genes This Genome Could Not Read Reliably"; + +/// `GENE (37%)`, or `GENE (not on this reference)`. +fn gene(g: &D<'_>) -> Result { + if g.at("status")?.is("not on this reference") { + return Ok(format!("{} (not on this reference)", g.s("gene")?)); + } + Ok(format!( + "{} ({}%)", + g.s("gene")?, + fixed(100.0 * g.at("share")?.num()?, 0) + )) +} + +/// The lines of the text block and the card. +pub fn lines(d: &D<'_>) -> Result, Malformed> { + let mut out = vec![format!( + "{} of the {} hard-to-read genes of assets/callability_genes.tsv were read: {}% or more of their \ + bases had {} or more reads of mapping quality {} or more.", + d.s("read")?, + d.s("catalog_genes")?, + fixed(100.0 * d.at("min_share")?.num()?, 0), + d.s("min_depth")?, + d.s("min_mapq")? + )]; + let nr = d.at("not_read")?.items()?; + let owned = d.at("not_read_owned")?.items()?; + let listed = if nr.is_empty() { + "none".to_owned() + } else { + nr.iter() + .map(gene) + .collect::, _>>()? + .join(", ") + }; + out.push(format!( + "Not read, and no other step calls them ({}): {listed}", + nr.len() + )); + if !owned.is_empty() { + let mut parts = Vec::new(); + for g in &owned { + parts.push(format!("{} by {}", gene(g)?, g.s("owner")?)); + } + out.push(format!( + "Not read here, called by a step of their own ({}): {}", + owned.len(), + parts.join(", ") + )); + } + Ok(out) +} + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "callability", Some(TITLE))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if head.ran { + let d = D::of(&s.section("callability")); + w.extend(lines(&d)?.iter().map(|l| format!(" {l}"))); + w.push(format!(" {CALLABILITY_NOTE}")); + w.push(String::new()); + } + Ok(w) +} + +/// The HTML report's card, when the step ran or failed. +pub fn html(s: &Summary) -> Result { + let sec = s.section("callability"); + if sec.state == State::Missing { + return Ok(HtmlPart::default()); + } + let d = D::of(&sec); + let mut body = Vec::new(); + if sec.state.ran() { + let not_read = d.at("not_read")?.len()?; + body.push(Line::Stat( + "Genes read".to_owned(), + Inline::Text(format!("{} of {}", d.s("read")?, d.s("catalog_genes")?)), + )); + body.push(Line::Stat( + "Not read, no other caller".to_owned(), + Inline::Badge( + not_read.to_string(), + if not_read > 0 { + Colour::Red + } else { + Colour::Green + }, + ), + )); + body.extend(lines(&d)?.into_iter().map(Line::Note)); + } + let card = html::card( + &sec, + Card { + title: TITLE.to_owned(), + caveats: vec![CALLABILITY_NOTE], + body, + ..Card::default() + }, + )?; + Ok(HtmlPart { + cards: vec![card], + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/carrier.rs b/rust/crates/og-render/src/sections/carrier.rs index 98e47f503..ffaf982a4 100644 --- a/rust/crates/og-render/src/sections/carrier.rs +++ b/rust/crates/og-render/src/sections/carrier.rs @@ -1,2 +1,107 @@ -//! The report section `carrier`. Empty until its unit of the 1.0.0 plan -//! lands. +//! The sample's own carrier list (no partner): the ClinVar P/LP hits in +//! the genes of `assets/carrier_genes.tsv`, the hypomorph and +//! pseudodeficiency alleles set aside, and the two-hit genes +//! ([`super::two_hits`]). + +use crate::model::{Card, HtmlPart, Line, Malformed, State, Summary}; +use crate::notes::CARRIER_NOTE; +use crate::sections::clinvar::D; +use crate::sections::two_hits; +use crate::{html, text}; + +const TITLE: &str = "Carrier List (this sample)"; + +fn hemi(x: &D<'_>) -> Result { + Ok(match x.opt("hemizygous")? { + Some(h) => format!(" ({})", h.text()?), + None => String::new(), + }) +} + +/// The list's lines, the same in both reports. +pub fn lines(c: &D<'_>) -> Result, Malformed> { + if c.flag("unavailable")? { + return Ok(vec![format!( + "Carrier list: not made, {}", + c.s("unavailable")? + )]); + } + let carriers = c.at("carriers")?.items()?; + let set_aside = c.at("set_aside")?; + let mut head = format!( + "Carrier list ({} genes): {} finding(s)", + c.s("genes_on_list")?, + carriers.len() + ); + if set_aside.truthy() { + head += &format!(", {} set aside", set_aside.len()?); + } + let mut out = vec![head]; + for x in &carriers { + out.push(format!( + "{} ({}; {}): {} {}{}, ClinVar {}", + x.s("gene")?, + x.s("condition")?, + x.s("inheritance")?, + x.s("variant")?, + x.s("genotype")?, + hemi(x)?, + x.s("significance")? + )); + } + for x in set_aside.items()? { + out.push(format!( + "Set aside: {} {} {}{}: {} is a {} allele, not a carrier finding on its own", + x.s("gene")?, + x.s("variant")?, + x.s("genotype")?, + hemi(&x)?, + x.s("name")?, + x.s("kind")? + )); + } + out.extend(two_hits::lines(c.get("two_hits")?.as_ref())?); + Ok(out) +} + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "carrier", Some(TITLE))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if head.ran { + let c = D::of(&s.section("carrier")); + w.extend(lines(&c)?.iter().map(|l| format!(" {l}"))); + w.push(format!(" {CARRIER_NOTE}")); + w.push(String::new()); + } + Ok(w) +} + +/// The HTML report's card, when the list was made or its step failed. +pub fn html(s: &Summary) -> Result { + let sec = s.section("carrier"); + if sec.state == State::Missing { + return Ok(HtmlPart::default()); + } + let body = if sec.state.ran() { + lines(&D::of(&sec))?.into_iter().map(Line::Para).collect() + } else { + Vec::new() + }; + let card = html::card( + &sec, + Card { + title: TITLE.to_owned(), + full: true, + caveats: vec![CARRIER_NOTE], + body, + ..Card::default() + }, + )?; + Ok(HtmlPart { + cards: vec![card], + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/cdna.rs b/rust/crates/og-render/src/sections/cdna.rs index bd3715784..c6777b999 100644 --- a/rust/crates/og-render/src/sections/cdna.rs +++ b/rust/crates/og-render/src/sections/cdna.rs @@ -1 +1,136 @@ -//! The report section `cdna`. Empty until its unit of the 1.0.0 plan lands. +//! Step 06's cDNA marks, shown in the ClinVar block of both reports: the +//! read check of each hit (alt reads clipped at an exon edge), the hits at a +//! known lab-construct site, and the scan of every exon for reads that jump +//! from one exon to the next. + +use crate::escape::e; +use crate::model::{Line, Malformed}; +use crate::sections::clinvar::{D, VIEWER_TH, viewer_td}; + +/// Step 06's read check of one ClinVar (or CPSR) hit. A hit at a known +/// construct site always shows its read counts when they were measured. +pub fn cdna_text(h: &D<'_>) -> Result { + let c = h.at("cdna")?; + let counts = format!( + "{} of {} alt reads and {} of {} ref reads clipped at an exon edge", + c.s("alt_clipped")?, + c.s("alt_reads")?, + c.s("ref_clipped")?, + c.s("ref_reads")? + ); + let site = match c.opt("construct")? { + Some(x) => format!("known lab-construct site ({}): ", x.text()?), + None => String::new(), + }; + let check = c.at("check")?; + if check.is("flagged") { + return Ok(format!("{site}{} ({counts})", c.s("note")?)); + } + if check.is("clear") { + return Ok(if site.is_empty() { + "clear".to_owned() + } else { + format!("{site}clear ({counts})") + }); + } + let note = c.at("note")?; + let mut out = format!("{site}not checked"); + if note.truthy() { + out += &format!(": {}", note.text()?); + } + let measured = c.get("alt_reads")?.is_some_and(|x| !x.is_none()); + if !site.is_empty() && measured { + out += &format!(" ({counts})"); + } + Ok(out) +} + +/// The hits at a known lab-construct site below the first `shown`: both +/// reports print them too, so every hit the overview counts has a row. +pub fn construct_past_cap<'a>(hits: &[D<'a>], shown: usize) -> Result>, Malformed> { + let mut out = Vec::new(); + for h in hits.iter().skip(shown) { + let site = match h.opt("cdna")? { + Some(c) => c.flag("construct")?, + None => false, + }; + if site { + out.push(h.clone()); + } + } + Ok(out) +} + +/// One such hit as a line. +pub fn construct_past_cap_text(h: &D<'_>) -> Result { + Ok(format!( + "{} {}:{} {}>{} {}: {}", + h.s("gene")?, + h.s("chrom")?, + h.s("pos")?, + h.s("ref")?, + h.s("alt")?, + h.s("genotype")?, + cdna_text(h)? + )) +} + +/// The scan's lines, the same in both reports; none when it did not run. +pub fn scan_lines(d: &D<'_>) -> Result, Malformed> { + let Some(scan) = d.get("cdna_scan")?.filter(|x| !x.is_none()) else { + return Ok(Vec::new()); + }; + let loci = scan.at("loci")?; + let note = scan.at("note")?; + let note_text = note.string()?; + if note_text.starts_with("not run") { + return Ok(vec![format!("cDNA scan of every exon: {note_text}")]); + } + // The note says what was read (exon regions, contigs): a BAM short of + // contigs is no whole-exome negative. + let loci = loci.items()?; + let mut first = format!( + "cDNA scan of every exon: {} loci with reads across two or more exon junctions", + loci.len() + ); + if note.truthy() { + first += &format!(" ({note_text})"); + } + let mut out = vec![first]; + for x in loci.iter().take(20) { + out.push(format!( + " {}:{}-{}: {} junctions, {} reads", + x.s("chrom")?, + x.s("start")?, + x.s("end")?, + x.s("junctions")?, + x.s("reads")? + )); + } + Ok(out) +} + +/// The HTML table of the scan's first 20 loci, with the Viewer note. +pub fn scan_table(scan: &D<'_>) -> Result, Malformed> { + let mut out = vec![ + Line::Html(" ".to_owned()), + Line::Html(format!( + " {VIEWER_TH}" + )), + ]; + for x in scan.at("loci")?.items()?.iter().take(20) { + let (chrom, start, end) = (x.at("chrom")?, x.at("start")?, x.at("end")?); + out.push(Line::Html(format!( + " {}", + e(chrom.string()?), + e(&start.text()?), + e(&end.text()?), + e(&x.s("junctions")?), + e(&x.s("reads")?), + viewer_td(chrom.v, start.v, Some(end.v))? + ))); + } + out.push(Line::Html("
    LocusJunctionsReads
    {}:{}-{}{}{}
    ".to_owned())); + out.push(Line::Html(crate::notes::VIEWER_NOTE.to_owned())); + Ok(out) +} diff --git a/rust/crates/og-render/src/sections/clinvar.rs b/rust/crates/og-render/src/sections/clinvar.rs index 9876ec648..abe84c00e 100644 --- a/rust/crates/og-render/src/sections/clinvar.rs +++ b/rust/crates/og-render/src/sections/clinvar.rs @@ -1,2 +1,719 @@ -//! The report section `clinvar`. Empty until its unit of the 1.0.0 plan -//! lands. +//! Step 06's ClinVar screen: the count of P/LP hits with the call floor and +//! the gnomAD frequency, the hits best-reviewed first (up to 50, then any +//! hit at a known lab-construct site), the cDNA read check and the cDNA +//! scan ([`super::cdna`]) and the known deep intronic sites +//! ([`super::deep_intronic`]). +//! +//! It also holds what the clinical sections share: [`D`], a dict of +//! summary.json read with Python's rules, and the Viewer cell of a findings +//! table. + +use pycompat::fmt::{general, ljust, percent}; +use pycompat::json::Value; + +use crate::escape::e; +use crate::model::{ + Card, Colour, HtmlPart, Inline, Line, Malformed, Section, Summary, contains, num, py, py_eq, + truthy, +}; +use crate::notes::{CDNA_SCAN_NOTE, CLINVAR_FREQ_NOTE, DEEP_INTRONIC_NOTE, VIEWER_NOTE}; +use crate::sections::{cdna, deep_intronic}; +use crate::{html, text}; + +/// The hits the report lists, and the anchor of their table (the charts +/// link a listed hit there). +pub const HITS_SHOWN: usize = 50; +pub const HITS_ANCHOR: &str = "clinvar-hits"; + +/// The genome viewer every Viewer cell opens (`scripts/owngenome-view.sh`). +pub const VIEWER_URL: &str = "http://127.0.0.1:8765/"; +/// The Viewer column's header cell. +pub const VIEWER_TH: &str = "Viewer"; + +// --- a dict of summary.json, read with Python's rules --------------------------------- + +/// A value of summary.json and the path that names it in an error. +#[derive(Debug, Clone)] +pub struct D<'a> { + pub v: &'a Value, + pub path: String, +} + +impl<'a> D<'a> { + pub fn new(v: &'a Value, path: impl Into) -> D<'a> { + D { + v, + path: path.into(), + } + } + + /// The data of section `sec`. + pub fn of(sec: &Section<'a>) -> D<'a> { + D::new(sec.data, format!("sections.{}.data", sec.key)) + } + + fn child(&self, v: &'a Value, key: &str) -> D<'a> { + D::new(v, format!("{}.{key}", self.path)) + } + + /// `d[key]`. + pub fn at(&self, key: &str) -> Result, Malformed> { + crate::model::need(self.v, key, &self.path).map(|v| self.child(v, key)) + } + + /// `d.get(key)`: `None` for a missing key. + pub fn get(&self, key: &str) -> Result>, Malformed> { + Ok(crate::model::get(self.v, key, &self.path)?.map(|v| self.child(v, key))) + } + + /// `d.get(key)` when it is true, else `None`. + pub fn opt(&self, key: &str) -> Result>, Malformed> { + Ok(self.get(key)?.filter(|x| truthy(x.v))) + } + + /// `bool(d.get(key))`. + pub fn flag(&self, key: &str) -> Result { + Ok(self.opt(key)?.is_some()) + } + + /// `key in d`. + pub fn has(&self, key: &str) -> bool { + contains(self.v, key) + } + + /// `str(d[key])`. + pub fn s(&self, key: &str) -> Result { + self.at(key)?.text() + } + + /// `str(d.get(key))`: `None` for a missing key. + pub fn s_or_none(&self, key: &str) -> Result { + self.get(key)?.map_or(Ok("None".to_owned()), |x| x.text()) + } + + /// `str(d.get(key, default))`. + pub fn s_or(&self, key: &str, default: &str) -> Result { + self.get(key)?.map_or(Ok(default.to_owned()), |x| x.text()) + } + + /// `str(v)`. + pub fn text(&self) -> Result { + py(self.v) + } + + /// `v` where Python needs a string (`html.escape(v)`, `s + v`). + pub fn string(&self) -> Result<&'a str, Malformed> { + match self.v { + Value::Str(s) => Ok(s), + _ => Err(Malformed(format!("{} is not a string", self.path))), + } + } + + /// `bool(v)`. + pub fn truthy(&self) -> bool { + truthy(self.v) + } + + /// `v is None`. + pub fn is_none(&self) -> bool { + matches!(self.v, Value::Null) + } + + /// `v == text`. + pub fn is(&self, text: &str) -> bool { + matches!(self.v, Value::Str(s) if s == text) + } + + /// The items of a list. + pub fn items(&self) -> Result>, Malformed> { + match self.v { + Value::Array(a) => Ok(a + .iter() + .enumerate() + .map(|(i, v)| D::new(v, format!("{}[{i}]", self.path))) + .collect()), + _ => Err(Malformed(format!("{} is not a list", self.path))), + } + } + + /// The items of a dict, in its order. + pub fn pairs(&self) -> Result)>, Malformed> { + match self.v { + Value::Object(o) => Ok(o + .iter() + .map(|(k, v)| (k.as_str(), self.child(v, k))) + .collect()), + _ => Err(Malformed(format!("{} is not a dict", self.path))), + } + } + + /// `len(v)` of a list, dict or string. + pub fn len(&self) -> Result { + match self.v { + Value::Array(a) => Ok(a.len()), + Value::Object(o) => Ok(o.len()), + Value::Str(s) => Ok(s.chars().count()), + _ => Err(Malformed(format!("{} has no length", self.path))), + } + } + + /// `not v` of a list, dict or string. + pub fn is_empty(&self) -> Result { + Ok(self.len()? == 0) + } + + /// The number `v` is. + pub fn num(&self) -> Result { + num(Some(self.v)).ok_or_else(|| Malformed(format!("{} is not a number", self.path))) + } + + /// `v == n` for a number `n`. + pub fn is_num(&self, n: i128) -> bool { + py_eq(self.v, &Value::Int(n)) + } +} + +/// `d.get(key) or []` as a list. +pub fn list_or_empty<'a>(d: &D<'a>, key: &str) -> Result>, Malformed> { + d.opt(key)?.map_or(Ok(Vec::new()), |x| x.items()) +} + +/// `f"{v:, width: usize) -> Result { + let t = match v.v { + Value::Str(s) => s.clone(), + Value::Bool(b) => u8::from(*b).to_string(), + Value::Int(_) | Value::Float(_) => v.text()?, + _ => return Err(Malformed(format!("{} cannot be padded", v.path))), + }; + Ok(ljust(&t, width)) +} + +/// `html.escape(v)` of a value that must be a string. +pub fn ev(v: &D<'_>) -> Result { + Ok(e(v.string()?).into_owned()) +} + +// --- the Viewer column ---------------------------------------------------------------- + +/// The Viewer cell of a row: a link that opens `chrom:start`, or +/// `chrom:start-end`, in the genome viewer; an empty cell without a +/// position. +pub fn viewer_td(chrom: &Value, start: &Value, end: Option<&Value>) -> Result { + if !truthy(chrom) || !truthy(start) { + return Ok("".to_owned()); + } + let mut at = format!("{}:{}", py(chrom)?, py(start)?); + if let Some(end) = end.filter(|v| truthy(v)) { + let end = py(end)?; + if end != py(start)? { + at += &format!("-{end}"); + } + } + let at = e(&at); + Ok(format!( + "
    {at}" + )) +} + +fn name_char(c: u8) -> bool { + c.is_ascii_alphanumeric() || c == b'_' || c == b'.' +} + +/// Every `name:start` or `name:start-end` in `text`, left to right, as the +/// Python's `([A-Za-z0-9_.]+):([0-9]+)(?:-([0-9]+))?(?![0-9])` finds them. +pub fn loci_in(text: &str) -> Vec<(&str, &str, Option<&str>)> { + let b = text.as_bytes(); + let digits = |from: usize| { + let mut j = from; + while j < b.len() && b[j].is_ascii_digit() { + j += 1; + } + j + }; + let mut out = Vec::new(); + let mut i = 0; + while i < b.len() { + if !name_char(b[i]) { + i += 1; + continue; + } + let mut j = i; + while j < b.len() && name_char(b[j]) { + j += 1; + } + // Every start inside the run ends the name where the run ends. + if j < b.len() && b[j] == b':' && j + 1 < b.len() && b[j + 1].is_ascii_digit() { + let k = digits(j + 1); + let mut end = None; + let mut next = k; + if k + 1 < b.len() && b[k] == b'-' && b[k + 1].is_ascii_digit() { + let m = digits(k + 1); + end = Some(&text[k + 1..m]); + next = m; + } + out.push((&text[i..j], &text[j + 1..k], end)); + i = next; + } else { + i = j.max(i + 1); + } + } + out +} + +/// The Viewer cell of a row whose locus is written in its text: the span of +/// every position on the first locus's chromosome. +pub fn viewer_td_of(text: &str) -> Result { + let found = loci_in(text); + let Some((chrom, _, _)) = found.first().copied() else { + return Ok("".to_owned()); + }; + let pos: Vec = found + .iter() + .filter(|(c, _, _)| *c == chrom) + .flat_map(|(_, a, b)| [Some(*a), *b]) + .flatten() + .map(|x| x.parse::().unwrap_or(i128::MAX)) + .collect(); + let lo = pos.iter().min().copied().unwrap_or_default(); + let hi = pos.iter().max().copied().unwrap_or_default(); + viewer_td( + &Value::Str(chrom.to_owned()), + &Value::Int(lo), + Some(&Value::Int(hi)), + ) +} + +/// `str(text or "")` for [`viewer_td_of`]. +pub fn locus_text(v: &D<'_>) -> Result { + if v.truthy() { + v.text() + } else { + Ok(String::new()) + } +} + +// --- the hits ------------------------------------------------------------------------- + +/// A hit's combined-allele note (step 06), `""` when it has none. +pub fn combined_cell(h: &D<'_>) -> Result { + match h.get("combined_allele")? { + None => Ok(String::new()), + Some(n) if n.is(".") || n.is("") => Ok(String::new()), + Some(n) => n.text(), + } +} + +/// A hit's gnomAD allele frequency, `""` for a summary written before the +/// field. +pub fn af_text(h: &D<'_>) -> Result { + let Some(af) = h.get("af")? else { + return Ok(String::new()); + }; + if af.is_none() { + return Ok("gnomAD AF unknown".to_owned()); + } + let mut t = format!("gnomAD AF {}", general(af.num()?, 3)); + if h.flag("common")? { + t += "; common: unlikely to be a high-penetrance finding"; + } + Ok(t) +} + +/// The low-confidence mark of a ClinVar or CPSR row, `""` for a confident +/// one. +pub fn call_text(h: &D<'_>) -> Result { + if !h.flag("low_confidence")? { + return Ok(String::new()); + } + let mut vals = Vec::new(); + for (k, key) in [("GQ", "gq"), ("DP", "dp")] { + if let Some(v) = h.get(key)?.filter(|v| !v.is_none()) { + vals.push(format!("{k} {}", v.text()?)); + } + } + Ok(format!( + "low-confidence call ({}): check the reads", + vals.join(", ") + )) +} + +/// How many hits fall below step 06's call floor, or `None` for a summary +/// written before the floor. +pub fn floor_line(d: &D<'_>) -> Result, Malformed> { + if !d.has("low_confidence") { + return Ok(None); + } + let (gq, dp) = floor(d)?; + Ok(Some(format!( + "Low-confidence calls (GQ below {gq} or DP below {dp}), listed but not counted above: {}", + d.s("low_confidence")? + ))) +} + +/// The call floor's GQ and DP, `None` when the summary has none. +fn floor(d: &D<'_>) -> Result<(String, String), Malformed> { + match d.opt("call_floor")? { + Some(f) => Ok((f.s_or_none("GQ")?, f.s_or_none("DP")?)), + None => Ok(("None".to_owned(), "None".to_owned())), + } +} + +/// Where the hits' gnomAD frequencies came from, or why there are none. +pub fn freq_line(d: &D<'_>) -> Result, Malformed> { + let Some(fr) = d.opt("frequency")? else { + return Ok(None); + }; + if let Some(note) = fr.opt("note")? { + return Ok(Some(format!( + "gnomAD AF: unknown for every hit ({})", + note.text()? + ))); + } + let mut common = 0; + if let Some(hits) = d.get("hits")? { + for h in hits.items()? { + if h.flag("common")? { + common += 1; + } + } + } + Ok(Some(format!( + "gnomAD AF from {}: {common} hit(s) above {}", + fr.s("source")?, + percent(fr.at("common_af")?.num()?, 0) + ))) +} + +// --- the text report ------------------------------------------------------------------ + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "clinvar", Some("ClinVar Pathogenic Screen"))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if !head.ran { + return Ok(w); + } + let d = D::of(&s.section("clinvar")); + w.push(format!( + " Pathogenic/Likely Pathogenic hits: {}", + d.s("count")? + )); + if let Some(low) = floor_line(&d)? { + w.push(format!(" {low}")); + } + w.push(format!(" ClinVar file date: {}", clinvar_release(s)?)); + let hits = d.at("hits")?; + if hits.truthy() { + let hits = hits.items()?; + let by = d.at("by_stars")?; + let mut parts = Vec::new(); + for k in ["4", "3", "2", "1", "0"] { + let plural = if k == "1" { "" } else { "s" }; + parts.push(format!("{k} star{plural} {}", by.s(k)?)); + } + w.push(format!(" By review status: {}", parts.join(", "))); + if d.has("cdna_flagged") { + w.push(format!( + " cDNA read check (alt reads clipped at exon edges): {} flagged", + d.s("cdna_flagged")? + )); + } + if let Some(freq) = freq_line(&d)? { + w.push(format!(" {freq}")); + } + w.push(String::new()); + w.push( + " Hits (best-reviewed first, up to 50): gene, position, genotype, significance [review status] stars" + .to_owned(), + ); + for h in hits.iter().take(HITS_SHOWN) { + let note = combined_cell(h)?; + let mut line = format!( + " {} {}:{} {}>{} {} {} [{}] {}*", + lj(&h.at("gene")?, 10)?, + h.s("chrom")?, + h.s("pos")?, + h.s("ref")?, + h.s("alt")?, + h.s("genotype")?, + h.s("significance")?, + h.s("review_status")?, + h.s("stars")? + ); + if !note.is_empty() { + line += &format!(" ({note})"); + } + w.push(line); + let af = af_text(h)?; + if !af.is_empty() { + w.push(format!(" frequency: {af}")); + } + let call = call_text(h)?; + if !call.is_empty() { + w.push(format!(" call: {call}")); + } + if h.has("cdna") { + w.push(format!(" reads: {}", cdna::cdna_text(h)?)); + } + } + let extra = cdna::construct_past_cap(&hits, HITS_SHOWN)?; + if !extra.is_empty() { + w.push(" Hits at a known lab-construct site past the first 50:".to_owned()); + for h in &extra { + w.push(format!(" {}", cdna::construct_past_cap_text(h)?)); + } + } + } + for line in cdna::scan_lines(&d)? { + w.push(format!(" {line}")); + } + if let Some(scan) = d.opt("cdna_scan")? + && scan.at("loci")?.truthy() + { + w.push(format!(" {CDNA_SCAN_NOTE}")); + } + if d.has("deep_intronic") { + w.push(" Known deep intronic sites (always reported): gene, variant, call".to_owned()); + for x in d.at("deep_intronic")?.items()? { + w.push(format!( + " {} {} {}:{} {}>{} {}", + lj(&x.at("gene")?, 10)?, + x.s("hgvs")?, + x.s("chrom")?, + x.s("pos")?, + x.s("ref")?, + x.s("alt")?, + deep_intronic::call(&x)? + )); + } + w.push(format!(" {DEEP_INTRONIC_NOTE}")); + } + w.push(format!(" {CLINVAR_FREQ_NOTE}")); + w.push(String::new()); + Ok(w) +} + +/// `s['databases'][key] or 'unknown'`. +pub fn database(s: &Summary, key: &str) -> Result { + let v = crate::model::need(s.need("databases")?, key, "summary.databases")?; + if truthy(v) { + py(v) + } else { + Ok("unknown".to_owned()) + } +} + +fn clinvar_release(s: &Summary) -> Result { + database(s, "clinvar_release") +} + +// --- the HTML report ------------------------------------------------------------------ + +fn badge(n: &D<'_>, on: Colour) -> Result { + let colour = if n.truthy() { on } else { Colour::Gray }; + Ok(Inline::Badge(n.text()?, colour)) +} + +/// The screen's card: the counts, never green (no hit is not an +/// all-clear). +fn screen_card(s: &Summary, d: &D<'_>) -> Result { + let count = d.get("count")?; + let n = count.clone().map_or(Ok("0".to_owned()), |x| x.text())?; + let n_on = count.as_ref().is_some_and(D::truthy); + let mut stars = Vec::new(); + let by = d.get("by_stars")?; + for k in ["4", "3", "2", "1", "0"] { + stars.push(match &by { + Some(by) => by.s_or(k, "0")?, + None => "0".to_owned(), + }); + } + let mut body = vec![ + Line::Stat( + "ClinVar matches".to_owned(), + Inline::Badge(n, if n_on { Colour::Yellow } else { Colour::Gray }), + ), + Line::Stat( + "By stars (4/3/2/1/0)".to_owned(), + Inline::Text(stars.join(" / ")), + ), + Line::Stat( + "ClinVar file date".to_owned(), + Inline::Text(clinvar_release(s)?), + ), + ]; + if d.has("cdna_flagged") { + body.insert( + 2, + Line::Stat( + "Flagged by the cDNA read check".to_owned(), + badge(&d.at("cdna_flagged")?, Colour::Red)?, + ), + ); + } + if let Some(sites) = d.opt("construct_sites")? { + body.insert( + 2, + Line::Stat( + "Hits at a known lab-construct site (check the reads)".to_owned(), + Inline::Badge(sites.text()?, Colour::Yellow), + ), + ); + } + if let Some(scan) = d.opt("cdna_scan")? + && !scan.at("note")?.string()?.starts_with("not run") + { + let nl = scan.at("loci")?.len()?; + body.insert( + 2, + Line::Stat( + "Loci with spliced reads (cDNA scan)".to_owned(), + Inline::Badge( + nl.to_string(), + if nl > 0 { Colour::Red } else { Colour::Gray }, + ), + ), + ); + } + if d.has("low_confidence") { + let (gq, dp) = floor(d)?; + body.insert( + 1, + Line::Stat( + format!("Low-confidence calls (GQ below {gq} or DP below {dp}), not counted"), + badge(&d.at("low_confidence")?, Colour::Yellow)?, + ), + ); + } + if let Some(freq) = freq_line(d)? { + body.push(Line::Note(freq)); + } + Ok(Card { + title: "ClinVar Screening".to_owned(), + caveats: vec![CLINVAR_FREQ_NOTE], + body, + ..Card::default() + }) +} + +/// The table of the first 50 hits, with the columns the summary carries. +fn hits_card(d: &D<'_>) -> Result { + let reads = d.has("cdna_flagged"); + let freq = d.has("frequency"); + let call = d.has("low_confidence"); + let hits = d.at("hits")?.items()?; + let nh = hits.len(); + let mut head = " ChrPositionRefAltGenotypeGene\ + SignificanceReview statusStarsCombined allele" + .to_owned(); + if freq { + head += "gnomAD frequency"; + } + if call { + head += "Call quality"; + } + if reads { + head += "cDNA read check"; + } + head += VIEWER_TH; + head += ""; + let mut body = vec![ + Line::Html(format!(" ")), + Line::Html(" ".to_owned()), + Line::Html(head), + ]; + for h in hits.iter().take(HITS_SHOWN) { + let mut row = format!( + "\ + ", + ev(&h.at("chrom")?)?, + e(&h.s("pos")?), + ev(&h.at("ref")?)?, + ev(&h.at("alt")?)?, + ev(&h.at("genotype")?)?, + ev(&h.at("gene")?)?, + ev(&h.at("significance")?)?, + ev(&h.at("review_status")?)?, + e(&h.s("stars")?), + e(&combined_cell(h)?) + ); + if freq { + row += &format!("", e(&af_text(h)?)); + } + if call { + row += &format!("", e(&call_text(h)?)); + } + if reads { + row += &format!("", e(&cdna::cdna_text(h)?)); + } + row += &viewer_td(h.at("chrom")?.v, h.at("pos")?.v, None)?; + row += ""; + body.push(Line::Html(format!(" {row}"))); + } + body.push(Line::Html("
    {}{}{}{}{}{}{}{}{}{}{}{}{}
    ".to_owned())); + for h in cdna::construct_past_cap(&hits, HITS_SHOWN)? { + body.push(Line::Note(format!( + "Hit at a known lab-construct site past the first 50: {}", + cdna::construct_past_cap_text(&h)? + ))); + } + if nh > 0 { + body.push(Line::Html(VIEWER_NOTE.to_owned())); + } + Ok(Card { + title: format!( + "ClinVar Hits (best-reviewed first, {} of {nh})", + nh.min(HITS_SHOWN) + ), + full: true, + body, + ..Card::default() + }) +} + +/// The known deep intronic sites and the cDNA scan's loci. +fn sites_card(d: &D<'_>) -> Result { + let mut body = Vec::new(); + let mut caveats = Vec::new(); + if d.has("deep_intronic") { + body.extend(deep_intronic::table(&d.at("deep_intronic")?)?); + caveats.push(DEEP_INTRONIC_NOTE); + } + let scan = cdna::scan_lines(d)?; + if let Some(first) = scan.first() { + body.push(Line::Para(first.clone())); + if scan.len() > 1 { + body.extend(cdna::scan_table(&d.at("cdna_scan")?)?); + caveats.push(CDNA_SCAN_NOTE); + } + } + Ok(Card { + title: "Known Deep Intronic Sites and the cDNA Scan".to_owned(), + full: true, + caveats, + body, + ..Card::default() + }) +} + +/// The HTML report's cards: the screen always, the hits table when there +/// are hits, the deep intronic sites and the cDNA scan when step 06 wrote +/// them. +pub fn html(s: &Summary) -> Result { + let sec = s.section("clinvar"); + let d = D::of(&sec); + let mut cards = vec![html::card(&sec, screen_card(s, &d)?)?]; + if sec.state.ran() && d.opt("hits")?.is_some() { + cards.push(html::card(&sec, hits_card(&d)?)?); + } + if sec.state.ran() && (d.has("deep_intronic") || d.opt("cdna_scan")?.is_some()) { + cards.push(html::card(&sec, sites_card(&d)?)?); + } + Ok(HtmlPart { + cards, + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/comphet.rs b/rust/crates/og-render/src/sections/comphet.rs index df853c692..35c43bb00 100644 --- a/rust/crates/og-render/src/sections/comphet.rs +++ b/rust/crates/og-render/src/sections/comphet.rs @@ -1,2 +1,177 @@ -//! The report section `comphet`. Empty until its unit of the 1.0.0 plan -//! lands. +//! Step 31's variant prioritization (slivar): the prioritized and +//! compound-het counts, and the phase of each compound-het pair read from +//! the reads (trans, cis or unknown), with the pairs in trans. + +use pycompat::fmt::ljust; +use pycompat::json::Value; + +use crate::escape::e; +use crate::model::{Card, HtmlPart, Inline, Line, Malformed, Summary}; +use crate::notes::{COMPHET_NOTE, VIEWER_NOTE}; +use crate::sections::clinvar::{D, VIEWER_TH, list_or_empty, locus_text, viewer_td_of}; +use crate::{html, text}; + +const TITLE: &str = "Variant Prioritization (slivar)"; + +/// `ph.get(key, 0)` as text. +fn count(ph: &D<'_>, key: &str) -> Result { + ph.s_or(key, "0") +} + +/// `ph.get("trans", 0) - len(trans)`, an int or a float as Python gives it. +fn more_in_trans(ph: &D<'_>, shown: usize) -> Result, Malformed> { + let Some(t) = ph.get("trans")? else { + return Ok(None); + }; + let n = t.num()?; + if n <= shown as f64 { + return Ok(None); + } + Ok(Some(match t.v { + Value::Int(i) => (i - shown as i128).to_string(), + Value::Bool(_) => (n as i128 - shown as i128).to_string(), + _ => pycompat::fmt::repr(n - shown as f64), + })) +} + +/// The phase lines of the text block. +pub fn phase_text(d: &D<'_>) -> Result, Malformed> { + let ph = d.at("compound_het_phase")?; + let mut out = vec![format!( + " Phase from the reads: {} trans, {} cis, {} unknown of {} pairs", + count(&ph, "trans")?, + count(&ph, "cis")?, + count(&ph, "unknown")?, + d.s_or("compound_het_pairs", "0")? + )]; + let trans = list_or_empty(d, "compound_het_trans")?; + for x in &trans { + out.push(format!( + " {} {} in trans on {} reads or read pairs", + ljust(&x.s("gene")?, 8), + x.s("variants")?, + x.s("reads")? + )); + } + if let Some(more) = more_in_trans(&ph, trans.len())? { + // The Python takes len() of the list here, which a missing or + // null list cannot give. + d.at("compound_het_trans")?.len()?; + out.push(format!(" ... {more} more in the pairs table")); + } + out.push(format!(" ({COMPHET_NOTE})")); + Ok(out) +} + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "slivar", Some(TITLE))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if !head.ran { + return Ok(w); + } + let d = D::of(&s.section("slivar")); + w.push(format!(" Prioritized variants: {}", d.s("prioritized")?)); + if d.has("frequency_unknown") { + w.push(format!( + " Frequency unknown (prioritized, not counted as rare): {}", + d.s("frequency_unknown")? + )); + } + if d.has("compound_het_variants") { + w.push(format!( + " Compound het candidates: {} variants across {} genes", + d.s("compound_het_variants")?, + d.s("compound_het_genes")? + )); + } + if d.has("compound_het_phase") { + w.extend(phase_text(&d)?); + } else if d.has("compound_het_variants") { + w.push( + " (Candidates only: unphased single-sample data. See docs/31-slivar.md)".to_owned(), + ); + } + w.push(String::new()); + Ok(w) +} + +/// The phase lines of the card: the counts, then the pairs in trans. +fn phase_html(d: &D<'_>) -> Result, Malformed> { + let ph = d.at("compound_het_phase")?; + let mut out = vec![Line::Stat( + "Pairs in trans / cis / unknown".to_owned(), + Inline::Text(format!( + "{} / {} / {}", + count(&ph, "trans")?, + count(&ph, "cis")?, + count(&ph, "unknown")? + )), + )]; + let trans = list_or_empty(d, "compound_het_trans")?; + if !trans.is_empty() { + out.push(Line::Html(" ".to_owned())); + out.push(Line::Html(format!( + " {VIEWER_TH}" + ))); + for x in &trans { + let variants = x.at("variants")?; + out.push(Line::Html(format!( + " {}", + e(&x.s("gene")?), + e(&variants.text()?), + e(&x.s("reads")?), + viewer_td_of(&locus_text(&variants)?)? + ))); + } + out.push(Line::Html("
    GenePair in transReads
    {}{}{}
    ".to_owned())); + out.push(Line::Html(VIEWER_NOTE.to_owned())); + } + Ok(out) +} + +/// The HTML report's card, always there. +pub fn html(s: &Summary) -> Result { + let sec = s.section("slivar"); + let d = D::of(&sec); + let mut body = vec![Line::Stat( + "Prioritized variants".to_owned(), + Inline::Text(d.s_or_none("prioritized")?), + )]; + if d.has("frequency_unknown") { + body.push(Line::Stat( + "Frequency unknown (not rare)".to_owned(), + Inline::Text(d.s("frequency_unknown")?), + )); + } + if d.has("compound_het_variants") { + body.push(Line::Stat( + "Compound het candidates".to_owned(), + Inline::Text(format!( + "{} variants, {} genes", + d.s("compound_het_variants")?, + d.s("compound_het_genes")? + )), + )); + } + let mut caveats = Vec::new(); + if d.has("compound_het_phase") { + body.extend(phase_html(&d)?); + caveats.push(COMPHET_NOTE); + } + let card = html::card( + &sec, + Card { + title: TITLE.to_owned(), + caveats, + body, + ..Card::default() + }, + )?; + Ok(HtmlPart { + cards: vec![card], + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/couple.rs b/rust/crates/og-render/src/sections/couple.rs index cf30455af..66a32335e 100644 --- a/rust/crates/og-render/src/sections/couple.rs +++ b/rust/crates/og-render/src/sections/couple.rs @@ -1,2 +1,76 @@ -//! The report section `couple`. Empty until its unit of the 1.0.0 plan -//! lands. +//! Step 39's couple carrier screen: the counts, then each shared and +//! X-linked gene with the chance for a child and both partners' findings. + +use crate::model::{Card, HtmlPart, Line, Malformed, State, Summary}; +use crate::notes::COUPLE_NOTE; +use crate::sections::clinvar::D; +use crate::{html, text}; + +/// The lines, the same in both reports; the last is [`COUPLE_NOTE`]. +pub fn lines(d: &D<'_>) -> Result, Malformed> { + let partner = match d.opt("partner")? { + Some(p) => p.text()?, + None => "the partner".to_owned(), + }; + let count = |k: &str| -> Result { d.get(k)?.map_or(Ok(0), |v| v.len()) }; + let mut out = vec![format!( + "Partner: {partner}. Genes where both carry a change: {}; X-linked findings: {}; genes where only \ + one partner carries a change: {}", + count("shared")?, + count("x_linked")?, + d.s_or("one_partner", "0")? + )]; + for (label, key) in [("Shared", "shared"), ("X-linked", "x_linked")] { + let Some(list) = d.get(key)? else { continue }; + for x in list.items()? { + out.push(format!( + "{label}: {} ({}): {}. This sample: {}. {partner}: {}", + x.s("gene")?, + x.s("condition")?, + x.s("risk")?, + x.s("own")?, + x.s("partner_findings")? + )); + } + } + out.push(COUPLE_NOTE.to_owned()); + Ok(out) +} + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "couple", Some("Couple Carrier Screen (step 39)"))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if head.ran { + let d = D::of(&s.section("couple")); + w.extend(lines(&d)?.iter().map(|l| format!(" {l}"))); + w.push(String::new()); + } + Ok(w) +} + +/// The HTML report's card, when the screen ran or its step failed. +pub fn html(s: &Summary) -> Result { + let sec = s.section("couple"); + if sec.state == State::Missing { + return Ok(HtmlPart::default()); + } + let mut body = lines(&D::of(&sec))?; + body.pop(); + let card = html::card( + &sec, + Card { + title: "Couple Carrier Screen".to_owned(), + full: true, + caveats: vec![COUPLE_NOTE], + body: body.into_iter().map(Line::Para).collect(), + ..Card::default() + }, + )?; + Ok(HtmlPart { + cards: vec![card], + files: Vec::new(), + }) +} diff --git a/rust/crates/og-render/src/sections/cpsr.rs b/rust/crates/og-render/src/sections/cpsr.rs index 4db6327f4..c2ae094a0 100644 --- a/rust/crates/og-render/src/sections/cpsr.rs +++ b/rust/crates/og-render/src/sections/cpsr.rs @@ -1 +1,203 @@ -//! The report section `cpsr`. Empty until its unit of the 1.0.0 plan lands. +//! Step 17's cancer predisposition screen (CPSR): its classification +//! breakdown, the two ClinVar releases, CPSR's own benign calls among the +//! final P/LP rows, the rows where CPSR and ClinVar disagree, the rows below +//! the depth floor and the cDNA read check. + +use crate::model::{Card, Colour, HtmlPart, Inline, Line, Malformed, Summary}; +use crate::notes::CPSR_NOTE; +use crate::sections::cdna::cdna_text; +use crate::sections::clinvar::{D, call_text}; +use crate::{html, text}; + +/// One P/LP or disagreement row: variant, gene, the classes and codes. +pub fn row_text(x: &D<'_>) -> Result { + let mut first = format!("final {}", x.s("final")?); + if let Some(a) = x.opt("authority")? { + first += &format!(" ({})", a.text()?); + } + let mut parts = vec![first]; + if let Some(c) = x.opt("clinvar")? { + parts.push(format!("ClinVar {}", c.text()?)); + } + if let Some(c) = x.opt("cpsr")? { + let mut p = format!("CPSR's own {}", c.text()?); + if let Some(codes) = x.opt("acmg_codes")? { + p += &format!(" ({})", codes.text()?); + } + parts.push(p); + } + Ok(format!( + "{} {}: {}", + x.s("gene")?, + x.s("variant")?, + parts.join(", ") + )) +} + +/// `dbs.get(key) or 'unknown'`. +fn release(dbs: &D<'_>, key: &str) -> Result { + match dbs.opt(key)? { + Some(v) => v.text(), + None => Ok("unknown".to_owned()), + } +} + +/// The lines both reports share beyond the breakdown. +pub fn lines(d: &D<'_>, dbs: &D<'_>) -> Result, Malformed> { + let mut out = Vec::new(); + if !d.has("plp") { + return Ok(out); + } + out.push(format!( + "ClinVar release in CPSR's bundle: {}; step 6 used the ClinVar file of {}", + release(dbs, "cpsr_clinvar_release")?, + release(dbs, "clinvar_release")? + )); + if d.flag("own_class_column")? { + out.push(format!( + "Final P/LP rows that CPSR's own rules class benign or likely benign: {}", + d.s("final_plp_own_benign")? + )); + let dis = d.at("disagreements")?; + out.push(format!( + "Rows where ClinVar and CPSR's own class disagree on P/LP: {}", + dis.len()? + )); + for x in dis.items()? { + out.push(format!(" {}", row_text(&x)?)); + } + } else { + out.push( + "The classification table has no CPSR_CLASSIFICATION column, so no disagreement can be shown." + .to_owned(), + ); + } + out.push(format!( + "P/LP rows below the depth floor (DP below {}): {}", + d.s("call_floor_dp")?, + d.s("low_confidence")? + )); + let plp = d.at("plp")?.items()?; + for x in &plp { + if x.flag("low_confidence")? { + out.push(format!(" {}; {}", row_text(x)?, call_text(x)?)); + } + } + if d.has("cdna_flagged") { + out.push(format!( + "cDNA read check of the P/LP rows (alt reads clipped at exon edges): {} flagged", + d.s("cdna_flagged")? + )); + for x in &plp { + if x.at("cdna")?.at("check")?.is("flagged") { + out.push(format!(" {}; reads: {}", row_text(x)?, cdna_text(x)?)); + } + } + } + Ok(out) +} + +fn databases(s: &Summary) -> Result, Malformed> { + Ok(D::new(s.need("databases")?, "summary.databases")) +} + +/// The text report's block. +pub fn text(s: &Summary) -> Result, Malformed> { + let Some(head) = text::head(s, "cpsr", Some("Cancer Predisposition Screening (CPSR)"))? else { + return Ok(Vec::new()); + }; + let mut w = head.lines; + if !head.ran { + return Ok(w); + } + let d = D::of(&s.section("cpsr")); + if d.flag("html_report")? { + w.push(" HTML report in cpsr/ (open in a browser)".to_owned()); + } + if let Some(cls) = d.opt("classification")? { + w.push(format!( + " Classification breakdown ({}):", + d.s("classification_column")? + )); + for (k, v) in cls.pairs()? { + w.push(format!(" {k}: {}", v.text()?)); + } + } else if d.has("classification_column") { + w.push(" Classification table has no CLASSIFICATION column.".to_owned()); + } + for line in lines(&d, &databases(s)?)? { + w.push(format!(" {line}")); + } + w.push(format!(" {CPSR_NOTE}")); + w.push(String::new()); + Ok(w) +} + +/// Python's `str.strip()`: whitespace as `str.isspace` counts it. +fn strip(s: &str) -> &str { + pycompat::text::strip(s, None) +} + +/// `os.path.dirname(p)`. +fn dirname(p: &str) -> &str { + let Some(i) = p.rfind('/') else { + return ""; + }; + let head = &p[..=i]; + if head.chars().all(|c| c == '/') { + head + } else { + head.trim_end_matches('/') + } +} + +/// The HTML report's card, always there, with a link to CPSR's own report +/// beside this one. +pub fn html(s: &Summary) -> Result { + let sec = s.section("cpsr"); + let d = D::of(&sec); + let report = if d.flag("html_report")? { + Inline::Badge("Complete".to_owned(), Colour::State) + } else { + Inline::Badge("No HTML".to_owned(), Colour::Gray) + }; + let mut body = vec![Line::Stat("CPSR report".to_owned(), report)]; + if let Some(cls) = d.opt("classification")? { + for (k, v) in cls.pairs()? { + body.push(Line::Stat(k.to_owned(), Inline::Text(v.text()?))); + } + } + for line in lines(&d, &databases(s)?)? { + body.push(Line::Note(strip(&line).to_owned())); + } + let card = html::card( + &sec, + Card { + title: "Cancer Predisposition".to_owned(), + caveats: vec![CPSR_NOTE], + body, + ..Card::default() + }, + )?; + let mut files = Vec::new(); + let src = sec.source.filter(|v| crate::model::truthy(v)); + if sec.state.ran() + && let Some(src) = src + && d.flag("html_report")? + { + let src = D::new(src, "sections.cpsr.source"); + let src = src.string()?; + files.push(if src.ends_with(".html") { + (src.to_owned(), "CPSR HTML report".to_owned()) + } else { + ( + format!("{}/", dirname(src)), + "the cpsr folder, which holds the CPSR HTML report".to_owned(), + ) + }); + } + Ok(HtmlPart { + cards: vec![card], + files, + }) +} diff --git a/rust/crates/og-render/src/sections/deep_intronic.rs b/rust/crates/og-render/src/sections/deep_intronic.rs index 8bda20f8a..f76f605b7 100644 --- a/rust/crates/og-render/src/sections/deep_intronic.rs +++ b/rust/crates/og-render/src/sections/deep_intronic.rs @@ -1,2 +1,53 @@ -//! The report section `deep_intronic`. Empty until its unit of the 1.0.0 -//! plan lands. +//! Step 06's known deep intronic sites, reported on every run: they lie far +//! from any exon, so most filters miss them. + +use crate::escape::e; +use crate::model::{Line, Malformed}; +use crate::notes::VIEWER_NOTE; +use crate::sections::clinvar::{D, VIEWER_TH, ev, viewer_td}; + +/// One site's call: `het, GQ 40, DP 30`, `ref, GQ 50 (gVCF block)`, +/// `no call: why`. +pub fn call(x: &D<'_>) -> Result { + let c = x.at("call")?; + let detail = x.at("detail")?; + if c.is("no call") { + let mut out = "no call".to_owned(); + if detail.truthy() { + out += &format!(": {}", detail.text()?); + } + return Ok(out); + } + let mut out = c.string()?.to_owned(); + if detail.truthy() { + out += &format!(", {}", detail.text()?); + } + Ok(out) +} + +/// The HTML table of the sites, with the Viewer note. +pub fn table(sites: &D<'_>) -> Result, Malformed> { + let mut out = vec![ + Line::Html(" ".to_owned()), + Line::Html(format!( + " {VIEWER_TH}" + )), + ]; + for x in sites.items()? { + let (chrom, pos) = (x.at("chrom")?, x.at("pos")?); + out.push(Line::Html(format!( + " {}", + ev(&x.at("gene")?)?, + ev(&x.at("hgvs")?)?, + ev(&chrom)?, + e(&pos.text()?), + ev(&x.at("ref")?)?, + ev(&x.at("alt")?)?, + e(&call(&x)?), + viewer_td(chrom.v, pos.v, None)? + ))); + } + out.push(Line::Html("
    GeneVariantPositionCall
    {}{}{}:{} {}>{}{}
    ".to_owned())); + out.push(Line::Html(VIEWER_NOTE.to_owned())); + Ok(out) +} diff --git a/rust/crates/og-render/src/sections/two_hits.rs b/rust/crates/og-render/src/sections/two_hits.rs index 695939b43..5fac84288 100644 --- a/rust/crates/og-render/src/sections/two_hits.rs +++ b/rust/crates/og-render/src/sections/two_hits.rs @@ -1,2 +1,71 @@ -//! The report section `two_hits`. Empty until its unit of the 1.0.0 plan -//! lands. +//! The genes a called deletion and a small variant both reach (og summary +//! collect's two-hit rule), shown under the carrier list: each gene with +//! its verdict, then which deletion callers were read. + +use crate::model::Malformed; +use crate::sections::clinvar::D; + +/// `str.capitalize()`: the first character upper case, the rest lower. +fn capitalize(s: &str) -> String { + let mut c = s.chars(); + match c.next() { + Some(first) => { + pycompat::text::upper(&first.to_string()) + &pycompat::text::lower(c.as_str()) + } + None => String::new(), + } +} + +/// `"; ".join(items)` of a list of strings. +fn joined(v: &D<'_>) -> Result { + let items = v.items()?; + let parts: Vec<&str> = items.iter().map(D::string).collect::>()?; + Ok(parts.join("; ")) +} + +/// The lines, none for a summary written before the rule. +pub fn lines(t: Option<&D<'_>>) -> Result, Malformed> { + let Some(t) = t.filter(|t| t.truthy()) else { + return Ok(Vec::new()); + }; + let mut out = Vec::new(); + let genes = t.at("genes")?; + for x in genes.items()? { + let allele = match x.opt("allele")? { + Some(a) => format!(" ({})", a.text()?), + None => String::new(), + }; + out.push(format!( + "{}: {} ({}; AR), both copies may be hit: {} and {} {}{allele}; {}", + capitalize(x.at("verdict")?.string()?), + x.s("gene")?, + x.s("condition")?, + x.s("deletion")?, + x.s("variant")?, + x.s("genotype")?, + x.s("why")? + )); + } + let found = if genes.truthy() { + format!("{} gene(s); ", genes.len()?) + } else { + "none found; ".to_owned() + }; + let read = joined(&t.at("deletions_read")?)?; + let read = if read.is_empty() { + "no caller".to_owned() + } else { + read + }; + let not_read = t.at("deletions_not_read")?; + let not_read = if not_read.truthy() { + format!("; not read: {}", joined(¬_read)?) + } else { + String::new() + }; + out.push(format!( + "Two hits across callers (a deletion plus a variant on the other copy): {found}deletions read from \ + {read}{not_read}" + )); + Ok(out) +} diff --git a/tests/parity/cases.tsv b/tests/parity/cases.tsv index 3e7e16214..ee6244192 100644 --- a/tests/parity/cases.tsv +++ b/tests/parity/cases.tsv @@ -1255,3 +1255,18 @@ 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-couple gen:report-corpus.sh --summary m-couple.json -o m-couple.txt -o m-couple.html m-couple.txt,m-couple.html bytes PYTHON_IMAGE +report-render m-couple-empty gen:report-corpus.sh --summary m-couple-empty.json -o m-couple-empty.txt -o m-couple-empty.html m-couple-empty.txt,m-couple-empty.html bytes PYTHON_IMAGE +report-render m-clinical-stale gen:report-corpus.sh --summary m-clinical-stale.json -o m-clinical-stale.txt -o m-clinical-stale.html m-clinical-stale.txt,m-clinical-stale.html bytes PYTHON_IMAGE +report-render m-clinical-failed gen:report-corpus.sh --summary m-clinical-failed.json -o m-clinical-failed.txt -o m-clinical-failed.html m-clinical-failed.txt,m-clinical-failed.html bytes PYTHON_IMAGE +report-render m-clinical-unreadable gen:report-corpus.sh --summary m-clinical-unreadable.json -o m-clinical-unreadable.txt -o m-clinical-unreadable.html m-clinical-unreadable.txt,m-clinical-unreadable.html bytes PYTHON_IMAGE +report-render m-clinvar-many gen:report-corpus.sh --summary m-clinvar-many.json -o m-clinvar-many.txt -o m-clinvar-many.html m-clinvar-many.txt,m-clinvar-many.html bytes PYTHON_IMAGE +report-render m-clinvar-none gen:report-corpus.sh --summary m-clinvar-none.json -o m-clinvar-none.txt -o m-clinvar-none.html m-clinvar-none.txt,m-clinvar-none.html bytes PYTHON_IMAGE +report-render m-clinvar-freq-note gen:report-corpus.sh --summary m-clinvar-freq-note.json -o m-clinvar-freq-note.txt -o m-clinvar-freq-note.html m-clinvar-freq-note.txt,m-clinvar-freq-note.html bytes PYTHON_IMAGE +report-render m-cpsr-noown gen:report-corpus.sh --summary m-cpsr-noown.json -o m-cpsr-noown.txt -o m-cpsr-noown.html m-cpsr-noown.txt,m-cpsr-noown.html bytes PYTHON_IMAGE +report-render m-cpsr-none gen:report-corpus.sh --summary m-cpsr-none.json -o m-cpsr-none.txt -o m-cpsr-none.html m-cpsr-none.txt,m-cpsr-none.html bytes PYTHON_IMAGE +report-render m-slivar-phase gen:report-corpus.sh --summary m-slivar-phase.json -o m-slivar-phase.txt -o m-slivar-phase.html m-slivar-phase.txt,m-slivar-phase.html bytes PYTHON_IMAGE +report-render m-slivar-unphased gen:report-corpus.sh --summary m-slivar-unphased.json -o m-slivar-unphased.txt -o m-slivar-unphased.html m-slivar-unphased.txt,m-slivar-unphased.html bytes PYTHON_IMAGE +report-render m-sf-notes gen:report-corpus.sh --summary m-sf-notes.json -o m-sf-notes.txt -o m-sf-notes.html m-sf-notes.txt,m-sf-notes.html bytes PYTHON_IMAGE +report-render m-clinical-escape gen:report-corpus.sh --summary m-clinical-escape.json -o m-clinical-escape.txt -o m-clinical-escape.html m-clinical-escape.txt,m-clinical-escape.html bytes PYTHON_IMAGE +report-render m-since-clinical gen:report-corpus.sh --summary m-since-clinical.json -o m-since-clinical.txt -o m-since-clinical.html m-since-clinical.txt,m-since-clinical.html bytes PYTHON_IMAGE diff --git a/tests/parity/gen/report-corpus.sh b/tests/parity/gen/report-corpus.sh index d16728bc9..2d7645463 100644 --- a/tests/parity/gen/report-corpus.sh +++ b/tests/parity/gen/report-corpus.sh @@ -69,7 +69,8 @@ done < folders/cases.tsv rm -rf folders # --- the summary of case "full", changed one way each --------------------------------- -# Its callability section (130 kB of genes, not ported yet) is set to not run. +# Its callability section (130 kB of genes) is set to not run, so each change stays small; +# the collected summaries render it. [ -s full.json ] || { echo "report-corpus.sh: og summary collect wrote no full.json" >&2; exit 1; } base=base.tmp jq -c '.sections.callability.state = "missing" | .sections.callability.data = {}' full.json > "$base" @@ -116,6 +117,46 @@ mut counts '.sections.variants.state = "ok" | .sections.variants.data = {"total" mut counts-float '.sections.coverage.state = "ok" | .sections.coverage.data.mean_depth = 30.05' rm -f "$base" +# The clinical sections: case cb-full, which holds every one of them but the couple +# screen and callability, changed one way each. (all and all-history, the only +# collected summaries with a couple screen, run as text-only cases.) +[ -s cb-full.json ] || { echo "report-corpus.sh: og summary collect wrote no cb-full.json" >&2; exit 1; } +cmut() { jq -c "$2" cb-full.json > "m-$1.json"; } +couple='{"title": "Couple carrier screen", "step": "39", "state": "ok", "note": null, "file_date": "2021-03-04 05:06 UTC", + "data": {"partner": "P<2>", "shared": [{"gene": "CFTR", "condition": "Cystic fibrosis", "risk": "1 in 4", + "own": "c.1521_1523del (P)", "partner_findings": "c.350G>A (LP)"}], "x_linked": [{"gene": "DMD", + "condition": "Duchenne", "risk": "1 in 4 sons", "own": "c.10G>T (P)", "partner_findings": "."}], "one_partner": 2}}' +cmut couple ".sections.couple = $couple" +cmut couple-empty '.sections.couple = {"title": "Couple carrier screen", "state": "ok", "note": null, "data": {}}' +for st in stale failed unreadable; do + keys='"clinvar", "clinical", "cpsr", "slivar", "carrier", "couple"' + [ "$st" = stale ] || keys="$keys, \"callability\"" + cmut "clinical-$st" ".sections.couple = $couple | reduce ($keys) as \$k (.; + .sections[\$k].state = \"$st\" | .sections[\$k].note = \"note of \(\$k) <&>\")" +done +cmut clinvar-many '.sections.clinvar.data.hits as $h | .sections.clinvar.data.hits = ($h + $h + $h + $h + $h + $h) + | .sections.clinvar.data.hits[55].cdna = {"check": "flagged", "note": "clipped ", "alt_reads": 4, "alt_clipped": 3, + "ref_reads": 10, "ref_clipped": 0, "construct": "pcDNA3"} + | .sections.clinvar.data.hits[57].cdna = {"check": "clear", "note": "", "alt_reads": 4, "alt_clipped": 0, + "ref_reads": 10, "ref_clipped": 0, "construct": "EGFP"} + | .sections.clinvar.data.hits[58].cdna = {"check": "not_checked", "note": "", "alt_reads": null, "alt_clipped": null, + "ref_reads": null, "ref_clipped": null, "construct": "lentiviral"}' +cmut clinvar-none '.sections.clinvar.data.hits = [] | .sections.clinvar.data.count = 0 | del(.sections.clinvar.data.deep_intronic) + | .sections.clinvar.data.cdna_scan = {"note": "not run: no BAM", "loci": []} | del(.sections.clinvar.data.frequency) + | del(.sections.clinvar.data.low_confidence) | del(.sections.clinvar.data.cdna_flagged)' +cmut clinvar-freq-note '.sections.clinvar.data.frequency = {"note": "no VEP output"} | .sections.clinvar.data.call_floor = null' +cmut cpsr-noown '.sections.cpsr.data.own_class_column = false | .sections.cpsr.data.classification = {} + | .sections.cpsr.source = "cpsr/S.cpsr.grch38.html" | del(.sections.cpsr.data.cdna_flagged)' +cmut cpsr-none '.sections.cpsr.data = {"html_report": false}' +cmut slivar-phase '.sections.slivar.data.compound_het_phase.trans = 2.5 | .sections.slivar.data.compound_het_trans = []' +cmut slivar-unphased 'del(.sections.slivar.data.compound_het_phase)' +cmut sf-notes '.sections.clinical.data.acmg_sf.two_hit_note = "carrier list " + | .sections.clinical.data.acmg_sf.structural_checked = true | .sections.clinical.data.acmg_sf.high_impact_weak = 2 + | .sections.clinical.data.acmg_sf.high_impact_frequency_unknown = 1 | del(.sections.clinical.data.acmg_sf.genes_on_list)' +cmut clinical-escape '.sections.clinvar.data.hits[0].gene = "A<&>'"'"'\"" | .sections.carrier.data.carriers[0].condition = "x & z" + | .sections.clinical.data.acmg_sf.clinvar_hits[0].gene = ""' +cmut since-clinical '.new_since = {"previous_utc": "2026-01-01T00:00:00Z", "sections": ["clinvar", "carrier", "slivar"]}' + # --- the oracle ----------------------------------------------------------------------- cat > oracle.sh << 'ORACLE' #!/bin/sh @@ -132,6 +173,7 @@ PYTHONPATH=$d python3 "$d/render_report.py" "$@" rm -rf "$d" # The sections og report render writes; the others are left out. PORTED=" qc variants not-run not-assessed " +PORTED="${PORTED}callability clinvar clinical-filter cpsr slivar carrier-list couple " while [ $# -gt 0 ]; do case "$1" in -o) @@ -163,6 +205,8 @@ END { m = 0 for (i = 1; i <= n; i++) { l = lines[i] + # The charts (og report charts) are not ported yet: their figures go. + if (l ~ /^
    /) continue if (l ~ /^
  • ") i++ @@ -194,6 +238,10 @@ cat > text.awk << 'AWK' BEGIN { rule = sprintf("%80s", ""); gsub(/ /, "#", rule) title["## Quality control"] = "qc"; title["## Variant Calling (DeepVariant)"] = "variants" + title["## Genes This Genome Could Not Read Reliably"] = "callability" + title["## ClinVar Pathogenic Screen"] = "clinvar"; title["## Clinical Variant Filter"] = "clinical-filter" + title["## Cancer Predisposition Screening (CPSR)"] = "cpsr"; title["## Variant Prioritization (slivar)"] = "slivar" + title["## Carrier List (this sample)"] = "carrier-list"; title["## Couple Carrier Screen (step 39)"] = "couple" } { lines[++n] = $0 } END { From d7b484d1fd09cf5fcc7b35abc66d94fafea0871a Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:49:43 +0100 Subject: [PATCH 05/29] fix(summary): refuse a sample folder og summary collect cannot list A missing, mistyped or unreadable --sample-dir walked as an empty folder, so the collect wrote a summary.json with every section missing and exited 0, over a good summary when --out named it. cli() now checks that the sample folder is a folder it can list before it reads anything, and exits 1 with "ERROR: : not a readable folder: " and writes nothing. Step folders below it that cannot be read still count as empty. The parity case summary-collect/no-folder gets goldens for the deliberate fix (the Python oracle wrote an empty summary and exited 0), and the unit test of an unwritable --out points --sample-dir at a folder that exists. Bead: C17 --- rust/crates/og-summary/src/collect.rs | 21 ++- rust/crates/og-summary/src/collect/scan.rs | 6 +- rust/crates/og-summary/src/collect/tests.rs | 2 +- .../tests/rv_summary_collect_missing_dir.rs | 139 ++++++++++++++++++ .../golden/summary-collect/no-folder/exit | 1 + .../no-folder/no-folder.json.absent | 0 .../golden/summary-collect/no-folder/stderr | 1 + .../golden/summary-collect/no-folder/stdout | 0 8 files changed, 165 insertions(+), 5 deletions(-) create mode 100644 rust/crates/og-summary/tests/rv_summary_collect_missing_dir.rs create mode 100644 tests/parity/golden/summary-collect/no-folder/exit create mode 100644 tests/parity/golden/summary-collect/no-folder/no-folder.json.absent create mode 100644 tests/parity/golden/summary-collect/no-folder/stderr create mode 100644 tests/parity/golden/summary-collect/no-folder/stdout diff --git a/rust/crates/og-summary/src/collect.rs b/rust/crates/og-summary/src/collect.rs index 2c87bff83..23ba181f5 100644 --- a/rust/crates/og-summary/src/collect.rs +++ b/rust/crates/og-summary/src/collect.rs @@ -92,8 +92,8 @@ pub enum Group { } /// `og summary collect`: exit 0 when summary.json is written (whatever -/// its sections say), 1 when it cannot be written, 2 for a command line it -/// cannot read. +/// its sections say), 1 when the sample folder cannot be read or the +/// summary cannot be written, 2 for a command line it cannot read. pub fn cli(argv: &[String], out: &mut impl Write, err: &mut impl Write) -> u8 { let a = match args::parse(argv, OPTS) { Ok(a) => a, @@ -130,6 +130,15 @@ pub fn cli(argv: &[String], out: &mut impl Write, err: &mut impl Write) -> u8 { return 2; } }; + // A mistyped or unreadable sample folder would read as one where no + // step ran, and its summary would replace a good one with every section + // missing. Only the folders below it count as empty when unreadable. + if let Err(e) = readable_folder(&input.sample_dir) { + return crate::finish( + Err(format!("{}: not a readable folder: {e}", input.sample_dir)), + err, + ); + } // The sections' own data is freed before the text is made, and the // tree before the file is written: a large ClinVar list is in memory in // one form at a time. @@ -160,6 +169,14 @@ pub fn cli(argv: &[String], out: &mut impl Write, err: &mut impl Write) -> u8 { crate::finish(r, err) } +/// Ok when `dir` is a folder whose entries can be listed. +fn readable_folder(dir: &str) -> std::io::Result<()> { + if !std::fs::metadata(dir)?.is_dir() { + return Err(std::io::Error::other("not a folder")); + } + std::fs::read_dir(dir).map(drop) +} + /// The `assets/` folder of the checkout the binary sits in (`bin/.og/og-*` /// and `rust/target/*/og` both do), else the one beside the binary's /// folder, as the Python found it beside `bin/`. diff --git a/rust/crates/og-summary/src/collect/scan.rs b/rust/crates/og-summary/src/collect/scan.rs index b81721f1f..26da7d11d 100644 --- a/rust/crates/og-summary/src/collect/scan.rs +++ b/rust/crates/og-summary/src/collect/scan.rs @@ -36,8 +36,10 @@ pub struct FolderIndex { impl FolderIndex { /// Walks `root`. A folder that cannot be read counts as empty, as it - /// does for `glob`; a missing root gives an empty index. A name that is - /// not UTF-8 is left out: no pattern of a section can name it. + /// does for `glob`; a missing root gives an empty index, so + /// `og summary collect` refuses a root it cannot list before it walks. + /// A name that is not UTF-8 is left out: no pattern of a section can + /// name it. pub fn walk(root: &Path) -> FolderIndex { let mut files = HashSet::new(); let mut stack: Vec<(PathBuf, String, usize)> = vec![(root.to_path_buf(), String::new(), 0)]; diff --git a/rust/crates/og-summary/src/collect/tests.rs b/rust/crates/og-summary/src/collect/tests.rs index 4f622c77f..fbad45cce 100644 --- a/rust/crates/og-summary/src/collect/tests.rs +++ b/rust/crates/og-summary/src/collect/tests.rs @@ -471,7 +471,7 @@ fn the_command_line() { "--sample", "S", "--sample-dir", - "x", + &d.path("S"), "--out", &d.path("no/such/dir/x.json"), "--only", diff --git a/rust/crates/og-summary/tests/rv_summary_collect_missing_dir.rs b/rust/crates/og-summary/tests/rv_summary_collect_missing_dir.rs new file mode 100644 index 000000000..c644b225c --- /dev/null +++ b/rust/crates/og-summary/tests/rv_summary_collect_missing_dir.rs @@ -0,0 +1,139 @@ +//! `og summary collect` refuses a sample folder it cannot list: a missing +//! one, a file, or one without read permission exits 1 and leaves `--out` +//! as it was. An empty folder that exists is still collected, with every +//! section missing. + +use og_summary::collect::cli; +use std::fs; +use std::path::PathBuf; + +const GOOD: &str = "{\"a good summary\": true}\n"; + +struct Dir(PathBuf); + +impl Dir { + fn new(tag: &str) -> Dir { + let p = std::env::temp_dir().join(format!( + "og-summary-missing-dir-{tag}-{}", + std::process::id() + )); + let _ = fs::remove_dir_all(&p); + fs::create_dir_all(&p).unwrap(); + Dir(p) + } + + fn path(&self, name: &str) -> String { + self.0.join(name).display().to_string() + } +} + +impl Drop for Dir { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } +} + +fn assets() -> String { + concat!(env!("CARGO_MANIFEST_DIR"), "/../../../assets").into() +} + +/// Runs the verb; gives the exit code and stderr. +fn run(sample_dir: &str, extra: &[&str]) -> (u8, String) { + let mut argv: Vec = [ + "--sample", + "S", + "--sample-dir", + sample_dir, + "--assets", + &assets(), + ] + .iter() + .map(|s| (*s).to_owned()) + .collect(); + argv.extend(extra.iter().map(|s| (*s).to_owned())); + let (mut out, mut err) = (Vec::new(), Vec::new()); + let code = cli(&argv, &mut out, &mut err); + (code, String::from_utf8(err).unwrap()) +} + +/// The folder is refused: exit 1, the error names it, and `out` keeps the +/// good summary, byte for byte. +fn assert_refused(sample_dir: &str, out: &str, extra: &[&str]) { + let mut args = vec!["--out", out]; + args.extend_from_slice(extra); + let (code, err) = run(sample_dir, &args); + assert_eq!(code, 1, "stderr: {err}"); + assert!( + err.contains(&format!("ERROR: {sample_dir}: not a readable folder")), + "stderr: {err}" + ); + assert_eq!(fs::read_to_string(out).unwrap(), GOOD); +} + +#[test] +fn a_missing_sample_folder_leaves_the_summary_as_it_was() { + let d = Dir::new("missing"); + let out = d.path("summary.json"); + fs::write(&out, GOOD).unwrap(); + let typo = d.path("SAMPEL"); + assert_refused(&typo, &out, &[]); + // Each group reads the same folder, so each refuses it. + for g in ["a", "b", "c"] { + assert_refused(&typo, &out, &["--only", g]); + } + assert!(!PathBuf::from(&typo).exists()); +} + +#[test] +fn without_out_a_missing_folder_writes_nothing() { + let d = Dir::new("no-out"); + let typo = d.path("SAMPEL"); + let (code, err) = run(&typo, &[]); + assert_eq!(code, 1, "stderr: {err}"); + // It stops on the folder, not on a write into a folder that is not there. + assert!(err.contains("not a readable folder"), "stderr: {err}"); + assert!(!PathBuf::from(&typo).exists()); +} + +#[test] +fn a_file_is_not_a_sample_folder() { + let d = Dir::new("file"); + let out = d.path("summary.json"); + fs::write(&out, GOOD).unwrap(); + let file = d.path("S.vcf.gz"); + fs::write(&file, "x").unwrap(); + assert_refused(&file, &out, &[]); +} + +#[cfg(unix)] +#[test] +fn an_unreadable_sample_folder_leaves_the_summary_as_it_was() { + use std::os::unix::fs::PermissionsExt; + let d = Dir::new("unreadable"); + let out = d.path("summary.json"); + fs::write(&out, GOOD).unwrap(); + let locked = d.path("S"); + fs::create_dir_all(format!("{locked}/qc")).unwrap(); + fs::set_permissions(&locked, fs::Permissions::from_mode(0o000)).unwrap(); + // Root lists any folder: there is nothing to refuse. + if fs::read_dir(&locked).is_err() { + assert_refused(&locked, &out, &[]); + } + fs::set_permissions(&locked, fs::Permissions::from_mode(0o755)).unwrap(); +} + +#[test] +fn an_empty_sample_folder_is_still_collected() { + let d = Dir::new("empty"); + let sample = d.path("S"); + fs::create_dir_all(&sample).unwrap(); + let out = d.path("summary.json"); + fs::write(&out, GOOD).unwrap(); + let (code, err) = run(&sample, &["--out", &out, "--only", "a"]); + assert_eq!(code, 0, "stderr: {err}"); + let body = fs::read_to_string(&out).unwrap(); + assert_ne!(body, GOOD); + assert!(body.contains("\"coverage\""), "{body}"); + assert!(body.contains("\"state\": \"missing\""), "{body}"); + assert!(!body.contains("\"state\": \"ok\""), "{body}"); +} diff --git a/tests/parity/golden/summary-collect/no-folder/exit b/tests/parity/golden/summary-collect/no-folder/exit new file mode 100644 index 000000000..d00491fd7 --- /dev/null +++ b/tests/parity/golden/summary-collect/no-folder/exit @@ -0,0 +1 @@ +1 diff --git a/tests/parity/golden/summary-collect/no-folder/no-folder.json.absent b/tests/parity/golden/summary-collect/no-folder/no-folder.json.absent new file mode 100644 index 000000000..e69de29bb diff --git a/tests/parity/golden/summary-collect/no-folder/stderr b/tests/parity/golden/summary-collect/no-folder/stderr new file mode 100644 index 000000000..5dcf7c827 --- /dev/null +++ b/tests/parity/golden/summary-collect/no-folder/stderr @@ -0,0 +1 @@ +ERROR: no-such-folder: not a readable folder: No such file or directory (os error 2) diff --git a/tests/parity/golden/summary-collect/no-folder/stdout b/tests/parity/golden/summary-collect/no-folder/stdout new file mode 100644 index 000000000..e69de29bb From a786d08e8f3f4ba99211d7cf87c4acb0fcb1f2e0 Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:49:43 +0100 Subject: [PATCH 06/29] docs(changelog): og summary collect refuses a sample folder it cannot list Bead: C17 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05c3388e1..e6b9cfa5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Each release's notes. From 1.0.0 on, a major or minor release has hand-written n ## Unreleased +- `og summary collect` refuses a sample folder it cannot list. A mistyped `--sample-dir`, a file, or a folder without read permission read as a folder where no step ran, so the collect wrote a summary.json with every section missing and exited 0, over the good summary when `--out` named it. It now exits 1 with `ERROR: : not a readable folder: ` and writes nothing. An empty folder that exists is still collected, and a step folder inside it that cannot be read still counts as empty. No output file or format changes. - 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. 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 07/29] 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
  • +
    +

    Clinical

    +

    Is there anything to take to a doctor?

    +
    +
    +
    +

    ClinVar Screening

    +

    Step 06 · Not run · link to this section

    +
    StatusNot run
    +
    +
    +
    +
    +

    Clinical Variant Filter

    +

    Step 23 · Not run · link to this section

    +
    StatusNot run
    +
    +
    +
    +
    +

    Cancer Predisposition

    +

    Step 17 · Not run · link to this section

    +
    StatusNot run
    +
    +
    +
    +
    +

    Variant Prioritization (slivar)

    +

    Step 31 · Not run · link to this section

    +
    StatusNot run
    +
    +
    +
    +
    +

    Repeat Expansions

    +

    Step 09 · Not run · link to this section

    +
    StatusNot run
    +
    +
    +
    +
    +

    Mitochondrial Analysis

    +

    Step 20 · Not run · link to this section

    +
    StatusNot run
    +
    +
    +
    +
    +
    +

    Carrier

    +

    What could I pass on to a child?

    +
    +
    +
    +

    Paralog Copy Number

    +

    Steps 35b, 35 · Not run · link to this section

    +
    StatusNot checked
    +

    Paralog copy number: not checked (neither step 35 nor step 35b ran)

    +
    +
    +
    +
    +
    +

    Medicines

    +

    How might I respond to medicines?

    +
    +
    +
    +

    Pharmacogenomics

    +

    Step 07 · Not run · link to this section

    +
    StatusNot run
    +
    +
    +
    +
    +

    CPIC Drug Recommendations

    +

    Step 27 · Not run · link to this section

    +
    StatusNot run
    +
    +
    +
    +
    +

    CYP2D6 Across Callers

    +

    Steps 32, 21 · Not run · link to this section

    +

    CYP2D6 sits beside its look-alike CYP2D7, and short reads often cannot tell them apart, so pypgx, Cyrius and step 36 can disagree or give no call. A disagreement or a missing call means the gene was not read reliably here; a clinical CYP2D6 test settles it.

    +
    PharmCAT (step 36's call)not run
    +
    pypgxnot run
    +
    Cyriusnot run
    +
    pypgx and Cyrius agreefewer than two usable calls
    +
    +
    +
    +
    +

    HLA Typing

    +

    Step 08 · Not run · link to this section

    +
    StatusNot run
    +
    +
    +
    +

    Ancestry

    Where do my ancestors come from?

    +
    +
    +

    Mitochondrial Haplogroup

    +

    Step 12 · Not run · link to this section

    +
    StatusNot run
    +
    +

    Runs of Homozygosity

    @@ -194,6 +326,13 @@

    Runs of Homozygosity

    Traits and scores

    What do studies say about my traits and risk scores?

    +
    +
    +

    Traits

    +

    Step 42 · Not run · link to this section

    +
    StatusNot run
    +
    +

    Telomere content (relative)

    diff --git a/tests/parity/golden/report-render/m-optin-viral-na/m-optin-viral-na.txt b/tests/parity/golden/report-render/m-optin-viral-na/m-optin-viral-na.txt index 9ebd34215..4d011bee3 100644 --- a/tests/parity/golden/report-render/m-optin-viral-na/m-optin-viral-na.txt +++ b/tests/parity/golden/report-render/m-optin-viral-na/m-optin-viral-na.txt @@ -26,6 +26,14 @@ SNPs: 11111 Indels: 6667 Counts of the calls DeepVariant made against the reference. A whole genome usually holds 4 to 5 million, most of them common. PASS calls passed the caller's filter; a count says nothing about health on its own. +################################################################################ +# 3. CARRIER: What could I pass on to a child? +################################################################################ + +## Paralog Copy Number +--- + Paralog copy number: not checked (neither step 35 nor step 35b ran) + ################################################################################ # 7. OTHER AND EXPORTS: What else did the run find or write? ################################################################################ From 2918247c5be3d9fc79dfd013f3183db1290fb43c Mon Sep 17 00:00:00 2001 From: GeiserX <9169332+GeiserX@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:01:05 +0100 Subject: [PATCH 29/29] test(report): check the AnnotSV tables and every card's Viewer sentence The structural-variants writer is not wired yet, so no report case reached the AnnotSV card's two converted tables. Two unit tests now render the card through the section frame: both tables, escaped genes and reasons, the defaults for empty fields, the heading between the tables, the Viewer sentence once after them, and the AnnotSV caveat. Each section now places its own Viewer sentence, so the HTML writer checks in debug builds that a card with a Viewer column and rows also has it. The chapter-order test again asserts which chapters show a card. --- rust/crates/og-render/src/html.rs | 17 +++- rust/crates/og-render/src/model.rs | 3 +- rust/crates/og-render/src/sections/annotsv.rs | 96 +++++++++++++++++++ .../og-render/tests/port_report_core.rs | 22 ++++- 4 files changed, 134 insertions(+), 4 deletions(-) diff --git a/rust/crates/og-render/src/html.rs b/rust/crates/og-render/src/html.rs index 415f019a4..efccdf40c 100644 --- a/rust/crates/og-render/src/html.rs +++ b/rust/crates/og-render/src/html.rs @@ -211,8 +211,23 @@ fn meta( Ok(out) } +/// A card that shows a Viewer column with rows also says how the viewer +/// opens: each section places [`Line::ViewerNote`] itself, so this catches +/// one that forgot. +fn viewer_note_placed(c: &Card) -> bool { + let shows_viewer = c.body.iter().any(|l| match l { + Line::Table(t) | Line::Folded(_, t) => t.viewer && !t.rows.is_empty(), + _ => false, + }); + !shows_viewer || c.body.contains(&Line::ViewerNote) +} + /// One section's frame. -fn frame(sid: &str, part: &HtmlPart, meta: Option, always: bool) -> Vec { +pub(crate) fn frame(sid: &str, part: &HtmlPart, meta: Option, always: bool) -> Vec { + debug_assert!( + part.cards.iter().all(viewer_note_placed), + "{sid}: a card shows a Viewer column without the line saying how it opens" + ); let wide = part.cards.len() > 1 || part.cards.iter().any(|c| c.full); let mut out = vec![format!( "
    ", diff --git a/rust/crates/og-render/src/model.rs b/rust/crates/og-render/src/model.rs index 8e649e6b9..939938781 100644 --- a/rust/crates/og-render/src/model.rs +++ b/rust/crates/og-render/src/model.rs @@ -361,7 +361,8 @@ impl Cell { /// A table: its column titles and rows. A table with a Viewer column (the /// rows end in a [`Cell::Viewer`]) has `viewer`; the card places the line /// saying how the viewer opens, [`Line::ViewerNote`], where the report has -/// it. `layout` says how its tags and rows sit on the page's lines. +/// it; a card with such a table and no such line fails a debug check in the +/// HTML writer. `layout` says how its tags and rows sit on the page's lines. #[derive(Debug, Clone, PartialEq, Eq, Default)] pub struct Table { pub head: Vec, diff --git a/rust/crates/og-render/src/sections/annotsv.rs b/rust/crates/og-render/src/sections/annotsv.rs index ba3072a91..37caf236a 100644 --- a/rust/crates/og-render/src/sections/annotsv.rs +++ b/rust/crates/og-render/src/sections/annotsv.rs @@ -286,3 +286,99 @@ pub fn html(s: &Summary) -> Result { files: Vec::new(), }) } + +#[cfg(test)] +mod tests { + use super::html; + use crate::escape::e; + use crate::model::Summary; + use crate::notes::{ANNOTSV_NOTE, VIEWER_NOTE}; + + /// The structural-variants writer is not wired yet, so no report case + /// reaches this card: this renders it through the section frame. + fn page(data: &str) -> String { + let s = Summary::parse(&format!( + r#"{{"sections": {{"annotsv": {{"state": "ok", "data": {data}}}}}}}"# + )) + .unwrap(); + let part = html(&s).unwrap(); + crate::html::frame("structural-variants", &part, None, false).join("\n") + } + + const ROWS: &str = r#""rows": [ + {"type": "DEL", "sv": "chr1:1000-5000", "acmg_class": "5", "genes": ["A function & more"}, + {"type": "INS", "sv": "chr3:7", "acmg_class": "4", "genes": [], "support": null, + "exon_edge": "no", "population_af": null}]"#; + + const DOWN: &str = r#""downgraded": [ + {"type": "DUP", "sv": "chr2:10-90", "annotsv_class": "4", "acmg_class": "3", "genes": [], + "rule": "", "rule_detail": ""}, + {"type": "DEL", "sv": "chr2:500-900", "annotsv_class": "5", "acmg_class": "3", "genes": ["X&Y"], + "rule": "B2", "rule_detail": "common & benign"}]"#; + + const HEAD: &str = r#""count": 2, "consensus": true, "single_caller": 0, "exon_edge_checked": true, + "exon_edge": 1, "common": 0"#; + + fn viewer(at: &str) -> String { + format!( + "{at}" + ) + } + + #[test] + fn both_tables_escape_their_cells_and_the_viewer_line_follows_them_once() { + let p = page(&format!("{{{HEAD}, {ROWS}, {DOWN}}}")); + let rows = format!( + " \ + \ + \ + {}\ + \ + {}
    SVClassGenesCallersChecksViewer
    DEL chr1:1000-50005A<B, C&D, G1 and 1 more2 callers (manta, delly)on exon edges: exon 2; benign-set AF 0.001 (gnomAD); \ + rule R1: loss <of> function & more
    INS chr3:74no genenot known (no step 22 consensus)none
    ", + viewer("chr1:1000-5000"), + viewer("chr3:7") + ); + let down = format!( + " \ + \ + \ + {}\ + \ + {}
    SVAnnotSV classClassGenesRuleWhyViewer
    DUP chr2:10-9043no genenot namedno detail
    DEL chr2:500-90053X&YB2common <AF> & benign
    ", + viewer("chr2:10-90"), + viewer("chr2:500-900") + ); + let lines: Vec<&str> = p.lines().collect(); + let at = |l: &str| lines.iter().position(|x| *x == l); + let (r, h, d, v) = ( + at(&rows).expect(&p), + at("

    Moved to class 3 by step 05's rules

    ").expect(&p), + at(&down).expect(&p), + at(VIEWER_NOTE).expect(&p), + ); + assert!(r < h && h + 1 == d && d + 1 == v, "{p}"); + assert_eq!(p.matches("

    ").count(), 1, "{p}"); + assert!( + p.contains(&format!("

    {}

    ", e(ANNOTSV_NOTE))), + "{p}" + ); + // Red control: an unescaped gene would not match the row. + assert!(!p.contains("AChecks"), "{p}"); + assert_eq!(p.matches("

    ").count(), 1, "{p}"); + let p = page(r#"{"count": 0}"#); + assert!(!p.contains(""), "{p}"); + assert!(!p.contains("class=\"caveat\""), "{p}"); + } +} diff --git a/rust/crates/og-render/tests/port_report_core.rs b/rust/crates/og-render/tests/port_report_core.rs index fb351d380..b8d8d44ca 100644 --- a/rust/crates/og-render/tests/port_report_core.rs +++ b/rust/crates/og-render/tests/port_report_core.rs @@ -272,8 +272,26 @@ fn both_reports_follow_the_chapter_order() { assert_eq!(&shown[..n - 2], in_order.as_slice()); // The HTML report shows a card for a section that did not run; the text // report leaves it out, except the paralogs' "not checked" line, which - // prints when neither step 35 nor step 35b ran. So the text keeps - // Quality and Carrier, in that order. + // prints when neither step 35 nor step 35b ran. So of the chapters with a + // card, the text keeps Quality and Carrier, in that order. + // Viral DNA, the one ported section of Other and exports, shows a card + // only when step 47 ran. + let with_card: Vec = CHAPTERS + .iter() + .filter(|c| c.sections.iter().any(|s| shown[..n - 2].contains(&s.id))) + .map(|c| c.title.to_uppercase()) + .collect(); + assert_eq!( + with_card, + [ + "QUALITY", + "CLINICAL", + "CARRIER", + "MEDICINES", + "ANCESTRY", + "TRAITS AND SCORES" + ] + ); assert_eq!(text_chapters(&txt), ["QUALITY", "CARRIER"]); let q = txt.split("# 1. QUALITY").nth(1).unwrap_or(""); assert!(q.find("## Quality control") < q.find("## Variant Calling (DeepVariant)"));