File: treesplit.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 (179 lines) | stat: -rw-r--r-- 8,246 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#! /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.
##
##############################################################################

"""
Split calculation and management.
DEPRECATED IN DENDROPY 4.
"""

import dendropy
from dendropy.utility import bitprocessing
from dendropy.utility import deprecate

def tree_from_splits(splits, taxon_set, is_rooted=False):
    deprecate.dendropy_deprecation_warning(
            preamble="Deprecated since DendroPy 4: 'dendropy.treesplit.tree_from_splits()'.",
            old_construct="from dendropy import treesplit\ntree = treesplit.tree_from_splits(...)",
            new_construct="import dendropy\ntree = dendropy.Tree.from_split_bitmasks(...)")
    return dendropy.Tree.from_split_bitmasks(
            split_bitmasks=splits,
            taxon_namespace=taxon_set,
            is_rooted=is_rooted)

def split_to_list(s, mask=-1, one_based=False, ordination_in_mask=False):
    deprecate.dendropy_deprecation_warning(
            preamble="Deprecated since DendroPy 4: 'dendropy.treesplit.split_to_list()'.",
            old_construct="from dendropy import treesplit\nd = treesplit.split_to_list(...)",
            new_construct="from dendropy.utility import bitprocessing\nd = bitprocessing.indexes_of_set_bits(...)")
    return bitprocessing.indexes_of_set_bits(
            s=s,
            fill_bitmask=mask,
            one_based=one_based,
            ordination_in_mask=ordination_in_mask)

def is_trivial_split(split, mask):
    deprecate.dendropy_deprecation_warning(
            preamble="Deprecated since DendroPy 4: 'dendropy.treesplit.is_trivial_split()'.",
            old_construct="from dendropy import treesplit\nd = treesplit.is_trivial_split(...)",
            new_construct="import dendropy\nd = dendropy.Bipartition.is_trivial_bitmask(...)")
    return dendropy.Bipartition.is_trivial_bitmask(bitmask=split, fill_bitmask=mask)

def is_non_singleton_split(split, mask):
    deprecate.dendropy_deprecation_warning(
            preamble="Deprecated since DendroPy 4: 'dendropy.treesplit.is_non_singleton_split()'.",
            old_construct="from dendropy import treesplit\nd = treesplit.is_non_singleton_split(...)",
            new_construct="import dendropy\nd = not dendropy.Bipartition.is_trivial_bitmask(...)")
    return dendropy.Bipartition.is_trivial_bitmask(bitmask=split, fill_bitmask=mask)

def split_as_string(split_mask, width, symbol1=None, symbol2=None):
    deprecate.dendropy_deprecation_warning(
            preamble="Deprecated since DendroPy 4: 'dendropy.treesplit.split_as_string()'.",
            old_construct="from dendropy import treesplit\nd = treesplit.split_as_string(...)",
            new_construct="""\
# if using a bipartition
d = bipartition.split_as_bitstring(...)
d = bipartition.leafset_as_bitstring(...)
# if a "raw" bitmask
from dendropy.utility import bitprocessing
d = bitprocessing.int_as_bitstring(...)""")
    return bitprocessing.int_as_bitstring(
            n=split_mask,
            length=width,
            symbol0=symbol1,
            symbol1=symbol2,
            reverse=False)

def split_as_string_rev(split_mask, width, symbol1='.', symbol2='*'):
    deprecate.dendropy_deprecation_warning(
            preamble="Deprecated since DendroPy 4: 'dendropy.treesplit.split_as_string_rev()'.",
            old_construct="from dendropy import treesplit\nd = treesplit.split_as_string(...)",
            new_construct="""\
# if using a bipartition
d = bipartition.split_as_bitstring(...)[::-1]
d = bipartition.leafset_as_bitstring(...)[::-1]
# if a "raw" bitmask
from dendropy.utility import bitprocessing
d = bitprocessing.int_as_bitstring(..., reverse=True)""")
    return bitprocessing.int_as_bitstring(
            mask=split_mask,
            length=width,
            symbol0=symbol0,
            symbol1=symbol1,
            reverse=True)

def find_edge_from_split(root, split_to_find, mask=-1):
    deprecate.dendropy_deprecation_warning(
            preamble="Deprecated since DendroPy 4: 'dendropy.treesplit.find_edge_from_split()'",
            old_construct="from dendropy import treesplit\nd = treesplit.find_edge_from_split(...)",
            new_construct="""\
# if using a bipartition
d = bipartition.edge
# if a "raw" bitmask
d = tree.find_edge_for_split_bitmask(...)""")
    return root.find_edge_for_split_bitmask(split_to_find, fill_bitmask=mask)

def encode_splits(tree, create_dict=True, suppress_unifurcations=True):
    deprecate.dendropy_deprecation_warning(
            preamble="Deprecated since DendroPy 4: 'dendropy.treesplit.encode_splits()'.",
            old_construct="from dendropy import treesplit\nd = treesplit.encode_splits(tree, ...)",
            new_construct="bipartitions = tree.encode_bipartitions(...)\nsplit_bitmasks = tree.split_bitmask_edge_map.keys()")
    return tree.encode_bipartitions(suppress_unifurcations=suppress_unifurcations)

def is_compatible(split1, split2, mask):
    deprecate.dendropy_deprecation_warning(
            preamble="Deprecated since DendroPy 4: 'dendropy.treesplit.is_compatible()'.",
            old_construct="from dendropy import treesplit\nd = treesplit.is_compatible(...)",
            new_construct="""\
# if using a bipartition
d = bipartition.is_compatible_with(other_bipartition)
# if a "raw" bitmask
d = dendropy.Bipartition.is_compatible_bitmasks(m1, m2, fill_bitmask=mask)""")
    """
    Mask should have 1 for every leaf in the leaf_set
    """
    # m1 = mask & split1
    # m2 = mask & split2
    # if 0 == (m1 & m2):
    #     return True
    # c2 = mask ^ split2
    # if 0 == (m1 & c2):
    #     return True
    # c1 = mask ^ split1
    # if 0 == (c1 & m2):
    #     return True
    # if 0 == (c1 & c2):
    #     return True
    # return False
    return dendropy.Bipartition.is_compatible_bitmasks(split1, split2, fill_bitmask=mask)

def delete_outdegree_one(tree):
    deprecate.dendropy_deprecation_warning(
            preamble="Deprecated since DendroPy 4: 'dendropy.treesplit.delete_outdegree_one()'.",
            old_construct="from dendropy import treesplit\nd = treesplit.delete_outdegree_one(tree)",
            new_construct="tree.suppress_unifurcations()")
    return tree.suppress_unifurcations()

def lowest_bit_only(s):
    deprecate.dendropy_deprecation_warning(
            preamble="Deprecated since DendroPy 4: 'dendropy.treesplit.lowest_bit_only()'.",
            old_construct="from dendropy import treesplit\nd = treesplit.lowest_bit_only(...)",
            new_construct="from dendropy.utility import bitprocessing\nd = bitprocessing.least_significant_set_bit(...)")
    return bitprocessing.least_significant_set_bit(s)

def count_bits(a):
    deprecate.dendropy_deprecation_warning(
            preamble="Deprecated since DendroPy 4: 'dendropy.treesplit.count_bits()'.",
            old_construct="from dendropy import treesplit\nd = treesplit.count_bits(...)",
            new_construct="from dendropy.utility import bitprocessing\nd = bitprocessing.num_set_bits(...)")
    return bitprocessing.num_set_bits(a)

class SplitDistribution(dendropy.SplitDistribution):
    def __init__(self, taxon_set=None, split_set=None):
        deprecate.dendropy_deprecation_warning(
                preamble="Deprecated since DendroPy 4: The 'dendropy.treesplit.SplitDistribution' class has moved to 'dendropy.calculate.treesplit.SplitDistribution'.",
                old_construct="from dendropy import treesplit\nm = treesplit.SplitDistribution(...)",
                new_construct="import dendropy\nm = dendropy.SplitDistribution(...)")
        dendropy.SplitDistribution.__init__(self,
                taxon_namespace=taxon_set)
        if split_set:
            for split in split_set:
                self.add_split_count(split, count=1)