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, in left-first DFS order. - Non-root internal nodes occupy indices
n_leaveston_nodes - 2. - The root occupies index
n_nodes - 1.
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
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 | |
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
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. |