about summary refs log tree commit diff
path: root/scripts
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-07 18:15:37 +0000
committerPjotr <wrk@bosa.lan>2026-11-07 18:15:37 +0000
commitf9ba0f12e3bf52919fc363c037fbf0a0deda8c17 (patch)
treef8ba4f4de75ffe45dd05183d066dc9e079ba6450 /scripts
parentb6c5538bc2bf1166ba176a0d80966c04229854ad (diff)
downloadgenenetwork4-f9ba0f12e3bf52919fc363c037fbf0a0deda8c17.tar.gz
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).
Diffstat (limited to 'scripts')
-rw-r--r--scripts/plot-gemma.py118
-rw-r--r--scripts/plot-rqtl.py112
2 files changed, 230 insertions, 0 deletions
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'''<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {W} {H}" width="{W}" height="{H}" role="img" aria-labelledby="t d" font-family="system-ui,-apple-system,Segoe UI,sans-serif">
+<title id="t">{title}</title>
+<desc id="d">{desc}</desc>
+<style>
+:root{{--surface:#fcfcfb;--ink:#0b0b0b;--ink2:#52514e;--grid:#e6e5e1;--band:#f3f2ef;--s2:#eb6834}}
+@media (prefers-color-scheme:dark){{:root{{--surface:#1a1a19;--ink:#f0efec;--ink2:#c3c2b7;--grid:#333330;--band:#222220;--s2:#d95926}}}}
+text{{fill:var(--ink2);font-size:12px}} .ttl{{fill:var(--ink);font-size:16px;font-weight:600}} .ann{{fill:var(--ink);font-size:12px}}
+</style>
+<rect width="{W}" height="{H}" fill="var(--surface)"/>
+<text class="ttl" x="{L}" y="26">{title}</text>
+<text x="{L}" y="46">{sub}</text>''']
+    for i, c in enumerate(order):
+        if i % 2 == 0:
+            x0, x1 = X(c, 0), X(c, by[c][-1][0])
+            s.append(f'<rect x="{x0:.1f}" y="{T}" width="{x1 - x0:.1f}" height="{ph}" fill="var(--band)"/>')
+    for v in range(0, ymax + 1):
+        y = Y(v)
+        s.append(f'<line x1="{L}" x2="{W - R}" y1="{y:.1f}" y2="{y:.1f}" stroke="var(--grid)" stroke-width="1"/>')
+        s.append(f'<text x="{L - 8}" y="{y + 4:.1f}" text-anchor="end">{v}</text>')
+    s.append(f'<text transform="translate(16 {T + ph / 2}) rotate(-90)" text-anchor="middle">-log10 p</text>')
+    for c in order:
+        xm = (X(c, 0) + X(c, by[c][-1][0])) / 2
+        s.append(f'<text x="{xm:.1f}" y="{T + ph + 18}" text-anchor="middle">{c}</text>')
+    s.append(f'<text x="{L + pw / 2}" y="{H - 10}" text-anchor="middle">Chromosome</text>')
+    for c in order:
+        pts = " ".join(f"{X(c, p):.1f},{Y(v):.1f}" for p, v, _ in by[c])
+        s.append(f'<polyline points="{pts}" fill="none" stroke="var(--s2)" stroke-width="1.5" stroke-linejoin="round"/>')
+    if not a.no_bonferroni:
+        y = Y(bonf)
+        s.append(f'<line x1="{L}" x2="{W - R}" y1="{y:.1f}" y2="{y:.1f}" stroke="var(--ink2)" stroke-width="1.25" stroke-dasharray="7 4"/>')
+        s.append(f'<text x="{W - R - 4}" y="{y - 5:.1f}" text-anchor="end">Bonferroni, {nrows:,} markers: {bonf:.2f}</text>')
+    s.append(f'<circle cx="{tx:.1f}" cy="{ty:.1f}" r="4.5" fill="var(--s2)" stroke="var(--surface)" stroke-width="2"><title>{top[3]}: chr{top[1]} {top[2]:.1f} Mb, -log10 p {top[0]:.2f}</title></circle>')
+    s.append(f'<text class="ann" x="{tx + 10:.1f}" y="{ty + 4:.1f}">chr{top[1]} {top[2]:.0f} Mb, -log10 p {top[0]:.2f}</text>')
+    s.append("</svg>")
+    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()
diff --git a/scripts/plot-rqtl.py b/scripts/plot-rqtl.py
new file mode 100644
index 0000000..73d8c51
--- /dev/null
+++ b/scripts/plot-rqtl.py
@@ -0,0 +1,112 @@
+#!/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'''<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {W} {H}" width="{W}" height="{H}" role="img" aria-labelledby="t d" font-family="system-ui,-apple-system,Segoe UI,sans-serif">
+<title id="t">{title}</title>
+<desc id="d">{desc}</desc>
+<style>
+:root{{--surface:#fcfcfb;--ink:#0b0b0b;--ink2:#52514e;--grid:#e6e5e1;--band:#f3f2ef;--s1:#2a78d6}}
+@media (prefers-color-scheme:dark){{:root{{--surface:#1a1a19;--ink:#f0efec;--ink2:#c3c2b7;--grid:#333330;--band:#222220;--s1:#3987e5}}}}
+text{{fill:var(--ink2);font-size:12px}} .ttl{{fill:var(--ink);font-size:16px;font-weight:600}} .ann{{fill:var(--ink);font-size:12px}}
+</style>
+<rect width="{W}" height="{H}" fill="var(--surface)"/>
+<text class="ttl" x="{L}" y="26">{title}</text>
+<text x="{L}" y="46">{sub}</text>''']
+    for i, c in enumerate(order):
+        if i % 2 == 0:
+            x0, x1 = X(c, 0), X(c, by[c][-1][0])
+            s.append(f'<rect x="{x0:.1f}" y="{T}" width="{x1 - x0:.1f}" height="{ph}" fill="var(--band)"/>')
+    for v in range(0, ymax + 1):
+        y = Y(v)
+        s.append(f'<line x1="{L}" x2="{W - R}" y1="{y:.1f}" y2="{y:.1f}" stroke="var(--grid)" stroke-width="1"/>')
+        s.append(f'<text x="{L - 8}" y="{y + 4:.1f}" text-anchor="end">{v}</text>')
+    s.append(f'<text transform="translate(16 {T + ph / 2}) rotate(-90)" text-anchor="middle">LOD</text>')
+    for c in order:
+        xm = (X(c, 0) + X(c, by[c][-1][0])) / 2
+        s.append(f'<text x="{xm:.1f}" y="{T + ph + 18}" text-anchor="middle">{c}</text>')
+    s.append(f'<text x="{L + pw / 2}" y="{H - 10}" text-anchor="middle">Chromosome</text>')
+    for c in order:
+        pts = " ".join(f"{X(c, p):.1f},{Y(l):.1f}" for p, l in by[c])
+        s.append(f'<polyline points="{pts}" fill="none" stroke="var(--s1)" stroke-width="1.5" stroke-linejoin="round"/>')
+    for v, name, dash in thr:
+        y = Y(v)
+        s.append(f'<line x1="{L}" x2="{W - R}" y1="{y:.1f}" y2="{y:.1f}" stroke="var(--ink2)" stroke-width="1.25" stroke-dasharray="{dash}"/>')
+        s.append(f'<text x="{W - R - 4}" y="{y - 5:.1f}" text-anchor="end">{name}  LOD {v:.2f}</text>')
+    s.append(f'<circle cx="{tx:.1f}" cy="{ty:.1f}" r="4.5" fill="var(--s1)" stroke="var(--surface)" stroke-width="2"><title>{top[0]}: chr{top[1]} {top[2]:.1f} Mb, LOD {top[3]:.2f}</title></circle>')
+    s.append(f'<text class="ann" x="{tx + 10:.1f}" y="{ty + 4:.1f}">chr{top[1]} {top[2]:.0f} Mb, LOD {top[3]:.2f}</text>')
+    s.append("</svg>")
+    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()