API Reference
Library usage not recommended
I recommend using TACT mainly via its console commands for stability and best results. To get started, see the Tutorial.
While TACT can be used as a Python library, its internal interface is subject to change at any time, even for minor or patch versions. This API documentation is provided merely for the sake of completeness.
Numerical functions
Functions in tact/lib.py.
Functions to handle various numerical operations, including optimization.
crown_capture_probability(n: int, k: int) -> float
Calculate probability of observing the crown node in an incomplete sample.
Probability that a random sample of k taxa from a clade of n total taxa
includes the crown (root) node, under a Yule process.
Reference: Sanderson (1996), Systematic Biology 45:168-173.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Total number of taxa in the clade. |
required |
k
|
int
|
Number of sampled taxa. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability of crown node inclusion. |
Raises:
| Type | Description |
|---|---|
Exception
|
If |
Source code in tact/lib.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
get_bd(r: float, a: float) -> tuple[float, float]
Convert turnover and relative extinction to birth and death rates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r
|
float
|
Turnover rate (net diversification, birth - death). |
required |
a
|
float
|
Relative extinction rate (death / birth). |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
Tuple of (birth rate, death rate). |
Source code in tact/lib.py
40 41 42 43 44 45 46 47 48 49 50 | |
get_new_times(ages: list[float], birth: float, death: float, missing: int, told: float | None = None, tyoung: float | None = None) -> list[float]
Simulate new speciation event times in an incomplete phylogeny.
Simulates missing speciation events under a constant-rate birth-death process.
Adapted from TreeSim::corsim by Tanja Stadler. Reference: Cusimano et al.
(2012), Systematic Biology 61(5):785-792.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ages
|
list[float]
|
List of existing waiting times (will be sorted in place). |
required |
birth
|
float
|
Birth rate. |
required |
death
|
float
|
Death rate. |
required |
missing
|
int
|
Number of missing taxa to simulate. |
required |
told
|
float | None
|
Maximum simulated age. Defaults to |
None
|
tyoung
|
float | None
|
Minimum simulated age. Defaults to 0. |
None
|
Returns:
| Type | Description |
|---|---|
list[float]
|
List of simulated waiting times, sorted in descending order. |
Raises:
| Type | Description |
|---|---|
Exception
|
If zero or negative branch lengths are detected. |
Source code in tact/lib.py
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 | |
get_ra(b: float, d: float) -> tuple[float, float]
Convert birth and death rates to turnover and relative extinction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
float
|
Birth rate. |
required |
d
|
float
|
Death rate. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
Tuple of (turnover rate, relative extinction rate). |
Source code in tact/lib.py
53 54 55 56 57 58 59 60 61 62 63 | |
intp1(t: float, l: float, m: float) -> float
Compute integration constant for sampling missing speciation event times.
This is a portion of the cdf used to perform inverse-transform sampling of missing speciation event times
under a constant-rate birth-death model. It is the c_2 term from equation A.2 in Cusimano et al. (2012),
Systematic Biology 61(5):785-792. Originally implemented as TreeSim:::intp1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
float
|
Time before present. |
required |
l
|
float
|
Birth rate (lambda). |
required |
m
|
float
|
Death rate (mu). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Integration constant value. |
Source code in tact/lib.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
intp1_exact(t: float, l: float, m: float) -> D
Compute intp1 using exact Decimal arithmetic.
Used as fallback when floating-point arithmetic fails due to overflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
float
|
Time before present. |
required |
l
|
float
|
Birth rate (lambda). |
required |
m
|
float
|
Death rate (mu). |
required |
Returns:
| Type | Description |
|---|---|
Decimal
|
Integration constant as a Decimal. |
Source code in tact/lib.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | |
lik_constant(vec: tuple[float, float], rho: float, t: list[float], root: int = 1, survival: int = 1, p1: Callable[[float, float, float, float], float] = p1) -> float
Calculate likelihood of a constant-rate birth-death process.
Likelihood conditioned on waiting times and incomplete sampling. Based on
TreePar::LikConstant by Tanja Stadler. Reference: Stadler (2009), Journal
of Theoretical Biology 261:58-66.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vec
|
tuple[float, float]
|
Tuple of (birth rate, death rate). |
required |
rho
|
float
|
Sampling fraction. |
required |
t
|
list[float]
|
List of waiting times (will be sorted in place). |
required |
root
|
int
|
Include root contribution (1) or not (0). Default: 1. |
1
|
survival
|
int
|
Assume process survival (1) or not (0). Default: 1. |
1
|
p1
|
Callable[[float, float, float, float], float]
|
Function to compute p1 probability (default: |
p1
|
Returns:
| Type | Description |
|---|---|
float
|
Negative log-likelihood value. |
Source code in tact/lib.py
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 | |
optim_bd(ages: list[float], sampling: float, min_bound: float = 1e-09) -> tuple[float, float]
Optimize birth and death rates from node ages under a birth-death model.
Uses maximum likelihood estimation with the Magallon-Sanderson crown estimator for initial values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ages
|
list[float]
|
List of node ages (splitting times). |
required |
sampling
|
float
|
Sampling fraction in (0, 1]. |
required |
min_bound
|
float
|
Minimum allowed birth rate (default: 1e-9). |
1e-09
|
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
Tuple of (optimized birth rate, optimized death rate). |
Source code in tact/lib.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
optim_yule(ages: list[float], sampling: float, min_bound: float = 1e-09) -> tuple[float, float]
Optimize birth rate under a Yule (pure birth) model.
Assumes zero extinction rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ages
|
list[float]
|
List of node ages (splitting times). |
required |
sampling
|
float
|
Sampling fraction in (0, 1]. |
required |
min_bound
|
float
|
Minimum allowed birth rate (default: 1e-9). |
1e-09
|
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
Tuple of (optimized birth rate, 0.0). |
Raises:
| Type | Description |
|---|---|
Exception
|
If optimization fails. |
Source code in tact/lib.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
p0(t: float, l: float, m: float, rho: float) -> float
Compute the probability of no sampled descendants.
Probability that an individual alive at time t before present has no sampled
descendants (extant or extinct), assuming no past sampling. Falls back to
exact Decimal arithmetic if floating-point overflow occurs.
Reference: Stadler (2010), Journal of Theoretical Biology 267(3):396-404,
remark 3.2. Originally implemented as TreePar:::p0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
float
|
Time before present. |
required |
l
|
float
|
Birth rate (lambda). |
required |
m
|
float
|
Death rate (mu). |
required |
rho
|
float
|
Sampling fraction. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability of no sampled descendants. |
Source code in tact/lib.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
p0_exact(t: float, l: float, m: float, rho: float) -> D
Compute p0 using exact Decimal arithmetic.
Used as fallback when floating-point arithmetic fails due to overflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
float
|
Time before present. |
required |
l
|
float
|
Birth rate (lambda). |
required |
m
|
float
|
Death rate (mu). |
required |
rho
|
float
|
Sampling fraction. |
required |
Returns:
| Type | Description |
|---|---|
Decimal
|
Probability of no sampled descendants as a Decimal. |
Source code in tact/lib.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
p1(t: float, l: float, m: float, rho: float) -> float
Compute the probability of exactly one sampled descendant.
Probability that an individual alive at time t before present has precisely
one sampled extant descendant and no sampled extinct descendants, assuming
no past sampling. Optimized version using common subexpression elimination.
Reference: Stadler (2010), Journal of Theoretical Biology 267(3):396-404,
remark 3.2. Originally implemented as TreePar:::p1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
float
|
Time before present. |
required |
l
|
float
|
Birth rate (lambda). |
required |
m
|
float
|
Death rate (mu). |
required |
rho
|
float
|
Sampling fraction. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability of exactly one sampled descendant. |
Source code in tact/lib.py
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 | |
p1_exact(t: float, l: float, m: float, rho: float) -> D
Compute p1 using exact Decimal arithmetic.
Used as fallback when floating-point arithmetic fails due to overflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
float
|
Time before present. |
required |
l
|
float
|
Birth rate (lambda). |
required |
m
|
float
|
Death rate (mu). |
required |
rho
|
float
|
Sampling fraction. |
required |
Returns:
| Type | Description |
|---|---|
Decimal
|
Probability of exactly one sampled descendant as a Decimal. |
Source code in tact/lib.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
p1_orig(t: float, l: float, m: float, rho: float) -> float
Original implementation of p1 for testing and comparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
float
|
Time before present. |
required |
l
|
float
|
Birth rate (lambda). |
required |
m
|
float
|
Death rate (mu). |
required |
rho
|
float
|
Sampling fraction. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Probability of exactly one sampled descendant. |
Source code in tact/lib.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
replicate_seed(seed: int, replicate: int) -> int
Derive the seed of one replicate from the seed of a run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int
|
Seed of the run. |
required |
replicate
|
int
|
Index of the replicate. |
required |
Returns:
| Type | Description |
|---|---|
int
|
A seed for |
Source code in tact/lib.py
27 28 29 30 31 32 33 34 35 36 37 | |
seed_rngs(seed: int) -> None
Seed all random number generators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int
|
Seed for Python's |
required |
Source code in tact/lib.py
17 18 19 20 21 22 23 24 | |
two_step_optim(func: Callable[..., float], x0: tuple[float, ...], bounds: tuple[tuple[float, float], ...], args: tuple[Any, ...]) -> list[float]
Optimize a function using a two-step approach.
First attempts L-BFGS-B (fast gradient-based method), then falls back to simulated annealing if L-BFGS-B fails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., float]
|
Objective function to minimize. |
required |
x0
|
tuple[float, ...]
|
Initial parameter values. |
required |
bounds
|
tuple[tuple[float, float], ...]
|
Parameter bounds as tuples of (min, max) for each parameter. |
required |
args
|
tuple[Any, ...]
|
Additional arguments to pass to the objective function. |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
Optimized parameter values as a list. |
Raises:
| Type | Description |
|---|---|
Exception
|
If both optimization methods fail. |
Source code in tact/lib.py
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 | |
wrapped_lik_constant(x: tuple[float, float], sampling: float, ages: list[float]) -> float
Wrapper for birth-death likelihood function for optimization.
Converts turnover and relative extinction parameters to birth/death rates before computing the likelihood.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
tuple[float, float]
|
Tuple of (turnover, relative extinction). |
required |
sampling
|
float
|
Sampling fraction in (0, 1]. |
required |
ages
|
list[float]
|
List of node ages. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Negative log-likelihood value. |
Source code in tact/lib.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
wrapped_lik_constant_yule(x: float, sampling: float, ages: list[float]) -> float
Wrapper for Yule model likelihood function for optimization.
Assumes zero extinction (pure birth process).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Birth rate. |
required |
sampling
|
float
|
Sampling fraction in (0, 1]. |
required |
ages
|
list[float]
|
List of node ages. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Negative log-likelihood value. |
Source code in tact/lib.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
Tree functions
Functions in tact/tree_util.py.
Functions specifically to handle DendroPy tree objects.
compute_node_depths(tree: dendropy.Tree) -> dict[str, int]
Compute node depths for all labeled nodes.
Depth is defined as the number of labeled ancestor nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
Tree
|
DendroPy tree object. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Dictionary mapping tip labels to their node depths. |
Source code in tact/tree_util.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
count_newick_trees(path: str) -> int
Count the trees in a Newick file by their terminating semicolons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path to the tree file. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of trees in the file. |
Source code in tact/tree_util.py
109 110 111 112 113 114 115 116 117 118 119 120 | |
edge_iter(node: dendropy.Node, filter_fn: Callable[[dendropy.Edge], bool] | None = None) -> Iterator[dendropy.Edge]
Iterate over edges in a subtree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
DendroPy node representing the root of the subtree. |
required |
filter_fn
|
Callable[[Edge], bool] | None
|
Optional function to filter edges. Default: None. |
None
|
Yields:
| Type | Description |
|---|---|
Edge
|
DendroPy edge objects in the subtree. |
Source code in tact/tree_util.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
encode_leafset_bitmasks(node: dendropy.Node, taxon_namespace: dendropy.TaxonNamespace) -> int
Compute and store leafset bitmasks for every edge in a subtree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
Node representing the root of the subtree. |
required |
taxon_namespace
|
TaxonNamespace
|
Taxon namespace that the leaves of the subtree belong to. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Bitmask of the taxa in the subtree. |
Source code in tact/tree_util.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
get_age_intervals(node: dendropy.Node) -> portion.Interval
Get the age interval for possible grafts in a clade.
Computes the union of all age intervals from unlocked edges, which represents all possible ages where a graft could be placed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
Node representing the root of the clade. |
required |
Returns:
| Type | Description |
|---|---|
Interval
|
Portion interval (possibly disjoint) representing valid graft ages. |
Source code in tact/tree_util.py
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
get_ages(node: dendropy.Node, include_root: bool = False) -> list[float]
Get list of node ages in a subtree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
DendroPy node representing the root of the subtree. |
required |
include_root
|
bool
|
If True, include the root node's age. Default: False. |
False
|
Returns:
| Type | Description |
|---|---|
list[float]
|
List of node ages, sorted in descending order. |
Source code in tact/tree_util.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
get_birth_death_rates(node: dendropy.Node, sampfrac: float, yule: bool = False, include_root: bool = False) -> tuple[float, float]
Estimate birth-death rates from a subtree.
Computes maximum likelihood birth and death rates for the subtree descending from the given node, optionally using a Yule (pure birth) model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
DendroPy node representing the root of the subtree. |
required |
sampfrac
|
float
|
Sampling fraction in (0, 1]. |
required |
yule
|
bool
|
If True, use Yule model (zero extinction). Default: False. |
False
|
include_root
|
bool
|
If True, include root node age in calculations. Default: False. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
Tuple of (birth rate, death rate). |
Source code in tact/tree_util.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
get_min_age(node: dendropy.Node) -> float
Get the minimum possible age for a graft in a clade.
Computes the minimum age that could be generated by grafting into the clade, considering only unlocked edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
Node representing the root of the clade. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Minimum possible age (0.0 if interval is empty). |
Raises:
| Type | Description |
|---|---|
DisjointConstraintError
|
If the age interval is not atomic (disjoint). |
Source code in tact/tree_util.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | |
get_monophyletic_node(tree: dendropy.Tree, species: set[str]) -> dendropy.Node | None
Get the MRCA node if it forms a monophyletic group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
Tree
|
DendroPy tree object. |
required |
species
|
set[str]
|
Set of taxon labels to check for monophyly. |
required |
Returns:
| Type | Description |
|---|---|
Node | None
|
MRCA node if all its descendants are in the species set, None otherwise. |
Source code in tact/tree_util.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
get_short_branches(node: dendropy.Node) -> Iterator[dendropy.Edge]
Get edges with very short branch lengths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
DendroPy node representing the root of the subtree. |
required |
Yields:
| Type | Description |
|---|---|
Edge
|
DendroPy edge objects with length <= 0.001. |
Source code in tact/tree_util.py
196 197 198 199 200 201 202 203 204 205 206 207 | |
get_tip_labels(tree_or_node: dendropy.Tree | dendropy.Node) -> set[str]
Get set of tip labels for a tree or node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree_or_node
|
Tree | Node
|
DendroPy tree or node object. |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of taxon labels for all tips. |
Source code in tact/tree_util.py
71 72 73 74 75 76 77 78 79 80 81 82 | |
get_tree(path: str, namespace: dendropy.TaxonNamespace | None = None) -> dendropy.Tree
Load a DendroPy tree from a file path.
Loads a Newick-format tree and precalculates node ages and bipartition bitmasks for efficient operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path to the tree file. |
required |
namespace
|
TaxonNamespace | None
|
Optional taxon namespace to use. Default: None. |
None
|
Returns:
| Type | Description |
|---|---|
Tree
|
DendroPy tree object with ages and bipartitions calculated. |
Source code in tact/tree_util.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
graft_node(graft_recipient: dendropy.Node, graft: dendropy.Node, stem: bool = False, taxon_namespace: dendropy.TaxonNamespace | None = None) -> dendropy.Node
Graft a node randomly into a subtree.
Grafts a node into the subtree below the recipient node, randomly selecting an eligible edge. The graft node's age must be set. Edge lengths are adjusted to maintain ultrametricity.
The eligible edge (above which the graft is placed) must satisfy:
- Not be the crown node
- Head node younger than graft node
- Tail node older than graft node
- Not be locked
If taxon_namespace is given, ensure that the tree is left ready for MRCA lookups
without a subsequent call to update_tree_view. Prior to grafting, the bitmasks of
the tree must be up-to-date, and every node in the grafted subtree must already have
its age set.
- leafset bitmasks are computed for the grafted subtree
- new taxa are added to the bitmask of every edge between the graft and the root
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graft_recipient
|
Node
|
Node representing the clade to graft into. |
required |
graft
|
Node
|
Node to graft (must have age attribute set). |
required |
stem
|
bool
|
If True, allow grafting on the stem edge. Default: False. |
False
|
taxon_namespace
|
TaxonNamespace | None
|
If given, keep the leafset bitmasks of the tree up to date. Default: None. |
None
|
Returns:
| Type | Description |
|---|---|
Node
|
The crown node of the clade (may be the graft if it becomes the new crown). |
Raises:
| Type | Description |
|---|---|
Exception
|
If no eligible edge is found or negative branch lengths result. |
Source code in tact/tree_util.py
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 | |
is_binary(node: dendropy.Node) -> bool
Check if a subtree is fully bifurcating.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
DendroPy node representing the root of the subtree. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if all internal nodes have exactly two children, False otherwise. |
Source code in tact/tree_util.py
158 159 160 161 162 163 164 165 166 167 168 169 170 | |
is_fully_locked(node: dendropy.Node) -> bool
Check if all edges in a subtree are locked.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
Node representing the root of the subtree. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if all edges are locked, False otherwise. |
Source code in tact/tree_util.py
387 388 389 390 391 392 393 394 395 396 | |
is_ultrametric(tree: dendropy.Tree, tolerance: float = 1e-06) -> tuple[bool, tuple[tuple[str, float], tuple[str, float]]]
Check if a tree is ultrametric within a specified tolerance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
Tree
|
DendroPy tree object to check. |
required |
tolerance
|
float
|
Relative tolerance for ultrametricity check. Default: 1e-6. |
1e-06
|
Returns:
| Type | Description |
|---|---|
bool
|
Tuple of (is_ultrametric, (min_tip, max_tip)) where min_tip and max_tip |
tuple[tuple[str, float], tuple[str, float]]
|
are tuples of (label, root_distance) for the tips with minimum and |
tuple[bool, tuple[tuple[str, float], tuple[str, float]]]
|
maximum root-to-tip distances. |
Source code in tact/tree_util.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
lock_clade(node: dendropy.Node, stem: bool = False) -> None
Lock a clade to prevent future grafts.
Marks all edges in the subtree as locked, preventing new grafts from being placed on them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
Node representing the root of the clade to lock. |
required |
stem
|
bool
|
If True, also lock the stem edge. Default: False. |
False
|
Source code in tact/tree_util.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 | |
node_age(node: dendropy.Node) -> float
Get the age of a node.
DendroPy initializes Node.age to None, so this narrows it to a float for type checkers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
DendroPy node with its age set. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The age of the node. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the node's age has not been set. |
Source code in tact/tree_util.py
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
set_leafset_bitmask(node: dendropy.Node, bitmask: int) -> None
Set the leafset bitmask of the edge subtending a node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
Node whose subtending edge should be updated. |
required |
bitmask
|
int
|
Bitmask of the taxa descending from the node. |
required |
Source code in tact/tree_util.py
231 232 233 234 235 236 237 238 239 240 | |
unlock_clade(node: dendropy.Node, stem: bool = False) -> None
Unlock a clade to allow future grafts.
Removes the locked label from all edges in the subtree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
Node representing the root of the clade to unlock. |
required |
stem
|
bool
|
If True, also unlock the stem edge. Default: False. |
False
|
Source code in tact/tree_util.py
372 373 374 375 376 377 378 379 380 381 382 383 384 | |
update_tree_view(tree: dendropy.Tree) -> set[str]
Update a tree with node ages and bipartition bitmasks.
Performs in-place updates to calculate node ages and bipartition bitmasks, correcting for minor ultrametricity errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
Tree
|
DendroPy tree object to update. |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of tip labels in the tree. |
Source code in tact/tree_util.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
FastMRCA
Functions in tact/fastmrca.py.
Fast MRCA lookups by taxon label.
FastMRCA
MRCA lookups by taxon label on one tree.
TaxonNamespace.taxa_bitmask(labels=...) scans the whole namespace once per label, which
is quadratic. This caches the bitmask of each label instead, and rebuilds the cache when taxa
are added to or removed from the namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
Tree
|
Tree to look up MRCAs in. Its bitmasks must be kept up to date, for example with
|
required |
Source code in tact/fastmrca.py
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 | |
tree = tree
instance-attribute
__init__(tree: dendropy.Tree) -> None
See the class docstring for the arguments.
Source code in tact/fastmrca.py
22 23 24 25 26 27 28 | |
bitmask(labels: Iterable[str]) -> int
Get a bitmask for the specified taxa labels.
Intended to be equivalent to DendroPy's TaxonNamespace.taxa_bitmask(labels=labels).
- Labels that are not in the namespace are ignored
- Case sensitivity of the namespace is respected
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Iterable[str]
|
Iterable of taxon labels. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Bitmask representing the taxa. |
Source code in tact/fastmrca.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 | |
get(labels: Iterable[str]) -> dendropy.Node | None
Get the MRCA node for the specified taxa.
Returns the most recent common ancestor node if all descendants of that node are included in the label set, otherwise returns None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Iterable[str]
|
Iterable of taxon labels to find MRCA for. |
required |
Returns:
| Type | Description |
|---|---|
Node | None
|
MRCA node if it forms a monophyletic group, None otherwise. |
Source code in tact/fastmrca.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
Validation
Functions in tact/validation.py.
Various validation functions for click classes and parameters.
NEXUS_TREE = 'this is a Nexus file! Use a Newick tree instead'
module-attribute
BackboneCommand
Bases: Command
Helper class to validate a Click Command that contains a backbone tree.
At a minimum, the Command must contain a backbone parameter, which is validated by validate_newick
and checked to ensure it is a binary tree.
If the command also contains a taxonomy parameter, representing a taxonomic phylogeny,
every tip of backbone must match a species in it, ignoring spaces, underscores and case,
and the backbone is moved onto the taxonomy's TaxonNamespace. An optional outgroups
parameter may add other taxa not in the taxonomy.
If the command also contains an ultrametricity_precision parameter, the
ultrametricity of the backbone is also checked.
Source code in tact/validation.py
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 | |
make_context(info_name: str | None, args: list[str], parent: click.Context | None = None, **extra: object) -> click.Context
Set up the proper Click context for a command handler.
Source code in tact/validation.py
280 281 282 283 284 285 286 | |
validate_backbone_variables(ctx: click.Context, params: dict[str, Any]) -> dict[str, Any]
Validates variables related to the backbone and taxonomy files.
Source code in tact/validation.py
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 | |
check_binary(ctx: click.Context | None, param: click.Parameter | None, tree: dendropy.Tree) -> None
Checks that every internal node of a tree has two children, naming those that do not.
Source code in tact/validation.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
check_branch_lengths(ctx: click.Context | None, param: click.Parameter | None, tree: dendropy.Tree) -> None
Checks that every branch of a tree below its root has a length.
Source code in tact/validation.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
check_ultrametric(ctx: click.Context | None, param: click.Parameter | None, tree: dendropy.Tree, precision: float) -> None
Checks that all tips of a tree are the same distance from the root, within a relative precision.
Source code in tact/validation.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | |
first_tip(node: dendropy.Node) -> str
The label of the first tip below node.
Source code in tact/validation.py
172 173 174 | |
fmt_bullets(items: Sequence[str]) -> str
List items one per line, up to MAX_PROBLEMS of them, saying how many more there are.
Source code in tact/validation.py
32 33 34 35 36 37 | |
fmt_spellings(spellings: Sequence[str]) -> str
Join the spellings of a name, as A, B and C.
Source code in tact/validation.py
27 28 29 | |
is_nexus(text: str) -> bool
Whether text is the start of a Nexus file.
Source code in tact/validation.py
22 23 24 | |
read_newick(ctx: click.Context | None, param: click.Parameter | None, text: str) -> dendropy.Tree
Read a Newick tree into a namespace of its own, with its names cleaned.
Names that differ only in spaces, underscores or case are an error, rather than being silently merged.
Source code in tact/validation.py
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 | |
validate_newick(ctx: click.Context | None, param: click.Parameter | None, value: IO[str]) -> dendropy.Tree
Validates a Newick tree, using appropriate defaults.
Source code in tact/validation.py
78 79 80 | |
validate_newick_path(ctx: click.Context | None, param: click.Parameter | None, value: str) -> str
Validates that a path looks like a Newick tree, from its first characters, without reading the entire tree.
Source code in tact/validation.py
53 54 55 56 57 58 59 60 61 62 63 64 | |
validate_outgroups(ctx: click.Context | None, param: click.Parameter | None, value: str | Sequence[str] | None) -> list[str] | None
Validates an outgroups parameter, by splitting on commas and cleaning each name.
Source code in tact/validation.py
67 68 69 70 71 72 73 74 75 | |
validate_output(ctx: click.Context | None, param: click.Parameter | None, value: str) -> str
Validates an output base name, checking that its directory exists so that writing to it later will work.
Make the parameter eager if other parameters' callbacks are slow, so that this runs before them.
Source code in tact/validation.py
40 41 42 43 44 45 46 47 48 49 50 | |
validate_taxonomy_tree(ctx: click.Context | None, param: click.Parameter | None, value: str) -> dendropy.Tree
Validates a taxonomy, given either as a Newick tree or as a CSV file that is built into a tree in memory.
The format is sniffed from the first character of the file rather than taken from its extension, because
taxonomy trees go by many extensions (.tre, .tree, .nwk, .txt) and a CSV need not end in .csv. A Newick
tree starts with (, or [ for a comment such as [&R], which a CSV header row does not. Anything else
is read as a CSV, so that a file that is neither gets the list of problems from read_taxonomy rather than
a DendroPy parse error.
If the command has a fill_empty parameter, it must be eager so that it is set before this callback runs.
Source code in tact/validation.py
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 | |
validate_tree_node_depths(ctx: click.Context | None, param: click.Parameter | None, value: dendropy.Tree) -> dendropy.Tree
Validates a DendroPy tree, ensuring that the node depth is equal for all tips.
Source code in tact/validation.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
Exceptions
Functions in tact/exceptions.py.
Exceptions used by TACT.
DisjointConstraintError
Bases: TactError
Exception raised when a set of constraints lead to a disjoint implied age interval.
Source code in tact/exceptions.py
8 9 | |
TactError
Bases: Exception
Base class for errors raised by TACT.
Source code in tact/exceptions.py
4 5 | |