If you are not using GitHub click here to open the notebook using nbviewer.
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.
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.
mf = load_mf('keyboard/mapping-file.txt')
res = OrdinationResults.read('keyboard/unweighted-unifrac.even1000.txt')
# change the remote parameter to False/True depending on what you want to do
x = Emperor(res, mf, remote=True)
x
<emperor.core.Emperor at 0x106b40128>