Page MenuHomec4science

3_Project_inherited_division_death.html
No OneTemporary

File Metadata

Created
Sun, Jun 2, 15:31

3_Project_inherited_division_death.html

<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<script>
function draw_line_chart(data,x_label,y_label,legend_values,x_max,y_max_flex) {
var margin = {top: 20, right: 30, bottom: 80, left: 100},
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", -10) // define y POSITION of x label
.attr("dy", "3em")
.style("fill", "#000")
.style("font-size","12px") // font size of x label
.style("font-weight","700") // weight of x label
.text(x_label);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height / 2)
.attr("y", 20) // define x POSITION of y label
.attr("dy", "-3.5em")
.style("text-anchor", "middle")
.style("fill", "#000")
.style("font-size","25px") // font size of y label
.style("font-weight","700")// weight of y label
.text(y_label);
svg.select(".x.axis")
.selectAll("text")
.style("font-size","18px"); //font size of x axis
svg.select(".y.axis")
.selectAll("text")
.style("font-size","18px"); //font size of y axis
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","20px 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","20px").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");
}
// initiation of variables
// ------ Uncomment if called by "WebPage.html" ------
/*var params = location.href.split('?')[1].split('&');
var N = Number(params[0].split('=')[1]);
var division_rate = Number(params[1].split('=')[1]);
var killing_rate = Number(params[2].split('=')[1]);
var noise_division = Number(params[3].split('=')[1]);
var noise_killing = Number(params[4].split('=')[1]);
*/
// --------------------------------------------------
// ------Comment if called by "WebPage.html"--------
var division_rate = 1/150; //per minutes
var killing_rate = 1/150; //per minutes
var N = 15; // initial population size
var noise_division=0.01;
var noise_killing=0.01;
// --------------------------------------------------
var time_lapse_in_minutes = 1000; // duration of the simulation
var simulations = 10;
var data = [];
var growth_rates= [];
var n_div=0;
// create a new bacterium
function new_bacterium(div_rate){
// inherited division rate (not killing rate)
if (div_rate != undefined) {
var new_bact = { individual_division_rate: div_rate, individual_killing_rate: 1/random_from_normal_distribution(1/killing_rate,noise_killing * (1/killing_rate))};
return new_bact;
}
// random division rate
else {
var new_bact = { individual_division_rate: 1/random_from_normal_distribution(1/division_rate, noise_division * (1/division_rate)), individual_killing_rate: 1/random_from_normal_distribution(1/killing_rate, noise_killing * (1/killing_rate))};
return new_bact;
}
}
// kill a bacterium
function kill_bacterium(index,bacterium_to_kill){
bacterium_to_kill.splice(index,1);
}
//uses Box-muller transform to get normal random variables from uniform random variables
//https://theclevermachine.wordpress.com/2012/09/11/sampling-from-the-normal-distribution-using-the-box-muller-transform/
function random_from_normal_distribution(mean, variance) {
var u1 = Math.random(); // should belong to ]0,1]
if (u1 == 0){
u1 = 1;
}
var u2 = Math.random(); // should belong to ]0,1]
if (u2 == 0){
u2 = 1;
}
theta = 2 * 3.14 * u2;
var r = Math.sqrt(-2 * Math.log(u1));
var x = r * Math.cos(theta);
var y = r * Math.sin(theta);
x = mean + Math.sqrt(variance) * x;
// return only one of the two normal random variable generated
return x;
}
// calculate the population size for each time step
function next_time_step(current_N) {
var next_N = current_N; // creates a copy directly because it is a primitive value
var temp_bacterium = [];
temp_bacterium = bacterium.slice(0); // creates a copy - not the reference of the array.
var all_bacterium_to_kill=[];
for (var i = 0; i < current_N; i++){
//Divide cells with a frequency of "individual_division_rate"
if (binomial_draws(1,bacterium[i].individual_division_rate) == 1){
temp_bacterium.push(new_bacterium(bacterium[i].individual_division_rate)); // division rate IS inherited
next_N = next_N + 1;
n_div=n_div+1;
}
//kills cells with a frequency of "individual_killing_rate"
if (binomial_draws(1 , bacterium[i].individual_killing_rate) == 1){
all_bacterium_to_kill.push(i);
}
}
// kill them all at once otherwise the index changes
for (var i = all_bacterium_to_kill.length-1; i >= 0 ; i--){
kill_bacterium(all_bacterium_to_kill[i],temp_bacterium);
next_N = next_N - 1;
}
bacterium = temp_bacterium;
return next_N;
}
function binomial_draws(n,p){
var number_of_success = 0;
for (var i = 0; i < n; i++){
var random_number = Math.random();
if (random_number < p){
number_of_success = number_of_success + 1;
}
}
return number_of_success;
}
// calculate the final population size for each simulation
function simulation(simulation_counter) {
var population_sizes = [];
population_sizes.push(N);
// calculate the intermediate population sizes for each simulation
for (var i = 0; i < time_lapse_in_minutes; i++){
var new_N = next_time_step(population_sizes[i]);
population_sizes.push(new_N);
if (new_N > 1000){ // in order to limit computation time, stop if population size is bigger than 1000
break;
}
}
// in case I want to compare the growth rates between different simulations - I do not use it now
var sum_of_growth_rates = 0 ;
for (var i = 0; i < population_sizes[time_lapse_in_minutes]; i++){
sum_of_growth_rates += bacterium[i].individual_division_rate;
}
average_growth_rate = sum_of_growth_rates/population_sizes[time_lapse_in_minutes];
return [population_sizes, average_growth_rate];
}
/*function calculate_average(values){
var total = 0;
for (var i = 0; i < values.length; i++) {
total += values[i];
}
return total / values.length;
}*/
// run all simulations
function run_simulations(){
for (var i = 0; i < simulations; i++) {
n_div=0;
bacterium =[];
for (var ii = 0; ii < N; ii++){
bacterium.push(new_bacterium());
}
output_simulation = simulation(i);
console.log(n_div);
population_sizes = output_simulation[0];
//average_growth_rate = output_simulation[1];
data.push(population_sizes);
//growth_rates.push(average_growth_rate);
}
}
run_simulations();
draw_line_chart(data, "Time","Population Size",[],time_lapse_in_minutes,true);
</script>
</body>
</html>

Event Timeline