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
|
//! `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"
);
}
|