Trees¶
TreeNode
¶
Tree nodes for class Tree.
Attributes:
| Name | Type | Description |
|---|---|---|
parent |
TreeNode
|
Parent node of this node. |
children |
DLList
|
Child nodes of this node in a doubly-linked list. |
Source code in tralda/datastructures/tree.py
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 | |
__init__(**kwargs: Any) -> None
¶
Constructor for TreeNode class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Set arbitrary node attributes using key=value. |
{}
|
Source code in tralda/datastructures/tree.py
__str__() -> str
¶
Return the label of the tree node if available.
Returns:
| Type | Description |
|---|---|
str
|
The label of the tree node if available or an empty string. |
__repr__() -> str
¶
Return a string representation of the tree node.
If available, the label attribute will be contained, otherwise the object ID of the
TreeNode instance.
Returns:
| Type | Description |
|---|---|
str
|
A string representation of the tree node. |
Source code in tralda/datastructures/tree.py
attributes() -> Iterator[tuple[str, Any]]
¶
A generator for the node attributes.
Yields:
| Type | Description |
|---|---|
tuple[str, Any]
|
Pairs of str and the type of the corresponding value. |
Source code in tralda/datastructures/tree.py
add_child(child_node: TreeNode) -> None
¶
Add a node as a child of this node.
Does nothing if the node is already a child node of this node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child_node
|
TreeNode
|
The node to add as a new child to this node. |
required |
Source code in tralda/datastructures/tree.py
add_child_right_of(child_node: TreeNode, right_of: TreeNode) -> None
¶
Add a node as a child of this node as a right sibling of one of its children.
Can also be used to change the position of a child node, i.e., to detach it and re-insert it to the right of the specified node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child_node
|
TreeNode
|
The node to add as a new child to this node. |
required |
right_of
|
TreeNode
|
The child of this node right of which 'child_node' gets inserted. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If 'right_of' is not a child of this node. |
Source code in tralda/datastructures/tree.py
remove_child(child_node: TreeNode) -> None
¶
Remove a child node of this node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child_node
|
TreeNode
|
The node to be removed from the list of children. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the supplied node is not a child of this node. |
Source code in tralda/datastructures/tree.py
detach() -> None
¶
Detach this node from its parent.
The node has no parent afterwards.
is_leaf() -> bool
¶
Return True if the node is a leaf, False otherwise.
Returns:
| Type | Description |
|---|---|
bool
|
True if the node is a leaf, i.e., it has no children, else False. |
child_subsequence(left_node: TreeNode, right_node: TreeNode) -> list[TreeNode]
¶
Consecutive subsequence of children within a left and right bound.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left_node
|
TreeNode
|
The left bound of the subsequence. |
required |
right_node
|
TreeNode
|
The right bound of the subsequence. |
required |
Returns:
| Type | Description |
|---|---|
list[TreeNode]
|
The children in the subsequence. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If 'right_node' or 'left_node' is not a child of this node. |
Source code in tralda/datastructures/tree.py
Tree
¶
Rooted tree whose nodes may have an arbitrary number of children.
Attributes:
| Name | Type | Description |
|---|---|---|
root |
TreeNode
|
The root node of the tree. |
Source code in tralda/datastructures/tree.py
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 | |
__init__(arg: TreeNode | str) -> None
¶
Constructor for the class Tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
TreeNode | str
|
The root node for the newly created tree or a Newick representation of a tree. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If 'arg' is neither a TreeNode instance nor a string. |
Source code in tralda/datastructures/tree.py
__len__() -> int
¶
Size of the tree (number of nodes).
Runs in O(n) where n is the size of the tree.
Returns:
| Type | Description |
|---|---|
int
|
The number of nodes in the tree. |
height() -> int
¶
Height of the tree (number of edges in the longest path from the root to a leaf).
Runs in O(n) where n is the size of the tree. Return -1 if the tree is empty.
Returns:
| Type | Description |
|---|---|
int
|
The height of the tree. |
Source code in tralda/datastructures/tree.py
leaves() -> Iterator[TreeNode]
¶
Generator for leaves of the tree.
Yields:
| Type | Description |
|---|---|
TreeNode
|
The leaf nodes of the tree. |
Source code in tralda/datastructures/tree.py
preorder() -> Iterator[TreeNode]
¶
Generator for a pre-order traversal of the tree.
Yields:
| Type | Description |
|---|---|
TreeNode
|
All nodes of the tree in pre-order. |
Source code in tralda/datastructures/tree.py
preorder_and_level() -> Iterator[tuple[TreeNode, int]]
¶
Generator for a pre-order traversal with node levels.
Yields:
| Type | Description |
|---|---|
tuple[TreeNode, int]
|
Nodes and their level (distance from the root) in pre-order. |
Source code in tralda/datastructures/tree.py
traverse_subtree(node: TreeNode) -> Iterator[TreeNode]
¶
Generator for a pre-order traversal of the subtree rooted at the given node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
TreeNode
|
The node whose subtree to traverse. |
required |
Yields:
| Type | Description |
|---|---|
TreeNode
|
All nodes in the subtree rooted at the given node in pre-order. |
Source code in tralda/datastructures/tree.py
postorder() -> Iterator[TreeNode]
¶
Generator for post-order traversal of the tree.
Yields:
| Type | Description |
|---|---|
TreeNode
|
All nodes of the tree in post-order. |
Source code in tralda/datastructures/tree.py
inner_nodes() -> Iterator[TreeNode]
¶
Generator for inner nodes in pre-order.
Inner nodes are those nodes that do not have any children.
Yields:
| Type | Description |
|---|---|
TreeNode
|
All inner nodes of the tree in pre-order. |
Source code in tralda/datastructures/tree.py
edges() -> Iterator[tuple[TreeNode, TreeNode]]
¶
Generator for all edges of the tree.
Yields:
| Type | Description |
|---|---|
tuple[TreeNode, TreeNode]
|
All edges of the tree as tuples of TreeNode instances. |
Source code in tralda/datastructures/tree.py
edges_sibling_order() -> Iterator[tuple[TreeNode, TreeNode, int]]
¶
Generator for all edges of the tree with sibling order.
Yields:
| Type | Description |
|---|---|
TreeNode
|
Edges uv as tuples (u, v, nr) where nr is the index of v in the list of children of |
TreeNode
|
node u. |
Source code in tralda/datastructures/tree.py
inner_edges() -> Iterator[tuple[TreeNode, TreeNode]]
¶
Generator for all inner edges of the tree.
Yields:
| Type | Description |
|---|---|
tuple[TreeNode, TreeNode]
|
All inner edges uv of the tree, i.e., edges for which the child v of u is not a leaf. |
Source code in tralda/datastructures/tree.py
euler_generator() -> Iterator[TreeNode]
¶
Generator for an Euler tour of the tree.
Yields:
| Type | Description |
|---|---|
TreeNode
|
Nodes in an Euler tour of the tree. |
Source code in tralda/datastructures/tree.py
euler_and_level() -> Iterator[tuple[TreeNode, int]]
¶
Generator for an Euler tour with node levels.
Yields:
| Type | Description |
|---|---|
tuple[TreeNode, int]
|
Nodes and their level (distance from the root) in an Euler tour of the tree. |
Source code in tralda/datastructures/tree.py
leaf_dict() -> dict[TreeNode, list[TreeNode]]
¶
Leaves in the subtree rooted at each node.
Computes the list of leaves for every node in the tree containing the leaf nodes in the subtree rooted at the node.
Returns:
| Type | Description |
|---|---|
dict[TreeNode, list[TreeNode]]
|
The leaves under each vertex (as a dictionary with |
Source code in tralda/datastructures/tree.py
contract(edges: Iterable[tuple[TreeNode]], inplace: bool = True) -> Tree
¶
Contract edges in the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edges
|
Iterable[tuple[TreeNode]]
|
The edges to be contracted in the tree. |
required |
inplace
|
bool
|
If True, the edges are contracted in this tree instance, otherwise a copy is returned and the original tree is not affected. |
True
|
Returns:
| Type | Description |
|---|---|
Tree
|
The original tree with contracted edges or a copy in which the contractions are performed. |
Source code in tralda/datastructures/tree.py
get_triples(label_only: bool = False) -> list[tuple[Any, Any, Any]]
¶
Retrieve a list of all (rooted) triples of the tree.
A tree displays a triple ab|c on the leaf nodes a, b and c if the last common ancestor of a and b is a (proper) descendant of the last common ancestor of a and c (b and c).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label_only
|
bool
|
If True, the triples are represented by the label attribute of the nodes. |
False
|
Returns:
| Type | Description |
|---|---|
list[tuple[Any, Any, Any]]
|
List of tuples where each tuple (a, b, c) represents the triple ab|c (=ba|c), i.e., the first two items are closer related in the tree. |
Source code in tralda/datastructures/tree.py
delete_and_reconnect(node: TreeNode) -> TreeNode | None
¶
Delete a node from the tree and reconnect its parent and children.
The function preserves the 'sibling order' of the remaining nodes of the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
TreeNode
|
The node to be deleted. |
required |
Returns:
| Type | Description |
|---|---|
TreeNode | None
|
The parent of the node, if it could be deleted, or None, if the node could not be deleted, i.e., it has no parent. |
Source code in tralda/datastructures/tree.py
random_leaves(proportion: int | float) -> list[TreeNode]
¶
A random sample of the leaves.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
proportion
|
int | float
|
The proportion of the sample w.r.t. the full set of leaves. |
required |
Returns:
| Type | Description |
|---|---|
list[TreeNode]
|
A randomly samples list of the leaves of the tree. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If 'proportion' is not a number between 0 and 1. |
Source code in tralda/datastructures/tree.py
is_binary() -> bool
¶
Check whether the tree is a binary tree.
Nodes in (rooted) binary trees are either leaves or have exactly two children.
Returns:
| Type | Description |
|---|---|
bool
|
True if the tree is binary, else False. |
Source code in tralda/datastructures/tree.py
is_phylogenetic() -> bool
¶
Check whether the tree is a phylogenetic tree.
Nodes in (rooted) phylogenetic trees are either leaves or have at least two children.
Returns:
| Type | Description |
|---|---|
bool
|
True if the tree is phylogenetic, else False. |
Source code in tralda/datastructures/tree.py
get_hierarchy() -> set[tuple[Any]]
¶
Hierarchy set on the leaf labels defined by the tree.
Every (phylogenetic) tree can be represented by a hierarchy on the set of its leaves. The label attributes of the leaf nodes must be set and unique for each leaf.
Returns:
| Type | Description |
|---|---|
set[tuple[Any]]
|
A set of tuples representing the hierarchy where the leaves are represented by their labels. |
Source code in tralda/datastructures/tree.py
equal_topology(other: Tree, verbose: bool = False) -> bool
¶
Compare the tree topology based on the leaf labels.
Only works for phylogenetic trees with unique leaf labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Tree
|
Another tree which this tree shall be compared to. |
required |
verbose
|
bool
|
If True, print where the equality check failed (the first time). |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the topologies are equal, else False. |
Source code in tralda/datastructures/tree.py
is_refinement(other: Tree) -> bool
¶
Checks whether the tree is a refinement of 'other' based on the leaf labels.
Only works for phylogenetic trees with unique leaf labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Tree
|
Another tree which this tree shall be compared to. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the tree is refinement of 'other', else False. |
Source code in tralda/datastructures/tree.py
copy(mapping: bool = False) -> Tree | tuple[Tree, dict[TreeNode, TreeNode]]
¶
Return a copy of the tree.
Constructs a copy of the tree to the level of nodes, i.e., the attributes are only copied as references. If the node attributes are all immutable data types, the original tree is not affected by operations on the copy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
bool
|
If True, additionally return the mapping from original to copied nodes as dictionary. |
False
|
Returns:
| Type | Description |
|---|---|
Tree | tuple[Tree, dict[TreeNode, TreeNode]]
|
A copy of the tree and optionally the mapping from original to copied nodes. |
Source code in tralda/datastructures/tree.py
to_newick(node: TreeNode | None = None) -> str
¶
Newick representation of the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
TreeNode | None
|
The node whose subtree shall be returned as a Newick string, the default is None, in which case the whole tree is returned in Newick format. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A newick representation of the (sub)tree. |
Source code in tralda/datastructures/tree.py
parse_newick(newick: str) -> Tree
classmethod
¶
Parses trees in Newick format into Tree object.
If available (after colons in the Newick strings), the distance is stored in the 'dist' attribute of the nodes. Moreover, labels are converted to integers if possible.
NOTE: Do not use this function for serialization and reloading Tree objects. Use the 'serialize()' function instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
newick
|
str
|
A tree in Newick format. |
required |
Returns:
| Type | Description |
|---|---|
Tree
|
The parsed tree. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input is not a string. |
ValueError
|
If the input is not a valid Newick string. |
Source code in tralda/datastructures/tree.py
to_nx() -> tuple[nx.DiGraph, int]
¶
Convert a Tree into a NetworkX DiGraph.
The attributes correspond to the node attributes in the resulting graph. The nodes of the resulting graph correspond to the object ids of the TreeNode instances belonging to the tree.
Returns:
| Type | Description |
|---|---|
tuple[DiGraph, int]
|
A graph representation of the tree and the object id of the root (and thus the corresponding node in the graph) in order to be able to completely reconstruct the tree. |
Source code in tralda/datastructures/tree.py
parse_nx(graph: nx.DiGraph, root: int) -> Tree
classmethod
¶
Convert a NetworkX DiGraph version back into a Tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
DiGraph
|
A tree represented as a Networkx DiGraph. |
required |
root
|
int
|
The node in the graph corresponding to the root. |
required |
Returns:
| Type | Description |
|---|---|
Tree
|
The reconstructed tree. |
Source code in tralda/datastructures/tree.py
to_dict() -> dict[str, Any]
¶
Convert the tree into a nested dictionary.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dict representation of the tree, |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the tree is empty. |
Source code in tralda/datastructures/tree.py
parse_dict(tree_dict: dict[str, Any]) -> Tree
classmethod
¶
Convert a dictionary representation back into a Tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree_dict
|
dict[str, Any]
|
A dictionary representation of the tree. |
required |
Returns:
| Type | Description |
|---|---|
Tree
|
The reconstructed tree. |
Source code in tralda/datastructures/tree.py
serialize(filename: str, mode: str = None) -> None
¶
Serialize the tree using pickle or json.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The filename (including the path) of the file to be created. |
required |
mode
|
str
|
The serialization mode. Supported are pickle and json. The default is None in which case the mode is inferred from the file extension. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the serialization mode is unknown or could not be inferred. |
Source code in tralda/datastructures/tree.py
load(filename: str, mode: bool = None) -> Tree
classmethod
¶
Reload a Tree from a file (pickle or json).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The filename (including the path) of the file to be loaded. |
required |
mode
|
bool
|
The serialization mode. Supported are pickle and json. The default is None in which case the mode is inferred from the file extension. |
None
|
Returns:
| Type | Description |
|---|---|
Tree
|
The tree reloaded from file. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the serialization mode is unknown or could not be inferred. |
Source code in tralda/datastructures/tree.py
print_tree(child_indentation: int = 3) -> None
¶
Print a representation of the tree to the console.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child_indentation
|
int
|
The indentation added per level of the nodes. Must be >= 1. |
3
|
Source code in tralda/datastructures/tree.py
plot(*, backend: str = 'matplotlib', path: str | os.PathLike | None = None, show: bool = False, **kwargs: Any) -> Any
¶
Render this tree; see :func:tralda.visualization.plot_tree for all parameters.
Requires the optional visualization dependencies, e.g. pip install tralda[matplotlib],
tralda[plotly], or tralda[viz] for both backends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
str
|
|
'matplotlib'
|
path
|
str | PathLike | None
|
If given, save the resulting figure to this path. |
None
|
show
|
bool
|
If |
False
|
**kwargs
|
Any
|
Additional keyword arguments forwarded to
:func: |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
|
Source code in tralda/datastructures/tree.py
random_tree(number_of_leaves: int, binary: bool = False) -> Tree
classmethod
¶
A simple function to generate a random tree.
The tree is generated by iteratively sampling a random node to which a new leaf is attached (or two leaves if the sampled node was a leaf). Each step increases the number of leaves by one. The procedure ends as soon as the desired number of leaves is reached. The resulting tree is always phylogenetic, i.e., each inner node has at least two children.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number_of_leaves
|
int
|
The desired number of leaves. |
required |
binary
|
bool
|
If True, the resulting tree is binary, otherwise it may contain multifurcations. |
False
|
Returns:
| Type | Description |
|---|---|
Tree
|
A randomly generated tree with the specified number of leaves. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in tralda/datastructures/tree.py
LCA
¶
Compute last common ancestors in a tree efficiently.
Uses a reduction to a +/-1 Range minimum query (RMQ) problem and a sparse table implementation. - Preprocessing complexity: O(n) where n is the number of nodes in the tree - Query complexity: O(1)
References
- M. A. Bender, M. Farach-Colton, G. Pemmasani, S. Skiena, P. Sumazin. Lowest common ancestors in trees and directed acyclic graphs. In: Journal of Algorithms. 57, Nr. 2, November 2005, S. 75-94. DOI: 10.1016/j.jalgor.2005.08.001
- https://cp-algorithms.com/data_structures/sparse-table.html
- https://cp-algorithms.com/graph/lca_farachcoltonbender.html
Source code in tralda/datastructures/last_common_ancestor.py
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 | |
__init__(tree: Tree, strict_labels: bool = True) -> None
¶
Constructor for class LCA.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
Tree
|
The Tree instance for which this instance will allow efficient last common ancestor queries. |
required |
strict_labels
|
bool
|
If True (default), raise a |
True
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in tralda/datastructures/last_common_ancestor.py
__call__(a: NodeType, b: NodeType) -> TreeNode
¶
Last common ancestor of two nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
NodeType
|
A node or its label in the tree corresponding to this LCA instance. |
required |
b
|
NodeType
|
A node or its label in the tree corresponding to this LCA instance. |
required |
Returns:
| Type | Description |
|---|---|
TreeNode
|
The last common ancestor of a and b. |
Source code in tralda/datastructures/last_common_ancestor.py
get(a: NodeType, b: NodeType) -> TreeNode
¶
Last common ancestor of two nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
NodeType
|
A node or its label in the tree corresponding to this LCA instance. |
required |
b
|
NodeType
|
A node or its label in the tree corresponding to this LCA instance. |
required |
Returns:
| Type | Description |
|---|---|
TreeNode
|
The last common ancestor of a and b. |
Source code in tralda/datastructures/last_common_ancestor.py
displays_triple(a: NodeType, b: NodeType, c: NodeType) -> bool
¶
Return whether the tree displays the rooted triple ab|c (= ba|c).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
NodeType
|
A node or its label in the tree corresponding to this LCA instance. |
required |
b
|
NodeType
|
A node or its label in the tree corresponding to this LCA instance. |
required |
c
|
NodeType
|
A node or its label in the tree corresponding to this LCA instance. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the tree displays the triple ab|c (= ba|c); False otherwise. |
Source code in tralda/datastructures/last_common_ancestor.py
are_comparable(u: NodeOrEdge, v: NodeOrEdge) -> bool
¶
Returns True if two nodes/edges are comparable in the tree.
Two nodes/edges are comparable if one lies on the unique path from the other to the root of the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
NodeOrEdge
|
A node or edge in the tree corresponding to this LCA instance. |
required |
v
|
NodeOrEdge
|
A node or edge in the tree corresponding to this LCA instance. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if u and v are comparable in the tree, else False. |
Source code in tralda/datastructures/last_common_ancestor.py
ancestor_or_equal(u: NodeOrEdge, v: NodeOrEdge) -> bool
¶
Return True if u is equal to or an ancestor of v.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
NodeOrEdge
|
A node or edge in the tree corresponding to this LCA instance. |
required |
v
|
NodeOrEdge
|
A node or edge in the tree corresponding to this LCA instance. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if u is equal or an ancestor of v, else False. |
Source code in tralda/datastructures/last_common_ancestor.py
ancestor_not_equal(u: NodeOrEdge, v: NodeOrEdge) -> bool
¶
Return True if u is a strict ancestor of v.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
NodeOrEdge
|
A node or edge in the tree corresponding to this LCA instance. |
required |
v
|
NodeOrEdge
|
A node or edge in the tree corresponding to this LCA instance. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if u is a strict ancestor of v, else False. |
Source code in tralda/datastructures/last_common_ancestor.py
descendant_or_equal(u: NodeOrEdge, v: NodeOrEdge) -> bool
¶
Return True if u is equal to or a descendant of v.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
NodeOrEdge
|
A node or edge in the tree corresponding to this LCA instance. |
required |
v
|
NodeOrEdge
|
A node or edge in the tree corresponding to this LCA instance. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if u is equal or a descendant of v, else False. |
Source code in tralda/datastructures/last_common_ancestor.py
descendant_not_equal(u: NodeOrEdge, v: NodeOrEdge) -> bool
¶
Return True if u is a strict descendant of v.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
NodeOrEdge
|
A node or edge in the tree corresponding to this LCA instance. |
required |
v
|
NodeOrEdge
|
A node or edge in the tree corresponding to this LCA instance. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if u is a strict descendant of v, else False. |
Source code in tralda/datastructures/last_common_ancestor.py
consistent_triples(triples: Iterable[Triple]) -> list[Triple]
¶
List with the subset of triples that are displayed by the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triples
|
Iterable[Triple]
|
An iterable of triples of which each may or may not be displayed by the tree. |
required |
Returns:
| Type | Description |
|---|---|
list[Triple]
|
A list containing the subset of the input list that are displayed by the tree. |
Source code in tralda/datastructures/last_common_ancestor.py
consistent_triple_generator(triples: Iterable[Triple]) -> Iterator[Triple]
¶
Generator for the items in 'triples' that are displayed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triples
|
Iterable[Triple]
|
An iterable of triples of which each may or may not be displayed by the tree. |
required |
Yields:
| Type | Description |
|---|---|
Triple
|
The triples in the input list that is displayed by the tree. |