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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
//! `POST /plot/rqtl` and `POST /plot/gemma`: an SVG back, not an envelope.
//!
//! They take a **posted mapping result**, so they need no engine at all:
//! the plotter scripts (`RQTL_PLOTTER`, `GEMMA_PLOTTER`, run by
//! `PLOT_PYTHON`) are the only requirement, and a canned result exercises
//! the whole path.
use axum::http::StatusCode;
use http_body_util::BodyExt;
use serde_json::Value;
mod common;
use common::{auth_token, post_json_with_token};
fn tool_available(tool: &str, args: &[&str]) -> bool {
std::process::Command::new(tool)
.args(args)
.output()
.map(|output| output.status.success())
.unwrap_or(false)
}
fn rscript_available() -> bool {
tool_available("Rscript", &["--version"])
}
fn genotype_dir_present() -> bool {
let directory = genenetwork4::config::Config::load().genotype_files_dir;
std::path::Path::new(&directory).is_dir()
}
fn plotter_present(which: &str) -> bool {
let config = genenetwork4::config::Config::load();
let path = if which == "rqtl" {
config.rqtl_plotter
} else {
config.gemma_plotter
};
std::path::Path::new(&path).is_file() && tool_available(&config.plot_python, &["--version"])
}
fn canned_rqtl_result() -> Value {
serde_json::json!({
"data": {
"markers": [
["rs1", "1", 3.1, 2.4],
["rs2", "1", 8.2, 1.1],
["rs3", "2", 12.0, 4.2],
["rs4", "2", 19.5, 0.3],
["rs5", "X", 30.0, 1.8]
],
"permutation": {"values": [1.0, 1.2], "suggestive": 2.5, "significant": 3.5}
},
"notes": ["Genotype: prepared panel subset x (version BXD.8, 198 individuals, 0 parents/F1s dropped); 5 of 5 supplied values aligned onto it by name."]
})
}
fn canned_gemma_result() -> Value {
serde_json::json!({
"meta": {"state": "complete", "took_ms": 12, "request_id": "canned",
"columns": ["chr", "rs", "ps", "n_miss", "allele1", "allele0", "af",
"beta", "se", "l_mle", "p_lrt"],
"num_rows": 3, "num_cols": 11},
"data": [
[1.0, "rs1", 3.1, 0.0, "X", "Y", 0.2, 1.0, 2.0, 3.0, 0.5],
[1.0, "rs2", 8.2, 0.0, "X", "Y", 0.2, 1.0, 2.0, 3.0, 0.001],
[2.0, "rs3", 12.0, 0.0, "X", "Y", 0.2, 1.0, 2.0, 3.0, 0.3]
],
"notes": ["Trait: BXD::20010"]
})
}
#[tokio::test]
async fn a_posted_rqtl_result_is_drawn_and_the_suggestion_says_so() {
if !plotter_present("rqtl") {
eprintln!(
"skipping a_posted_rqtl_result_is_drawn_and_the_suggestion_says_so: needs the \
plotter script (RQTL_PLOTTER) and PLOT_PYTHON"
);
return;
}
let Some(token) = auth_token().await else {
eprintln!("skipping a_posted_rqtl_result_is_drawn_and_the_suggestion_says_so: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)");
return;
};
let response = post_json_with_token(
"POST /plot/rqtl",
"/plot/rqtl?trait=BXDPublish::10001",
&token,
&canned_rqtl_result(),
)
.await;
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.headers().get("content-type").unwrap(),
"image/svg+xml; charset=utf-8"
);
let summary = response
.headers()
.get("x-plot-summary")
.expect("x-plot-summary")
.to_str()
.unwrap()
.to_string();
assert!(summary.contains("peak rs3"), "the peak is the strongest LOD: {summary}");
let svg = response.into_body().collect().await.unwrap().to_bytes();
let text = String::from_utf8_lossy(&svg);
assert!(text.starts_with("<svg"), "an SVG document");
assert!(text.trim_end().ends_with("</svg>"), "a complete SVG");
assert!(text.contains("BXDPublish::10001"), "the title carries the trait label");
// Just the `data` member works too.
let response = post_json_with_token(
"POST /plot/rqtl",
"/plot/rqtl",
&token,
&canned_rqtl_result()["data"],
)
.await;
assert_eq!(response.status(), StatusCode::OK);
// GEMMA's plotter reads `meta.columns`, so a bare `data` is refused
// with a message saying to post the whole response.
let response = post_json_with_token(
"POST /plot/gemma",
"/plot/gemma",
&token,
&canned_gemma_result()["data"],
)
.await;
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
}
#[tokio::test]
async fn a_posted_gemma_result_is_drawn() {
if !plotter_present("gemma") {
eprintln!("skipping a_posted_gemma_result_is_drawn: needs the plotter script (GEMMA_PLOTTER) and PLOT_PYTHON");
return;
}
let Some(token) = auth_token().await else {
eprintln!("skipping a_posted_gemma_result_is_drawn: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)");
return;
};
let response = post_json_with_token(
"POST /plot/gemma",
"/plot/gemma?trait=BXDPublish::10001",
&token,
&canned_gemma_result(),
)
.await;
assert_eq!(response.status(), StatusCode::OK);
let summary = response
.headers()
.get("x-plot-summary")
.expect("x-plot-summary")
.to_str()
.unwrap()
.to_string();
assert!(summary.contains("rs2"), "the smallest p_lrt: {summary}");
let svg = response.into_body().collect().await.unwrap().to_bytes();
let text = String::from_utf8_lossy(&svg);
assert!(text.starts_with("<svg"));
// An explicit ?trait= wins over the response's own note.
assert!(text.contains("BXDPublish::10001"), "?trait= labels the plot");
// Without one, the response's note labels it -- so a GEMMA plot of two
// traits is not titled "gemma" twice.
let response = post_json_with_token(
"POST /plot/gemma (note as label)",
"/plot/gemma",
&token,
&canned_gemma_result(),
)
.await;
assert_eq!(response.status(), StatusCode::OK);
let svg = response.into_body().collect().await.unwrap().to_bytes();
let text = String::from_utf8_lossy(&svg);
assert!(
text.contains("BXD::20010"),
"the title names the trait from the response's own note, not \"gemma\": {}",
&text[..text.len().min(400)]
);
}
#[tokio::test]
async fn a_body_that_is_not_a_mapping_result_is_a_422() {
let Some(token) = auth_token().await else {
eprintln!("skipping a_body_that_is_not_a_mapping_result_is_a_422: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)");
return;
};
// A compute *request* is what this used to take; it is not a result.
let request = serde_json::json!({"group": "BXD", "trait_name": "t", "samples": []});
let response = post_json_with_token("POST /plot/rqtl", "/plot/rqtl", &token, &request).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"], "not_an_rqtl_result");
assert!(
error["detail"]
.as_str()
.unwrap()
.contains("post that response back"),
"{error}"
);
let response = post_json_with_token(
"POST /plot/gemma",
"/plot/gemma",
&token,
&serde_json::json!({"markers": []}),
)
.await;
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
}
/// Two responses for two traits, differing only in their LOD values, must
/// draw two different pictures -- and say which trait each is.
///
/// The trace is the point: a plot that reused anything between requests
/// (a cached SVG, a stale scratch file) would draw the first trait's shape
/// for the second, and the only thing giving it away would be the title.
/// This pins both: the drawings differ, and each names its own trait from
/// the response's own `Trait:` note rather than falling back to a title
/// that says nothing.
#[tokio::test]
async fn two_traits_draw_two_different_plots() {
if !plotter_present("rqtl") {
eprintln!("skipping two_traits_draw_two_different_plots: needs the plotter script (RQTL_PLOTTER) and PLOT_PYTHON");
return;
}
let Some(token) = auth_token().await else {
eprintln!("skipping two_traits_draw_two_different_plots: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)");
return;
};
let canned = |name: &str, lods: [f64; 5]| {
serde_json::json!({
"data": {
"markers": [
["rs1", "1", 3.1, lods[0]],
["rs2", "1", 8.2, lods[1]],
["rs3", "2", 12.0, lods[2]],
["rs4", "2", 19.5, lods[3]],
["rs5", "X", 30.0, lods[4]]
],
"permutation": null
},
// What /compute/rqtl writes first, and what /plot/rqtl reads
// its title from.
"notes": [
format!("Trait: BXD::{name}"),
"Genotype: prepared panel subset x (version BXD.4, 93 individuals, 0 parents/F1s dropped); 5 of 5 supplied values aligned onto it by name."
]
})
};
let mut svgs: Vec<(String, String, String)> = Vec::new();
for (name, lods) in [
("10001", [0.2, 0.3, 2.9, 0.1, 0.4]),
("10002", [1.8, 0.4, 0.2, 4.7, 0.9]),
] {
let response = post_json_with_token(
"POST /plot/rqtl (two traits)",
"/plot/rqtl",
&token,
&canned(name, lods),
)
.await;
assert_eq!(response.status(), StatusCode::OK);
let summary = response
.headers()
.get("x-plot-summary")
.expect("x-plot-summary")
.to_str()
.unwrap()
.to_string();
let bytes = response.into_body().collect().await.unwrap().to_bytes();
let text = String::from_utf8_lossy(&bytes).to_string();
// The title names the trait, taken from the posted notes.
assert!(
text.contains(&format!("BXD::{name}")),
"the title carries the trait from the response's own note: {name}"
);
svgs.push((name.to_string(), summary, text));
}
let (first_name, first_summary, first) = &svgs[0];
let (second_name, second_summary, second) = &svgs[1];
assert_ne!(first, second, "{first_name} and {second_name} must not draw the same SVG");
// And each draw its own peak, not the other's.
assert!(first_summary.contains("peak rs3"), "{first_summary}");
assert!(second_summary.contains("peak rs4"), "{second_summary}");
assert!(
first.contains("chr2 12 Mb, LOD 2.90") && !first.contains("LOD 4.70"),
"{first_name}'s own annotation only"
);
assert!(
second.contains("chr2 20 Mb, LOD 4.70"),
"{second_name}'s own annotation: {second_summary}"
);
// The traces themselves: no polyline point set should coincide.
let traces = |svg: &str| -> Vec<String> {
svg.split("<polyline points=\"")
.skip(1)
.map(|rest| rest.split('"').next().unwrap_or_default().to_string())
.collect()
};
let (a, b) = (traces(first), traces(second));
assert_eq!(a.len(), b.len(), "same chromosomes in both");
assert!(
a.iter().zip(&b).all(|(left, right)| left != right),
"every chromosome's trace differs between the two traits"
);
}
/// GEMMA's own reference line is a Bonferroni threshold; a threshold from
/// another engine (BulkLMM's rotations) is a different null and must be
/// labelled as such, not drawn as if it were GEMMA's.
#[tokio::test]
async fn a_gemma_plot_takes_labelled_thresholds_from_another_engine() {
let Some(token) = common::auth_token().await else {
eprintln!("skipping a_gemma_plot_takes_labelled_thresholds_from_another_engine: no token");
return;
};
if !common::db_available() {
eprintln!("skipping: 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 mapped = common::post_json_with_token(
"POST /compute/gemma (for the plot)",
"/compute/gemma",
&token,
&serde_json::json!({
"group": "BXD", "genotype_version": "BXD.8", "trait_name": "BXD::16627",
"samples": samples,
}),
)
.await;
assert_eq!(mapped.status(), StatusCode::OK);
let bytes = mapped.into_body().collect().await.unwrap().to_bytes();
let envelope: Value = serde_json::from_slice(&bytes).unwrap();
// Without them: GEMMA's Bonferroni line only.
let plain = common::post_json_with_token(
"POST /plot/gemma",
"/plot/gemma",
&token,
&envelope,
)
.await;
assert_eq!(plain.status(), StatusCode::OK);
let bytes = plain.into_body().collect().await.unwrap().to_bytes();
let svg = String::from_utf8_lossy(&bytes).to_string();
assert!(
!svg.contains("Bonferroni") && !svg.contains("bulklmm"),
"no reference line unless one is asked for: a line on a plot is an annotation, \
not part of the result"
);
// With them: drawn and named.
let with = common::post_json_with_token(
"POST /plot/gemma?thresholds=",
"/plot/gemma?thresholds=3.50,3.65&threshold_labels=bulklmm%20alpha%200.10,bulklmm%20alpha%200.05",
&token,
&envelope,
)
.await;
assert_eq!(with.status(), StatusCode::OK);
let bytes = with.into_body().collect().await.unwrap().to_bytes();
let svg = String::from_utf8_lossy(&bytes);
assert!(svg.contains("bulklmm alpha 0.10: 3.50"), "labelled with its source");
assert!(svg.contains("bulklmm alpha 0.05: 3.65"), "and the second one");
assert!(!svg.contains("Bonferroni"), "and nothing else unless it is asked for");
// GEMMA's own convention line is one more opt-in labelling.
let with_bonferroni = common::post_json_with_token(
"POST /plot/gemma?bonferroni=true",
"/plot/gemma?bonferroni=true",
&token,
&envelope,
)
.await;
assert_eq!(with_bonferroni.status(), StatusCode::OK);
let bytes = with_bonferroni.into_body().collect().await.unwrap().to_bytes();
let svg = String::from_utf8_lossy(&bytes);
assert!(svg.contains("Bonferroni"), "asked for, and labelled as such");
// A threshold that is not a number is refused, not silently dropped.
let bad = common::post_json_with_token(
"POST /plot/gemma?thresholds=",
"/plot/gemma?thresholds=abc",
&token,
&envelope,
)
.await;
assert_eq!(bad.status(), StatusCode::UNPROCESSABLE_ENTITY);
let bytes = bad.into_body().collect().await.unwrap().to_bytes();
let error: Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(error["code"], "invalid_threshold");
}
/// `/plot/bnw` is the same deal as `/plot/gemma`: post the engine's own
/// result back and get an SVG. Two figures, because a network diagram cannot
/// say how close a rejected edge came to the threshold and a matrix can.
#[tokio::test]
async fn a_posted_bnw_result_is_drawn_in_two_figures() {
let Some(token) = auth_token().await else {
eprintln!("skipping a_posted_bnw_result_is_drawn_in_two_figures: set GN4_TEST_AUTH_TOKEN");
return;
};
let config = genenetwork4::config::Config::load();
if !std::path::Path::new(&config.bnw_plotter).is_file() {
eprintln!("skipping a_posted_bnw_result_is_drawn_in_two_figures: no BNW_PLOTTER");
return;
}
// A canned /compute/bnw reply: BNW's own six-trait answer, trimmed to the
// edges that matter here.
let posted = serde_json::json!({
"data": [
["Trait2", "Trait1", 1.0, true],
["Trait2", "Trait4", 1.0, true],
["Trait3", "Trait4", 0.42, false]
],
"meta": {"columns": ["from", "to", "posterior", "in_structure"]},
"notes": ["Parameters: maxparents 4, k 1, tiers 3, threshold 0.5 -- BNW's own defaults."]
});
for (query, must_contain) in [
("", "posterior"),
("?kind=posterior", "edge posteriors"),
("?kind=network&trait=BXD::hippocampus", "BXD::hippocampus"),
] {
let response = post_json_with_token(
"POST /plot/bnw",
&format!("/plot/bnw{query}"),
&token,
&posted,
)
.await;
let status = response.status();
let summary = response
.headers()
.get("x-plot-summary")
.and_then(|value| value.to_str().ok())
.unwrap_or_default()
.to_string();
let bytes = response.into_body().collect().await.unwrap().to_bytes();
let svg = String::from_utf8_lossy(&bytes);
assert_eq!(status, StatusCode::OK, "{query}: {svg}");
assert!(svg.starts_with("<svg"), "{query}: not an SVG");
assert!(svg.contains(must_contain), "{query}: expected {must_contain:?} in the figure");
assert!(summary.contains("Trait2 -> Trait1"), "{query}: summary {summary:?}");
}
}
#[tokio::test]
async fn a_body_that_is_not_a_bnw_result_is_a_422() {
let Some(token) = auth_token().await else {
eprintln!("skipping a_body_that_is_not_a_bnw_result_is_a_422: set GN4_TEST_AUTH_TOKEN");
return;
};
for (posted, expected) in [
(serde_json::json!({"data": [], "meta": {"columns": []}}), "not_a_bnw_result"),
(
serde_json::json!({"data": [["m1", 1, 2.0]], "meta": {"columns": ["marker", "chr", "lod"]}}),
"not_a_bnw_result",
),
(
serde_json::json!({
"data": [["A", "B", 1.0, true]],
"meta": {"columns": ["from", "to", "posterior", "in_structure"]}
}),
"invalid_kind", // with ?kind=nonsense below
),
][..2]
.iter()
.cloned()
.chain(std::iter::once((
serde_json::json!({
"data": [["A", "B", 1.0, true]],
"meta": {"columns": ["from", "to", "posterior", "in_structure"]}
}),
"invalid_kind",
)))
{
let path = if expected == "invalid_kind" {
"/plot/bnw?kind=nonsense".to_string()
} else {
"/plot/bnw".to_string()
};
let response = post_json_with_token("POST /plot/bnw", &path, &token, &posted).await;
let status = response.status();
let bytes = response.into_body().collect().await.unwrap().to_bytes();
let reply: Value = serde_json::from_slice(&bytes).unwrap();
assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "{reply}");
assert_eq!(reply["code"], expected, "{reply}");
}
}
|