本文整理汇总了Python中skbio.TreeNode类的典型用法代码示例。如果您正苦于以下问题:Python TreeNode类的具体用法?Python TreeNode怎么用?Python TreeNode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TreeNode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
def setUp(self):
"""Prep the self"""
self.simple_t = TreeNode.read(StringIO(u"((a,b)i1,(c,d)i2)root;"))
nodes = dict([(x, TreeNode(x)) for x in "abcdefgh"])
nodes["a"].append(nodes["b"])
nodes["b"].append(nodes["c"])
nodes["c"].append(nodes["d"])
nodes["c"].append(nodes["e"])
nodes["c"].append(nodes["f"])
nodes["f"].append(nodes["g"])
nodes["a"].append(nodes["h"])
self.TreeNode = nodes
self.TreeRoot = nodes["a"]
def rev_f(items):
items.reverse()
def rotate_f(items):
tmp = items[-1]
items[1:] = items[:-1]
items[0] = tmp
self.rev_f = rev_f
self.rotate_f = rotate_f
self.complex_tree = TreeNode.read(StringIO(u"(((a,b)int1,(x,y,(w,z)int" "2,(c,d)int3)int4),(e,f)int" "5);"))
示例2: test_extend
def test_extend(self):
"""Extend a few nodes"""
second_tree = TreeNode.read(StringIO(u"(x1,y1)z1;"))
third_tree = TreeNode.read(StringIO(u"(x2,y2)z2;"))
first_tree = TreeNode.read(StringIO(u"(x1,y1)z1;"))
fourth_tree = TreeNode.read(StringIO(u"(x2,y2)z2;"))
self.simple_t.extend([second_tree, third_tree])
first_tree.extend(fourth_tree.children)
self.assertEqual(0, len(fourth_tree.children))
self.assertEqual(first_tree.children[0].name, "x1")
self.assertEqual(first_tree.children[1].name, "y1")
self.assertEqual(first_tree.children[2].name, "x2")
self.assertEqual(first_tree.children[3].name, "y2")
self.assertEqual(self.simple_t.children[0].name, "i1")
self.assertEqual(self.simple_t.children[1].name, "i2")
self.assertEqual(self.simple_t.children[2].name, "z1")
self.assertEqual(self.simple_t.children[3].name, "z2")
self.assertEqual(len(self.simple_t.children), 4)
self.assertEqual(self.simple_t.children[2].children[0].name, "x1")
self.assertEqual(self.simple_t.children[2].children[1].name, "y1")
self.assertEqual(self.simple_t.children[3].children[0].name, "x2")
self.assertEqual(self.simple_t.children[3].children[1].name, "y2")
self.assertIs(second_tree.parent, self.simple_t)
self.assertIs(third_tree.parent, self.simple_t)
示例3: test_backfill_names_gap
def test_backfill_names_gap(self):
"""correctly backfill names"""
consensus_tree = TreeNode.from_newick(
"(((s1,s2)g1,(s3,s4)g2,(s5,s6)g3)f1)o1;")
rank_lookup = {'s': 6, 'g': 5, 'f': 4, 'o': 3, 'c': 2, 'p': 1, 'k': 0}
for n in consensus_tree.traverse(include_self=True):
n.Rank = rank_lookup[n.name[0]]
input = "((((1)s1,(2)s2),((3)s3,(4)s5)))o1;"
lookup = dict([(n.name, n)
for n in consensus_tree.traverse(include_self=True)])
#exp = "((((1)s1,(2)s2)g1,((3)'g2; s3',(4)'g3; s5')))'o1; f1'"
t = TreeNode.from_newick(input)
t.Rank = 3
t.children[0].Rank = None
t.children[0].children[0].Rank = None
t.children[0].children[1].Rank = None
t.children[0].children[0].children[0].Rank = 6
t.children[0].children[0].children[1].Rank = 6
t.children[0].children[1].children[0].Rank = 6
t.children[0].children[1].children[1].Rank = 6
backfill_names_gap(t, lookup)
self.assertEqual(t.BackFillNames, ['o1'])
self.assertEqual(t.children[0].BackFillNames, [])
self.assertEqual(t.children[0].children[0].BackFillNames, [])
self.assertEqual(t.children[0].children[1].BackFillNames, [])
self.assertEqual(t.children[0].children[0]
.children[0].BackFillNames, ['f1', 'g1', 's1'])
self.assertEqual(t.children[0].children[0]
.children[1].BackFillNames, ['f1', 'g1', 's2'])
self.assertEqual(t.children[0].children[1]
.children[0].BackFillNames, ['f1', 'g2', 's3'])
self.assertEqual(t.children[0].children[1]
.children[1].BackFillNames, ['f1', 'g3', 's5'])
示例4: test_validate_otu_ids_and_tree
def test_validate_otu_ids_and_tree(self):
# basic valid input
t = TreeNode.read(
StringIO(u"(((((OTU1:0.5,OTU2:0.5):0.5,OTU3:1.0):1.0):0.0,(OTU4:" u"0.75,OTU5:0.75):1.25):0.0)root;")
)
counts = [1, 1, 1]
otu_ids = ["OTU1", "OTU2", "OTU3"]
self.assertTrue(_validate_otu_ids_and_tree(counts, otu_ids, t) is None)
# all tips observed
t = TreeNode.read(
StringIO(u"(((((OTU1:0.5,OTU2:0.5):0.5,OTU3:1.0):1.0):0.0,(OTU4:" u"0.75,OTU5:0.75):1.25):0.0)root;")
)
counts = [1, 1, 1, 1, 1]
otu_ids = ["OTU1", "OTU2", "OTU3", "OTU4", "OTU5"]
self.assertTrue(_validate_otu_ids_and_tree(counts, otu_ids, t) is None)
# no tips observed
t = TreeNode.read(
StringIO(u"(((((OTU1:0.5,OTU2:0.5):0.5,OTU3:1.0):1.0):0.0,(OTU4:" u"0.75,OTU5:0.75):1.25):0.0)root;")
)
counts = []
otu_ids = []
self.assertTrue(_validate_otu_ids_and_tree(counts, otu_ids, t) is None)
# all counts zero
t = TreeNode.read(
StringIO(u"(((((OTU1:0.5,OTU2:0.5):0.5,OTU3:1.0):1.0):0.0,(OTU4:" u"0.75,OTU5:0.75):1.25):0.0)root;")
)
counts = [0, 0, 0, 0, 0]
otu_ids = ["OTU1", "OTU2", "OTU3", "OTU4", "OTU5"]
self.assertTrue(_validate_otu_ids_and_tree(counts, otu_ids, t) is None)
示例5: test_index_tree
def test_index_tree(self):
"""index_tree should produce correct index and node map"""
# test for first tree: contains singleton outgroup
t1 = TreeNode.read(StringIO(u'(((a,b),c),(d,e));'))
t2 = TreeNode.read(StringIO(u'(((a,b),(c,d)),(e,f));'))
t3 = TreeNode.read(StringIO(u'(((a,b,c),(d)),(e,f));'))
id_1, child_1 = t1.index_tree()
nodes_1 = [n.id for n in t1.traverse(self_before=False,
self_after=True)]
self.assertEqual(nodes_1, [0, 1, 2, 3, 6, 4, 5, 7, 8])
self.assertEqual(child_1, [(2, 0, 1), (6, 2, 3), (7, 4, 5), (8, 6, 7)])
# test for second tree: strictly bifurcating
id_2, child_2 = t2.index_tree()
nodes_2 = [n.id for n in t2.traverse(self_before=False,
self_after=True)]
self.assertEqual(nodes_2, [0, 1, 4, 2, 3, 5, 8, 6, 7, 9, 10])
self.assertEqual(child_2, [(4, 0, 1), (5, 2, 3), (8, 4, 5), (9, 6, 7),
(10, 8, 9)])
# test for third tree: contains trifurcation and single-child parent
id_3, child_3 = t3.index_tree()
nodes_3 = [n.id for n in t3.traverse(self_before=False,
self_after=True)]
self.assertEqual(nodes_3, [0, 1, 2, 4, 3, 5, 8, 6, 7, 9, 10])
self.assertEqual(child_3, [(4, 0, 2), (5, 3, 3), (8, 4, 5), (9, 6, 7),
(10, 8, 9)])
示例6: test_commonname_promotion
def test_commonname_promotion(self):
"""correctly promote names if possible"""
consensus_tree = TreeNode.read(StringIO(u"(((s1,s2)g1,(s3,s4)g2,(s5,s6)g3)f1)o1;"))
rank_lookup = {'s': 6, 'g': 5, 'f': 4, 'o': 3, 'c': 2, 'p': 1, 'k': 0}
for n in consensus_tree.traverse(include_self=True):
n.Rank = rank_lookup[n.name[0]]
data = StringIO(u"((((1)s1,(2)s2),((3)s3,(4)s5)))o1;")
lookup = dict([(n.name, n)
for n in consensus_tree.traverse(include_self=True)])
exp = "((((1)s1,(2)s2)g1,((3)'g2; s3',(4)'g3; s5')))'o1; f1';"
t = TreeNode.read(data)
t.Rank = 3
t.children[0].Rank = None
t.children[0].children[0].Rank = None
t.children[0].children[1].Rank = None
t.children[0].children[0].children[0].Rank = 6
t.children[0].children[0].children[1].Rank = 6
t.children[0].children[1].children[0].Rank = 6
t.children[0].children[1].children[1].Rank = 6
backfill_names_gap(t, lookup)
commonname_promotion(t)
fp = StringIO()
t.write(fp)
self.assertEqual(fp.getvalue().strip(), exp)
示例7: test_reformat_treepuzzle
def test_reformat_treepuzzle(self):
""" Test functionality of reformat_treepuzzle()
"""
species_tree = TreeNode.read(self.species_tree_fp, format='newick')
gene_tree_3 = TreeNode.read(self.gene_tree_3_fp, format='newick')
output_tree_fp = join(self.working_dir, "joined_trees.nwk")
output_msa_phy_fp = join(self.working_dir, "gene_tree_3.phy")
reformat_treepuzzle(gene_tree_3,
species_tree,
self.msa_fa_3_fp,
output_tree_fp,
output_msa_phy_fp)
reformat_tree_exp = [
"(((((((SE001:2.1494877,SE010:1.08661):3.7761166,SE008:"
"0.86305436):0.21024487,(SE006:0.56704221,SE009:0.5014676):"
"0.90294223):0.20542323,SE005:3.0992506):0.37145632,SE004:"
"1.8129133):0.72933621,SE003:1.737411):0.24447835,(SE002:"
"1.6606127,SE007:0.70000178):1.6331374);\n",
"(((((((SE001:2.1494876,SE010:2.1494876):"
"3.7761166,SE008:5.9256042):0.2102448,(SE006:"
"5.2329068,SE009:5.2329068):0.9029422):0.2054233,"
"SE005:6.3412723):0.3714563,SE004:6.7127286):"
"0.7293362,SE003:7.4420648):0.2444784,SE002:"
"7.6865432);\n"]
with open(output_tree_fp, 'r') as output_tree_f:
reformat_tree_act = output_tree_f.readlines()
self.assertListEqual(reformat_tree_exp, reformat_tree_act)
msa_fa = TabularMSA.read(output_msa_phy_fp, constructor=Protein)
labels_exp = [u'SE001', u'SE002', u'SE003', u'SE004', u'SE005',
u'SE006', u'SE008', u'SE009', u'SE010']
labels_act = list(msa_fa.index)
self.assertListEqual(labels_exp, labels_act)
示例8: _main
def _main(gene_tree_fp, species_tree_fp, gene_msa_fa_fp, output_tree_fp, output_msa_phy_fp, method):
""" Reformat trees to input accepted by various HGT detection methods.
Species tree can be multifurcating, however will be converted to
bifurcating trees for software that require them. Leaf labels of
species tree and gene tree must match, however the label
SPECIES_GENE is acceptable for multiple genes in the gene
tree. Leaf labels must also be at most 10 characters long (for
PHYLIP manipulations).
"""
# add function to check where tree is multifurcating and the labeling
# is correct
gene_tree = TreeNode.read(gene_tree_fp, format="newick")
species_tree = TreeNode.read(species_tree_fp, format="newick")
if method == "ranger-dtl":
reformat_rangerdtl(gene_tree=gene_tree, species_tree=species_tree, output_tree_fp=output_tree_fp)
elif method == "trex":
reformat_trex(gene_tree=gene_tree, species_tree=species_tree, output_tree_fp=output_tree_fp)
elif method == "riata-hgt":
reformat_riatahgt(gene_tree=gene_tree, species_tree=species_tree, output_tree_fp=output_tree_fp)
elif method == "jane4":
reformat_jane4(gene_tree=gene_tree, species_tree=species_tree, output_tree_fp=output_tree_fp)
elif method == "tree-puzzle":
reformat_treepuzzle(
gene_tree=gene_tree,
species_tree=species_tree,
gene_msa_fa_fp=gene_msa_fa_fp,
output_tree_fp=output_tree_fp,
output_msa_phy_fp=output_msa_phy_fp,
)
示例9: test_reformat_jane4
def test_reformat_jane4(self):
""" Test functionality of reformat_jane4()
"""
species_tree = TreeNode.read(self.species_tree_fp, format='newick')
gene_tree_1 = TreeNode.read(self.gene_tree_1_fp, format='newick')
output_tree_fp = join(self.working_dir, "joined_trees.nex")
reformat_jane4(gene_tree_1,
species_tree,
output_tree_fp)
reformat_tree_exp = [
"#NEXUS\n", "begin host;\n",
"tree host = "
"(((((((SE001,SE010),SE008),(SE006,SE009)),SE005),SE004),SE003),"
"(SE002,SE007));\n", "\n",
"endblock;\n", "begin parasite;\n",
"tree parasite = "
"(((((((SE001_01623,SE010_01623),SE008_01623),(SE006_01623,"
"SE009_01623)),SE005_01623),SE004_01623),SE003_01623),"
"((SE002_01623,SE007_01623),((((SE001_04123,SE010_04123),"
"SE008_04123),(SE006_04123,SE009_04123)),SE005_04123)));\n", "\n",
"endblock;\n",
"begin distribution;\n",
"Range SE010_01623:SE010, SE010_04123:SE010, SE009_01623:SE009, "
"SE009_04123:SE009, SE008_01623:SE008, SE008_04123:SE008, "
"SE007_01623:SE007, SE006_01623:SE006, SE006_04123:SE006, "
"SE005_01623:SE005, SE005_04123:SE005, SE004_01623:SE004, "
"SE003_01623:SE003, SE002_01623:SE002, SE001_01623:SE001, "
"SE001_04123:SE001;\n",
"endblock;\n"]
with open(output_tree_fp, 'r') as output_tree_f:
reformat_tree_act = output_tree_f.readlines()
self.assertListEqual(reformat_tree_exp, reformat_tree_act)
示例10: test_biom_match_tips_intersect_columns
def test_biom_match_tips_intersect_columns(self):
# table has less columns than tree tips
table = Table(
np.array([[0, 0, 1],
[2, 3, 4],
[5, 5, 3],
[0, 0, 1]]).T,
['a', 'b', 'd'],
['s1', 's2', 's3', 's4'])
tree = TreeNode.read([u"(((a,b)f, c),d)r;"])
table = Table(
np.array([[0, 0, 1],
[2, 3, 4],
[5, 5, 3],
[0, 0, 1]]).T,
['a', 'b', 'd'],
['s1', 's2', 's3', 's4'])
exp_table = Table(
np.array([[1, 0, 0],
[4, 2, 3],
[3, 5, 5],
[1, 0, 0]]).T,
['d', 'a', 'b'],
['s1', 's2', 's3', 's4'])
exp_tree = TreeNode.read([u"(d,(a,b)f)r;"])
res_table, res_tree = match_tips(table, tree)
self.assertEqual(exp_table, res_table)
self.assertEqual(str(exp_tree), str(res_tree))
示例11: test_extend
def test_extend(self):
"""Extend a few nodes"""
second_tree = TreeNode.read(io.StringIO("(x1,y1)z1;"))
third_tree = TreeNode.read(io.StringIO("(x2,y2)z2;"))
first_tree = TreeNode.read(io.StringIO("(x1,y1)z1;"))
fourth_tree = TreeNode.read(io.StringIO("(x2,y2)z2;"))
self.simple_t.extend([second_tree, third_tree])
first_tree.extend(fourth_tree.children)
self.assertEqual(0, len(fourth_tree.children))
self.assertEqual(first_tree.children[0].name, 'x1')
self.assertEqual(first_tree.children[1].name, 'y1')
self.assertEqual(first_tree.children[2].name, 'x2')
self.assertEqual(first_tree.children[3].name, 'y2')
self.assertEqual(self.simple_t.children[0].name, 'i1')
self.assertEqual(self.simple_t.children[1].name, 'i2')
self.assertEqual(self.simple_t.children[2].name, 'z1')
self.assertEqual(self.simple_t.children[3].name, 'z2')
self.assertEqual(len(self.simple_t.children), 4)
self.assertEqual(self.simple_t.children[2].children[0].name, 'x1')
self.assertEqual(self.simple_t.children[2].children[1].name, 'y1')
self.assertEqual(self.simple_t.children[3].children[0].name, 'x2')
self.assertEqual(self.simple_t.children[3].children[1].name, 'y2')
self.assertIs(second_tree.parent, self.simple_t)
self.assertIs(third_tree.parent, self.simple_t)
示例12: test_compare_subsets
def test_compare_subsets(self):
"""compare_subsets should return the fraction of shared subsets"""
t = TreeNode.read(io.StringIO('((H,G),(R,M));'))
t2 = TreeNode.read(io.StringIO('(((H,G),R),M);'))
t4 = TreeNode.read(io.StringIO('(((H,G),(O,R)),X);'))
result = t.compare_subsets(t)
self.assertEqual(result, 0)
result = t2.compare_subsets(t2)
self.assertEqual(result, 0)
result = t.compare_subsets(t2)
self.assertEqual(result, 0.5)
result = t.compare_subsets(t4)
self.assertEqual(result, 1 - 2. / 5)
result = t.compare_subsets(t4, exclude_absent_taxa=True)
self.assertEqual(result, 1 - 2. / 3)
result = t.compare_subsets(self.TreeRoot, exclude_absent_taxa=True)
self.assertEqual(result, 1)
result = t.compare_subsets(self.TreeRoot)
self.assertEqual(result, 1)
示例13: test_tip_tip_distances_missing_length
def test_tip_tip_distances_missing_length(self):
t = TreeNode.read(io.StringIO("((a,b:6)c:4,(d,e:0)f);"))
exp_t = TreeNode.read(io.StringIO("((a:0,b:6)c:4,(d:0,e:0)f:0);"))
exp_t_dm = exp_t.tip_tip_distances()
t_dm = npt.assert_warns(RepresentationWarning, t.tip_tip_distances)
self.assertEqual(t_dm, exp_t_dm)
示例14: test_index_tree
def test_index_tree(self):
"""index_tree should produce correct index and node map"""
# test for first tree: contains singleton outgroup
t1 = TreeNode.read(io.StringIO('(((a,b),c),(d,e));'))
t2 = TreeNode.read(io.StringIO('(((a,b),(c,d)),(e,f));'))
t3 = TreeNode.read(io.StringIO('(((a,b,c),(d)),(e,f));'))
id_1, child_1 = t1.index_tree()
nodes_1 = [n.id for n in t1.traverse(self_before=False,
self_after=True)]
self.assertEqual(nodes_1, [0, 1, 2, 3, 6, 4, 5, 7, 8])
npt.assert_equal(child_1, np.array([[2, 0, 1], [6, 2, 3], [7, 4, 5],
[8, 6, 7]]))
# test for second tree: strictly bifurcating
id_2, child_2 = t2.index_tree()
nodes_2 = [n.id for n in t2.traverse(self_before=False,
self_after=True)]
self.assertEqual(nodes_2, [0, 1, 4, 2, 3, 5, 8, 6, 7, 9, 10])
npt.assert_equal(child_2, np.array([[4, 0, 1], [5, 2, 3],
[8, 4, 5], [9, 6, 7],
[10, 8, 9]]))
# test for third tree: contains trifurcation and single-child parent
id_3, child_3 = t3.index_tree()
nodes_3 = [n.id for n in t3.traverse(self_before=False,
self_after=True)]
self.assertEqual(nodes_3, [0, 1, 2, 4, 3, 5, 8, 6, 7, 9, 10])
npt.assert_equal(child_3, np.array([[4, 0, 2], [5, 3, 3], [8, 4, 5],
[9, 6, 7], [10, 8, 9]]))
示例15: test_reformat_riatahgt
def test_reformat_riatahgt(self):
""" Test functionality of reformat_riatahgt()
"""
species_tree = TreeNode.read(self.species_tree_fp, format='newick')
gene_tree_1 = TreeNode.read(self.gene_tree_1_fp, format='newick')
output_tree_fp = join(self.working_dir, "joined_trees.nex")
reformat_riatahgt(gene_tree_1,
species_tree,
output_tree_fp)
reformat_tree_exp = [
"#NEXUS\n", "BEGIN TREES;\n",
"Tree speciesTree = "
"(((((((SE001:2.1494877,SE010:1.08661):3.7761166,SE008:"
"0.86305436):0.21024487,(SE006:0.56704221,SE009:0.5014676):"
"0.90294223):0.20542323,SE005:3.0992506):0.37145632,SE004:"
"1.8129133):0.72933621,SE003:1.737411):0.24447835,(SE002:"
"1.6606127,SE007:0.70000178):1.6331374):1.594016;\n",
"Tree geneTree = "
"(((((((SE001:2.1494876,SE010:2.1494876):"
"3.7761166,SE008:5.9256042):0.2102448,(SE006:"
"5.2329068,SE009:5.2329068):0.9029422):0.2054233,"
"SE005:6.3412723):0.3714563,SE004:6.7127286):"
"0.7293362,SE003:7.4420648):0.2444784,((SE002:"
"6.0534057,SE007:6.0534057):0.4589905,((((SE001:"
"2.1494876,SE010:2.1494876):3.7761166,SE008:"
"5.9256042):0.2102448,(SE006:5.2329068,SE009:"
"5.2329068):0.9029422):0.2054233,SE005:6.3412723):"
"0.1711239):1.174147):1.594016;\n",
"END;\n",
"BEGIN PHYLONET;\n",
"RIATAHGT speciesTree {geneTree};\n",
"END;\n"]
with open(output_tree_fp, 'r') as output_tree_f:
reformat_tree_act = output_tree_f.readlines()
self.assertListEqual(reformat_tree_exp, reformat_tree_act)