//! `POST /plot/rqtl` and `POST /plot/gemma`: an SVG back, not an envelope. //! //! They take a **posted mapping result**, so they need no engine at all: //! the plotter scripts (`RQTL_PLOTTER`, `GEMMA_PLOTTER`, run by //! `PLOT_PYTHON`) are the only requirement, and a canned result exercises //! the whole path. 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"]) } fn canned_rqtl_result() -> Value { serde_json::json!({ "data": { "markers": [ ["rs1", "1", 3.1, 2.4], ["rs2", "1", 8.2, 1.1], ["rs3", "2", 12.0, 4.2], ["rs4", "2", 19.5, 0.3], ["rs5", "X", 30.0, 1.8] ], "permutation": {"values": [1.0, 1.2], "suggestive": 2.5, "significant": 3.5} }, "notes": ["Genotype: prepared panel subset x (version BXD.8, 198 individuals, 0 parents/F1s dropped); 5 of 5 supplied values aligned onto it by name."] }) } fn canned_gemma_result() -> Value { serde_json::json!({ "meta": {"state": "complete", "took_ms": 12, "request_id": "canned", "columns": ["chr", "rs", "ps", "n_miss", "allele1", "allele0", "af", "beta", "se", "l_mle", "p_lrt"], "num_rows": 3, "num_cols": 11}, "data": [ [1.0, "rs1", 3.1, 0.0, "X", "Y", 0.2, 1.0, 2.0, 3.0, 0.5], [1.0, "rs2", 8.2, 0.0, "X", "Y", 0.2, 1.0, 2.0, 3.0, 0.001], [2.0, "rs3", 12.0, 0.0, "X", "Y", 0.2, 1.0, 2.0, 3.0, 0.3] ], "notes": ["Trait: BXD::20010"] }) } #[tokio::test] async fn a_posted_rqtl_result_is_drawn_and_the_suggestion_says_so() { if !plotter_present("rqtl") { eprintln!( "skipping a_posted_rqtl_result_is_drawn_and_the_suggestion_says_so: needs the \ plotter script (RQTL_PLOTTER) and PLOT_PYTHON" ); return; } let Some(token) = auth_token().await else { eprintln!("skipping a_posted_rqtl_result_is_drawn_and_the_suggestion_says_so: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)"); return; }; let response = post_json_with_token( "POST /plot/rqtl", "/plot/rqtl?trait=BXDPublish::10001", &token, &canned_rqtl_result(), ) .await; assert_eq!(response.status(), StatusCode::OK); assert_eq!( response.headers().get("content-type").unwrap(), "image/svg+xml; charset=utf-8" ); let summary = response .headers() .get("x-plot-summary") .expect("x-plot-summary") .to_str() .unwrap() .to_string(); assert!(summary.contains("peak rs3"), "the peak is the strongest LOD: {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"); assert!(text.contains("BXDPublish::10001"), "the title carries the trait label"); // Just the `data` member works too. let response = post_json_with_token( "POST /plot/rqtl", "/plot/rqtl", &token, &canned_rqtl_result()["data"], ) .await; assert_eq!(response.status(), StatusCode::OK); // GEMMA's plotter reads `meta.columns`, so a bare `data` is refused // with a message saying to post the whole response. let response = post_json_with_token( "POST /plot/gemma", "/plot/gemma", &token, &canned_gemma_result()["data"], ) .await; assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY); } #[tokio::test] async fn a_posted_gemma_result_is_drawn() { if !plotter_present("gemma") { eprintln!("skipping a_posted_gemma_result_is_drawn: needs the plotter script (GEMMA_PLOTTER) and PLOT_PYTHON"); return; } let Some(token) = auth_token().await else { eprintln!("skipping a_posted_gemma_result_is_drawn: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)"); return; }; let response = post_json_with_token( "POST /plot/gemma", "/plot/gemma?trait=BXDPublish::10001", &token, &canned_gemma_result(), ) .await; assert_eq!(response.status(), StatusCode::OK); let summary = response .headers() .get("x-plot-summary") .expect("x-plot-summary") .to_str() .unwrap() .to_string(); assert!(summary.contains("rs2"), "the smallest p_lrt: {summary}"); let svg = response.into_body().collect().await.unwrap().to_bytes(); let text = String::from_utf8_lossy(&svg); assert!(text.starts_with(" = 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 { svg.split(" Trait1"), "{query}: summary {summary:?}"); } } #[tokio::test] async fn a_body_that_is_not_a_bnw_result_is_a_422() { let Some(token) = auth_token().await else { eprintln!("skipping a_body_that_is_not_a_bnw_result_is_a_422: set GN4_TEST_AUTH_TOKEN"); return; }; for (posted, expected) in [ (serde_json::json!({"data": [], "meta": {"columns": []}}), "not_a_bnw_result"), ( serde_json::json!({"data": [["m1", 1, 2.0]], "meta": {"columns": ["marker", "chr", "lod"]}}), "not_a_bnw_result", ), ( serde_json::json!({ "data": [["A", "B", 1.0, true]], "meta": {"columns": ["from", "to", "posterior", "in_structure"]} }), "invalid_kind", // with ?kind=nonsense below ), ][..2] .iter() .cloned() .chain(std::iter::once(( serde_json::json!({ "data": [["A", "B", 1.0, true]], "meta": {"columns": ["from", "to", "posterior", "in_structure"]} }), "invalid_kind", ))) { let path = if expected == "invalid_kind" { "/plot/bnw?kind=nonsense".to_string() } else { "/plot/bnw".to_string() }; let response = post_json_with_token("POST /plot/bnw", &path, &token, &posted).await; let status = response.status(); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let reply: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "{reply}"); assert_eq!(reply["code"], expected, "{reply}"); } }