about summary refs log tree commit diff
path: root/tests/frequencies.rs
blob: 48369a95006e2ad9130bec1d0917993ac24771c3 (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
//! `GET /groups/{group}/genotypes/{version}/frequencies`, its single-marker
//! form, and the `unlisted` namespace for files no group lists.
//!
//! The tally is the one place the API says how a panel is actually called, so
//! the tests pin the shape (a call -> frequency map that sums to 1), that the
//! marker route and `?marker=` agree, and that a wrong version is refused
//! with the versions that exist rather than an empty tally.

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
}

fn sum(map: &Value) -> f64 {
    map.as_object()
        .expect("the data is a call -> frequency map")
        .values()
        .map(|value| value.as_f64().expect("frequencies are numbers"))
        .sum()
}

#[tokio::test]
async fn a_panel_reports_the_frequencies_of_its_calls() {
    let Some(token) = ready().await else { return };
    let response = common::get_with_token(
        "GET /groups/BXD/genotypes/BXD.4/frequencies",
        "/groups/BXD/genotypes/BXD.4/frequencies",
        &token,
    )
    .await;
    assert_eq!(response.status(), StatusCode::OK);
    let bytes = response.into_body().collect().await.unwrap().to_bytes();
    let frequencies: Value = common::data(&bytes);
    let envelope: Value = serde_json::from_slice(&bytes).unwrap();
    let calls = frequencies.as_object().expect("a map of calls").clone();
    assert!(
        calls.keys().all(|call| call.len() <= 2),
        "calls as the file spells them, not positions: {:?}",
        calls.keys().collect::<Vec<_>>()
    );
    assert!((sum(&frequencies) - 1.0).abs() < 1e-5, "{}", sum(&frequencies));
    // A panel typed as riset is called with its two parental codes plus het.
    assert!(calls.contains_key("B"), "parental B is a call: {calls:?}");
    assert!(calls.contains_key("D"), "parental D is a call: {calls:?}");
    let notes = envelope["notes"].as_array().unwrap();
    assert!(
        notes
            .iter()
            .any(|note| note.as_str().unwrap().contains("marker(s)")),
        "the counts are in notes: {notes:?}"
    );
}

#[tokio::test]
async fn the_marker_route_and_the_marker_parameter_agree() {
    let Some(token) = ready().await else { return };
    let marker = "rs6269442";
    let in_path = common::get_with_token(
        "GET /groups/BXD/genotypes/BXD.4/markers/{marker}/frequencies",
        &format!("/groups/BXD/genotypes/BXD.4/markers/{marker}/frequencies"),
        &token,
    )
    .await;
    assert_eq!(in_path.status(), StatusCode::OK);
    let bytes = in_path.into_body().collect().await.unwrap().to_bytes();
    let path_data: Value = common::data(&bytes);
    let path_envelope: Value = serde_json::from_slice(&bytes).unwrap();

    let as_query = common::get_with_token(
        "GET /groups/BXD/genotypes/BXD.4/frequencies?marker=",
        &format!("/groups/BXD/genotypes/BXD.4/frequencies?marker={marker}"),
        &token,
    )
    .await;
    assert_eq!(as_query.status(), StatusCode::OK);
    let bytes = as_query.into_body().collect().await.unwrap().to_bytes();
    let query_data: Value = common::data(&bytes);

    assert_eq!(
        path_data, query_data,
        "one marker, two ways to ask for it"
    );
    // Frequencies are rounded to six decimal places, so a three-call marker
    // can sum to 0.999999.
    assert!((sum(&path_data) - 1.0).abs() < 1e-5, "{}", sum(&path_data));
    let notes = path_envelope["notes"].as_array().unwrap();
    let first = notes[0].as_str().unwrap();
    assert!(first.contains(marker), "{first}");
    assert!(first.contains("chr"), "the marker's position is reported: {first}");
}

#[tokio::test]
async fn a_marker_that_is_not_there_is_a_404() {
    let Some(token) = ready().await else { return };
    let response = common::get_with_token(
        "GET /groups/BXD/genotypes/BXD.4/markers/nonesuch/frequencies",
        "/groups/BXD/genotypes/BXD.4/markers/nonesuch/frequencies",
        &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"], "genotype_not_found");
    assert!(
        error["detail"].as_str().unwrap().contains("nonesuch"),
        "{error}"
    );
}

#[tokio::test]
async fn a_version_the_group_does_not_offer_names_the_ones_it_does() {
    let Some(token) = ready().await else { return };
    let response = common::get_with_token(
        "GET /groups/BXD/genotypes/nonesuch/frequencies",
        "/groups/BXD/genotypes/nonesuch/frequencies",
        &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();
    let detail = error["detail"].as_str().unwrap();
    assert!(detail.contains("available:"), "{detail}");
}

/// A 3.7 GB panel is refused instead of read: the request that produced this
/// limitation sat in `read_to_string` for ten minutes.
#[tokio::test]
async fn an_oversized_panel_is_refused_with_another_call_to_make() {
    let Some(token) = common::auth_token().await else { return };
    let response = common::get_with_token(
        "GET /groups/unlisted/genotypes/HSNIH-Palmer_true/frequencies",
        "/groups/unlisted/genotypes/HSNIH-Palmer_true/frequencies",
        &token,
    )
    .await;
    match response.status() {
        StatusCode::PAYLOAD_TOO_LARGE => {
            let bytes = response.into_body().collect().await.unwrap().to_bytes();
            let error: Value = serde_json::from_slice(&bytes).unwrap();
            assert_eq!(error["code"], "genotype_too_large");
            assert!(
                error["detail"].as_str().unwrap().contains("one marker"),
                "the refusal says what to do instead: {error}"
            );
        }
        // This deployment may not have that panel at all.
        StatusCode::NOT_FOUND => eprintln!("skipping: the 3.7 GB panel is not deployed"),
        other => panic!("unexpected status {other}"),
    }
}

/// Per-class trait summaries: the question that follows "why do two engines
/// peak at different markers". The classes are joined to a trait's values
/// inside the server, so the test also pins that no per-animal genotype
/// leaves the API.
#[tokio::test]
async fn a_marker_summarises_a_trait_per_genotype_class() {
    let Some(token) = ready().await else { return };
    let marker = "JAX00418132";
    let response = common::get_with_token(
        "GET .../markers/{marker}/classes",
        &format!("/groups/DOL/genotypes/DOL/markers/{marker}/classes?trait=10003"),
        &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 classes = envelope["data"].as_object().expect("class -> stats");
    // DOL's file spells its calls B/A/H.
    for class in ["A", "H", "B"] {
        let stats = &classes[class];
        for field in ["n", "mean", "sd", "min", "max"] {
            assert!(stats.get(field).is_some(), "{class} has {field}: {stats}");
        }
    }
    // The whole point: this marker's A class is two animals, and they are the
    // trait's two lowest values -- which is why an engine that keeps the 20
    // hets sees a peak an engine that drops them cannot.
    assert_eq!(classes["A"]["n"], 2, "{}", envelope["data"]);
    let a_mean = classes["A"]["mean"].as_f64().unwrap();
    let b_mean = classes["B"]["mean"].as_f64().unwrap();
    assert!(a_mean < b_mean, "A below B: {a_mean} vs {b_mean}");
    assert!(classes["H"]["n"].as_u64().unwrap() > 10);

    // A small class is called out: a peak resting on two animals is not the
    // same claim as one resting on a hundred.
    let notes: Vec<&str> = envelope["notes"]
        .as_array()
        .unwrap()
        .iter()
        .map(|note| note.as_str().unwrap())
        .collect();
    assert!(
        notes
            .iter()
            .any(|note| note.contains("smallest class") && note.contains("sensitive")),
        "{notes:?}"
    );
    assert!(
        notes.iter().any(|note| note.contains("not served")),
        "the reply says genotypes stay inside: {notes:?}"
    );
    // The values are statistics, not per-animal rows: nothing in the payload
    // is an animal name.
    let body = envelope["data"].to_string();
    assert!(!body.contains("TB0"), "no individual names in the payload: {body}");

    // A trait the group does not have is a 404, not an empty summary.
    let response = common::get_with_token(
        "GET .../classes?trait=<missing>",
        &format!("/groups/DOL/genotypes/DOL/markers/{marker}/classes?trait=999999"),
        &token,
    )
    .await;
    assert_eq!(response.status(), StatusCode::NOT_FOUND);
}