about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-17 11:31:10 +0000
committerPjotr <wrk@bosa.lan>2026-11-17 11:31:10 +0000
commita3d67c72dfa78840eb846f5d93f6f6326ac32b32 (patch)
tree27c7690e26bdb472274f8286f6f6cb69ef0dca94 /src
parentab710df0a1502b851292b61a1052585374c40ea3 (diff)
downloadgenenetwork4-a3d67c72dfa78840eb846f5d93f6f6326ac32b32.tar.gz
impute: a monotone genetic map for the HMM, and say hmm is not yet sound
The HMM smooths along a genetic map, and ours is degenerate: the .geno files
carry no cM, so the converted cross has 19 markers "with missing genetic map
position; using 0".  With no distance between markers the HMM has nothing to
smooth with, and the probabilities show it: P(BB) never rises above 0.5 and
every individual is called AA.  When the map has gaps it is now replaced by
physical position at 1 cM per Mb -- crude, monotone, and better than zeros.

That fixed part of it (the spread of calls at a marker went from sd 0.21 to
0.34 where the panel has 0.46) but not the collapse: at rsm10000005700, where
the panel has 125 BB, 84 A and 26 missing, maxmarg still answers 228 AA and 7
BB.  Two explanations have been eliminated by measurement -- the code mapping
(maxmarg returns genotype *names*; that is now derived from the data, so AA/BB
map correctly) and the missing-value encoding (0 is already qtl2's missing).
What is left is the converted cross itself: both HMM states appear to
correspond to the same physical allele at many markers, which is a question for
src/qtl2.rs, not for the imputation.

So the reply now says so out loud: "NOT YET VALIDATED on a riself panel ...
treat the hmm scan as experimental and prefer none or mean".  A mode that runs
and answers the wrong question silently is worse than one that says it is not
ready.
Diffstat (limited to 'src')
-rw-r--r--src/routes/gemma.rs46
1 files changed, 44 insertions, 2 deletions
diff --git a/src/routes/gemma.rs b/src/routes/gemma.rs
index e94ca72..31d5538 100644
--- a/src/routes/gemma.rs
+++ b/src/routes/gemma.rs
@@ -941,6 +941,7 @@ async fn prepare_genotypes(
     snps_file: &str,
     group: &Option<String>,
     version: Option<&str>,
+    names: Option<&Vec<String>>,
     rid: RequestInfo,
 ) -> Result<PreparedGenotypes, ApiError> {
     let stem = std::path::Path::new(genotype_file)
@@ -952,7 +953,7 @@ async fn prepare_genotypes(
     // script would otherwise be masked by its own old output (measured: a
     // corrected individuals file was ignored and the run kept failing on the
     // stale one).  Bump this when either script changes what it writes.
-    const IMPUTE_CACHE_VERSION: &str = "v2";
+    const IMPUTE_CACHE_VERSION: &str = "v6";
     let prefix = std::path::Path::new(&config.ravanan_cache_dir)
         .join("imputed")
         .join(format!("{stem}-{mode}-{IMPUTE_CACHE_VERSION}"));
@@ -1039,6 +1040,39 @@ async fn prepare_genotypes(
         std::path::PathBuf::new()
     };
 
+    if let Some(parent) = std::path::Path::new(&prepared_geno).parent() {
+        if let Err(error) = std::fs::create_dir_all(parent) {
+            return Err(ApiError::new(
+                StatusCode::INTERNAL_SERVER_ERROR,
+                "internal_error",
+                "Could not create the imputation cache directory",
+                format!("creating {} failed: {error}", parent.display()),
+                "compute_gemma_ravanan",
+            )
+            .with_request_id(rid));
+        }
+    }
+    // The individuals the request actually serves, so the HMM output has the
+    // same columns as the other modes and the phenotype lines up positionally.
+    let individuals_path = format!("{}.panel.txt", prefix.to_string_lossy());
+    if let Some(names) = names {
+        if !names.is_empty() {
+            if let Err(error) = std::fs::write(
+                &individuals_path,
+                names.iter().map(|n| format!("{n}\n")).collect::<String>(),
+            ) {
+                return Err(ApiError::new(
+                    StatusCode::INTERNAL_SERVER_ERROR,
+                    "internal_error",
+                    "Could not write the individual list",
+                    format!("writing {individuals_path} failed: {error}"),
+                    "compute_gemma_ravanan",
+                )
+                .with_request_id(rid));
+            }
+        }
+    }
+    let individuals_arg = individuals_path.clone();
     let script = config.prepare_genotypes.clone();
     let script_for_job = script.clone();
     let geno = genotype_file.to_string();
@@ -1063,6 +1097,10 @@ async fn prepare_genotypes(
             args.push(cross_dir.to_string_lossy().into_owned());
             args.push("--probs".to_string());
             args.push("-".to_string());
+            if std::path::Path::new(&individuals_arg).is_file() {
+                args.push("--individuals-file".to_string());
+                args.push(individuals_arg.clone());
+            }
         }
         std::process::Command::new(&script_for_job).args(&args).output()
     })
@@ -1109,7 +1147,10 @@ async fn prepare_genotypes(
             .to_string(),
         _ => "genotypes: missing calls predicted by R/qtl2's HMM (maxmarg on the \
               group's cached genotype probabilities, the same calls /compute/qtl2 \
-              maps on)"
+              maps on).  NOT YET VALIDATED on a riself panel: measured on BXD.8 \
+              the calls collapse towards one allele at markers that carry both \
+              (228 AA against the panel's 125 BB at rsm10000005700), so treat the \
+              hmm scan as experimental and prefer none or mean."
             .to_string(),
     });
     Ok(PreparedGenotypes {
@@ -1297,6 +1338,7 @@ pub async fn compute_gemma_ravanan(
             &snps_file,
             &Some(resolved_group.clone()),
             request.genotype_version.as_deref(),
+            resolved_names.as_ref(),
             rid.clone(),
         )
         .await?;