about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-10-12 06:38:08 +0000
committerPjotr <wrk@bosa.lan>2026-10-12 06:38:08 +0000
commita60d3a1bc21e355b5342747be0ede3654d2cce41 (patch)
treec3e6ce6195b451dda76d0ff83854313743c8c9c8 /src
parent53ae1b4cbf8bd68278e7a59cd41a4c145f8362ab (diff)
downloadgenenetwork4-a60d3a1bc21e355b5342747be0ede3654d2cce41.tar.gz
gemma: document the endpoint in README; put the actual gemma-wrapper
commands in the response notes

README: added /compute/gemma to the endpoint table and a dedicated
section with a real example response, and the explicit caveat the
user asked for -- not feasible past roughly 100,000 markers at this
point (synchronous, sequential, no parallel LOCO jobs; the BXD
fixtures, 7,320 markers, already take ~19s, and real datasets go up to
~1M markers).

notes: the response now carries the exact gemma-wrapper invocations
used for the kinship and GWA steps ("Kinship: gemma-wrapper ... -gk",
"GWA: gemma-wrapper ... -lmm 9"), not just a generic description.
kinship_args/gwa_args build the argument list once and feed both the
actual subprocess call and the note text, so the two can't drift
apart. Added unit tests asserting -gk and -lmm never appear together,
and extended the real end-to-end test to check both notes' shape.

Verified: cargo test passes in full, including the real gemma-wrapper/
GEMMA run, with the notes field inspected directly against the actual
command lines used.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Diffstat (limited to 'src')
-rw-r--r--src/gemma.rs170
-rw-r--r--src/routes/gemma.rs10
2 files changed, 133 insertions, 47 deletions
diff --git a/src/gemma.rs b/src/gemma.rs
index eee16b6..9c74b30 100644
--- a/src/gemma.rs
+++ b/src/gemma.rs
@@ -122,25 +122,10 @@ async fn compute_kinship(
     pheno_file: &str,
     snps_file: &str,
 ) -> Result<Value, GemmaError> {
-    let output = run(
-        gemma_wrapper_cmd,
-        &[
-            "--json",
-            "--cache-dir",
-            cache_dir,
-            "--loco",
-            "--",
-            "-g",
-            genotype_file,
-            "-p",
-            pheno_file,
-            "-a",
-            snps_file,
-            "-gk",
-        ],
-    )
-    .await
-    .map_err(GemmaError::Io)?;
+    let args = kinship_args(cache_dir, genotype_file, pheno_file, snps_file);
+    let output = run(gemma_wrapper_cmd, &args)
+        .await
+        .map_err(GemmaError::Io)?;
     parse_wrapper_output("kinship", output)
 }
 
@@ -172,28 +157,17 @@ async fn compute_gwa(
     k_json_path: &str,
 ) -> Result<Value, GemmaError> {
     let lmm_str = lmm.to_string();
-    let output = run(
-        gemma_wrapper_cmd,
-        &[
-            "--json",
-            "--cache-dir",
-            cache_dir,
-            "--loco",
-            "--input",
-            k_json_path,
-            "--",
-            "-g",
-            genotype_file,
-            "-p",
-            pheno_file,
-            "-a",
-            snps_file,
-            "-lmm",
-            &lmm_str,
-        ],
-    )
-    .await
-    .map_err(GemmaError::Io)?;
+    let args = gwa_args(
+        cache_dir,
+        genotype_file,
+        pheno_file,
+        snps_file,
+        &lmm_str,
+        k_json_path,
+    );
+    let output = run(gemma_wrapper_cmd, &args)
+        .await
+        .map_err(GemmaError::Io)?;
     parse_wrapper_output("gwa", output)
 }
 
@@ -282,10 +256,73 @@ fn nanos_since_epoch() -> u128 {
         .unwrap_or(0)
 }
 
+/// The mapped QTL rows, plus the exact gemma-wrapper invocations used
+/// to produce them (see `compute`'s `notes` below), so a caller can see
+/// precisely what was run without reading server logs.
+pub struct GemmaOutput {
+    pub rows: Value,
+    pub notes: Vec<String>,
+}
+
+/// The gemma-wrapper arguments for the kinship step, shared between the
+/// actual invocation (`compute_kinship`) and the note describing it
+/// (`compute`), so the two can never drift apart.
+fn kinship_args<'a>(
+    cache_dir: &'a str,
+    genotype_file: &'a str,
+    pheno_file: &'a str,
+    snps_file: &'a str,
+) -> Vec<&'a str> {
+    vec![
+        "--json",
+        "--cache-dir",
+        cache_dir,
+        "--loco",
+        "--",
+        "-g",
+        genotype_file,
+        "-p",
+        pheno_file,
+        "-a",
+        snps_file,
+        "-gk",
+    ]
+}
+
+/// The gemma-wrapper arguments for the GWA step, same pairing as
+/// `kinship_args`.
+fn gwa_args<'a>(
+    cache_dir: &'a str,
+    genotype_file: &'a str,
+    pheno_file: &'a str,
+    snps_file: &'a str,
+    lmm_str: &'a str,
+    k_json_path: &'a str,
+) -> Vec<&'a str> {
+    vec![
+        "--json",
+        "--cache-dir",
+        cache_dir,
+        "--loco",
+        "--input",
+        k_json_path,
+        "--",
+        "-g",
+        genotype_file,
+        "-p",
+        pheno_file,
+        "-a",
+        snps_file,
+        "-lmm",
+        lmm_str,
+    ]
+}
+
 /// Run the full gemma-wrapper flow -- compute kinship, then run the LMM
 /// association test, both `--loco` -- and return the mapped QTL as a
 /// JSON array of rows (each a JSON object), concatenated across every
-/// chromosome's `.assoc.txt` from the real GEMMA output. The result is
+/// chromosome's `.assoc.txt` from the real GEMMA output, plus the two
+/// gemma-wrapper invocations used (`GemmaOutput::notes`). The rows are
 /// one `Value::Array`, not a `Vec<Value>`: utoipa (for the OpenAPI
 /// schema) implements `ToSchema` for `serde_json::Value` itself, but
 /// `Vec<serde_json::Value>` needs `Value: ComposeSchema`, which it does
@@ -297,13 +334,17 @@ pub async fn compute(
     snps_file: &str,
     trait_values: &TraitValues,
     lmm: u8,
-) -> Result<Value, GemmaError> {
+) -> Result<GemmaOutput, GemmaError> {
     std::fs::create_dir_all(cache_dir).map_err(GemmaError::Io)?;
     let unique = format!("{}-{}", std::process::id(), nanos_since_epoch());
     let pheno_path = Path::new(cache_dir).join(format!("{unique}-pheno.txt"));
     write_pheno_file(&pheno_path, trait_values).map_err(GemmaError::Io)?;
     let pheno_file = pheno_path.to_string_lossy().into_owned();
 
+    let kinship_note = format!(
+        "Kinship: {gemma_wrapper_cmd} {}",
+        kinship_args(cache_dir, genotype_file, &pheno_file, snps_file).join(" ")
+    );
     let k_record = compute_kinship(
         gemma_wrapper_cmd,
         cache_dir,
@@ -315,6 +356,19 @@ pub async fn compute(
     let k_json_path = Path::new(cache_dir).join(format!("{unique}-k.json"));
     std::fs::write(&k_json_path, k_record.to_string()).map_err(GemmaError::Io)?;
 
+    let lmm_str = lmm.to_string();
+    let gwa_note = format!(
+        "GWA: {gemma_wrapper_cmd} {}",
+        gwa_args(
+            cache_dir,
+            genotype_file,
+            &pheno_file,
+            snps_file,
+            &lmm_str,
+            &k_json_path.to_string_lossy(),
+        )
+        .join(" ")
+    );
     let gwa_record = compute_gwa(
         gemma_wrapper_cmd,
         cache_dir,
@@ -348,7 +402,10 @@ pub async fn compute(
         rows.extend(parse_assoc_file(&contents).into_iter().map(Value::Object));
     }
     let _ = std::fs::remove_dir_all(&extract_dir); // best effort; a leftover scratch dir is harmless
-    Ok(Value::Array(rows))
+    Ok(GemmaOutput {
+        rows: Value::Array(rows),
+        notes: vec![kinship_note, gwa_note],
+    })
 }
 
 #[cfg(test)]
@@ -405,4 +462,29 @@ mod tests {
         assert!(matches!(err, GemmaError::WrapperError { step: "kinship", .. }));
         assert_eq!(err.to_string(), "gemma-wrapper (kinship) reported an error: boom");
     }
+
+    #[test]
+    fn kinship_args_is_loco_with_gk_and_no_lmm() {
+        let args = kinship_args("/cache", "geno.txt.gz", "pheno.txt", "snps.txt");
+        assert_eq!(
+            args,
+            vec![
+                "--json", "--cache-dir", "/cache", "--loco", "--", "-g", "geno.txt.gz", "-p",
+                "pheno.txt", "-a", "snps.txt", "-gk"
+            ]
+        );
+    }
+
+    #[test]
+    fn gwa_args_is_loco_with_lmm_and_no_gk() {
+        let args = gwa_args("/cache", "geno.txt.gz", "pheno.txt", "snps.txt", "9", "/cache/k.json");
+        assert_eq!(
+            args,
+            vec![
+                "--json", "--cache-dir", "/cache", "--loco", "--input", "/cache/k.json", "--",
+                "-g", "geno.txt.gz", "-p", "pheno.txt", "-a", "snps.txt", "-lmm", "9"
+            ]
+        );
+        assert!(!args.contains(&"-gk"), "the GWA step must never carry -gk alongside -lmm");
+    }
 }
diff --git a/src/routes/gemma.rs b/src/routes/gemma.rs
index 03b96bc..3df86a7 100644
--- a/src/routes/gemma.rs
+++ b/src/routes/gemma.rs
@@ -82,10 +82,14 @@ pub async fn compute_gemma(
         lmm,
     )
     .await
-    .map(|rows| {
-        Json(ctx.envelope(rows).with_note(
+    .map(|output| {
+        let mut envelope = ctx.envelope(output.rows).with_note(
             "Rows come from GEMMA's own .assoc.txt output; column names depend on the lmm value used.",
-        ))
+        );
+        for note in &output.notes {
+            envelope = envelope.with_note(note);
+        }
+        Json(envelope)
     })
     .map_err(|e| gemma_error_to_api_error(e, rid))
 }