{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Microbial Metabolic Interactions Tutorial\n",
    "\n",
    "**MMinte** (pronounced /‘minti/) is a set of widgets that allows you to explore the pairwise interactions (positive or negative) that occur in a microbial community. From an association network and 16S rDNA sequence data, **MMinte** identifies corresponding genomes, reconstructs metabolic models, estimates growth under specific metabolic conditions, analyzes pairwise interactions, assigns interaction types to network links, and generates the corresponding network of interactions.\n",
    "\n",
    "This notebook shows how to run all six widgets in an integrated pipeline from start to finish. See the Just Interactions notebook for a tutorial on how to run specific steps in the pipeline when you already have metabolic models for the organisms in a microbial community. \n",
    "\n",
    "### Getting started\n",
    "\n",
    "**MMinte** can reconstruct ModelSEED models for the species in the microbial community using Widget 3. The [ModelSEED service](http://modelseed.org) uses genomes available in the [Pathosystems Resource Integration Center](https://www.patricbrc.org/portal/portal/patric/Home) (PATRIC) and requires you to authenticate as a [registered PATRIC user](http://enews.patricbrc.org/faqs/workspace-faqs/registration-faqs/). If needed, complete a [new user registration](https://user.patricbrc.org/register/). Before you use Widget 3 you must get an authentication token. The [Mackinac](https://github.com/mmundy42/mackinac) `get_token()` function stores the authentication token in the `.patric_config` file in your home directory. You can use the token until it expires.\n",
    "\n",
    "In the cell below, change `username` to your PATRIC username and enter your password when prompted. The returned user ID identifies your ModelSEED workspace."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "patric password: ········\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "u'mmundy@patricbrc.org'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mackinac import get_token\n",
    "get_token('username')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Now import the mminte package and a few other packages needed for this tutorial."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import mminte\n",
    "from os.path import expanduser, join\n",
    "from os import makedirs\n",
    "import pkg_resources"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "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. Since there can be a large number of model files, it helps to store them in a separate folders. Change the value assigned to `analysis_folder` if you want the output files stored somewhere else."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "analysis_folder = join(expanduser('~'), 'mminte_tutorial')\n",
    "single_model_folder = join(analysis_folder, 'single_models')\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 folders. You only need to run this cell if the folders do not exist."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "makedirs(analysis_folder)\n",
    "makedirs(single_model_folder)\n",
    "makedirs(pair_model_folder)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Widget 1 - Get sequences for representative OTUs\n",
    "\n",
    "In Widget 1, you get the 16S rDNA sequences for the representative OTUs in the microbial community using the `get_unique_otu_sequences()` function. The first input parameter is a list of tuples that has three elements: (1) first OTU number, (2) second OTU number, and (3) correlation value between -1 and 1. You need to provide this data based on your own analysis of the microbial community.\n",
    "\n",
    "A correlation file for this tutorial is in the test data included with the mminte package. Each line of the correlation file has three columns: (1) first OTU number, (2) second OTU number, and (3) correlation value. For example:\n",
    "\n",
    "    18\t310\t0.98\n",
    "    18\t401\t1\n",
    "    18\t405\t0.92\n",
    "    37\t72\t0.96\t\n",
    "    37\t74\t1\n",
    "    37\t632\t-0.2\n",
    "\n",
    "Each column in the correlation file is separated by a tab and the first line of the line is a header that is ignored. The correlation file is used again in Widget 6 to visualize the results.\n",
    "\n",
    "Change the value assigned to `correlation_filename` if you want to use your own correlation file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "correlation_filename = pkg_resources.resource_filename('mminte', 'test/data/correlation.txt')\n",
    "correlations = mminte.read_correlation_file(correlation_filename)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "The second input parameter is the path to a fasta file that has the 16S rDNA sequences for all of the OTUs in the microbial community. The record ID in each fasta record must be an OTU number. All of the OTUs in the correlation file must have a record in the fasta file.\n",
    "\n",
    "Typically you get the fasta file as output from 16S rDNA sequencing of the microbial community. A fasta file for this tutorial is in the test data included with the mminte package. The data in the fasta file comes from the Human Microbiome Project. Change the value assigned to `all_otus_filename` if you want to use your own sequence file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "all_otus_filename = pkg_resources.resource_filename('mminte', 'test/data/all_otus.fasta')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "The third input parameter is the path to a fasta file where the 16S rDNA sequences for the unique representative OTUs are stored."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "unique_otus_filename = join(analysis_folder, 'unique_otus.fasta')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "The output is the number of unique OTUs that are found in the input files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mminte.get_unique_otu_sequences(correlations, all_otus_filename, unique_otus_filename)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Widget 2 - Search for closest matching bacterial species\n",
    "\n",
    "In Widget 2, you use the unique representative OTU sequences to find the closest matching bacterial species with a whole genome sequence by running blast using the `search()` function. The first input parameter is the path to a fasta file with the 16S rDNA sequences for the unique OTUs that was created in Widget 1. The second input parameter is the path to the blast output file. *Talk about how database was generated*\n",
    "\n",
    "The two outputs are (1) a list of the unique NCBI genome IDs and (2) a data frame with the percent similarity between each representative OTUs and the closest match, identified by the NCBI genome ID."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "genome_ids, similarity = mminte.search(unique_otus_filename, join(analysis_folder, 'blast.txt'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Each row in the `similarity` data frame has three columns: (1) OTU ID, (2) NCBI genome ID, and (3) percent similarity of the organism represented by the OTU number to the organism with the NCBI genome ID. For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "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>OTU_ID</th>\n",
       "      <th>GENOME_ID</th>\n",
       "      <th>SIMILARITY</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>18</td>\n",
       "      <td>657315.3</td>\n",
       "      <td>100.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>37</td>\n",
       "      <td>484018.6</td>\n",
       "      <td>98.33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>72</td>\n",
       "      <td>1235835.3</td>\n",
       "      <td>25.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>74</td>\n",
       "      <td>742823.3</td>\n",
       "      <td>100.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>109</td>\n",
       "      <td>1414720.3</td>\n",
       "      <td>25.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>112</td>\n",
       "      <td>1156417.3</td>\n",
       "      <td>90.64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>150</td>\n",
       "      <td>180332.3</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>221</td>\n",
       "      <td>717962.3</td>\n",
       "      <td>99.16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>243</td>\n",
       "      <td>1297617.3</td>\n",
       "      <td>91.85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>253</td>\n",
       "      <td>1367212.3</td>\n",
       "      <td>97.24</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  OTU_ID  GENOME_ID  SIMILARITY\n",
       "0     18   657315.3      100.00\n",
       "1     37   484018.6       98.33\n",
       "2     72  1235835.3       25.00\n",
       "3     74   742823.3      100.00\n",
       "4    109  1414720.3       25.22\n",
       "5    112  1156417.3       90.64\n",
       "6    150   180332.3        0.00\n",
       "7    221   717962.3       99.16\n",
       "8    243  1297617.3       91.85\n",
       "9    253  1367212.3       97.24"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "similarity[0:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "For reference, save the genome IDs and similarity data frame to files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "with open(join(analysis_folder, 'genome_ids.txt'), 'w') as handle:\n",
    "    handle.write('\\n'.join(genome_ids)+'\\n')\n",
    "mminte.write_similarity_file(similarity, join(analysis_folder, 'similarity.csv'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### Widget 3 - Reconstruct and download metabolic models from ModelSEED\n",
    "\n",
    "In Widget 3, you reconstruct and gap fill metabolic models for the unique species in the microbial community with the [ModelSEED service](http://modelseed.org) using the `create_species_models()` function. The first input parameter is the list of genome IDs that was created in Widget 2. The second input parameter is the path to a folder for storing the single species model files (created in \"Getting Started\").\n",
    "\n",
    "The output is a list of paths to the downloaded single species model files. The `create_species_models()` function can run for a long time if all of the models need to be reconstructed and gap filled. By default, `create_species_models()` uses a previously downloaded model or a previously reconstructed model available in your ModelSEED workspace. You can force the models to be reconstructed using the `replace=True` parameter.\n",
    "\n",
    "The output is a list of paths to the downloaded single species model files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "single_model_filenames = mminte.create_species_models(genome_ids, single_model_folder)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "For reference, save the list of paths to single species models to a file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "with open(join(analysis_folder, 'single_model_filenames.txt'), 'w') as handle:\n",
    "    handle.write('\\n'.join(single_model_filenames)+'\\n')"
   ]
  },
  {
   "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. You can use all possible pairs from the list of single species models created in Widget 3 as shown in the cell below or select specific pairs by building the input list yourself."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('/Users/m097749/mminte_tutorial/single_models/1367212.3.json',\n",
       " '/Users/m097749/mminte_tutorial/single_models/1235835.3.json')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pairs = mminte.get_all_pairs(single_model_filenames)\n",
    "pairs[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "The second input parameter is the path to a folder for storing the two species community model files (created in \"Getting Started\").\n",
    "\n",
    "The output is a list of paths to the two species community model files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "pair_model_filenames = mminte.create_interaction_models(pairs, pair_model_folder)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "For reference, save the list of paths to the two species community models to a file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": 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. To determine how the presence of one species in the community affects the growth of the other species, the two species community model is optimized for growth with the two species together and with each species alone under specific nutritient conditions. The type of interaction is set based on the outcomes identified in Table 1 of [Anoxic Conditions Promote Species-Specific Mutualism between Gut Microbes In Silico](http://aem.asm.org/content/81/12/4049.full). What happens between two species may be broadly divided into positive, negative, or no interaction. \n",
    "\n",
    "The first input parameter is the list of paths to two species community model files that were created in Widget 4. \n",
    "\n",
    "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. A complete medium for the ModelSEED models used in this tutorial is in the test data included with the mminte package. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "complete_diet = mminte.read_diet_file(pkg_resources.resource_filename('mminte', 'test/data/ms_complete100.txt'))"
   ]
  },
  {
   "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": 18,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "growth_rates = mminte.calculate_growth_rates(pair_model_filenames, complete_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. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "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>1367212.3</td>\n",
       "      <td>1235835.3</td>\n",
       "      <td>Parasitism</td>\n",
       "      <td>4.778327</td>\n",
       "      <td>4.778327</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.389163</td>\n",
       "      <td>2.145181</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1367212.3</td>\n",
       "      <td>1165092.3</td>\n",
       "      <td>Parasitism</td>\n",
       "      <td>23.201545</td>\n",
       "      <td>13.772707</td>\n",
       "      <td>9.428838</td>\n",
       "      <td>2.389163</td>\n",
       "      <td>22.060714</td>\n",
       "      <td>4.764657</td>\n",
       "      <td>-0.572596</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1367212.3</td>\n",
       "      <td>1313290.3</td>\n",
       "      <td>Parasitism</td>\n",
       "      <td>6.141120</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>6.141120</td>\n",
       "      <td>2.389163</td>\n",
       "      <td>4.912896</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>0.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1367212.3</td>\n",
       "      <td>1469948.3</td>\n",
       "      <td>Parasitism</td>\n",
       "      <td>8.534269</td>\n",
       "      <td>6.613560</td>\n",
       "      <td>1.920709</td>\n",
       "      <td>2.389163</td>\n",
       "      <td>6.230712</td>\n",
       "      <td>1.768149</td>\n",
       "      <td>-0.691735</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1367212.3</td>\n",
       "      <td>1000569.4</td>\n",
       "      <td>Parasitism</td>\n",
       "      <td>4.380133</td>\n",
       "      <td>4.380133</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.389163</td>\n",
       "      <td>3.130046</td>\n",
       "      <td>0.833333</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1367212.3</td>\n",
       "      <td>1414720.3</td>\n",
       "      <td>Parasitism</td>\n",
       "      <td>5.972908</td>\n",
       "      <td>5.972908</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.389163</td>\n",
       "      <td>5.916418</td>\n",
       "      <td>1.500000</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1367212.3</td>\n",
       "      <td>657315.3</td>\n",
       "      <td>Parasitism</td>\n",
       "      <td>21.732161</td>\n",
       "      <td>13.319827</td>\n",
       "      <td>8.412334</td>\n",
       "      <td>2.389163</td>\n",
       "      <td>19.120254</td>\n",
       "      <td>4.575101</td>\n",
       "      <td>-0.560030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1367212.3</td>\n",
       "      <td>525362.3</td>\n",
       "      <td>Parasitism</td>\n",
       "      <td>3.583745</td>\n",
       "      <td>3.583745</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.389163</td>\n",
       "      <td>3.579748</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1367212.3</td>\n",
       "      <td>1156417.3</td>\n",
       "      <td>Amensalism</td>\n",
       "      <td>11.129313</td>\n",
       "      <td>2.145923</td>\n",
       "      <td>8.983390</td>\n",
       "      <td>2.389163</td>\n",
       "      <td>9.510915</td>\n",
       "      <td>-0.101810</td>\n",
       "      <td>-0.055465</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1367212.3</td>\n",
       "      <td>180332.3</td>\n",
       "      <td>Amensalism</td>\n",
       "      <td>27.933158</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>27.933158</td>\n",
       "      <td>2.389163</td>\n",
       "      <td>26.537878</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>0.052577</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        A_ID       B_ID        TYPE   TOGETHER  A_TOGETHER  B_TOGETHER  \\\n",
       "0  1367212.3  1235835.3  Parasitism   4.778327    4.778327    0.000000   \n",
       "1  1367212.3  1165092.3  Parasitism  23.201545   13.772707    9.428838   \n",
       "2  1367212.3  1313290.3  Parasitism   6.141120    0.000000    6.141120   \n",
       "3  1367212.3  1469948.3  Parasitism   8.534269    6.613560    1.920709   \n",
       "4  1367212.3  1000569.4  Parasitism   4.380133    4.380133    0.000000   \n",
       "5  1367212.3  1414720.3  Parasitism   5.972908    5.972908    0.000000   \n",
       "6  1367212.3   657315.3  Parasitism  21.732161   13.319827    8.412334   \n",
       "7  1367212.3   525362.3  Parasitism   3.583745    3.583745    0.000000   \n",
       "8  1367212.3  1156417.3  Amensalism  11.129313    2.145923    8.983390   \n",
       "9  1367212.3   180332.3  Amensalism  27.933158    0.000000   27.933158   \n",
       "\n",
       "    A_ALONE    B_ALONE  A_CHANGE  B_CHANGE  \n",
       "0  2.389163   2.145181  1.000000 -1.000000  \n",
       "1  2.389163  22.060714  4.764657 -0.572596  \n",
       "2  2.389163   4.912896 -1.000000  0.250000  \n",
       "3  2.389163   6.230712  1.768149 -0.691735  \n",
       "4  2.389163   3.130046  0.833333 -1.000000  \n",
       "5  2.389163   5.916418  1.500000 -1.000000  \n",
       "6  2.389163  19.120254  4.575101 -0.560030  \n",
       "7  2.389163   3.579748  0.500000 -1.000000  \n",
       "8  2.389163   9.510915 -0.101810 -0.055465  \n",
       "9  2.389163  26.537878 -1.000000  0.052577  "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "growth_rates[0:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "For reference, save the growth rates data frame to a file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "mminte.write_growth_rates_file(growth_rates, join(analysis_folder, 'growth_rates.csv'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "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()`. \n",
    "\n",
    "The first input is the growth rates data frame created in Widget 5. The second input parameter is the similarity data frame created in Widget 2. The third input parameter is the correlations between OTUs used as input in Widget 1.\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 shading of a node indicates how close the sequence of the OTU is to the sequence of the genome. The darker the node, the higher the similarity. 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. The thickness of an edge indicates the strength of the correlation between the OTUs in a pair. The thicker the edge, the higher the correlation between the OTUs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "graph = mminte.make_graph(growth_rates, similarity, correlations)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "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": 22,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "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": 0
}
