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
|
.. jupyter-execute::
:hide-code:
import set_working_directory
Trees
-----
.. authors, Gavin Huttley, Tom Elliott
Loading a tree from a file and visualizing it with ``ascii_art()``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
print(tr.ascii_art())
Writing a tree to a file
^^^^^^^^^^^^^^^^^^^^^^^^
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
tr.write("data/temp.tree")
Getting the individual nodes of a tree by name
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
names = tr.get_node_names()
names[:4]
.. jupyter-execute::
names[4:]
names_nodes = tr.get_nodes_dict()
names_nodes["Human"]
.. jupyter-execute::
tr.get_node_matching_name("Mouse")
Getting the name of a node (or a tree)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
hu = tr.get_node_matching_name("Human")
tr.name
.. jupyter-execute::
hu.name
The object type of a tree and its nodes is the same
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
nodes = tr.get_nodes_dict()
hu = nodes["Human"]
type(hu)
.. jupyter-execute::
type(tr)
Working with the nodes of a tree
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Get all the nodes, tips and edges
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
nodes = tr.get_nodes_dict()
for n in nodes.items():
print(n)
only the terminal nodes (tips)
.. jupyter-execute::
for n in tr.iter_tips():
print(n)
for internal nodes (edges) we can use Newick format to simplify the output
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
for n in tr.iter_nontips():
print(n.get_newick())
Getting the path between two tips or edges (connecting edges)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
edges = tr.get_connecting_edges("edge.1", "Human")
for edge in edges:
print(edge.name)
Getting the distance between two nodes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
nodes = tr.get_nodes_dict()
hu = nodes["Human"]
mu = nodes["Mouse"]
hu.distance(mu)
hu.is_tip()
Getting the last common ancestor (LCA) for two nodes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
nodes = tr.get_nodes_dict()
hu = nodes["Human"]
mu = nodes["Mouse"]
lca = hu.last_common_ancestor(mu)
lca
.. jupyter-execute::
type(lca)
Getting all the ancestors for a node
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
hu = tr.get_node_matching_name("Human")
for a in hu.ancestors():
print(a.name)
Getting all the children for a node
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
node = tr.get_node_matching_name("edge.1")
children = list(node.iter_tips()) + list(node.iter_nontips())
for child in children:
print(child.name)
Getting all the distances for a tree
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
dists = tr.get_distances()
We also show how to select a subset of distances involving just one species.
.. jupyter-execute::
human_dists = [names for names in dists if "Human" in names]
for dist in human_dists:
print(dist, dists[dist])
Getting the two nodes that are farthest apart
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
tr.max_tip_tip_distance()
Get the nodes within a given distance
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
hu = tr.get_node_matching_name("Human")
tips = hu.tips_within_distance(0.2)
for t in tips:
print(t)
Rerooting trees
^^^^^^^^^^^^^^^
At a named node
"""""""""""""""
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
print(tr.rooted_at("edge.0").ascii_art())
At the midpoint
"""""""""""""""
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
print(tr.root_at_midpoint().ascii_art())
.. jupyter-execute::
print(tr.ascii_art())
Near a given tip
""""""""""""""""
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
print(tr.ascii_art())
.. jupyter-execute::
print(tr.rooted_with_tip("Mouse").ascii_art())
Tree representations
^^^^^^^^^^^^^^^^^^^^
Newick format
"""""""""""""
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
tr.get_newick()
.. jupyter-execute::
tr.get_newick(with_distances=True)
XML format
""""""""""
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
xml = tr.get_xml()
for line in xml.splitlines():
print(line)
Tree traversal
^^^^^^^^^^^^^^
Here is the example tree for reference:
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
print(tr.ascii_art())
Preorder
""""""""
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
for t in tr.preorder():
print(t.get_newick())
Postorder
"""""""""
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
for t in tr.postorder():
print(t.get_newick())
Selecting subtrees
^^^^^^^^^^^^^^^^^^
One way to do it
""""""""""""""""
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
for tip in tr.iter_nontips():
tip_names = tip.get_tip_names()
print(tip_names)
sub_tree = tr.get_sub_tree(tip_names)
print(sub_tree.ascii_art())
..
We do some file clean up
.. jupyter-execute::
:hide-code:
from cogent3.util.io import remove_files
remove_files(["data/temp.tree", "data/temp.pdf"], error_on_missing=False)
Tree manipulation methods
^^^^^^^^^^^^^^^^^^^^^^^^^
Pruning the tree
""""""""""""""""
Remove internal nodes with only one child. Create new connections
and branch lengths (if tree is a PhyloNode) to reflect the change.
.. jupyter-execute::
from cogent3 import make_tree
simple_tree_string = "(B:0.2,(D:0.4)E:0.5)F;"
simple_tree = make_tree(simple_tree_string)
print(simple_tree.ascii_art())
.. jupyter-execute::
simple_tree.prune()
print(simple_tree.ascii_art())
.. jupyter-execute::
print(simple_tree)
Create a full unrooted copy of the tree
"""""""""""""""""""""""""""""""""""""""
.. jupyter-execute::
from cogent3 import load_tree
tr1 = load_tree("data/test.tree")
print(tr1.get_newick())
.. jupyter-execute::
tr2 = tr1.unrooted_deepcopy()
print(tr2.get_newick())
Transform tree into a bifurcating tree
""""""""""""""""""""""""""""""""""""""
Add internal nodes so that every node has 2 or fewer children.
.. jupyter-execute::
from cogent3 import make_tree
tree_string = "(B:0.2,H:0.2,(C:0.3,D:0.4,E:0.1)F:0.5)G;"
tr = make_tree(tree_string)
print(tr.ascii_art())
.. jupyter-execute::
print(tr.bifurcating().ascii_art())
Transform tree into a balanced tree
"""""""""""""""""""""""""""""""""""
Using a balanced tree can substantially improve performance of
likelihood calculations. Note that the resulting tree has a
different orientation with the effect that specifying clades or
stems for model parameterisation should be done using the
"outgroup_name" argument.
.. jupyter-execute::
from cogent3 import load_tree
tr = load_tree("data/test.tree")
print(tr.ascii_art())
.. jupyter-execute::
print(tr.balanced().ascii_art())
Test two trees for same topology
""""""""""""""""""""""""""""""""
Branch lengths don't matter.
.. jupyter-execute::
from cogent3 import make_tree
tr1 = make_tree("(B:0.2,(C:0.2,D:0.2)F:0.2)G;")
tr2 = make_tree("((C:0.1,D:0.1)F:0.1,B:0.1)G;")
tr1.same_topology(tr2)
Measure topological distances between two trees
"""""""""""""""""""""""""""""""""""""""""""""""
A number of topological tree distance metrics are available. They include:
* The Robinson-Foulds Distance for rooted trees.
* The Matching Cluster Distance for rooted trees.
* The Robinson-Foulds Distance for unrooted trees.
* The Lin-Rajan-Moret Distance for unrooted trees.
There are several variations of the Robinson-Foulds metric in the literature. The definition used by ``cogent3`` is the
cardinality of the symmetric difference of the sets of clades/splits in the two rooted/unrooted trees. Other definitions sometimes
divide this by two, or normalise it to the unit interval.
The Robinson-Foulds distance is quick to compute, but is known to saturate quickly. Moving a single leaf in a tree can maximise this metric.
The Matching Cluster and Lin-Rajan-Moret are two matching-based distances that are more statistically robust.
Unlike the Robinson-Foulds distance which counts how many of the splits/clades are not exactly same, the matching-based distances
measures the degree by which the splits/clades are different. The matching-based distances solve a min-weight matching problem,
which for large trees may take longer to compute.
.. jupyter-execute::
# Distance metrics for rooted trees
from cogent3 import make_tree
tr1 = make_tree(treestring="(a,(b,(c,(d,e))));")
tr2 = make_tree(treestring="(e,(d,(c,(b,a))));")
mc_distance = tr1.tree_distance(tr2, method="matching_cluster") # or method="mc" or method="matching"
rooted_rf_distance = tr1.tree_distance(tr2, method="rooted_robinson_foulds") # or method="rrf" or method="rf"
print("Matching Cluster Distance:", mc_distance)
print("Rooted Robinson Foulds Distance:", rooted_rf_distance)
.. jupyter-execute::
# Distance metrics for unrooted trees
from cogent3 import make_tree
tr1 = make_tree(treestring="(a,b,(c,(d,e)));")
tr2 = make_tree(treestring="((a,c),(b,d),e);")
lrm_distance = tr1.tree_distance(tr2, method="lin_rajan_moret") # or method="lrm" or method="matching"
unrooted_rf_distance = tr1.tree_distance(tr2, method="unrooted_robinson_foulds") # or method="urf" or method="rf"
print("Lin-Rajan-Moret Distance:", lrm_distance)
print("Unrooted Robinson Foulds Distance:", unrooted_rf_distance)
Calculate each node's maximum distance to a tip
"""""""""""""""""""""""""""""""""""""""""""""""
Sets each node's "TipDistance" attribute to be
the distance from that node to its most distant tip.
.. jupyter-execute::
from cogent3 import make_tree
tr = make_tree("(B:0.2,(C:0.3,D:0.4)F:0.5)G;")
print(tr.ascii_art())
.. jupyter-execute::
tr.set_tip_distances()
for t in tr.preorder():
print(t.name, t.TipDistance)
Scale branch lengths in place to integers for ascii output
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.. jupyter-execute::
from cogent3 import make_tree
tr = make_tree("(B:0.2,(C:0.3,D:0.4)F:0.5)G;")
print(tr)
.. jupyter-execute::
tr.scale_branch_lengths()
print(tr)
Get tip-to-tip distances
""""""""""""""""""""""""
Get a distance matrix between all pairs of tips
and a list of the tip nodes.
.. jupyter-execute::
from cogent3 import make_tree
tr = make_tree("(B:3,(C:2,D:4)F:5)G;")
d, tips = tr.tip_to_tip_distances()
for i, t in enumerate(tips):
print(t.name, d[i])
Compare two trees using tip-to-tip distance matrices
""""""""""""""""""""""""""""""""""""""""""""""""""""
Score ranges from 0 (minimum distance) to 1 (maximum
distance). The default is to use Pearson's correlation,
in which case a score of 0 means that the Pearson's
correlation was perfectly good (1), and a score of 1
means that the Pearson's correlation was perfectly bad (-1).
Note: automatically strips out the names that don't match.
.. jupyter-execute::
from cogent3 import make_tree
tr1 = make_tree("(B:2,(C:3,D:4)F:5)G;")
tr2 = make_tree("(C:2,(B:3,D:4)F:5)G;")
tr1.compare_by_tip_distances(tr2)
|