forked from tskit-dev/tskit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinatorics.py
More file actions
1536 lines (1295 loc) · 55.1 KB
/
Copy pathcombinatorics.py
File metadata and controls
1536 lines (1295 loc) · 55.1 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# MIT License
#
# Copyright (c) 2020-2024 Tskit Developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
Module for ranking and unranking trees. Trees are considered only
leaf-labelled and unordered, so order of children does not influence equality.
"""
import collections
import functools
import heapq
import itertools
import json
import random
from typing import NamedTuple
import attr
import numpy as np
import tskit
class Rank(NamedTuple):
"""
A tuple of 2 numbers, ``(shape, label)``, together defining a unique
topology for a labeled tree. See :ref:`sec_combinatorics`.
"""
shape: int
"""
A non-negative integer representing the (unlabelled) topology of a tree with a
defined number of tips.
"""
label: int
"""
A non-negative integer representing the order of labels for a given tree topology.
"""
def equal_chunks(lst, k):
"""
Yield k successive equally sized chunks from lst of size n.
If k >= n, we return n chunks of size 1.
Otherwise, we always return k chunks. The first k - 1 chunks will
contain exactly n // k items, and the last chunk the remainder.
"""
n = len(lst)
if k <= 0 or int(k) != k:
raise ValueError("Number of chunks must be a positive integer")
if n > 0:
chunk_size = max(1, n // k)
offset = 0
j = 0
while offset < n - chunk_size and j < k - 1:
yield lst[offset : offset + chunk_size]
offset += chunk_size
j += 1
yield lst[offset:]
@attr.s(eq=False)
class TreeNode:
"""
Simple linked tree class used to generate tree topologies.
"""
parent = attr.ib(default=None)
children = attr.ib(factory=list)
label = attr.ib(default=None)
def as_tables(self, *, num_leaves, span, branch_length):
"""
Convert the tree rooted at this node into an equivalent
TableCollection. Internal nodes are allocated in postorder.
"""
tables = tskit.TableCollection(span)
for _ in range(num_leaves):
tables.nodes.add_row(flags=tskit.NODE_IS_SAMPLE, time=0)
def assign_internal_labels(node):
if len(node.children) == 0:
node.time = 0
else:
max_child_time = 0
for child in node.children:
assign_internal_labels(child)
max_child_time = max(max_child_time, child.time)
node.time = max_child_time + branch_length
node.label = tables.nodes.add_row(time=node.time)
for child in node.children:
tables.edges.add_row(0, span, node.label, child.label)
# Do a postorder traversal to assign the internal node labels and times.
assign_internal_labels(self)
tables.sort()
return tables
@staticmethod
def random_binary_tree(leaf_labels, rng):
"""
Returns a random binary tree where the leaves have the specified
labels using the specified random.Random instance. The root node
of this tree is returned.
Based on the description of Remy's method of generating "decorated"
random binary trees in TAOCP 7.2.1.6. This is not a direct
implementation of Algorithm R, because we are interested in
the leaf node labellings.
The pre-fascicle text is available here, page 16:
http://www.cs.utsa.edu/~wagner/knuth/fasc4a.pdf
"""
nodes = [TreeNode(label=leaf_labels[0])]
for label in leaf_labels[1:]:
# Choose a node x randomly and insert a new internal node above
# it with the (n + 1)th labelled leaf as its sibling.
x = rng.choice(nodes)
new_leaf = TreeNode(label=label)
new_internal = TreeNode(parent=x.parent, children=[x, new_leaf])
if x.parent is not None:
index = x.parent.children.index(x)
x.parent.children[index] = new_internal
rng.shuffle(new_internal.children)
x.parent = new_internal
new_leaf.parent = new_internal
nodes.extend([new_leaf, new_internal])
root = nodes[0]
while root.parent is not None:
root = root.parent
# Canonicalise the order of the children within a node using iterative approach
# This replaces the recursive reorder_children function
# Use post-order traversal to process leaf nodes first, then internal nodes
# We need to visit children before parents to compute (num_leaves, min_label)
# First, collect all nodes in post-order using two stacks
stack1 = [root]
stack2 = []
while stack1:
node = stack1.pop()
stack2.append(node)
for child in node.children:
stack1.append(child)
# Now process nodes in reverse order (post-order)
# Store results for each node as we compute them
node_keys = {}
while stack2:
node = stack2.pop()
if len(node.children) == 0:
# Leaf node
node_keys[node] = (1, node.label)
else:
# Internal node - get keys from children
keys = [node_keys[child] for child in node.children]
if keys[0] > keys[1]:
node.children = node.children[::-1]
keys = keys[::-1] # Update keys to match new order
node_keys[node] = (
sum(leaf_count for leaf_count, _ in keys),
min(min_label for _, min_label in keys),
)
return root
@classmethod
def balanced_tree(cls, leaf_labels, arity):
"""
Returns a balanced tree of the specified arity. At each node the
leaf labels are split equally among the arity children using the
equal_chunks method.
"""
assert len(leaf_labels) > 0
if len(leaf_labels) == 1:
root = cls(label=leaf_labels[0])
else:
children = [
cls.balanced_tree(chunk, arity)
for chunk in equal_chunks(leaf_labels, arity)
]
root = cls(children=children)
for child in children:
child.parent = root
return root
def generate_star(num_leaves, *, span, branch_length, record_provenance, **kwargs):
"""
Generate a star tree for the specified number of leaves.
See the documentation for :meth:`Tree.generate_star` for more details.
"""
if num_leaves < 2:
raise ValueError("The number of leaves must be 2 or greater")
tables = tskit.TableCollection(sequence_length=span)
tables.nodes.set_columns(
flags=np.full(num_leaves, tskit.NODE_IS_SAMPLE, dtype=np.uint32),
time=np.zeros(num_leaves),
)
root = tables.nodes.add_row(time=branch_length)
tables.edges.set_columns(
left=np.full(num_leaves, 0),
right=np.full(num_leaves, span),
parent=np.full(num_leaves, root, dtype=np.int32),
child=np.arange(num_leaves, dtype=np.int32),
)
if record_provenance:
# TODO replace with a version of https://github.com/tskit-dev/tskit/pull/243
# TODO also make sure we convert all the arguments so that they are
# definitely JSON encodable.
parameters = {"command": "generate_star", "TODO": "add parameters"}
tables.provenances.add_row(
record=json.dumps(tskit.provenance.get_provenance_dict(parameters))
)
return tables.tree_sequence().first(**kwargs)
def generate_comb(num_leaves, *, span, branch_length, record_provenance, **kwargs):
"""
Generate a comb tree for the specified number of leaves.
See the documentation for :meth:`Tree.generate_comb` for more details.
"""
if num_leaves < 2:
raise ValueError("The number of leaves must be 2 or greater")
tables = tskit.TableCollection(sequence_length=span)
tables.nodes.set_columns(
flags=np.full(num_leaves, tskit.NODE_IS_SAMPLE, dtype=np.uint32),
time=np.zeros(num_leaves),
)
right_child = num_leaves - 1
time = branch_length
for left_child in range(num_leaves - 2, -1, -1):
parent = tables.nodes.add_row(time=time)
time += branch_length
tables.edges.add_row(0, span, parent, left_child)
tables.edges.add_row(0, span, parent, right_child)
right_child = parent
if record_provenance:
# TODO replace with a version of https://github.com/tskit-dev/tskit/pull/243
# TODO also make sure we convert all the arguments so that they are
# definitely JSON encodable.
parameters = {"command": "generate_comb", "TODO": "add parameters"}
tables.provenances.add_row(
record=json.dumps(tskit.provenance.get_provenance_dict(parameters))
)
return tables.tree_sequence().first(**kwargs)
def generate_balanced(
num_leaves, *, arity, span, branch_length, record_provenance, **kwargs
):
"""
Generate a balanced tree for the specified number of leaves.
See the documentation for :meth:`Tree.generate_balanced` for more details.
"""
if num_leaves < 1:
raise ValueError("The number of leaves must be at least 1")
if arity < 2:
raise ValueError("The arity must be at least 2")
root = TreeNode.balanced_tree(range(num_leaves), arity)
tables = root.as_tables(
num_leaves=num_leaves, span=span, branch_length=branch_length
)
if record_provenance:
# TODO replace with a version of https://github.com/tskit-dev/tskit/pull/243
# TODO also make sure we convert all the arguments so that they are
# definitely JSON encodable.
parameters = {"command": "generate_balanced", "TODO": "add parameters"}
tables.provenances.add_row(
record=json.dumps(tskit.provenance.get_provenance_dict(parameters))
)
return tables.tree_sequence().first(**kwargs)
def generate_random_binary(
num_leaves, *, span, branch_length, random_seed, record_provenance, **kwargs
):
"""
Sample a leaf-labelled binary tree uniformly.
See the documentation for :meth:`Tree.generate_random_binary` for more details.
"""
if num_leaves < 1:
raise ValueError("The number of leaves must be at least 1")
rng = random.Random(random_seed)
root = TreeNode.random_binary_tree(range(num_leaves), rng)
tables = root.as_tables(
num_leaves=num_leaves, span=span, branch_length=branch_length
)
if record_provenance:
# TODO replace with a version of https://github.com/tskit-dev/tskit/pull/243
# TODO also make sure we convert all the arguments so that they are
# definitely JSON encodable.
parameters = {"command": "generate_random_binary", "TODO": "add parameters"}
tables.provenances.add_row(
record=json.dumps(tskit.provenance.get_provenance_dict(parameters))
)
ts = tables.tree_sequence()
return ts.first(**kwargs)
def split_polytomies(
tree,
*,
epsilon=None,
method=None,
record_provenance=True,
random_seed=None,
**kwargs,
):
"""
Return a new tree where extra nodes and edges have been inserted
so that any any node with more than two children is resolved into
a binary tree.
See the documentation for :meth:`Tree.split_polytomies` for more details.
"""
allowed_methods = ["random"]
if method is None:
method = "random"
if method not in allowed_methods:
raise ValueError(f"Method must be chosen from {allowed_methods}")
tables = tree.tree_sequence.dump_tables()
tables.keep_intervals([tree.interval], simplify=False)
tables.edges.clear()
rng = random.Random(random_seed)
for u in tree.nodes():
if tree.num_children(u) > 2:
root = TreeNode.random_binary_tree(tree.children(u), rng)
root.label = u
root_time = tree.time(u)
stack = [(child, root_time) for child in root.children]
while len(stack) > 0:
node, parent_time = stack.pop()
if node.label is None:
if epsilon is None:
child_time = np.nextafter(parent_time, -np.inf)
else:
child_time = parent_time - epsilon
node.label = tables.nodes.add_row(time=child_time)
else:
assert len(node.children) == 0
# This is a leaf node connecting back into the original tree
child_time = tree.time(node.label)
if parent_time <= child_time:
u = root.label
min_child_time = min(tree.time(v) for v in tree.children(u))
min_time = root_time - min_child_time
message = (
f"Cannot resolve the degree {tree.num_children(u)} "
f"polytomy rooted at node {u} with minimum time difference "
f"of {min_time} to the resolved leaves."
)
if epsilon is None:
message += (
" The time difference between nodes is so small that "
"more nodes cannot be inserted between within the limits "
"of floating point precision."
)
else:
# We can also have parent_time == child_time if epsilon is
# chosen such that we exactly divide up the branch in the
# original tree. We avoid saying this is caused by a
# too-small epsilon by noting it can only happen when we
# are at leaf node in the randomly generated tree.
if parent_time == child_time and len(node.children) > 0:
message += (
f" The fixed epsilon value of {epsilon} is too small, "
"resulting in the parent and child times being equal "
"within the limits of numerical precision."
)
else:
message += (
f" The fixed epsilon value of {epsilon} is too large, "
"resulting in the parent time being less than the child "
"time."
)
raise tskit.LibraryError(message)
tables.edges.add_row(*tree.interval, node.parent.label, node.label)
for child in node.children:
stack.append((child, child_time))
else:
for v in tree.children(u):
tables.edges.add_row(*tree.interval, u, v)
if record_provenance:
parameters = {"command": "split_polytomies"}
tables.provenances.add_row(
record=json.dumps(tskit.provenance.get_provenance_dict(parameters))
)
try:
tables.sort()
ts = tables.tree_sequence()
except tskit.LibraryError as e:
msg = str(e)
# We should have caught all topology time travel above.
assert not msg.startswith("time[parent] must be greater than time[child]")
if msg.startswith(
"A mutation's time must be < the parent node of the edge on which it occurs"
):
if epsilon is not None:
msg = (
f"epsilon={epsilon} not small enough to create new nodes below a "
"polytomy, due to the time of a mutation above a child of the "
"polytomy."
)
else:
msg = (
"Cannot split polytomy: mutation with numerical precision "
"of the parent time."
)
e.args += (msg,)
raise e
return ts.at(tree.interval.left, **kwargs)
def treeseq_count_topologies(ts, sample_sets):
topology_counter = np.full(ts.num_nodes, None, dtype=object)
parent = np.full(ts.num_nodes, -1)
def update_state(tree, u):
stack = [u]
while len(stack) > 0:
v = stack.pop()
children = []
for c in tree.children(v):
if topology_counter[c] is not None:
children.append(topology_counter[c])
if len(children) > 0:
topology_counter[v] = combine_child_topologies(children)
else:
topology_counter[v] = None
p = parent[v]
if p != -1:
stack.append(p)
for sample_set_index, sample_set in enumerate(sample_sets):
for u in sample_set:
if not ts.node(u).is_sample():
raise ValueError(f"Node {u} in sample_sets is not a sample.")
topology_counter[u] = TopologyCounter.from_sample(sample_set_index)
for tree, (_, edges_out, edges_in) in zip(ts.trees(), ts.edge_diffs()):
# Avoid recomputing anything for the parent until all child edges
# for that parent are inserted/removed
for p, sibling_edges in itertools.groupby(edges_out, key=lambda e: e.parent):
for e in sibling_edges:
parent[e.child] = -1
update_state(tree, p)
for p, sibling_edges in itertools.groupby(edges_in, key=lambda e: e.parent):
if tree.is_sample(p):
raise ValueError("Internal samples not supported.")
for e in sibling_edges:
parent[e.child] = p
update_state(tree, p)
counters = []
for root in tree.roots:
if topology_counter[root] is not None:
counters.append(topology_counter[root])
yield TopologyCounter.merge(counters)
def tree_count_topologies(tree, sample_sets):
for u in tree.samples():
if not tree.is_leaf(u):
raise ValueError("Internal samples not supported.")
topology_counter = np.full(tree.tree_sequence.num_nodes, None, dtype=object)
for sample_set_index, sample_set in enumerate(sample_sets):
for u in sample_set:
if not tree.is_sample(u):
raise ValueError(f"Node {u} in sample_sets is not a sample.")
topology_counter[u] = TopologyCounter.from_sample(sample_set_index)
for u in tree.nodes(order="postorder"):
children = []
for v in tree.children(u):
if topology_counter[v] is not None:
children.append(topology_counter[v])
if len(children) > 0:
topology_counter[u] = combine_child_topologies(children)
counters = []
for root in tree.roots:
if topology_counter[root] is not None:
counters.append(topology_counter[root])
return TopologyCounter.merge(counters)
def combine_child_topologies(topology_counters):
"""
Select all combinations of topologies from different
counters in ``topology_counters`` that are capable of
being combined into a single topology. This includes
any combination of at least two topologies, all from
different children, where no topologies share a
sample set index.
"""
partial_topologies = PartialTopologyCounter()
for tc in topology_counters:
partial_topologies.add_sibling_topologies(tc)
return partial_topologies.join_all_combinations()
class TopologyCounter:
"""
Contains the distributions of embedded topologies for every combination
of the sample sets used to generate the ``TopologyCounter``. It is
indexable by a combination of sample set indexes and returns a
``collections.Counter`` whose keys are topology ranks
(see :ref:`sec_tree_ranks`). See :meth:`Tree.count_topologies` for more
detail on how this structure is used.
"""
def __init__(self):
self.topologies = collections.defaultdict(collections.Counter)
def __getitem__(self, sample_set_indexes):
k = TopologyCounter._to_key(sample_set_indexes)
return self.topologies[k]
def __setitem__(self, sample_set_indexes, counter):
k = TopologyCounter._to_key(sample_set_indexes)
self.topologies[k] = counter
@staticmethod
def _to_key(sample_set_indexes):
if not isinstance(sample_set_indexes, collections.abc.Iterable):
sample_set_indexes = (sample_set_indexes,)
return tuple(sorted(sample_set_indexes))
def __eq__(self, other):
return self.__class__ == other.__class__ and self.topologies == other.topologies
@staticmethod
def merge(topology_counters):
"""
Union together independent topology counters into one.
"""
total = TopologyCounter()
for tc in topology_counters:
for k, v in tc.topologies.items():
total.topologies[k] += v
return total
@staticmethod
def from_sample(sample_set_index):
"""
Generate the topologies covered by a single sample. This
is the single-leaf topology representing the single sample
set.
"""
rank_tree = RankTree(children=[], label=sample_set_index)
tc = TopologyCounter()
tc[sample_set_index][rank_tree.rank()] = 1
return tc
class PartialTopologyCounter:
"""
Represents the possible combinations of children under a node in a tree
and the combinations of embedded topologies that are rooted at the node.
This allows an efficient way of calculating which unique embedded
topologies arise by only every storing a given pairing of sibling topologies
once.
``partials`` is a dictionary where a key is a tuple of sample set indexes,
and the value is a ``collections.Counter`` that counts combinations of
sibling topologies whose tips represent the sample sets in the key.
Each element of the counter is a homogeneous tuple where each element represents
a topology. The topology is itself a tuple of the sample set indexes in that
topology and the rank.
"""
def __init__(self):
self.partials = collections.defaultdict(collections.Counter)
def add_sibling_topologies(self, topology_counter):
"""
Combine each topology in the given TopologyCounter with every existing
combination of topologies whose sample set indexes are disjoint from the
topology from the counter. This also includes adding the topologies from
the counter without joining them to any existing combinations.
"""
merged = collections.defaultdict(collections.Counter)
for sample_set_indexes, topologies in topology_counter.topologies.items():
for rank, count in topologies.items():
topology = ((sample_set_indexes, rank),)
# Cross with existing topology combinations
for sibling_sample_set_indexes, siblings in self.partials.items():
if isdisjoint(sample_set_indexes, sibling_sample_set_indexes):
for sib_topologies, sib_count in siblings.items():
merged_topologies = merge_tuple(sib_topologies, topology)
merged_sample_set_indexes = merge_tuple(
sibling_sample_set_indexes, sample_set_indexes
)
merged[merged_sample_set_indexes][merged_topologies] += (
count * sib_count
)
# Propagate without combining
merged[sample_set_indexes][topology] += count
for sample_set_indexes, counter in merged.items():
self.partials[sample_set_indexes] += counter
def join_all_combinations(self):
"""
For each pairing of child topologies, join them together into a new
tree and count the resulting topologies.
"""
topology_counter = TopologyCounter()
for sample_set_indexes, sibling_topologies in self.partials.items():
for topologies, count in sibling_topologies.items():
# A node must have at least two children
if len(topologies) >= 2:
rank = PartialTopologyCounter.join_topologies(topologies)
topology_counter[sample_set_indexes][rank] += count
else:
# Pass on the single tree without adding a parent
for _, rank in topologies:
topology_counter[sample_set_indexes][rank] += count
return topology_counter
@staticmethod
def join_topologies(child_topologies):
children = []
for sample_set_indexes, rank in child_topologies:
n = len(sample_set_indexes)
t = RankTree.unrank(n, rank, list(sample_set_indexes))
children.append(t)
children.sort(key=RankTree.canonical_order)
return RankTree(children).rank()
def all_trees(num_leaves, span=1):
"""
Generates all unique leaf-labelled trees with ``num_leaves``
leaves. See :ref:`sec_combinatorics` on the details of this
enumeration. The leaf labels are selected from the set
``[0, num_leaves)``. The times and labels on internal nodes are
chosen arbitrarily.
:param int num_leaves: The number of leaves of the tree to generate.
:param float span: The genomic span of each returned tree.
:rtype: tskit.Tree
"""
for rank_tree in RankTree.all_labelled_trees(num_leaves):
yield rank_tree.to_tsk_tree(span=span)
def all_tree_shapes(num_leaves, span=1):
"""
Generates all unique shapes of trees with ``num_leaves`` leaves.
:param int num_leaves: The number of leaves of the tree to generate.
:param float span: The genomic span of each returned tree.
:rtype: tskit.Tree
"""
for rank_tree in RankTree.all_unlabelled_trees(num_leaves):
default_labelling = rank_tree.label_unrank(0)
yield default_labelling.to_tsk_tree(span=span)
def all_tree_labellings(tree, span=1):
"""
Generates all unique labellings of the leaves of a
:class:`tskit.Tree`. Leaves are labelled from the set
``[0, n)`` where ``n`` is the number of leaves of ``tree``.
:param tskit.Tree tree: The tree used to generate
labelled trees of the same shape.
:param float span: The genomic span of each returned tree.
:rtype: tskit.Tree
"""
rank_tree = RankTree.from_tsk_tree(tree)
for labelling in RankTree.all_labellings(rank_tree):
yield labelling.to_tsk_tree(span=span)
class RankTree:
"""
A tree class that maintains the topological ranks of each node in the tree.
This structure can be used to efficiently compute the rank of a tree of
n labelled leaves and produce a tree given a rank.
"""
def __init__(self, children, label=None):
# Children are assumed to be sorted by RankTree.canonical_order
self.children = children
if len(children) == 0:
self.num_leaves = 1
self.labels = [label]
else:
self.num_leaves = sum(c.num_leaves for c in children)
self.labels = list(heapq.merge(*(c.labels for c in children)))
self._shape_rank = None
self._label_rank = None
def compute_shape_rank(self):
"""
Mirroring the way in which unlabelled trees are enumerated, we must
first calculate the number of trees whose partitions of number of leaves
rank lesser than this tree's partition.
Once we reach the partition of leaves in this tree, we examine the
groups of child subtrees assigned to subsequences of the partition.
For each group of children with the same number of leaves, k, the trees
in that group were selected according to a combination with replacement
of those trees from S(k). By finding the rank of that combination,
we find how many combinations preceded the current one in that group.
That rank is then multiplied by the total number of arrangements that
could be made in the following groups, added to the total rank,
and then we recur on the rest of the group and groups.
"""
part = self.leaf_partition()
total = 0
for prev_part in partitions(self.num_leaves):
if prev_part == part:
break
total += num_tree_pairings(prev_part)
child_groups = self.group_children_by_num_leaves()
next_child_idx = 0
for g in child_groups:
next_child_idx += len(g)
k = g[0].num_leaves
S_k = num_shapes(k)
child_ranks = [c.shape_rank() for c in g]
g_rank = Combination.with_replacement_rank(child_ranks, S_k)
# TODO precompute vector before loop
rest_part = part[next_child_idx:]
total_rest = num_tree_pairings(rest_part)
total += g_rank * total_rest
return total
def compute_label_rank(self):
"""
Again mirroring how we've labeled a particular tree, T, we can rank the
labelling on T.
We group the children into symmetric groups. In the context of labelling,
symmetric groups contain child trees that are of the same shape. Each
group contains a combination of labels selected from all the labels
available to T.
The different variables to consider are:
1. How to assign a combination of labels to the first group.
2. Given a combination of labels assigned to the group, how can we
distribute those labels to each tree in the group.
3. Given an assignment of the labels to each tree in the group, how many
distinct ways could all the trees in the group be labelled.
These steps for generating labelled trees break down the stages of
ranking them.
For each group G, we can find the rank of the combination of labels
assigned to G. This rank times the number of ways the trees in G
could be labelled, times the number of possible labellings of the
rest of the trees, gives the number of labellings that precede those with
the given combination of labels assigned to G. This process repeats and
breaks down to give the rank of the assignment of labels to trees in G,
and the label ranks of the trees themselves in G.
"""
all_labels = self.labels
child_groups = self.group_children_by_shape()
total = 0
for i, g in enumerate(child_groups):
rest_groups = child_groups[i + 1 :]
g_labels = list(heapq.merge(*(t.labels for t in g)))
num_rest_labellings = num_list_of_group_labellings(rest_groups)
# Preceded by all of the ways to label all the groups
# with a lower ranking combination given to g.
comb_rank = Combination.rank(g_labels, all_labels)
num_g_labellings = num_group_labellings(g)
preceding_comb = comb_rank * num_g_labellings * num_rest_labellings
# Preceded then by all the configurations of g ranking less than
# the current one
rank_from_g = group_rank(g) * num_rest_labellings
total += preceding_comb + rank_from_g
all_labels = set_minus(all_labels, g_labels)
return total
# TODO I think this would boost performance if it were a field and not
# recomputed.
def num_labellings(self):
child_groups = self.group_children_by_shape()
return num_list_of_group_labellings(child_groups)
def rank(self):
return Rank(self.shape_rank(), self.label_rank())
def shape_rank(self):
if self._shape_rank is None:
self._shape_rank = self.compute_shape_rank()
return self._shape_rank
def label_rank(self):
if self._label_rank is None:
assert self.shape_rank() is not None
self._label_rank = self.compute_label_rank()
return self._label_rank
@staticmethod
def unrank(num_leaves, rank, labels=None):
"""
Produce a ``RankTree`` of the given ``rank`` with ``num_leaves`` leaves,
labelled with ``labels``. Labels must be sorted, and if ``None`` default
to ``[0, num_leaves)``.
"""
shape_rank, label_rank = rank
if shape_rank < 0 or label_rank < 0:
raise ValueError("Rank is out of bounds.")
unlabelled = RankTree.shape_unrank(num_leaves, shape_rank)
return unlabelled.label_unrank(label_rank, labels)
@staticmethod
def shape_unrank(n, shape_rank):
"""
Generate an unlabelled tree with n leaves with a shape corresponding to
the `shape_rank`.
"""
part, child_shape_ranks = children_shape_ranks(shape_rank, n)
children = [
RankTree.shape_unrank(k, rk) for k, rk in zip(part, child_shape_ranks)
]
t = RankTree(children=children)
t._shape_rank = shape_rank
return t
def label_unrank(self, label_rank, labels=None):
"""
Generate a tree with the same shape, whose leaves are labelled
from ``labels`` with the labelling corresponding to ``label_rank``.
"""
if labels is None:
labels = list(range(self.num_leaves))
if self.is_leaf():
if label_rank != 0:
raise ValueError("Rank is out of bounds.")
return RankTree(children=[], label=labels[0])
child_groups = self.group_children_by_shape()
child_labels, child_label_ranks = children_label_ranks(
child_groups, label_rank, labels
)
children = self.children
labelled_children = [
RankTree.label_unrank(c, c_rank, c_labels)
for c, c_rank, c_labels in zip(children, child_label_ranks, child_labels)
]
t = RankTree(children=labelled_children)
t._shape_rank = self.shape_rank()
t._label_rank = label_rank
return t
@staticmethod
def canonical_order(c):
"""
Defines the canonical ordering of sibling subtrees.
"""
return c.num_leaves, c.shape_rank(), c.min_label()
@staticmethod
def from_tsk_tree_node(tree, u):
if tree.is_leaf(u):
return RankTree(children=[], label=u)
if tree.num_children(u) == 1:
raise ValueError("Cannot rank trees with unary nodes")
children = list(
sorted(
(RankTree.from_tsk_tree_node(tree, c) for c in tree.children(u)),
key=RankTree.canonical_order,
)
)
return RankTree(children=children)
@staticmethod
def from_tsk_tree(tree):
if tree.num_roots != 1:
raise ValueError("Cannot rank trees with multiple roots")
return RankTree.from_tsk_tree_node(tree, tree.root)
def to_tsk_tree(self, span=1, branch_length=1):
"""
Convert a ``RankTree`` into the only tree in a new tree sequence. Internal
nodes and their times are assigned via a postorder traversal of the tree.
:param float span: The genomic span of the returned tree. The tree will cover
the interval :math:`[0, span)` and the :attr:`~Tree.tree_sequence` from which
the tree is taken will have its :attr:`~tskit.TreeSequence.sequence_length`
equal to ``span``.
:param float branch_length: The minimum length of a branch in the returned
tree.
"""
if set(self.labels) != set(range(self.num_leaves)):
raise ValueError("Labels set must be equivalent to [0, num_leaves)")
tables = tskit.TableCollection(span)
def add_node(node):
if node.is_leaf():
assert node.label is not None
return node.label
child_ids = [add_node(child) for child in node.children]
max_child_time = max(tables.nodes.time[c] for c in child_ids)
parent_id = tables.nodes.add_row(time=max_child_time + branch_length)
for child_id in child_ids:
tables.edges.add_row(0, span, parent_id, child_id)
return parent_id
for _ in range(self.num_leaves):
tables.nodes.add_row(flags=tskit.NODE_IS_SAMPLE, time=0)
add_node(self)
# The way in which we're inserting nodes doesn't necessarily
# adhere to the ordering constraint on edges, so we have
# to sort.
tables.sort()
return tables.tree_sequence().first()
@staticmethod
def all_labelled_trees(n):
"""
Generate all unordered, leaf-labelled trees with n leaves.
"""
for tree in RankTree.all_unlabelled_trees(n):
yield from RankTree.all_labellings(tree)
@staticmethod
def all_unlabelled_trees(n):
"""
Generate all tree shapes with n leaves. See :ref:`sec_combinatorics`
for how tree shapes are enumerated.
"""
if n == 1:
yield RankTree(children=[])
else:
for part in partitions(n):
for subtree_pairing in RankTree.all_subtree_pairings(
group_partition(part)
):
yield RankTree(children=subtree_pairing)
@staticmethod