about summary refs log tree commit diff
path: root/tests/parental_metadata.rs
blob: b4e3fa36bc7a8be9c99f2d36d9815e103d4173a1 (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
//! The parental metadata is the same fact in two places, so check they agree.
//!
//! `parents_and_f1s.json` is GN2's `ParInfo` as data. They drifted once
//! already -- the JSON's `paternal` and `maternal` labels were swapped against
//! the way GN2 unpacks the tuple (`f1, f12, maternal, paternal`) -- and nothing
//! noticed, because the exclusion rule takes both strains regardless of role.
//! The roles are not decoration: anything reading "maternal" (reciprocal F1
//! direction, the `@mat`/`@pat` wording) would have been backwards.
//!
//! While GN2 exists, this test runs `scripts/check-parental-metadata.py` against
//! it. It skips when the GN2 checkout is absent, which is the normal case once
//! GN2 is retired -- at which point the JSON is the only copy and there is
//! nothing to drift from.

use std::path::{Path, PathBuf};

fn gn2_checkout() -> Option<PathBuf> {
    if let Ok(path) = std::env::var("GN2_CHECKOUT") {
        let path = PathBuf::from(path);
        return path.join("gn2/utility/webqtlUtil.py").is_file().then_some(path);
    }
    // A sibling checkout of this repository: <code>/genenetwork2 next to
    // <code>/genenetwork/genenetwork4.
    let candidates = [
        Path::new(env!("CARGO_MANIFEST_DIR")).join("../genenetwork2"),
        Path::new(env!("CARGO_MANIFEST_DIR")).join("../../genenetwork2"),
    ];
    candidates
        .into_iter()
        .find(|path| path.join("gn2/utility/webqtlUtil.py").is_file())
}

#[test]
fn the_parental_metadata_matches_gn2s_parinfo() {
    let Some(gn2) = gn2_checkout() else {
        eprintln!(
            "skipping the_parental_metadata_matches_gn2s_parinfo: no GN2 checkout \
             (set GN2_CHECKOUT)"
        );
        return;
    };
    let config = genenetwork4::config::Config::load();
    let json = Path::new(&config.genotype_files_dir).join("parents_and_f1s.json");
    if !json.is_file() {
        eprintln!("skipping the_parental_metadata_matches_gn2s_parinfo: no {json:?}");
        return;
    }
    let script = Path::new(env!("CARGO_MANIFEST_DIR")).join("scripts/check-parental-metadata.py");
    let output = std::process::Command::new("python3")
        .arg(&script)
        .arg("--parinfo")
        .arg(gn2.join("gn2/utility/webqtlUtil.py"))
        .arg("--json")
        .arg(&json)
        .output()
        .expect("python3 must be on PATH (PLOT_PYTHON's interpreter)");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "the parental metadata disagrees with GN2's ParInfo:\n{stdout}{stderr}"
    );
    // A silent pass is not very informative; the script prints what it checked.
    assert!(
        stdout.contains("GN2 groups reproduced"),
        "expected the checker to report what it compared: {stdout}{stderr}"
    );
}