Skip to main content
SciNet
  • Home
  • All Courses
  • Calendar
  • Certificates
  • SciNet
    Main Site Documentation my.SciNet
  • CCDB
  • More
Close
Toggle search input
English
English Français
You are currently using guest access
Log in
SciNet
Home All Courses Calendar Certificates SciNet Collapse Expand
Main Site Documentation my.SciNet
CCDB
Expand all Collapse all
  1. MSC1090 - Fall 2026
  2. Assignment 4

Assignment 4

Completion requirements
Opened: Thursday, 8 October 2026, 10:00 AM
Due: Thursday, 15 October 2026, 11:59 PM

The luck of a mutation

Imagine a single bacterium in a huge population picks up a mutation (let's call it allele 'A') that makes it (and its descendants) resistant to an antibiotic. Right now the antibiotic is not being prescribed, so the mutation confers no advantage: it is neutral. Will the resistance survive in the population?

The answer is: almost certainly not - but not because of selection. It is pure luck. In every generation, some bacteria leave more offspring than others purely by chance, so the frequency of the resistance allele wanders up and down randomly. This random wandering is genetic drift. A brand-new neutral mutation has probability exactly \(1/(2N)\) of eventually taking over the population, where \(N\) is the number of individuals (\(2N\) copies of the gene). For a bacterial population this is essentially zero - but never exactly zero, which is why resistance still eventually appears.

Now suppose the hospital starts using the antibiotic. The mutation suddenly becomes beneficial: carriers of allele A leave, on average, \(1+s\) offspring for every 1 left by non-carriers, where \(s > 0\) is the selection coefficient. Selection pushes the allele up while drift fights back. The central question of this assignment when does selection beat drift, and when does drift beat selection? The rule of thumb is expressed by the dimensionless product \(2Ns\): when \(|2Ns|\) is small compared to 1, drift wins and the mutation behaves almost as if it were neutral; once \(2Ns\) grows past ~1, selection wins.

The standard model for this is the Wright-Fisher model. One generation has two steps, mirroring biology:

  • SELECTION: if carriers have relative fitness \(1+s\), the allele fraction changes deterministically from \(p\) to \(\frac{p(1+s)}{(1 + ps)}\) (the standard haploid selection equation);
  • DRIFT: the next generation allele fraction is obtained by sampling \(2N\) gene copies independently, each with probability \(\frac{p(1+s)}{(1+ps)}\) of being the resistance allele.

With \(s = 0\) the selection step does nothing and you recover purely neutral drift.

0) You must use version control (git), as you develop your code. We suggest you start, from the Linux command line, by creating a new directory, e.g. assignment4, initialize a git repository (git init) within it, and perform git add ..., git commit repeatedly as you add to your code. You will hand in the output of git log for your assignment repository as part of the assignment. You must have a significant number of commits representing the modifications, alterations and changes to your code.


1) Create a file named Drift.Utilities.R containing the following functions.

1a) Create a function called selection.fraction, which takes two arguments, \(p\) (the current fraction of genes which are allele A, between 0 and 1) and \(s\) (the selection coefficient). The function should return the fraction of A after one round of selection, using the haploid selection equation

$$\frac{p (1 + s)}{(1 + ps)}$$

This is the deterministic part of the model: with \(s > 0\) it pushes the frequency up, with \(s < 0\) it pushes it down, and with \(s = 0\) it leaves it unchanged.

1b) Create a function called next.fraction, which takes three arguments, \(p\), \(n\) (the population size), and \(s\), with \(s\) given the default value 0. The function should advance the population by one generation, by:

  • calculating the current probability of the gene being passed on,
  • randomly sampling the number of A copies in the next generation, and
  • return the new fraction.

Because \(s\) has a default value of 0, calling the function with just \(p\) and \(n\) gives pure neutral drift. Note that the bacterial population is assumed to not change in this approach.

1c) Create a function called simulate.evolution, which takes \(p0\) (the starting fraction of allele A), \(n\), an optional \(s\) argument (default 0), and an optional max.generations argument (default 10000). The function should repeatedly apply the next.fraction function until the allele 'fixes' (the fraction becomes exactly 1), is lost (fraction becomes exactly 0), or the max.generation cap is reached. It should return the final fraction and the number of generations that were run.

1d) Create a function called simulate.many.pops, which takes \(s\), \(p\), \(n\), and an option num (default 1000). The function should run num evolutions of independent populations, and return the fraction of times the final allele-A fraction was zero, one, and neither. Your code should use one of the *apply family of functions.


2) Create an R script, named Drift.Analysis.R, which:

  • Sources the file Drift.Utilities.R.
  • Takes an argument from the command line, the number of bacteria in the population, \(n\).
  • Initializes the starting fraction to \(p0 = 1 / (2*n)\).
  • Creates a vector of \(s\) values, equal to $$ \begin{align*} s = c(&-40, -20, -10, -4, -1, -0.25, 0,\\ &0.25, 0.5, 1, 2, 4, 10, 20, 40, 60, 80) * p0 \end{align*} $$ If any of these values is less than -1 they should be removed.
  • Simulates the evolution of 1000 populations for each of the remaining above values of \(s\), using one of the *apply family of functions.
  • For the case of \(s=0\), a sentence should be printed out comparing the theoretical fraction of times the population 'fixed' (reached an allele-A fraction of 1) versus the observed fraction. The which command may be helpful here.
  • The script should create a plot of the fraction of times the populations 'fixed' , versus \(s\). Note that this will create a file called Rplots.pdf in your assignment directory when you run the script.
  • If the command-line argument is not numeric the script should exit with an appropriate error message. The as.numeric() and is.na() functions may be helpful here.  You may assume that the numeric argument is an integer.
  • If the value of the command-line argument does not make sense the script should exit with an appropriate error message.
  • If the number of command-line arguments is not 1 the script should exit with an error message.

Your script should output similarly to this:

$ Rscript Drift.Analysis.R 100
  For s = 0 the predicted fraction is 0.005 and the observed is 0.003.
$ 
Plot showing 'fixing' fraction versus selection coefficient for a population of 100.

Note that, starting with this assignment and for the rest of the semester, you will be expected to use coding best practices in all of the work that you submit. This includes, but is not limited to:

  • Plenty of comments in the code, describing what you have done.
  • Sensible variable names.
  • Explicitly returning values, if the function in question is returning a value.
  • Not using the print() function to return values.
  • Proper indentation of code blocks.
  • No use of global variables.
  • Using existing R functionality, when possible.
  • Creating modular code. Using functions.
  • Never copy-and-pasting code!

Because of the random nature of the simulation, your numbers will differ, but the story should be the same: neutral fixation probability equals the starting frequency; and the fixation probability of a mutation stays near its neutral value \(1/(2N)\) until the product \(2Ns\) grows past order 1, after which selection takes over.


Submit your scripts Drift.Utilities.R and Drift.Analysis.R.

Both R code files must be added and committed frequently to the repository. To capture the output of git log use redirection (git log > git.log, and hand in the git.log file). 

Assignments will be graded on a 10 point basis.

Due date: October 15, 11:59 p.m., with 0.5 point penalty per day for late submission until the cut-off date.

You are currently using guest access (Log in)
Data retention summary


All content on this website is made available under the Creative Commons Attribution 4.0 International licence, with the exception of all videos which are released under the Creative Commons Attribution-NoDerivatives 4.0 International licence.
Powered by Moodle