about summary refs log tree commit diff
path: root/scripts/plot-gemma.py
blob: f2a6302967a20684209450ca24d5d5714cef45cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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()