{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider a traffic model of ribosomes on RNA. The length of the RNA is 1000 steps\n",
    "\n",
    "and 100 randomly assigned steps are slow-moving steps, where the speed to move is 1/10 of typical speed.\n",
    "\n",
    "Feed in ribosomes at one end with some frequency, say 1/2 of the time it takes to move one step. but never allowing a ribosome to occupy more than one step.\n",
    "\n",
    " In practice move ribosomes stochastically, with a probability of 0.5 at a typical step and 0.05 at a slow step.\n",
    "\n",
    "Investigate the times it takes a ribosome to reach the end with and without the rule that there can only be one ribosome at each step. \n",
    "\n",
    "Change the frequency of adding a new ribosome at the start site by a factor of 2 up or down.\n",
    "\n",
    "It is illustrative to make a space-time plot with a dot for each ribosome along the x-axis, and the y-axis representing subsequent timesteps."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7f79d24d8310>]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 35956 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from random import random\n",
    "\n",
    "def decide (prob):\n",
    "    return True if random() <= prob else False\n",
    "\n",
    "ribosomes = np.zeros(1000)\n",
    "is_slow_step = np.zeros(1000)\n",
    "starts = np.zeros(10000)\n",
    "finishes = np.zeros(10000)\n",
    "\n",
    "def move_ribosome(i, global_step):\n",
    "    if i < 999:\n",
    "        ribosomes[i+1] += 1\n",
    "    else: \n",
    "        finishes[global_step] += 1\n",
    "    ribosomes[i] -= 1\n",
    "\n",
    "random_indices = np.random.choice(1000, 100, replace=False)\n",
    "is_slow_step[random_indices] = 1\n",
    "\n",
    "for step in range(0,10000):\n",
    "    for i in range(0,1000):\n",
    "        i = 1000 - i - 1\n",
    "        x = int(ribosomes[i])\n",
    "        for j in range(0, x):\n",
    "            if is_slow_step[i]:\n",
    "                if decide(.05):\n",
    "                    move_ribosome(i, step)\n",
    "            else:\n",
    "                if decide(.5):\n",
    "                    move_ribosome(i, step)\n",
    "    if decide(.5):\n",
    "        ribosomes[0] += 1\n",
    "        starts[step] += 1\n",
    "\n",
    "plt.title(\"Multiple ribosomes per step\")\n",
    "plt.plot(np.cumsum(starts))\n",
    "plt.plot(np.cumsum(finishes))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7f79d244b850>]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 31444 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from random import random\n",
    "\n",
    "def decide (prob):\n",
    "    return True if random() <= prob else False\n",
    "\n",
    "ribosomes = np.zeros(1000)\n",
    "is_slow_step = np.zeros(1000)\n",
    "starts = np.zeros(10000)\n",
    "finishes = np.zeros(10000)\n",
    "\n",
    "def move_ribosome(i, global_step):\n",
    "    if i < 999:\n",
    "        if ribosomes[i+1] < 1:\n",
    "            ribosomes[i+1] += 1\n",
    "            ribosomes[i] -= 1\n",
    "    else: \n",
    "        finishes[global_step] += 1\n",
    "        ribosomes[i] -= 1\n",
    "\n",
    "random_indices = np.random.choice(1000, 100, replace=False)\n",
    "is_slow_step[random_indices] = 1\n",
    "\n",
    "for step in range(0,10000):\n",
    "    for i in range(0,1000):\n",
    "        i = 1000 - i - 1\n",
    "        x = int(ribosomes[i])\n",
    "        for j in range(0, x):\n",
    "            if is_slow_step[i]:\n",
    "                if decide(.05):\n",
    "                    move_ribosome(i, step)\n",
    "            else:\n",
    "                if decide(.5):\n",
    "                    move_ribosome(i, step)\n",
    "    if decide(.5):\n",
    "        ribosomes[0] += 1\n",
    "        starts[step] += 1\n",
    "\n",
    "plt.title(\"Single ribosome per step\")\n",
    "plt.plot(np.cumsum(starts))\n",
    "plt.plot(np.cumsum(finishes))\n"
   ]
  }
 ],
 "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
}
