File: test_datamodel_bipartitions.py

package info (click to toggle)
python-dendropy 4.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 68,392 kB
  • ctags: 3,947
  • sloc: python: 41,840; xml: 1,400; makefile: 15
file content (101 lines) | stat: -rw-r--r-- 5,252 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#! /usr/bin/env python

##############################################################################
##  DendroPy Phylogenetic Computing Library.
##
##  Copyright 2010-2015 Jeet Sukumaran and Mark T. Holder.
##  All rights reserved.
##
##  See "LICENSE.rst" for terms and conditions of usage.
##
##  If you use this work or any portion thereof in published work,
##  please cite it as:
##
##     Sukumaran, J. and M. T. Holder. 2010. DendroPy: a Python library
##     for phylogenetic computing. Bioinformatics 26: 1569-1571.
##
##############################################################################

"""
Testing of calculation of and operations with bipartitions.
"""

import warnings
import unittest
import re
import sys
import json
from dendropy.test.support import pathmap
from dendropy.test.support import paupsplitsreference
from dendropy.test.support.dendropytest import ExtendedTestCase
from dendropy.utility import messaging
from dendropy.interop import paup
from dendropy.calculate import treecompare
import dendropy

# @unittest.skip('BipartitionEncodingTestCase skipped. Test in development')
class BipartitionEncodingTestCase(ExtendedTestCase):

    @classmethod
    def setUpClass(cls):
        ref_path = pathmap.tree_source_path("bipartition_encoding_fixture.json")
        with open(ref_path, "r") as src:
            cls.reference = json.load(src)

    def test_encoding(self):
        for source_name in self.reference:
            # if "multifurcating" in source_name:
            #     continue
            tree_filepath = pathmap.tree_source_path(source_name)
            for rooting in self.reference[source_name]:
                for collapse_unrooted_basal_bifurcation_desc in self.reference[source_name][rooting]:
                    if "collapse_unrooted_basal_bifurcation=True" in collapse_unrooted_basal_bifurcation_desc:
                        collapse_unrooted_basal_bifurcation = True
                    elif "collapse_unrooted_basal_bifurcation=False" in collapse_unrooted_basal_bifurcation_desc:
                        collapse_unrooted_basal_bifurcation = False
                    else:
                        raise ValueError(collapse_unrooted_basal_bifurcation_desc)
                    for suppress_unifurcations_desc in self.reference[source_name][rooting][collapse_unrooted_basal_bifurcation_desc]:
                        if "suppress_unifurcations=True" in suppress_unifurcations_desc:
                            suppress_unifurcations = True
                        elif "suppress_unifurcations=False" in suppress_unifurcations_desc:
                            suppress_unifurcations = False
                        else:
                            raise ValueError(suppress_unifurcations_desc)
                        trees_bipartitions_ref = self.reference[source_name][rooting][collapse_unrooted_basal_bifurcation_desc][suppress_unifurcations_desc]
                        source_path = pathmap.tree_source_path(source_name)
                        trees = dendropy.TreeList.get_from_path(
                                source_path,
                                "nexus",
                                rooting=rooting,
                                suppress_leaf_node_taxa=False,
                                suppress_internal_node_taxa=False,
                                )
                        for tree_idx, tree in enumerate(trees):
                            tree_bipartitions_ref = trees_bipartitions_ref[str(tree_idx)]
                            bipartition_encoding = tree.encode_bipartitions(
                                    suppress_unifurcations=suppress_unifurcations,
                                    collapse_unrooted_basal_bifurcation=collapse_unrooted_basal_bifurcation,
                                    )
                            seen = set()
                            for edge in tree.postorder_edge_iter():
                                bipartition = edge.bipartition
                                assert edge.head_node.taxon is not None
                                assert edge.head_node.taxon.label is not None
                                label = edge.head_node.taxon.label
                                # print("{}: {}: {}: {}".format(source_name, tree_idx, rooting, label, ))
                                # print("    {}".format(tree_bipartitions_ref[label]))
                                # print("    {} ({}), {}({})".format(
                                #     bipartition.split_bitmask,
                                #     bipartition.as_bitstring(),
                                #     bipartition.leafset_bitmask,
                                #     bipartition.leafset_as_bitstring(),
                                #     ))
                                expected_leafset_bitmask = int(tree_bipartitions_ref[label]["leafset_bitmask"])
                                self.assertEqual(bipartition.leafset_bitmask, expected_leafset_bitmask)
                                expected_split_bitmask = int(tree_bipartitions_ref[label]["split_bitmask"])
                                self.assertEqual(bipartition.split_bitmask, expected_split_bitmask)

if __name__ == "__main__":
    unittest.main()