#!/usr/bin/env python # coding: utf-8 # 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). # # Emperor's Python API # # **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).** # In[1]: import pandas as pd, numpy as np from emperor import Emperor, nbinstall from skbio import OrdinationResults from emperor.qiime_backports.parse import parse_mapping_file from emperor.qiime_backports.format import format_mapping_file from skbio.io.util import open_file nbinstall() def load_mf(fn): with open_file(fn) as f: mapping_data, header, _ = parse_mapping_file(f) _mapping_file = pd.DataFrame(mapping_data, columns=header) _mapping_file.set_index('SampleID', inplace=True) return _mapping_file def write_mf(f, _df): with open(f, 'w') as fp: lines = format_mapping_file(['SampleID'] + _df.columns.tolist(), list(_df.itertuples())) fp.write(lines+'\n') # 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). # # Specifically, here we will reproduce *Figure 1 A*. # In[2]: mf = load_mf('keyboard/mapping-file.txt') res = OrdinationResults.read('keyboard/unweighted-unifrac.even1000.txt') # In[3]: # change the remote parameter to False/True depending on what you want to do x = Emperor(res, mf, remote=True) # In[4]: x