Last active
July 10, 2021 11:50
-
-
Save github524/8f42fa89aca82a7bb7bb4f85f2d8c686 to your computer and use it in GitHub Desktop.
Context: You’re part of a research team that has found a new mysterious organism at the bottom of the ocean near hydrothermal vents. Your team names the organism, Pila aequor (P. aequor), and finds that it is only comprised of 15 DNA bases. The small DNA samples and frequency at which it mutates due to the hydrothermal vents make P. aequor an in…
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 | |
} | |
// create array of unique Specimen Numbers | |
const specimenNumbersUsed = [] | |
function PAequorFactory(num, arr){ | |
// prevent duplication by checking with unique array above | |
if(specimenNumbersUsed.includes(num)){ | |
return ("That Speciment number is already taken. Taken numbers include: " + specimenNumbersUsed) | |
} else { | |
// if unique, push into array | |
specimenNumbersUsed.push(num); | |
return { | |
specimenNum: num, | |
dna: arr, | |
// method to randomly select an element in the dna array and replace it with another random dnaBase | |
mutate(){ | |
let randomChoice = this.dna[Math.floor(Math.random()* this.dna.length)]; | |
//console.log(randomChoice) | |
let replaceWith = returnRandBase(); | |
// while loop to prevent replacing element with its same type | |
while(randomChoice === replaceWith) { | |
replaceWith = returnRandBase(); | |
} | |
//console.log(replaceWith) | |
console.log(`"${randomChoice}" at index ${this.dna.indexOf(randomChoice)} replaced with "${replaceWith}".`) | |
if(randomChoice === replaceWith){ | |
console.log('ERROR - same letter replaced') | |
} | |
return this.dna.splice(this.dna.indexOf(randomChoice),1 ,replaceWith) | |
}, | |
// method to compare the dna arrays of two specimens and return percentage match of same element at same index | |
compareDNA(otherSpecimensDNA) { | |
let count = 0 | |
let collection =[] | |
for(let i = 0; i < this.dna.length; i++) { | |
if(this.dna[i] === otherSpecimensDNA.dna[i]){ | |
count ++; | |
} | |
} | |
let percentage = parseInt((count / this.dna.length * 100).toFixed(1)); | |
collection.push(this.specimenNum,otherSpecimensDNA.specimenNum, percentage) | |
console.log(`Specimen #${this.specimenNum} DNA Strand: ${this.dna} \nSpecimen #${otherSpecimensDNA.specimenNum} DNA Strand: ${otherSpecimensDNA.dna}.`) | |
console.log(`Specimen #${this.specimenNum} and Specimen #${otherSpecimensDNA.specimenNum} have ${percentage}% DNA in common`) | |
}, | |
// method to return true of dna array contains 60% or more 'C' and 'G' dnaBases. | |
willLikelySurvive(){ | |
let result = this.dna.reduce(function(obj,x) { | |
let res = obj[x] = (obj[x] || 0) + 1; | |
return obj | |
},{}); | |
return (result['C'] + result['G']) / this.dna.length * 100 > 59 ? true : false; | |
}, | |
// method to create a complementary DNA strand that switches letters | |
complementStrand() { | |
return this.dna.map(base => { | |
switch(base) { | |
case 'A': | |
return 'T'; | |
break; | |
case 'T': | |
return 'A'; | |
break; | |
case 'C': | |
return 'G'; | |
break; | |
case 'G': | |
return 'C'; | |
default: | |
break; | |
} | |
}) | |
}, | |
} | |
} | |
}; | |
// create function to generate 30 instances of organisms with willLikelySurvive() === true. (ie, they can survive in their environment) | |
// put the survivors in here | |
const pAequorSurvivers = [] | |
// function will keep creating survivors whilst there are < 30 in the above array. | |
function generateSurvivors() { | |
// to ensure specimenNum remains unique | |
let num = 1; | |
while(pAequorSurvivers.length < 30){ | |
let survivor = PAequorFactory(num,mockUpStrand()) | |
// if the created survivor likely survive, push into the array | |
if(survivor.willLikelySurvive()){ | |
pAequorSurvivers.push(survivor) | |
} | |
// increment num to ensure unique specimenNum | |
num++ | |
} | |
console.log(`Survivors Array, "pAequorSurvivers" populated with ${pAequorSurvivers.length} entries. To see list of Arrays call showSurvivorsArrays()\n`) | |
}; | |
// -- see all arrays of survivors | |
function showSurvivorsArrays(){ | |
for(let i = 0; i < pAequorSurvivers.length; i++){ | |
console.log(`Specimen #${pAequorSurvivers[i].specimenNum}, DNA Strand: [${pAequorSurvivers[i].dna}].`) | |
} | |
console.log(`\nTotal number of Specimens: ${pAequorSurvivers.length}`) | |
}; | |
// check that the the 30 instances will survive | |
function checkTrue(){ | |
for(let i = 0; i < pAequorSurvivers.length; i++) { | |
// re-number their specimenNum 1-30 | |
pAequorSurvivers[i].specimenNum = i+1 | |
console.log('Speciment number ' + pAequorSurvivers[i].specimenNum) | |
console.log("Can survive? " +pAequorSurvivers[i].willLikelySurvive()) | |
} | |
} | |
// ---- invokations --- | |
//-- mutate | |
// let specimen1 = PAequorFactory(1, mockUpStrand()) | |
// console.log(specimen1.dna) | |
// specimen1.mutate() | |
// console.log(specimen1.dna) | |
// -- function to run specimen1.mutate() 1000 times to check that selected random values are never passed if they are the same | |
// let specimen1 = PAequorFactory(1, mockUpStrand()) | |
// function testMutate() { | |
// for(let i=0;i<100;i++) { | |
// specimen1.mutate() | |
// } | |
// } | |
// testMutate() | |
// -- generate 30 organisms that will survive their environment | |
//generateSurvivors() | |
// see all arrays (30) of survivors | |
//showSurvivorsArrays() | |
//-- check that each of the 30 have willLikelySurvive == true | |
//checkTrue() | |
// --generate a complementary strand of dna and display | |
// let specimen1 = PAequorFactory(1, mockUpStrand()) | |
// console.log(specimen1.dna) | |
// let specimen1Partner = specimen1.complementStrand(); | |
// console.log(specimen1Partner) | |
// --compare two specimens | |
// let specimen1 = PAequorFactory(1, mockUpStrand()) | |
// let specimen2 = PAequorFactory(2, mockUpStrand()) | |
// specimen1.compareDNA(specimen2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment