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 ( )
13
86
}
0 commit comments