about summary refs log tree commit diff
path: root/tests/qtl2.rs
blob: dbf6c1dd3c6f2d45ed5f333b22dfc7b4578d01b1 (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//! `POST /compute/qtl2`: R/qtl2 scans with an LMM and LOCO kinship.
//!
//! Two levels: the riset fixture (does the endpoint work at all), and a real
//! panel (`BXD.4`) cross-checked against `/compute/rqtl` -- an LMM and a
//! Haley-Knott scan are different models, so the assertion is that they
//! agree about *which chromosome* the peak is on and that the LMM is the
//! better-behaved of the two, not that the LODs match.

use axum::http::StatusCode;
use http_body_util::BodyExt;
use serde_json::Value;

mod common;

fn rscript_available() -> bool {
    std::process::Command::new("Rscript").arg("--version").output().is_ok()
}

fn qtl2_available() -> bool {
    std::process::Command::new("Rscript")
        .args(["-e", "library(qtl2)"])
        .output()
        .map(|output| output.status.success())
        .unwrap_or(false)
}

fn test_data_path(name: &str) -> String {
    format!("{}/tests/test_data/{name}", env!("CARGO_MANIFEST_DIR"))
}

async fn ready() -> Option<String> {
    if !rscript_available() {
        eprintln!("skipping: Rscript not on PATH");
        return None;
    }
    if !qtl2_available() {
        eprintln!("skipping: r-qtl2 not on the R library path (see guix.scm)");
        return None;
    }
    match common::auth_token().await {
        Some(token) => Some(token),
        None => {
            eprintln!("skipping: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)");
            None
        }
    }
}

#[tokio::test]
async fn a_fixture_panel_scans_into_marker_chr_pos_lod() {
    let Some(token) = ready().await else { return };
    let body = serde_json::json!({
        "genotype_file": test_data_path("rqtl_sample1.geno"),
        "trait_name": "fixture",
        "trait_values": [9.5, 8.2, 7.1, 6.4, 5.9, 4.3],
    });
    let response =
        common::post_json_with_token("POST /compute/qtl2", "/compute/qtl2", &token, &body).await;
    let status = response.status();
    let bytes = response.into_body().collect().await.unwrap().to_bytes();
    assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes));
    let envelope: Value = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(envelope["meta"]["columns"], serde_json::json!(["marker", "chr", "pos", "lod"]));
    let rows = envelope["data"].as_array().unwrap();
    assert!(!rows.is_empty(), "the fixture's markers are all scanned");
    for row in rows {
        assert!(row[0].is_string(), "{row}");
        assert!(row[2].is_number() && row[3].is_number(), "{row}");
    }
    let notes: Vec<&str> = envelope["notes"]
        .as_array()
        .unwrap()
        .iter()
        .map(|note| note.as_str().unwrap())
        .collect();
    assert!(notes.iter().any(|note| note.starts_with("Trait: fixture")));
    assert!(
        notes.iter().any(|note| note.starts_with("Cross: riself")),
        "the cross type is reported: {notes:?}"
    );
    assert!(notes.iter().any(|note| note.starts_with("Rscript ")));
}

/// The point of the endpoint: on a real panel its LMM finds the same
/// chromosome as the Haley-Knott scan, and the response says what ran.
#[tokio::test]
async fn a_group_panel_scans_and_agrees_with_the_haley_knott_scan() {
    let Some(token) = ready().await else { return };
    if !common::db_available() {
        eprintln!("skipping a_group_panel_scans_and_agrees_with_the_haley_knott_scan: no usable database");
        return;
    }
    let response = common::get_with_token(
        "GET /groups/BXD/traits/10001/values",
        "/groups/BXD/traits/10001/values",
        &token,
    )
    .await;
    assert_eq!(response.status(), StatusCode::OK);
    let bytes = response.into_body().collect().await.unwrap().to_bytes();
    let samples: Value = common::data(&bytes);

    let body = serde_json::json!({
        "group": "BXD",
        "genotype_version": "BXD.4",
        "trait_name": "BXD::10001",
        "samples": samples,
        "cores": 4,
    });
    let response =
        common::post_json_with_token("POST /compute/qtl2 (group)", "/compute/qtl2", &token, &body)
            .await;
    let status = response.status();
    let bytes = response.into_body().collect().await.unwrap().to_bytes();
    assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes));
    let envelope: Value = serde_json::from_slice(&bytes).unwrap();
    let rows = envelope["data"].as_array().unwrap();
    assert!(rows.len() > 3000, "the whole panel is scanned: {}", rows.len());
    let peak = rows
        .iter()
        .max_by(|left, right| {
            left[3]
                .as_f64()
                .unwrap()
                .partial_cmp(&right[3].as_f64().unwrap())
                .unwrap()
        })
        .unwrap();
    assert_eq!(peak[1].as_str().unwrap(), "8", "the peak is on chr8: {peak}");

    // And the same trait through the Haley-Knott scan: same chromosome.
    let body = serde_json::json!({
        "group": "BXD",
        "genotype_version": "BXD.4",
        "trait_name": "BXD::10001",
        "samples": samples,
    });
    let response =
        common::post_json_with_token("POST /compute/rqtl", "/compute/rqtl", &token, &body).await;
    assert_eq!(response.status(), StatusCode::OK);
    let bytes = response.into_body().collect().await.unwrap().to_bytes();
    let scan: Value = serde_json::from_slice(&bytes).unwrap();
    let hk_peak = scan["data"]["markers"]
        .as_array()
        .unwrap()
        .iter()
        .max_by(|left, right| {
            left[3]
                .as_f64()
                .unwrap()
                .partial_cmp(&right[3].as_f64().unwrap())
                .unwrap()
        })
        .unwrap();
    assert_eq!(hk_peak[1].as_str().unwrap(), "8", "scanone agrees: {hk_peak}");
}

/// Refusals: something to scan, a kinship name that exists, a model that
/// exists.
#[tokio::test]
async fn qtl2_refuses_inputs_it_cannot_scan() {
    let Some(token) = ready().await else { return };
    for (body, code) in [
        (serde_json::json!({"trait_values": [1.0, 2.0]}), "missing_genotype"),
        (
            serde_json::json!({
                "genotype_file": test_data_path("rqtl_sample1.geno"),
                "trait_values": [9.5, 8.2, 7.1, 6.4, 5.9, 4.3],
                "kinship": "sometimes"
            }),
            "invalid_kinship",
        ),
    ] {
        let response =
            common::post_json_with_token("POST /compute/qtl2", "/compute/qtl2", &token, &body)
                .await;
        assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
        let bytes = response.into_body().collect().await.unwrap().to_bytes();
        let error: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(error["code"], code, "{error}");
    }
}

/// A cross that ships its own control file is the only route to a
/// multiparent scan, so its refusals matter: the name must exist, the
/// samples must belong to the cross, and the two genotype sources cannot
/// both be named.
#[tokio::test]
async fn a_shipped_multiparent_cross_is_named_not_guessed() {
    let Some(token) = ready().await else { return };
    for (body, code) in [
        (
            serde_json::json!({"rqtl2_cross": "nonesuch",
                              "samples": [{"name": "S1", "value": 1.0}]}),
            "unsupported_cross",
        ),
        (serde_json::json!({"rqtl2_cross": "do"}), "missing_samples"),
        (
            serde_json::json!({"rqtl2_cross": "do",
                              "samples": [{"name": "not-an-individual", "value": 1.0}]}),
            "no_samples_in_cross",
        ),
        (
            serde_json::json!({"rqtl2_cross": "do", "group": "BXD",
                              "samples": [{"name": "BXD1", "value": 1.0}]}),
            "ambiguous_genotype",
        ),
    ] {
        let response =
            common::post_json_with_token("POST /compute/qtl2 (cross)", "/compute/qtl2", &token, &body)
                .await;
        let status = response.status();
        let bytes = response.into_body().collect().await.unwrap().to_bytes();
        let error: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "{error}");
        assert_eq!(error["code"], code, "{error}");
    }
}

/// The real thing: the DO, 8 founder alleles, an LMM with LOCO kinship.
/// Ignored because the genotype probabilities for 742 individuals over 6 413
/// markers take minutes the first time (then they are cached); run it with
/// `cargo test --test qtl2 -- --ignored`.
#[tokio::test]
#[ignore]
async fn a_do_scan_fits_eight_founder_alleles() {
    let Some(token) = ready().await else { return };
    let pheno_path = std::path::Path::new(
        "/home/wrk/tmp/ai-root/factory/gn4-data/genotype-files/do/do_pheno.csv",
    );
    let Ok(text) = std::fs::read_to_string(pheno_path) else {
        eprintln!("skipping: {} is not readable", pheno_path.display());
        return;
    };
    let mut samples = Vec::new();
    for line in text.lines().filter(|line| !line.starts_with('#')) {
        let fields: Vec<&str> = line.split(',').collect();
        if fields.len() < 2 || fields[0] == "id" {
            continue;
        }
        samples.push(serde_json::json!({
            "name": fields[0],
            "value": fields[1].parse::<f64>().ok(),
        }));
    }
    assert!(samples.len() > 700, "the DO's 742 individuals: {}", samples.len());
    let body = serde_json::json!({
        "rqtl2_cross": "do",
        "trait_name": "DO::WBC",
        "samples": samples,
        "cores": 8,
    });
    let response =
        common::post_json_with_token("POST /compute/qtl2 (DO)", "/compute/qtl2", &token, &body).await;
    let status = response.status();
    let bytes = response.into_body().collect().await.unwrap().to_bytes();
    assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes));
    let envelope: Value = serde_json::from_slice(&bytes).unwrap();
    let rows = envelope["data"].as_array().unwrap();
    assert!(rows.len() > 5000, "the DO's autosomes: {}", rows.len());
    let notes: Vec<&str> = envelope["notes"]
        .as_array()
        .unwrap()
        .iter()
        .map(|note| note.as_str().unwrap())
        .collect();
    let cross_note = notes.iter().find(|note| note.starts_with("Cross: ")).unwrap();
    assert!(cross_note.contains("do"), "{cross_note}");
    assert!(cross_note.contains("8 founder alleles"), "{cross_note}");
    // WBC's known peak region: the scan says something, not a flat line.
    let best = rows
        .iter()
        .map(|row| row[3].as_f64().unwrap())
        .fold(0.0_f64, f64::max);
    assert!(best > 3.0, "a real peak, not noise: {best}");
}

/// The two genotype models, and what the reply says about each: `riself` drops
/// the heterozygous calls (and says how many), `f2` keeps them as a third
/// genotype as a diagnostic. BXD trait 16627 is the case where the difference
/// is visible -- see the README's "Why R/qtl and R/qtl2 disagree".
#[tokio::test]
async fn the_genotype_model_is_reported_and_switchable() {
    let Some(token) = ready().await else { return };
    if !common::db_available() {
        eprintln!("skipping the_genotype_model_is_reported_and_switchable: no database");
        return;
    }
    let values = common::get_with_token(
        "GET /groups/BXD/traits/16627/values",
        "/groups/BXD/traits/16627/values",
        &token,
    )
    .await;
    assert_eq!(values.status(), StatusCode::OK);
    let bytes = values.into_body().collect().await.unwrap().to_bytes();
    let samples: Value = common::data(&bytes);

    let mut peaks = Vec::new();
    for model in [None, Some("f2")] {
        let mut body = serde_json::json!({
            "group": "BXD",
            "genotype_version": "BXD.8",
            "trait_name": "BXD::16627",
            "samples": samples,
            "cores": 4,
        });
        if let Some(model) = model {
            body["genotype_model"] = serde_json::json!(model);
        }
        let response = common::post_json_with_token(
            "POST /compute/qtl2 (genotype_model)",
            "/compute/qtl2",
            &token,
            &body,
        )
        .await;
        let status = response.status();
        let bytes = response.into_body().collect().await.unwrap().to_bytes();
        assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes));
        let envelope: Value = serde_json::from_slice(&bytes).unwrap();
        let notes: Vec<&str> = envelope["notes"]
            .as_array()
            .unwrap()
            .iter()
            .map(|note| note.as_str().unwrap())
            .collect();
        let cross = notes
            .iter()
            .find(|note| note.starts_with("Cross: "))
            .copied()
            .unwrap();
        match model {
            None => {
                assert!(cross.contains("riself"), "{cross}");
                assert!(
                    notes.iter().any(|note| note.starts_with("genotype_model riself: ")
                        && note.contains("heterozygous call(s)")),
                    "the reply counts what it dropped: {notes:?}"
                );
            }
            Some(_) => {
                assert!(cross.contains("f2"), "{cross}");
                assert!(
                    notes.iter().any(|note| note.contains("kept as a third genotype")),
                    "the reply says it is a diagnostic: {notes:?}"
                );
            }
        }
        let rows = envelope["data"].as_array().unwrap();
        let peak = rows
            .iter()
            .max_by(|left, right| {
                left[3]
                    .as_f64()
                    .unwrap()
                    .partial_cmp(&right[3].as_f64().unwrap())
                    .unwrap()
            })
            .unwrap();
        peaks.push((cross.to_string(), peak[3].as_f64().unwrap()));
    }
    // Keeping the calls recovers the signal the dropped ones cost: the point
    // of the switch. (Measured 3.7 against 9.7; assert the direction only,
    // since the phenotypes and the panel may change.)
    assert!(
        peaks[1].1 > peaks[0].1 + 1.0,
        "f2 should find more than riself here: {peaks:?}"
    );

    // An unknown model is refused rather than ignored.
    let response = common::post_json_with_token(
        "POST /compute/qtl2 (bad genotype_model)",
        "/compute/qtl2",
        &token,
        &serde_json::json!({
            "group": "BXD", "trait_name": "t", "samples": samples,
            "genotype_model": "genril2-ish",
        }),
    )
    .await;
    assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
    let bytes = response.into_body().collect().await.unwrap().to_bytes();
    let error: Value = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(error["code"], "invalid_het");
}