From f9ba0f12e3bf52919fc363c037fbf0a0deda8c17 Mon Sep 17 00:00:00 2001 From: Pjotr Date: Sat, 7 Nov 2026 18:15:37 +0000 Subject: plot: draw a mapping, and suggest it from the mapping The two plotters Pjotr added (scripts/plot-rqtl.py, plot-gemma.py) turn a saved mapping response into an SVG Manhattan panel. They are now endpoints, and the mapping that produces the response suggests them: POST /plot/rqtl same body as /compute/rqtl -> image/svg+xml POST /plot/gemma same body as /compute/gemma -> image/svg+xml Taking the mapping's own body rather than the mapping's output means one call gets you the picture, and the plot cannot describe a run you did not do: the handlers call the compute handlers and hand the resulting envelope to the script, exactly as the scripts' docstrings describe (they read `data.markers`, `data.permutation` and the notes' strain counts). The mapping is re-run, but through the engines' own caches -- seconds on BXD. The reply is an SVG, not the JSON envelope: an image is not JSON, and the plotter's one-line peak summary rides back in `x-plot-summary` so a caller that cannot see the picture still learns where the peak is. Both compute responses gain the matching suggestion: `actions` carries "the same request, plotted" (built from the request that ran, like the permutation suggestion), so an assistant can map, look, and only then decide whether the peak is worth the 100-permutation run. * src/plot.rs: run the configured script over the envelope, read the SVG back, clean up; a missing script is a 502 naming interpreter and script, not a panic. * src/routes/plot.rs: the two handlers (token-gated), delegating to compute_rqtl/compute_gemma so validation and alignment stay in one place; `x-plot-summary` sanitised into a header. * src/routes/{rqtl,gemma}.rs: the plot suggestion, before the permutation one. * src/config.rs: PLOT_PYTHON, RQTL_PLOTTER, GEMMA_PLOTTER. * tests/plot.rs: the suggestion resolves to a complete SVG with a peak summary, and an invalid mapping request is the mapping's own 422. * tests/{rqtl,phewas}.rs: find suggestions by href rather than position. * README.md, docs/www/ai-instruction.md (4a), docs/www/operator-checklist.md (new "Plotting": python3 must be on the server's PATH). --- tests/plot.rs | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 tests/plot.rs (limited to 'tests/plot.rs') diff --git a/tests/plot.rs b/tests/plot.rs new file mode 100644 index 0000000..012dc84 --- /dev/null +++ b/tests/plot.rs @@ -0,0 +1,123 @@ +//! `POST /plot/rqtl` and `POST /plot/gemma`: an SVG back, not an envelope. +//! +//! These run a real mapping, so they need the engines -- and the plotter +//! scripts, which are configured (`RQTL_PLOTTER`, `GEMMA_PLOTTER`, +//! `PLOT_PYTHON`). Skips when a piece is missing, like the mapping tests. + +use axum::http::StatusCode; +use http_body_util::BodyExt; +use serde_json::Value; + +mod common; + +use common::{auth_token, post_json_with_token}; + +fn tool_available(tool: &str, args: &[&str]) -> bool { + std::process::Command::new(tool) + .args(args) + .output() + .map(|output| output.status.success()) + .unwrap_or(false) +} + +fn rscript_available() -> bool { + tool_available("Rscript", &["--version"]) +} + +fn genotype_dir_present() -> bool { + let directory = genenetwork4::config::Config::load().genotype_files_dir; + std::path::Path::new(&directory).is_dir() +} + +fn plotter_present(which: &str) -> bool { + let config = genenetwork4::config::Config::load(); + let path = if which == "rqtl" { + config.rqtl_plotter + } else { + config.gemma_plotter + }; + std::path::Path::new(&path).is_file() && tool_available(&config.plot_python, &["--version"]) +} + +#[tokio::test] +async fn an_rqtl_mapping_can_be_plotted_and_the_suggestion_resolves() { + if !genotype_dir_present() || !rscript_available() || !plotter_present("rqtl") { + eprintln!( + "skipping an_rqtl_mapping_can_be_plotted_and_the_suggestion_resolves: needs \ + genotype files, Rscript and the plotter script" + ); + return; + } + let Some(token) = auth_token().await else { + eprintln!("skipping an_rqtl_mapping_can_be_plotted_and_the_suggestion_resolves: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)"); + return; + }; + + let samples: Vec = ["BXD1", "BXD2", "BXD5", "BXD6", "BXD8", "BXD9", "BXD11", "BXD12"] + .iter() + .enumerate() + .map(|(index, name)| serde_json::json!({"name": name, "value": index as f64 + 1.0})) + .collect(); + let body = serde_json::json!({"group": "BXD", "trait_name": "plot-check", "samples": samples}); + + // The mapping suggests the plot; the suggestion is runnable as it stands. + let response = post_json_with_token( + "POST /compute/rqtl", + "/compute/rqtl", + &token, + &body, + ) + .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(); + let plot_action = envelope["actions"] + .as_array() + .unwrap() + .iter() + .find(|action| action["href"] == "/plot/rqtl") + .expect("a plot suggestion") + .clone(); + + let response = post_json_with_token( + "POST /plot/rqtl", + "/plot/rqtl", + &token, + &plot_action["body"], + ) + .await; + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.headers().get("content-type").unwrap(), + "image/svg+xml; charset=utf-8" + ); + // The plotter's one-line peak summary rides along as a header. + let summary = response + .headers() + .get("x-plot-summary") + .expect("x-plot-summary") + .to_str() + .unwrap() + .to_string(); + assert!(summary.contains("peak"), "{summary}"); + let svg = response.into_body().collect().await.unwrap().to_bytes(); + let text = String::from_utf8_lossy(&svg); + assert!(text.starts_with(""), "a complete SVG"); +} + +#[tokio::test] +async fn plotting_refuses_an_invalid_mapping_request_like_the_mapping_does() { + let Some(token) = auth_token().await else { + eprintln!("skipping plotting_refuses_an_invalid_mapping_request_like_the_mapping_does: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)"); + return; + }; + // No group and no genotype_file: the mapping handler's own 422, before + // any plotting happens. + let body = serde_json::json!({"trait_name": "t", "samples": []}); + let response = post_json_with_token("POST /plot/rqtl", "/plot/rqtl", &token, &body).await; + assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY); + let bytes = response.into_body().collect().await.unwrap().to_bytes(); + let error: Value = serde_json::from_slice(&bytes).unwrap(); + assert_eq!(error["code"], "missing_genotype"); +} -- cgit 1.4.1