{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8718382c-245b-472d-9346-560baf8ea4c9",
   "metadata": {},
   "source": [
    "# Reaction Inheritance\n",
    "\n",
    "In MobsPy, meta-species can be used to create new meta-species, and the new meta-species receive all reactions defined for the ones used to make them. \n",
    "This concept is called reaction inheritance. \n",
    "\n",
    "We present an example of inheritance starting by defining several BaseSpecies:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "115322c4-e3a8-4a0b-9d38-8d0d26c4c79b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from mobspy import *\n",
    "\n",
    "Duplicator, Mortal, Eater, Food = BaseSpecies()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd283d31-7e59-4f4c-84e4-bb379fcbcb6a",
   "metadata": {},
   "source": [
    "Next, we define reactions for the BaseSpecies.\n",
    "Furthermore, Zero, a meta-species that appears in these reactions, is a meta-species made available by the MobsPy to represent nothing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f9687e7a-abc5-4fde-a61c-aa8b6a2e8181",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mobspy.modules.meta_class.Reactions at 0x7fc0ef2a1610>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Duplicator >> 2*Duplicator [1]\n",
    "Eater + Food >> Eater [1]\n",
    "Mortal >> Zero [1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "857582f8-7b9c-4ae2-b7e8-ef4c83f3af0a",
   "metadata": {},
   "source": [
    "There are two ways of creating new meta-species with other meta-species. \n",
    "The first one is to multiple meta-species.\n",
    "The product of this multiplication is a new meta-species that inherits from all the factors in this multiplication. \n",
    "Below, we show an example where we define Bacteria, a species that is also a Duplicator, an Eater, and a Mortal:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "43d91f12-5c95-4789-b10d-ef88c0d02f74",
   "metadata": {},
   "outputs": [],
   "source": [
    "Bacteria = Duplicator*Eater*Mortal"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2da3dcdc-c434-457a-8c26-3d664c9969cc",
   "metadata": {},
   "source": [
    "We now create new different types of bacteria from the previous ones, Ecoli and Strepto. As they will also be Bacteria, they inherit all the reactions. We can do this using the New call. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2fc1888a-bbd9-431f-95fd-34b62f4865dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "Ecoli, Strepto = New(Bacteria)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4ac10e7-ccbf-4d3c-b02a-1966af2fed5d",
   "metadata": {},
   "source": [
    "We also define two types of Food:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "30df3ac6-f019-43f0-87ae-c50beab9b0da",
   "metadata": {},
   "outputs": [],
   "source": [
    "Glucose, Amino = New(Food)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d291352-6fee-4d98-9c39-b469df6d3042",
   "metadata": {},
   "source": [
    "MobsPy will create all possible combinations for inheritance when defining the model, which means that both Ecoli and Strepto will eat Glucose and Amino.\n",
    "\n",
    "Finally, we define the species we want to simulate.\n",
    "Since Duplicator, Eater, Mortal, and Food were just defined to construct the other meta-species, we don't pass them to Simulation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5f0c9765-b7d0-4e26-98ed-8660fcd74f26",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Adding some counts\n",
    "Ecoli(50), Strepto(25), Glucose(10), Amino(100)\n",
    "\n",
    "S = Simulation(Ecoli | Strepto | Glucose | Amino)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2709519d-a951-4995-a3a3-d048f2b4a6c8",
   "metadata": {},
   "source": [
    "Now, we compile the simulation to show all defined reactions. \n",
    "The meta-reactions are stored in the meta-species themselves. Once the meta-species are passed to the Simulation constructor, it compiles all meta-reactions in all participating meta-species to get all the model reactions.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "98b7da18-0d3e-4e75-8e27-893e99c91a5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling model\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Species\n",
      "Amino,100\n",
      "Ecoli,50\n",
      "Glucose,10\n",
      "Strepto,25\n",
      "\n",
      "Mappings\n",
      "Amino :\n",
      "Amino\n",
      "Ecoli :\n",
      "Ecoli\n",
      "Glucose :\n",
      "Glucose\n",
      "Strepto :\n",
      "Strepto\n",
      "\n",
      "Parameters\n",
      "volume,1\n",
      "\n",
      "Reactions\n",
      "reaction_0,{'re': [(1, 'Ecoli'), (1, 'Amino')], 'pr': [(1, 'Ecoli')], 'kin': 'Ecoli * Amino * 1 * volume^-1'}\n",
      "reaction_1,{'re': [(1, 'Ecoli'), (1, 'Glucose')], 'pr': [(1, 'Ecoli')], 'kin': 'Ecoli * Glucose * 1 * volume^-1'}\n",
      "reaction_2,{'re': [(1, 'Ecoli')], 'pr': [(2, 'Ecoli')], 'kin': 'Ecoli * 1'}\n",
      "reaction_3,{'re': [(1, 'Ecoli')], 'pr': [], 'kin': 'Ecoli * 1'}\n",
      "reaction_4,{'re': [(1, 'Strepto'), (1, 'Amino')], 'pr': [(1, 'Strepto')], 'kin': 'Strepto * Amino * 1 * volume^-1'}\n",
      "reaction_5,{'re': [(1, 'Strepto'), (1, 'Glucose')], 'pr': [(1, 'Strepto')], 'kin': 'Strepto * Glucose * 1 * volume^-1'}\n",
      "reaction_6,{'re': [(1, 'Strepto')], 'pr': [(2, 'Strepto')], 'kin': 'Strepto * 1'}\n",
      "reaction_7,{'re': [(1, 'Strepto')], 'pr': [], 'kin': 'Strepto * 1'}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(S.compile())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24452c56-6dd9-47d8-98f7-df00cb895273",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
