about summary refs log tree commit diff
path: root/tests/matrices.rs
blob: 29359f082b42de6fdb90c2550aaac311f82646ae (plain)
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
//! Uploading new traits and covariates as one named matrix.
//!
//! A matrix is checked against a genotype version's panel before it is stored:
//! every individual of that version must appear exactly once. These tests walk
//! the real loop -- build a table for BXD.4's own individuals, upload it, map a
//! column with R/qtl2 -- and the refusals that keep a mismatched upload from
//! becoming a silently wrong mapping.

use axum::http::StatusCode;
use http_body_util::BodyExt;
use serde_json::Value;

mod common;

fn unique() -> String {
    format!(
        "matrix-test-{}",
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|elapsed| elapsed.as_millis())
            .unwrap_or(0)
    )
}

/// The individuals of a genotype version, as `/groups/{group}/genotypes/{version}`
/// reports them.
async fn panel(token: &str) -> Vec<String> {
    let response = common::get_with_token(
        "GET /groups/BXD/genotypes/BXD.4",
        "/groups/BXD/genotypes/BXD.4",
        token,
    )
    .await;
    assert_eq!(response.status(), StatusCode::OK);
    let bytes = response.into_body().collect().await.unwrap().to_bytes();
    let detail: Value = common::data(&bytes);
    detail["individuals"]
        .as_array()
        .unwrap()
        .iter()
        .map(|name| name.as_str().unwrap().to_string())
        .collect()
}

/// A deterministic table for a panel: two traits and one covariate.
fn table(individuals: &[String]) -> String {
    let mut text = String::from("id,body_weight,glucose,batch\n");
    for (index, individual) in individuals.iter().enumerate() {
        text.push_str(&format!(
            "{individual},{:.3},{:.3},{}\n",
            10.0 + (index % 17) as f64 * 1.5,
            5.0 + (index % 11) as f64 * 0.75,
            1 + index % 3
        ));
    }
    text
}

#[tokio::test]
async fn a_matrix_uploads_against_a_panel_and_its_columns_map() {
    let Some(token) = common::auth_token().await else {
        eprintln!("skipping a_matrix_uploads_against_a_panel_and_its_columns_map: no token");
        return;
    };
    if !common::db_available() {
        eprintln!("skipping a_matrix_uploads_against_a_panel_and_its_columns_map: no database");
        return;
    }
    let individuals = panel(&token).await;
    assert!(individuals.len() > 50, "BXD.4's panel: {}", individuals.len());
    let name = unique();

    let response = common::post_json_with_token(
        "POST /matrices",
        "/matrices",
        &token,
        &serde_json::json!({
            "name": name,
            "genotype_version": "BXD.4",
            "description": "an uploaded table",
            "contents": table(&individuals),
            "kinds": {"batch": "covariate"},
        }),
    )
    .await;
    let status = response.status();
    let bytes = response.into_body().collect().await.unwrap().to_bytes();
    assert_eq!(status, StatusCode::CREATED, "{}", String::from_utf8_lossy(&bytes));
    let matrix: Value = common::data(&bytes);
    assert_eq!(matrix["individuals"], individuals.len());
    let columns = matrix["columns"].as_array().unwrap();
    assert_eq!(columns.len(), 3);
    let names: Vec<&str> = columns
        .iter()
        .map(|column| column["name"].as_str().unwrap())
        .collect();
    assert_eq!(names, vec!["body_weight", "glucose", "batch"]);
    assert_eq!(columns[2]["kind"], "covariate", "kinds says which are covariates");
    assert_eq!(columns[0]["kind"], "trait");
    assert_eq!(columns[0]["basket"], serde_json::json!(format!("{name}.body_weight")));
    assert_eq!(columns[0]["missing"], 0);

    // The matrix reads back as a table.
    let read_back = common::get_with_token(
        "GET /matrices/{name}/values",
        &format!("/matrices/{name}/values"),
        &token,
    )
    .await;
    assert_eq!(read_back.status(), StatusCode::OK);
    let bytes = read_back.into_body().collect().await.unwrap().to_bytes();
    let values: Value = common::data(&bytes);
    assert_eq!(values["individuals"].as_array().unwrap().len(), individuals.len());
    assert_eq!(values["columns"].as_array().unwrap().len(), 3);
    assert_eq!(values["columns"][0]["values"][0].as_f64(), Some(10.0));

    // A column maps like any basket, on any engine.
    let mapped = common::post_json_with_token(
        "POST /compute/qtl2 (matrix column)",
        "/compute/qtl2",
        &token,
        &serde_json::json!({
            "group": "BXD",
            "genotype_version": "BXD.4",
            "trait_name": format!("{name}.body_weight"),
            "basket": format!("{name}.body_weight"),
        }),
    )
    .await;
    let status = mapped.status();
    let bytes = mapped.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["data"].as_array().unwrap().is_empty());
    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("Basket ") && note.contains(".body_weight")),
        "the mapping says which column ran: {notes:?}"
    );

    let deleted = common::delete_with_token(
        "DELETE /matrices/{name}",
        &format!("/matrices/{name}"),
        &token,
    )
    .await;
    assert_eq!(deleted.status(), StatusCode::NO_CONTENT);
    let gone = common::get_with_token(
        "GET /matrices/{name}",
        &format!("/matrices/{name}"),
        &token,
    )
    .await;
    assert_eq!(gone.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn an_upload_that_does_not_match_the_panel_is_refused() {
    let Some(token) = common::auth_token().await else {
        eprintln!("skipping an_upload_that_does_not_match_the_panel_is_refused: no token");
        return;
    };
    if !common::db_available() {
        eprintln!("skipping an_upload_that_does_not_match_the_panel_is_refused: no database");
        return;
    }
    let individuals = panel(&token).await;

    // One individual short: the count is the first thing that must match.
    let short = table(&individuals[..individuals.len() - 1]);
    let response = common::post_json_with_token(
        "POST /matrices (too few)",
        "/matrices",
        &token,
        &serde_json::json!({
            "name": unique(),
            "genotype_version": "BXD.4",
            "contents": short,
        }),
    )
    .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"], "invalid_matrix");
    let detail = error["detail"].as_str().unwrap();
    assert!(
        detail.contains(&format!("{}", individuals.len())),
        "the refusal says how many the version has: {detail}"
    );

    // A name the panel does not have, with the count unchanged.
    let mut renamed = individuals.clone();
    renamed[0] = "NOT-AN-INDIVIDUAL".to_string();
    let response = common::post_json_with_token(
        "POST /matrices (wrong names)",
        "/matrices",
        &token,
        &serde_json::json!({
            "name": unique(),
            "genotype_version": "BXD.4",
            "contents": table(&renamed),
        }),
    )
    .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!(
        error["detail"].as_str().unwrap().contains("NOT-AN-INDIVIDUAL"),
        "{error}"
    );

    // A version that does not exist is a 404, not a validation puzzle.
    let response = common::post_json_with_token(
        "POST /matrices (unknown version)",
        "/matrices",
        &token,
        &serde_json::json!({
            "name": unique(),
            "genotype_version": "NOSUCH.9",
            "contents": table(&individuals),
        }),
    )
    .await;
    assert_eq!(response.status(), StatusCode::NOT_FOUND);
}