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
|
//! `GET /groups/{group}/phewas` -- traits peaking within a window of one
//! position.
//!
//! Needs the database (traits), a token, and the deployment's genotype
//! files (the marker table that defines the window); skips without any of
//! them, like the other database-backed tests.
use axum::http::StatusCode;
use http_body_util::BodyExt;
use serde_json::Value;
mod common;
use common::{auth_token, db_available, get_with_token, post_json_with_token};
fn genotype_dir_present() -> bool {
let directory = genenetwork4::config::Config::load().genotype_files_dir;
std::path::Path::new(&directory).is_dir()
}
async fn get_json(uri: &str, token: &str) -> (StatusCode, Value) {
let response = get_with_token(&format!("GET {uri}"), uri, token).await;
let status = response.status();
let bytes = response.into_body().collect().await.unwrap().to_bytes();
(
status,
serde_json::from_slice(&bytes).unwrap_or(Value::Null),
)
}
#[tokio::test]
async fn a_window_around_a_qtl_peak_lists_the_traits_peaking_there() {
if !db_available() {
eprintln!("skipping a_window_around_a_qtl_peak_lists_the_traits_peaking_there: no usable database (check SQL_URI credentials)");
return;
}
let Some(token) = auth_token().await else {
eprintln!("skipping a_window_around_a_qtl_peak_lists_the_traits_peaking_there: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)");
return;
};
if !genotype_dir_present() {
eprintln!("skipping a_window_around_a_qtl_peak_lists_the_traits_peaking_there: no genotype-files directory (GENOTYPE_FILES)");
return;
}
// 1. Centred on a marker: rs30524230 is a real BXD QTL peak on chr 9
// (PublishXRef.Locus, LRS 460). Made a qtl by hand in the data.
let (status, body) = get_json(
"/groups/BXD/phewas?locus=rs30524230&window=10000",
&token,
)
.await;
assert_eq!(status, StatusCode::OK, "{body}");
let data: Value = common::data(&serde_json::to_vec(&body).unwrap());
let centre = &data["centre"];
let centre_mb = centre["mb"].as_f64().expect("a centre in Mb");
assert_eq!(centre["locus"], "rs30524230");
assert_eq!(data["window_bp"].as_f64().unwrap(), 10000.0);
let markers = data["markers"].as_array().expect("markers");
assert!(!markers.is_empty(), "a window at a QTL peak has markers: {body}");
let names: Vec<&str> = markers
.iter()
.map(|marker| marker["name"].as_str().unwrap())
.collect();
assert!(names.contains(&"rs30524230"), "{names:?}");
for marker in markers {
let mb = marker["mb"].as_f64().unwrap();
assert!(
(mb - centre_mb).abs() <= 0.01,
"every marker is in the window: {marker} vs {centre_mb}"
);
}
let traits = data["traits"].as_array().expect("traits");
assert!(!traits.is_empty(), "traits peak in this window: {body}");
let mut previous = f64::INFINITY;
for hit in traits {
let lrs = hit["lrs"].as_f64().unwrap();
assert!(lrs <= previous, "strongest first: {lrs} after {previous}");
previous = lrs;
assert_eq!(hit["chr"], "9", "{hit}");
assert!(
(hit["mb"].as_f64().unwrap() - centre_mb).abs() <= 0.01,
"{hit}"
);
let locus = hit["locus"].as_str().unwrap();
assert!(names.contains(&locus), "peaks at a window marker: {hit}");
// lod = lrs/4.604, the same rule as everywhere else.
assert!(
(hit["lod"].as_f64().unwrap() * 4.604 - lrs).abs() < 0.01,
"{hit}"
);
}
// 2. The same window given as chr+mb, and a zero-width window, which
// keeps only markers exactly at the position.
let (status, by_position) = get_json(
&format!("/groups/BXD/phewas?chr=9&mb={centre_mb}&window=0"),
&token,
)
.await;
assert_eq!(status, StatusCode::OK, "{by_position}");
let narrow: Value = common::data(&serde_json::to_vec(&by_position).unwrap());
assert!(
narrow["markers"].as_array().unwrap().len() <= names.len(),
"a zero-width window cannot hold more markers: {by_position}"
);
// 3. Failures: no position at all, and a marker that is not there.
let (status, body) = get_json("/groups/BXD/phewas?window=10000", &token).await;
assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "{body}");
assert_eq!(body["code"], "missing_position");
let (status, body) = get_json("/groups/BXD/phewas?locus=not-a-marker", &token).await;
assert_eq!(status, StatusCode::NOT_FOUND, "{body}");
// 4. A position with nothing near it is a 200 with an explanation,
// not an error.
let (status, body) = get_json("/groups/BXD/phewas?chr=9&mb=195.9&window=100", &token).await;
assert_eq!(status, StatusCode::OK, "{body}");
let empty: Value = common::data(&serde_json::to_vec(&body).unwrap());
if empty["markers"].as_array().unwrap().is_empty() {
assert!(
body["notes"]
.as_array()
.unwrap()
.iter()
.any(|note| note.as_str().unwrap().contains("No marker")),
"an empty window says why: {body}"
);
}
// 5. The suggested wider window resolves.
let (status, body) = get_json("/groups/BXD/phewas?locus=rs30524230&window=10000", &token).await;
assert_eq!(status, StatusCode::OK);
let wider = body["next"]
.as_array()
.unwrap()
.iter()
.find(|next| next["href"].as_str().unwrap().contains("window="))
.expect("a wider-window suggestion");
let href = wider["href"].as_str().unwrap();
let (status, _) = get_json(href, &token).await;
assert_eq!(status, StatusCode::OK, "the suggested window resolves: {href}");
}
/// R/qtl on a group needs Rscript; the same gate the mapping tests use.
fn rscript_available() -> bool {
std::process::Command::new("Rscript")
.arg("--version")
.output()
.map(|output| output.status.success())
.unwrap_or(false)
}
/// A group-scoped mapping ends with the two follow-ups: a PheWAS at the
/// peak (`next`, a GET that returns 200) and the same scan with 100
/// permutations (`actions`, a POST).
#[tokio::test]
async fn a_group_mapping_suggests_a_phewas_and_a_permutation_run() {
if !db_available() || !genotype_dir_present() || !rscript_available() {
eprintln!(
"skipping a_group_mapping_suggests_a_phewas_and_a_permutation_run: needs a \
database, genotype files and Rscript"
);
return;
}
let Some(token) = auth_token().await else {
eprintln!("skipping a_group_mapping_suggests_a_phewas_and_a_permutation_run: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)");
return;
};
// A handful of BXD strains is enough for a scan; names must exist in
// the panel, which the endpoint checks.
let samples: Vec<Value> = ["BXD1", "BXD2", "BXD5", "BXD6", "BXD8", "BXD9", "BXD11", "BXD12"]
.iter()
.enumerate()
.map(|(index, name)| serde_json::json!({"name": name, "value": index as f64 + 1.0}))
.collect();
let body = serde_json::json!({
"group": "BXD", "trait_name": "suggestion-check", "samples": samples,
});
let response =
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 envelope: Value = serde_json::from_slice(&bytes).unwrap();
let phewas = envelope["next"]
.as_array()
.unwrap()
.iter()
.find(|next| next["href"].as_str().unwrap().contains("/phewas?locus="))
.expect("a PheWAS suggestion at the peak")
.clone();
let href = phewas["href"].as_str().unwrap();
let (status, phewas_body) = get_json(href, &token).await;
assert_eq!(status, StatusCode::OK, "the PheWAS suggestion resolves: {phewas_body}");
let actions = envelope["actions"].as_array().expect("suggestions");
assert!(
actions.iter().any(|action| action["href"]
.as_str()
.is_some_and(|href| href.starts_with("/plot/rqtl"))),
"a plot suggestion (its href names the trait, so the plot titles itself): {envelope}"
);
let action = actions
.iter()
.find(|action| action["href"] == "/compute/rqtl")
.expect("a permutation suggestion");
assert_eq!(action["method"], "POST");
assert_eq!(action["body"]["nperm"], 100);
assert_eq!(action["body"]["group"], "BXD");
assert!(
action["description"]
.as_str()
.unwrap()
.to_lowercase()
.contains("permutation"),
"{action}"
);
}
|