#!/usr/bin/env python3 """Manhattan-style LOD plot of a GeneNetwork4 R/qtl result, as one SVG panel. Input is the JSON response of POST /gn4/compute/rqtl (saved as-is). If the request had nperm > 0, the significant and suggestive thresholds from `data.permutation` are drawn as dashed lines. python3 plot.py r20010_p100.json --trait 20010 -o r20010_lod_perm.svg Standard library only; writes plain SVG (light/dark aware). """ import argparse, json, math, re, sys def chr_key(c): return (0, int(c), "") if c.isdigit() else (1, 0, c) def main(): ap = argparse.ArgumentParser(description=__doc__.split("\n")[0]) ap.add_argument("result", help="saved /compute/rqtl response (JSON)") ap.add_argument("-o", "--out", default="rqtl_lod.svg") ap.add_argument("--trait", default="", help="trait label for the title, e.g. 20010") ap.add_argument("--title", default=None, help="override the title text") a = ap.parse_args() doc = json.load(open(a.result)) if "data" not in doc: sys.exit(f"{a.result}: no 'data' member (error response?): {str(doc)[:200]}") res = doc["data"] rows = res["markers"] # [name, chr, pos_mb, lod] perm = res.get("permutation") thr = [] # (value, label, dash) if perm: thr = [(perm["significant"], "significant (95%)", "7 4"), (perm["suggestive"], "suggestive", "2 4")] nperm = len(perm["values"]) if perm and perm.get("values") else 0 notes = " ".join(doc.get("notes", [])) m = re.search(r"(\d+) of (\d+) supplied values", notes) ver = re.search(r"version (\S+?),", notes) nstr = f"{m.group(1)} strains" if m else "" panel = ver.group(1) if ver else "" by = {} for name, c, pos, lod in rows: by.setdefault(str(c), []).append((pos, lod)) order = sorted(by, key=chr_key) off, tot, gap = {}, 0.0, 8 for c in order: by[c].sort() off[c] = tot tot += by[c][-1][0] + gap top = max(rows, key=lambda r: r[3]) ymax = max(4, math.ceil(max([top[3]] + [t[0] for t in thr]) + 0.3)) W, H, L, R, T, B = 1000, 430, 62, 20, 64, 52 pw, ph = W - L - R, H - T - B X = lambda c, p: L + (off[c] + p) / tot * pw Y = lambda v: T + ph - v / ymax * ph tx, ty = X(str(top[1]), top[2]), Y(top[3]) label = f"trait {a.trait}" if a.trait else "R/qtl scan" title = a.title or f"{label}: LOD along the genome" sub = "R/qtl scanone (Haley-Knott)" + (f", {panel}" if panel else "") + (f", {nstr}" if nstr else "") sub += f", thresholds from {nperm} permutations" if thr else ", no permutation threshold" desc = (f"R/qtl scan. The highest peak is LOD {top[3]:.2f} on chromosome {top[1]} " f"at {top[2]:.1f} Mb ({top[0]})." + (f" Permutation thresholds from {nperm} permutations: " f"significant LOD {thr[0][0]:.2f}, suggestive LOD {thr[1][0]:.2f}." if thr else "")) s = [f''' {title} {desc} {title} {sub}'''] for i, c in enumerate(order): if i % 2 == 0: x0, x1 = X(c, 0), X(c, by[c][-1][0]) s.append(f'') for v in range(0, ymax + 1): y = Y(v) s.append(f'') s.append(f'{v}') s.append(f'LOD') for c in order: xm = (X(c, 0) + X(c, by[c][-1][0])) / 2 s.append(f'{c}') s.append(f'Chromosome') for c in order: pts = " ".join(f"{X(c, p):.1f},{Y(l):.1f}" for p, l in by[c]) s.append(f'') for v, name, dash in thr: y = Y(v) s.append(f'') s.append(f'{name} LOD {v:.2f}') s.append(f'{top[0]}: chr{top[1]} {top[2]:.1f} Mb, LOD {top[3]:.2f}') s.append(f'chr{top[1]} {top[2]:.0f} Mb, LOD {top[3]:.2f}') s.append("") open(a.out, "w").write("\n".join(s)) print(f"wrote {a.out}: peak {top[0]} chr{top[1]} {top[2]:.1f} Mb LOD {top[3]:.2f}" + (f"; thresholds sig {thr[0][0]:.2f} sugg {thr[1][0]:.2f}" if thr else "")) if __name__ == "__main__": main()