{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## MMinte with your own models\n",
    "\n",
    "This notebook shows how to perform an interaction analysis using a set of single species models that you provide. A set of four models for this tutorial is in the test data included with the mminte package. The models are a subset of the species analyzed in [Anoxic Conditions Promote Species-Specific Mutualism between Gut Microbes In Silico](http://aem.asm.org/content/81/12/4049.full) and include the following:\n",
    "\n",
    "* BT.sbml is a model for Bacteroides thetaiotaomicron\n",
    "* FP.sbml is a model for Faecalibacterium prausnitzii\n",
    "* HP.sbml is a model for Helicobacter pylori\n",
    "* KP.sbml is a model for Klebsiella pneumoniae\n",
    "\n",
    "### Getting started\n",
    "\n",
    "Import the mminte package and a few other packages needed for this tutorial."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import mminte\n",
    "from os.path import expanduser, join\n",
    "from os import makedirs\n",
    "import pkg_resources\n",
    "import json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "The single species models are provided in the test data folder in the mminte package. Each MMinte widget either creates output files or returns data that is used as input to the next widget. You need to select a base folder for storing the output files and separate folders for the single species and pair model files. Change the value assigned to `analysis_folder` if you want the output files stored somewhere else."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "source_folder = pkg_resources.resource_filename('mminte', 'test/data')\n",
    "analysis_folder = join(expanduser('~'), 'mminte_interactions')\n",
    "pair_model_folder = join(analysis_folder, 'pair_models')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Run the cell below to create the output folders. You only need to run this cell if the folder does not exist."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "makedirs(analysis_folder)\n",
    "makedirs(pair_model_folder)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When you provide your own models, you can skip the first three widgets and start the analysis with Widget 4."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Widget 4 - Create two species community models\n",
    "\n",
    "In Widget 4, you create two species community models from single species models using the `create_interaction_models()` function. The first input parameter is a list of tuples with two elements, the paths to the single species model files in each pair. Since the microbial community is small, select all possible pairs for the analysis. The output is a list of paths to the six pairs of two species community model files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "model_filenames = [join(source_folder, name) for name in ['BT.sbml', 'FP.sbml', 'HP.sbml', 'KP.sbml']]\n",
    "pairs = mminte.get_all_pairs(model_filenames)\n",
    "pair_model_filenames = mminte.create_interaction_models(pairs, analysis_folder)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For reference, save the list of paths to the two species community models to a file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open(join(analysis_folder, 'pair_model_filenames.txt'), 'w') as handle:\n",
    "    handle.write('\\n'.join(pair_model_filenames)+'\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Widget 5 - Calculate growth rates and evaluate interactions\n",
    "\n",
    "In Widget 5, you calculate growth rates and evaluate interactions for the pairs in the microbial community using the `calculate_growth_rates()` function (more details are provided in \"Widget 4\" of the tutorial notebook).\n",
    "\n",
    "The first input parameter is the list of paths to two species community model files that were created in Widget 4. The second input parameter is a dictionary that defines the medium (or nutritient conditions) that the community is growing in. The medium dictionary is keyed by exchange reaction ID with the uptake rate as the value. The western diet medium used in the paper is in the test data included with the mminte package."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "western_diet = json.load(open(pkg_resources.resource_filename('mminte', 'test/data/western.json')))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "The output is a data frame with details on the growth rates of the species in each pair and the type of interaction."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "growth_rates = mminte.calculate_growth_rates(pair_model_filenames, western_diet)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Each row in the growth_rates data frame, details the interaction between a pair in the microbial community and identifies the type of interaction. This should match paper results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A_ID</th>\n",
       "      <th>B_ID</th>\n",
       "      <th>TYPE</th>\n",
       "      <th>TOGETHER</th>\n",
       "      <th>A_TOGETHER</th>\n",
       "      <th>B_TOGETHER</th>\n",
       "      <th>A_ALONE</th>\n",
       "      <th>B_ALONE</th>\n",
       "      <th>A_CHANGE</th>\n",
       "      <th>B_CHANGE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>BT</td>\n",
       "      <td>FP</td>\n",
       "      <td>Parasitism</td>\n",
       "      <td>0.495075</td>\n",
       "      <td>0.277463</td>\n",
       "      <td>0.217612</td>\n",
       "      <td>0.440738</td>\n",
       "      <td>0.169338</td>\n",
       "      <td>-0.370460</td>\n",
       "      <td>0.285078</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>BT</td>\n",
       "      <td>HP</td>\n",
       "      <td>Parasitism</td>\n",
       "      <td>0.500853</td>\n",
       "      <td>0.036457</td>\n",
       "      <td>0.464396</td>\n",
       "      <td>0.440738</td>\n",
       "      <td>0.197557</td>\n",
       "      <td>-0.917283</td>\n",
       "      <td>1.350692</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>BT</td>\n",
       "      <td>KP</td>\n",
       "      <td>Parasitism</td>\n",
       "      <td>0.586633</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.586633</td>\n",
       "      <td>0.440738</td>\n",
       "      <td>0.510884</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>0.148269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>FP</td>\n",
       "      <td>HP</td>\n",
       "      <td>Commensalism</td>\n",
       "      <td>0.431667</td>\n",
       "      <td>0.177563</td>\n",
       "      <td>0.254104</td>\n",
       "      <td>0.169338</td>\n",
       "      <td>0.197557</td>\n",
       "      <td>0.048574</td>\n",
       "      <td>0.286228</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>FP</td>\n",
       "      <td>KP</td>\n",
       "      <td>Amensalism</td>\n",
       "      <td>0.545572</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.545572</td>\n",
       "      <td>0.169338</td>\n",
       "      <td>0.510884</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>0.067898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>HP</td>\n",
       "      <td>KP</td>\n",
       "      <td>Amensalism</td>\n",
       "      <td>0.540178</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.540178</td>\n",
       "      <td>0.197557</td>\n",
       "      <td>0.510884</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>0.057339</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  A_ID B_ID          TYPE  TOGETHER  A_TOGETHER  B_TOGETHER   A_ALONE  \\\n",
       "0   BT   FP    Parasitism  0.495075    0.277463    0.217612  0.440738   \n",
       "1   BT   HP    Parasitism  0.500853    0.036457    0.464396  0.440738   \n",
       "2   BT   KP    Parasitism  0.586633    0.000000    0.586633  0.440738   \n",
       "3   FP   HP  Commensalism  0.431667    0.177563    0.254104  0.169338   \n",
       "4   FP   KP    Amensalism  0.545572    0.000000    0.545572  0.169338   \n",
       "5   HP   KP    Amensalism  0.540178    0.000000    0.540178  0.197557   \n",
       "\n",
       "    B_ALONE  A_CHANGE  B_CHANGE  \n",
       "0  0.169338 -0.370460  0.285078  \n",
       "1  0.197557 -0.917283  1.350692  \n",
       "2  0.510884 -1.000000  0.148269  \n",
       "3  0.197557  0.048574  0.286228  \n",
       "4  0.510884 -1.000000  0.067898  \n",
       "5  0.510884 -1.000000  0.057339  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "growth_rates"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For reference, save the growth rates data frame to a file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "mminte.write_growth_rates_file(growth_rates, join(analysis_folder, 'growth_rates.csv'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Widget 6 - Visualize interactions in the community\n",
    "\n",
    "In Widget 6, you visualize the interactions between pairs in the microbial community by creating a graph that represents the interaction network using make_graph(). The input is the growth rates data frame created in Widget 5.\n",
    "\n",
    "The output is a networkx Graph object where the nodes represent the different OTUs and the edges represent the interaction between OTUs in a pair. The color of an edge indicates the kind of interaction predicted between the OTUs in a pair. A red edge indicates a negative interaction, a green edge indcates a positive interaction, and a gray edge indicates no interaction. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "graph = mminte.make_graph(growth_rates)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You plot the graph using plot_graph() which opens a new window with the visualization. The default is a circular layout. If you want a different layout, you can use any of the plotting functions available in the networkx package."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using matplotlib backend: MacOSX\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/m097749/Envs/mminte-py27/lib/python2.7/site-packages/networkx/drawing/nx_pylab.py:126 \u001b[1;31mMatplotlibDeprecationWarning\u001b[0m: pyplot.hold is deprecated.\n",
      "    Future behavior will be consistent with the long-time default:\n",
      "    plot commands add elements without first clearing the\n",
      "    Axes and/or Figure.\n",
      "/Users/m097749/Envs/mminte-py27/lib/python2.7/site-packages/networkx/drawing/nx_pylab.py:138 \u001b[1;31mMatplotlibDeprecationWarning\u001b[0m: pyplot.hold is deprecated.\n",
      "    Future behavior will be consistent with the long-time default:\n",
      "    plot commands add elements without first clearing the\n",
      "    Axes and/or Figure.\n",
      "/Users/m097749/Envs/mminte-py27/lib/python2.7/site-packages/matplotlib/__init__.py:917 \u001b[1;31mUserWarning\u001b[0m: axes.hold is deprecated. Please remove it from your matplotlibrc and/or style files.\n",
      "/Users/m097749/Envs/mminte-py27/lib/python2.7/site-packages/matplotlib/rcsetup.py:152 \u001b[1;31mUserWarning\u001b[0m: axes.hold is deprecated, will be removed in 3.0\n"
     ]
    }
   ],
   "source": [
    "%matplotlib\n",
    "mminte.plot_graph(graph)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "MMinte_Python27",
   "language": "python",
   "name": "mminte_python27"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
