-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenetic_drift.html
More file actions
222 lines (190 loc) · 6.63 KB
/
Genetic_drift.html
File metadata and controls
222 lines (190 loc) · 6.63 KB
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
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v4.min.js" charset="utf-8"></script>
</head>
<body>
<script>
/*function round_number(value,decimals){
var shifter=Math.pow(10,decimals);
return Math.round(value*shifter)/shifter;
}*/
function draw_line_chart(data,x_label,y_label,legend_values,x_max,y_max_flex) {
var margin = {top: 20, right: 20, bottom: 50, left: 50},
width = 700 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var version = d3.scale ? 3 : 4;
var color = (version == 3 ? d3.scale.category10() : d3.scaleOrdinal(d3.schemeCategory10));
if (!x_max) {
x_max = data[0].length > 0 ? data[0].length : data.length
}
var y_max = data[0].length > 0 ? d3.max(data, function(array) {
return d3.max(array);
}) : d3.max(data);
var x = (version == 3 ? d3.scale.linear() : d3.scaleLinear())
.domain([0,x_max])
.range([0, width]);
var y = y_max_flex ? (version == 3 ? d3.scale.linear() : d3.scaleLinear())
.domain([0, 1.1 * y_max])
.range([height, 0]) : (version == 3 ? d3.scale.linear() : d3.scaleLinear())
.range([height, 0]);
var xAxis = (version == 3 ? d3.svg.axis().scale(x).orient("bottom") :
d3.axisBottom().scale(x));
var yAxis = (version == 3 ? d3.svg.axis().scale(y).orient("left") :
d3.axisLeft().scale(y));
var line = (version == 3 ? d3.svg.line() : d3.line())
.x(function (d, i) {
var dat = (data[0].length > 0 ? data[0] : data);
return x((i/(dat.length-1)) * x_max);
})
.y(function (d) {
return y(d);
});
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.style("text-anchor", "middle")
.attr("x", width / 2)
.attr("y", 6)
.attr("dy", "3em")
.style("fill", "#000")
.text(x_label);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height / 2)
.attr("dy", "-3.5em")
.style("text-anchor", "middle")
.style("fill", "#000")
.text(y_label);
if (legend_values.length > 0) {
var legend = svg.append("text")
.attr("text-anchor", "star")
.attr("y", 30)
.attr("x", width-100)
.append("tspan").attr("class", "legend_title")
.text(legend_values[0])
.append("tspan").attr("class", "legend_text")
.attr("x", width-100).attr("dy", 20).text(legend_values[1])
.append("tspan").attr("class", "legend_title")
.attr("x", width-100).attr("dy", 20).text(legend_values[2])
.append("tspan").attr("class", "legend_text")
.attr("x", width-100).attr("dy", 20).text(legend_values[3]);
}
else {
svg.selectAll("line.horizontalGridY")
.data(y.ticks(10)).enter()
.append("line")
.attr("x1", 1)
.attr("x2", width)
.attr("y1", function(d){ return y(d);})
.attr("y2", function(d){ return y(d);})
.style("fill", "none")
.style("shape-rendering", "crispEdges")
.style("stroke", "#f5f5f5")
.style("stroke-width", "1px");
svg.selectAll("line.horizontalGridX")
.data(x.ticks(10)).enter()
.append("line")
.attr("x1", function(d,i){ return x(d);})
.attr("x2", function(d,i){ return x(d);})
.attr("y1", 1)
.attr("y2", height)
.style("fill", "none")
.style("shape-rendering", "crispEdges")
.style("stroke", "#f5f5f5")
.style("stroke-width", "1px");
}
d3.select("body").style("font","10px sans-serif");
d3.selectAll(".axis line").style("stroke","#000");
d3.selectAll(".y.axis path").style("display","none");
d3.selectAll(".x.axis path").style("display","none");
d3.selectAll(".legend_title")
.style("font-size","12px").style("fill","#555").style("font-weight","400");
d3.selectAll(".legend_text")
.style("font-size","20px").style("fill","#bbb").style("font-weight","700");
if (data[0].length > 0) {
var simulation = svg.selectAll(".simulation")
.data(data)
.enter().append("g")
.attr("class", "simulation");
simulation.append("path")
.attr("class", "line")
.attr("fill", "none")
.attr("d", function(d) { return line(d); })
.style("stroke", function(d,i) { return color(i); });
}
else {
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("fill", "none")
.attr("d", line)
.style("stroke","steelblue");
}
d3.selectAll(".line").style("fill", "none").style("stroke-width","1.5px");
}
var p=0.5;
var N=10;
var data=[];
var generations=100;
var simulations=10;
var population_sizes=[];
function next_generation(simulation_data,current_N){
var draws=2*current_N; //No. of Alleles
var a1=0;
var a2=0;
for(var i=0;i<draws;i++){
if(Math.random()<=p){
a1++;
}else{
a2++;
}
}
p=a1/draws;
simulation_data.push(p);
// Now toss a coin 2000 times and find the percentage of no. of times
// a particular allele or (heads/tails) showed up . based on that allele
//frequencies , update the p and q in this generation which will be used for
// next generation.
}
function simulation(simulation_counter){
p=0.5;
var population_size;
for(var i=0;i<generations;i++)
{
if(i%10==9){
population_size=10;
}else{
population_size=N;
}
population_sizes.push(population_size);
next_generation(data[simulation_counter],population_size);
//console.log("generation "+i,round_number(p,4),round_number(1-p,4));
}
}
for(var i=0;i<simulations;i++){
data.push([]);
simulation(i);
}
function effective_population_size(all_Ns){
var denominator=0;
for(var i=0;i<all_Ns.length;i++){
denominator+=(1/all_Ns[i]);
}
return Math.round(all_Ns.length/denominator);
}
var Ne=effective_population_size(population_sizes);
draw_line_chart(data,"Generation","p",["Eff. Population Size:",Ne,"Generations",generations]);
</script>
</body>
</html>