From 7f6d92b546892ea5dba5a3f5df541c77c229f1e4 Mon Sep 17 00:00:00 2001 From: Pjotr Date: Mon, 16 Nov 2026 20:20:20 +0000 Subject: impute: one switch for how a missing genotype is treated, on the ravanan path "How do we treat a missing genotype?" now has one answer shared by every engine, so the same trait can be mapped both ways and the results compared: none the file as it stands -- GEMMA does complete-case per marker and reports n_miss (235 individuals for some markers, 234 for others) mean every gap takes that marker's mean (complete file, all individuals) hmm R/qtl2's HMM predicts the gap from flanking markers (maxmarg on the genotype probabilities, the calls /compute/qtl2 itself maps on) scripts/prepare-genotypes.sh is that one place. It writes .bimbam.txt and .snps.txt and nothing else, so the two engines consume the same files: /compute/gemma/ravanan takes `impute`, and the pangemma store builder (the next endpoint) takes the same script with --mode mean|hmm into the LMDB makers. On the ravanan path the files are cached under RAVANAN_CACHE_DIR/imputed/ -, so a second request for the same panel and treatment costs nothing: mean is a single pass (1.7 s for BXD.8), hmm is the qtl2 HMM (10 s including computing the probabilities the first time, then cached beside the cross). The reply says which treatment ran, and `hmm` refuses without a group (422 impute_needs_group) since the cross it predicts from is the group's. Measured, BXD.8 (20 453 markers, 235 individuals), missing calls in the prepared panel: 1 465 of 4.8M as NA. Both modes produce complete files of 9.2 and 10.0 MB, and a request with impute=mean or impute=hmm writes them and then proceeds to ravanan. What this box could not finish is the run itself: ravanan in the scratch instance failed on its own manifest cache ("manifest.scm newer than compiled ... warning") and, for hmm, answered "no association output files" -- environment, not the preparation, and the next thing to settle. Why not keep it in the workflow: the treatment has to be the same for both engines, and pangemma's store builder needs the file *before* the workflow exists (it builds an LMDB store from it). One script, two consumers. --- scripts/prepare-genotypes.sh | 77 ++++++++++++++++++++++++++++++++++++++++++++ scripts/qtl2-impute-bimbam.R | 19 ++++++++++- 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100755 scripts/prepare-genotypes.sh (limited to 'scripts') diff --git a/scripts/prepare-genotypes.sh b/scripts/prepare-genotypes.sh new file mode 100755 index 0000000..36a959b --- /dev/null +++ b/scripts/prepare-genotypes.sh @@ -0,0 +1,77 @@ +#!/bin/sh +# prepare-genotypes.sh -- one place where "how do we treat a missing genotype?" +# is decided, so the same question can be asked of every engine and the answers +# compared. +# +# --mode none leave the file as it is (GEMMA then does complete-case per +# marker and reports n_miss; pangemma cannot represent a gap at +# all, so its store builder refuses this mode) +# --mode mean replace each missing call by that marker's mean over the +# individuals that have one (cheap, but it invents a datum and +# deflates the variance) +# --mode hmm predict the missing calls from flanking markers with R/qtl2's +# HMM (maxmarg on genotype probabilities) -- the genotype set +# /compute/qtl2 maps on, and the one that does not invent a value +# +# scripts/prepare-genotypes.sh --mode mean|hmm|none \ +# --geno --snps --out \ +# [--cross ] [--probs ] [--minprob N] +# +# Writes .bimbam.txt and .snps.txt and prints the prefix. +# Exits 2 on bad arguments, 1 when a step fails. +set -eu + +mode= geno= snps= out= cross_dir= probs= minprob=0 +while [ $# -gt 0 ]; do + case $1 in + --mode) mode=$2; shift 2 ;; + --geno) geno=$2; shift 2 ;; + --snps) snps=$2; shift 2 ;; + --out) out=$2; shift 2 ;; + --cross) cross_dir=$2; shift 2 ;; + --probs) probs=$2; shift 2 ;; + --minprob) minprob=$2; shift 2 ;; + -h|--help) sed -n '2,26p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; + *) echo "prepare-genotypes.sh: unknown argument $1" >&2; exit 2 ;; + esac +done + +case $mode in none|mean|hmm) ;; *) echo "prepare-genotypes.sh: --mode must be none, mean or hmm" >&2; exit 2 ;; esac +[ -n "$out" ] || { echo "prepare-genotypes.sh: --out is required" >&2; exit 2; } +mkdir -p "$(dirname "$out")" + +case $mode in +none) + [ -n "$geno" ] && [ -n "$snps" ] || { echo "prepare-genotypes.sh: --geno and --snps are required" >&2; exit 2; } + cp "$geno" "$out.bimbam.txt" + cp "$snps" "$out.snps.txt" + ;; +mean) + [ -n "$geno" ] && [ -n "$snps" ] || { echo "prepare-genotypes.sh: --geno and --snps are required" >&2; exit 2; } + awk -F, 'BEGIN { OFS = "," } + { + sum = 0; n = 0 + for (i = 4; i <= NF; i++) if ($i != "NA" && $i != "") { sum += $i; n++ } + mean = (n ? sum / n : 0) + for (i = 4; i <= NF; i++) if ($i == "NA" || $i == "") $i = sprintf("%.4f", mean) + print + }' "$geno" > "$out.bimbam.txt" + cp "$snps" "$out.snps.txt" + ;; +hmm) + [ -n "$cross_dir" ] || { echo "prepare-genotypes.sh: --mode hmm needs --cross DIR" >&2; exit 2; } + RSCRIPT=${RSCRIPT:-Rscript} + command -v "$RSCRIPT" >/dev/null 2>&1 || + { echo "prepare-genotypes.sh: $RSCRIPT is not on PATH (R with qtl2)" >&2; exit 1; } + # R's own complaint last, so a caller that reports the final stderr line + # reports the cause rather than our wrapper sentence. + if ! r_output=$("$RSCRIPT" "$(dirname "$0")/qtl2-impute-bimbam.R" \ + "$cross_dir" "${probs:--}" "$out" "$minprob" 2>&1); then + echo "prepare-genotypes.sh: HMM imputation failed:" >&2 + printf '%s\n' "$r_output" | tail -6 >&2 + exit 1 + fi + ;; +esac + +printf '%s\n' "$out" diff --git a/scripts/qtl2-impute-bimbam.R b/scripts/qtl2-impute-bimbam.R index 1a9cd1b..4003f4e 100755 --- a/scripts/qtl2-impute-bimbam.R +++ b/scripts/qtl2-impute-bimbam.R @@ -29,7 +29,24 @@ minprob <- if (length(args) >= 4) as.numeric(args[4]) else 0 setwd(cross_dir) cross <- read_cross2("control.json") -probs <- readRDS(probs_path) + +# Passing "-" as the probabilities means "compute them": a caller that only has +# the converted cross (the GEMMA path has no qtl2 scan to borrow a cache entry +# from) should not have to run /compute/qtl2 first just to get genotypes. The +# result is cached in the cross directory, which is itself content-keyed, so a +# second call is free. +probs <- if (identical(probs_path, "-")) { + cached <- "probs-imputed.rds" + if (file.exists(cached)) { + readRDS(cached) + } else { + p <- calc_genoprob(cross, error_prob = 0.002, cores = 1, quiet = TRUE) + saveRDS(p, cached) + p + } +} else { + readRDS(probs_path) +} # maxmarg returns one call per marker and individual, at the highest # probability above MINPROB -- or NA when the HMM is less sure than that. At -- cgit 1.4.1