diff options
| author | Pjotr <wrk@bosa.lan> | 2026-11-07 17:35:44 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-11-07 17:35:44 +0000 |
| commit | 632cf0c61ee4602ee8423593a8aa8fd8a4373c41 (patch) | |
| tree | ee81f60e0211f510969ac8122ac496048c5db87d /tests | |
| parent | 555fe06b0ab72240f254206f0f0c4ddf76ec00c5 (diff) | |
| download | genenetwork4-632cf0c61ee4602ee8423593a8aa8fd8a4373c41.tar.gz | |
phewas: every trait peaking within a window of one position
PublishXRef records the marker each published trait's QTL peaks at
(Locus) with its LRS, which is a PheWAS waiting to happen: fix a
position, find the markers within a window of it, and ask which traits
peak at those markers.
GET /groups/{group}/phewas?locus=rs30524230&window=10000
GET /groups/{group}/phewas?chr=9&mb=29.940961&window=10000
The window is in base pairs, centred, default 10 kb either side -- the
"10K up and downstream" case. Positions come from the group's genotype
marker table (the endpoint added last round), so `genotype_version`
selects whose markers define the window and the answer reports them
alongside the traits: an empty `traits` list is then readable as
"nothing peaks that close" rather than a failure, and a ten-times wider
window is offered as `next`.
Measured on real data, ±10 kb around rs30524230 (chr 9, 29.94 Mb, a BXD
QTL peak at LRS 460): 4 markers, 10 traits, from LOD 99.9 down to 2.68 --
ANPEP_band, then microbiome/haematology/infectious-disease traits. That
cluster of 99.9s is the 460 LRS cap, not a spectrum, which the notes say.
* src/db.rs (get_phewas_traits): PublishXRef rows for a set of peak
markers, joining Phenotype/Publication through the existing
trait_info_sql, strongest first; marker names are re-validated before
they reach the IN list.
* src/routes/phewas.rs: the endpoint, token-gated; window/centre
validation (422 for none or negative), 404 for an unknown locus, notes
for the empty cases, and a unit test for the version/group helper.
* tests/phewas.rs: markers in the window, traits sorted by LRS with
lod = lrs/4.604, every hit peaking at a listed marker, zero-width window,
the two failure modes, and that the suggested wider window resolves.
* README.md, docs/www/ai-instruction.md (new 4b: how to read a PheWAS),
docs/www/operator-checklist.md: the endpoint and its reading.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/phewas.rs | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/tests/phewas.rs b/tests/phewas.rs new file mode 100644 index 0000000..7e31c74 --- /dev/null +++ b/tests/phewas.rs @@ -0,0 +1,146 @@ +//! `GET /groups/{group}/phewas` -- traits peaking within a window of one +//! position. +//! +//! Needs the database (traits), a token, and the deployment's genotype +//! files (the marker table that defines the window); skips without any of +//! them, like the other database-backed tests. + +use axum::http::StatusCode; +use http_body_util::BodyExt; +use serde_json::Value; + +mod common; + +use common::{auth_token, db_available, get_with_token}; + +fn genotype_dir_present() -> bool { + let directory = genenetwork4::config::Config::load().genotype_files_dir; + std::path::Path::new(&directory).is_dir() +} + +async fn get_json(uri: &str, token: &str) -> (StatusCode, Value) { + let response = get_with_token(&format!("GET {uri}"), uri, token).await; + let status = response.status(); + let bytes = response.into_body().collect().await.unwrap().to_bytes(); + ( + status, + serde_json::from_slice(&bytes).unwrap_or(Value::Null), + ) +} + +#[tokio::test] +async fn a_window_around_a_qtl_peak_lists_the_traits_peaking_there() { + if !db_available() { + eprintln!("skipping a_window_around_a_qtl_peak_lists_the_traits_peaking_there: no usable database (check SQL_URI credentials)"); + return; + } + let Some(token) = auth_token().await else { + eprintln!("skipping a_window_around_a_qtl_peak_lists_the_traits_peaking_there: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)"); + return; + }; + if !genotype_dir_present() { + eprintln!("skipping a_window_around_a_qtl_peak_lists_the_traits_peaking_there: no genotype-files directory (GENOTYPE_FILES)"); + return; + } + + // 1. Centred on a marker: rs30524230 is a real BXD QTL peak on chr 9 + // (PublishXRef.Locus, LRS 460). Made a qtl by hand in the data. + let (status, body) = get_json( + "/groups/BXD/phewas?locus=rs30524230&window=10000", + &token, + ) + .await; + assert_eq!(status, StatusCode::OK, "{body}"); + let data: Value = common::data(&serde_json::to_vec(&body).unwrap()); + let centre = &data["centre"]; + let centre_mb = centre["mb"].as_f64().expect("a centre in Mb"); + assert_eq!(centre["locus"], "rs30524230"); + assert_eq!(data["window_bp"].as_f64().unwrap(), 10000.0); + + let markers = data["markers"].as_array().expect("markers"); + assert!(!markers.is_empty(), "a window at a QTL peak has markers: {body}"); + let names: Vec<&str> = markers + .iter() + .map(|marker| marker["name"].as_str().unwrap()) + .collect(); + assert!(names.contains(&"rs30524230"), "{names:?}"); + for marker in markers { + let mb = marker["mb"].as_f64().unwrap(); + assert!( + (mb - centre_mb).abs() <= 0.01, + "every marker is in the window: {marker} vs {centre_mb}" + ); + } + + let traits = data["traits"].as_array().expect("traits"); + assert!(!traits.is_empty(), "traits peak in this window: {body}"); + let mut previous = f64::INFINITY; + for hit in traits { + let lrs = hit["lrs"].as_f64().unwrap(); + assert!(lrs <= previous, "strongest first: {lrs} after {previous}"); + previous = lrs; + assert_eq!(hit["chr"], "9", "{hit}"); + assert!( + (hit["mb"].as_f64().unwrap() - centre_mb).abs() <= 0.01, + "{hit}" + ); + let locus = hit["locus"].as_str().unwrap(); + assert!(names.contains(&locus), "peaks at a window marker: {hit}"); + // lod = lrs/4.604, the same rule as everywhere else. + assert!( + (hit["lod"].as_f64().unwrap() * 4.604 - lrs).abs() < 0.01, + "{hit}" + ); + } + + // 2. The same window given as chr+mb, and a zero-width window, which + // keeps only markers exactly at the position. + let (status, by_position) = get_json( + &format!("/groups/BXD/phewas?chr=9&mb={centre_mb}&window=0"), + &token, + ) + .await; + assert_eq!(status, StatusCode::OK, "{by_position}"); + let narrow: Value = common::data(&serde_json::to_vec(&by_position).unwrap()); + assert!( + narrow["markers"].as_array().unwrap().len() <= names.len(), + "a zero-width window cannot hold more markers: {by_position}" + ); + + // 3. Failures: no position at all, and a marker that is not there. + let (status, body) = get_json("/groups/BXD/phewas?window=10000", &token).await; + assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "{body}"); + assert_eq!(body["code"], "missing_position"); + + let (status, body) = get_json("/groups/BXD/phewas?locus=not-a-marker", &token).await; + assert_eq!(status, StatusCode::NOT_FOUND, "{body}"); + + // 4. A position with nothing near it is a 200 with an explanation, + // not an error. + let (status, body) = get_json("/groups/BXD/phewas?chr=9&mb=195.9&window=100", &token).await; + assert_eq!(status, StatusCode::OK, "{body}"); + let empty: Value = common::data(&serde_json::to_vec(&body).unwrap()); + if empty["markers"].as_array().unwrap().is_empty() { + assert!( + body["notes"] + .as_array() + .unwrap() + .iter() + .any(|note| note.as_str().unwrap().contains("No marker")), + "an empty window says why: {body}" + ); + } + + // 5. The suggested wider window resolves. + let (status, body) = get_json("/groups/BXD/phewas?locus=rs30524230&window=10000", &token).await; + assert_eq!(status, StatusCode::OK); + let wider = body["next"] + .as_array() + .unwrap() + .iter() + .find(|next| next["href"].as_str().unwrap().contains("window=")) + .expect("a wider-window suggestion"); + let href = wider["href"].as_str().unwrap(); + let (status, _) = get_json(href, &token).await; + assert_eq!(status, StatusCode::OK, "the suggested window resolves: {href}"); +} |
