예제 #1
0
    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
0
    def test_tip_tip_distances_missing_length(self):
        t = TreeNode.read(StringIO(u"((a,b:6)c:4,(d,e:0)f);"))
        exp_t = TreeNode.read(StringIO(u"((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)
예제 #3
0
    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)
예제 #4
0
    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])
        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]]))
예제 #5
0
    def test_compare_subsets(self):
        """compare_subsets should return the fraction of shared subsets"""
        t = TreeNode.read(StringIO(u'((H,G),(R,M));'))
        t2 = TreeNode.read(StringIO(u'(((H,G),R),M);'))
        t4 = TreeNode.read(StringIO(u'(((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)
예제 #6
0
    def test_majority_rule(self):
        trees = [
            TreeNode.read(StringIO("(A,(B,(H,(D,(J,(((G,E),(F,I)),C))))));")),
            TreeNode.read(StringIO("(A,(B,(D,((J,H),(((G,E),(F,I)),C)))));")),
            TreeNode.read(StringIO("(A,(B,(D,(H,(J,(((G,E),(F,I)),C))))));")),
            TreeNode.read(StringIO("(A,(B,(E,(G,((F,I),((J,(H,D)),C))))));")),
            TreeNode.read(StringIO("(A,(B,(E,(G,((F,I),(((J,H),D),C))))));")),
            TreeNode.read(StringIO("(A,(B,(E,((F,I),(G,((J,(H,D)),C))))));")),
            TreeNode.read(StringIO("(A,(B,(E,((F,I),(G,(((J,H),D),C))))));")),
            TreeNode.read(StringIO("(A,(B,(E,((G,(F,I)),((J,(H,D)),C)))));")),
            TreeNode.read(StringIO("(A,(B,(E,((G,(F,I)),(((J,H),D),C)))));"))
        ]

        exp = TreeNode.read(StringIO("(((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])
예제 #7
0
    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)
예제 #8
0
 def test_newick_to_tree_node_invalid_files(self):
     for invalid, error_fragments in self.invalid_newicks:
         fh = StringIO(invalid)
         with self.assertRaises(NewickFormatError) as cm:
             _newick_to_tree_node(fh)
         for frag in error_fragments:
             self.assertIn(frag, str(cm.exception))
         fh.close()
예제 #9
0
 def test_newick_to_tree_node_invalid_files(self):
     for invalid, error_fragments in self.invalid_newicks:
         fh = StringIO(invalid)
         with self.assertRaises(NewickFormatError) as cm:
             _newick_to_tree_node(fh)
         for frag in error_fragments:
             self.assertIn(frag, str(cm.exception))
         fh.close()
예제 #10
0
    def test_tree_node_to_newick(self):
        for tree, newicks in self.trees_newick_lists:
            newick = newicks[0]
            fh = StringIO()
            _tree_node_to_newick(tree, fh)

            self.assertEqual(newick, fh.getvalue())

            fh.close()
예제 #11
0
    def test_newick_to_tree_node_valid_files(self):
        for tree, newicks in self.trees_newick_lists:
            for newick in newicks:
                fh = StringIO(newick)
                read_tree = _newick_to_tree_node(fh)

                self._assert_equal(tree, read_tree)

                fh.close()
예제 #12
0
    def test_tree_node_to_newick(self):
        for tree, newicks in self.trees_newick_lists:
            newick = newicks[0]
            fh = StringIO()
            _tree_node_to_newick(tree, fh)

            self.assertEqual(newick, fh.getvalue())

            fh.close()
예제 #13
0
    def test_newick_to_tree_node_valid_files(self):
        for tree, newicks in self.trees_newick_lists:
            for newick in newicks:
                fh = StringIO(newick)
                read_tree = _newick_to_tree_node(fh)

                self._assert_equal(tree, read_tree)

                fh.close()
예제 #14
0
 def test_compare_tip_distances(self):
     t = TreeNode.read(StringIO(u'((H:1,G:1):2,(R:0.5,M:0.7):3);'))
     t2 = TreeNode.read(StringIO(u'(((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)
예제 #15
0
    def test_tip_tip_distances_no_length(self):
        t = TreeNode.read(StringIO(u"((a,b)c,(d,e)f);"))
        exp_t = TreeNode.read(StringIO(u"((a:0,b:0)c:0,(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)

        for node in t.preorder():
            self.assertIs(node.length, None)
예제 #16
0
    def test_root_at_midpoint_tie(self):
        nwk = u"(((a:1,b:1)c:2,(d:3,e:4)f:5),g:1)root;"
        t = TreeNode.read(StringIO(nwk))
        exp = u"((d:3,e:4)f:2,((a:1,b:1)c:2,(g:1)):3)root;"
        texp = TreeNode.read(StringIO(exp))

        obs = t.root_at_midpoint()

        for o, e in zip(obs.traverse(), texp.traverse()):
            self.assertEqual(o.name, e.name)
            self.assertEqual(o.length, e.length)
예제 #17
0
    def test_walk_clades(self):
        trees = [
            TreeNode.read(StringIO("((A,B),(D,E));")),
            TreeNode.read(StringIO("((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)
예제 #18
0
 def test_newick_to_tree_node_convert_underscores(self):
     fh = StringIO('(_:0.1, _a, _b)__;')
     tree = _newick_to_tree_node(fh, convert_underscores=False)
     fh2 = StringIO()
     _tree_node_to_newick(tree, fh2)
     self.assertEqual(fh2.getvalue(), "('_':0.1,'_a','_b')'__';\n")
     fh2.close()
     fh.close()
예제 #19
0
    def test_bifurcate(self):
        t1 = TreeNode.read(StringIO(u'(((a,b),c),(d,e));'))
        t2 = TreeNode.read(StringIO(u'((a,b,c));'))
        t3 = t2.copy()

        t1.bifurcate()
        t2.bifurcate()
        t3.bifurcate(insert_length=0)

        self.assertEqual(str(t1), '(((a,b),c),(d,e));\n')
        self.assertEqual(str(t2), '((c,(a,b)));\n')
        self.assertEqual(str(t3), '((c,(a,b):0));\n')
예제 #20
0
    def test_find_by_id(self):
        """Find a node by id"""
        t1 = TreeNode.read(StringIO(u"((,),(,,));"))
        t2 = TreeNode.read(StringIO(u"((,),(,,));"))

        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)
예제 #21
0
    def setUp(self):
        self.table1 = np.array([[1, 3, 0, 1, 0], [0, 2, 0, 4, 4],
                                [0, 0, 6, 2, 1], [0, 0, 1, 1, 1]])
        self.sids1 = list('ABCD')
        self.oids1 = ['OTU%d' % i for i in range(1, 6)]
        self.tree1 = TreeNode.read(
            StringIO(u'(((((OTU1:0.5,OTU2:0.5):0.5,OTU3:1.0):1.0):'
                     u'0.0,(OTU4:0.75,OTU5:0.75):1.25):0.0)root;'))

        self.table2 = np.array([[1, 3], [0, 2], [0, 0]])
        self.sids2 = list('xyz')
        self.oids2 = ['OTU1', 'OTU5']
        self.tree2 = TreeNode.read(
            StringIO(u'(((((OTU1:42.5,OTU2:0.5):0.5,OTU3:1.0):1.0):'
                     u'0.0,(OTU4:0.75,OTU5:0.0001):1.25):0.0)root;'))
예제 #22
0
    def test_compare_tip_distances_sample(self):
        t = TreeNode.read(StringIO(u'((H:1,G:1):2,(R:0.5,M:0.7):3);'))
        t2 = TreeNode.read(StringIO(u'(((H:1,G:1,O:1):2,R:3):1,X:4);'))
        obs = t.compare_tip_distances(t2, sample=3, shuffle_f=sorted)
        # 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)

        # 4 common taxa, still picking H, G, R
        s = u'((H:1,G:1):2,(R:0.5,M:0.7,Q:5):3);'
        t = TreeNode.read(StringIO(s))
        s3 = u'(((H:1,G:1,O:1):2,R:3,Q:10):1,X:4);'
        t3 = TreeNode.read(StringIO(s3))
        obs = t.compare_tip_distances(t3, sample=3, shuffle_f=sorted)
예제 #23
0
 def test_get_max_distance(self):
     """get_max_distance should get max tip distance across tree"""
     tree = TreeNode.read(
         StringIO(u"((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()
     npt.assert_almost_equal(dist, 1.6)
     self.assertEqual(sorted([n.name for n in nodes]), ['b', 'e'])
예제 #24
0
    def test_lowest_common_ancestor(self):
        """TreeNode lowestCommonAncestor should return LCA for set of tips"""
        t1 = TreeNode.read(StringIO(u"((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([])
예제 #25
0
 def test_vectorize_counts_and_tree(self):
     t = TreeNode.read(StringIO(u"((a:1, b:2)c:3)root;"))
     counts = np.array([[0, 1], [1, 5], [10, 1]])
     count_array, indexed, branch_lengths = \
         _vectorize_counts_and_tree(counts, np.array(['a', 'b']), t)
     exp_counts = np.array([[0, 1, 10], [1, 5, 1], [1, 6, 11], [1, 6, 11]])
     npt.assert_equal(count_array, exp_counts.T)
예제 #26
0
 def test_set_max_distance(self):
     """set_max_distance sets MaxDistTips across tree"""
     tree = TreeNode.read(
         StringIO(u"((a:0.1,b:0.2)c:0.3,(d:0.4,e:0.5)f:0.6)root;"))
     tree._set_max_distance()
     tip_a, tip_b = tree.MaxDistTips
     self.assertEqual(tip_a[0] + tip_b[0], 1.6)
     self.assertEqual(sorted([tip_a[1].name, tip_b[1].name]), ['b', 'e'])
예제 #27
0
    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.read(StringIO(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.read(StringIO(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.read(StringIO(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)
예제 #28
0
    def test_assign_ids(self):
        """Assign IDs to the tree"""
        t1 = TreeNode.read(StringIO(u"(((a,b),c),(e,f),(g));"))
        t2 = TreeNode.read(StringIO(u"(((a,b),c),(e,f),(g));"))
        t3 = TreeNode.read(StringIO(u"((g),(e,f),(c,(a,b)));"))
        t1_copy = t1.copy()

        t1.assign_ids()
        t2.assign_ids()
        t3.assign_ids()
        t1_copy.assign_ids()

        self.assertEqual([(n.name, n.id) for n in t1.traverse()],
                         [(n.name, n.id) for n in t2.traverse()])
        self.assertEqual([(n.name, n.id) for n in t1.traverse()],
                         [(n.name, n.id) for n in t1_copy.traverse()])
        self.assertNotEqual([(n.name, n.id) for n in t1.traverse()],
                            [(n.name, n.id) for n in t3.traverse()])
예제 #29
0
    def test_compare_rfd(self):
        """compare_rfd should return the Robinson Foulds distance"""
        t = TreeNode.read(StringIO(u'((H,G),(R,M));'))
        t2 = TreeNode.read(StringIO(u'(((H,G),R),M);'))
        t4 = TreeNode.read(StringIO(u'(((H,G),(O,R)),X);'))

        obs = t.compare_rfd(t2)
        exp = 2.0
        self.assertEqual(obs, exp)

        self.assertEqual(t.compare_rfd(t2), t2.compare_rfd(t))

        obs = t.compare_rfd(t2, proportion=True)
        exp = 0.5
        self.assertEqual(obs, exp)

        with self.assertRaises(ValueError):
            t.compare_rfd(t4)
예제 #30
0
    def test_roundtrip(self):
        for tree, newicks in self.trees_newick_lists:
            newick = newicks[0]
            fh = StringIO(newick)
            tree = _newick_to_tree_node(fh)
            fh2 = StringIO()
            _tree_node_to_newick(tree, fh2)
            fh2.seek(0)
            tree2 = _newick_to_tree_node(fh2)

            self.assertEqual(newick, fh2.getvalue())
            self._assert_equal(tree, tree2)

            fh.close()
            fh2.close()
예제 #31
0
 def test_newick_to_tree_node_convert_underscores(self):
     fh = StringIO('(_:0.1, _a, _b)__;')
     tree = _newick_to_tree_node(fh, convert_underscores=False)
     fh2 = StringIO()
     _tree_node_to_newick(tree, fh2)
     self.assertEqual(fh2.getvalue(), "('_':0.1,'_a','_b')'__';\n")
     fh2.close()
     fh.close()
예제 #32
0
    def test_invalidate_attr_caches(self):
        tree = TreeNode.read(StringIO(u"((a,b,(c,d)e)f,(g,h)i)root;"))

        def f(n):
            return [n.name] if n.is_tip() else []

        tree.cache_attr(f, 'tip_names')
        tree.invalidate_caches()
        for n in tree.traverse(include_self=True):
            self.assertFalse(hasattr(n, 'tip_names'))
예제 #33
0
    def test_root_at(self):
        """Form a new root"""
        t = TreeNode.read(StringIO(u"(((a,b)c,(d,e)f)g,h)i;"))
        with self.assertRaises(TreeError):
            t.root_at(t.find('h'))

        exp = "(a,b,((d,e)f,(h)g)c)root;\n"
        rooted = t.root_at('c')
        obs = str(rooted)
        self.assertEqual(obs, exp)
예제 #34
0
 def test_has_children(self):
     """Test if has children"""
     t = TreeNode.read(StringIO(u"((a,b)c,(d,e)f);"))
     self.assertTrue(t.has_children())
     self.assertTrue(t.children[0].has_children())
     self.assertTrue(t.children[1].has_children())
     self.assertFalse(t.children[0].children[0].has_children())
     self.assertFalse(t.children[0].children[1].has_children())
     self.assertFalse(t.children[1].children[0].has_children())
     self.assertFalse(t.children[1].children[1].has_children())
예제 #35
0
    def test_find_by_func(self):
        """Find nodes by a function"""
        t = TreeNode.read(StringIO(u"((a,b)c,(d,e)f);"))

        def func(x):
            return x.parent == t.find('c')

        exp = ['a', 'b']
        obs = [n.name for n in t.find_by_func(func)]
        self.assertEqual(obs, exp)
예제 #36
0
    def test_roundtrip_read_write(self):
        for reader_fn, writer_fn, fhs in (
            (_lsmat_to_dissimilarity_matrix, _dissimilarity_matrix_to_lsmat, self.dissim_fhs),
            (_lsmat_to_distance_matrix, _distance_matrix_to_lsmat, self.dist_fhs),
        ):
            for fh in fhs:
                # Read.
                fh.seek(0)
                lsmat1 = reader_fn(fh)

                # Write.
                out_fh = StringIO()
                writer_fn(lsmat1, out_fh)
                out_fh.seek(0)

                # Read.
                lsmat2 = reader_fn(out_fh)
                out_fh.close()

                self.assertEqual(lsmat1, lsmat2)
예제 #37
0
    def test_write(self):
        for fn, objs, strs in (
            (_dissimilarity_matrix_to_lsmat, self.dissim_objs, self.dissim_strs),
            (_distance_matrix_to_lsmat, self.dist_objs, self.dist_strs),
        ):
            for obj, str_ in zip(objs, strs):
                fh = StringIO()
                fn(obj, fh)
                obs = fh.getvalue()
                fh.close()
                self.assertEqual(obs, str_)

        # Test writing CSV (TSV is written above).
        for fn, cls in (
            (_dissimilarity_matrix_to_lsmat, DissimilarityMatrix),
            (_distance_matrix_to_lsmat, DistanceMatrix),
        ):
            obj = cls(self.lsmat_3x3_data, ["a", "b", "c"])
            fh = StringIO()
            fn(obj, fh, delimiter=",")
            obs = fh.getvalue()
            fh.close()
            self.assertEqual(obs, LSMat_3x3_CSV)
예제 #38
0
 def test_newick_sniffer_valid_files(self):
     for _, newicks in self.trees_newick_lists:
         for newick in newicks:
             fh = StringIO(newick)
             self.assertEqual(_newick_sniffer(fh), (True, {}))
             fh.close()
예제 #39
0
 def test_newick_sniffer_invalid_files(self):
     for invalid, _ in self.invalid_newicks:
         fh = StringIO(invalid)
         self.assertEqual(_newick_sniffer(fh), (False, {}))
         fh.close()