当前位置: 首页>>代码示例>>Python>>正文


Python DndParser.traverse方法代码示例

本文整理汇总了Python中cogent.parse.tree.DndParser.traverse方法的典型用法代码示例。如果您正苦于以下问题:Python DndParser.traverse方法的具体用法?Python DndParser.traverse怎么用?Python DndParser.traverse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cogent.parse.tree.DndParser的用法示例。


在下文中一共展示了DndParser.traverse方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_backfill_names_gap

# 需要导入模块: from cogent.parse.tree import DndParser [as 别名]
# 或者: from cogent.parse.tree.DndParser import traverse [as 别名]
    def test_backfill_names_gap(self):
        """correctly backfill names"""
        consensus_tree = DndParser("(((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 = DndParser(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'])
开发者ID:teravest,项目名称:tax2tree,代码行数:31,代码来源:test_nlevel.py

示例2: test_reroot

# 需要导入模块: from cogent.parse.tree import DndParser [as 别名]
# 或者: from cogent.parse.tree.DndParser import traverse [as 别名]
 def test_reroot(self):
     """Should correctly reroot a tree"""
     t = DndParser("(((a,b)c,(d,e)f)g,(h,i)j);")
     tips = ['a','b']
     for n in t.traverse():
         n.Length = 1.0
     
     # note, g is lost because it has a single descendent and gets pruned off
     exp = "((a:1.0,b:1.0)c:0.5,((d:1.0,e:1.0)f:1.0,(h:1.0,i:1.0)j:2.0):0.5);"
     obs = reroot(t, tips)
     self.assertEqual(obs.getNewick(with_distances=True), exp)
开发者ID:teravest,项目名称:tax2tree,代码行数:13,代码来源:test_util.py

示例3: test_commonname_promotion

# 需要导入模块: from cogent.parse.tree import DndParser [as 别名]
# 或者: from cogent.parse.tree.DndParser import traverse [as 别名]
 def test_commonname_promotion(self):
     """correctly promote names if possible"""
     consensus_tree = DndParser("(((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 = DndParser(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)
     commonname_promotion(t)
     self.assertEqual(t.getNewick(with_distances=False), exp)
开发者ID:teravest,项目名称:tax2tree,代码行数:23,代码来源:test_nlevel.py

示例4: test_gnodedata

# 需要导入模块: from cogent.parse.tree import DndParser [as 别名]
# 或者: from cogent.parse.tree.DndParser import traverse [as 别名]
 def test_gnodedata(self):
     """DndParser should assign Name to internal nodes correctly"""
     t = DndParser(nodedata)
     self.assertEqual(len(t), 2)
     self.assertEqual(len(t[0]), 0)  #first child is terminal
     self.assertEqual(len(t[1]), 2)  #second child has two children
     self.assertEqual(str(t), '(abc:3.0,(def:4.0,ghi:5.0)jkl:6.0);')
     info_dict = {}
     for node in t.traverse():
         info_dict[node.Name] = node.Length
     self.assertEqual(info_dict['abc'], 3.0)
     self.assertEqual(info_dict['def'], 4.0)
     self.assertEqual(info_dict['ghi'], 5.0)
     self.assertEqual(info_dict['jkl'], 6.0)
开发者ID:GavinHuttley,项目名称:pycogent,代码行数:16,代码来源:test_tree.py

示例5: test_backfill_names_dangling

# 需要导入模块: from cogent.parse.tree import DndParser [as 别名]
# 或者: from cogent.parse.tree.DndParser import traverse [as 别名]
 def test_backfill_names_dangling(self):
     """correctly fill in dangling missing ranks"""
     consensus_tree = DndParser("(((s1,s2)g1,(s3,s4)g2,(s5,s6)g3)f1)o1;")
     input = "((((1),(2)),((3),(4))))'o1; f1';"
     lookup = dict([(n.Name, n) for n in consensus_tree.traverse(include_self=True)])
开发者ID:teravest,项目名称:tax2tree,代码行数:7,代码来源:test_nlevel.py

示例6: OldPhyloNodeTests

# 需要导入模块: from cogent.parse.tree import DndParser [as 别名]
# 或者: from cogent.parse.tree.DndParser import traverse [as 别名]
class OldPhyloNodeTests(TestCase):
    """Tests of the PhyloNode class -- these are all now methods of RangeNode."""
    def setUp(self):
        """Make a couple of standard trees"""
        self.t1 = DndParser('((a,(b,c)),(d,e))', RangeNode)
        #selt.t1 indices: ((0,(1,2)5)6,(3,4)7)8
    
    def test_makeIdIndex(self):
        """RangeNode makeIdIndex should assign ids to every node"""
        self.t1.makeIdIndex()
        result = self.t1.IdIndex
        nodes = list(self.t1.traverse(self_before=True))
        #check we got an entry for each node
        self.assertEqual(len(result), len(nodes))
        #check the ids are in the result
        for i in nodes:
            assert hasattr(i, 'Id')
            assert i.Id in result
            
    def test_assignQ_single_passed(self):
        """RangeNode assignQ should propagate single Q param down tree"""
        #should work if Q explicitly passed
        t = self.t1
        Q = ['a']
        t.assignQ(Q)
        for node in t.traverse(self_before=True):
            assert node.Q is Q

    def test_assignQ_single_set(self):
        """RangeNode assignQ should propagate single Q if set"""
        t = self.t1
        Q = ['a']
        assert not hasattr(t, 'Q')
        t.Q = Q
        t.assignQ()
        for node in t.traverse(self_before=True):
            assert node.Q is Q

    def test_assignQ_single_overwrite(self):
        """RangeNode assignQ should overwrite root Q if new Q passed"""
        t = self.t1
        Q = ['a']
        Q2 = ['b']
        t.Q = Q
        t.assignQ(Q2)
        for node in t.traverse(self_before=True):
            assert node.Q is Q2
            assert not node.Q is Q

    def test_assignQ_multiple(self):
        """RangeNode assignQ should propagate multiple Qs"""
        t = self.t1
        Q1 = ['a']
        Q2 = ['b']
        Q3 = ['c']
        t.makeIdIndex()
        t.IdIndex[7].Q = Q1
        t.IdIndex[5].Q = Q2
        t.assignQ(Q3) 
        result = [i.Q for i in t.traverse(self_after=True)]
        assert t.Q is Q3
        self.assertEqual(result, [Q3,Q2,Q2,Q2,Q3,Q1,Q1,Q1,Q3])

    def test_assignQ_multiple_overwrite(self):
        """RangeNode assignQ should allow overwrite"""
        t = self.t1
        Q1 = ['a']
        Q2 = ['b']
        Q3 = ['c']
        t.makeIdIndex()
        t.IdIndex[7].Q = Q1
        t.IdIndex[5].Q = Q2
        t.assignQ(Q3, overwrite=True)
        for i in t.traverse(self_after=True):
            assert i.Q is Q3

    def test_assignQ_special(self):
       """RangeNode assignQ should work with special Qs"""
       t = self.t1
       Q1 = 'a'
       Q2 = 'b'
       Q3 = 'c'
       t.makeIdIndex()
       special = {7:Q1, 1:Q2}
       #won't work if no Q at root
       self.assertRaises(ValueError, t.assignQ, special_qs=special)
       t.assignQ(Q3, special_qs=special)
       result = [i.Q for i in t.traverse(self_after=True)]
       self.assertEqual(result, ['c','b','c','c','c','a','a','a','c'])
        

    def test_assignP(self):
        """RangeNode assignP should work when Qs set."""
        t = self.t1
        for i in t.traverse(self_before=True):
            i.Length = random() * 0.5 #range 0 to 0.5
        t.Q = Rates.random(DnaPairs)
        t.assignQ()
        t.assignP()
        t.assignIds()
#.........这里部分代码省略.........
开发者ID:miklou,项目名称:pycogent,代码行数:103,代码来源:test_tree.py


注:本文中的cogent.parse.tree.DndParser.traverse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。