about summary refs log tree commit diff
path: root/tests/bnw.rs
blob: 6241d8d66660f239a998c02c6cf36401fc2a2e88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//! `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: <code>/gn-bnw, while this
    // checkout is <code>/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<Vec<&str>> = 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<Value> = rows
            .iter()
            .enumerate()
            .map(|(index, row)| {
                json!({"name": format!("s{index}"), "value": row[column].parse::<f64>().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<Value> = 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);
}