{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "If you are not using GitHub [click here to open the notebook using nbviewer](http://nbviewer.jupyter.org/github/biocore/emperor/blob/new-api/examples/keyboard.ipynb)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Emperor's Python API\n", "\n", "**This notebook demonstrate Emperor's new Python API, which can and will change as we continue to exercise this interface, for more information, have a look at the [pull request here](https://github.com/biocore/emperor/pull/405).**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "import pandas as pd, numpy as np\n", "from emperor import Emperor, nbinstall\n", "from skbio import OrdinationResults\n", "\n", "from emperor.qiime_backports.parse import parse_mapping_file\n", "from emperor.qiime_backports.format import format_mapping_file\n", "\n", "from skbio.io.util import open_file\n", "\n", "nbinstall()\n", "\n", "def load_mf(fn):\n", " with open_file(fn) as f:\n", " mapping_data, header, _ = parse_mapping_file(f)\n", " _mapping_file = pd.DataFrame(mapping_data, columns=header)\n", " _mapping_file.set_index('SampleID', inplace=True)\n", " return _mapping_file\n", "\n", "def write_mf(f, _df):\n", " with open(f, 'w') as fp:\n", " lines = format_mapping_file(['SampleID'] + _df.columns.tolist(),\n", " list(_df.itertuples()))\n", " fp.write(lines+'\\n')" ] }, { "cell_type": "markdown", "metadata": {}, "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).\n", "\n", "Specifically, here we will reproduce *Figure 1 A*." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "mf = load_mf('keyboard/mapping-file.txt')\n", "res = OrdinationResults.read('keyboard/unweighted-unifrac.even1000.txt')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# change the remote parameter to False/True depending on what you want to do\n", "x = Emperor(res, mf, remote=True)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "
\n", "
Emperor resources missing. Expected them to be found in https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files
\n", "
\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] } ], "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 }