about summary refs log tree commit diff
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/geno-to-bimbam.py49
1 files changed, 45 insertions, 4 deletions
diff --git a/scripts/geno-to-bimbam.py b/scripts/geno-to-bimbam.py
index 089da45..b31829d 100755
--- a/scripts/geno-to-bimbam.py
+++ b/scripts/geno-to-bimbam.py
@@ -34,8 +34,16 @@ is not in the `.geno` header, this repository, or the panel's `rdata/*.RData`
 decision for whoever owns the panel -- pick the founders, then pass the table
 here.  R/qtl remains the engine for 4-way panels (docs/genotypes.md).
 
-    scripts/geno-to-bimbam.py --geno HET3-ITP.12.geno --out DIR
+    scripts/geno-to-bimbam.py --geno HET3-ITP.12.geno --out DIR --pairwise
 writes DIR/HET3-ITP.12_geno.txt and DIR/HET3-ITP.12_snps.txt.
+
+NOT every panel needs a fixed table.  A panel whose markers are each biallelic
+but whose *code pair* varies from marker to marker (HET3-ITP.12: 486 markers
+carry codes {5,6}, 395 carry {7,8}, a handful more) needs `--pairwise`: for
+each marker its two present codes become 0 and 1 (lower code -> 0), anything
+else missing.  That is the biallelic projection the panel actually supports --
+a single global table cannot express it, and forcing one there was measured to
+be constant (881 of 893 markers with zero variance).
 """
 
 import argparse
@@ -71,16 +79,27 @@ def main():
         help="basename for the outputs (default: the .geno's name)",
     )
     parser.add_argument(
+        "--pairwise",
+        action="store_true",
+        help="per-marker biallelic coding: each marker's two present codes "
+        "become 0 and 1. Use this for a panel whose code pair varies by "
+        "marker (see the docstring); it conflicts with --codes.",
+    )
+    parser.add_argument(
         "--codes",
-        required=True,
+        required=False,
         help="projection table: '<dosage>=<code,code,...>' separated by ';', "
         "e.g. '0=1,2;1=3,4;0.5=5,6,7,8' (required: a guessed table produces a "
         "silently empty projection, see the docstring)",
     )
     args = parser.parse_args()
 
+    if bool(args.pairwise) == bool(args.codes):
+        sys.exit("give exactly one of --pairwise or --codes")
     table = {}
-    for part in args.codes.split(";"):
+    for part in (args.codes or "").split(";"):
+        if not part:
+            continue
         dosage, codes = part.split("=")
         for code in codes.split(","):
             table[code.strip()] = dosage.strip()
@@ -93,12 +112,27 @@ def main():
 
     seen = {"markers": 0, "calls": 0, "missing": 0}
     per_dosage = {}
+    pairwise_seen = {}
     with open(geno_out, "w") as geno, open(snps_out, "w") as snps:
         for chrom, locus, _cm, mb, calls in read_geno(args.geno):
+            marker_table = table
+            if args.pairwise:
+                present = sorted({c.strip() for c in calls if c.strip() not in ("NA", "", ".")})
+                # Exactly two classes is what a biallelic marker looks like.
+                # One class (monomorphic) or more than two (a genuinely
+                # multi-allelic marker, which BIMBAM cannot carry) is left
+                # missing rather than silently folded.
+                if len(present) == 2:
+                    marker_table = {present[0]: "0", present[1]: "1"}
+                    pairwise_seen[(present[0], present[1])] = (
+                        pairwise_seen.get((present[0], present[1]), 0) + 1
+                    )
+                else:
+                    marker_table = {}
             projected = []
             for call in calls:
                 call = call.strip()
-                dosage = table.get(call)
+                dosage = marker_table.get(call)
                 if dosage is None:
                     projected.append("NA")
                     seen["missing"] += 1
@@ -133,6 +167,13 @@ def main():
         f"calls projected ({100 * (1 - seen['missing'] / seen['calls']):.1f}% present), "
         f"dosages {dict(sorted(per_dosage.items()))}"
     )
+    if args.pairwise:
+        print(
+            "per-marker code pairs: "
+            + ", ".join(
+                f"{a}/{b}: {n}" for (a, b), n in sorted(pairwise_seen.items(), key=lambda kv: -kv[1])
+            )
+        )
     print(f"{snps_out}")