//! `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}"); } } /// The point of `/features`: a module becomes a trait, so it can be mapped. #[tokio::test] async fn module_scores_come_back_in_the_shape_a_mapping_endpoint_takes() { let Some(token) = common::auth_token().await else { eprintln!("skipping module_scores_come_back_in_the_shape_a_mapping_endpoint_takes: no token"); return; }; if !bnw_available() { eprintln!("skipping module_scores_come_back_in_the_shape_a_mapping_endpoint_takes: no BNW driver"); return; } let variables = six_traits(); if variables.is_null() { eprintln!("skipping module_scores_come_back_in_the_shape_a_mapping_endpoint_takes: no fixtures"); return; } // The module list let response = common::post_json_with_token( "POST /compute/bnw/features", "/compute/bnw/features", &token, &json!({"values": variables, "trait_name": "golden-6-traits"}), ) .await; let status = response.status(); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let body: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(status, StatusCode::OK, "{body}"); assert_eq!( body["meta"]["columns"], json!(["module", "members", "size", "variance_explained"]) ); let modules = body["data"].as_array().unwrap(); assert!(!modules.is_empty(), "the fixture has modules: {body}"); let first = modules[0][0].as_str().unwrap().to_string(); assert!( body["notes"][0].as_str().unwrap().contains("first principal component"), "the definition of the score has to be named" ); // One module's scores, and their shape let response = common::post_json_with_token( "POST /compute/bnw/features", &format!("/compute/bnw/features?module={first}"), &token, &json!({"values": six_traits(), "trait_name": "golden-6-traits"}), ) .await; let status = response.status(); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let body: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(status, StatusCode::OK, "{body}"); let values = body["data"].as_array().unwrap(); assert_eq!(values.len(), 200, "one score per sample"); assert!(values[0]["name"].is_string() && values[0]["value"].is_number(), "{:?}", values[0]); // ... which is exactly what a mapping endpoint accepts let samples: Vec = values .iter() .map(|value| json!({"name": value["name"], "value": value["value"]})) .collect(); let response = common::post_json_with_token( "POST /compute/bnw/features", &format!("/compute/bnw/features?module=no-such-module"), &token, &json!({"values": six_traits(), "trait_name": "golden-6-traits"}), ) .await; assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY); assert_eq!(samples.len(), 200); } /// BNW's own examples, served in the shape `/compute/bnw` takes. #[tokio::test] async fn the_worked_examples_are_offered_as_ready_to_post_matrices() { let Some(token) = common::auth_token().await else { eprintln!("skipping the_worked_examples_are_offered_as_ready_to_post_matrices: no token"); return; }; let config = genenetwork4::config::Config::load(); if config.bnw_examples_dir.trim().is_empty() || !std::path::Path::new(&config.bnw_examples_dir).is_dir() { eprintln!("skipping the_worked_examples_are_offered_as_ready_to_post_matrices: no BNW_EXAMPLES_DIR"); return; } let response = common::get_with_token("GET /compute/bnw/examples", "/compute/bnw/examples", &token).await; let status = response.status(); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let body: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(status, StatusCode::OK, "{body}"); let examples = body["data"].as_array().unwrap(); assert!(!examples.is_empty(), "{body}"); let name = examples[0][0].as_str().unwrap().to_string(); let path = format!("/compute/bnw/examples/{name}"); let response = common::get_with_token(&format!("GET {path}"), &path, &token).await; let status = response.status(); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let body: Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(status, StatusCode::OK, "{body}"); let values = body["data"]["values"].as_array().unwrap(); assert!(values.len() >= 2, "a matrix has variables: {body}"); assert!(values[0]["samples"].as_array().unwrap().len() >= 3); // It is offered as a ready-to-post action, so an agent does not have to // assemble the body itself. let actions = body["actions"].as_array().unwrap(); assert_eq!(actions[0]["href"], "/compute/bnw", "{body}"); assert!(actions[0]["body"]["values"].is_array(), "{body}"); // An unknown example is a 404, not an empty matrix. let response = common::get_with_token( "GET /compute/bnw/examples/no_such_example", "/compute/bnw/examples/no_such_example", &token, ) .await; assert_eq!(response.status(), StatusCode::NOT_FOUND); }