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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#!/usr/bin/env python3
"""Clustered heatmap of a GeneNetwork4 correlation matrix, as one SVG panel.
Input is the JSON response of POST /compute/correlation/matrix with
`"cluster": true` (saved as-is). The cells are the r matrix drawn in the
clustered order, with the dendrogram that produced that order alongside, so a
reader can see *why* two traits are neighbours rather than trusting the order.
python3 plot-heatmap.py matrix.json -o heatmap.svg --trait collection-name
Standard library only; writes plain SVG (light/dark aware).
"""
import argparse
import json
import sys
W, H = 880, 720
LEFT_TREE = 150 # dendrogram width
TOP, BOTTOM = 78, 116 # header, legend
RIGHT_LABELS = 250 # trait names on the right
def heatmap_of(doc):
data = doc.get("data") if isinstance(doc, dict) else None
for candidate in (data, doc):
if isinstance(candidate, dict) and isinstance(candidate.get("heatmap"), dict):
return candidate["heatmap"], candidate
return None, data if isinstance(data, dict) else {}
def colour(value):
"""Diverging scale for r in [-1, 1]: blue negative, red positive."""
v = max(-1.0, min(1.0, value))
if v >= 0:
# white -> red
r, g, b = 250, int(250 - 150 * v), int(250 - 170 * v)
else:
# white -> blue
t = -v
r, g, b = int(250 - 160 * t), int(250 - 100 * t), 250
return f"#{r:02x}{g:02x}{b:02x}"
def main():
ap = argparse.ArgumentParser(description=__doc__.split("\n")[0])
ap.add_argument("result", help="saved /compute/correlation/matrix response (JSON), cluster: true")
ap.add_argument("-o", "--out", default="heatmap.svg")
ap.add_argument("--trait", default="", help="collection label for the title")
ap.add_argument("--title", default=None)
a = ap.parse_args()
doc = json.load(open(a.result))
clustering, data = heatmap_of(doc)
if not clustering or not clustering.get("order"):
sys.exit(f"{a.result}: no heatmap.order -- post the matrix reply with \"cluster\": true")
traits = data.get("traits") or []
r = data.get("r") or []
order = clustering["order"]
n = len(order)
if n < 2:
sys.exit("a heatmap needs at least two traits")
names = [t.get("trait_name", "?") for t in traits]
datasets = [t.get("dataset", "") for t in traits]
# Cell size: keep the whole matrix in the panel, labels only if they fit.
size = min((H - TOP - BOTTOM) / n, (W - LEFT_TREE - RIGHT_LABELS) / n)
size = max(size, 1.0)
grid = size * n
x0 = LEFT_TREE
y0 = TOP
label = a.trait or "correlation matrix"
title = a.title or f"{label}: clustered correlation heatmap"
values = [r[i][j] for i in order for j in order if r[i][j] is not None]
sub = (f"{n} trait(s), average linkage on 1 - r ({len(clustering.get('merges') or [])} merges, "
f"max distance {clustering.get('max_height', 0):.3f})"
+ (f" · {len(values)} of {n * n} cells shown" if len(values) != n * n else ""))
desc = (f"Clustered heatmap of {n} traits. Cell colour is the correlation coefficient, "
f"blue negative and red positive, in the order the average-linkage tree produced.")
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;--tree:#52514e;--na:#dedcd6}}
@media (prefers-color-scheme:dark){{:root{{--surface:#1a1a19;--ink:#f0efec;--ink2:#c3c2b7;--grid:#333330;--tree:#a9a89f;--na:#2a2a28}}}}
text{{fill:var(--ink2);font-size:12px}} .ttl{{fill:var(--ink);font-size:16px;font-weight:600}}
</style>
<rect width="{W}" height="{H}" fill="var(--surface)"/>
<text class="ttl" x="{x0}" y="26">{title}</text>
<text x="{x0}" y="46">{sub}</text>''']
# Cells.
for row, i in enumerate(order):
for column, j in enumerate(order):
value = r[i][j]
x = x0 + column * size
y = y0 + row * size
if value is None:
fill = "var(--na)"
label = "no correlation"
else:
fill = colour(value)
label = f"r = {value:+.3f}"
s.append(f'<rect x="{x:.1f}" y="{y:.1f}" width="{size + 0.4:.1f}" height="{size + 0.4:.1f}" '
f'fill="{fill}"><title>{names[i]} × {names[j]}: {label}</title></rect>')
# Dendrogram on the left: leaves in the same order, heights from the tree.
leaves = {trait_index: position for position, trait_index in enumerate(order)}
nodes = {}
max_height = max(clustering.get("max_height") or 0.0, 1e-12)
x_max = LEFT_TREE - 18
x_min = 14
def pos_x(height):
return x_max - (height / max_height) * (x_max - x_min)
for index, trait_index in enumerate(order):
nodes[trait_index] = (x_max, y0 + (leaves[trait_index] + 0.5) * size)
for merge_index, merge in enumerate(clustering.get("merges") or []):
left, right = merge["left"], merge["right"]
if left not in nodes or right not in nodes:
continue
(lx, ly), (rx, ry) = nodes[left], nodes[right]
x = pos_x(merge["height"])
s.append(f'<path d="M{lx:.1f} {ly:.1f} H{x:.1f} V{ry:.1f} H{rx:.1f}" fill="none" '
f'stroke="var(--tree)" stroke-width="1"/>')
nodes[n + merge_index] = (x, (ly + ry) / 2)
# Labels: trait names on the right, dataset only when it differs.
label_font = 11 if size < 14 else 12
for row, i in enumerate(order):
y = y0 + (row + 0.5) * size + 3.5
text = names[i]
s.append(f'<text x="{x0 + grid + 8:.1f}" y="{y:.1f}" font-size="{label_font}">{text}</text>')
if datasets[i]:
s.append(f'<text x="{x0 + grid + 8 + 92:.1f}" y="{y:.1f}" font-size="{label_font - 1}" '
f'opacity="0.7">{datasets[i]}</text>')
# Legend: the diverging scale, drawn as a strip.
lx, ly, lw, lh = x0, H - 74, 220, 12
steps = 40
for step in range(steps):
value = -1.0 + 2.0 * step / (steps - 1)
s.append(f'<rect x="{lx + step * lw / steps:.1f}" y="{ly}" width="{lw / steps + 0.5:.1f}" '
f'height="{lh}" fill="{colour(value)}"/>')
s.append(f'<text x="{lx}" y="{ly + lh + 14}">-1</text>')
s.append(f'<text x="{lx + lw / 2:.1f}" y="{ly + lh + 14}" text-anchor="middle">0</text>')
s.append(f'<text x="{lx + lw:.1f}" y="{ly + lh + 14}" text-anchor="end">+1</text>')
s.append(f'<text x="{lx + lw + 14}" y="{ly + 10}">correlation coefficient r</text>')
s.append(f'<text x="{lx}" y="{H - 18}">clustered order, both axes: average linkage on 1 - r</text>')
s.append("</svg>")
open(a.out, "w").write("\n".join(s) + "\n")
print(f"{a.out}: {n}x{n} heatmap, {len(clustering.get('merges') or [])} merges, "
f"cell {size:.1f}px")
if __name__ == "__main__":
main()
|