Trees¶
Newick / IQ-TREE tree parsing, conversion from scipy linkage matrices, and dendrogram rendering for dot-plot and heatmap axes. See Similarity & Clustering for guidance.
Tree
¶
A rooted tree with named leaves.
Construct via :meth:from_newick, :meth:read, or
:meth:from_linkage; the leaf order (:meth:leaf_names) defines the
plot row order when the tree is passed to
:meth:dot_explorer.DotPlotter.plot or
:func:dot_explorer.plot_similarity_heatmap.
Source code in dot_explorer/tree.py
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 | |
Methods:¶
from_newick(text)
classmethod
¶
Parse a newick string (IQ-TREE .treefile compatible).
Handles quoted labels ('...' with '' escapes), bracketed
comments, missing branch lengths, and internal support labels in
both the )95:0.1 and )label/95:0.1 styles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Newick source; everything after the first |
required |
Returns:
| Type | Description |
|---|---|
Tree
|
The parsed tree. |
Raises:
| Type | Description |
|---|---|
ValueError
|
On malformed newick (unbalanced parentheses, unterminated quotes, empty input, or duplicate leaf names). |
Source code in dot_explorer/tree.py
read(path)
classmethod
¶
Read a newick tree from path (.nwk/.newick/.treefile/.txt).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or Path
|
File containing a single newick tree. |
required |
Returns:
| Type | Description |
|---|---|
Tree
|
The parsed tree. |
Source code in dot_explorer/tree.py
from_linkage(Z, labels)
classmethod
¶
Build a tree from a scipy linkage matrix.
Branch lengths are derived from merge heights, so cutting the
tree at distance d from the tips matches
scipy.cluster.hierarchy.fcluster(Z, t=d, criterion='distance').
The leaf order matches scipy's default (unsorted) dendrogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Z
|
ndarray
|
|
required |
labels
|
sequence of str
|
Names of the |
required |
Returns:
| Type | Description |
|---|---|
Tree
|
Ultrametric tree with one leaf per label. |
Source code in dot_explorer/tree.py
leaves()
¶
Return the leaf nodes in left-to-right (plot top-to-bottom) order.
Source code in dot_explorer/tree.py
leaf_names()
¶
max_depth()
¶
Return the largest root-to-leaf cumulative branch length.
When the tree carries no branch lengths at all, every edge counts as 1.0; otherwise missing lengths count as 0.0.
Source code in dot_explorer/tree.py
validate_labels(seq_names)
¶
Check that tree tips and sequence names match exactly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seq_names
|
sequence of str
|
The sequence names the tree will be plotted against. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
When any tip lacks a sequence or any sequence lacks a tip; the message lists both directions of the mismatch. |
Source code in dot_explorer/tree.py
TreeNode
dataclass
¶
A node in a :class:Tree.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str or None
|
Leaf label, or internal-node label when present (IQ-TREE writes support values here). |
length |
float or None
|
Branch length to the parent, or |
support |
float or None
|
Numeric support value parsed from an internal-node label
( |
children |
list of TreeNode
|
Child nodes; empty for leaves. |
Source code in dot_explorer/tree.py
draw_tree(ax, tree, leaf_pos, *, orientation='left', cutoff=None, scalebar=True, leaf_labels=False, label_size=None, color='0.2', lw=1.0, gid_prefix='de-tree')
¶
Draw tree as a rectangular dendrogram on ax.
The tree is drawn with the root at the left edge and tips pointing right (toward the plot the axis sits beside). The caller supplies the y position of every leaf via leaf_pos, so tips line up with plot rows of any height; the y limits of ax are left untouched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Axes
|
Axis to draw into; spines and ticks are hidden. |
required |
tree
|
Tree
|
Tree to draw. |
required |
leaf_pos
|
dict of str to float
|
Maps each leaf name to its y coordinate in ax data space. |
required |
orientation
|
str
|
Only |
'left'
|
cutoff
|
float or None
|
When given, draw a dashed vertical line at this distance from the tips (matching a clustering cutoff for ultrametric trees). |
None
|
scalebar
|
bool
|
Draw a branch-length scale bar below the tree. |
True
|
leaf_labels
|
bool
|
Write each leaf name at its tip, right-aligned against the axis edge nearest the plot. |
False
|
label_size
|
float or None
|
Font size for leaf labels (default: matplotlib small). |
None
|
color
|
str
|
Line colour. |
'0.2'
|
lw
|
float
|
Line width. |
1.0
|
gid_prefix
|
str
|
SVG gid prefix: segments get |
'de-tree'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
On unsupported orientation or a leaf missing from leaf_pos. |
Source code in dot_explorer/tree.py
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 | |