about summary refs log tree commit diff
path: root/src
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 /src
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 'src')
-rw-r--r--src/routes/plot.rs28
-rw-r--r--src/routes/rqtl.rs9
2 files changed, 34 insertions, 3 deletions
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 \