diff options
| author | Pjotr <wrk@bosa.lan> | 2026-11-10 23:08:31 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-11-10 23:08:31 +0000 |
| commit | b4246412072773819f4500cb008f71c3f3101fdf (patch) | |
| tree | d953ed61c7ca9dfad87483b0e0e311d79505e71f /src | |
| parent | e2386cdf37196f7571afda4cfb9d1ec70b194cf7 (diff) | |
| download | genenetwork4-b4246412072773819f4500cb008f71c3f3101fdf.tar.gz | |
qtl2: name the genotype model, and say what the conversion dropped
Two engines reading one riset panel can fit different predictors, and the
reply now says which one it used and what it cost -- with this panel's own
numbers.
* `genotype_model: "riself" | "f2"` on /compute/qtl2 (`het` kept as the
older spelling: drop/keep). riself is the default: R/qtl's
convert2riself coding, the H calls become missing and the HMM
re-imputes them. f2 keeps them as a third genotype -- a diagnostic.
* The conversion now counts the calls it drops and keeps the count
beside the cross (conversion.json), so a reply can say
"610684 heterozygous call(s) (12.7 %) were dropped". A cross
converted before that file existed measures the count once, on first
use, rather than never.
* Crossed with the note that already explained the H drop, so the two
read together: what riself does, how many calls it cost, and what f2
would give.
Documented in the README as "Why R/qtl and R/qtl2 disagree on a riset
panel", with the measurements (BXD trait 16627, BXD.8: R/qtl 9.59 at
rs33299937; qtl2 riself 3.73; qtl2 f2 9.68), R/qtl's adaptive
convert2riself rule quoted from its own source, and qtl2's side: riself
has two states, f2 three, and `genril2` -- from qtl2 0.18, "the progeny
are inbred lines", cross_info first column the number of generations --
is the honest model for residual heterozygosity and is available in the
0.42 we run. What is missing for it is recorded precisely: a founder-based
cross expects individual genotypes coded as founder alleles rather than
1/2/3 (a prototype with numeric codes read every genotype as missing), so
founder_geno from the two parental alleles and an allele-coded het remain
to be done, with DOL as the counter-example (its H is a collapsed DO, not
residual heterozygosity).
tests/qtl2.rs: the model is reported (Cross: riself / Cross: f2), the
dropped count is in the reply, keeping the calls finds more, and an
unknown model is refused rather than ignored.
Diffstat (limited to 'src')
| -rw-r--r-- | src/qtl2.rs | 79 | ||||
| -rw-r--r-- | src/routes/qtl2.rs | 37 |
2 files changed, 110 insertions, 6 deletions
diff --git a/src/qtl2.rs b/src/qtl2.rs index c234150..318c2ee 100644 --- a/src/qtl2.rs +++ b/src/qtl2.rs @@ -78,6 +78,13 @@ pub struct PreparedCross { /// Founder alleles the cross fits (8 for the DO). Zero when the cross /// carries no founder genotypes, which is every `.geno` panel here. pub founders: usize, + /// Heterozygous calls the conversion wrote as missing. R/qtl's + /// `convert2riself` drops the same ones; the count is what turns "the + /// engines disagree" into "this panel has 14 % H at that marker". + pub het_dropped: usize, + /// Cells in the converted matrix (individuals x autosomal markers), for + /// turning `het_dropped` into a percentage. + pub cells: usize, /// True when qtl2 fits one coefficient per founder allele, so a LOD /// asks whether *any* founder differs rather than which allele at a /// biallelic marker does. @@ -143,6 +150,28 @@ pub struct Qtl2Output { pub summary: String, } +/// How many genotype cells carry one code -- the count a cached conversion +/// needs to say what it dropped, without re-converting. +fn count_calls(text: &str, code: &str) -> usize { + let mut seen_header = false; + let mut start = 3usize; + let mut count = 0usize; + for line in text.lines() { + let line = line.trim_end_matches('\r'); + if line.trim().is_empty() || line.starts_with('#') || line.starts_with('@') { + continue; + } + let columns: Vec<&str> = line.split('\t').collect(); + if !seen_header { + seen_header = true; + start = if columns.len() > 3 && columns[3] == "Mb" { 4 } else { 3 }; + continue; + } + count += columns.iter().skip(start).filter(|cell| cell.trim() == code).count(); + } + count +} + /// The header's `@het` code, if it declares one. fn metadata_het(text: &str) -> Option<String> { text.lines() @@ -224,10 +253,12 @@ pub fn prepare_cross( // genotypes, and qtl2 needs the same treatment -- left in, it reads the // cross as multiparent and its scan1 takes a path this panel cannot // satisfy ("scan1_pg ..."). + let mut het_kept: Option<String> = None; if crosstype == "riself" { let het_code = metadata_het(&text); if let Some(het_code) = het_code { codes.remove(&het_code); + het_kept = Some(het_code); } } if codes.len() < 2 { @@ -266,6 +297,7 @@ pub fn prepare_cross( geno_csv.push('\n'); let mut markers = 0usize; let mut non_autosomal = 0usize; + let mut het_dropped = 0usize; for line in lines { let line = line.trim_end_matches('\r'); if line.trim().is_empty() || line.starts_with('#') { @@ -286,6 +318,9 @@ pub fn prepare_cross( geno_csv.push_str(marker); for value in &row[first_individual..] { let call = codes.get(value.trim()).map(String::as_str).unwrap_or(""); + if call.is_empty() && het_kept.as_deref() == Some(value.trim()) { + het_dropped += 1; + } geno_csv.push(','); geno_csv.push_str(call); } @@ -328,12 +363,22 @@ pub fn prepare_cross( "geno_transposed": true, }); std::fs::write(&control, control_json.to_string()).map_err(|e| io_error(&control, e))?; + // What the conversion discarded, kept beside the cross: a cached cross + // must be able to say how many calls it dropped. + let report = serde_json::json!({ + "het_dropped": het_dropped, + "cells": individuals.len() * markers, + }); + let report_path = directory.join("conversion.json"); + let _ = std::fs::write(&report_path, report.to_string()); return Ok(PreparedCross { control, - individuals, + individuals: individuals.clone(), markers, non_autosomal, crosstype: crosstype.to_string(), + het_dropped, + cells: individuals.len() * markers, founders: 0, multiparent: false, }); @@ -347,12 +392,42 @@ pub fn prepare_cross( .filter(|line| !line.trim().is_empty()) .count() .saturating_sub(1); + let report_path = directory.join("conversion.json"); + let mut report: Value = std::fs::read_to_string(&report_path) + .ok() + .and_then(|text| serde_json::from_str(&text).ok()) + .unwrap_or_else(|| serde_json::json!({})); + if report.get("het_dropped").is_none() { + // A cross converted before the count was kept: measure it once from the + // panel and record it beside the cross, so later replies are free. + let het_code = if crosstype == "riself" { + metadata_het(&text) + } else { + None + }; + let dropped = het_code + .as_deref() + .map(|code| count_calls(&text, code)) + .unwrap_or(0); + report = serde_json::json!({ + "het_dropped": dropped, + "cells": individuals.len() * markers, + }); + let _ = std::fs::write(&report_path, report.to_string()); + } + let het_dropped = report + .get("het_dropped") + .and_then(Value::as_u64) + .unwrap_or(0) as usize; + let cells = report.get("cells").and_then(Value::as_u64).unwrap_or(0) as usize; Ok(PreparedCross { control, individuals, markers, non_autosomal: 0, crosstype: crosstype.to_string(), + het_dropped, + cells, founders: 0, multiparent: false, }) @@ -526,6 +601,8 @@ pub fn prepare_control_cross(root: &Path, requested: &str) -> Result<PreparedCro markers, non_autosomal, crosstype, + het_dropped: 0, + cells: 0, founders, multiparent, }) diff --git a/src/routes/qtl2.rs b/src/routes/qtl2.rs index b2b882c..bc68aa5 100644 --- a/src/routes/qtl2.rs +++ b/src/routes/qtl2.rs @@ -82,10 +82,15 @@ pub struct Qtl2Request { pub kinship: Option<String>, /// `normal` (default) or `binary`. pub model: Option<String>, - /// What to do with heterozygous calls on a `riset` panel: `drop` (default; - /// R/qtl's `convert2riself`, the HMM re-imputes them) or `keep` (a third - /// genotype, i.e. an `f2` cross -- a diagnostic, since an inbred line has - /// no heterozygotes). Ignored for a cross that ships its own control file. + /// How heterozygous calls on a `riset` panel are modelled: `riself` + /// (default -- R/qtl's `convert2riself` coding, the calls are dropped and + /// the HMM re-imputes them) or `f2` (the calls are kept as a third + /// genotype). `f2` is a diagnostic: it fits het as a real genotype but + /// also assumes an outbred intercross design, which an inbred line is not. + /// Ignored for a cross that ships its own control file. + pub genotype_model: Option<String>, + /// Deprecated spelling of `genotype_model`: `drop` -> `riself`, + /// `keep` -> `f2`. pub het: Option<String>, /// Cores for the per-chromosome steps (default 4, capped at 16). pub cores: Option<u32>, @@ -195,7 +200,16 @@ pub async fn compute_qtl2( ) .with_request_id(rid)); } - let het = qtl2::Het::parse(request.het.as_deref()).map_err(|detail| { + let model_word = request + .genotype_model + .clone() + .or_else(|| request.het.clone()); + let het = qtl2::Het::parse(model_word.as_deref().map(|word| match word { + "riself" => "drop", + "f2" => "keep", + other => other, + })) + .map_err(|detail| { ApiError::new( StatusCode::UNPROCESSABLE_ENTITY, "invalid_het", @@ -637,6 +651,19 @@ pub async fn compute_qtl2( if let Some(note) = basket_note { notes.push(note); } + if het == qtl2::Het::Drop && cross.het_dropped > 0 && cross.cells > 0 { + // The one fact that explains most disagreements with R/qtl on a riset + // panel, stated with this panel's own numbers. + let share = cross.het_dropped as f64 / cross.cells as f64 * 100.0; + notes.push(format!( + "genotype_model riself: {} heterozygous call(s) ({share:.1} %) were dropped before the \ + scan -- R/qtl's convert2riself keeps two of the three codes and drops the third the \ + same way -- and the HMM re-imputes them from flanking markers. On a marker that is \ + mostly H the two engines then fit different predictors; genotype_model: \"f2\" keeps \ + the calls as a third genotype and shows what that costs or recovers.", + cross.het_dropped + )); + } if het == qtl2::Het::Keep { notes.push( "Heterozygous calls were kept as a third genotype, so the cross is fitted as `f2` \ |
