#!/usr/bin/env python3 """Manhattan plot of a GeneNetwork4 GEMMA result, as one SVG panel. Input is the JSON response of POST /gn4/compute/gemma (saved as-is). Plots -log10(p_lrt) per marker. GEMMA has no permutation test through the API, so the only line GEMMA can justify is a Bonferroni threshold, 0.05 / number of markers (conservative: neighbouring markers are correlated). python3 plot-gemma.py g20010.json --trait 20010 -o g20010_manhattan.svg Standard library only; writes plain SVG (light/dark aware). """ import argparse, json, math, re, sys def chr_name(v): return str(int(v)) if isinstance(v, (int, float)) else str(v) 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/gemma response (JSON)") ap.add_argument("-o", "--out", default="gemma_manhattan.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") ap.add_argument("--bonferroni", action="store_true", help="draw GEMMA's Bonferroni line (off by default: a reference line is a " "label on the plot, not part of the result)") ap.add_argument("--threshold", action="append", default=[], help="extra reference line as VALUE[:LABEL], e.g. 3.81:bulklmm alpha 0.05 " "(thresholds another engine computed for the same values)") 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]}") cols = doc["meta"].get("columns") or [] ci = {n: i for i, n in enumerate(cols)} for need in ("chr", "rs", "ps", "p_lrt"): if need not in ci: sys.exit(f"column {need!r} not in meta.columns {cols} (was GEMMA run with -lmm 9?)") by, nrows = {}, 0 for r in doc["data"]: p = r[ci["p_lrt"]] if p is None or p <= 0: continue nrows += 1 by.setdefault(chr_name(r[ci["chr"]]), []).append( (r[ci["ps"]] / 1e6, -math.log10(p), r[ci["rs"]])) 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(((v, c, pos, rs) for c in order for pos, v, rs in by[c])) bonf = -math.log10(0.05 / nrows) if a.bonferroni else None # Reference lines from elsewhere: (value, label). Whoever computed them gets # named in the label, so a reader can tell a BulkLMM threshold from GEMMA's # own Bonferroni line -- they are not the same null. external = [] for spec in a.threshold: value, _, label = spec.partition(":") external.append((float(value), label.strip() or "bulklmm threshold")) ymax = max(4, math.ceil(max([top[0]] + ([bonf] if bonf else []) + [value for value, _ in external]) + 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(top[1], top[2]), Y(top[0]) notes = " ".join(doc.get("notes", [])) m = re.search(r"(\d+) values used", notes) ver = re.search(r"version (\S+?),", notes) nstr = f"{m.group(1)} strains" if m else "" panel = ver.group(1) if ver else "" label = f"trait {a.trait}" if a.trait else "GEMMA scan" title = a.title or f"{label}: GEMMA association along the genome" sub = "GEMMA LOCO linear mixed model (-lmm 9)" + (f", {panel}" if panel else "") + (f", {nstr}" if nstr else "") desc = (f"GEMMA scan over {nrows} markers. The strongest association is -log10 p {top[0]:.2f} " f"on chromosome {top[1]} at {top[2]:.1f} Mb ({top[3]}). " f"Bonferroni threshold for {nrows} markers: -log10 p {bonf:.2f}." if bonf is not None 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'-log10 p') 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(v):.1f}" for p, v, _ in by[c]) s.append(f'') if bonf is not None: y = Y(bonf) s.append(f'') s.append(f'Bonferroni, {nrows:,} markers: {bonf:.2f}') for value, label in external: y = Y(value) s.append(f'') s.append(f'{label}: {value:.2f}') s.append(f'{top[3]}: chr{top[1]} {top[2]:.1f} Mb, -log10 p {top[0]:.2f}') s.append(f'chr{top[1]} {top[2]:.0f} Mb, -log10 p {top[0]:.2f}') s.append("") open(a.out, "w").write("\n".join(s)) extra_note = "" if external: extra_note = "; reference line(s) " + ", ".join( f"{label} {value:.2f}" for value, label in external) bonf_note = f"; Bonferroni {bonf:.2f} over {nrows} markers" if bonf is not None else "" print(f"wrote {a.out}: peak {top[3]} chr{top[1]} {top[2]:.1f} Mb -log10p {top[0]:.2f}" f"{bonf_note}{extra_note}") if __name__ == "__main__": main()