-
-
Save peterghostine/3f1715ce058a3f35a9b6872f2e9ac7a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Returns a random DNA base | |
const returnRandBase = () => { | |
const dnaBases = ['A', 'T', 'C', 'G'] | |
return dnaBases[Math.floor(Math.random() * 4)] | |
} | |
// Returns a random single stand of DNA containing 15 bases | |
const mockUpStrand = () => { | |
const newStrand = [] | |
for (let i = 0; i < 15; i++) { | |
newStrand.push(returnRandBase()) | |
} | |
return newStrand | |
} | |
function pAequorFactory(n, arrDNA) { | |
// Add properties specimenNum and dna. | |
let pAequor = { | |
specimenNum: n, | |
dna: arrDNA, | |
//[ 'C', 'G', 'A', 'C', 'T', 'T', 'A', 'A', 'C', 'G', 'G', 'C', 'A', 'G', 'A' ], | |
mutate: function() { | |
let dnaBases = ['A', 'T', 'C', 'G']; | |
// Randomly choose an index position inside this.dna. | |
randDNABaseIndex = Math.floor(Math.random() * this.dna.length); | |
// Read the DNA base at this random position. | |
randDNABase = this.dna[randDNABaseIndex]; | |
// Remove the DNA base from the list of posible replacements. | |
dnaBases = dnaBases.filter(item => !(item === randDNABase)); | |
// Randomly choose a DNA base from the list of candidates. | |
const newDNABase = dnaBases[Math.floor(Math.random() * dnaBases.length)]; | |
// Change the DNA base in this.dna. | |
this.dna[randDNABaseIndex] = newDNABase; | |
return this.dna; | |
}, | |
compareDNA: function(some_pAequor) { | |
const length = some_pAequor.dna.length; | |
let similarities = 0; | |
for (let i = 0; i < length ; i++) { | |
// console.log(somePAequor.dna[i] === this.dna[i]); | |
if (some_pAequor.dna[i] === this.dna[i]) { | |
// console.log(i); | |
similarities = similarities + 1; | |
} | |
} | |
return Math.round(similarities * 10000 / length, 2) / 100; | |
// console.log(`Specimen #${this.specimenNum} and specimen #${some_pAequor.specimenNum} have ${Math.round(similarities * 10000 / length, 2) / 100}% DNA in common.`); | |
}, | |
willLikelySurvive: function() { | |
const dnaCorG = this.dna.filter(item => (item === 'C' || item === 'G')); | |
if ((dnaCorG.length / this.dna.length) >= 0.6) { | |
return true; | |
} else { | |
return false; | |
} | |
}, | |
complementStrand: function() { | |
return this.dna.map(item => { | |
switch (item) { | |
case 'A': | |
return 'T'; | |
break; | |
case 'T': | |
return 'A'; | |
break; | |
case 'C': | |
return 'G'; | |
break; | |
case 'G': | |
return 'C'; | |
break; | |
} | |
}); | |
} | |
} | |
// Mutate pAequor before returning it. | |
pAequor.mutate(); | |
return pAequor; | |
} | |
// Create 30 instances of pAequor. | |
let pAequorArr = []; | |
for (let i = 0; i <= 30; i++) { | |
pAequorArr.push(pAequorFactory(1, mockUpStrand())); | |
} | |
// Find two most related instances. | |
function findTwoMostRelated(arr) { | |
let mostRelated = [0, 0, 0]; | |
for (let i = 0; i < arr.length; i++) { | |
for (let j = i + 1; j < arr.length; j++) { | |
let compare = arr[i].compareDNA(arr[j]); | |
if (compare > mostRelated[2]) { | |
mostRelated = [i, j, compare]; | |
} | |
} | |
} | |
console.log(`Specimen #${mostRelated[0]} and specimen #${mostRelated[1]} are related by a factor of ${mostRelated[2]}.`); | |
console.log(`Specimen #${mostRelated[0]}: ${pAequorArr[mostRelated[0]].dna}`) | |
console.log(`Specimen #${mostRelated[1]}: ${pAequorArr[mostRelated[1]].dna}`) | |
} | |
findTwoMostRelated(pAequorArr); | |
// Uncomment below to display the DNA property of all 30 specimens, along with their likelyhood of survival and their complements. | |
/* | |
for (let i = 0; i < pAequorArr.length; i++){ | |
console.log(`Specimen #${i}: ${pAequorArr[i].dna}` + ' (' + pAequorArr[i].willLikelySurvive() + ')'); | |
console.log(`Complement #${i}: ` + pAequorArr[i].complementStrand()); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment