If you are not using GitHub click here to open the notebook using nbviewer.

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.

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 (the data was retrieved from study 232 in Qiita, 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
Emperor resources missing. Expected them to be found in https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files
Out[4]:
<emperor.core.Emperor at 0x106b40128>