Branchwell

Watch Encyclopedia / website · watchencyclopedia.org / exp / dna.js

1WE.mount('dna', function (S) {
2 var C = S.C, ctx = S.ctx, NORMAL = 'ATGGTGCATCTGACTCCTGAGGAGAAGTCT', seq = NORMAL.split(''), G = { cells: [] }, last = -1;
3 var T = 'FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG', NM = { F: 'Phe', L: 'Leu', S: 'Ser', Y: 'Tyr', C: 'Cys', W: 'Trp', P: 'Pro', H: 'His', Q: 'Gln', R: 'Arg', I: 'Ile', M: 'Met', T: 'Thr', N: 'Asn', K: 'Lys', V: 'Val', A: 'Ala', D: 'Asp', E: 'Glu', G: 'Gly', '*': 'Stop' };
4 var COL = { A: C.green, T: C.red, G: C.yellow, C: C.blue }, PAIR = { A: 'T', T: 'A', G: 'C', C: 'G' };
5 function aa(i) { var c = seq.slice(i * 3, i * 3 + 3), ix = function (b) { return 'TCAG'.indexOf(b); }; return T[16 * ix(c[0]) + 4 * ix(c[1]) + ix(c[2])]; }
6 S.button('Make the sickle cell change', function () { seq = NORMAL.split(''); seq[19] = 'T'; last = 19; }); S.button('Put it back', function () { seq = NORMAL.split(''); last = -1; });
7 S.pointer({ scroll: true, down: function (x, y) { G.cells.forEach(function (c, i) { if (x >= c[0] && x < c[0] + c[2] && y >= c[1] - 6 && y < c[1] + c[3] + 6) { seq[i] = 'ACGT'[('ACGT'.indexOf(seq[i]) + 1) % 4]; last = i; } }); } });
8 S.loop(function (dt, t) {
9 var w = S.W, h = S.H, sm = S.small(), n = 30, hx0 = w * .04, hx1 = w * .96, hy = sm ? h * .16 : h * .24, amp = sm ? h * .08 : h * .13;
10 S.clear();
11 // helix
12 var items = []; for (var i = 0; i < n; i++) { var a = i * .62 + t * .7, x = hx0 + (hx1 - hx0) * (i + .5) / n, y1 = hy + Math.sin(a) * amp, y2 = hy - Math.sin(a) * amp, z = Math.cos(a); items.push([x, y1, y2, z, i]); }
13 [0, 1].forEach(function (s) { ctx.beginPath(); for (var k = 0; k <= 240; k++) { var u = k / 240 * n - .5, a = u * .62 + t * .7; ctx.lineTo(hx0 + (hx1 - hx0) * (u + .5) / n, hy + (s ? -1 : 1) * Math.sin(a) * amp); } ctx.strokeStyle = s ? '#6a7080' : '#8a90a0'; ctx.lineWidth = sm ? 3 : 5; ctx.stroke(); });
14 items.forEach(function (q) { var b = seq[q[4]], mid = (q[1] + q[2]) / 2, lw = sm ? 3 : 6; ctx.globalAlpha = .55 + .45 * Math.abs(q[3]); S.ln(q[0], q[1], q[0], mid, COL[b], lw); S.ln(q[0], mid, q[0], q[2], COL[PAIR[b]], lw); ctx.globalAlpha = 1; if (q[4] === last) S.ring(q[0], hy, amp + 8, C.ink, 1.5); });
15 // letters and codons
16 var per = sm ? 15 : 30, rowsN = n / per, cw = (w - (sm ? 16 : 40)) / per, x0 = sm ? 8 : 20, ry = sm ? h * .34 : h * .5, rh = sm ? h * .3 : h * .42, stop = false; G.cells = [];
17 for (var c = 0; c < 10; c++) { var r = Math.floor(c * 3 / per), cx = x0 + ((c * 3) % per) * cw, cy = ry + r * rh, A = aa(c), norm = (function () { var s = NORMAL.slice(c * 3, c * 3 + 3), ix = function (b) { return 'TCAG'.indexOf(b); }; return T[16 * ix(s[0]) + 4 * ix(s[1]) + ix(s[2])]; })(), changed = A !== norm;
18 S.rr(cx + 2, cy, cw * 3 - 4, sm ? 84 : 118, 10, stop ? '#12151a' : '#181d25', changed ? C.red : C.grid);
19 for (var j = 0; j < 3; j++) { var idx = c * 3 + j, b = seq[idx], bx = cx + j * cw, bh = sm ? 26 : 34; G.cells[idx] = [bx, cy + 8, cw, bh]; S.rr(bx + 4, cy + 8, cw - 8, bh, 6, COL[b]); if (b !== NORMAL[idx]) S.rr(bx + 4, cy + 8, cw - 8, bh, 6, null, C.ink); S.txt(b, bx + cw / 2, cy + 8 + bh / 2 + 1, '#14110a', sm ? 14 : 17, 'center', 700); }
20 S.txt(NM[A], cx + cw * 1.5, cy + (sm ? 54 : 70), A === '*' ? C.red : stop ? C.dim : changed ? C.red : C.ink, sm ? 14 : 19, 'center', 700); S.txt(stop ? 'never made' : changed ? 'was ' + NM[norm] : c === 0 ? 'start' : A === '*' ? 'chain ends' : '', cx + cw * 1.5, cy + (sm ? 71 : 94), C.mut, sm ? 9.5 : 11.5, 'center');
21 if (A === '*') stop = true; }
22 if (!sm) S.txt('Each rung is a pair of bases. A always pairs with T, and G always pairs with C.', hx0, h * .44, C.mut, 12.5);
23 var cur = seq.join(''), nd = 0; for (i = 0; i < n; i++) if (seq[i] !== NORMAL[i]) nd++;
24 var sick = nd === 1 && seq[19] === 'T', same = true; for (c = 0; c < 10; c++) { var s2 = NORMAL.slice(c * 3, c * 3 + 3), ix2 = function (b) { return 'TCAG'.indexOf(b); }; if (aa(c) !== T[16 * ix2(s2[0]) + 4 * ix2(s2[1]) + ix2(s2[2])]) same = false; }
25 S.say(nd === 0 ? 'These are the first 30 letters of the human gene for beta-globin, part of the protein that carries oxygen in blood. The cell reads the letters <b>three at a time</b>. Each group of three names one building block of the protein. <b>Tap any letter to change it.</b>' : sick ? '<b>One letter changed: A became T.</b> The seventh block switches from Glu to Val. That single swap makes the protein clump when oxygen is low and bends red blood cells into a sickle shape. It is the cause of sickle cell disease.' : stop ? 'Your change made a <b>Stop</b> signal. The cell quits building here, so the rest of the protein is never made. Changes like this usually break the protein.' : same ? 'You changed a letter, but every block is the same as before. Most blocks can be spelled more than one way, so some changes are <b>silent</b>.' : 'Your change swapped a building block, marked in red. Some swaps do nothing much. Others change how the whole protein folds and works.');
26 });
27});