Population genetics course resources: Exercise coding a wright-fisher model of selection and drift

A homework exercise coding a wright-fisher model of selection and drift. There’s a variety of ways this could be done, the code is setup to guide a person through one of those.

##Code a Wright-Fisher model of a single locus experiencing selection and genetic drift. 
#To do this write the following functions and for loop.
##complete the exercises at the bottom of this file using your simulation code.
##Hand in your script, and the graphs and calculations. 

##the absolute fitnesses (viabilities) of the three genotypes
wAA=0.9
wAa=0.8
waa=0.7

N=1000 ## your population size 
p= 0.1  ##the initial frequency of the A allele

#PART 1
##Write a function which give the allele frequency p and the population size
##simulates and returns the population number of each of the three genotypes at birth, assuming that parents mate at random
##i.e. each child's genotype represents a binomial draw of size 2 from the population frequency. 
##the command rbinom(n,size,prob) will be useful here, note that the argument n, allows you to do this binomial draw n times
sample.population<-function(p,N){

return(c(NAA,NAa,Naa))
}


##PART 2
##write a function which given the number of each genotype at birth
##calculates the expected frequency of the A allele in adults, given the absolute fitnesses wAA,wAa,waa
new.frequency<-function(NAA,NAa,Naa,N){
	
	
return(new.p)	
}

##PART 3
##write a for loop that uses these two functions to
##simulate a population of children given the frequency of A: p
##then  calculate the frequency of A in adults
##use this new frequency to simulate a new population of children
## and iterate this over 1000 (or more) generations

##store the frequency over time and plot it. 

##CONGRATULATIONS you've just coded the Wright-Fisher model for a single locus.
##You should explore a range of parameters to improve your intuition.  Brief exercises using your code:
#Exercise 1
##using the fitnesses: wAA = 0.5, wAa=0.8, waa=0.7
##plot the frequency over generations
##confirm that your simulations approach the right equilibrium frequency

###Exercise 2: Using the following selection coefficients: wAA=0.8, wAa=0.78,waa=0.76 and a population size of 1000. How many generations 
##does your simulation take to travel from p=.05 to p=.95? Does this match your theoretical prediction. 

#Exercise 3
##Set the fitnesses to be identical across the three genotypes
##run a number of simulations, graphing a few on the same graph
##try a few different population sizes to get a sense of the rate of genetic drift
##Using different combinations of fitnesses (giving the A allele a directional advantage) and population size
##explore when selection dominates and when drift dominates. Briefly comment. 


 

If you do use these scripts and figures, please acknowledge that fact (mainly so that others can find this resource). Also if you do use them it would be great if you could add a comment to the post, so I can see how widely used they are, to get a sense of how worthwhile this is. If you find a bug or make an improved version do let me know.

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.

This entry was posted in popgen teaching, Programming exercises, teaching. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s