---
title: Evolutionary Dynamics - Practice
output:
  html_document:
    theme: null
    highlight: null
    css: ../styles/handelshiny.css
    fig_caption: true
    mathjax: default 
    keep_md: false
    includes:
      #in_header: in_header.txt
      before_body: ../styles/before_shiny_body.txt
      after_body: ../styles/after_shiny_body.txt 
bibliography: ../media/references.bib
---

##Overview {#shinytab1}
For this module, we will explore a stochastic SIR-type model with 2 different pathogen strains, wild-type and a drug resistant mutant in the presence of drug treatment. Read about the model in the "Model" tab. Then do the tasks described in the "What to do" tab.


##The Model {#shinytab2}

###Model Overview

This model tracks susceptibles, wild-type infected untreated, wild-type infected treated, drug resistant infected and recovered hosts. 
The following compartments are included:  

* **S** - uninfected and susceptible individuals 
* **I~u~** - individuals who are infected with the wild-type/drug sensitive strain and not on treatment.
* **I~t~** - individuals who are infected with the wild-type/drug sensitive strain and on treatment.
* **I~r~** - individuals who are infected with the drug resistant strain. Treatment has no impact on those hosts.
* **R** - recovered/removed individuals. Those individuals have recovered and are immune. 


The included processes/mechanisms are the following:

* Susceptible individuals (S) can become infected by untreated or treated hosts infected with the wild-type strain at rates _b~u~_ and _b~t~_. A fraction _f_ of those hosts is assumed to receive treatment, the remainder do not. Small fractions (_c~u~_ and _c~t~_) of wild-type infected untreated or treated hosts can generate resistant mutants and transmit those, leading to resistant infections.  
* Susceptible individuals (S) can become infected by hosts infected with the resistant strain at rate _b~r~_. 
* All infected hosts recover at some rate _g~i~_, with _i=u,t,r_. 
* Recovered individuals are immune to reinfection. 


###Model Implementation
The flow diagram for the model implemented in this app is:

![Flow diagram for this model.](../media/evolutionmodel.png)


Note that this model is _not_ an ordinary differential equation model. It is instead its stochastic equivalent.
We can specify the model by writing down every possible transition/event/reaction that can occur and their propensities (the propensity multiplied with the
time step gives the probability that a given event/transition occurs). For our model these are the following:

Event type              |  Transitions               |   Propensity      |
----------              |  -----------               |   ----------      |
S turn into I~u~        |  S => S-1,  I~u~ => I~u~+1 |   (1-f) * (b~u~ * (1-c~u~) * I~u~ + b~t~ * (1-c~t~) * I~t~) * S  |
S turn into I~t~        |  S => S-1,  I~t~ => I~t~+1 |   f * (b~u~ * (1-c~u~) * I~u~ + b~t~ * (1-c~t~) * I~t~) * S  |
S turn into I~r~        |  S => S-1,  I~r~ => I~r~+1 |   (b~u~ * c~u~ * I~u~ + b~t~ * c~t~ * I~t~ + b~r~ * I~r~) * S  |
Recovery of I~u~        |  I~u~ => I~u~-1, R => R+1  |   g~u~ * I~u~       |
Recovery of I~t~        |  I~t~ => I~t~-1, R => R+1  |   g~t~ * I~t~       |
Recovery of I~r~        |  I~r~ => I~r~-1, R => R+1  |   g~r~ * I~r~       |




##What to do {#shinytab3}

__All parameters described below and in the model are assumed to be in units of (inverse) days__
      
###Task 1: 
* Set the model parameters such that it corresponds to the following setting: 
    * 500 susceptible and 1 initially untreated infected host. No other infected hosts.
    * Simulation duration approximately half a year.
    * Assume that  untreated individuals transmit at rate 0.001, treated at rate 0.0005 and resistant at rate 0.0008. 
    * Assume that the duration of the infectious period for untreated is 5 days long, 4 days for treated and 5 days for resistant. 
    * Set number of simulations to 20.
    
* With parameters set to correspond to the scenario just described, run the simulation.
* You should see some simulations with outbreaks and some without. For those with outbreaks, you should have around 10-100 susceptible left at the end.

###Task 2: 
* With everything as before, set the initial number of untreated infected hosts to 10. 
* Run simulations. You should pretty much always get outbreaks. Record the average number of susceptibles left.


###Task 3: 
* Now turn on treatment. With settings as above, run the simulation with fraction receiving treatment at 0, 0.25, 0.5, 0.75 and 1.
* Observe what happens to the susceptibles at the end of the outbreak as you change treatment. Draw conclusions about the usefulness of treatment.

###Task 4: 
* Now allow resistance to be generated during treatment, with the fraction of resistant generation set to 0.2.
* Set treatment to 0. Contemplate what you expect to see. Run simulations to check.
* Now turn on treatment. With settings as above, run the simulation with fraction receiving treatment at 0.25, 0.5, 0.75 and 1.

###Task 5: 
* Repeat task 3 with number of simulations at 50. Record the average number of susceptibles at the end of each of the 5 treatment levels. 
* Set rate of transmission of resistant hosts to the same as that for untreated hosts, 0.001 and turn resistance generation on to 0.3.
* Repeat running 50 simulations at a time (you can repeat a few times to get a better idea about random variation). Record the average number of susceptibles at the end of each of the 5 treatment levels. 
* In your head or on a piece of paper, sketch out the relation between treatment level and number of susceptibles left at the end in the absence and presence of resistance evolution. What do you conclude from that?

###Task 6: 
* Keep exploring. 
* Turn on resistance generation by treated and untreated. 
* Explore how population size, rate of resistance generation and fitness of the different strains affect outcomes.



##Further Information {#shinytab4}

* The model is a simpler version of a previously published model in [@handel09a]. 
* For this app, the underlying function running the simulation is called `simulate_evolution.R`. You can call this function directly, without going through the shiny app. Check the help file for the function for more information. 
* If you call the function directly, you need to process the results returned from this function and produce useful output (such as a plot) yourself. 
* You can also modify this R function for your own purposes. To find out how to do that, type `?DSAIDE` at the R console.

###References




