about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-08 15:45:48 +0000
committerPjotr <wrk@bosa.lan>2026-11-08 15:45:48 +0000
commitfc0e8fff3c1bed8d40e982a0a5e808180e772590 (patch)
treebe76e5db9c0566adc50f9d193a786601010f554d
parent386aed031612abc118e3390638cb5aff144be971 (diff)
downloadgenenetwork4-fc0e8fff3c1bed8d40e982a0a5e808180e772590.tar.gz
compute: refuse GEMMA permutations instead of ignoring them
`gemma-wrapper --permutate n` repeats the *whole* LOCO association scan
per permutation -- it shuffles the phenotype file and starts over -- so a
thousand permutations is thousands of full scans: hours to days on a real
panel, against the single slot `/compute/gemma` has. That is not a test
this deployment carries, and an assistant asked for significance
thresholds will ask for it, so the answer has to be an explicit one.

Until now the request type simply had no such field and serde ignores
unknown keys, which is the worst of the three outcomes: the caller gets a
200, no thresholds, and no indication that what it asked for was dropped.
`nperm`, `permutations` and `permutate` are now named fields and refused
with `422 permutations_not_supported`, naming the cost, pointing at
`POST /compute/rqtl` with `nperm` (the endpoint that does have the test,
over the same panel and values) and at the `actions` member every
successful GEMMA response already carries, pre-filled with exactly that
call. Zero keeps its usual meaning -- off -- so a client sharing one body
shape across the two endpoints still works.

* src/routes/gemma.rs: the three fields, the refusal before anything
  expensive runs, and the response note now saying permutations are
  refused rather than merely absent.
* tests/gemma.rs: `compute_gemma_refuses_permutations` pins all three
  spellings, the code, and that `nperm: 0` is not a request for any. It
  takes the compute lock: the gate answers before the handler does, so a
  concurrent job in the same test binary came back 409 instead.
* README.md, docs/www/ai-instruction.md: what to do instead -- judge a
  peak against the smallest `p_lrt`, and get thresholds from R/qtl.

273 tests pass; the same four as before do not.
-rw-r--r--README.md29
-rw-r--r--docs/www/ai-instruction.md23
-rw-r--r--src/routes/gemma.rs44
-rw-r--r--tests/gemma.rs64
4 files changed, 147 insertions, 13 deletions
diff --git a/README.md b/README.md
index af41b51..11baf94 100644
--- a/README.md
+++ b/README.md
@@ -992,6 +992,20 @@ genotype file, a SNP annotation file and a list of trait values. See
 real against the BXD fixtures -- GeneNetwork3's always errors out;
 GeneNetwork2's non-LOCO branch does too; only `--loco` works).
 
+**No permutations.** GEMMA's permutation test is not run here and
+`nperm`/`permutations`/`permutate` are **refused** (`422
+permutations_not_supported`), not ignored -- an ignored flag is worse than
+a refusal, because the caller believes it got thresholds. The reason is
+cost: `gemma-wrapper --permutate` shuffles the phenotype file and repeats
+the entire LOCO association scan per permutation, so a thousand is
+thousands of full scans -- hours to days on a real panel, against the one
+slot this endpoint has. R/qtl does have the test, on the same panel and the
+same values: `POST /compute/rqtl` with `"nperm": 100` returns the
+suggestive and significant LOD thresholds, and every successful GEMMA
+response already carries that call, pre-filled, in `actions`. Judge a GEMMA
+peak against the smallest `p_lrt` it reached, never against a genome-wide
+threshold of its own.
+
 **Covariates.** `covariates` fits one or more covariates alongside the
 trait -- the same request shape `/compute/rqtl` takes (below), each
 either a **case attribute of the group** by name, which the server reads,
@@ -1048,12 +1062,15 @@ it suggests one with `nperm: 100` in `actions` -- the thresholds a peak
 needs to be judged against -- and `next` carries a PheWAS at the peak the
 run found.
 
-**No permutation test.** GEMMA reports a p-value per marker and has no
-permutation mode, so this endpoint has no `nperm`: judge a peak against
-the smallest `p_lrt` it reached. The response's `actions` suggests the
-R/qtl permutation run over the same panel and values, which is where
-genome-wide thresholds come from here. (gemma-wrapper's own
-`--permutate n` shuffles phenotypes, but this endpoint does not pass it.)
+**No permutation test, and requesting one is an error.** GEMMA reports a
+p-value per marker; `gemma-wrapper --permutate n` does exist, but it
+repeats the whole LOCO scan per permutation, so it is hours of the one
+slot this endpoint has. `nperm`, `permutations` and `permutate` are
+therefore refused (`422 permutations_not_supported`) rather than ignored,
+and a peak is judged against the smallest `p_lrt` it reached. The
+response's `actions` carries the R/qtl permutation run over the same panel
+and values, pre-filled, which is where genome-wide thresholds come from
+here.
 
 **Give it a group, not a path.** `group` (an InbredSet name) plus an
 optional `genotype_version` makes the server resolve the genotype files
diff --git a/docs/www/ai-instruction.md b/docs/www/ai-instruction.md
index fe033d4..8c76cee 100644
--- a/docs/www/ai-instruction.md
+++ b/docs/www/ai-instruction.md
@@ -474,12 +474,23 @@ all-zero column is singular, and GEMMA stops rather than dropping the
 term). Either way, covariates reach only the association step and not the
 kinship, so adding one does not recompute the relatedness matrix.
 
-**GEMMA has no permutation test.** Its `actions` therefore suggests the
-*R/qtl* permutation run on the same group and the same values, and says
-so; judge a GEMMA peak against the smallest `p_lrt` it reached, not
-against a genome-wide threshold. (gemma-wrapper itself can shuffle
-phenotypes with `--permutate n`; this API does not pass it — see
-docs/genotypes.md.)
+**GEMMA has no permutation test, and do not ask for one.** A permutation
+run repeats the entire LOCO association scan for every permutation
+(`gemma-wrapper --permutate` shuffles the phenotype file and starts over),
+so a thousand permutations is thousands of full scans — hours to days on
+a real panel, on the one slot GEMMA has here. `/compute/gemma` therefore
+**refuses** `nperm`, `permutations` and `permutate` with `422
+permutations_not_supported` rather than ignoring them: an ignored flag is
+how you end up believing you have thresholds you never computed. Zero is
+accepted and means off, as elsewhere.
+
+For thresholds, use the endpoint that has the test: send the *same* panel
+and the *same* values to `POST /compute/rqtl` with `"nperm": 100`, which
+returns `suggestive` and `significant` LOD thresholds. Every successful
+GEMMA response already carries that call, pre-filled, in `actions` — take
+that rather than assembling your own. Until then, judge a GEMMA peak
+against the smallest `p_lrt` it reached, not against a genome-wide
+threshold of its own.
 
 **A few jobs at a time, one per caller.** Each compute endpoint has a
 fixed number of slots — **1** for `/compute/gemma`, **1** for
diff --git a/src/routes/gemma.rs b/src/routes/gemma.rs
index 38f0a8f..66caf57 100644
--- a/src/routes/gemma.rs
+++ b/src/routes/gemma.rs
@@ -78,6 +78,14 @@ pub struct GemmaRequest {
     /// carries `p_lrt`, not `p_wald` -- some other mapping than the
     /// stock 1-4 scale, not independently verified here.
     pub lmm: Option<u8>,
+    /// Refused unless 0: GEMMA permutations are not supported here (see
+    /// `permutations_not_supported`). Named so that asking for them is an
+    /// error rather than a silently ignored key.
+    pub nperm: Option<u32>,
+    /// Refused unless 0, as `nperm`.
+    pub permutations: Option<u32>,
+    /// Refused unless 0, as `nperm`.
+    pub permutate: Option<u32>,
 }
 
 /// One trait value, by individual name.
@@ -141,6 +149,36 @@ pub async fn compute_gemma(
         }
     }
 
+    // Permutations are refused rather than ignored: gemma-wrapper's
+    // `--permutate` shuffles the phenotype file and runs the *whole* LOCO
+    // GWA again for each permutation, so 1000 of them is thousands of
+    // full scans -- hours to days on a real panel, with the single slot
+    // this endpoint has. R/qtl has the permutation test (nperm), on the
+    // same panel and the same values.
+    let requested_permutations = [request.nperm, request.permutations, request.permutate]
+        .into_iter()
+        .flatten()
+        .max()
+        .unwrap_or(0);
+    if requested_permutations > 0 {
+        return Err(ApiError::new(
+            StatusCode::UNPROCESSABLE_ENTITY,
+            "permutations_not_supported",
+            "GEMMA permutations are not supported",
+            format!(
+                "{requested_permutations} permutations requested, but this endpoint runs none: \
+                 each one repeats the entire LOCO association scan (gemma-wrapper --permutate), \
+                 so a thousand is thousands of scans and hours of the one slot GEMMA has here. \
+                 For significance thresholds, send the same trait values to POST /compute/rqtl \
+                 with \"nperm\": 100 -- R/qtl has the permutation test and reports the \
+                 suggestive and significant LOD thresholds; judge a GEMMA peak against the \
+                 smallest p_lrt it reached."
+            ),
+            "compute_gemma",
+        )
+        .with_request_id(rid));
+    }
+
     let config = Config::load();
     let mut extra_notes: Vec<String> = Vec::new();
     let request_group = request.group.clone();
@@ -402,7 +440,11 @@ pub async fn compute_gemma(
         );
         envelope = envelope.with_note(
             "GEMMA reports a p-value per marker, not a permutation threshold: judge a peak \
-             against the smallest p_lrt it reached, not against a genome-wide threshold.",
+             against the smallest p_lrt it reached, not against a genome-wide threshold. \
+             Permutations are deliberately not run here -- each one repeats the whole LOCO \
+             scan -- so a request asking for them is refused, not silently ignored. Use \
+             POST /compute/rqtl with nperm for suggestive and significant LOD thresholds on \
+             the same values.",
         );
         // The picture: post this response back (no body duplicated here
         // -- it is the response the caller already has).
diff --git a/tests/gemma.rs b/tests/gemma.rs
index b1a145d..7220e4c 100644
--- a/tests/gemma.rs
+++ b/tests/gemma.rs
@@ -632,3 +632,67 @@ async fn compute_gemma_fits_a_case_attribute_as_a_covariate() {
         "the error lists what the group has: {error}"
     );
 }
+
+/// Permutations are refused outright, not ignored: gemma-wrapper's
+/// `--permutate` repeats the whole LOCO scan per permutation, which is
+/// hours on a real panel. R/qtl has the test, so the error says so.
+#[tokio::test]
+async fn compute_gemma_refuses_permutations() {
+    // The gate answers before the handler does, so a job from another test
+    // in this binary would come back 409 instead of the refusal under test.
+    let _serial = serialised();
+    let Some(token) = common::auth_token().await else {
+        eprintln!("skipping compute_gemma_refuses_permutations: no token (see docs/gn-auth.md)");
+        return;
+    };
+
+    for field in ["nperm", "permutations", "permutate"] {
+        let mut 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(),
+        });
+        body[field] = serde_json::json!(100);
+        let response =
+            common::post_json_with_token("POST /compute/gemma (permutations)", "/compute/gemma", &token, &body)
+                .await;
+        let status = response.status();
+        let bytes = response.into_body().collect().await.unwrap().to_bytes();
+        assert_eq!(
+            status,
+            StatusCode::UNPROCESSABLE_ENTITY,
+            "{field} must be refused: {}",
+            String::from_utf8_lossy(&bytes)
+        );
+        let error: Value = serde_json::from_slice(&bytes).unwrap();
+        assert_eq!(error["code"], "permutations_not_supported", "{error}");
+        let detail = error["detail"].as_str().unwrap();
+        assert!(detail.contains("100 permutations"), "{detail}");
+        assert!(
+            detail.contains("/compute/rqtl") && detail.contains("nperm"),
+            "the error points at the endpoint that can do it: {detail}"
+        );
+    }
+
+    // Zero permutations is not a request for any: the field is accepted
+    // and ignored, the way every other endpoint treats its own off-switch.
+    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(),
+        "nperm": 0,
+        "lmm": 99,
+    });
+    let response =
+        common::post_json_with_token("POST /compute/gemma (nperm 0)", "/compute/gemma", &token, &body)
+            .await;
+    // Refused for the lmm value, which proves the request got past the
+    // permutation check rather than being rejected for it.
+    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["code"] != "permutations_not_supported",
+        "nperm 0 is not a permutation request: {error}"
+    );
+}