{ "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", "# pydata/scipy\n", "import pandas as pd, numpy as np\n", "from scipy.spatial import procrustes\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": [ "We'll create five different distance matrices and compare them." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "ordinations = []\n", "distances = ['jaccard', 'dice', 'russellrao']\n", "rarefied = bt.subsample(1000)\n", "\n", "for r in range(len(distances)):\n", " data = np.array([rarefied.data(i) for i in rarefied.ids()], dtype='int64')\n", " \n", " res = pcoa(beta_diversity(distances[r], data, rarefied.ids()))\n", " \n", " ordinations.append(res)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Procrustes plots need a *master* set of coordinates where there rest of the matrices will be fitted around." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "master = ordinations[0]\n", "fitted_ordinations = []\n", "\n", "for i in range(1, len(ordinations)):\n", " reference, matrix, _ = procrustes(master.samples.values, ordinations[i].samples.values)\n", " \n", " if i == 0:\n", " master.samples.iloc[:] = reference\n", " \n", " samples = pd.DataFrame(index=ordinations[i].samples.index,\n", " columns=ordinations[i].samples.columns,\n", " data=matrix)\n", " ordinations[i].samples = samples\n", " fitted_ordinations.append(ordinations[i])" ] }, { "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "viz = Emperor(master, mf, procrustes=fitted_ordinations, remote=False)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Lastly, we set the name of the distances as the `procrustes_names` attribute so we can differentiate them in the plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "viz.procrustes_names = distances" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "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 }