From f9ba0f12e3bf52919fc363c037fbf0a0deda8c17 Mon Sep 17 00:00:00 2001 From: Pjotr Date: Sat, 7 Nov 2026 18:15:37 +0000 Subject: plot: draw a mapping, and suggest it from the mapping The two plotters Pjotr added (scripts/plot-rqtl.py, plot-gemma.py) turn a saved mapping response into an SVG Manhattan panel. They are now endpoints, and the mapping that produces the response suggests them: POST /plot/rqtl same body as /compute/rqtl -> image/svg+xml POST /plot/gemma same body as /compute/gemma -> image/svg+xml Taking the mapping's own body rather than the mapping's output means one call gets you the picture, and the plot cannot describe a run you did not do: the handlers call the compute handlers and hand the resulting envelope to the script, exactly as the scripts' docstrings describe (they read `data.markers`, `data.permutation` and the notes' strain counts). The mapping is re-run, but through the engines' own caches -- seconds on BXD. The reply is an SVG, not the JSON envelope: an image is not JSON, and the plotter's one-line peak summary rides back in `x-plot-summary` so a caller that cannot see the picture still learns where the peak is. Both compute responses gain the matching suggestion: `actions` carries "the same request, plotted" (built from the request that ran, like the permutation suggestion), so an assistant can map, look, and only then decide whether the peak is worth the 100-permutation run. * src/plot.rs: run the configured script over the envelope, read the SVG back, clean up; a missing script is a 502 naming interpreter and script, not a panic. * src/routes/plot.rs: the two handlers (token-gated), delegating to compute_rqtl/compute_gemma so validation and alignment stay in one place; `x-plot-summary` sanitised into a header. * src/routes/{rqtl,gemma}.rs: the plot suggestion, before the permutation one. * src/config.rs: PLOT_PYTHON, RQTL_PLOTTER, GEMMA_PLOTTER. * tests/plot.rs: the suggestion resolves to a complete SVG with a peak summary, and an invalid mapping request is the mapping's own 422. * tests/{rqtl,phewas}.rs: find suggestions by href rather than position. * README.md, docs/www/ai-instruction.md (4a), docs/www/operator-checklist.md (new "Plotting": python3 must be on the server's PATH). --- scripts/plot-gemma.py | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 scripts/plot-gemma.py (limited to 'scripts/plot-gemma.py') diff --git a/scripts/plot-gemma.py b/scripts/plot-gemma.py new file mode 100644 index 0000000..f2a6302 --- /dev/null +++ b/scripts/plot-gemma.py @@ -0,0 +1,118 @@ +#!/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 reference line 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("--no-bonferroni", action="store_true", help="omit the threshold line") + 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) + ymax = max(4, math.ceil(max(top[0], 0 if a.no_bonferroni else bonf) + 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}.") + + 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 not a.no_bonferroni: + y = Y(bonf) + s.append(f'') + s.append(f'Bonferroni, {nrows:,} markers: {bonf:.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)) + print(f"wrote {a.out}: peak {top[3]} chr{top[1]} {top[2]:.1f} Mb -log10p {top[0]:.2f}; Bonferroni {bonf:.2f} over {nrows} markers") + + +if __name__ == "__main__": + main() -- cgit 1.4.1