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


Python TreeNode.from_newick方法代码示例

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


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

示例1: test_majority_rule

# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_newick [as 别名]
    def test_majority_rule(self):
        trees = [
            TreeNode.from_newick("(A,(B,(H,(D,(J,(((G,E),(F,I)),C))))));"),
            TreeNode.from_newick("(A,(B,(D,((J,H),(((G,E),(F,I)),C)))));"),
            TreeNode.from_newick("(A,(B,(D,(H,(J,(((G,E),(F,I)),C))))));"),
            TreeNode.from_newick("(A,(B,(E,(G,((F,I),((J,(H,D)),C))))));"),
            TreeNode.from_newick("(A,(B,(E,(G,((F,I),(((J,H),D),C))))));"),
            TreeNode.from_newick("(A,(B,(E,((F,I),(G,((J,(H,D)),C))))));"),
            TreeNode.from_newick("(A,(B,(E,((F,I),(G,(((J,H),D),C))))));"),
            TreeNode.from_newick("(A,(B,(E,((G,(F,I)),((J,(H,D)),C)))));"),
            TreeNode.from_newick("(A,(B,(E,((G,(F,I)),(((J,H),D),C)))));")]

        exp = TreeNode.from_newick("(((E,(G,(F,I),(C,(D,J,H)))),B),A);")
        obs = majority_rule(trees)
        self.assertEqual(exp.compare_subsets(obs[0]), 0.0)
        self.assertEqual(len(obs), 1)

        tree = obs[0]
        exp_supports = sorted([9.0, 9.0, 9.0, 6.0, 6.0, 6.0])
        obs_supports = sorted([n.support for n in tree.non_tips()])
        self.assertEqual(obs_supports, exp_supports)

        obs = majority_rule(trees, weights=np.ones(len(trees)) * 2)
        self.assertEqual(exp.compare_subsets(obs[0]), 0.0)
        self.assertEqual(len(obs), 1)

        tree = obs[0]
        exp_supports = sorted([18.0, 18.0, 12.0, 18.0, 12.0, 12.0])
        obs_supports = sorted([n.support for n in tree.non_tips()])

        with self.assertRaises(ValueError):
            majority_rule(trees, weights=[1, 2])
开发者ID:nbresnick,项目名称:scikit-bio,代码行数:34,代码来源:test_majority_rule.py

示例2: test_DndParser

# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_newick [as 别名]
    def test_DndParser(self):
        """DndParser tests"""
        t_str = "(A_a,(B:1.0,C),'D_e':0.5)E;"
        tree_unesc = TreeNode.from_newick(t_str, unescape_name=True)
        tree_esc = TreeNode.from_newick(t_str, unescape_name=False)

        self.assertEqual(tree_unesc.name, 'E')
        self.assertEqual(tree_unesc.children[0].name, 'A a')
        self.assertEqual(tree_unesc.children[1].children[0].name, 'B')
        self.assertEqual(tree_unesc.children[1].children[0].length, 1.0)
        self.assertEqual(tree_unesc.children[1].children[1].name, 'C')
        self.assertEqual(tree_unesc.children[2].name, 'D_e')
        self.assertEqual(tree_unesc.children[2].length, 0.5)

        self.assertEqual(tree_esc.name, 'E')
        self.assertEqual(tree_esc.children[0].name, 'A_a')
        self.assertEqual(tree_esc.children[1].children[0].name, 'B')
        self.assertEqual(tree_esc.children[1].children[0].length, 1.0)
        self.assertEqual(tree_esc.children[1].children[1].name, 'C')
        self.assertEqual(tree_esc.children[2].name, "'D_e'")
        self.assertEqual(tree_esc.children[2].length, 0.5)

        reload_test = tree_esc.to_newick(with_distances=True,
                                         escape_name=False)
        obs = TreeNode.from_newick(reload_test, unescape_name=False)
        self.assertEqual(obs.to_newick(with_distances=True),
                         tree_esc.to_newick(with_distances=True))
        reload_test = tree_unesc.to_newick(with_distances=True,
                                           escape_name=False)
        obs = TreeNode.from_newick(reload_test, unescape_name=False)
        self.assertEqual(obs.to_newick(with_distances=True),
                         tree_unesc.to_newick(with_distances=True))
开发者ID:gblanchard4,项目名称:scikit-bio,代码行数:34,代码来源:test_tree.py

示例3: test_index_tree

# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_newick [as 别名]
    def test_index_tree(self):
        """index_tree should produce correct index and node map"""
        # test for first tree: contains singleton outgroup
        t1 = TreeNode.from_newick('(((a,b),c),(d,e))')
        t2 = TreeNode.from_newick('(((a,b),(c,d)),(e,f))')
        t3 = TreeNode.from_newick('(((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)])
开发者ID:gblanchard4,项目名称:scikit-bio,代码行数:30,代码来源:test_tree.py

示例4: setUp

# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_newick [as 别名]
    def setUp(self):
        """Prep the self"""
        self.simple_t = TreeNode.from_newick("((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.from_newick("(((a,b)int1,(x,y,(w,z)int2,"
                                                 "(c,d)int3)int4),(e,f)int5);")
开发者ID:gblanchard4,项目名称:scikit-bio,代码行数:28,代码来源:test_tree.py

示例5: test_backfill_names_gap

# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_newick [as 别名]
    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'])
开发者ID:IUEayhu,项目名称:tax2tree,代码行数:37,代码来源:test_nlevel.py

示例6: test_compare_subsets

# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_newick [as 别名]
    def test_compare_subsets(self):
        """compare_subsets should return the fraction of shared subsets"""
        t = TreeNode.from_newick('((H,G),(R,M));')
        t2 = TreeNode.from_newick('(((H,G),R),M);')
        t4 = TreeNode.from_newick('(((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)
开发者ID:gblanchard4,项目名称:scikit-bio,代码行数:28,代码来源:test_tree.py

示例7: test_compare_tip_distances

# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_newick [as 别名]
 def test_compare_tip_distances(self):
     t = TreeNode.from_newick('((H:1,G:1):2,(R:0.5,M:0.7):3);')
     t2 = TreeNode.from_newick('(((H:1,G:1,O:1):2,R:3):1,X:4);')
     obs = t.compare_tip_distances(t2)
     # note: common taxa are H, G, R (only)
     m1 = np.array([[0, 2, 6.5], [2, 0, 6.5], [6.5, 6.5, 0]])
     m2 = np.array([[0, 2, 6], [2, 0, 6], [6, 6, 0]])
     r = pearsonr(m1.flat, m2.flat)[0]
     self.assertAlmostEqual(obs, (1 - r) / 2)
开发者ID:gblanchard4,项目名称:scikit-bio,代码行数:11,代码来源:test_tree.py

示例8: test_get_nearest_named_ancestor

# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_newick [as 别名]
 def test_get_nearest_named_ancestor(self):
     """correctly get the nearest named ancestor"""
     t = TreeNode.from_newick("(((s1,s2)g1,s3))root;")
     t2 = TreeNode.from_newick("(((s1,s2)g1,s3));")
     exp_t = t
     exp_t2 = None
     obs_t = get_nearest_named_ancestor(t.find('s3'))
     obs_t2 = get_nearest_named_ancestor(t2.find('s3'))
     self.assertEqual(obs_t, exp_t)
     self.assertEqual(obs_t2, exp_t2)
开发者ID:IUEayhu,项目名称:tax2tree,代码行数:12,代码来源:test_nlevel.py

示例9: setUp

# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_newick [as 别名]
    def setUp(self):
        data1 = [[0,  5,  9,  9,  8],
                 [5,  0, 10, 10,  9],
                 [9, 10,  0,  8,  7],
                 [9, 10,  8,  0,  3],
                 [8,  9,  7,  3,  0]]
        ids1 = list('abcde')
        self.dm1 = DistanceMatrix(data1, ids1)
        # this newick string was confirmed against http://www.trex.uqam.ca/
        # which generated the following (isomorphic) newick string:
        # (d:2.0000,e:1.0000,(c:4.0000,(a:2.0000,b:3.0000):3.0000):2.0000);
        self.expected1_str = ("(d:2.000000, (c:4.000000, (b:3.000000,"
                              " a:2.000000):3.000000):2.000000, e:1.000000);")
        self.expected1_TreeNode = TreeNode.from_newick(self.expected1_str)

        # this example was pulled from the Phylip manual
        # http://evolution.genetics.washington.edu/phylip/doc/neighbor.html
        data2 = [[0.0000, 1.6866, 1.7198, 1.6606, 1.5243, 1.6043, 1.5905],
                 [1.6866, 0.0000, 1.5232, 1.4841, 1.4465, 1.4389, 1.4629],
                 [1.7198, 1.5232, 0.0000, 0.7115, 0.5958, 0.6179, 0.5583],
                 [1.6606, 1.4841, 0.7115, 0.0000, 0.4631, 0.5061, 0.4710],
                 [1.5243, 1.4465, 0.5958, 0.4631, 0.0000, 0.3484, 0.3083],
                 [1.6043, 1.4389, 0.6179, 0.5061, 0.3484, 0.0000, 0.2692],
                 [1.5905, 1.4629, 0.5583, 0.4710, 0.3083, 0.2692, 0.0000]]
        ids2 = ["Bovine", "Mouse", "Gibbon", "Orang", "Gorilla", "Chimp",
                "Human"]
        self.dm2 = DistanceMatrix(data2, ids2)
        self.expected2_str = ("(Mouse:0.76891, (Gibbon:0.35793, (Orang:0.28469"
                              ", (Gorilla:0.15393, (Chimp:0.15167, Human:0.117"
                              "53):0.03982):0.02696):0.04648):0.42027, Bovine:"
                              "0.91769);")
        self.expected2_TreeNode = TreeNode.from_newick(self.expected2_str)

        data3 = [[0, 5, 4, 7, 6, 8],
                 [5, 0, 7, 10, 9, 11],
                 [4, 7, 0, 7, 6, 8],
                 [7, 10, 7, 0, 5, 8],
                 [6, 9, 6, 5, 0, 8],
                 [8, 11, 8, 8, 8, 0]]
        ids3 = map(str, range(6))
        self.dm3 = DistanceMatrix(data3, ids3)
        self.expected3_str = ("((((0:1.000000,1:4.000000):1.000000,2:2.000000"
                              "):1.250000,5:4.750000):0.750000,3:2.750000,4:2."
                              "250000);")
        self.expected3_TreeNode = TreeNode.from_newick(self.expected3_str)

        # this dm can yield negative branch lengths
        data4 = [[0,  5,  9,  9,  800],
                 [5,  0, 10, 10,  9],
                 [9, 10,  0,  8,  7],
                 [9, 10,  8,  0,  3],
                 [800,  9,  7,  3,  0]]
        ids4 = list('abcde')
        self.dm4 = DistanceMatrix(data4, ids4)
开发者ID:ResilientScience,项目名称:scikit-bio,代码行数:56,代码来源:test_nj.py

示例10: test_walk_clades

# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_newick [as 别名]
    def test_walk_clades(self):
        trees = [TreeNode.from_newick("((A,B),(D,E));"),
                 TreeNode.from_newick("((A,B),(D,(E,X)));")]
        exp_clades = [
            (frozenset(['A']), 2.0),
            (frozenset(['B']), 2.0),
            (frozenset(['A', 'B']), 2.0),
            (frozenset(['D', 'E']), 1.0),
            (frozenset(['D', 'E', 'A', 'B']), 1.0),
            (frozenset(['D']), 2.0),
            (frozenset(['E']), 2.0),
            (frozenset(['X']), 1.0),
            (frozenset(['E', 'X']), 1.0),
            (frozenset(['D', 'E', 'X']), 1.0),
            (frozenset(['A', 'B', 'D', 'E', 'X']), 1.0)]

        exp_lengths_nolength = {
            frozenset(['A']): None,
            frozenset(['B']): None,
            frozenset(['A', 'B']): None,
            frozenset(['D', 'E']): None,
            frozenset(['D', 'E', 'A', 'B']): None,
            frozenset(['D']): None,
            frozenset(['E']): None,
            frozenset(['X']): None,
            frozenset(['E', 'X']): None,
            frozenset(['D', 'E', 'X']): None,
            frozenset(['A', 'B', 'D', 'E', 'X']): None}

        exp_lengths = {
            frozenset(['A']): 2.0,
            frozenset(['B']): 2.0,
            frozenset(['A', 'B']): 2.0,
            frozenset(['D', 'E']): 1.0,
            frozenset(['D', 'E', 'A', 'B']): 1.0,
            frozenset(['D']): 2.0,
            frozenset(['E']): 2.0,
            frozenset(['X']): 1.0,
            frozenset(['E', 'X']): 1.0,
            frozenset(['D', 'E', 'X']): 1.0,
            frozenset(['A', 'B', 'D', 'E', 'X']): 1.0}

        obs_clades, obs_lengths = _walk_clades(trees, np.ones(len(trees)))
        self.assertEqual(set(obs_clades), set(exp_clades))
        self.assertEqual(obs_lengths, exp_lengths_nolength)

        for t in trees:
            for n in t.traverse(include_self=True):
                n.length = 2.0

        obs_clades, obs_lengths = _walk_clades(trees, np.ones(len(trees)))

        self.assertEqual(set(obs_clades), set(exp_clades))
        self.assertEqual(obs_lengths, exp_lengths)
开发者ID:nbresnick,项目名称:scikit-bio,代码行数:56,代码来源:test_majority_rule.py

示例11: test_data

# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_newick [as 别名]
 def test_data(self):
     """DndParser should work as expected on real data"""
     t = TreeNode.from_newick(sample)
     self.assertEqual(
         str(t), '((xyz:0.28124,(def:0.24498,mno:0.03627):0.1771):0.0487,'
                 'abc:0.05925,(ghi:0.06914,jkl:0.13776):0.09853);')
     tdata = TreeNode.from_newick(node_data_sample, unescape_name=True)
     self.assertEqual(
         str(tdata), "((xyz:0.28124,(def:0.24498,mno:0.03627)A:0.1771)"
                     "B:0.0487,abc:0.05925,(ghi:0.06914,jkl:0.13776)"
                     "C:0.09853);")
开发者ID:gblanchard4,项目名称:scikit-bio,代码行数:13,代码来源:test_tree.py

示例12: test_find_by_id

# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_newick [as 别名]
    def test_find_by_id(self):
        """Find a node by id"""
        t1 = TreeNode.from_newick("((,),(,,));")
        t2 = TreeNode.from_newick("((,),(,,));")

        exp = t1.children[1]
        obs = t1.find_by_id(6)  # right inner node with 3 children
        self.assertEqual(obs, exp)

        exp = t2.children[1]
        obs = t2.find_by_id(6)  # right inner node with 3 children
        self.assertEqual(obs, exp)

        with self.assertRaises(MissingNodeError):
            t1.find_by_id(100)
开发者ID:gblanchard4,项目名称:scikit-bio,代码行数:17,代码来源:test_tree.py

示例13: test_lowest_common_ancestor

# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_newick [as 别名]
    def test_lowest_common_ancestor(self):
        """TreeNode lowestCommonAncestor should return LCA for set of tips"""
        t1 = TreeNode.from_newick("((a,(b,c)d)e,f,(g,h)i)j;")
        t2 = t1.copy()
        t3 = t1.copy()
        t4 = t1.copy()
        input1 = ['a']  # return self
        input2 = ['a', 'b']  # return e
        input3 = ['b', 'c']  # return d
        input4 = ['a', 'h', 'g']  # return j
        exp1 = t1.find('a')
        exp2 = t2.find('e')
        exp3 = t3.find('d')
        exp4 = t4
        obs1 = t1.lowest_common_ancestor(input1)
        obs2 = t2.lowest_common_ancestor(input2)
        obs3 = t3.lowest_common_ancestor(input3)
        obs4 = t4.lowest_common_ancestor(input4)
        self.assertEqual(obs1, exp1)
        self.assertEqual(obs2, exp2)
        self.assertEqual(obs3, exp3)
        self.assertEqual(obs4, exp4)

        # verify multiple calls work
        t_mul = t1.copy()
        exp_1 = t_mul.find('d')
        exp_2 = t_mul.find('i')
        obs_1 = t_mul.lowest_common_ancestor(['b', 'c'])
        obs_2 = t_mul.lowest_common_ancestor(['g', 'h'])
        self.assertEqual(obs_1, exp_1)
        self.assertEqual(obs_2, exp_2)

        # empty case
        with self.assertRaises(ValueError):
            t1.lowest_common_ancestor([])
开发者ID:gblanchard4,项目名称:scikit-bio,代码行数:37,代码来源:test_tree.py

示例14: test_get_max_distance

# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_newick [as 别名]
 def test_get_max_distance(self):
     """get_max_distance should get max tip distance across tree"""
     tree = TreeNode.from_newick(
         "((a:0.1,b:0.2)c:0.3,(d:0.4,e:0.5)f:0.6)root;")
     dist, nodes = tree.get_max_distance()
     nptest.assert_almost_equal(dist, 1.6)
     self.assertEqual(sorted([n.name for n in nodes]), ['b', 'e'])
开发者ID:gblanchard4,项目名称:scikit-bio,代码行数:9,代码来源:test_tree.py

示例15: test_find_by_func

# 需要导入模块: from skbio import TreeNode [as 别名]
# 或者: from skbio.TreeNode import from_newick [as 别名]
 def test_find_by_func(self):
     """Find nodes by a function"""
     t = TreeNode.from_newick("((a,b)c,(d,e)f);")
     func = lambda x: x.parent == t.find('c')
     exp = ['a', 'b']
     obs = [n.name for n in t.find_by_func(func)]
     self.assertEqual(obs, exp)
开发者ID:gblanchard4,项目名称:scikit-bio,代码行数:9,代码来源:test_tree.py


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