{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Course**: BIO-341 [_Dynamical systems in Biology_](https://moodle.epfl.ch/course/info.php?id=14291)\n",
    "\n",
    "**Professor**: _Julian Shillcock_ & _Felix Naef_\n",
    "\n",
    "SSV, BA5, 2025\n",
    "\n",
    "Note that this document is primarily aimed at being consulted as a Jupyter notebook, the PDF rendering being not optimal."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#import important libraries\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from ipywidgets import interact\n",
    "from scipy.integrate import odeint\n",
    "from IPython.display import set_matplotlib_formats\n",
    "from matplotlib.markers import MarkerStyle\n",
    "set_matplotlib_formats('png', 'pdf')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The Allee effect"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The goal of this exercise is to understand the stability properties\n",
    "of 1-dimensional first order ODEs and to study a model of population growth.\n",
    "\n",
    "A fixed point $x^{*}$ of a 1-dimensional ODE $\\dot{x}=F(x)$ satisfies\n",
    "the condition $F(x^{*})=0$.\n",
    "\n",
    "\n",
    "* A fixed point $x^{*}$ is (linearly) $\\underline{stable}$ if $F'(x^{*})<0$.\n",
    "\n",
    "* A fixed point $x^{*}$ is (linearly) $\\underline{unstable}$ if $F'(x^{*})>0$.\n",
    "\n",
    "* A fixed point $x^{*}$ is (linearly) $\\underline{semi-stable}$ if $F'(x^{*})=0$ and $F''(x^{*})\\neq 0$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stability of fixed points in 1-dimensional systems (paper and pencil)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Analyze the 1-dimensional systems (a, b, c) below graphically:\n",
    "\n",
    "* Sketch the derivative $\\dot{x}=F(x)$ in function of $x$. \n",
    "* Mark all fixed points and determine their stability. When possible, calculate the fixed points explicitly. \n",
    "* Sketch the solutions $x(t)$ for two initial conditions leading to different qualitative behavior (for example one solution that flows to a fixed point and one that goes to infinity). \n",
    "\n",
    "(a) $\\dot{x}=2x-x^{2}$  \n",
    "(b) $\\dot{x}=\\cos(2x)+1-x$  \n",
    "(c) $\\dot{x}=x^{2}-8x+16$\n",
    "\n",
    "**Hint**: In the second equation, $F(x)$ is written as the difference of two functions, \\textit{i.e.} $F(x)=g(x)-h(x)$. In order to find the fixed point(s), plot $g(x)$ and $h(x)$ individually and look for intersections. You might want to use trigonometric identities."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Allee effect (paper and pencil)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The classical view of population dynamics states that due to competition\n",
    "\tfor resources, a population will experience a reduced growth rate\n",
    "\twhen the number of individuals is large. But this is not necessarily\n",
    "\tsufficient to describe the dynamics of some populations. Indeed, for\n",
    "\tcertain species, it has been observed that the growth rate of the population\n",
    "\tcan be null or even negative for a low number of individuals. This\n",
    "\tis called the Allee effect and can be explained, for instance, by\n",
    "\tthe fact that it is hard to find mates when the population density\n",
    "\tis too low. The effect leads to distinct dynamics for\n",
    "\tthe population size.\n",
    "\t\n",
    "\n",
    "Let us consider the model with $a,b>0,\\,r>0$, and the number of individuals $N(t)\\geq0$:\n",
    "\n",
    "\\begin{equation}\n",
    "\\dot{N}=rN-\\frac N a(N-b)^{2}\n",
    "\\end{equation}\n",
    "\t\n",
    "As $N$ can be factorized in this equation, it is convenient to study\n",
    "\tthe *relative growth rate* $\\dot{N}/N$. In order to get an Allee\n",
    "\teffect, **1)** the relative growth rate needs to reach a maximum\n",
    "\tat an intermediate population size and **2)** it has to be negative\n",
    "\tat small population sizes."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1) Sketch the relative growth rate. \n",
    "- Which kind of function is it?\n",
    "- What is the maximum relative growth rate and for which number of individuals it is realised?\n",
    "- For which values of the parameters $a,b,r$ we have an Allee effect as described above?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2) Study the qualitative behavior of the solution using the  procedure:\n",
    "\n",
    "* Sketch $\\frac{\\dot{N}}{N}$ in function of $N$ and the one-dimensional vector field on the $N$-axis. Which are the fixed points of this equation? Are they stable or unstable?\n",
    "* Now, let's go back to the full equation (1). Represent $\\dot{N}$ in function of $N$ for different conditions satisfying or not the Allee effect.\n",
    "* Discuss the existence and stability of the fixed points, and how it relates to the presence/absence of the Allee effect.\n",
    "* Discuss how the number of fixed points changes as a function of $a$. Plot the fixed points as a function of $a$. Which kind of trajectories do we have in the $N-t$ plane depending on whether $a$ is smaller or bigger than $b^2/r$?\n",
    "* Can you think of other causes that would lead to an Allee Effect ?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Formation of a dimer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The formation of a dimer $C$ from its subunits $A$ and $B$ can be described by the following reversible chemical reaction :\n",
    "\n",
    "$$\n",
    "A_{F}+B_{F}\\overset{k_1}{\\longrightarrow}C\n",
    "$$\n",
    "\n",
    "$$\n",
    "C\\overset{k_{-1}}{\\longrightarrow}A_{F}+B_{F}\n",
    "$$\n",
    "\t\n",
    "The total concentrations of the proteins $A$ and $B$ are constant ; they are denoted by $A_{T}$ and $B_{T}$ , and by $A_{F}$ and $B_{F}$ for the concentrations of the free (non-bound) forms.\n",
    "\n",
    "* Express the conservation of $A_{T}$ in function of $A_{F}$ and $C$.\n",
    "* Write the equation describing the temporal evolution of the concentration of the complex $C$ by using the total concentrations of $A_{T}$ and $B_{T}$ and the kinetic constants $k_{1}$ and $k_{-1}$.\n",
    "* Describe the type of this dynamical system (order, linearity, etc.).\n",
    "* Calculate the **equilibrium** concentration of the complex, in other words what is the value of the **stable** fixed point for $C$. Express the result in function of $A_{T}$ and $B_{T}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scatchard plots"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Scatchard plots** were historically used in molecular biology to estimate the affinity (equilibrium constant) of DNA-binding proteins for DNA. Imagine that $A$ is your protein of interest and $B$ the amount of DNA that you can vary over a  broad range. Assume that you can measure the steady state amount (the values at the fixed point) of DNA bound to the protein (which is equivalent to the complex $C$ in the above reaction), and the amount of free (unbound) DNA.  \n",
    " **Note**: in practice this can be done using electrophoretic mobility shift assays (EMSA) but you do not need to know such techniques to solve the problem here.  \n",
    " \n",
    "* Show that one can determine the dissociation constant $K_{d}=k_{-1}/k_{1}$ by linear regression, _i.e._ show that there is a linear relationship between the ratio $C/B_{F}$ and the bound fraction $C$.\n",
    "* Set the necessary parameters and use Python to do a Scatchard plot\n",
    "    * Start by plottig the linear relation between $C/B_{F}$ and $C$.\n",
    "    * Add labels to your plot \n",
    "    * Add lines for the axis\n",
    "    * Write what you are plotting"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  },
  "title": "Exercise 2: The Allee effect"
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
