Esempio n. 1
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])
Esempio n. 2
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])
Esempio n. 3
0
    def consensusTrees(self, iterations, stddev=None):
        """
        Make new perturbed distance matrices, create NJ trees from them, and
        calculate consensus trees based on our original tree and the new ones.

        @param iterations: The C{int} number of times to create a new NJ tree
            via a perturbed distance matrix.
        @param stddev: The C{float} standard deviation of the noise to add to
            off-diagonal distance matrix elements before creating a new NJ
            tree to add to the support.
        @return: A C{list} of new C{NJTree} instances, holding the consensus
            trees produced by C{skbio.tree.majority_rule}.
        """
        trees = [self.tree] + list(self.generateTrees(iterations, stddev))
        result = []
        for tree in majority_rule(trees):
            new = NJTree()
            new.labels = self.labels
            new.sequences = self.sequences
            new.distance = self.distance
            new.supportIterations += iterations + 1
            new.tree = tree
            result.append(new)

        return result
Esempio n. 4
0
    def test_majority_rule_tree_node_class(self):
        class TreeNodeSubclass(TreeNode):
            pass

        trees = [
            TreeNode.read(io.StringIO("((a,b),(c,d),(e,f));")),
            TreeNode.read(io.StringIO("(a,(c,d),b,(e,f));")),
            TreeNode.read(io.StringIO("((c,d),(e,f),b);")),
            TreeNode.read(io.StringIO("(a,(c,d),(e,f));"))
        ]

        trees = majority_rule(trees, tree_node_class=TreeNodeSubclass)
        self.assertEqual(len(trees), 4)

        for tree in trees:
            self.assertIs(type(tree), TreeNodeSubclass)

        exp = set([
            frozenset(['a']),
            frozenset(['b']),
            frozenset([None, 'c', 'd']),
            frozenset([None, 'e', 'f'])
        ])

        obs = set([frozenset([n.name for n in t.traverse()]) for t in trees])
        self.assertEqual(obs, exp)
Esempio n. 5
0
def main():
    option_parser, opts, args = parse_command_line_parameters(**script_info)

    trees = load_tree_files(opts.input_dir)

    # this retains the default behavior from PyCogent's majority_rule function
    if opts.strict:
        cutoff = 0.5
    else:
        cutoff = 0.0
    consensus = majority_rule(trees=trees, cutoff=cutoff)[0]

    f = open(opts.output_fname, 'w')
    f.write(consensus.to_newick(with_distances=True))
    f.close()
Esempio n. 6
0
    def test_majority_rule_multiple_trees(self):
        trees = [
            TreeNode.read(StringIO("((a,b),(c,d),(e,f));")),
            TreeNode.read(StringIO("(a,(c,d),b,(e,f));")),
            TreeNode.read(StringIO("((c,d),(e,f),b);")),
            TreeNode.read(StringIO("(a,(c,d),(e,f));"))]

        trees = majority_rule(trees)
        self.assertEqual(len(trees), 4)

        exp = set([
                  frozenset(['a']),
                  frozenset(['b']),
                  frozenset([None, 'c', 'd']),
                  frozenset([None, 'e', 'f'])])

        obs = set([frozenset([n.name for n in t.traverse()]) for t in trees])
        self.assertEqual(obs, exp)
    def test_majority_rule_multiple_trees(self):
        trees = [
            TreeNode.read(StringIO("((a,b),(c,d),(e,f));")),
            TreeNode.read(StringIO("(a,(c,d),b,(e,f));")),
            TreeNode.read(StringIO("((c,d),(e,f),b);")),
            TreeNode.read(StringIO("(a,(c,d),(e,f));"))]

        trees = majority_rule(trees)
        self.assertEqual(len(trees), 4)

        exp = set([
                  frozenset(['a']),
                  frozenset(['b']),
                  frozenset([None, 'c', 'd']),
                  frozenset([None, 'e', 'f'])])

        obs = set([frozenset([n.name for n in t.traverse()]) for t in trees])
        self.assertEqual(obs, exp)
Esempio n. 8
0
    def _get_bootstrap_consensus_tree(self) -> Phylo:
        """Get the consensus tree.

        :return: The consensus tree of the list of newick trees.
        """
        # Create the StringIO newick tree holder.
        newick_tree = StringIO()

        # Find the consensus tree among all trees.
        consensus_tree = majority_rule(trees=self._get_bootstrap_trees(),
                                       cutoff=self._bct_option.cutoff)

        # Grab the tree from the returned list and convert it to newick format.
        consensus_tree[0].write(newick_tree)
        consensus_tree_str = newick_tree.getvalue()

        # Return consensus tree as a ETE tree object.
        return Phylo.read(StringIO(consensus_tree_str), format="newick")
Esempio n. 9
0
    def test_majority_rule_tree_node_class(self):
        class TreeNodeSubclass(TreeNode):
            pass

        trees = [
            TreeNode.read(io.StringIO("((a,b),(c,d),(e,f));")),
            TreeNode.read(io.StringIO("(a,(c,d),b,(e,f));")),
            TreeNode.read(io.StringIO("((c,d),(e,f),b);")),
            TreeNode.read(io.StringIO("(a,(c,d),(e,f));"))]

        trees = majority_rule(trees, tree_node_class=TreeNodeSubclass)
        self.assertEqual(len(trees), 4)

        for tree in trees:
            self.assertIs(type(tree), TreeNodeSubclass)

        exp = set([
                  frozenset(['a']),
                  frozenset(['b']),
                  frozenset([None, 'c', 'd']),
                  frozenset([None, 'e', 'f'])])

        obs = set([frozenset([n.name for n in t.traverse()]) for t in trees])
        self.assertEqual(obs, exp)