diff options
| author | Pjotr <wrk@bosa.lan> | 2026-11-10 22:35:45 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-11-10 22:35:45 +0000 |
| commit | b3811de4f2fb0e1d65ade757ad3ea992eb3f9f67 (patch) | |
| tree | aad2b1cb2b97f08e202f1931bcd590ecba408e56 /src/routes/plot.rs | |
| parent | 62cc5f73cb94fe85c31a2f177d1d150e643e84b1 (diff) | |
| download | genenetwork4-b3811de4f2fb0e1d65ade757ad3ea992eb3f9f67.tar.gz | |
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=<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.
Diffstat (limited to 'src/routes/plot.rs')
| -rw-r--r-- | src/routes/plot.rs | 136 |
1 files changed, 136 insertions, 0 deletions
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<serde_json::Value> { + let mut calls: std::collections::BTreeMap<String, String> = 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<String, f64> = 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<GenotypePlot>, +) -> Result<Response, ApiError> { + 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)) +} |
