{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 352964 characters]",
      "text/plain": [
       "<Figure size 1000x800 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 10.1.1\n",
    "    \n",
    "a = np.logspace(-2, 2, 100)\n",
    "b = np.logspace(-2, 2, 100)\n",
    "A, B = np.meshgrid(a,b)\n",
    "\n",
    "def act_and(a,b):\n",
    "    return  (a*b/.001) / (1 + a + b + a*b/.001)\n",
    "\n",
    "def act_or(a,b):\n",
    "    return a/(1+a) + b/(1+b)\n",
    "\n",
    "def act_nand(a,b):\n",
    "    return (1) / (1 + a + b + a*b/.001)\n",
    "\n",
    "def act_xor(a,b):\n",
    "    return act_or(a,b) * act_nand(a,b)\n",
    "\n",
    "\n",
    "A_log = np.log10(A)\n",
    "B_log = np.log10(B)\n",
    "\n",
    "fig, axs = plt.subplots(2, 2, figsize=(10, 8), subplot_kw={'projection': '3d'})\n",
    "\n",
    "axs[0, 0].plot_surface(A_log, B_log, act_and(A,B))\n",
    "axs[0, 0].set_title(\"AND\")\n",
    "axs[0, 1].plot_surface(A_log, B_log, act_or(A,B))\n",
    "axs[0, 1].set_title(\"OR\")\n",
    "axs[1, 0].plot_surface(A_log, B_log, act_nand(A,B))\n",
    "axs[1, 0].set_title(\"NAND\")\n",
    "axs[1, 1].plot_surface(A_log, B_log, act_xor(A,B))\n",
    "axs[1, 1].set_title(\"XOR\")\n",
    "\n",
    "\n",
    "for ax in axs.flat:\n",
    "    ax.set_xlabel('log(A)')\n",
    "    ax.set_ylabel('log(B)')\n",
    "    ax.set_zlabel('activity')\n",
    "\n",
    "#plt.tight_layout()\n",
    "\n",
    "# Set title\n",
    "fig.suptitle('Combinatorial transcription logic')\n",
    "\n",
    "# Show the plot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22536 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 10.2.2\n",
    "\n",
    "E = np.linspace(0.5, 1.5, 100)\n",
    "F = 1\n",
    "KE = .1\n",
    "KF = .1\n",
    "dt = .01\n",
    "\n",
    "\n",
    "def EZ (Z, E, K):\n",
    "    return (Z + E + K) / 2 - np.sqrt((Z+E+K)**2 / 4 - Z*E)\n",
    "\n",
    "\n",
    "Zs = np.zeros_like(E)\n",
    "\n",
    "for i, e in enumerate(E):\n",
    "    Zold = 0\n",
    "    Z = 1\n",
    "\n",
    "    while np.abs(Z - Zold) > .0001:\n",
    "        Zold = Z\n",
    "        Z = Z + dt * (EZ(100-Z,F,KF) - EZ(Z,e,KE))\n",
    "    \n",
    "    Zs[i] = Z\n",
    "\n",
    "\n",
    "plt.title(\"Push-Pull-Signalling (strong binding)\")\n",
    "plt.xlabel(\"E\")\n",
    "plt.ylabel(\"Z\")\n",
    "plt.plot(E,Zs)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30724 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "E = np.linspace(0.5, 1.5, 100)\n",
    "F = 1\n",
    "KE = 10\n",
    "KF = 10\n",
    "dt = .01\n",
    "\n",
    "\n",
    "def EZ (Z, E, K):\n",
    "    return (Z + E + K) / 2 - np.sqrt((Z+E+K)**2 / 4 - Z*E)\n",
    "\n",
    "\n",
    "Zs = np.zeros_like(E)\n",
    "\n",
    "for i, e in enumerate(E):\n",
    "    Zold = 0\n",
    "    Z = 1\n",
    "\n",
    "    while np.abs(Z - Zold) > .0001:\n",
    "        Zold = Z\n",
    "        Z = Z + dt * (EZ(100-Z,F,KF) - EZ(Z,e,KE))\n",
    "    \n",
    "    Zs[i] = Z\n",
    "\n",
    "\n",
    "plt.title(\"Push-Pull-Signalling (weak binding)\")\n",
    "plt.xlabel(\"E\")\n",
    "plt.ylabel(\"Z\")\n",
    "plt.plot(E,Zs)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 34660 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 10.3.1\n",
    "\n",
    "dt = .005\n",
    "C = .5\n",
    "\n",
    "t = np.linspace(0,20,int(20 / dt))\n",
    "B = np.zeros_like(t)\n",
    "F = np.zeros_like(t)\n",
    "E = np.where(t < 10, 1, 2)\n",
    "\n",
    "F[0] = 1\n",
    "B[0] = E[0] - C\n",
    "for i in range (0, int(20 / dt) - 1):\n",
    "    B[i+1] = B[i] + dt * (F[i] * B[i] - B[i])\n",
    "    F[i+1] = F[i] + dt * (E[i] - F[i] * B[i] - C * F[i])\n",
    "\n",
    "plt.title(\"Intestine: Host suddenly eats twice as much\")\n",
    "plt.xlabel(\"t\")\n",
    "plt.plot(t, B, label=\"Bacteria\")\n",
    "plt.plot(t, F, label=\"Food\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
