diff options
| author | Pjotr <wrk@bosa.lan> | 2026-11-09 11:52:57 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-11-09 11:55:25 +0000 |
| commit | de07aabd384d3adabf2b095b0a76a09efd81de45 (patch) | |
| tree | a9655dcca7f849a8f77c82c5eca3963c993b239b | |
| parent | 731e085c42e22104f4cba9975a33f5d22b058caf (diff) | |
| download | genenetwork4-de07aabd384d3adabf2b095b0a76a09efd81de45.tar.gz | |
plot: say which trait a plot is, not just draw it
Two traits over the same panel really do draw two different pictures --
verified on BXD.4, where all 20 chromosomes' polylines differ and the peaks
are LOD 2.93 at chr8 69.4 Mb (mCV24409492) against LOD 4.78 at chr8
95.7 Mb (rs3666069) -- but you could not tell from the title: every plot
came out headed **"trait rqtl: LOD along the genome"**, because
`/plot/rqtl` labelled it from `?trait=` and nothing else, defaulting to
"rqtl". The only visible difference was the peak annotation, and a caller
posting the saved response back (which is the documented way) could not
know the label was missing.
The response now names what it mapped: `/compute/rqtl` writes `Trait:
<name>` as its **first** note, and `/plot/rqtl` and `/plot/gemma` take the
title's label from that note when `?trait=` was not given. The
`POST /plot/rqtl` suggestion in `actions` also carries `?trait=`, so a
caller that posts only `data` still gets the right title.
* src/routes/rqtl.rs: the `Trait:` note, ahead of the column note; the
plot suggestion's href names the trait.
* src/routes/plot.rs: `trait_from_document`, used by both plot endpoints.
* tests/plot.rs: `two_traits_draw_two_different_plots` -- two canned
responses differing only in LOD, asserting each title names its own
trait from its own note, each annotates its own peak and not the
other's, and every polyline differs. That is the regression this is
about: a plot that reused anything between requests would draw the
first trait's shape for the second.
* tests/{rqtl,phewas}.rs: the plot suggestion is now matched by prefix,
with the trait asserted.
* README.md, docs/www/ai-instruction.md: where the title comes from, and
to post the whole response (or add `?trait=`) if you post only `data`.
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | docs/www/ai-instruction.md | 5 | ||||
| -rw-r--r-- | src/routes/plot.rs | 28 | ||||
| -rw-r--r-- | src/routes/rqtl.rs | 9 | ||||
| -rw-r--r-- | tests/phewas.rs | 6 | ||||
| -rw-r--r-- | tests/plot.rs | 98 | ||||
| -rw-r--r-- | tests/rqtl.rs | 14 |
7 files changed, 158 insertions, 10 deletions
diff --git a/README.md b/README.md index 6e16e77..e8a5e5a 100644 --- a/README.md +++ b/README.md @@ -974,8 +974,12 @@ accepts: the `PublishFreeze`'s own `Name`/`ShortName`/`Id` (e.g. the plotter's one-line peak summary comes back in `x-plot-summary`. R/qtl plots LOD with the permutation thresholds as dashed lines when the result carried them; GEMMA plots `-log10(p_lrt)` with a Bonferroni - line (`?bonferroni=false` drops it). Optional `?trait=`/`?title=` - label the plot. Configuration: `PLOT_PYTHON` (default `python3`), + line (`?bonferroni=false` drops it). The title names the trait: taken + from the posted response's own `Trait: ...` note (`/compute/rqtl` + writes it first), or from `?trait=` when you post only `data`; + `?title=` replaces the whole title. Two traits over the same panel draw + two different traces -- the plot is drawn from the rows you posted, and + nothing is cached between calls. Configuration: `PLOT_PYTHON` (default `python3`), `RQTL_PLOTTER`, `GEMMA_PLOTTER`. Both mapping responses suggest these calls in `actions`. diff --git a/docs/www/ai-instruction.md b/docs/www/ai-instruction.md index d1d26e6..81b17e4 100644 --- a/docs/www/ai-instruction.md +++ b/docs/www/ai-instruction.md @@ -427,7 +427,10 @@ A mapping's response ends with what to do next: in the `x-plot-summary` header. Post the whole response for both, or just its `data` member for `/plot/rqtl`; `/plot/gemma` needs `meta.columns` to find the `p_lrt` column. A body that is not a - mapping result is a `422` saying so. + mapping result is a `422` saying so. The title names the trait, read + from the response's own `Trait: ...` note — post the whole response and + two traits' plots are told apart at a glance; post only `data` and add + `?trait=` yourself, or every one of them is titled the same. * **`actions`: the same scan with 100 permutations** (`nperm: 100`), when you did not already ask for permutations. It is a `POST`, which is why it is not a `next` link. Those 100 shuffles are what turn "this peak diff --git a/src/routes/plot.rs b/src/routes/plot.rs index bd4a68b..00119dc 100644 --- a/src/routes/plot.rs +++ b/src/routes/plot.rs @@ -40,6 +40,20 @@ pub struct PlotParams { pub bonferroni: Option<String>, } +/// The trait a posted compute response says it mapped: the `Trait: ...` +/// note `/compute/rqtl` writes. It is what makes two plots of two traits +/// distinguishable at a glance -- the *data* differs, but without this the +/// title of every one of them said "trait rqtl". +fn trait_from_document(posted: &serde_json::Value) -> Option<String> { + posted["notes"] + .as_array()? + .iter() + .filter_map(|note| note.as_str()) + .find_map(|note| note.strip_prefix("Trait: ")) + .map(|trait_name| trait_name.trim().to_string()) + .filter(|trait_name| !trait_name.is_empty()) +} + /// What `/plot/gemma` validates in a posted GEMMA result: GEMMA's own /// `.assoc` rows are positional, 11 columns wide, with `rs` second and /// `p_lrt` last. @@ -155,7 +169,12 @@ pub async fn plot_rqtl( &config.rqtl_plotter, std::path::Path::new(&config.rqtl_cache_dir), &document, - plot_params.trait_label.as_deref().unwrap_or("rqtl"), + plot_params + .trait_label + .clone() + .or_else(|| trait_from_document(&document)) + .as_deref() + .unwrap_or("rqtl"), &borrowed, ) .await @@ -222,7 +241,12 @@ pub async fn plot_gemma( &config.gemma_plotter, std::path::Path::new(&config.gemma_cache_dir), &document, - plot_params.trait_label.as_deref().unwrap_or("gemma"), + plot_params + .trait_label + .clone() + .or_else(|| trait_from_document(&document)) + .as_deref() + .unwrap_or("gemma"), &borrowed, ) .await diff --git a/src/routes/rqtl.rs b/src/routes/rqtl.rs index ee0b159..104c041 100644 --- a/src/routes/rqtl.rs +++ b/src/routes/rqtl.rs @@ -361,9 +361,13 @@ pub async fn compute_rqtl( .map(|output| { let notes = output.notes.clone(); let num_rows = output.markers.len(); + // What was mapped comes first: it is the one thing that tells two + // responses from the same panel apart, and `/plot/rqtl` reads it + // for the title. let mut envelope = ctx .envelope(rqtl::to_json(output)) .with_columns(rqtl::MARKER_COLUMNS.iter().map(|s| s.to_string()).collect(), num_rows) + .with_note(&format!("Trait: {}", request.trait_name)) .with_note( "markers come from R/qtl's scanone output, positional (see meta.columns for names); permutation is null unless nperm > 0 was requested.", ); @@ -423,9 +427,12 @@ pub async fn compute_rqtl( // The picture: post this response back. The body is not // duplicated here -- it is the response the caller already has. + // The trait goes in the URL as well as the response: `/plot/rqtl` + // reads it from the posted `notes` now, but a caller that posts + // only `data` still gets the right title this way. envelope = envelope.with_action(crate::response::SuggestedCall { method: "POST".to_string(), - href: "/plot/rqtl".to_string(), + href: format!("/plot/rqtl?trait={}", path_segment(&request.trait_name)), description: "Draw this scan: POST this response back to /plot/rqtl (as-is, or its \ `data` member) and get an SVG Manhattan plot of these rows, with the \ permutation thresholds as dashed lines when nperm > 0. Nothing is \ diff --git a/tests/phewas.rs b/tests/phewas.rs index 16f4391..8ff3fb8 100644 --- a/tests/phewas.rs +++ b/tests/phewas.rs @@ -200,8 +200,10 @@ async fn a_group_mapping_suggests_a_phewas_and_a_permutation_run() { let actions = envelope["actions"].as_array().expect("suggestions"); assert!( - actions.iter().any(|action| action["href"] == "/plot/rqtl"), - "a plot suggestion: {envelope}" + actions.iter().any(|action| action["href"] + .as_str() + .is_some_and(|href| href.starts_with("/plot/rqtl"))), + "a plot suggestion (its href names the trait, so the plot titles itself): {envelope}" ); let action = actions .iter() diff --git a/tests/plot.rs b/tests/plot.rs index 848ae49..0788ce9 100644 --- a/tests/plot.rs +++ b/tests/plot.rs @@ -193,3 +193,101 @@ async fn a_body_that_is_not_a_mapping_result_is_a_422() { .await; assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY); } + +/// Two responses for two traits, differing only in their LOD values, must +/// draw two different pictures -- and say which trait each is. +/// +/// The trace is the point: a plot that reused anything between requests +/// (a cached SVG, a stale scratch file) would draw the first trait's shape +/// for the second, and the only thing giving it away would be the title. +/// This pins both: the drawings differ, and each names its own trait from +/// the response's own `Trait:` note rather than falling back to a title +/// that says nothing. +#[tokio::test] +async fn two_traits_draw_two_different_plots() { + if !plotter_present("rqtl") { + eprintln!("skipping two_traits_draw_two_different_plots: needs the plotter script (RQTL_PLOTTER) and PLOT_PYTHON"); + return; + } + let Some(token) = auth_token().await else { + eprintln!("skipping two_traits_draw_two_different_plots: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)"); + return; + }; + + let canned = |name: &str, lods: [f64; 5]| { + serde_json::json!({ + "data": { + "markers": [ + ["rs1", "1", 3.1, lods[0]], + ["rs2", "1", 8.2, lods[1]], + ["rs3", "2", 12.0, lods[2]], + ["rs4", "2", 19.5, lods[3]], + ["rs5", "X", 30.0, lods[4]] + ], + "permutation": null + }, + // What /compute/rqtl writes first, and what /plot/rqtl reads + // its title from. + "notes": [ + format!("Trait: BXD::{name}"), + "Genotype: prepared panel subset x (version BXD.4, 93 individuals, 0 parents/F1s dropped); 5 of 5 supplied values aligned onto it by name." + ] + }) + }; + let mut svgs: Vec<(String, String, String)> = Vec::new(); + for (name, lods) in [ + ("10001", [0.2, 0.3, 2.9, 0.1, 0.4]), + ("10002", [1.8, 0.4, 0.2, 4.7, 0.9]), + ] { + let response = post_json_with_token( + "POST /plot/rqtl (two traits)", + "/plot/rqtl", + &token, + &canned(name, lods), + ) + .await; + assert_eq!(response.status(), StatusCode::OK); + let summary = response + .headers() + .get("x-plot-summary") + .expect("x-plot-summary") + .to_str() + .unwrap() + .to_string(); + let bytes = response.into_body().collect().await.unwrap().to_bytes(); + let text = String::from_utf8_lossy(&bytes).to_string(); + // The title names the trait, taken from the posted notes. + assert!( + text.contains(&format!("BXD::{name}")), + "the title carries the trait from the response's own note: {name}" + ); + svgs.push((name.to_string(), summary, text)); + } + let (first_name, first_summary, first) = &svgs[0]; + let (second_name, second_summary, second) = &svgs[1]; + assert_ne!(first, second, "{first_name} and {second_name} must not draw the same SVG"); + // And each draw its own peak, not the other's. + assert!(first_summary.contains("peak rs3"), "{first_summary}"); + assert!(second_summary.contains("peak rs4"), "{second_summary}"); + assert!( + first.contains("chr2 12 Mb, LOD 2.90") && !first.contains("LOD 4.70"), + "{first_name}'s own annotation only" + ); + assert!( + second.contains("chr2 20 Mb, LOD 4.70"), + "{second_name}'s own annotation: {second_summary}" + ); + // The traces themselves: no polyline point set should coincide. + let traces = |svg: &str| -> Vec<String> { + svg.split("<polyline points=\"") + .skip(1) + .map(|rest| rest.split('"').next().unwrap_or_default().to_string()) + .collect() + }; + let (a, b) = (traces(first), traces(second)); + assert_eq!(a.len(), b.len(), "same chromosomes in both"); + assert!( + a.iter().zip(&b).all(|(left, right)| left != right), + "every chromosome's trace differs between the two traits" + ); +} diff --git a/tests/rqtl.rs b/tests/rqtl.rs index 45157b2..5e060bb 100644 --- a/tests/rqtl.rs +++ b/tests/rqtl.rs @@ -95,9 +95,19 @@ async fn compute_rqtl_maps_a_qtl_from_the_bxd_riset_fixture() { assert_eq!(action["method"], "POST"); assert_eq!(action["body"]["nperm"], 100); // ... and the plot of the same run, which is the other suggestion. + // Its href carries the trait, so the plot titles itself even when the + // caller posts only `data`. + let plot = actions + .iter() + .find(|action| { + action["href"] + .as_str() + .is_some_and(|href| href.starts_with("/plot/rqtl")) + }) + .unwrap_or_else(|| panic!("a plot suggestion: {envelope}")); assert!( - actions.iter().any(|action| action["href"] == "/plot/rqtl"), - "a plot suggestion: {envelope}" + plot["href"].as_str().unwrap().contains("trait=mytrait"), + "the plot suggestion names the trait: {plot}" ); assert_eq!( action["body"]["genotype_file"], body["genotype_file"], |
