about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-09 20:38:35 +0000
committerPjotr <wrk@bosa.lan>2026-11-09 20:38:35 +0000
commitdc3e0e5ba0fa6f9462d55e9ca9a4802a60eb205f (patch)
treef154549bf81ce0c0d588e4c37e6ad8c7dc80845d /src
parent832bf0a6f3961b7ad43635d5770939acba55fb7b (diff)
downloadgenenetwork4-dc3e0e5ba0fa6f9462d55e9ca9a4802a60eb205f.tar.gz
gemma: quote GEMMA's own error, not the wrapper's banner
Every GEMMA failure the sweep produced read the same way -- "gemma-wrapper
bailed out with pid ... exit 1", sometimes with the Ruby backtrace -- which
says nothing about whether the API or the panel is at fault. The line that
does is GEMMA's, printed in the middle of the wrapper's per-chromosome
chatter:

  ERROR: Enforce failed for N>0 in src/fastblas.cpp at line 194 in fast_cblas_dgemm

so `CommandFailed` now carries the last GEMMA diagnostic (an `ERROR`,
`error!`, `****` warning, `Enforce failed` or "not enough" line) rather than
the raw stderr, falling back to the wrapper's own tail when GEMMA said
nothing at all. The wrapper's banner and `.rb:` backtrace lines are skipped:
they are the noise this exists to get past.

Two tests: the complaint is GEMMA's line, not the banner; and a silent
failure still names something.
Diffstat (limited to 'src')
-rw-r--r--src/gemma.rs78
1 files changed, 77 insertions, 1 deletions
diff --git a/src/gemma.rs b/src/gemma.rs
index 6da1d8d..c48e3fd 100644
--- a/src/gemma.rs
+++ b/src/gemma.rs
@@ -130,11 +130,60 @@ pub fn write_pheno_file(path: &Path, values: &TraitValues) -> io::Result<()> {
     std::fs::write(path, contents)
 }
 
+/// The one line worth quoting from a failed gemma-wrapper run.
+///
+/// The wrapper prints GEMMA's own diagnostics in the middle of its
+/// per-chromosome chatter and then bails out with a banner that says
+/// nothing ("gemma-wrapper bailed out with pid ... exit 1"). What a caller
+/// needs is GEMMA's sentence -- `ERROR: Enforce failed for N>0 ...` -- so
+/// that a panel GEMMA cannot fit is not mistaken for a broken API.
+fn gemma_complaint(stdout: &str, stderr: &str) -> String {
+    let interesting = |line: &&str| {
+        let line = line.trim();
+        // The wrapper's own banner and Ruby backtrace are not the answer.
+        if line.contains("gemma-wrapper") || line.contains(".rb:") {
+            return false;
+        }
+        line.contains("ERROR")
+            || line.contains("error!")
+            || line.starts_with("****")
+            || line.contains("Enforce failed")
+            || line.contains("not enough")
+    };
+    let last = stdout
+        .lines()
+        .chain(stderr.lines())
+        .filter(interesting)
+        .next_back()
+        .map(str::trim)
+        .unwrap_or_default();
+    if last.is_empty() {
+        // Nothing GEMMA said: hand back the tail, which at least names the
+        // wrapper's own complaint.
+        let tail: Vec<&str> = stderr
+            .lines()
+            .chain(stdout.lines())
+            .filter(|line| !line.trim().is_empty())
+            .collect();
+        return tail
+            .iter()
+            .rev()
+            .take(2)
+            .rev()
+            .cloned()
+            .collect::<Vec<_>>()
+            .join(" / ");
+    }
+    last.to_string()
+}
+
 fn parse_wrapper_output(step: &'static str, output: Output) -> Result<Value, GemmaError> {
     if !output.status.success() {
+        let stdout = String::from_utf8_lossy(&output.stdout);
+        let stderr = String::from_utf8_lossy(&output.stderr);
         return Err(GemmaError::CommandFailed {
             step,
-            stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
+            stderr: gemma_complaint(&stdout, &stderr),
         });
     }
     let stdout = String::from_utf8_lossy(&output.stdout);
@@ -1179,3 +1228,30 @@ mod effect_size_tests {
         assert_eq!(rows[0][10], Value::Null);
     }
 }
+
+#[cfg(test)]
+mod complaint_tests {
+    use super::gemma_complaint;
+
+    /// The wrapper's banner says nothing; GEMMA's own line says everything.
+    #[test]
+    fn the_complaint_is_gemmas_own_line_not_the_wrappers_banner() {
+        let stdout = "\
+Executing: gemma -gk 1 ...
+## number of analyzed SNPs = 0
+ERROR: Enforce failed for N>0 in src/fastblas.cpp at line 194 in fast_cblas_dgemm
+FATAL ERROR: gemma-wrapper bailed out with pid 42 exit 130
+";
+        let got = gemma_complaint(stdout, "");
+        assert_eq!(
+            got,
+            "ERROR: Enforce failed for N>0 in src/fastblas.cpp at line 194 in fast_cblas_dgemm"
+        );
+    }
+
+    #[test]
+    fn a_silent_failure_still_names_something() {
+        let got = gemma_complaint("", "gemma-wrapper bailed out with pid 7 exit 1\n");
+        assert!(got.contains("bailed out"), "{got}");
+    }
+}