about summary refs log tree commit diff
path: root/src/config.rs
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-10-11 19:49:57 +0000
committerPjotr <wrk@bosa.lan>2026-10-11 19:49:57 +0000
commit9e03cf40b875790c19d3fa1623297841b72b553e (patch)
tree95b891af0f3015fbdd74243c23c4533fee97ae24 /src/config.rs
parent8e13f31ceca05aa06b8a98b76e1141763b6a4140 (diff)
downloadgenenetwork4-9e03cf40b875790c19d3fa1623297841b72b553e.tar.gz
gemma: add POST /compute/gemma, backed by the real gemma-wrapper/GEMMA
Maps a QTL from a genotype file, a SNP annotation file and trait
values, by running gemma-wrapper's K-matrix step then its LMM
association step, both synchronously (axum/tokio awaits the subprocess
directly, no job queue needed) and returning the parsed .assoc.txt
rows in the standard envelope.

Both steps use a bare --loco (gemma-wrapper derives the chromosome
list from the SNP annotation file). This was arrived at empirically,
not from GeneNetwork3's or GeneNetwork2's own gemma code: run for real
against the BXD fixtures,
- GeneNetwork3's gn3/computations/gemma.py composes a GWA command that
  always keeps "-gk" alongside "-lmm"; real GEMMA rejects that
  combination outright ("only one of -gk ... -lmm ... options is
  allowed"), so none of its gwa-compute/k-gwa-compute endpoints can
  produce output. Unsurprising: tests/integration/test_gemma.py is
  entirely @unittest.skip, so this was never exercised.
- GeneNetwork2's wqflask/marker_regression/gemma_mapping.py correctly
  omits "-gk" from the GWA step, but its non-LOCO branch fails too,
  the same way my own first (non-LOCO) attempt did: gemma-wrapper only
  ever archives a non-LOCO kinship matrix into a .tar.xz, with no
  loose file at the path its own non-LOCO GWA code assumes
  ("Enforce failed for file_kin ... open file"). Only --loco, which
  gemma-wrapper's own USAGE banner documents and which does unpack
  that archive, actually works.
A --loco run's .assoc.txt files (one per chromosome) are themselves
only ever inside gemma-wrapper's own output archive, never loose, so
gemma.rs shells out to `tar` (one of gemma-wrapper's own propagated
dependencies) to extract them before concatenating all chromosomes'
rows into the response.

guix.scm now depends on (gn packages gemma) from guix-bioinformatics
for gemma-wrapper (propagated-input: it's invoked as a subprocess, not
linked, and this makes it land in a consuming profile too, same as
GeneNetwork3's and GeneNetwork2's own packages do it). That dependency
needed guix-bioinformatics' own gn/packages/gemma.scm fixed first (see
that repo's fix/gemma-which-shadowing branch) -- gemma-gn2 would not
build at all before that fix.

Verified end to end: `cargo test` passes in full, including a real
(non-mocked, non-skipped) run of gemma-wrapper/GEMMA against the BXD
fixtures copied from genenetwork3/tests/test_data, producing exactly
7320 association rows (one per marker in BXD_snps.txt) in ~19s.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs
index 10f5755..16286b5 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -60,6 +60,14 @@ pub struct Config {
     pub dev_token_client_secret: String,
     pub dev_token_username: String,
     pub dev_token_password: String,
+    /// The gemma-wrapper executable to invoke for POST /compute/gemma.
+    /// Same GEMMA_WRAPPER_CMD config key GeneNetwork3 uses. Defaults to
+    /// "gemma-wrapper", resolved via PATH.
+    pub gemma_wrapper_cmd: String,
+    /// Directory gemma-wrapper uses to cache K matrices and association
+    /// results between calls (its own --cache-dir switch). Defaults to
+    /// a "gn4-gemma" directory under the system temp dir.
+    pub gemma_cache_dir: String,
 }
 
 impl Config {
@@ -102,6 +110,14 @@ impl Config {
         values.insert("GN3_TIMEOUT_SECS".to_string(), "30".to_string());
         values.insert("XAPIAN_DB_PATH".to_string(), String::new());
         values.insert("SEARCH_ENGINE".to_string(), "forward".to_string());
+        values.insert("GEMMA_WRAPPER_CMD".to_string(), "gemma-wrapper".to_string());
+        values.insert(
+            "GEMMA_CACHE_DIR".to_string(),
+            std::env::temp_dir()
+                .join("gn4-gemma")
+                .to_string_lossy()
+                .into_owned(),
+        );
 
         // Layers 2 and 3: config files, in increasing priority.
         for env_key in ["GN4_CONF", "GN4_SECRETS"] {
@@ -127,6 +143,8 @@ impl Config {
             "GN3_TIMEOUT_SECS",
             "XAPIAN_DB_PATH",
             "SEARCH_ENGINE",
+            "GEMMA_WRAPPER_CMD",
+            "GEMMA_CACHE_DIR",
         ] {
             if let Ok(value) = std::env::var(key) {
                 values.insert(key.to_string(), value);
@@ -153,6 +171,8 @@ impl Config {
             dev_token_client_secret: values["GN_AUTH_DEV_CLIENT_SECRET"].clone(),
             dev_token_username: values["GN_AUTH_DEV_USERNAME"].clone(),
             dev_token_password: values["GN_AUTH_DEV_PASSWORD"].clone(),
+            gemma_wrapper_cmd: values["GEMMA_WRAPPER_CMD"].clone(),
+            gemma_cache_dir: values["GEMMA_CACHE_DIR"].clone(),
         }
     }
 }
@@ -223,6 +243,8 @@ mod tests {
             "GN3_TIMEOUT_SECS",
             "XAPIAN_DB_PATH",
             "SEARCH_ENGINE",
+            "GEMMA_WRAPPER_CMD",
+            "GEMMA_CACHE_DIR",
         ] {
             if std::env::var(key).is_ok() {
                 eprintln!("skipping: {key} is set in the environment");
@@ -232,6 +254,8 @@ mod tests {
         let config = Config::load();
         assert!(config.sql_uri.contains("db_webqtl"));
         assert_eq!(config.port, 3000);
+        assert_eq!(config.gemma_wrapper_cmd, "gemma-wrapper");
+        assert!(config.gemma_cache_dir.ends_with("gn4-gemma"));
         assert_eq!(config.auth_server_url, "http://127.0.0.1:8081/");
         assert_eq!(config.bind, "0.0.0.0");
         assert!(config.test_token_enabled, "on by default");