Published
Edited
Apr 29, 2021
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
fractionZeroAlleles = {
const data = []
generationsWF.forEach((pop, gen) => {
data.push({
gen,
fraction: calcFractionZeroAlleles(pop),
type: "Wright-Fisher"
})
data.push({
gen,
fraction: calcFractionZeroAlleles(generationsM[gen]),
type: "Moran"
})
data.push({
gen,
fraction: calcFractionZeroAlleles(generationsM[gen * N]),
type: "Moran * N"
})
})
return data
}
Insert cell
Insert cell
Insert cell
function simulateMoran() {
// const initial = Array.from({ length: N }, () => randBit())
const initial = generationsWF[0]
const generations = [initial];
for (let i = 0; i < (r-1) * N; ++i) {
let pop = generations[generations.length - 1]

// Copy parent generation
let offspring = pop.map(genotype => genotype)
// Pick a random individual to clone and a random to die
const [i, j] = randWithReplacement()
// Replace type on site j with the type on site i
offspring[j] = offspring[i]
generations.push(offspring)
}
return generations
}
Insert cell
function simulateWrightFisher() {
const initial = Array.from({ length: N }, () => randBit())
const gen = [initial];
for (let i = 0; i < r-1; ++i) {
let pop = gen[gen.length - 1]
let offspring = Array.from({ length: N }, () => {
return pop[randInt(N)]
})
gen.push(offspring)
}
return gen
}
Insert cell
calcFractionZeroAlleles = (pop) => {
let numOneAlleles = pop.reduce((tot, bit) => tot + bit, 0)

return (N - numOneAlleles) / N
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more