Skip to content

Commit 2bb0249

Browse files
Courssumm02Courssumm02
authored andcommitted
Trying Again
1 parent aa20311 commit 2bb0249

File tree

2 files changed

+98
-14
lines changed

2 files changed

+98
-14
lines changed

Module 3/app.js

Lines changed: 85 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,86 @@
1-
const BASES = ["A","T","G","C"]
2-
const DNA_LEN = 100
3-
const duplications = 100
4-
function rand_base(){
5-
return BASES[Math.floor(Math.random() * BASES.length)]
6-
}
7-
var dna = new Array(DNA_LEN).fill(0).map(()=>{return rand_base()})
8-
const original_dna = dna
9-
10-
for (let i = 0; i < 10; i++){
11-
console.log(i);
12-
// From here on!!
1+
function app(){
2+
3+
const BASES = ["A","T","G","C"]
4+
const DNA_LEN = parseInt(document.getElementById("len").value)
5+
const generations = parseInt(document.getElementById("generations").value)
6+
const num_clones = parseInt(document.getElementById("clones").value)
7+
const mutation_rate = parseFloat(document.getElementById("mutations").value)
8+
function rand_base(){
9+
return BASES[Math.floor(Math.random() * BASES.length)]
10+
}
11+
12+
Array.prototype.contains = function(v) {
13+
for (var i = 0; i < this.length; i++) {
14+
if (this[i] === v) return true;
15+
}
16+
return false;
17+
};
18+
19+
Array.prototype.unique = function() {
20+
var arr = [];
21+
for (var i = 0; i < this.length; i++) {
22+
if (!arr.contains(this[i])) {
23+
arr.push(this[i]);
24+
}
25+
}
26+
return arr;
27+
}
28+
29+
function num_Unique(array){
30+
var u = array.map(JSON.stringify)
31+
return u.unique().length
32+
}
33+
34+
var dna = new Array(DNA_LEN).fill(0).map(()=>{return rand_base()})
35+
const original_dna = dna
36+
37+
const clones = new Array(num_clones).fill(original_dna)
38+
39+
var unique_dna = []
40+
41+
for (let i = 0; i < generations; i++){
42+
for (let j = 0; j < clones.length; j++){
43+
clones[j]=clones[j].map((base)=>{
44+
if (Math.random() < mutation_rate){
45+
return rand_base()
46+
}
47+
return base
48+
})
49+
}
50+
unique_dna.push(num_Unique(clones))
51+
}
52+
53+
54+
var ctx = document.getElementById('myChart').getContext('2d');
55+
56+
57+
var unique_dna_data = {
58+
label: "Total Unique DNA's",
59+
data: unique_dna,
60+
lineTension: 0.5,
61+
fill: false,
62+
borderColor: 'red',
63+
borderWidth: 1.5
64+
}
65+
66+
67+
const data = {
68+
labels: [...Array(generations).keys()],
69+
datasets: [unique_dna_data]
70+
};
71+
72+
73+
var myChart = new Chart(ctx, {
74+
type: 'line',
75+
data: data,
76+
options: {
77+
scales: {
78+
y: {
79+
beginAtZero: true
80+
}
81+
},
82+
bezierCurve: true
83+
}
84+
});
85+
myChart.update()
1386
}

Module 3/main.html

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,19 @@
1010
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
1111

1212
</head>
13-
<body class="container">
14-
13+
<body class="container pt-4">
14+
<form action="" id="input" class="pb-4">
15+
<label for="len">DNA length</label>
16+
<input type="number" name="len" id="len" placeholder="100" value="100">
17+
<label for="generations">Number of generations</label>
18+
<input type="number" name="generations" id="generations" placeholder="100" value="100">
19+
<label for="clones">Number of clones</label>
20+
<input type="number" name="clones" id="clones" placeholder="100" value="100">
21+
<label for="mutations">mutations</label>
22+
<input type="number" name="mutations" id="mutations" placeholder="100" value="0.001" step="0.000000001">
23+
</form>
24+
<button type="button" onclick="app();">Run Simulation</button>
25+
<canvas id="myChart"></canvas>
1526
<script src="app.js"></script>
1627
</body>
1728
</html>

0 commit comments

Comments
 (0)