/
boot-sample.html
557 lines (539 loc) · 19.9 KB
/
boot-sample.html
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<!DOCTYPE html>
<!-- from http://bl.ocks.org/mbostock/4339083
and http://bl.ocks.org/mbostock/4063550
!-->
<meta charset="utf-8">
<style>
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 1.5px;
}
.textsumm {
font: 12px monospace;
}
.node {
font: 10px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
#tree1par {
vertical-align: top;
float: left;
}
#tree2par {
vertical-align: center;
float: left;
}
#tree3par {
vertical-align: bottom;
float: left;
}
#histpar {
vertical-align: top;
float: right;
}
</style>
<body>
<script src="d3/d3.v3.min.js"></script>
<script src="jquery/jquery.v2.min.js"></script>
<table>
<tr>
<td>
<input id="bootbtn" name="boot" type="button" value="Add 1 bootstrap rep" onclick="bootstrap()" />
</td>
<td><div id="src"/></td>
</tr>
<tr>
<td></td>
<td><span id="result">Click "Add 1 bootstrap rep" to draw a pseudoreplicate</span></td>
</tr>
<tr>
<td><span id="nreps">0 replicates done</span></td>
<td class="textsumm">
<span id="abBootText"></span>
<span id="acBootText"></span>
<span id="adBootText"></span>
</td>
<td />
</tr>
<tr>
<td></td>
<td><div id="hist"/></td>
</tr>
<tr>
<td colspan="3"> <img src="boot_tre.png" width="875" height="400"></td>
</tr>
</table>
<h2>Background</h2>
<p> This page demonstrates the non-parametric bootstrap for phylogenetics (<a href="http://dx.doi.org/10.2307/2408678">Felsenstein, 1985</a>).
The top character table represents a (fictitious) alignment of DNA sequences from 4 primates.
Assuming that the orangutan is the outgroup and disregarding the possibility that the three lineages
leading to humans, chimpanzees and gorrillas divergend simultaneously leaves us with 3 possible trees for these data, shown above.
Humans could be sister to chimps (H+C). Humans could be sister to gorillas (H+G). Or chimps and gorillas
could be most closely related to each other (C+G).</p>
<p>Columns shown in grey are sites which have no preference for any of the trees according to the parsimony criterion. In other words, all three trees explain these sites equally well.
Sites that favor the human+chimp tree are shown in red;
sites favoring the human+gorilla tree are in blue; and
sites favoring the chimp+gorilla tree are in green.
</p>
<p>In this (contrived) data set there are 3 sites favoring the H+C tree, 2 favoring the C+G tree, 1 favoring the H+G tree, and 4 sites that do not prefer any of the trees.
Thus the point estimate of the best relationships (under the parsimony criterion) would be the H+C tree.
</p>
<p>Bootstrapping tries to help us determine if we should be confident that this estimate is not an artifact of sampling error.</p>
<p>The recipe for bootstrapping is to draw new data sets from your original data by sampling with replacement.
For each pseudoreplicate dataset, you can estimate the best tree.
If there groupings in the tree are disturbed by this resampling, you will fail to see those groups recovered in a large fraction of the bootstrap replicates.
The bootstrap proportions for these groups will not be close to 100%, and you can conclude that
the amount of signal present in the original data for these groups could plausibly be explained by sampling error.</p>
<h2>Using the demo</h2>
<p>The triangle on the bottom shows a representation of the frequency of the parsimony-informative sites in pattern frequency space. This coordinate system is explained <a href="./parsimony.html">here</a>.
The observed data set is depicted as a white circle.
</p>
<p>Each time you click the bootstrap button, you will see a pseudoreplicate dataset being drawn from the original dataset.
As the bootstrap replicate is being drawn, you will see its parsimony-informative pattern spectrum update with
each parsimony-informative pattern that is added. This shows up as the yellow circle moving during the bootstrap draws.
This moving circle is just to give you a sense of how the coordinate system works - the only result that matters is which tree is favored when you have drawn a number of characters equal to the size of the original data (10 in this example).
</p>
<p>When a full bootstrap resampled data set is created, we can decide which tree is favored by the pseudoreplicate. If there are ties we give each of the two trees 1/2 credit (or give each of the 3 trees 1/3 credit in the event of a three-way tie for which tree is the best explanation of the pseudoreplicate).
The bootstrap proportions for each of the 3 trees are updated each time you finish a bootstrap replicate.
The previously sampled datasets remain as small black dot in the pattern-frequency space figure.</p>
<p>In a real analysis, you would want to perform hundreds of bootstrap pseudoreplicates before you put much faith in the bootstarp proportions.
Note that on larger examples, a tree search must be performed.
With parsimony and only 3 possibilities, we can simply figure out which tree is preferred by whether the most frequent informative patterns are red, green, or blue.</p>
<!-- footer -->
<h4></h4>
<hr />
<p>Further information on toplogical testing will be available in a forthcoming "Encyclopedia of Evolution" article by Emily Jane B. McTavish and Mark T. Holder.</p>
<p>Back to the demo <a href="./index.html">table of contents...</a></p>
<p> Source code at <a href="https://github.com/mtholder/mephytis">https://github.com/mtholder/mephytis</a></p>
<p>Thanks to the <a href="http://www.nsf.gov">U.S. National Science Foundation</a> and the
<a href="http://www.h-its.org/english/">Heidelberg Institute for Theoretical Studies</a> for support.</p>
<!-- end footer -->
<script>
var MAT_WIDTH = 200;
var MAT_HEIGHT = 120;
var SPACE = 60;
var FIG_HEIGHT = 2*MAT_HEIGHT + SPACE;
var NUM_CHAR = 10;
var OFFSET = MAT_WIDTH / NUM_CHAR;
var COL_WIDTH = OFFSET;
var TEXT_X = COL_WIDTH/2;
var TEXT_Y = MAT_HEIGHT/4;
var TEXT_Y_OFF = TEXT_Y/2;
var NUM_OVERLAID = 1; /* 1 + NUM_CHAR; */
var LOWER_Y = MAT_HEIGHT + SPACE;
var margin = {top: 10, right: 10, bottom: 35, left: 75}
var src_svg = d3.select('#src').append('svg')
.attr({'width': MAT_WIDTH + margin.left + margin.right,
'height': FIG_HEIGHT + margin.bottom + margin.top, })
.style('background', 'white');
var src_data = [];
var i = 0;
var ind = 0;
var pat = ['GGGC', 'CCTT', 'CCCC', 'AGAA', 'GGAA', 'CGCG', 'TTTT', 'GAAG', 'CTTC', 'TTGG'];
var typ = [ 0, 1, 0, 0, 1, 3, 0, 2, 2, 1];
var ab = 0;
var ac = 0;
var ad = 0;
var nc = 0
for (nc = 0 ; nc < NUM_CHAR; ++nc) {
var cp = pat[nc];
var ct = typ[nc]
for (i = 0; i < NUM_OVERLAID; ++i) {
src_data[ind] = {'x': margin.left + COL_WIDTH*nc,
'y': 0,
'value': ct,
'site': cp,
'oind': i};
if (ct == 1) {
ad += 1;
} else if (ct == 2) {
ab += 1;
} else if (ct == 3) {
ac += 1;
}
ind += 1;
}
}
var color = d3.scale.threshold()
.domain([.5, 1.5, 2.5, 3.5])
.range(['#BBBBBB', '#FF0000', '#00FF00', '#6666FF'])
var do_init = function (src_data) {
var bar = src_svg.selectAll("g")
.data(src_data);
bar.enter().append("g");
bar.append("rect")
.attr("x", function(d) {return d.x})
.attr("y", function(d) {return d.y})
.attr("width", OFFSET)
.attr("height", MAT_HEIGHT)
.attr("fill", function(d) {return color(d.value)});
var j;
for (j = 0 ; j < 4; ++j) {
var k = j;
bar.append("text")
.attr("x", function(d) {return d.x + TEXT_X;})
.attr("y", function(d) {return d.y + k*TEXT_Y + TEXT_Y_OFF;})
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) {return d.site[k];})
}
bar.exit().remove();
return bar;
}
var pseudo_rep_x = 0;
var do_update = function (src_data) {
var bar = src_svg.selectAll("g")
.data(src_data);
var cp = margin.left + pseudo_rep_x;
var nb = bar
.enter().append("g")
.attr("transform", function(d, i) {return "translate(" + d.x +", " + d.y+ ")";});
pseudo_rep_x += COL_WIDTH * nb.length;
nb.append("rect")
.attr("width", OFFSET)
.attr("height", MAT_HEIGHT)
.attr("fill", function(d) {return color(d.value)});
var j;
for (j = 0 ; j < 4; ++j) {
var k = j;
nb.append("text")
.attr("x", function(d) {return TEXT_X;})
.attr("y", function(d) {return k*TEXT_Y + TEXT_Y_OFF;})
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) {return d.site[k];})
}
nb.transition()
.duration(350)
.attr("transform", function(d, i) {return "translate(" + cp +", " + LOWER_Y + ")";});
}
do_init(src_data);
var LABEL_Y = 5
var LABEL_X = 20;
var labels = ["Human", "Chimp", "Gorilla", "Orang"];
var ul;
for (ul = 0; ul < 2; ++ul) {
for (i = 0 ; i < 4; ++i) {
src_svg.append("text").attr("x", LABEL_X).attr("y", LABEL_Y + i*TEXT_Y + TEXT_Y_OFF).text(labels[i]);
}
LABEL_Y += LOWER_Y;
}
var prev_points = [];
var num_sampled = 0;
var curr_points = [0, 0, 0];
var norm_curr_points = [];
var resultTextSpan = d3.select("#result");
var nrepsTextSpan = d3.select("#nreps");
var bootstrap = function() {
$("#bootbtn").prop("disabled", true);
curr_points = [0, 0, 0];
norm_curr_points = [];
src_data = src_data.slice(0, NUM_CHAR);
resultTextSpan.text('Drawing samples...');
do_init(src_data);
num_sampled = 0;
pseudo_rep_x = 0
do_boot_draw();
};
var millisecond_sleep = 300;
var num_boot_reps = 0;
var nCG = 0;
var nHG = 0;
var nHC = 0;
var do_boot_draw = function() {
var sel_ind = Math.floor(Math.random()*NUM_CHAR);
src_data[src_data.length] = src_data[sel_ind];
num_sampled += 1;
var v = src_data[sel_ind].value;
if (v == 1) {
curr_points[2] += 1;
} else if (v == 2) {
curr_points[0] += 1;
} else if (v == 3) {
curr_points[1] += 1;
}
update_bary_for_boot();
do_update(src_data);
if (num_sampled < NUM_CHAR) {
setTimeout(function() {do_boot_draw();}, millisecond_sleep);
} else {
prev_points[prev_points.length] = {'counts': norm_curr_points};
if (prev_points.length == 1) {
prev_points[prev_points.length] = prev_points[0];
}
num_boot_reps += 1;
if (num_boot_reps == 1) {
nrepsTextSpan.text("" + num_boot_reps + " replicate:");
} else {
nrepsTextSpan.text("" + num_boot_reps + " replicates:");
}
if (norm_curr_points[0] > norm_curr_points[1]) {
if (norm_curr_points[0] > norm_curr_points[2]) {
resultTextSpan.text('Chimp + Gorilla tree preferred on this rep.');
nCG += 1;
} else if (norm_curr_points[2] > norm_curr_points[0]) {
resultTextSpan.text('Human + Chimp tree preferred on this rep.');
nHC += 1;
} else {
resultTextSpan.text('H+C tree tied with C+G tree on this rep.');
nHC += .5;
nCG += .5;
}
} else if (norm_curr_points[1] > norm_curr_points[0]) {
if (norm_curr_points[1] > norm_curr_points[2]) {
resultTextSpan.text('Human + Gorilla tree preferred on this rep.');
nHG += 1;
} else if (norm_curr_points[2] > norm_curr_points[1]) {
resultTextSpan.text('Human + Chimp tree preferred on this rep.');
nHC += 1;
} else {
resultTextSpan.text('H+C tree tied with H+G tree on this rep.');
nHC += .5;
nHG += .5;
}
} else {
if (norm_curr_points[2] > norm_curr_points[0]) {
resultTextSpan.text('Human + Chimp tree preferred on this rep.');
nHC += 1;
} else if (norm_curr_points[2] < norm_curr_points[0]) {
resultTextSpan.text('H+C tree tied with C+G tree on this rep.');
nHC += .5;
nCG += .5;
} else {
resultTextSpan.text('All three trees tied on this rep.');
nHC += 1/3;
nHG += 1/3;
nCG += 1/3;
}
}
abBootText.text('C+G ' + (100*nCG/num_boot_reps).toFixed(1) + '%');
acBootText.text('H+G ' + (100*nHG/num_boot_reps).toFixed(1) + '%');
adBootText.text('H+C ' + (100*nHC/num_boot_reps).toFixed(1) + '%');
$("#bootbtn").prop("disabled", false);
}
};
/* ############################################################### */
var BARY_WIDTH = 300;
var BARY_HEIGHT = 220;
var i;
var radius = 3;
var hist_svg = d3.select('#hist').append('svg')
.attr({'width': BARY_WIDTH,
'height': BARY_HEIGHT, })
.style('background', 'white');
var TREE1COLOR = '#0E3';
var TREEPREF1COLOR = '#EFD';
var TREE2COLOR = '#00F';
var TREEPREF2COLOR = '#EEF';
var TREE3COLOR = '#F00';
var TREEPREF3COLOR = '#FEE';
var prob_vec = [[1/3, 1/3, 1/3],
[1/2, 1/4, 1/4],
[1/4, 1/2, 1/4],
[1/4, 1/4, 1/2]
] ;
var BARY_MARGIN = 10;
var BARY_LEFT_MARGIN = 40;
var BARY_RIGHT_MARGIN = 10;
var BARY_X_SCALER = BARY_HEIGHT - BARY_RIGHT_MARGIN - BARY_LEFT_MARGIN;
var BARY_BASE = BARY_HEIGHT - BARY_MARGIN;
var BARY_TOP = 20;
var BARY_Y_SCALER = BARY_BASE - BARY_TOP;
if (BARY_Y_SCALER > BARY_X_SCALER) {
BARY_Y_SCALER = BARY_X_SCALER;
} else {
BARY_X_SCALER = BARY_Y_SCALER;
}
var barycentric2xy = function(probs) {
var y = BARY_BASE - BARY_Y_SCALER*probs[0];
var x = BARY_LEFT_MARGIN + BARY_X_SCALER*(probs[0] + probs[1]*2.0)/1.7320508075688767;
return {'x':x, 'y':y};
}
var barY = function(data, i) {
return barycentric2xy(data).y;
};
var barX = function(data, i) {
return barycentric2xy(data).x;
};
var lower_left_tri = barycentric2xy([0, 0])
var lower_right_tri = barycentric2xy([0,1])
var top_tri = barycentric2xy([1, 0])
var mid_left_tri = barycentric2xy([0.5, 0])
var mid_right_tri = barycentric2xy([0.5, 0.5])
var mid_bottom_tri = barycentric2xy([0, 0.5])
var mid_point_tri = barycentric2xy([1/3, 1/3])
var t1pathSpec = 'M ' + mid_point_tri.x + ',' + mid_point_tri.y + 'L ' + mid_left_tri.x + ',' + mid_left_tri.y + 'L ' + top_tri.x + ',' + top_tri.y + 'L ' + mid_right_tri.x + ',' + mid_right_tri.y + ' Z' ;
var t2pathSpec = 'M ' + mid_point_tri.x + ',' + mid_point_tri.y + 'L ' + mid_right_tri.x + ',' + mid_right_tri.y + 'L ' + lower_right_tri.x + ',' + lower_right_tri.y + 'L ' + mid_bottom_tri.x + ',' + mid_bottom_tri.y + ' Z' ;
var t3pathSpec = 'M ' + mid_point_tri.x + ',' + mid_point_tri.y + 'L ' + mid_left_tri.x + ',' + mid_left_tri.y + 'L ' + lower_left_tri.x + ',' + lower_left_tri.y + 'L ' + mid_bottom_tri.x + ',' + mid_bottom_tri.y + ' Z' ;
hist_svg.append('path')
.attr({'stroke': TREEPREF1COLOR,
'fill': TREEPREF1COLOR,
'd': t1pathSpec});
hist_svg.append('path')
.attr({'stroke': TREEPREF2COLOR,
'fill': TREEPREF2COLOR,
'd': t2pathSpec});
hist_svg.append('path')
.attr({'stroke': TREEPREF3COLOR,
'fill': TREEPREF3COLOR,
'd': t3pathSpec});
hist_svg.append('text')
.attr({'x': lower_left_tri.x - 36,
'y': lower_left_tri.y + 6,
'stroke': TREE3COLOR,
'fill': TREE3COLOR})
.text("H+C");
hist_svg.append('text')
.attr({'x': lower_right_tri.x + 2,
'y': lower_right_tri.y + 6,
'stroke': TREE2COLOR,
'fill': TREE2COLOR})
.text("H+G");
hist_svg.append('text')
.attr({'x': top_tri.x - 15,
'y': top_tri.y - 6,
'stroke': TREE1COLOR,
'fill': TREE1COLOR})
.text("C+G");
hist_svg.append('line')
.attr({'x1': mid_left_tri.x,
'y1': mid_left_tri.y,
'x2': mid_point_tri.x,
'y2': mid_point_tri.y,
'stroke': '#000',
'stroke-width': 1});
hist_svg.append('line')
.attr({'x1': mid_right_tri.x,
'y1': mid_right_tri.y,
'x2': mid_point_tri.x,
'y2': mid_point_tri.y,
'stroke': '#000',
'stroke-width': 1});
hist_svg.append('line')
.attr({'x1': mid_bottom_tri.x,
'y1': mid_bottom_tri.y,
'x2': mid_point_tri.x,
'y2': mid_point_tri.y,
'stroke': '#000',
'stroke-width': 1});
hist_svg.append('line')
.attr({'x1': lower_left_tri.x,
'y1': lower_left_tri.y,
'x2': lower_right_tri.x,
'y2': lower_right_tri.y,
'stroke': '#000',
'stroke-width': 2});
hist_svg.append('line')
.attr({'x1': lower_left_tri.x,
'y1': lower_left_tri.y,
'x2': top_tri.x,
'y2': top_tri.y,
'stroke': '#000',
'stroke-width': 2});
hist_svg.append('line')
.attr({'x1': top_tri.x,
'y1': top_tri.y,
'x2': lower_right_tri.x,
'y2': lower_right_tri.y,
'stroke': '#000',
'stroke-width': 2});
var bary_points = hist_svg
.append('circle')
.attr({'cx': mid_point_tri.x,
'cy': mid_point_tri.y,
'r': radius,
'fill': '#000',
'stroke': '#000',
'id': 'origpoint'});
var abBootText = d3.select("#abBootText");
var acBootText = d3.select("#acBootText");
var adBootText = d3.select("#adBootText");
function update_bary() {
var denom = ab + ac + ad;
var p_arr = [1/3, 1/3, 1/3];
if (denom > 0) {
p_arr = [ab/denom, ac/denom, ad/denom];
}
coord_vec = barycentric2xy(p_arr);
hist_svg.selectAll("circle")
.data([])
.exit().remove();
hist_svg.append('circle')
.attr({'cx': coord_vec.x,
'cy': coord_vec.y,
'r': radius,
'fill': '#FFF',
'stroke': '#000',
'id': 'origpoint'});
abBootText.attr('fill', TREE1COLOR).attr('stroke', TREE1COLOR).text('C+G ??.?%');
acBootText.text('H+G ??.?%').attr('fill', TREE2COLOR).attr('stroke', TREE2COLOR);
adBootText.text('H+C ??.?%').attr('fill', TREE3COLOR).attr('stroke', TREE3COLOR);
}
var count_vec_radius = function(data, i) {
if (i > 0) {
return 2;
}
return 0;
}
var count_vec_to_x = function(v) {
var c = v.counts;
var denom = c[0] + c[1] + c[2];
var p_arr = [c[0]/denom, c[1]/denom, c[2]/denom];
var coord_vec = barycentric2xy(p_arr);
return coord_vec.x;
}
var count_vec_to_y = function(v) {
var c = v.counts;
var denom = c[0] + c[1] + c[2];
var p_arr = [c[0]/denom, c[1]/denom, c[2]/denom];
var coord_vec = barycentric2xy(p_arr);
return coord_vec.y;
}
var update_bary_for_boot = function (p_arr) {
var curr_denom = curr_points[0] + curr_points[1] + curr_points[2];
if (curr_denom == 0) {
norm_curr_points = [1, 1, 1];
} else {
norm_curr_points = [curr_points[0], curr_points[1], curr_points[2]];
}
hist_svg.selectAll("circle")
.data([])
.exit().remove();
hist_svg.selectAll("circle")
.data([{'counts': norm_curr_points}])
.enter().append('circle')
.attr({'cx': count_vec_to_x,
'cy': count_vec_to_y,
'fill':'#FF0',
'stroke': '#000',
'r': 3,
'class': '.bootpoint'});
hist_svg.selectAll("circle")
.data(prev_points)
.enter().append('circle')
.attr({'cx': count_vec_to_x,
'cy': count_vec_to_y,
'fill':'#000',
'r': count_vec_radius,
'class': '.bootpoint'});
var denom = ab + ac + ad;
var p_arr = [1/3, 1/3, 1/3];
if (denom > 0) {
p_arr = [ab/denom, ac/denom, ad/denom];
}
coord_vec = barycentric2xy(p_arr);
hist_svg.append('circle')
.attr({'cx': coord_vec.x,
'cy': coord_vec.y,
'r': radius,
'fill': '#FFF',
'stroke': '#000',
'id': 'origpoint'});
}
update_bary();
</script>