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
|
//! `POST /compute/fitqtl`: effect sizes at named markers.
//!
//! Two checks, both meaningful: the fixture request exercises the endpoint's
//! shapes, and the group request cross-checks the *numbers* against
//! `/compute/rqtl` -- fitqtl at a marker and scanone at the same marker must
//! report the same LOD, because they are the same cross and the same
//! phenotype. (They do, to every digit the CSV carries: `fitqtl`'s `$lod` is
//! the fitted model's LOD at that position, which is what scanone computes
//! there.)
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 test_data_path(name: &str) -> String {
format!("{}/tests/test_data/{name}", env!("CARGO_MANIFEST_DIR"))
}
async fn fixture_with_token() -> Option<String> {
if !rscript_available() {
eprintln!("skipping: Rscript not on PATH");
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
}
}
}
/// The six-sample fixture: three markers fitted, in the documented columns,
/// with the effect's sign following the phenotype.
#[tokio::test]
async fn a_fixture_fitqtl_returns_effects_in_the_documented_columns() {
let Some(token) = fixture_with_token().await else {
return;
};
let body = serde_json::json!({
"genotype_file": test_data_path("rqtl_sample1.geno"),
"trait_name": "mytrait",
// 9.5 … 4.3 against BXD1, BXD2, BXD5, BXD6, BXD8, BXD9 -- a
// descending series, so the B-allele effect must be negative.
"trait_values": [9.5, 8.2, 7.1, 6.4, 5.9, 4.3],
"markers": ["rs31443144", "rs6269442", "rs32285189"]
});
let response = common::post_json_with_token(
"POST /compute/fitqtl",
"/compute/fitqtl",
&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 columns: Vec<&str> = envelope["meta"]["columns"]
.as_array()
.unwrap()
.iter()
.map(|name| name.as_str().unwrap())
.collect();
assert_eq!(
columns,
vec!["marker", "chr", "pos", "lod", "pct_var", "additive", "se", "t", "p"]
);
assert_eq!(envelope["meta"]["num_rows"], 3);
let at = |row: &Value, column: &str| -> Value {
let index = columns.iter().position(|name| *name == column).unwrap();
row[index].clone()
};
for row in envelope["data"].as_array().unwrap() {
assert!(at(row, "lod").as_f64().unwrap() >= 0.0);
assert!(at(row, "se").as_f64().unwrap() > 0.0);
let p = at(row, "p").as_f64().unwrap();
assert!((0.0..=1.0).contains(&p), "p is a probability: {row}");
assert!(
at(row, "additive").as_f64().unwrap() < 0.0,
"the phenotype falls with the B allele at these markers: {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: mytrait")));
assert!(
notes.iter().any(|note| note.starts_with("Rscript ")),
"the exact command line comes back: {notes:?}"
);
assert!(
notes.iter().any(|note| note.starts_with("fitqtl: wrote")),
"and the script's own summary: {notes:?}"
);
}
/// fitqtl and scanone must agree at the same marker, or one of them is
/// describing a different cross than the other.
#[tokio::test]
async fn the_lod_agrees_with_the_scan_at_the_same_marker() {
let Some(token) = fixture_with_token().await else {
return;
};
if !common::db_available() {
eprintln!("skipping the_lod_agrees_with_the_scan_at_the_same_marker: 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 scan_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, &scan_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 mut rows = scan["data"]["markers"].as_array().unwrap().clone();
rows.sort_by(|left, right| {
right[3]
.as_f64()
.unwrap()
.partial_cmp(&left[3].as_f64().unwrap())
.unwrap()
});
let peak = rows[0][0].as_str().unwrap().to_string();
let scan_lod = rows[0][3].as_f64().unwrap();
let fit_body = serde_json::json!({
"group": "BXD",
"genotype_version": "BXD.4",
"trait_name": "BXD::10001",
"samples": samples,
"markers": [peak.clone()],
});
let response = common::post_json_with_token(
"POST /compute/fitqtl (group)",
"/compute/fitqtl",
&token,
&fit_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 row = &envelope["data"][0];
assert_eq!(row[0].as_str().unwrap(), peak);
let fit_lod = row[3].as_f64().unwrap();
assert!(
(fit_lod - scan_lod).abs() < 1e-6,
"fitqtl at {peak} gives LOD {fit_lod}, scanone gives {scan_lod}"
);
assert!(
row[5].as_f64().unwrap().abs() > 0.0,
"and it comes with an effect, not just a statistic: {row}"
);
}
/// Refusals: no markers, too many markers, both genotype inputs.
#[tokio::test]
async fn fitqtl_refuses_inputs_it_cannot_fit() {
let Some(token) = fixture_with_token().await else {
return;
};
for (body, code) in [
(
serde_json::json!({
"genotype_file": test_data_path("rqtl_sample1.geno"),
"trait_values": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
"markers": []
}),
"missing_markers",
),
(
serde_json::json!({
"group": "BXD",
"genotype_file": test_data_path("rqtl_sample1.geno"),
"markers": ["rs31443144"]
}),
"ambiguous_genotype",
),
] {
let response =
common::post_json_with_token("POST /compute/fitqtl", "/compute/fitqtl", &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}");
}
}
/// Covariates, as on `/compute/rqtl`: a case attribute by name, resolved and
/// coded the same way, and reported in `notes`. A covariate can be spread
/// across the whole panel yet constant among the individuals a trait
/// happens to cover -- R/qtl then stops with "system is exactly singular",
/// so the script leaves those columns out and says which.
#[tokio::test]
async fn fitqtl_fits_a_covariate_and_reports_what_it_dropped() {
let Some(token) = fixture_with_token().await else {
return;
};
if !common::db_available() {
eprintln!("skipping fitqtl_fits_a_covariate_and_reports_what_it_dropped: 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,
"markers": ["mCV24409492"],
"covariates": [{"case_attribute": "Status"}],
});
let response = common::post_json_with_token(
"POST /compute/fitqtl (covariate)",
"/compute/fitqtl",
&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"]["num_rows"], 1,
"the marker is fitted with the covariate: {}",
envelope["notes"]
);
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("Covariate Status")),
"the same note /compute/rqtl gives: {notes:?}"
);
assert!(
notes.iter().any(|note| note.starts_with("fitqtl:")) ,
"{notes:?}"
);
// Fitting a covariate changes the fit, so the LOD is no longer the
// plain scan's -- that is the point of fitting it.
let row = &envelope["data"][0];
assert!(row[3].as_f64().unwrap() > 0.0);
assert!(row[5].as_f64().unwrap().abs() > 1.0);
// An attribute the group does not define is refused, listing its own.
let body = serde_json::json!({
"group": "BXD",
"trait_name": "t",
"samples": [{"name": "BXD1", "value": 1.0}],
"markers": ["mCV24409492"],
"covariates": [{"case_attribute": "Nope"}],
});
let response = common::post_json_with_token(
"POST /compute/fitqtl (bad covariate)",
"/compute/fitqtl",
&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"], "covariate_unavailable", "{error}");
}
|