"""Teslagon: the Eterna100-V2 target itself, laid out from its dot-bracket string (the code block in README.md). Layout: every loop is a circle through its nucleotides (backbone steps of length 1, base-pair rungs of length RUNG), every helix a straight ladder leaving its loop along the radius. The whole drawing is then turned to fill the card. The 42 base pairs sit in 21 two-pair helices, the short stems that make it hard; each is a solid block in the field colour, and the backbone is ink. Checks: the README's counts, and that no two backbone steps cross or touch. """ import math import re from pathlib import Path import kit HERE = Path(__file__).resolve().parent TARGET = re.search(r'```\n([.()]+)\n```', (HERE / 'README.md').read_text()).group(1) RUNG = 1.8 # base-pair width, in backbone steps SPREAD = {16: 1.4} # backbone step inside the seven-way junction (closed by nucleotide 17), so its arms do not touch n = len(TARGET) pt, stack = [-1] * n, [] for i, c in enumerate(TARGET): if c == '(': stack.append(i) elif c == ')': j = stack.pop() pt[i], pt[j] = j, i pairs = [(i, pt[i]) for i in range(n) if pt[i] > i] assert not stack and n == 381 and len(pairs) == 42 and TARGET.count('.') == 297, 'the README numbers' def helices(): """Every helix as its list of pairs; all of Teslagon's are two pairs long.""" out, seen = [], set() for i, j in pairs: if i in seen: continue h = [(i, j)] while pt[h[-1][0] + 1] == h[-1][1] - 1: h.append((h[-1][0] + 1, h[-1][1] - 1)) seen.update(a for a, _ in h) out.append(h) return out assert all(len(h) == 2 for h in helices()), 'all in two-pair helices' P = [None] * n loops = [] # loop index, centre, radius, vertex list def radius(lengths): lo, hi = max(lengths) / 2 + 1e-9, 1e4 for _ in range(200): mid = (lo + hi) / 2 lo, hi = (mid, hi) if sum(2 * math.asin(L / (2 * mid)) for L in lengths) > 2 * math.pi else (lo, mid) return hi def loop(i, j, u): """Lays out the loop closed by the pair (i, j), whose two ends are placed; u points from the pair into the loop.""" verts, k = [i], i + 1 while k < j: verts.append(k) if pt[k] > k: verts.append(pt[k]) k = pt[k] k += 1 verts.append(j) step = SPREAD.get(i, 1.0) lengths = [RUNG if pt[a] == b else step for a, b in zip(verts, verts[1:])] + [RUNG] R = radius(lengths) mx, my = (P[i][0] + P[j][0]) / 2, (P[i][1] + P[j][1]) / 2 d = math.sqrt(max(R * R - RUNG * RUNG / 4, 0)) cx, cy = mx + d * u[0], my + d * u[1] a0 = math.atan2(P[i][1] - cy, P[i][0] - cx) aj = math.atan2(P[j][1] - cy, P[j][0] - cx) chord = 2 * math.asin(RUNG / (2 * R)) s = 1 if abs(((aj - a0) % (2 * math.pi)) - (2 * math.pi - chord)) < 1e-6 else -1 loops.append((len(loops), (cx, cy), R, verts)) a = a0 for v, L in zip(verts[1:-1], lengths): a += s * 2 * math.asin(L / (2 * R)) P[v] = (cx + R * math.cos(a), cy + R * math.sin(a)) for a_, b_ in zip(verts, verts[1:]): if pt[a_] == b_ and a_ != i: # a branch: its helix leaves along the radius mx, my = (P[a_][0] + P[b_][0]) / 2, (P[a_][1] + P[b_][1]) / 2 L = math.hypot(mx - cx, my - cy) stem(a_, b_, ((mx - cx) / L, (my - cy) / L)) def stem(i, j, u): while pt[i + 1] == j - 1: P[i + 1] = (P[i][0] + u[0], P[i][1] + u[1]) P[j - 1] = (P[j][0] + u[0], P[j][1] + u[1]) i, j = i + 1, j - 1 loop(i, j, u) # the exterior loop: one helix, with the two unpaired tails splayed away from the rest i0 = TARGET.index('(') P[i0], P[pt[i0]] = (0.0, 0.0), (RUNG, 0.0) for k in range(i0): P[k] = (-(i0 - k) * .7, (i0 - k) * .7) for k in range(pt[i0] + 1, n): P[k] = (RUNG + (k - pt[i0]) * .7, (k - pt[i0]) * .7) stem(i0, pt[i0], (0.0, -1.0)) assert all(p is not None for p in P) hub = [v for _, _, _, v in loops if v[0] == 16][0] assert sum(pt[a] == b for a, b in zip(hub, hub[1:])) == 6, 'the seven-way junction: its closing helix and six more' # check: no two backbone steps that are not neighbours come closer than half a step segs = [(P[k], P[k + 1]) for k in range(n - 1)] def seg_dist(p, q, r, s): def pd(a, b, c): bx, by, cx, cy = b[0] - a[0], b[1] - a[1], c[0] - a[0], c[1] - a[1] t = max(0, min(1, (cx * bx + cy * by) / (bx * bx + by * by))) return math.hypot(cx - t * bx, cy - t * by) return min(pd(p, q, r), pd(p, q, s), pd(r, s, p), pd(r, s, q)) close = min(seg_dist(*segs[a], *segs[b]) for a in range(len(segs)) for b in range(a + 2, len(segs))) assert close > 0.5, f'layout overlaps ({close:.2f})' # turn it to fill the card, then scale best = None for deg in range(0, 180): t = math.radians(deg) q = [(x * math.cos(t) - y * math.sin(t), x * math.sin(t) + y * math.cos(t)) for x, y in P] xs, ys = [a for a, _ in q], [b for _, b in q] sc = min(600 / (max(xs) - min(xs)), 250 / (max(ys) - min(ys))) if best is None or sc > best[0]: best = (sc, q, xs, ys) sc, q, xs, ys = best ox, oy = kit.W / 2 - sc * (max(xs) + min(xs)) / 2, 132 - sc * (max(ys) + min(ys)) / 2 Q = [(ox + sc * x, oy + sc * y) for x, y in q] body = [kit.polyline(Q, 'ink-s', 2.4)] for h in helices(): # each helix as one solid block between its two pairs (a, b), (c, d) = h body.append(kit.polygon([Q[a], Q[c], Q[d], Q[b]], 'f', ' stroke="var(--f)" stroke-width="3" stroke-linejoin="round"')) kit.write(__file__, 'life-health', ''.join(body), label='target fold ยท 381 nt, 42 pairs in two-pair helices', title='The Teslagon target secondary structure, 381 nucleotides with 42 base pairs') print(f'scale {sc:.1f} px per step, closest approach {close:.2f} steps')