Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const assert = require('assert');
- // This is an implementation of the Hardy-Weinberg frequencies in Javascript.
- //
- // We are going to consider a simple gene with 2 alleles, and 3 genotypes (A1A1, A1A2, A2A2)
- // We are going to model this by using p for the frequency of the A1 allele, q for
- // the frequency of the A2 allele, and using the HW principle, we get the following frequencies
- // based on our original input:
- //
- // A1A1 = p * p = p^2
- // A1A2 = p * q = pq
- // A2A1 = q * p = qp (--> follows from above = 2pq)
- // A2A2 = q * q = q^2
- //
- // So we get a final polynomial function of p^2 + 2pq + q^2 = 1.
- var a1a1 = 0.15;
- var a2a2 = 0.35;
- var a1a2 = 1 - (a1a1 + a2a2);
- var p = a1a1 + (a1a2 / 2); // Because half of them are from the heterozygous cell
- var q = 1 - p; // Because both of them must add up to 1.
- function create_next_generations(runs, first_gen_prob) {
- console.log("generation 0:\t" + a1a1 + "\t" + a1a2 + "\t" + a2a2);
- function _round_number(value, decimals) {
- var shifter = Math.pow(10, decimals);
- return Math.round(value * shifter) / shifter;
- }
- function _next_gen(runs, current_gen, gen_prob) {
- p = gen_prob[0];
- q = gen_prob[1];
- // Now, let's calculate the next generation
- a1a1 = _round_number(p * p, 2);
- a1a2 = _round_number(2 * p * q, 2);
- a2a2 = _round_number(q * q, 2);
- // Verify correctness of the result.
- all = a1a1 + a1a2 + a2a2;
- assert.equal(all, 1);
- next_gen = current_gen+1;
- console.log("generation " + next_gen +":\t" + a1a1 + "\t" + a1a2 + "\t" + a2a2);
- next_generation_probabilities = [a1a1, a1a2, a2a2];
- allelic_probabilities = [p, q];
- if (current_gen == runs-1) {
- return next_generation_probabilities;
- } else {
- _next_gen(runs, current_gen+1, allelic_probabilities);
- }
- }
- return _next_gen(runs, 0, first_gen_prob);
- }
- create_next_generations(12, [p, q]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement