about summary refs log tree commit diff
path: root/scripts
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-16 18:11:38 +0000
committerPjotr <wrk@bosa.lan>2026-11-16 18:11:38 +0000
commit061b76a5d963b7d5e2ce9d858094b305929766f2 (patch)
tree717cd9192f64225a064c91366bd9645efe97fb08 /scripts
parentea3dd926211e8db4a0d7d3739a606440b888e8b0 (diff)
downloadgenenetwork4-061b76a5d963b7d5e2ce9d858094b305929766f2.tar.gz
scripts/make-mdb.sh: build pangemma's genotype store, lock-free and frozen
pangemma picks an LMDB genotype store by looking for ".mdb" in the -g path
(src/param.cpp:943), so the store's name is part of the interface.  Two of
gemma-wrapper's helpers make it -- anno2mdb.rb then geno2mdb.rb -- and this
script does the three things they leave to the caller:

* the lock file.  Measured on a real store: these environments are single
  files (MDB_NOSUBDIR), and the lock is a *sibling*, <name>.mdb-lock, which
  every open recreates with different bytes (8d3d268b after writing, another
  sha1 after one read-only open, another after a read-write open) while the
  .mdb itself never changes.  Since the .mdb is what gets hashed and interned,
  the lock is deleted after writing and the .mdb is made read-only.
* the name: <name>.geno.mdb, so `-g' recognises it and the outer name says
  which translation it holds (Gf, Gb, G0_1, G0_2).
* atomicity: the makers write beside their input, so they are given links in a
  temporary directory and the finished store is renamed into place; nothing is
  written next to the caller's files (an earlier hand run of anno2mdb.rb put a
  2 MB .mdb into gn4-data, which is exactly the mistake this prevents).

Measured on BXD.8 (21 056 markers, 235 individuals): 0.17 s for the annotation
half, 4.7 s for the genotypes, 29 MB as Gf.

Two things found while testing it, both for the plan rather than this script:
the anno mdb is an intermediate that pangemma does not need (it takes the snps
file with -a), so it is not kept; and a store built from a BIMBAM whose chr/pos
columns are the "X"/"Y" placeholders makes pangemma produce an all-NaN K
silently -- 235 individuals read, no error, a 235x235 matrix of -nan -- because
geno2mdb keys genotypes by (chr,pos) and nothing matches the annotation.
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/make-mdb.sh93
1 files changed, 93 insertions, 0 deletions
diff --git a/scripts/make-mdb.sh b/scripts/make-mdb.sh
new file mode 100755
index 0000000..ea58fc6
--- /dev/null
+++ b/scripts/make-mdb.sh
@@ -0,0 +1,93 @@
+#!/bin/sh
+# make-mdb.sh -- build pangemma's genotype store (an LMDB environment) from a
+# BIMBAM genotype file and its marker annotation.
+#
+# Two of gemma-wrapper's helpers do the work:
+#
+#   anno2mdb.rb  snps.txt        -> <name>.snps.mdb   (chr/pos -> marker name)
+#   geno2mdb.rb  geno.txt --anno <name>.snps.mdb -> <name>.geno.mdb
+#
+# and this script exists for the three things they leave to the caller:
+#
+# 1. the lock file.  An LMDB environment is data.mdb *plus* lock.mdb, and
+#    lock.mdb's bytes change on every open -- measured: 8d3d268b after writing,
+#    a different sha1 after one read-only open, different again after a
+#    read-write open, while data.mdb never changed.  A directory whose hash is
+#    its identity (ravanan interns inputs by content) must therefore not
+#    contain one, so it is removed last and the tree is made read-only to stop
+#    anything recreating it.
+# 2. the name.  pangemma decides "this is an mdb" by looking for ".mdb" in the
+#    -g path (src/param.cpp:943), so the directory must end in .mdb and the
+#    outer name should say which translation it holds.
+# 3. atomicity.  A half-written environment must never be visible under the
+#    final name: build under a temporary name in the destination directory and
+#    rename when whole.
+#
+#   scripts/make-mdb.sh --geno <bimbam geno> --snps <snps.txt> \
+#                       [--eval Gf|Gb|G0_1|G0_2] [--name NAME] [--out DIR]
+#
+# Prints the directory it built.  Exit 2 on bad arguments, 1 on a maker failure.
+set -eu
+
+geno= snps= eval_=Gf name= out=.
+while [ $# -gt 0 ]; do
+	case $1 in
+	--geno) geno=$2; shift 2 ;;
+	--snps) snps=$2; shift 2 ;;
+	--eval) eval_=$2; shift 2 ;;
+	--name) name=$2; shift 2 ;;
+	--out) out=$2; shift 2 ;;
+	-h|--help) sed -n '2,30p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
+	*) echo "make-mdb.sh: unknown argument $1" >&2; exit 2 ;;
+	esac
+done
+[ -n "$geno" ] && [ -n "$snps" ] || { echo "make-mdb.sh: --geno and --snps are required" >&2; exit 2; }
+for f in "$geno" "$snps"; do
+	[ -r "$f" ] || { echo "make-mdb.sh: cannot read $f" >&2; exit 2; }
+done
+case $eval_ in Gf|Gb|G0_1|G0_2) ;; *) echo "make-mdb.sh: unknown --eval $eval_" >&2; exit 2 ;; esac
+
+# The makers, and something to run them with.  MAKE_MDB_ANNO / MAKE_MDB_GENO
+# let a caller point at a different checkout; ANNO2MDB_OK is set by the caller
+# when it has checked they exist.
+ANNO=${ANNO2MDB:-anno2mdb.rb}
+GENO=${GENO2MDB:-geno2mdb.rb}
+for tool in "$ANNO" "$GENO"; do
+	command -v "$tool" >/dev/null 2>&1 || { echo "make-mdb.sh: $tool is not on PATH" >&2; exit 1; }
+done
+
+mkdir -p "$out"
+[ -n "$name" ] || name=$(basename "$geno" | sed 's/\.\(txt\|gz\|bimbam\)$//')
+work=$(mktemp -d "$out/.$name.XXXXXX")
+trap 'rm -rf "$work"' EXIT
+
+# The makers write <input>.mdb *beside their input* and take it as a
+# positional argument, so they get links to work on: nothing is written next to
+# the caller's files.
+ln -s "$(cd "$(dirname "$geno")" && pwd)/$(basename "$geno")" "$work/geno.txt"
+ln -s "$(cd "$(dirname "$snps")" && pwd)/$(basename "$snps")" "$work/snps.txt"
+
+# 1. the annotation half.  It is an intermediate: pangemma is given the snps
+#    file itself (-a) and only geno2mdb needs this database, so it is not kept.
+"$ANNO" "$work/snps.txt" >/dev/null 2>&1 ||
+	{ echo "make-mdb.sh: anno2mdb.rb failed" >&2; exit 1; }
+
+# 2. the genotypes, translated the way pangemma expects
+"$GENO" --eval "$eval_" --anno "$work/snps.txt.mdb" "$work/geno.txt" >/dev/null 2>&1 ||
+	{ echo "make-mdb.sh: geno2mdb.rb failed" >&2; exit 1; }
+
+# 3. the lock file.  These environments are single files (MDB_NOSUBDIR), so the
+#    lock is a *sibling*, <name>.mdb-lock -- measured on a real store: every
+#    open recreates it with different bytes while the .mdb itself never changes.
+#    The .mdb is what gets interned and hashed (a File input hashes its
+#    content), so the lock only needs to go so the directory we hand out
+#    contains nothing but the store itself.
+rm -f "$work/geno.txt.mdb-lock" "$work/snps.txt.mdb-lock"
+chmod a-w "$work/geno.txt.mdb"
+
+rm -f "$out/$name.geno.mdb" "$out/$name.geno.mdb-lock"
+mv "$work/geno.txt.mdb" "$out/$name.geno.mdb"
+trap 'rm -rf "$work"' EXIT
+rm -rf "$work"
+trap - EXIT
+printf '%s\n' "$out/$name.geno.mdb"