From d9e1bf7fd9da4e0bae6579b95fd0028296b997ac Mon Sep 17 00:00:00 2001 From: Pjotr Date: Thu, 12 Nov 2026 17:15:53 +0000 Subject: compute/bnw: a Bayesian network over traits, called out to BNW's driver POST /compute/bnw learns a network over a set of variables and returns one row per candidate edge: from, to, posterior, in_structure. Inputs are published traits of a group (group + traits) or an explicit matrix (values: one entry per variable with per-sample values), which covers the case the database cannot -- a basket's columns, a handful of probe sets. BNW's knowledge stays in the gn-bnw repository. GN4 writes a variables x samples matrix, runs `bnw learn` (BNW_CMD, BNW_BIN_DIR, BNW_CACHE_DIR) and parses the reply: nothing about the three C programs, the private work directory or the type line in the input file lives here, so BNW's pipeline can change without a GN4 release. The driver's failures map onto the API's own: bad input 422, a missing driver 502 bnw_not_installed (its exit 77), a failed computation 502 bnw_failed in the driver's words. Decisions worth recording: * parameters are BNW's own defaults -- maxparents 4, k 1, tiers 3, threshold 0.5, read from its create_tiers_gom_part1.php rather than chosen here -- and the reply names the values used, because a network at 0.5 and one at 0.9 are different answers to the same question; * at most 40 variables and at least 3 samples complete across every variable. The first is the structure search's exponential in-degree; the second is BNW modelling a complete multivariate normal, so a sample missing any value is dropped -- and the note says how many survived rather than silently shrinking the panel; * its own gate, always claimed (threshold 0): there is no such thing as a small BNW run, the cost is the search, not the number of individuals; * the notes say plainly that a posterior is not a p-value and that a network is not a QTL, because that is the easiest thing to over-read. tests/bnw.rs compares the endpoint against the answer the BNW *web application* gives for its own six-trait example (the same fixture as the driver's parity check, gn-bnw/test/golden/): the five edges must match, and the notes must name the parameters and carry the caveat. It skips when the driver or its binaries are absent. Not deployed to production. --- tests/bnw.rs | 207 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 tests/bnw.rs (limited to 'tests') diff --git a/tests/bnw.rs b/tests/bnw.rs new file mode 100644 index 0000000..c26c1c8 --- /dev/null +++ b/tests/bnw.rs @@ -0,0 +1,207 @@ +//! `POST /compute/bnw`: a Bayesian network over a set of traits. +//! +//! The endpoint calls out to BNW's headless driver (the `bnw` program from the +//! gn-bnw repository). These tests use the six continuous traits of BNW's own +//! 8-node example -- the same fixture the driver's parity check compares +//! against the web application -- so "the endpoint works" means "the edges are +//! the ones BNW itself produces", not merely "a 200 came back". +//! +//! They skip when the driver is not installed, and also when the driver is +//! installed but its C programs are not: that is a deployment that has not +//! finished, not a bug here. + +use axum::http::StatusCode; +use http_body_util::BodyExt; +use serde_json::{json, Value}; + +mod common; + +/// True only when the BNW driver can actually run a network: the program +/// exists *and* the compiled C programs it needs are beside it. +fn bnw_available() -> bool { + let config = genenetwork4::config::Config::load(); + if std::process::Command::new(&config.bnw_cmd) + .arg("version") + .output() + .is_err() + { + return false; + } + // A driver with no binaries exits 77 ("skip"), which is exactly the + // condition this guard is for. + matches!( + std::process::Command::new(&config.bnw_cmd) + .args(["learn", "--input", "/dev/null"]) + .output(), + Ok(output) if output.status.code() != Some(77) + ) +} + +/// The six continuous traits of BNW's own example, as an explicit matrix. +fn six_traits() -> Value { + // The fixture lives in a sibling repository: /gn-bnw, while this + // checkout is /genenetwork/genenetwork4. GN_BNW_DIR overrides both. + let candidates = [ + std::env::var("GN_BNW_DIR") + .map(|dir| format!("{dir}/test/golden/six_traits_matrix.tsv")) + .unwrap_or_default(), + format!("{}/../../gn-bnw/test/golden/six_traits_matrix.tsv", env!("CARGO_MANIFEST_DIR")), + format!("{}/../gn-bnw/test/golden/six_traits_matrix.tsv", env!("CARGO_MANIFEST_DIR")), + ]; + let Some(text) = candidates + .iter() + .filter(|path| !path.is_empty()) + .find_map(|path| std::fs::read_to_string(path).ok()) + else { + return Value::Null; + }; + let mut lines = text.lines().filter(|line| !line.trim().is_empty()); + let Some(header) = lines.next() else { + return Value::Null; + }; + let names: Vec<&str> = header.split('\t').collect(); + let rows: Vec> = lines.map(|line| line.split('\t').collect()).collect(); + let mut variables = Vec::with_capacity(names.len()); + for (column, name) in names.iter().enumerate() { + let samples: Vec = rows + .iter() + .enumerate() + .map(|(index, row)| { + json!({"name": format!("s{index}"), "value": row[column].parse::().unwrap()}) + }) + .collect(); + variables.push(json!({"name": name, "samples": samples})); + } + Value::Array(variables) +} + +async fn post(body: Value, token: &str) -> (StatusCode, Value) { + let response = common::post_json_with_token( + "POST /compute/bnw", + "/compute/bnw", + token, + &body, + ) + .await; + let status = response.status(); + let bytes = response.into_body().collect().await.unwrap().to_bytes(); + (status, serde_json::from_slice(&bytes).unwrap()) +} + +#[tokio::test] +async fn a_network_reports_the_edges_bnw_finds() { + let Some(token) = common::auth_token().await else { + eprintln!("skipping a_network_reports_the_edges_bnw_finds: set GN4_TEST_AUTH_TOKEN"); + return; + }; + if !bnw_available() { + eprintln!( + "skipping a_network_reports_the_edges_bnw_finds: no BNW driver (set BNW_CMD / \ + BNW_BIN_DIR; see gn-bnw/var_lib_genenet_bnw/build.sh)" + ); + return; + } + let variables = six_traits(); + if variables.is_null() { + eprintln!("skipping a_network_reports_the_edges_bnw_finds: gn-bnw fixtures not found"); + return; + } + + let (status, body) = post( + json!({"values": variables, "trait_name": "golden-6-traits"}), + &token, + ) + .await; + assert_eq!(status, StatusCode::OK, "{body}"); + assert_eq!(body["meta"]["columns"], json!(["from", "to", "posterior", "in_structure"])); + + let rows = body["data"].as_array().unwrap(); + // 6 variables: 6 x 5 directed pairs, self-edges excluded. + assert_eq!(rows.len(), 30, "one row per candidate edge"); + let in_structure: Vec<(String, String)> = rows + .iter() + .filter(|row| row[3].as_bool().unwrap_or(false)) + .map(|row| { + ( + row[0].as_str().unwrap().to_string(), + row[1].as_str().unwrap().to_string(), + ) + }) + .collect(); + // The web application's own answer for this fixture (see + // gn-bnw/test/golden/six_traits_structure_input.txt): + let mut expected = vec![ + ("Trait2".to_string(), "Trait1".to_string()), + ("Trait2".to_string(), "Trait4".to_string()), + ("Trait3".to_string(), "Trait4".to_string()), + ("Trait6".to_string(), "Trait3".to_string()), + ("Trait6".to_string(), "Trait4".to_string()), + ]; + let mut got = in_structure.clone(); + expected.sort(); + got.sort(); + assert_eq!(got, expected, "the network must be BNW's own"); + + // The notes have to name the parameters and say what a posterior is. + let notes = body["notes"].as_array().unwrap(); + let notes: Vec<&str> = notes.iter().map(|note| note.as_str().unwrap()).collect(); + assert!( + notes.iter().any(|note| note.contains("maxparents 4") && note.contains("threshold 0.5")), + "the parameters used are not named: {notes:?}" + ); + assert!( + notes.iter().any(|note| note.contains("not a p-value")), + "the posterior caveat is missing: {notes:?}" + ); +} + +#[tokio::test] +async fn an_unusable_network_request_is_refused_before_any_work() { + let Some(token) = common::auth_token().await else { + eprintln!( + "skipping an_unusable_network_request_is_refused_before_any_work: set GN4_TEST_AUTH_TOKEN" + ); + return; + }; + let cases = [ + // one variable is not a network + ( + json!({"values": [{"name": "a", "samples": [ + {"name": "s1", "value": 1.0}, {"name": "s2", "value": 2.0}, + {"name": "s3", "value": 3.0}]}]}), + "invalid_network_input", + ), + // one trait is not a network either + ( + json!({"group": "BXD", "traits": [10001]}), + "too_few_variables", + ), + // a threshold that is not a posterior + ( + json!({"values": [ + {"name": "a", "samples": [{"name": "s1", "value": 1.0}, + {"name": "s2", "value": 2.0}, + {"name": "s3", "value": 3.0}]}, + {"name": "b", "samples": [{"name": "s1", "value": 1.0}, + {"name": "s2", "value": 2.5}, + {"name": "s3", "value": 2.0}]}], + "threshold": 1.5}), + "invalid_threshold", + ), + // both shapes at once + ( + json!({"group": "BXD", "traits": [10001, 10002], + "values": [{"name": "a", "samples": []}]}), + "ambiguous_input", + ), + ]; + for (body, expected) in cases { + let (status, reply) = post(body, &token).await; + assert_eq!( + status, + StatusCode::UNPROCESSABLE_ENTITY, + "{expected}: {reply}" + ); + assert_eq!(reply["code"], expected, "{reply}"); + } +} -- cgit 1.4.1