diff options
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)) +} |
