/
TreeComparison.py
348 lines (322 loc) · 14.6 KB
/
TreeComparison.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"""
Compare a phylogenetic tree to a reference tree.
"""
import unittest
import FelTree
import NewickIO
import Util
def get_nontrivial_split_count(tree):
"""
@param tree: an unrooted tree object
@return: the number of nontrivial splits implied by the tree
"""
return len(get_nontrivial_partitions(tree))
def get_weighted_split_count(tree):
"""
A balanced nontrivial split adds more weight than a less balanced nontrivial split.
@param tree: an unrooted tree object
@return: the weighted number of nontrivial splits implied by the tree
"""
parts = get_nontrivial_partitions(tree)
total = 0
for a, b in parts:
n = len(a) + len(b)
k = min(len(a), len(b))
total += Util.choose(n, k)
return total
def _get_branch_id_to_node_id_set(tree):
"""
I want this function so I can get full id splits of the tree.
By full I mean including internal nodes.
@param tree: a tree object
@return: a map from the id fo a directed branch to a set of node ids
"""
d = {}
for source, dbranch in tree.gen_postorder_exits():
id_set = set()
target = dbranch.get_target()
id_set.add(id(target))
for next_dbranch in target.gen_exits(source):
id_set.update(d[id(next_dbranch)])
d[id(dbranch)] = id_set
return d
def _get_branch_id_to_leaf_name_set(tree):
"""
@param tree: a tree object
@return: a map from the id of a directed branch to a set of leaf names
"""
d = {}
for source, dbranch in tree.gen_postorder_exits():
leaf_name_set = set()
target = dbranch.get_target()
if target.is_tip():
leaf_name_set.add(target.get_name())
else:
for next_dbranch in target.gen_exits(source):
leaf_name_set.update(d[id(next_dbranch)])
d[id(dbranch)] = leaf_name_set
return d
def get_partitions(tree):
"""
Get all of the partitions implied by a tree.
Each positive branch implies a partition.
Each partition is a frozenset of two frozensets of leaf names.
The return value is the set of these partitions.
Note that the word 'partition' is a python keyword,
so the word 'part' will be used here instead.
@param tree: a tree object
@return: the set of partitions implied by the tree.
"""
# map a directed branch id to the set of leaf names in its subtree
d = _get_branch_id_to_leaf_name_set(tree)
# for each branch in the tree get the frozenset of leaf names on each end of the branch
parts = set()
for node in tree.gen_non_root_nodes():
parent = node.get_parent()
directed_branches = (node.get_directed_branch_to(parent), parent.get_directed_branch_to(node))
branch_length = directed_branches[0].get_branch_length()
for dbranch in directed_branches:
assert dbranch.get_branch_length() == branch_length
if branch_length > 0:
leaf_sets = [d[id(dbranch)] for dbranch in directed_branches]
part = frozenset(frozenset(leaf_set) for leaf_set in leaf_sets)
parts.add(part)
# return the set of partitions
return parts
def get_partitions_and_branch_lengths(tree):
"""
Each partition is a frozenset of two frozensets of leaf names.
@param tree: a tree object
@return: a set of (partition, branch length) pairs
"""
# map a directed branch id to the set of leaf names in its subtree
d = _get_branch_id_to_leaf_name_set(tree)
# for each branch in the tree get the frozenset of leaf names on each end of the branch
ret = set()
for node in tree.gen_non_root_nodes():
parent = node.get_parent()
directed_branches = (node.get_directed_branch_to(parent), parent.get_directed_branch_to(node))
branch_length = directed_branches[0].get_branch_length()
for dbranch in directed_branches:
assert dbranch.get_branch_length() == branch_length
if branch_length > 0:
leaf_sets = [d[id(dbranch)] for dbranch in directed_branches]
part = frozenset(frozenset(leaf_set) for leaf_set in leaf_sets)
ret.add((part, branch_length))
# return the set of (partition, branch length) pairs
return ret
def get_nontrivial_partitions(tree):
"""
Get all of the nontrivial partitions implied by a tree.
Note that the word 'partition' is a python keyword,
so the word 'part' will be used here instead.
@param tree: a tree object
@return: the set of nontrivial partitions implied by the tree.
"""
nontrivial_partitions = set()
for part in get_partitions(tree):
if min(len(leaf_set) for leaf_set in part) > 1:
nontrivial_partitions.add(part)
return nontrivial_partitions
def get_split_distance(observed_tree, expected_tree):
"""
@param observed_tree: a tree object
@param expected_tree: a tree object
@return: the number of nontrivial splits in the expected tree that are not in the observed tree
"""
expected_partitions = get_nontrivial_partitions(expected_tree)
observed_partitions = get_nontrivial_partitions(observed_tree)
missing_partitions = expected_partitions - observed_partitions
return len(missing_partitions)
def get_weighted_split_distance(observed_tree, expected_tree):
"""
@param observed_tree: a tree object
@param expected_tree: a tree object
@return: the cost of splits in the expected tree that are not present in the observed tree
"""
missing_partitions = get_nontrivial_partitions(expected_tree) - get_nontrivial_partitions(observed_tree)
# get the sum of the count pair weights
total = 0
for a, b in missing_partitions:
n = len(a) + len(b)
k = min(len(a), len(b))
total += Util.choose(n, k)
return total
def _make_partition(first_group, second_group):
"""
This is a helper function that returns a partition.
@param first_group: a collection of hashable items
@param second_group: another collection of hashable items
@return: a frozenset of two frozensets
"""
return frozenset((frozenset(first_group), frozenset(second_group)))
class TestTreeComparison(unittest.TestCase):
def test_get_nontrivial_split_count(self):
"""
Test the function that gets the number of nontrivial splits
"""
# define some trees
tree_string_a = '((A:1, B:1):1, (C:1, D:1):1, (E:1, F:1):1);'
tree_string_b = '(((A:1, B:1):1, C:1):1, D:1, (E:1, F:1):1);'
tree_string_c = '((A:1, B:1, C:1):1, D:1, (E:1, F:1):1);'
tree_string_d = '(((A:1, B:1):1, C:1):1, (D:1, (E:1, F:1):1):1);'
tree_a = NewickIO.parse(tree_string_a, FelTree.NewickTree)
tree_b = NewickIO.parse(tree_string_b, FelTree.NewickTree)
tree_c = NewickIO.parse(tree_string_c, FelTree.NewickTree)
tree_d = NewickIO.parse(tree_string_d, FelTree.NewickTree)
# assert that the correct split count is recovered
self.assertEqual(get_nontrivial_split_count(tree_a), 3)
self.assertEqual(get_nontrivial_split_count(tree_b), 3)
self.assertEqual(get_nontrivial_split_count(tree_c), 2)
self.assertEqual(get_nontrivial_split_count(tree_d), 3)
def test_get_weighted_split_count(self):
"""
Test the function that gets the weighted number of nontrivial splits
"""
# define some trees
tree_string_a = '((A:1, B:1):1, (C:1, D:1):1, (E:1, F:1):1);'
tree_string_b = '(((A:1, B:1):1, C:1):1, D:1, (E:1, F:1):1);'
tree_string_c = '(((A:1, B:1):1, C:1):1, (D:1, (E:1, F:1):1):1);'
tree_a = NewickIO.parse(tree_string_a, FelTree.NewickTree)
tree_b = NewickIO.parse(tree_string_b, FelTree.NewickTree)
tree_c = NewickIO.parse(tree_string_c, FelTree.NewickTree)
# the weighted split counts are different,
# even though both trees have internal nodes of order 3 and have the same number of leaves
self.assertEqual(get_weighted_split_count(tree_a), 45)
self.assertEqual(get_weighted_split_count(tree_b), 50)
self.assertEqual(get_weighted_split_count(tree_c), 50)
def test_get_partitions(self):
"""
Test the function that gets the set of partitions implied by a tree.
"""
# get the observed partitions
tree_string = '((A:1, B:1):1, (C:1, (D:1, E:1):1):1);'
tree = NewickIO.parse(tree_string, FelTree.NewickTree)
observed_partitions = get_partitions(tree)
# get the expected partitions
arr = (
('A', 'BCDE'),
('B', 'ACDE'),
('C', 'ABDE'),
('D', 'ABCE'),
('E', 'ABCD'),
('AB', 'CDE'),
('ABC', 'DE')
)
expected_partitions = set()
for a, b in arr:
part = frozenset([frozenset(a), frozenset(b)])
expected_partitions.add(part)
# assert that the observed partitions equal the expected partitions
self.assertEqual(observed_partitions, expected_partitions)
def test_get_nontrivial_partitions(self):
"""
Test the function that gets the set of nontrivial partitions implied by a tree.
"""
# get the observed nontrivial partitions
tree_string = '((A:1, B:1):1, (C:1, (D:1, E:1):1):1);'
tree = NewickIO.parse(tree_string, FelTree.NewickTree)
observed_nontrivial_partitions = get_nontrivial_partitions(tree)
# get the expected nontrivial partitions
arr = (
('AB', 'CDE'),
('ABC', 'DE')
)
expected_nontrivial_partitions = set()
for a, b in arr:
part = frozenset([frozenset(a), frozenset(b)])
expected_nontrivial_partitions.add(part)
# assert that the observed nontrivial partitions equal the expected nontrivial partitions
self.assertEqual(observed_nontrivial_partitions, expected_nontrivial_partitions)
# each nontrivial partition corresponds to a split
nontrivial_split_count = get_nontrivial_split_count(tree)
self.assertEqual(nontrivial_split_count, len(expected_nontrivial_partitions))
def test_get_nontrivial_partitions_b(self):
"""
Test nontrivial partitions of trees with branch lengths.
"""
tree_string_a = '(c:1, (a:1, e:1):1, ((d:1, f:1):1, b:1):1);'
tree_string_b = '((f:1, d:1):1, ((b:1, (c:1, a:1):1):1, e:1):1);'
tree_a = NewickIO.parse(tree_string_a, FelTree.NewickTree)
tree_b = NewickIO.parse(tree_string_b, FelTree.NewickTree)
partitions_a = get_nontrivial_partitions(tree_a)
partitions_b = get_nontrivial_partitions(tree_b)
expected_a = set((
_make_partition('ae', 'cdfb'),
_make_partition('aec', 'dfb'),
_make_partition('aecb', 'df')))
expected_b = set((
_make_partition('ac', 'bedf'),
_make_partition('acb', 'edf'),
_make_partition('acbe', 'df')))
self.assertEqual(partitions_a, expected_a)
self.assertEqual(partitions_b, expected_b)
def test_get_nontrivial_partitions_c(self):
"""
Test nontrivial partitions of trees without branch lengths.
"""
"""
tree_string_a = '(c, (a, e), ((d, f), b));'
tree_string_b = '((f, d), ((b, (c, a)), e));'
tree_a = NewickIO.parse(tree_string_a, FelTree.NewickTree)
tree_b = NewickIO.parse(tree_string_b, FelTree.NewickTree)
partitions_a = get_nontrivial_partitions(tree_a)
partitions_b = get_nontrivial_partitions(tree_b)
expected_a = set((
_make_partition('ae', 'cdfb'),
_make_partition('aec', 'dfb'),
_make_partition('aecb', 'df')))
expected_b = set((
_make_partition('ac', 'bedf'),
_make_partition('acb', 'edf'),
_make_partition('acbe', 'df')))
self.assertEqual(partitions_a, expected_a)
self.assertEqual(partitions_b, expected_b)
"""
pass
def test_get_split_distance(self):
"""
Test the function that gets the number of missing nontrivial partitions.
"""
# define some trees
tree_string_a = '((A:1, B:1):1, C:1, (D:1, E:1):1);'
tree_string_b = '((A:1, B:1):1, D:1, (C:1, E:1):1);'
tree_string_c = '((A:1, D:1):1, C:1, (B:1, E:1):1);'
tree_string_d = '((A:1, D:1):1, (C:1, B:1, E:1):1);'
tree_a = NewickIO.parse(tree_string_a, FelTree.NewickTree)
tree_b = NewickIO.parse(tree_string_b, FelTree.NewickTree)
tree_c = NewickIO.parse(tree_string_c, FelTree.NewickTree)
tree_d = NewickIO.parse(tree_string_d, FelTree.NewickTree)
# the distance from a tree to itself should be zero
self.assertEqual(get_split_distance(tree_a, tree_a), 0)
self.assertEqual(get_split_distance(tree_b, tree_b), 0)
self.assertEqual(get_split_distance(tree_c, tree_c), 0)
self.assertEqual(get_split_distance(tree_d, tree_d), 0)
# some of the distances are symmetric
self.assertEqual(get_split_distance(tree_a, tree_b), 1)
self.assertEqual(get_split_distance(tree_b, tree_a), 1)
self.assertEqual(get_split_distance(tree_b, tree_c), 2)
self.assertEqual(get_split_distance(tree_c, tree_b), 2)
self.assertEqual(get_split_distance(tree_a, tree_c), 2)
self.assertEqual(get_split_distance(tree_c, tree_a), 2)
# it is possible for the distance to be asymmetric if internal nodes are not order 3
self.assertEqual(get_split_distance(tree_a, tree_d), 1)
self.assertEqual(get_split_distance(tree_d, tree_a), 2)
def test_get_weighted_split_distance(self):
"""
Test the function that gets the number of missing nontrivial partitions.
"""
# define some trees
tree_string_a = '((A:1, B:1):1, (C:1, D:1):1, (E:1, F:1):1);'
tree_string_b = '(((A:1, B:1):1, C:1):1, D:1, (E:1, F:1):1);'
tree_a = NewickIO.parse(tree_string_a, FelTree.NewickTree)
tree_b = NewickIO.parse(tree_string_b, FelTree.NewickTree)
# the distance from a tree to itself should be zero
self.assertEqual(get_weighted_split_distance(tree_a, tree_a), 0)
self.assertEqual(get_weighted_split_distance(tree_b, tree_b), 0)
# the distance is not necessarily symmetric
self.assertEqual(get_weighted_split_distance(tree_a, tree_b), 20)
self.assertEqual(get_weighted_split_distance(tree_b, tree_a), 15)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestTreeComparison)
unittest.TextTestRunner(verbosity=2).run(suite)