From b3811de4f2fb0e1d65ade757ad3ea992eb3f9f67 Mon Sep 17 00:00:00 2001 From: Pjotr Date: Tue, 10 Nov 2026 22:35:45 +0000 Subject: traits: genotypes at a confirmed peak, and the plot of them Returning genotypes is the one thing this API otherwise never does, so it is fenced and the fence is the feature: GET /groups/{group}/traits/{trait_id}/peak-genotypes?marker= * the server re-runs the R/qtl scan for the trait -- the same machinery and gate as /compute/rqtl -- and takes the peak from that run, rather than trusting a client that names a marker; * the marker must lie within ?window= Mb of that peak (default 10), or the answer is 422 not_the_peak quoting the peak's marker, chromosome, Mb and LOD; * the peak must reach ?min_lod= (default 3), or 422 not_a_hit; * only animals that have a value for the trait are listed, and only this marker's calls. Nothing else of the matrix leaves. On DOL 10003 that is UNCHS026345 (chr9 75.76 Mb, LOD 5.86) with 192 animals across A 110 / B 8 / H 74 -- the two a class of eight being exactly the thing worth seeing. POST /plot/genotypes draws R/qtl's Fig 4.1 from it: post the reply's `animals` (or its `genotypes` map) with the trait's values and get an SVG with one point per animal, genotype class across, value up, class means marked. Nothing is recomputed -- the plot is of the numbers the client sends, like the other plots. New script scripts/plot-genotypes.py (stdlib-only SVG, like its siblings) and the GENOTYPE_PLOTTER key. tests/peakgenotypes.rs: the peak's calls for animals with values (with the counts agreeing with the rows), the SVG, and a marker on another chromosome refused with the peak quoted. Also keeps /compute/rqtl's error order: naming both a group and a file is "ambiguous" even when the request also forgot its values. --- tests/peakgenotypes.rs | 171 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 tests/peakgenotypes.rs (limited to 'tests') diff --git a/tests/peakgenotypes.rs b/tests/peakgenotypes.rs new file mode 100644 index 0000000..56461f8 --- /dev/null +++ b/tests/peakgenotypes.rs @@ -0,0 +1,171 @@ +//! `GET /groups/{group}/traits/{id}/peak-genotypes` and `POST /plot/genotypes`. +//! +//! Returning genotypes is the one thing this API otherwise never does, so the +//! tests are as much about the fence as the data: the marker must sit at a peak +//! the server re-confirms, and only animals with a value get a call. + +use axum::http::StatusCode; +use http_body_util::BodyExt; +use serde_json::Value; + +mod common; + +async fn ready() -> Option { + if !common::db_available() { + eprintln!("skipping: no usable database"); + return None; + } + common::auth_token().await +} + +/// The peak of a fresh scan, so the marker asked for is genuinely the peak. +async fn scan_peak(token: &str, values: &Value) -> Value { + let response = common::post_json_with_token( + "POST /compute/rqtl (peak)", + "/compute/rqtl", + token, + &serde_json::json!({ + "group": "DOL", + "trait_name": "DOL::10003", + "samples": values, + }), + ) + .await; + assert_eq!(response.status(), StatusCode::OK); + let bytes = response.into_body().collect().await.unwrap().to_bytes(); + let envelope: Value = serde_json::from_slice(&bytes).unwrap(); + envelope["data"]["markers"] + .as_array() + .unwrap() + .iter() + .max_by(|left, right| { + left[3] + .as_f64() + .unwrap() + .partial_cmp(&right[3].as_f64().unwrap()) + .unwrap() + }) + .unwrap() + .clone() +} + +#[tokio::test] +async fn the_peak_markers_genotypes_come_back_for_animals_with_values() { + let Some(token) = ready().await else { return }; + let values = common::get_with_token( + "GET /groups/DOL/traits/10003/values", + "/groups/DOL/traits/10003/values", + &token, + ) + .await; + assert_eq!(values.status(), StatusCode::OK); + let bytes = values.into_body().collect().await.unwrap().to_bytes(); + let values: Value = common::data(&bytes); + + let peak = scan_peak(&token, &values).await; + let marker = peak[0].as_str().unwrap(); + let response = common::get_with_token( + "GET .../peak-genotypes", + &format!("/groups/DOL/traits/10003/peak-genotypes?marker={marker}"), + &token, + ) + .await; + let status = response.status(); + let bytes = response.into_body().collect().await.unwrap().to_bytes(); + assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes)); + let envelope: Value = serde_json::from_slice(&bytes).unwrap(); + let data = &envelope["data"]; + assert_eq!(data["marker"], serde_json::json!(marker)); + assert_eq!(data["chr"], peak[1], "the marker's chromosome comes from the marker table"); + // The scan that confirmed it is in the reply. + assert_eq!(data["peak"]["marker"], serde_json::json!(marker)); + assert!(data["peak"]["lod"].as_f64().unwrap() >= 3.0); + + let animals = data["animals"].as_array().unwrap(); + assert!(animals.len() > 50, "a real panel: {}", animals.len()); + let mut counted = std::collections::BTreeMap::new(); + for animal in animals { + assert!(animal["individual"].is_string()); + assert!(animal["genotype"].is_string()); + assert!( + animal["value"].is_f64() || animal["value"].is_i64(), + "every animal has a value: {animal}" + ); + *counted + .entry(animal["genotype"].as_str().unwrap().to_string()) + .or_insert(0usize) += 1; + } + // The counts agree with the animals, which is what the plot's x axis is. + for (genotype, count) in counted { + assert_eq!(data["counts"][&genotype].as_u64().unwrap() as usize, count); + } + // Only animals with a value are here: the trait has fewer than the panel. + assert!( + animals.len() <= 200, + "no more animals than the panel: {}", + animals.len() + ); + + // And the plot takes the reply and the values. + let plot = serde_json::json!({ + "marker": marker, + "genotypes": animals.iter().map(|animal| { + (animal["individual"].as_str().unwrap().to_string(), animal["genotype"].clone()) + }).collect::>(), + "values": values, + }); + let response = common::post_json_with_token("POST /plot/genotypes", "/plot/genotypes", &token, &plot).await; + let status = response.status(); + let bytes = response.into_body().collect().await.unwrap().to_bytes(); + assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes)); + let svg = String::from_utf8_lossy(&bytes); + assert!(svg.trim_start().starts_with("