-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
136 lines (124 loc) · 3.95 KB
/
main.js
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
// Returns a random DNA base
const returnRandBase = () => {
const dnaBases = ['A', 'T', 'C', 'G']
return dnaBases[Math.floor(Math.random() * 4)]
}
// Returns a random single strand of DNA containing 15 bases
const mockUpStrand = () => {
const newStrand = []
for (let i = 0; i < 15; i++) {
newStrand.push(returnRandBase())
}
return newStrand
}
// factory function to create multiple p.aequor speciments
const pAequorFactory = (specimenNum,dna) => {
return{
specimenNum: specimenNum,
dna:dna,
//specimen's method to mutate a randomly selected base of its DNA strand
mutate() {
let index = Math.floor(Math.random()*16);
this.dna.splice(index,0,returnRandBase());
return dna;
},
//specimen's method to compare its own dna sequences with that of a different P. aequor specimen
compareDNA(pAequor) {
let identicalBaseCount = 0;
for (i=0; i<dna.length;i++){
if (this.dna[i] === pAequor.dna[i]) {
identicalBaseCount ++;
}
}
let msg = `Specimen:${this.specimenNum} and Specimen:${pAequor.specimenNum} have ${((identicalBaseCount /dna.length)*100).toFixed(1)} % DNA in commom`;
return msg;
},
//willLikelySurvive() determines if specimen is likely to survive
willLikelySurvive() {
let baseCount = 0;
this.dna.forEach(DNAbase => {
if ((DNAbase === 'C') || (DNAbase === 'G')) {
baseCount++;
}
})
let survival = ((baseCount/this.dna.length)*100).toFixed(1);
if (survival >= 60) {
return true;
} else {
return false;
}
},
//specimen's method to auto create its own complementary DNA Strand
complementStrand() {
let complementaryDNA = [];
for (const base of this.dna) {
switch (base) {
case 'A':
complementaryDNA.push('T');
break;
case 'T':
complementaryDNA.push('A');
break;
case 'C':
complementaryDNA.push('G');
break;
case 'G':
complementaryDNA.push('C');
break;
}
}
return complementaryDNA;
}
};
}// end of factory function
// function to generate a desired number of specimens that are more likely to survive
let pAequorSpecimens = [];//holds the specimens
const createSpecimens = number =>{
let count = 1;
while (count<=number) {
let specimen= pAequorFactory(count,mockUpStrand());
if (specimen.willLikelySurvive() === true){
pAequorSpecimens.push(specimen);
count++;
}
}
return pAequorSpecimens;
}
//function to build complementary specimens from existing ones
let compAequorSpecimens = []; //holds the complementary specimens
const createComSpecimens = baseSpecimens => {
for(const specimen of baseSpecimens){
compAequorSpecimens.push(pAequorFactory(specimen.specimenNum,specimen.complementStrand()));
}
return compAequorSpecimens;
}
//compare DNA of speciments
const compareSpecimensDNA = (specimensx) =>{
for (const strandx of specimensx) {
for (const strandy of specimensx){
if (strandx !== strandy){
console.log(strandx.compareDNA(strandy));
}
}
}
};
//log speciments from pAequorSpecimens array of speciments to console
const logToConsole = (specimensArray, num) =>{
if (specimensArray === pAequorSpecimens) {
createSpecimens(num);
specimensArray.forEach(specimen => {
console.log(`pAequorSpecimens: Specimen ${specimen.specimenNum}: DNA: ${specimen['dna']}`);
});}
if (specimensArray === compAequorSpecimens) {
createSpecimens(num);
createComSpecimens(pAequorSpecimens);
specimensArray.forEach(specimen => {
console.log(`compAequorSpecimens: Specimen ${specimen.specimenNum}: DNA: ${specimen['dna']}`);
});}
}
// tests
logToConsole(pAequorSpecimens,5); // Donot comment out this while testing the other functionalities below
//logToConsole(compAequorSpecimens,05);
//console.log(pAequorSpecimens[0].mutate()); //works
//console.log(pAequorSpecimens[0].compareDNA(pAequorSpecimens[1])); // works
//compareSpecimensDNA(pAequorSpecimens); works