In python 2, division of integers is not true division. This will be changed in python 3. For example:
1/4
Note that this isn't rounding, but truncation of the remainder.
3/4
There are a few ways around this, but most convenient is to add the the following import
statement. It's always a good idea to add this to your python code to avoid any surprises.
from __future__ import division
3/4
Genetic bottleneck programming example
This example illustrates the concept of genetic drift in a population of genotypes. Imagine we start with four genotypes, A, B, C, and D and 10,000 individuals. First, let’s define a starting population with the following genotype frequencies:
genotype_frequencies = ['A'] * 5000 + ['B'] * 2500 + ['C'] * 1250 + ['D'] * 1250
This syntax may be new to you. Break this apart to individual addends to figure out what the full statement is doing.
Next, let’s define a function that will conveniently allow us to summarize this population:
def summarize_composition(population):
population_size = len(population)
print 'A: %0.4f' % (population.count('A') / population_size)
print 'B: %0.4f' % (population.count('B') / population_size)
print 'C: %0.4f' % (population.count('C') / population_size)
print 'D: %0.4f' % (population.count('D') / population_size)
You can then call this as follows:
summarize_composition(genotype_frequencies)
Next we’re going to import the sample
function from the random
module. Given a list (population
) and a number of elements (k
) to select, sample randomly samples (without replacement) k
elements from the list and returns those as a new list. So, if we sample the full population and summarize the genotype composition, we should get the same result – let’s test it out:
from random import sample
new_genotype_frequencies = sample(genotype_frequencies,10000)
summarize_composition(new_genotype_frequencies)
Now, let’s simulate genetic drift. Imagine we have a population of organisms with the genotype frequencies represented in our genotype_frequencies
list. Regardless of which of these genotypes confers the most selective advantage a random removal of a large component from the population has the ability to affect the resulting genotypic composition.
To simulate an event that randomly kills off 10% of the total population, and look at the resulting genotype composition:
new_genotype_frequencies = sample(genotype_frequencies,9000)
summarize_composition(new_genotype_frequencies)
Do this a few times. You should notice that the frequencies don’t change a lot. What happens if instead of this relatively small dying off, there is a near-extinction event. Simulate an event that randomly kills off 99.9% of the population. What happens now? Run this simulation several times and explain the results of this experiment.