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
|
//! `POST /compute/bulklmm`: an LMM scan with permutation thresholds.
//!
//! The reason the endpoint exists is the *thresholds*: `/compute/gemma` refuses
//! `nperm` because a permutation repeats the whole LOCO scan, and R/qtl's
//! permutation is the wrong null for related individuals. BulkLMM rotates by
//! the kinship eigen decomposition, so a threshold costs seconds and belongs to
//! the model that produced the LODs.
use axum::http::StatusCode;
use http_body_util::BodyExt;
use serde_json::Value;
mod common;
/// BulkLMM needs a Julia with its packages instantiated; skip when the
/// deployment has none (a `guix shell` without the Julia side).
///
/// The project file has to *name the packages the driver loads*, not merely
/// exist: an env left behind by a half-instantiated run has a Project.toml
/// listing BulkLMM and nothing else, and the scan then dies on `using CSV`
/// with a stack trace instead of skipping here. `BULKLMM_ENV` decides which
/// project is asked, so a shell that wants these tests sets it.
fn bulklmm_available() -> bool {
let config = genenetwork4::config::Config::load();
if !std::path::Path::new(&config.bulklmm_cmd).is_file() {
return false;
}
let project = std::path::Path::new(&config.bulklmm_env).join("Project.toml");
let Ok(contents) = std::fs::read_to_string(&project) else {
return false;
};
["BulkLMM", "CSV", "DataFrames"]
.iter()
.all(|package| contents.contains(package))
}
#[tokio::test]
async fn a_scan_reports_lods_and_permutation_thresholds() {
let Some(token) = common::auth_token().await else {
eprintln!("skipping: set GN4_TEST_AUTH_TOKEN");
return;
};
if !common::db_available() {
eprintln!("skipping a_scan_reports_lods_and_permutation_thresholds: no database");
return;
}
if !bulklmm_available() {
eprintln!("skipping a_scan_reports_lods_and_permutation_thresholds: no Julia/BulkLMM");
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 response = common::post_json_with_token(
"POST /compute/bulklmm",
"/compute/bulklmm",
&token,
&serde_json::json!({
"group": "BXD",
"genotype_version": "BXD.8",
"trait_name": "BXD::16627",
"samples": samples,
"nperm": 20,
"seed": 7,
"cores": 2,
}),
)
.await;
let status = response.status();
let bytes = response.into_body().collect().await.unwrap().to_bytes();
// Both tests in this file share one caller and the gate gives a caller one
// scan at a time, so whichever starts second sees 409. That is the gate
// working, not a failure.
if status == StatusCode::CONFLICT {
eprintln!("skipping: the other BulkLMM test holds this caller's slot");
return;
}
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", "beta", "h2"])
);
let rows = envelope["data"].as_array().unwrap();
assert!(rows.len() > 1000, "the whole panel: {}", rows.len());
let peak = rows
.iter()
.max_by(|left, right| {
left[3]
.as_f64()
.unwrap()
.partial_cmp(&right[3].as_f64().unwrap())
.unwrap()
})
.unwrap();
// The published peak of this trait is rsm10000000888 (chr2, LOD 9.59); an
// LMM with the same values has to see it.
assert_eq!(peak[1], "2", "the peak stays on chr2: {peak}");
assert!(
peak[3].as_f64().unwrap() > 3.0,
"a real peak, not noise: {peak}"
);
let notes: Vec<&str> = envelope["notes"]
.as_array()
.unwrap()
.iter()
.map(|note| note.as_str().unwrap())
.collect();
assert!(
notes
.iter()
.any(|note| note.contains("Permutation thresholds from 20 rotations")),
"the thresholds are in the reply: {notes:?}"
);
assert!(
notes.iter().any(|note| note.contains("seed 7")),
"and the seed that produced them: {notes:?}"
);
assert!(
notes.iter().any(|note| note.contains("Null heritability")),
"and what the kinship term found: {notes:?}"
);
// The parallelism is a configured number, not folklore.
assert!(
notes
.iter()
.any(|note| note.contains("independent blocks") && note.contains("BULKLMM_BLOCKS")),
"the block count and where to set it: {notes:?}"
);
}
/// Without permutations there is no threshold, and the reply says so rather
/// than leaving a bare LOD to be read as significant.
#[tokio::test]
async fn a_scan_without_permutations_says_there_is_no_threshold() {
let Some(token) = common::auth_token().await else {
eprintln!("skipping: set GN4_TEST_AUTH_TOKEN");
return;
};
if !common::db_available() || !bulklmm_available() {
eprintln!("skipping a_scan_without_permutations_says_there_is_no_threshold");
return;
}
let values = common::get_with_token(
"GET /groups/BXD/traits/16627/values",
"/groups/BXD/traits/16627/values",
&token,
)
.await;
let bytes = values.into_body().collect().await.unwrap().to_bytes();
let samples: Value = common::data(&bytes);
let response = common::post_json_with_token(
"POST /compute/bulklmm (no permutations)",
"/compute/bulklmm",
&token,
&serde_json::json!({
"group": "BXD",
"genotype_version": "BXD.8",
"trait_name": "BXD::16627",
"samples": samples,
"cores": 2,
}),
)
.await;
// The two tests share one caller, and the gate allows one scan at a time
// per caller: a 409 here means the other test is running, not a failure.
if response.status() == StatusCode::CONFLICT {
eprintln!("skipping: another BulkLMM scan holds this caller's slot");
return;
}
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 notes = envelope["notes"].to_string();
assert!(notes.contains("No permutations requested"), "{notes}");
}
/// Posting a scan back draws it, thresholds included; and a GEMMA permutation
/// refusal points at this engine, since it is the one that can answer.
#[tokio::test]
async fn a_scan_plots_and_gemmas_refusal_points_here() {
let Some(token) = common::auth_token().await else {
eprintln!("skipping: set GN4_TEST_AUTH_TOKEN");
return;
};
if !common::db_available() || !bulklmm_available() {
eprintln!("skipping a_scan_plots_and_gemmas_refusal_points_here");
return;
}
let values = common::get_with_token(
"GET /groups/BXD/traits/16627/values",
"/groups/BXD/traits/16627/values",
&token,
)
.await;
let bytes = values.into_body().collect().await.unwrap().to_bytes();
let samples: Value = common::data(&bytes);
let response = common::post_json_with_token(
"POST /compute/bulklmm (for the plot)",
"/compute/bulklmm",
&token,
&serde_json::json!({
"group": "BXD", "genotype_version": "BXD.8", "trait_name": "BXD::16627",
"samples": samples, "nperm": 20, "seed": 3, "cores": 2,
}),
)
.await;
if response.status() == StatusCode::CONFLICT {
eprintln!("skipping: another BulkLMM scan holds this caller's slot");
return;
}
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!(
envelope["actions"]
.as_array()
.unwrap()
.iter()
.any(|action| action["href"] == "/plot/bulklmm"),
"the reply offers its own plot: {envelope}"
);
// The plot takes the reply back, thresholds and all.
let plot = common::post_json_with_token(
"POST /plot/bulklmm",
"/plot/bulklmm",
&token,
&envelope,
)
.await;
let status = plot.status();
let bytes = plot.into_body().collect().await.unwrap().to_bytes();
assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes));
let svg = String::from_utf8_lossy(&bytes);
assert!(svg.trim_start().starts_with("<svg"), "an SVG");
// Both thresholds the endpoint reports are drawn: alpha 0.05 (the
// genome-wide line) and alpha 0.01, when there are enough permutations.
// Two lines by default -- the genome-wide 0.05 and the stricter 0.01 --
// and never the loosest one unless it is asked for.
assert_eq!(
svg.matches("class=\"thr").count(),
2,
"the 0.05 and 0.01 thresholds are drawn, and not 0.10"
);
assert!(svg.contains("genome-wide (alpha 0.05)"), "the 5 % line is labelled");
assert!(svg.contains("alpha 0.01"), "and the 1 % line");
assert!(!svg.contains("alpha 0.10"), "the loosest line is not drawn by default");
assert!(svg.contains("rsm10000000888"), "the summary names the peak");
// A GEMMA permutation request now says where the thresholds are.
let refusal = common::post_json_with_token(
"POST /compute/gemma (nperm)",
"/compute/gemma",
&token,
&serde_json::json!({
"group": "BXD", "genotype_version": "BXD.8", "trait_name": "BXD::16627",
"samples": samples, "nperm": 100,
}),
)
.await;
assert_eq!(refusal.status(), StatusCode::UNPROCESSABLE_ENTITY);
let bytes = refusal.into_body().collect().await.unwrap().to_bytes();
let error: Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(error["code"], "permutations_not_supported");
assert_eq!(error["next"], "/compute/bulklmm");
let detail = error["detail"].as_str().unwrap();
assert!(
detail.contains("/compute/bulklmm"),
"the refusal names the engine that can: {error}"
);
assert!(
detail.contains("run GEMMA's own permutation locally"),
"and that the exact GEMMA null can be had by running it locally: {detail}"
);
}
|