-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-organism.ts
More file actions
112 lines (111 loc) · 3.69 KB
/
Copy pathclass-organism.ts
File metadata and controls
112 lines (111 loc) · 3.69 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
// for JSON data
export class PAequorSamples {
#dnaBases = ['A', 'T', 'C', 'G'];
_dna;
dnaLength;
// reflects number of completed iterations w/in survivingOrganismsFill() until this organism was created -- zero-based
specimenNum;
constructor(i: number) {
this.specimenNum = i;
this._dna = this.#mockUpStrand();
this.dnaLength = this._dna.length;
}
get dna() {
return this._dna;
}
#mockUpStrand(): string[] {
const newStrand: string[] = [];
for (let i = 0; i < 15; i++)
newStrand.push(this.#dnaBases[this.#ranNum(this.#dnaBases.length)]);
return newStrand;
}
#ranNum(multiplier: number) {
return Math.floor(Math.random() * multiplier);
}
willLikelySurvive() {
let count = 0;
for (let base of this._dna) if (base === 'C' || base === 'G') count++;
return count / this.dnaLength >= 0.6;
}
}
// for experimenting w/samples
export class PAequorTesting {
#complementStrand: string[] = [];
#dnaBases = ['A', 'T', 'C', 'G'];
_dna;
dnaLength;
// reflects number of completed iterations w/in survivingOrganismsFill() until this organism was created -- zero-based
specimenNum: number;
constructor(jsonSample: {
_dna: string[];
dnaLength: number;
specimenNum: number;
}) {
this._dna = jsonSample._dna;
this.dnaLength = jsonSample.dnaLength;
this.specimenNum = jsonSample.specimenNum;
this.#setComplementStrand(this._dna);
}
compareDNAWith(pAequor: { dna: string[]; specimenNum: number }) {
let count = 0;
for (let i = 0; i < this.dnaLength; i++)
this._dna[i] === pAequor.dna[i] && count++;
return `This specimen, #${this.specimenNum}, and specimen #${
pAequor.specimenNum
} have ${((count / this.dnaLength) * 100).toFixed(2)}% DNA in common`;
}
get complementStrand() {
return this.#complementStrand;
}
get dna() {
return this._dna;
}
#mockUpStrand(): string[] {
const newStrand: string[] = [];
for (let i = 0; i < 15; i++)
newStrand.push(this.#dnaBases[this.#ranNum(this.#dnaBases.length)]);
return newStrand;
}
// mutate single dna pair
mutate() {
const baseIndex = this.#ranNum(this.dnaLength);
const selectedBase = this._dna[baseIndex];
const newBases = this.#dnaBases.filter(base => base !== selectedBase);
this._dna[baseIndex] = newBases[this.#ranNum(newBases.length)];
if (!this.willLikelySurvive())
console.log(
'The mutation of a DNA base-pair has caused this organism to die.'
);
this.#setComplementStrand(this._dna);
return this._dna;
}
#ranNum(multiplier: number) {
return Math.floor(Math.random() * multiplier);
}
#setComplementStrand(dna: string[]) {
this.#complementStrand = [];
for (let base of dna)
switch (base) {
case 'A':
this.#complementStrand.push('T');
break;
case 'T':
this.#complementStrand.push('A');
break;
case 'C':
this.#complementStrand.push('G');
break;
case 'G':
this.#complementStrand.push('C');
break;
default:
'bug in the code';
}
return;
}
willLikelySurvive() {
let count = 0;
for (let base of this._dna) if (base === 'C' || base === 'G') count++;
return count / this.dnaLength >= 0.6;
}
}