//! `GET /groups/{group}/genotypes/{version}/frequencies`, its single-marker //! form, and the `unlisted` namespace for files no group lists. //! //! The tally is the one place the API says how a panel is actually called, so //! the tests pin the shape (a call -> frequency map that sums to 1), that the //! marker route and `?marker=` agree, and that a wrong version is refused //! with the versions that exist rather than an empty tally. use axum::http::StatusCode; use http_body_util::BodyExt; use serde_json::Value; mod common; async fn ready() -> Option { if !common::db_available() { eprintln!("skipping: no usable database"); return None; } common::auth_token().await } fn sum(map: &Value) -> f64 { map.as_object() .expect("the data is a call -> frequency map") .values() .map(|value| value.as_f64().expect("frequencies are numbers")) .sum() } #[tokio::test] async fn a_panel_reports_the_frequencies_of_its_calls() { let Some(token) = ready().await else { return }; let response = common::get_with_token( "GET /groups/BXD/genotypes/BXD.4/frequencies", "/groups/BXD/genotypes/BXD.4/frequencies", &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let frequencies: Value = common::data(&bytes); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); let calls = frequencies.as_object().expect("a map of calls").clone(); assert!( calls.keys().all(|call| call.len() <= 2), "calls as the file spells them, not positions: {:?}", calls.keys().collect::>() ); assert!((sum(&frequencies) - 1.0).abs() < 1e-5, "{}", sum(&frequencies)); // A panel typed as riset is called with its two parental codes plus het. assert!(calls.contains_key("B"), "parental B is a call: {calls:?}"); assert!(calls.contains_key("D"), "parental D is a call: {calls:?}"); let notes = envelope["notes"].as_array().unwrap(); assert!( notes .iter() .any(|note| note.as_str().unwrap().contains("marker(s)")), "the counts are in notes: {notes:?}" ); } #[tokio::test] async fn the_marker_route_and_the_marker_parameter_agree() { let Some(token) = ready().await else { return }; let marker = "rs6269442"; let in_path = common::get_with_token( "GET /groups/BXD/genotypes/BXD.4/markers/{marker}/frequencies", &format!("/groups/BXD/genotypes/BXD.4/markers/{marker}/frequencies"), &token, ) .await; assert_eq!(in_path.status(), StatusCode::OK); let bytes = in_path.into_body().collect().await.unwrap().to_bytes(); let path_data: Value = common::data(&bytes); let path_envelope: Value = serde_json::from_slice(&bytes).unwrap(); let as_query = common::get_with_token( "GET /groups/BXD/genotypes/BXD.4/frequencies?marker=", &format!("/groups/BXD/genotypes/BXD.4/frequencies?marker={marker}"), &token, ) .await; assert_eq!(as_query.status(), StatusCode::OK); let bytes = as_query.into_body().collect().await.unwrap().to_bytes(); let query_data: Value = common::data(&bytes); assert_eq!( path_data, query_data, "one marker, two ways to ask for it" ); // Frequencies are rounded to six decimal places, so a three-call marker // can sum to 0.999999. assert!((sum(&path_data) - 1.0).abs() < 1e-5, "{}", sum(&path_data)); let notes = path_envelope["notes"].as_array().unwrap(); let first = notes[0].as_str().unwrap(); assert!(first.contains(marker), "{first}"); assert!(first.contains("chr"), "the marker's position is reported: {first}"); } #[tokio::test] async fn a_marker_that_is_not_there_is_a_404() { let Some(token) = ready().await else { return }; let response = common::get_with_token( "GET /groups/BXD/genotypes/BXD.4/markers/nonesuch/frequencies", "/groups/BXD/genotypes/BXD.4/markers/nonesuch/frequencies", &token, ) .await; assert_eq!(response.status(), StatusCode::NOT_FOUND); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let error: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(error["code"], "genotype_not_found"); assert!( error["detail"].as_str().unwrap().contains("nonesuch"), "{error}" ); } #[tokio::test] async fn a_version_the_group_does_not_offer_names_the_ones_it_does() { let Some(token) = ready().await else { return }; let response = common::get_with_token( "GET /groups/BXD/genotypes/nonesuch/frequencies", "/groups/BXD/genotypes/nonesuch/frequencies", &token, ) .await; assert_eq!(response.status(), StatusCode::NOT_FOUND); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let error: Value = serde_json::from_slice(&bytes).unwrap(); let detail = error["detail"].as_str().unwrap(); assert!(detail.contains("available:"), "{detail}"); } /// A 3.7 GB panel is refused instead of read: the request that produced this /// limitation sat in `read_to_string` for ten minutes. #[tokio::test] async fn an_oversized_panel_is_refused_with_another_call_to_make() { let Some(token) = common::auth_token().await else { return }; let response = common::get_with_token( "GET /groups/unlisted/genotypes/HSNIH-Palmer_true/frequencies", "/groups/unlisted/genotypes/HSNIH-Palmer_true/frequencies", &token, ) .await; match response.status() { StatusCode::PAYLOAD_TOO_LARGE => { let bytes = response.into_body().collect().await.unwrap().to_bytes(); let error: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(error["code"], "genotype_too_large"); assert!( error["detail"].as_str().unwrap().contains("one marker"), "the refusal says what to do instead: {error}" ); } // This deployment may not have that panel at all. StatusCode::NOT_FOUND => eprintln!("skipping: the 3.7 GB panel is not deployed"), other => panic!("unexpected status {other}"), } } /// Per-class trait summaries: the question that follows "why do two engines /// peak at different markers". The classes are joined to a trait's values /// inside the server, so the test also pins that no per-animal genotype /// leaves the API. #[tokio::test] async fn a_marker_summarises_a_trait_per_genotype_class() { let Some(token) = ready().await else { return }; let marker = "JAX00418132"; let response = common::get_with_token( "GET .../markers/{marker}/classes", &format!("/groups/DOL/genotypes/DOL/markers/{marker}/classes?trait=10003"), &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); let classes = envelope["data"].as_object().expect("class -> stats"); // DOL's file spells its calls B/A/H. for class in ["A", "H", "B"] { let stats = &classes[class]; for field in ["n", "mean", "sd", "min", "max"] { assert!(stats.get(field).is_some(), "{class} has {field}: {stats}"); } } // The whole point: this marker's A class is two animals, and they are the // trait's two lowest values -- which is why an engine that keeps the 20 // hets sees a peak an engine that drops them cannot. assert_eq!(classes["A"]["n"], 2, "{}", envelope["data"]); let a_mean = classes["A"]["mean"].as_f64().unwrap(); let b_mean = classes["B"]["mean"].as_f64().unwrap(); assert!(a_mean < b_mean, "A below B: {a_mean} vs {b_mean}"); assert!(classes["H"]["n"].as_u64().unwrap() > 10); // A small class is called out: a peak resting on two animals is not the // same claim as one resting on a hundred. let notes: Vec<&str> = envelope["notes"] .as_array() .unwrap() .iter() .map(|note| note.as_str().unwrap()) .collect(); assert!( notes .iter() .any(|note| note.contains("smallest class") && note.contains("sensitive")), "{notes:?}" ); assert!( notes.iter().any(|note| note.contains("not served")), "the reply says genotypes stay inside: {notes:?}" ); // The values are statistics, not per-animal rows: nothing in the payload // is an animal name. let body = envelope["data"].to_string(); assert!(!body.contains("TB0"), "no individual names in the payload: {body}"); // A trait the group does not have is a 404, not an empty summary. let response = common::get_with_token( "GET .../classes?trait=", &format!("/groups/DOL/genotypes/DOL/markers/{marker}/classes?trait=999999"), &token, ) .await; assert_eq!(response.status(), StatusCode::NOT_FOUND); }