{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "This notebook creates a jackknifed PCoA plot based on multiple rarefactions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "from emperor import Emperor, nbinstall\n",
    "\n",
    "nbinstall()\n",
    "\n",
    "from skbio.stats.ordination import pcoa\n",
    "from skbio.diversity import beta_diversity\n",
    "\n",
    "from biom import load_table\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "def load_mf(fn, index='#SampleID'):\n",
    "    _df = pd.read_csv(fn, sep='\\t', dtype=str, keep_default_na=False, na_values=[])\n",
    "    _df.set_index(index, inplace=True)\n",
    "    return _df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "We are going to load data from [Fierer et al. 2010](http://www.pnas.org/content/107/14/6477.full) (the data was retrieved from study [232](https://qiita.ucsd.edu/study/description/232) in [Qiita](https://qiita.ucsd.edu), remember you need to be logged in to access the study)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "mf = load_mf('keyboard/mapping-file.txt')\n",
    "bt = load_table('keyboard/otu-table.biom')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Create 5 rarefactions and compute the Jaccard distance matrix for each resulting table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "ordinations = []\n",
    "for r in range(5):\n",
    "    rarefied = bt.subsample(1000)\n",
    "    data = np.array([rarefied.data(i) for i in rarefied.ids()], dtype='int64')\n",
    "    \n",
    "    res = pcoa(beta_diversity('jaccard', data, rarefied.ids()))\n",
    "    \n",
    "    ordinations.append(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Jackknifed plots need a *master* set of coordinates where there rest of the matrices will be centered around."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "master = ordinations[0]\n",
    "jackknifed = ordinations[1:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "If you want to share your notebook via GitHub use `remote=True` and make sure you share your notebook using nbviewer. Change the jackknifing method to standard deviation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "viz = Emperor(master, mf, jackknifed=jackknifed, remote=False)\n",
    "viz.jackknifing_method = 'sdev'\n",
    "viz"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Change the jackknifing method to inter quantile ranges:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "viz.jackknifing_method = 'IQR'\n",
    "viz"
   ]
  }
 ],
 "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.5.1"
  },
  "widgets": {
   "state": {},
   "version": "1.1.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}