def test_faith_pd_none_observed(self): actual = faith_pd(np.array([], dtype=int), np.array([], dtype=int), self.t1) expected = 0.0 self.assertAlmostEqual(actual, expected) actual = faith_pd([0, 0, 0, 0, 0], self.oids1, self.t1) expected = 0.0 self.assertAlmostEqual(actual, expected)
def test_faith_pd_all_observed(self): actual = faith_pd([1, 1, 1, 1, 1], self.oids1, self.t1) expected = sum(n.length for n in self.t1.traverse() if n.length is not None) self.assertAlmostEqual(actual, expected) actual = faith_pd([1, 2, 3, 4, 5], self.oids1, self.t1) expected = sum(n.length for n in self.t1.traverse() if n.length is not None) self.assertAlmostEqual(actual, expected)
def test_faith_pd_minimal_trees(self): # expected values computed by hand # zero tips tree = TreeNode.read(StringIO(u'root;')) actual = faith_pd(np.array([], dtype=int), [], tree) expected = 0.0 self.assertEqual(actual, expected) # two tips tree = TreeNode.read(StringIO(u'(OTU1:0.25, OTU2:0.25)root;')) actual = faith_pd([1, 0], ['OTU1', 'OTU2'], tree) expected = 0.25 self.assertEqual(actual, expected)
def test_faith_pd_root_not_observed(self): # expected values computed by hand tree = TreeNode.read(StringIO(u"((OTU1:0.1, OTU2:0.2):0.3, (OTU3:0.5, OTU4:0.7):1.1)" u"root;")) otu_ids = ["OTU%d" % i for i in range(1, 5)] # root node not observed, but branch between (OTU1, OTU2) and root # is considered observed actual = faith_pd([1, 1, 0, 0], otu_ids, tree) expected = 0.6 self.assertAlmostEqual(actual, expected) # root node not observed, but branch between (OTU3, OTU4) and root # is considered observed actual = faith_pd([0, 0, 1, 1], otu_ids, tree) expected = 2.3 self.assertAlmostEqual(actual, expected)
def test_faith_pd(self): # expected results derived from QIIME 1.9.1, which # is a completely different implementation skbio's initial # phylogenetic diversity implementation actual = faith_pd(self.b1[0], self.oids1, self.t1) expected = 4.5 self.assertAlmostEqual(actual, expected) actual = faith_pd(self.b1[1], self.oids1, self.t1) expected = 4.75 self.assertAlmostEqual(actual, expected) actual = faith_pd(self.b1[2], self.oids1, self.t1) expected = 4.75 self.assertAlmostEqual(actual, expected) actual = faith_pd(self.b1[3], self.oids1, self.t1) expected = 4.75 self.assertAlmostEqual(actual, expected)
def test_faith_pd_root_not_observed(self): # expected values computed by hand tree = TreeNode.read( StringIO(u'((OTU1:0.1, OTU2:0.2):0.3, (OTU3:0.5, OTU4:0.7):1.1)' u'root;')) otu_ids = ['OTU%d' % i for i in range(1, 5)] # root node not observed, but branch between (OTU1, OTU2) and root # is considered observed actual = faith_pd([1, 1, 0, 0], otu_ids, tree) expected = 0.6 self.assertAlmostEqual(actual, expected) # root node not observed, but branch between (OTU3, OTU4) and root # is considered observed actual = faith_pd([0, 0, 1, 1], otu_ids, tree) expected = 2.3 self.assertAlmostEqual(actual, expected)
def test_faith_pd(self): # calling faith_pd through alpha_diversity gives same results as # calling it directly expected = [] for e in self.table1: expected.append(faith_pd(e, tree=self.tree1, otu_ids=self.oids1)) expected = pd.Series(expected) actual = alpha_diversity('faith_pd', self.table1, tree=self.tree1, otu_ids=self.oids1) assert_series_almost_equal(actual, expected) # alt input table and tree expected = [] for e in self.table2: expected.append(faith_pd(e, tree=self.tree2, otu_ids=self.oids2)) expected = pd.Series(expected) actual = alpha_diversity('faith_pd', self.table2, tree=self.tree2, otu_ids=self.oids2) assert_series_almost_equal(actual, expected)
def test_faith_pd_extra_tips(self): # results are the same despite presences of unobserved tips in tree actual = faith_pd(self.b1[0], self.oids1, self.t1_w_extra_tips) expected = faith_pd(self.b1[0], self.oids1, self.t1) self.assertAlmostEqual(actual, expected) actual = faith_pd(self.b1[1], self.oids1, self.t1_w_extra_tips) expected = faith_pd(self.b1[1], self.oids1, self.t1) self.assertAlmostEqual(actual, expected) actual = faith_pd(self.b1[2], self.oids1, self.t1_w_extra_tips) expected = faith_pd(self.b1[2], self.oids1, self.t1) self.assertAlmostEqual(actual, expected) actual = faith_pd(self.b1[3], self.oids1, self.t1_w_extra_tips) expected = faith_pd(self.b1[3], self.oids1, self.t1) self.assertAlmostEqual(actual, expected)
def count_alpha_diversity(series: pd.Series, rooted_tree: TreeNode = None): d = { k: ALPHA_DIVERSITY_FUNCTIONS[k](series.values) for k in ALPHA_DIVERSITY_FUNCTIONS } d["Inverse Simpson Index"] = 1.0 / d["Simpson Index"] d["Gini–Simpson Index"] = 1.0 - d["Simpson Index"] if rooted_tree is not None: d["Faith Diversity"] = a.faith_pd(series.values, series.index.values, rooted_tree) out = dict2pd_series(d) out.name = series.name return out
def test_faith_pd_qiime_tiny_test(self): # the following table and tree are derived from the QIIME 1.9.1 # "tiny-test" data tt_table_fp = get_data_path(os.path.join("qiime-191-tt", "otu-table.tsv"), "data") tt_tree_fp = get_data_path(os.path.join("qiime-191-tt", "tree.nwk"), "data") self.q_table = pd.read_csv(tt_table_fp, sep="\t", skiprows=1, index_col=0) self.q_tree = TreeNode.read(tt_tree_fp) expected_fp = get_data_path(os.path.join("qiime-191-tt", "faith-pd.txt"), "data") expected = pd.read_csv(expected_fp, sep="\t", index_col=0) for sid in self.q_table.columns: actual = faith_pd(self.q_table[sid], otu_ids=self.q_table.index, tree=self.q_tree) self.assertAlmostEqual(actual, expected["PD_whole_tree"][sid])
def test_faith_pd_qiime_tiny_test(self): # the following table and tree are derived from the QIIME 1.9.1 # "tiny-test" data tt_table_fp = get_data_path( os.path.join('qiime-191-tt', 'otu-table.tsv'), 'data') tt_tree_fp = get_data_path(os.path.join('qiime-191-tt', 'tree.nwk'), 'data') self.q_table = pd.read_csv(tt_table_fp, sep='\t', skiprows=1, index_col=0) self.q_tree = TreeNode.read(tt_tree_fp) expected_fp = get_data_path( os.path.join('qiime-191-tt', 'faith-pd.txt'), 'data') expected = pd.read_csv(expected_fp, sep='\t', index_col=0) for sid in self.q_table.columns: actual = faith_pd(self.q_table[sid], otu_ids=self.q_table.index, tree=self.q_tree) self.assertAlmostEqual(actual, expected['PD_whole_tree'][sid])
def test_faith_pd_minimal(self): # two tips tree = TreeNode.read(StringIO('(OTU1:0.25, OTU2:0.25)root;')) actual = faith_pd([1, 0], ['OTU1', 'OTU2'], tree) expected = 0.25 self.assertEqual(actual, expected)
def test_faith_pd_minimal(self): # two tips tree = TreeNode.read(StringIO(u'(OTU1:0.25, OTU2:0.25)root;')) actual = faith_pd([1, 0], ['OTU1', 'OTU2'], tree) expected = 0.25 self.assertEqual(actual, expected)