API Reference
I recommend that TACT only be used via its console commands.
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, k)
Calculate the probability that a sample of k
taxa from a clade
of n
total taxa includes a root node, under a Yule process.
This equation is taken from:
Sanderson, M. J. 1996. How many taxa must be sampled to identify the root node of a large clade? Systematic Biology 45:168-173
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
total number of taxa |
required |
k |
int
|
sampled taxa |
required |
Returns:
Type | Description |
---|---|
float
|
probability of including a root node. |
Source code in tact/lib.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
|
get_bd(r, a)
Converts turnover and relative extinction to birth and death rates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
r |
float
|
turnover or net diversification (birth - death) |
required |
a |
float
|
relative extinction (death / birth) |
required |
Returns:
Type | Description |
---|---|
(float, float)
|
birth, death |
Source code in tact/lib.py
20 21 22 23 24 25 26 27 28 29 30 31 |
|
get_new_times(ages, birth, death, missing, told=None, tyoung=None)
Simulates new speciation events in an incomplete phylogeny assuming a constant-rate birth-death process.
Adapted from the R function TreeSim::corsim
written by Tanja Stadler.
N. Cusimano, T. Stadler, S. Renner. A new method for handling missing species in diversification analysis applicable to randomly or non-randomly sampled phylogenies. Syst. Biol., 61(5): 785-792, 2012.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ages |
list
|
vector of waiting times |
required |
birth |
float
|
birth rate |
required |
death |
float
|
death rate |
required |
missing |
int
|
number of missing taxa to simulate |
required |
told |
float
|
maximum simulated age (default: |
None
|
tyoung |
float
|
minimum simulated age bound (default: |
None
|
Returns:
Type | Description |
---|---|
list
|
vector of simulated waiting times. |
Source code in tact/lib.py
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 |
|
get_ra(b, d)
Converts birth and death to turnover and relative extinction rates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
b |
float
|
birth rate |
required |
d |
float
|
extinction rate |
required |
Returns:
Type | Description |
---|---|
(float, float)
|
turnover, relative extinction |
Source code in tact/lib.py
34 35 36 37 38 39 40 41 42 43 44 45 |
|
intp1(t, l, m)
Source code in tact/lib.py
218 219 220 221 222 |
|
intp1_exact(t, l, m)
Exact version of intp1
using Decimal math.
Source code in tact/lib.py
208 209 210 211 212 213 214 215 |
|
lik_constant(vec, rho, t, root=1, survival=1, p1=p1)
Calculates the likelihood of a constant-rate birth-death process, conditioned on the waiting times of a phylogenetic tree and degree of incomplete sampling.
Based off of the R function TreePar::LikConstant
written by Tanja Stadler.
T. Stadler. On incomplete sampling under birth-death models and connections to the sampling-based coalescent. Jour. Theo. Biol. 261: 58-66, 2009.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vec |
(float, float)
|
two element tuple of birth and death |
required |
rho |
float
|
sampling fraction |
required |
t |
list
|
vector of waiting times |
required |
root |
bool
|
include the root or not? (default: 1) |
1
|
survival |
bool
|
assume survival of the process? (default: 1) |
1
|
Returns:
Type | Description |
---|---|
float
|
likelihood of the birth-death process. |
Source code in tact/lib.py
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 |
|
optim_bd(ages, sampling, min_bound=1e-09)
Optimizes birth and death parameters given a vector of splitting times and sampling fraction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ages |
list
|
vector of node ages |
required |
sampling |
float
|
sampling fraction (0, 1] |
required |
min_bound |
float
|
minimum birth rate |
1e-09
|
Returns:
Name | Type | Description |
---|---|---|
birth |
float
|
optimized birth rate. |
death |
float
|
optimized death rate. |
Source code in tact/lib.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
optim_yule(ages, sampling, min_bound=1e-09)
Optimizes birth parameter under a Yule model, given a vector of splitting times and sampling fraction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ages |
list
|
vector of node ages |
required |
sampling |
float
|
sampling fraction (0, 1] |
required |
min_bound |
float
|
minimum birth rate |
1e-09
|
Returns:
Name | Type | Description |
---|---|---|
birth |
float
|
optimized birth rate. |
death |
float
|
optimized death rate. Always 0. |
Source code in tact/lib.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
p0(t, l, m, rho)
Source code in tact/lib.py
160 161 162 163 164 |
|
p0_exact(t, l, m, rho)
Exact version of p0
using Decimal math.
Source code in tact/lib.py
151 152 153 154 155 156 157 |
|
p1(t, l, m, rho)
Optimized version of p1_orig
using common subexpression elimination and strength reduction
from exponentiation to multiplication.
Source code in tact/lib.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
p1_exact(t, l, m, rho)
Exact version of p1
using Decimal math.
Source code in tact/lib.py
167 168 169 170 171 172 173 174 175 |
|
p1_orig(t, l, m, rho)
Original version of p1
, here for testing and comparison purposes.
Source code in tact/lib.py
178 179 180 181 182 183 184 185 186 187 188 |
|
two_step_optim(func, x0, bounds, args)
Conduct a two-step function optimization, first by using the fast L-BFGS-B method, and if that fails, use simulated annealing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
callable
|
function to optimize |
required |
x0 |
tuple
|
initial conditions |
required |
bounds |
tuple
|
boundary conditions |
required |
args |
list
|
additional arguments to pass to |
required |
Returns:
Name | Type | Description |
---|---|---|
params |
tuple
|
optimized parameter values |
Source code in tact/lib.py
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 |
|
wrapped_lik_constant(x, sampling, ages)
Wrapper for birth-death likelihood to make optimizing more convenient.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
(float, float)
|
turnover, relative extinction |
required |
sampling |
float
|
sampling fraction (0, 1] |
required |
ages |
list
|
vector of node ages |
required |
Returns:
Type | Description |
---|---|
float
|
a likelihood |
Source code in tact/lib.py
48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
wrapped_lik_constant_yule(x, sampling, ages)
Wrapper for Yule likelihood to make optimizing more convenient.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
float
|
birth rate |
required |
sampling |
float
|
sampling fraction (0, 1] |
required |
ages |
list
|
vector of node ages |
required |
Returns:
Type | Description |
---|---|
float
|
a likelihood |
Source code in tact/lib.py
63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
Tree functions
Functions in tact/tree_util.py
.
Functions specifically to handle DendroPy tree objects.
compute_node_depths(tree)
Returns a dictionary of node depths for each node with a label.
Source code in tact/tree_util.py
119 120 121 122 123 124 125 126 127 128 |
|
count_locked(node)
How many edges under node
are locked?
Source code in tact/tree_util.py
214 215 216 |
|
edge_iter(node, filter_fn=None)
Iterates over the child edge of node
and all its descendants.
Can optionally be filtered by filter_fn
.
Source code in tact/tree_util.py
56 57 58 59 60 61 62 63 64 65 66 |
|
get_age_intervals(node)
Gets the (possibly disjoint) interval that could be generated in the
clade under node
, assuming that grafts to locked edges are restricted.
Source code in tact/tree_util.py
242 243 244 245 246 247 248 249 250 |
|
get_ages(node, include_root=False)
Returns the list of ages of the children of a given node
,
optionally including the node
's age if include_root
is True.
Source code in tact/tree_util.py
37 38 39 40 41 42 43 44 45 |
|
get_birth_death_rates(node, sampfrac, yule=False, include_root=False)
Estimates the birth and death rates for the subtree descending from
node
with sampling fraction sampfrac
. Optionally restrict to a
Yule pure-birth model.
Source code in tact/tree_util.py
16 17 18 19 20 21 22 23 24 25 |
|
get_min_age(node)
Gets the minimum possible age that could be generated in a clade under node
,
assuming that grafts to locked edges are restricted.
Source code in tact/tree_util.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
|
get_monophyletic_node(tree, species)
Returns the node or None that is the MRCA of the species
in tree
.
Source code in tact/tree_util.py
28 29 30 31 32 33 34 |
|
get_short_branches(node)
Yields an iterator of especially short edges under node
.
Source code in tact/tree_util.py
112 113 114 115 116 |
|
get_tip_labels(tree_or_node)
Returns a set
of tip labels for a node or tree.
Source code in tact/tree_util.py
48 49 50 51 52 53 |
|
get_tree(path, namespace=None)
Gets a DendroPy tree from a path and precalculate its node ages and bipartition bitmask.
Source code in tact/tree_util.py
69 70 71 72 73 74 75 |
|
graft_node(graft_recipient, graft, stem=False)
Grafts a node graft
randomly in the subtree below node
graft_recipient
. The attribute graft.age
must be set so
we know where is the best place to graft the node. The node
graft
can optionally have child nodes, in this case the
edge.length
attribute should be set on all child nodes if
the tree is to remain ultrametric.
We graft things "below" a node by picking one of the children of that node and forcing it to be sister to the grafted node and adjusting the edge lengths accordingly. Therefore, the node above which the graft lives (i.e., the one that will be the child of the new graft) must fulfill the following requirements:
- Must not be the crown node (cannot graft things above crown node)
- Must be younger than the graft node (no negative branches)
- Seed node must be older than graft node (no negative branches)
- Must not be locked (intruding on monophyly)
Source code in tact/tree_util.py
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 |
|
is_binary(node)
Is the subtree under node
a fully bifurcating tree?
Source code in tact/tree_util.py
90 91 92 93 94 95 |
|
is_fully_locked(node)
Are all the edges below node
locked?
Source code in tact/tree_util.py
219 220 221 222 223 |
|
is_ultrametric(tree, tolerance=1e-06)
Is the tree
ultrametric, within a specified tolerance
?
Uses the relative difference between minimum and maximum root-to-tip distances.
Source code in tact/tree_util.py
98 99 100 101 102 103 104 105 106 107 108 109 |
|
lock_clade(node, stem=False)
Locks a clade descending from node
so future grafts will avoid locked edges.
Source code in tact/tree_util.py
194 195 196 197 198 199 200 201 |
|
unlock_clade(node, stem=False)
Unlocks a clade descending from node
so new tips can be grafted to its edges.
Source code in tact/tree_util.py
204 205 206 207 208 209 210 211 |
|
update_tree_view(tree)
Mutates a DendroPy tree object with updated node ages and bipartition bitmask. We also correct for minor ultrametricity errors.
Returns a list of tip labels.
Source code in tact/tree_util.py
78 79 80 81 82 83 84 85 86 87 |
|
FastMRCA
Functions in tact/fastmrca.py
.
Singleton object that helps speed up MRCA lookups.
bitmask(labels)
Gets a bitmask for the taxa in labels
, potentially in parallel.
Source code in tact/fastmrca.py
18 19 20 21 22 23 24 |
|
fastmrca_getter(tn, x)
Helper function for submitting stuff.
Source code in tact/fastmrca.py
37 38 39 40 41 42 43 |
|
get(labels)
Pulls a MRCA node out for the taxa in labels
.
Source code in tact/fastmrca.py
27 28 29 30 31 32 33 34 |
|
initialize(phy)
Initialize the fastmrca singleton with a tree.
Source code in tact/fastmrca.py
10 11 12 13 14 15 |
|
Validation
Functions in tact/validation.py
.
Various validation functions for click
classes and parameters.
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,
this is also validated to ensure that the DendroPy TaxonNamespace is non-strict superset
of the taxa contained in backbone
. 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
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 |
|
make_context(*args, **kwargs)
Source code in tact/validation.py
107 108 109 110 |
|
validate_backbone_variables(ctx, params)
Source code in tact/validation.py
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 |
|
validate_newick(ctx, param, value, **kwargs)
Validates a Newick tree, using appropriate defaults.
Source code in tact/validation.py
28 29 30 |
|
validate_outgroups(ctx, param, value)
Validates an outgroups
parameter, by splitting on commas and transforming underscores to spaces.
Source code in tact/validation.py
16 17 18 19 20 21 22 23 24 25 |
|
validate_taxonomy_tree(ctx, param, value)
Validates a taxonomy tree.
Source code in tact/validation.py
47 48 49 50 |
|
validate_tree_node_depths(ctx, param, value)
Validates a DendroPy tree, ensuring that the node depth is equal for all tips.
Source code in tact/validation.py
33 34 35 36 37 38 39 40 41 42 43 44 |
|
Exceptions
Functions in tact/exceptions.py
.
DisjointConstraintError
Bases: TactError
Exception raised when a set of constraints lead to a disjoint implied age interval.
Source code in tact/exceptions.py
5 6 |
|
TactError
Bases: Exception
Base class for errors raised by TACT.
Source code in tact/exceptions.py
1 2 |
|