diff options
| author | Pjotr <wrk@bosa.lan> | 2026-11-07 18:15:37 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-11-07 18:15:37 +0000 |
| commit | f9ba0f12e3bf52919fc363c037fbf0a0deda8c17 (patch) | |
| tree | f8ba4f4de75ffe45dd05183d066dc9e079ba6450 /src/plot.rs | |
| parent | b6c5538bc2bf1166ba176a0d80966c04229854ad (diff) | |
| download | genenetwork4-f9ba0f12e3bf52919fc363c037fbf0a0deda8c17.tar.gz | |
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).
Diffstat (limited to 'src/plot.rs')
| -rw-r--r-- | src/plot.rs | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/plot.rs b/src/plot.rs new file mode 100644 index 0000000..79edabc --- /dev/null +++ b/src/plot.rs @@ -0,0 +1,117 @@ +//! Running the plotter scripts (`scripts/plot-rqtl.py`, +//! `scripts/plot-gemma.py`) over a mapping response. +//! +//! The plotters take the **saved JSON response** of a mapping and write +//! an SVG, so this writes the envelope the compute endpoint just built to +//! a scratch file, runs the configured script over it, and hands back the +//! SVG text. Nothing is cached: a plot is cheap next to the mapping that +//! produced it, and the mapping itself is what gets cached. + +use std::path::Path; + +/// Why a plot could not be produced. +#[derive(Debug)] +pub enum PlotError { + /// Writing the response, or reading the SVG, failed. + Io(String), + /// The interpreter or script is missing, or the script failed. + Failed(String), +} + +impl std::fmt::Display for PlotError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + PlotError::Io(what) => write!(f, "{what}"), + PlotError::Failed(what) => write!(f, "{what}"), + } + } +} + +/// A plot, and the one-line summary the script printed. +#[derive(Debug)] +pub struct Plot { + pub svg: String, + pub summary: String, +} + +/// Run `script` over `response` (the JSON envelope of a mapping) and +/// return the SVG it wrote. +pub async fn render( + python: &str, + script: &str, + scratch_dir: &Path, + response: &serde_json::Value, + trait_label: &str, + extra_args: &[&str], +) -> Result<Plot, PlotError> { + std::fs::create_dir_all(scratch_dir) + .map_err(|e| PlotError::Io(format!("{}: {e}", scratch_dir.display())))?; + let unique = format!( + "{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_nanos()) + .unwrap_or(0) + ); + let json_path = scratch_dir.join(format!("{unique}-plot.json")); + let svg_path = scratch_dir.join(format!("{unique}-plot.svg")); + std::fs::write( + &json_path, + serde_json::to_vec(response).map_err(|e| PlotError::Io(e.to_string()))?, + ) + .map_err(|e| PlotError::Io(format!("{}: {e}", json_path.display())))?; + + let mut command = tokio::process::Command::new(python); + command + .arg(script) + .arg(&json_path) + .arg("-o") + .arg(&svg_path) + .arg("--trait") + .arg(trait_label) + .args(extra_args); + let output = command.output().await.map_err(|e| { + PlotError::Failed(format!( + "could not run {python} {script} (is the plotter configured? see PLOT_PYTHON, \ + RQTL_PLOTTER, GEMMA_PLOTTER): {e}" + )) + })?; + let summary = String::from_utf8_lossy(&output.stdout).trim().to_string(); + let svg = if output.status.success() { + std::fs::read_to_string(&svg_path) + .map_err(|e| PlotError::Io(format!("{}: {e}", svg_path.display())))? + } else { + let stderr = String::from_utf8_lossy(&output.stderr); + return Err(PlotError::Failed(format!( + "{script} failed: {}", + stderr.trim() + ))); + }; + let _ = std::fs::remove_file(&json_path); + let _ = std::fs::remove_file(&svg_path); + Ok(Plot { svg, summary }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn a_missing_script_is_a_failed_plot_not_a_panic() { + let dir = std::env::temp_dir().join(format!("gn4-plot-{}", std::process::id())); + let error = render( + "python3", + "scripts/does-not-exist.py", + &dir, + &serde_json::json!({"data": {}}), + "t", + &[], + ) + .await + .unwrap_err(); + assert!(matches!(error, PlotError::Failed(_)), "{error}"); + assert!(error.to_string().contains("failed"), "{error}"); + let _ = std::fs::remove_dir_all(&dir); + } +} |
