From b3811de4f2fb0e1d65ade757ad3ea992eb3f9f67 Mon Sep 17 00:00:00 2001 From: Pjotr Date: Tue, 10 Nov 2026 22:35:45 +0000 Subject: traits: genotypes at a confirmed peak, and the plot of them Returning genotypes is the one thing this API otherwise never does, so it is fenced and the fence is the feature: GET /groups/{group}/traits/{trait_id}/peak-genotypes?marker= * the server re-runs the R/qtl scan for the trait -- the same machinery and gate as /compute/rqtl -- and takes the peak from that run, rather than trusting a client that names a marker; * the marker must lie within ?window= Mb of that peak (default 10), or the answer is 422 not_the_peak quoting the peak's marker, chromosome, Mb and LOD; * the peak must reach ?min_lod= (default 3), or 422 not_a_hit; * only animals that have a value for the trait are listed, and only this marker's calls. Nothing else of the matrix leaves. On DOL 10003 that is UNCHS026345 (chr9 75.76 Mb, LOD 5.86) with 192 animals across A 110 / B 8 / H 74 -- the two a class of eight being exactly the thing worth seeing. POST /plot/genotypes draws R/qtl's Fig 4.1 from it: post the reply's `animals` (or its `genotypes` map) with the trait's values and get an SVG with one point per animal, genotype class across, value up, class means marked. Nothing is recomputed -- the plot is of the numbers the client sends, like the other plots. New script scripts/plot-genotypes.py (stdlib-only SVG, like its siblings) and the GENOTYPE_PLOTTER key. tests/peakgenotypes.rs: the peak's calls for animals with values (with the counts agreeing with the rows), the SVG, and a marker on another chromosome refused with the peak quoted. Also keeps /compute/rqtl's error order: naming both a group and a file is "ambiguous" even when the request also forgot its values. --- src/routes/plot.rs | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) (limited to 'src/routes/plot.rs') diff --git a/src/routes/plot.rs b/src/routes/plot.rs index 00119dc..f386526 100644 --- a/src/routes/plot.rs +++ b/src/routes/plot.rs @@ -299,3 +299,139 @@ mod tests { ); } } + +#[derive(Deserialize, utoipa::ToSchema)] +pub struct GenotypePlot { + /// The marker the calls are at, for the title. + pub marker: String, + /// Individual -> genotype call, as the peak-genotypes reply gives it. The + /// short form (`{"BXD1": "B"}`) or the reply's `animals` rows both work. + #[schema(value_type = Object)] + pub genotypes: serde_json::Value, + /// The trait's values: `/groups/{group}/traits/{id}/values` rows, or a + /// name -> value map. + #[schema(value_type = Object)] + pub values: serde_json::Value, +} + +/// Points as `{individual, genotype, value}` from the two shapes a caller has: +/// the peak-genotypes reply and the trait's values. +fn genotype_points( + genotypes: &serde_json::Value, + values: &serde_json::Value, +) -> Vec { + let mut calls: std::collections::BTreeMap = std::collections::BTreeMap::new(); + match genotypes { + serde_json::Value::Object(map) => { + for (name, call) in map { + if let Some(call) = call.as_str() { + calls.insert(name.clone(), call.to_string()); + } + } + } + serde_json::Value::Array(rows) => { + for row in rows { + if let (Some(name), Some(call)) = ( + row.get("individual").and_then(|v| v.as_str()), + row.get("genotype").and_then(|v| v.as_str()), + ) { + calls.insert(name.to_string(), call.to_string()); + } + } + } + _ => {} + } + let mut numbers: std::collections::BTreeMap = std::collections::BTreeMap::new(); + match values { + serde_json::Value::Object(map) => { + for (name, value) in map { + if let Some(value) = value.as_f64() { + numbers.insert(name.clone(), value); + } + } + } + serde_json::Value::Array(rows) => { + for row in rows { + if let (Some(name), Some(value)) = ( + row.get("name").and_then(|v| v.as_str()), + row.get("value").and_then(|v| v.as_f64()), + ) { + numbers.insert(name.to_string(), value); + } + } + } + _ => {} + } + calls + .into_iter() + .filter_map(|(individual, genotype)| { + let value = *numbers.get(&individual)?; + Some(serde_json::json!({ + "individual": individual, + "genotype": genotype, + "value": value, + })) + }) + .collect() +} + +#[utoipa::path(post, path = "/plot/genotypes", + summary = "Draw a trait by genotype class at one marker, as an SVG", + description = "Post the data from `/groups/{group}/traits/{id}/peak-genotypes` with the \ +trait's values (from `/groups/{group}/traits/{id}/values`) and get R/qtl's Fig 4.1 drawn: one \ +point per animal, x its genotype at the marker, y its value, with the class means. Nothing is \ +recomputed -- the plot is of the numbers you send. image/svg+xml, not the JSON envelope.", + request_body = GenotypePlot, + responses( + (status = 200, description = "The plot, as image/svg+xml", body = String, content_type = "image/svg+xml"), + Unauthorized, + (status = 422, description = "No point has both a call and a value", body = ApiError), + (status = 502, description = "The plotter could not run, or failed", body = ApiError) + ) +)] +#[gnauth(token_only)] +pub async fn plot_genotypes( + ctx: Ctx, + _token: AuthToken, + Json(request): Json, +) -> Result { + let rid = ctx.request_info(); + let points = genotype_points(&request.genotypes, &request.values); + if points.is_empty() { + return Err(ApiError::new( + StatusCode::UNPROCESSABLE_ENTITY, + "no_points", + "Nothing to plot", + "no individual has both a genotype call and a value; post the peak-genotypes \ + reply's `animals` (or its `genotypes`) together with the trait's values." + .to_string(), + "plot_genotypes", + ) + .with_request_id(rid)); + } + let config = Config::load(); + let document = serde_json::json!({ + "marker": request.marker, + "points": points, + }); + let rendered = plot::render( + &config.plot_python, + &config.genotype_plotter, + std::path::Path::new(&config.rqtl_cache_dir), + &document, + &request.marker, + &[], + ) + .await + .map_err(|error| { + ApiError::new( + StatusCode::BAD_GATEWAY, + "plot_failed", + "Could not draw the plot", + error.to_string(), + "plot_genotypes", + ) + .with_request_id(rid.clone()) + })?; + Ok(svg_response(rendered)) +} -- cgit 1.4.1