about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-09 13:00:47 +0000
committerPjotr <wrk@bosa.lan>2026-11-09 13:00:47 +0000
commit216d1491435cce156f89a01684eb5c07e07db31e (patch)
treeee9eb9b183a60c4916ae9d26c60d7a26876a1fc6 /tests
parentc560ce726bc0c45ac87b8f0f9bf83870e25ac195 (diff)
downloadgenenetwork4-216d1491435cce156f89a01684eb5c07e07db31e.tar.gz
compute: report GEMMA's effect size next to the score
A GEMMA row now carries the effect beside the p-value it belongs to:
`… beta, se, l_mle, additive, p_lrt`. `additive` is `-beta/2`, which is
what GeneNetwork's own tables and CSV call the additive effect
(gn2/wqflask/marker_regression/gemma_mapping.py:240). Halved because half
the difference between the two homozygotes is the usual additive effect;
negated because GN2's BIMBAM conversion codes `@mat` as 1, so GEMMA's beta
is per *maternal* copy while GeneNetwork's published sign is anchored on the
other allele. Two facts that make this worth doing carefully:

* `beta` and `se` are left exactly where GEMMA put them. `se` is what makes
  the effect interpretable (`beta +/- 1.96*se` is a 95% interval) and both
  are GEMMA's own numbers, so replacing them with a derived one would lose
  information;
* the derived column goes *before* the p-value, not after it. `p_lrt` must
  stay the last column: `gemma::peak_marker` reads `row.last()` for the peak
  (and with it the PheWAS and plot suggestions), and the plotters and the
  docs all say "the last column is the p-value". Appending would have
  silently pointed every peak at an effect size.

The response also says what the three columns mean, and that they describe
*this run* on these samples and this genotype version -- not the `additive`
trait info reports, which came from the published mapping (trait 20010:
this run -0.54, published +2.118, same peak marker).

Nothing is added when GEMMA wrote no `beta` (a non-LMM run): the columns
are then exactly GEMMA's.

* src/gemma.rs: `with_effect_size`, applied by `parse_assoc_file`, so
  /compute/gemma and /compute/gemma/ravanan both get it.
* src/routes/gemma.rs: the note.
* tests: four unit tests (placement, sign and halving, `peak_marker` still
  finds the peak from the last column, a table without beta untouched, a
  null effect for a row with `NA` beta) and one end-to-end test asserting
  the column sits immediately before `p_lrt`, every value is `-beta/2`,
  `p_lrt` stays a probability, and the note is there.
* README.md (the response example and what the column means),
  docs/www/SKILL.md (how to read and quote it).

286 tests pass; the same four as before do not.
Diffstat (limited to 'tests')
-rw-r--r--tests/gemma.rs91
1 files changed, 91 insertions, 0 deletions
diff --git a/tests/gemma.rs b/tests/gemma.rs
index df3f1f5..c1c1201 100644
--- a/tests/gemma.rs
+++ b/tests/gemma.rs
@@ -718,3 +718,94 @@ async fn compute_gemma_refuses_permutations() {
         "nperm 0 is not a permutation request: {error}"
     );
 }
+
+/// The effect size comes back **next to the score**: `additive = −beta/2`
+/// immediately before `p_lrt`, with GEMMA's own `beta`/`se` untouched and
+/// `p_lrt` still last (peak detection and the plotter read it there).
+#[tokio::test]
+async fn compute_gemma_reports_the_effect_next_to_the_score() {
+    let _serial = serialised();
+    if !gemma_wrapper_available() {
+        eprintln!("skipping compute_gemma_reports_the_effect_next_to_the_score: gemma-wrapper not on PATH");
+        return;
+    }
+    let Some(token) = common::auth_token().await else {
+        eprintln!("skipping compute_gemma_reports_the_effect_next_to_the_score: no token (see docs/gn-auth.md)");
+        return;
+    };
+
+    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(),
+        "trait_name": "BXD::fixture",
+    });
+    let response =
+        common::post_json_with_token("POST /compute/gemma (effect)", "/compute/gemma", &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();
+    let beta_at = columns.iter().position(|name| *name == "beta").expect("beta");
+    let se_at = columns.iter().position(|name| *name == "se").expect("se");
+    let additive_at = columns
+        .iter()
+        .position(|name| *name == "additive")
+        .expect("the effect size column");
+    let rows = envelope["data"].as_array().unwrap();
+    assert_eq!(
+        additive_at,
+        columns.len() - 2,
+        "additive sits immediately before the score: {columns:?}"
+    );
+    assert_eq!(
+        columns[additive_at + 1],
+        "p_lrt",
+        "and the p-value stays last: {columns:?}"
+    );
+
+    for row in rows.iter().take(50) {
+        let beta = row[beta_at].as_f64().expect("a numeric beta");
+        let additive = row[additive_at].as_f64().expect("a numeric additive");
+        assert!(
+            (additive - (-beta / 2.0)).abs() < 1e-9,
+            "additive = -beta/2: beta {beta}, additive {additive}"
+        );
+        assert!(row[se_at].is_number(), "and se is still there: {row}");
+    }
+    // The last column is still the p-value, not the effect: every value in
+    // it is a probability, and the additive column is not.
+    let p_at = columns.len() - 1;
+    assert!(
+        rows.iter().all(|row| row[p_at]
+            .as_f64()
+            .is_some_and(|p| (0.0..=1.0).contains(&p))),
+        "p_lrt stays a probability in the last column"
+    );
+    let effects: Vec<f64> = rows
+        .iter()
+        .filter_map(|row| row[additive_at].as_f64())
+        .collect();
+    assert!(
+        !effects.is_empty() && effects.iter().any(|value| *value != 0.0),
+        "the effect column carries numbers, not placeholders: {effects:?}"
+    );
+    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("Effect size:")),
+        "and the response says what the three columns mean: {notes:?}"
+    );
+}