Trees¶
Newick I/O and the quarimo array-layout tree representation.
HifukuTree
dataclass
¶
Unrooted binary phylogenetic tree, stored as parallel numpy arrays.
Node indices follow the quarimo layout:
- Leaves occupy indices
0ton_leaves - 1. - Non-root internal nodes occupy indices
n_leaveston_nodes - 2. - The root occupies index
n_nodes - 1.
Which leaf holds which index, and which internal node holds which, is not
fixed by the layout. Parsing puts the leaves in left-first depth-first
order and numbers the internal nodes as it goes. A survey renumbers to one
shared order instead, because its trees have to be numbered alike to be
compared, stored and continued, and because a walk rewires nodes in place
and leaves them wherever its moves put them. See
:func:reference_leaf_order and :func:canonical.
Parsing binarizes every multifurcation. Each k-ary node (k > 2) becomes a chain of binary nodes, joined by zero-length edges.
Attributes:
| Name | Type | Description |
|---|---|---|
parent |
ndarray
|
int32, shape (n_nodes,). Parent index of each node. The root has -1. |
left_child |
ndarray
|
int32, shape (n_nodes,). Left child index. Leaf nodes have -1. |
right_child |
ndarray
|
int32, shape (n_nodes,). Right child index. Leaf nodes have -1. |
edge_len |
ndarray
|
float64, shape (n_nodes,). Length of the edge from the node to its parent. The root has NaN, because it has no parent edge. |
names |
list[str]
|
Length n_nodes, one entry per node. Leaf entries hold the taxon label. Internal entries hold the empty string. This length equals n_nodes, not n_leaves. To get the taxon count, use n_leaves, n_taxa, or leaf_labels, not len(names). |
n_nodes |
int
|
Total number of nodes: leaves, plus internal nodes, plus the root. |
n_leaves |
int
|
Number of leaf (tip) nodes. This is the taxon count. |
root |
int
|
Index of the root node. This index always equals n_nodes - 1. |
had_multifurcations |
bool
|
True if the input Newick string had a polytomy. Parsing binarizes every polytomy, so this flag is the only record that one existed. |
global_leaf_indices |
ndarray or None
|
int32, shape (n_nodes,). For leaf nodes 0 to n_leaves - 1, the
global TaxonTable index of that leaf's taxon. Internal nodes and
the root hold -1. |
Notes
- Ref: quarimo (array layout and iterative parser pattern).
- Minh et al. (2020) : zero-length branch convention for polytomies.
Source code in src/hifuku/tree_io.py
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 | |
node_order
class-attribute
instance-attribute
¶
int32, shape (n_nodes - n_leaves - 1,). Holds the non-root internal nodes in DFS post-order, with each child listed before its parent. The Felsenstein peeling traversal uses this order.
leaf_labels
property
¶
Taxon labels for the leaf nodes, in index order 0 to n_leaves - 1.
compute_node_order
¶
Compute the non-root internal nodes in DFS post-order.
This method lists each child before its parent. It walks the tree using the left_child and right_child pointers, so the result is correct for any node index assignment.
Returns:
| Type | Description |
|---|---|
ndarray
|
int32, shape (n_nodes - n_leaves - 1,). Non-root internal nodes in DFS post-order. |
Source code in src/hifuku/tree_io.py
full_value
¶
The leaf values of the whole taxon set, combined.
clade_value
¶
The leaf values below node v, combined.
Collects the subtree, so the cost is the subtree's size. A caller that
wants every node's value sweeps the tree once instead, which
:meth:split_lengths does.
Source code in src/hifuku/tree_io.py
split_key
¶
Bipartition key of the edge above node v.
The same for a clade and its complement, so it does not depend on where
the tree is rooted. full is :meth:full_value, which a caller
keying many edges computes once.
Source code in src/hifuku/tree_io.py
masked_leaf_values
¶
Per-leaf values, zero for a leaf outside shared_labels.
Two trees over different taxon sets are compared on the taxa they share,
and the comparison reads the subtree each induces on that set. Zero is
the identity of :func:combine_leaf_values, so a leaf given zero adds
nothing to any clade above it and the value below a node comes out equal
to the value below the matching node of self.restrict(shared_labels).
Returns:
| Type | Description |
|---|---|
ndarray
|
uint64, shape |
Source code in src/hifuku/tree_io.py
split_lengths
¶
Map each unrooted split of the tree to its total branch length.
This is the vector form of a tree under the branch score: a tree is the lengths it assigns to its splits, and two trees are compared by comparing those lengths split by split.
The two root-child edges share a key, because they are the two halves of
one split, so their lengths sum. Every other split maps to one edge.
The map therefore holds n_nodes - 2 entries and describes the
unrooted tree.
The keys are in ascending order, and that order is part of the contract. A consumer adds the values in the order given, without sorting them again, and gets a result that does not depend on the rooting. Rooting the same tree on another edge, or writing the children of a node the other way round, visits the nodes in another order, and floating-point addition is not associative. Ascending key order holds a sum steady to the last bit, which keeps a tree that sits on a niche boundary in one niche.
The keys come from one compiled post-order sweep, which carries each
clade's value up to its parent. Asking each edge for the leaf set below
it instead collects that subtree, so the work summed to the tree's total
path length: O(n log n) on a balanced tree and O(n squared) on a
caterpillar, against O(n) here. The two agree bit for bit, which
tests/test_split_core.py holds them to.
Kuhner & Felsenstein (1994) : the split-length vector a branch score compares.
Source code in src/hifuku/tree_io.py
split_arrays
¶
(keys, lengths) of the unrooted splits, sorted by key.
The array form of :meth:split_lengths, holding the same pairs in the
same order. A caller comparing two trees matches their keys by
searching two sorted lists rather than looking each one up in a map.
Source code in src/hifuku/tree_io.py
as_newick
¶
write_newick
¶
restrict
¶
Return a new HifukuTree pruned to the taxa in labels.
Pruning can leave degree-2 internal nodes. This method removes each one and adds its edge length to the surviving child's edge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
list[str]
|
Taxon labels to keep. Every label must already be a leaf label in this tree. |
required |
Returns:
| Type | Description |
|---|---|
HifukuTree
|
A new, independent tree that contains only the given taxa. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a label in labels is not a leaf label in this tree. |
Notes
- Sukumaran & Holder (2010) : restriction semantics.
Source code in src/hifuku/tree_io.py
|
read_tree
¶
read_tree(source: Union[str, Path], taxon_table: TaxonTable, *, from_string: bool = False) -> HifukuTree
Parse a Newick tree and register its leaf taxa into taxon_table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str or Path
|
Path to a Newick file, or a Newick string when from_string is True. |
required |
taxon_table
|
TaxonTable
|
Shared global TaxonTable; leaf taxa are registered (added if absent). |
required |
from_string
|
bool
|
If True, treat source as a Newick string rather than a file path. |
False
|
Returns:
| Type | Description |
|---|---|
HifukuTree
|
Parsed tree with node_order set in DFS post-order. |