diff options
| author | Pjotr <wrk@bosa.lan> | 2026-11-10 12:53:06 +0000 |
|---|---|---|
| committer | Pjotr <wrk@bosa.lan> | 2026-11-10 12:53:06 +0000 |
| commit | 7fa899b66f484593e36b175dc6daa5be1d03f2fd (patch) | |
| tree | ff6d341118a542be14462b2fdf44221fb60f1c24 /tests | |
| parent | 2b41fc94a5e2b33a411d22721a6ee2a97ac9ae60 (diff) | |
| download | genenetwork4-7fa899b66f484593e36b175dc6daa5be1d03f2fd.tar.gz | |
probesets: cross-dataset lookups by probeset id and by gene symbol
A probeset id is not a Xapian field, so /search cannot find 1427571_at
exactly and the only route to it was guessing a dataset and calling
/datasets/{ds}/probesets/{name} until one answered. A gene symbol could
be searched, but the reply was capped at 100 hits, interleaved probesets
and platforms, and carried no total -- so "did I see all of Shh?" had no
answer. Both questions are one indexed query each: ProbeSet.Name_IDX and
symbol_IDX.
* GET /probesets/{name}: one row per dataset that carries the probeset,
with the panel (group, species, platform) and the scan's statistics
(mean, LRS, LOD, locus, additive). Suffixed chip variants match too
-- 1427571_at also finds 1427571_at_A -- and each row's `name` says
which matched, so the answer never hides a substitution. LIKE's
wildcards are escaped, or every Affymetrix `_at` would match any
character. 155 rows over 155 datasets in 32 ms.
* GET /genes/{symbol}/traits: the same rows filtered on Symbol, with a
real total, ?sort=lod, ?min_lod=, ?group=, ?species=, ?platform= and
paging -- "BXD datasets where Shh is a strong eQTL" is one call.
1225 rows over 17 datasets and 13 probesets in 38 ms.
* Both serve metadata, not values. A token is enough for the index;
trait values stay per-dataset and authorized per dataset, and `next`
goes to /datasets/{ds}/probesets/{name}/values, which checks. There
is no cross-dataset values endpoint, deliberately: that is bulk work
and /groups/{g}/traits is the bulk call.
The one-statement join took 5.2 s for Shh -- MySQL rescans
InbredSet/Species per row -- so the 933 ProbeSetFreeze rows and the 102
panels are fetched separately and joined in memory: 70 ms each. nsamples
is left out: it is a ProbeSetData count per row (+0.5 s) and the dataset
record already reports strains.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/probesets.rs | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/tests/probesets.rs b/tests/probesets.rs new file mode 100644 index 0000000..6c4ce9d --- /dev/null +++ b/tests/probesets.rs @@ -0,0 +1,147 @@ +//! `GET /probesets/{name}` and `GET /genes/{symbol}/traits`: the cross-dataset +//! lookups the search index cannot do (a probeset id is not a Xapian field, +//! and a symbol search is capped and ungrouped). +//! +//! Both return metadata only -- which datasets carry the probeset and what +//! their scans found -- so the tests pin the row shape, the totals, the +//! filters and the fact that every link out of them is followable. + +use axum::http::StatusCode; +use http_body_util::BodyExt; +use serde_json::Value; + +mod common; + +async fn ready() -> Option<String> { + if !common::db_available() { + eprintln!("skipping: no usable database"); + return None; + } + common::auth_token().await +} + +#[tokio::test] +async fn a_probeset_resolves_to_its_datasets() { + let Some(token) = ready().await else { return }; + let response = common::get_with_token( + "GET /probesets/{name}", + "/probesets/1427571_at?limit=5", + &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 columns: Vec<&str> = envelope["meta"]["columns"] + .as_array() + .unwrap() + .iter() + .map(|column| column.as_str().unwrap()) + .collect(); + for expected in [ + "dataset", "group", "species", "platform", "name", "symbol", "chr", "mb", "mean", "lrs", + "lod", "locus", "additive", + ] { + assert!(columns.contains(&expected), "{expected} in {columns:?}"); + } + let rows = envelope["data"].as_array().unwrap(); + assert_eq!(rows.len(), 5, "the page size is respected"); + let name = columns.iter().position(|column| *column == "name").unwrap(); + let symbol = columns.iter().position(|column| *column == "symbol").unwrap(); + assert!( + rows.iter().any(|row| row[name] + .as_str() + .unwrap() + .starts_with("1427571_at")), + "the rows are the probeset asked for: {rows:?}" + ); + // 1427571_at is Shh; a row that matched a suffixed variant says so itself. + assert!( + rows.iter() + .any(|row| row[symbol].as_str() == Some("Shh")), + "the annotation travels with the row: {rows:?}" + ); + // The totals are in notes, which is the point of the endpoint. + let notes = envelope["notes"].as_array().unwrap(); + let first = notes[0].as_str().unwrap(); + assert!(first.contains("dataset(s)"), "{first}"); + assert!(first.contains("1427571_at"), "{first}"); + // And it says where the values (which it does not serve) live. + let links: Vec<&str> = envelope["next"] + .as_array() + .unwrap() + .iter() + .map(|action| action["href"].as_str().unwrap()) + .collect(); + assert!( + links + .iter() + .any(|href| href.contains("/datasets/") && href.ends_with("/values")), + "the values are one link away: {links:?}" + ); +} + +#[tokio::test] +async fn a_symbol_lists_its_traits_with_a_real_total() { + let Some(token) = ready().await else { return }; + let response = common::get_with_token( + "GET /genes/{symbol}/traits", + "/genes/Shh/traits?sort=lod&min_lod=5&limit=5", + &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 rows = envelope["data"].as_array().unwrap(); + assert_eq!(rows.len(), 5); + let columns: Vec<&str> = envelope["meta"]["columns"] + .as_array() + .unwrap() + .iter() + .map(|column| column.as_str().unwrap()) + .collect(); + let lod = columns.iter().position(|column| *column == "lod").unwrap(); + // ?sort=lod means the page starts at the strongest peak, and ?min_lod=5 + // means nothing weaker is in it. + let lods: Vec<f64> = rows.iter().map(|row| row[lod].as_f64().unwrap()).collect(); + assert!(lods[0] >= lods[lods.len() - 1], "sorted by lod: {lods:?}"); + assert!(lods.iter().all(|value| *value >= 5.0), "{lods:?}"); + let notes = envelope["notes"].as_array().unwrap(); + let first = notes[0].as_str().unwrap(); + assert!(first.contains("expression trait row(s)"), "{first}"); + assert!(first.contains("after the filters"), "a filter count: {first}"); + + // An unknown symbol is a 404, not an empty list. + let response = + common::get_with_token("GET /genes/{symbol}/traits", "/genes/NOSUCHGENE/traits", &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"], "gene_not_found"); +} + +#[tokio::test] +async fn the_filters_narrow_a_gene_to_one_group() { + let Some(token) = ready().await else { return }; + let all = common::get_with_token("GET /genes/{symbol}/traits", "/genes/Shh/traits?limit=1", &token).await; + assert_eq!(all.status(), StatusCode::OK); + let bytes = all.into_body().collect().await.unwrap().to_bytes(); + let all: Value = serde_json::from_slice(&bytes).unwrap(); + let everything = all["notes"][0].as_str().unwrap().to_string(); + + let one = common::get_with_token( + "GET /genes/{symbol}/traits?group=", + "/genes/Shh/traits?group=BXD&limit=1", + &token, + ) + .await; + assert_eq!(one.status(), StatusCode::OK); + let bytes = one.into_body().collect().await.unwrap().to_bytes(); + let one: Value = serde_json::from_slice(&bytes).unwrap(); + let narrowed = one["notes"][0].as_str().unwrap().to_string(); + assert_ne!(everything, narrowed, "the group filter changes the totals"); + let row = one["data"][0].as_array().unwrap(); + assert_eq!(row[1].as_str(), Some("BXD"), "every row is that group: {row:?}"); +} |
