about summary refs log tree commit diff
path: root/tests/plot.rs
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-09 11:52:57 +0000
committerPjotr <wrk@bosa.lan>2026-11-09 11:55:25 +0000
commitde07aabd384d3adabf2b095b0a76a09efd81de45 (patch)
treea9655dcca7f849a8f77c82c5eca3963c993b239b /tests/plot.rs
parent731e085c42e22104f4cba9975a33f5d22b058caf (diff)
downloadgenenetwork4-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`.
Diffstat (limited to 'tests/plot.rs')
-rw-r--r--tests/plot.rs98
1 files changed, 98 insertions, 0 deletions
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"
+    );
+}