about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/gemma.rs181
1 files changed, 181 insertions, 0 deletions
diff --git a/tests/gemma.rs b/tests/gemma.rs
index 4ded3ab..b1a145d 100644
--- a/tests/gemma.rs
+++ b/tests/gemma.rs
@@ -12,6 +12,19 @@ mod common;
 
 /// True only when gemma-wrapper is actually runnable. `-v` is cheap and
 /// does not touch the cache dir or invoke the real GEMMA binary.
+/// The tests that run a real GEMMA job take this lock first: the compute
+/// gate refuses a second job from the same caller with 409 instead of
+/// queueing it, and every test shares the one token.
+static COMPUTE_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
+
+/// Take [`COMPUTE_LOCK`], ignoring poisoning (one failing test must not
+/// fail the rest).
+fn serialised() -> std::sync::MutexGuard<'static, ()> {
+    COMPUTE_LOCK
+        .lock()
+        .unwrap_or_else(|poisoned| poisoned.into_inner())
+}
+
 fn gemma_wrapper_available() -> bool {
     std::process::Command::new("gemma-wrapper")
         .arg("-v")
@@ -52,6 +65,7 @@ fn bxd_trait_values() -> Vec<Option<f64>> {
 
 #[tokio::test]
 async fn compute_gemma_maps_a_qtl_from_the_bxd_fixtures() {
+    let _serial = serialised();
     if !gemma_wrapper_available() {
         eprintln!(
             "skipping compute_gemma_maps_a_qtl_from_the_bxd_fixtures: gemma-wrapper not on PATH"
@@ -451,3 +465,170 @@ async fn compute_gemma_rejects_a_run_id_already_in_use() {
 
     std::fs::remove_file(std::path::Path::new(&cache_dir).join(format!("{run_id}.log"))).unwrap();
 }
+
+/// GEMMA with a covariate: a case attribute of the group, dummy-expanded
+/// into a covariate file of its own. Needs gemma-wrapper, a database and
+/// the genotype files.
+///
+/// BXD, its classic `BXD.4` panel (3 811 markers, so this is a mechanism
+/// test rather than a mapping) and the `Status` case attribute, which has
+/// five levels among this trait's 34 strains -- so four dummy columns, and
+/// a check that a categorical covariate really is expanded rather than
+/// fitted as one ordinal number.
+#[tokio::test]
+async fn compute_gemma_fits_a_case_attribute_as_a_covariate() {
+    let _serial = serialised();
+    if !gemma_wrapper_available() {
+        eprintln!(
+            "skipping compute_gemma_fits_a_case_attribute_as_a_covariate: gemma-wrapper not on PATH"
+        );
+        return;
+    }
+    let Some(token) = common::auth_token().await else {
+        eprintln!(
+            "skipping compute_gemma_fits_a_case_attribute_as_a_covariate: no token (see docs/gn-auth.md)"
+        );
+        return;
+    };
+    if !common::db_available() {
+        eprintln!(
+            "skipping compute_gemma_fits_a_case_attribute_as_a_covariate: no usable database"
+        );
+        return;
+    }
+    let directory = genenetwork4::config::Config::load().genotype_files_dir;
+    if !std::path::Path::new(&directory).is_dir() {
+        eprintln!(
+            "skipping compute_gemma_fits_a_case_attribute_as_a_covariate: no genotype files"
+        );
+        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: Vec<Value> = common::data(&bytes);
+
+    // Runs one request and hands back its status and body, so each step
+    // below reads as "post this, look at that".
+    let post = |samples: Vec<Value>, covariates: Value| {
+        let body = serde_json::json!({
+            "group": "BXD",
+            "genotype_version": "BXD.4",
+            "samples": samples,
+            "covariates": covariates,
+        });
+        let token = token.clone();
+        async move {
+            let response =
+                common::post_json_with_token("POST /compute/gemma", "/compute/gemma", &token, &body)
+                    .await;
+            let status = response.status();
+            let bytes = response.into_body().collect().await.unwrap().to_bytes();
+            (status, bytes)
+        }
+    };
+
+    // The same trait without covariates, for comparison.
+    let (status, bytes) = post(samples.clone(), serde_json::json!([])).await;
+    assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes));
+    let plain: Value = serde_json::from_slice(&bytes).unwrap();
+
+    let (status, bytes) = post(
+        samples.clone(),
+        serde_json::json!([{"case_attribute": "Status"}]),
+    )
+    .await;
+    assert_eq!(status, StatusCode::OK, "{}", String::from_utf8_lossy(&bytes));
+    let with_status: Value = serde_json::from_slice(&bytes).unwrap();
+    assert!(!with_status["data"].as_array().unwrap().is_empty());
+
+    let notes: Vec<&str> = with_status["notes"]
+        .as_array()
+        .unwrap()
+        .iter()
+        .map(|note| note.as_str().unwrap())
+        .collect();
+    let covariate_note = notes
+        .iter()
+        .find(|note| note.starts_with("Covariate Status:"))
+        .expect("a note saying what the covariate was");
+    assert!(covariate_note.contains("categorical"), "{covariate_note}");
+    assert!(
+        covariate_note.contains("coded"),
+        "the coding is reported: {covariate_note}"
+    );
+    // A categorical covariate is expanded, never fitted as one ordinal
+    // number: several levels, one reference, a dummy for each of the rest.
+    assert!(
+        covariate_note.contains("dummy column(s) named Status.<level>"),
+        "a categorical covariate is dummy-expanded, not fitted as one number: {covariate_note}"
+    );
+    // A covariate only ever reaches the GWA step.
+    let kinship = notes
+        .iter()
+        .find(|note| note.starts_with("Kinship:"))
+        .expect("the kinship command line");
+    assert!(!kinship.contains(" -c "), "{kinship}");
+    let gwa = notes
+        .iter()
+        .find(|note| note.starts_with("GWA:"))
+        .expect("the GWA command line");
+    assert!(gwa.contains(" -c "), "{gwa}");
+    assert!(gwa.contains("covariates.txt"), "{gwa}");
+
+    // The covariate changed the fit: the wrapper hashes its file
+    // arguments, so this also proves the covariate file was part of the
+    // key rather than a cached no-covariate run being reused.
+    let peak_p = |envelope: &Value| {
+        let columns: Vec<&str> = envelope["meta"]["columns"]
+            .as_array()
+            .unwrap()
+            .iter()
+            .map(|c| c.as_str().unwrap())
+            .collect();
+        let index = columns.iter().position(|c| *c == "p_lrt").expect("p_lrt");
+        envelope["data"]
+            .as_array()
+            .unwrap()
+            .iter()
+            .filter_map(|row| row[index].as_f64())
+            .fold(f64::INFINITY, f64::min)
+    };
+    assert!(
+        (peak_p(&plain) - peak_p(&with_status)).abs() > 0.0,
+        "the covariate must change the fit, not reuse the plain run: {} vs {}",
+        peak_p(&plain),
+        peak_p(&with_status)
+    );
+
+    // Covariates are aligned by name, so explicit files cannot have them.
+    let body = serde_json::json!({
+        "genotype_file": test_data_path("BXD_geno.txt.gz"),
+        "snps_file": test_data_path("BXD_snps.txt"),
+        "trait_values": bxd_trait_values(),
+        "covariates": [{"case_attribute": "Epoch"}],
+    });
+    let response =
+        common::post_json_with_token("POST /compute/gemma (paths)", "/compute/gemma", &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");
+
+    // An attribute the group does not define is refused, listing its own.
+    let (status, bytes) = post(samples, serde_json::json!([{"case_attribute": "Sex"}])).await;
+    assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY);
+    let error: Value = serde_json::from_slice(&bytes).unwrap();
+    assert_eq!(error["code"], "covariate_unavailable");
+    assert!(
+        error["detail"].as_str().unwrap().contains("Status"),
+        "the error lists what the group has: {error}"
+    );
+}