TreeNode class#
Note
The bdms.TreeNode class inherits from ETE’s ete3.TreeNode.
Only the child class’ new members are documented here, so see the linked ETE documentation of inherited members.
- class bdms.TreeNode(t=0, state=None, state_attr='state', **kwargs)#
Bases:
TreeNodeA tree generated by a BDMS process. Subclasses
ete3.TreeNode.- Parameters:
t (
float) – Time of this node.state (
Hashable) – State of this node.state_attr (
str) – Name of the node attribute to store the state in.kwargs (
Any) – Keyword arguments passed toete3.TreeNodeinitializer.
-
n_mutations:
int# Number of mutations on the branch above this node (zero unless the tree has been pruned above this node, removing mutation event nodes).
- evolve(t, birth_process=ConstantProcess(attr=state, value=1), death_process=ConstantProcess(attr=state, value=0), mutation_process=ConstantProcess(attr=state, value=0), mutator=DiscreteMutator(attr=state, state_space=(None,), state_space_idxs={None: 0}, transition_matrix=[[1.]]), birth_mutation_prob=0.0, min_survivors=1, capacity=1000, capacity_method=None, init_population=1, seed=None, verbose=False)#
Evolve for time \(\Delta t\).
- Parameters:
t (
float) – Evolve for a duration of \(t\) time units.birth_process (
Process) – Birth process function.death_process (
Process) – Death process function.mutation_process (
Process) – Mutation process function.mutator (
Mutator) – Generator of mutation effects at mutation events (and possibly on daughters of birth events ifbirth_mutation_prob > 0).birth_mutation_prob (
Union[float,Callable[[Any],float]]) – Probability of a mutation event for each daughter node of a birth event. If a callable, then it should take a node attribute as an argument and return a probability.min_survivors (
int) – Minimum number of survivors. If the simulation finishes with fewer than this number of survivors, then aTreeErroris raised.capacity (
int) – Population carrying capacity.capacity_method (
Optional[Literal['birth','death','hard']]) – Method to enforce population carrying capacity. IfNone, then aTreeErroris raised if the population exceeds the carrying capacity. If'stop', then the simulation stops when the population reaches the carrying capacity. If'birth', then the birth rate is logistically modulated such that the process is critical when the population is at carrying capacity. If'death', then the death rate is logistically modulated such that the process is critical when the population is at carrying capacity. If'hard', then a random individual is chosen to die whenever a birth event results in carrying capacity being exceeded.init_population (
int) – Initial population size.seed (
int|Generator|None) – A seed to initialize the random number generation. IfNone, then fresh, unpredictable entropy will be pulled from the OS. If anint, then it will be used to derive the initial state. If anumpy.random.Generator, then it will be used directly.verbose (
bool) – Flag to indicate whether to print progress information.
- Raises:
TreeError – If the tree is not a root node, or if the tree has already evolved, or if the number of survivors is less than
min_survivors, or if the population exceedscapacitywithcapacity_method=None.ValueError – If
init_populationis greater thancapacityor if the event processes refer to different node attributes.
- Return type:
- sample_survivors(n=None, p=1.0, seed=None)#
Choose \(n\) survivor leaves from the tree, or each survivor leaf with probability \(p\), to mark as sampled (via the event attribute).
- Parameters:
seed (
int|Generator|None) – A seed to initialize the random number generation. IfNone, then fresh, unpredictable entropy will be pulled from the OS. If anint, then it will be used to derive the initial state. If anumpy.random.Generator, then it will be used directly.
- Raises:
ValueError – If the tree has already been sampled below this node, or if neither
nnorpis specified.- Return type:
- slice(t, attr='x')#
- Parameters:
- Return type:
- Returns:
List of attribute
attrvalues at time \(t\) for all lineages alive at that time.- Raises:
ValueError – If the tree has not evolved or has already been pruned below this node, or if the tree has not been sampled below this node, or if
tis before the root time or after the tree end time.
- prune_unsampled()#
Prune the tree to the subtree subtending the sampled leaves, removing unobserved subtrees.
- Raises:
ValueError – If the tree has not been sampled below this node, or if the tree has already been pruned below this node.
TreeError – If no leaves were sampled.
- Return type:
- remove_mutation_events()#
Remove unifurcating mutation event nodes, preserving branch length, and annotate mutation counts in child node
n_mutationsattribute.- Raises:
ValueError – If the tree has not been pruned below this node with
prune().- Return type:
- render(file_name, color_by='state', color_map=None, mode='r', scale=None, **kwargs)#
A thin wrapper around
ete3.TreeNode.render()that adds some custom decoration and a color bar. See also ETE’s tree rendering tutorial and linked API docs pages there.If tree is not pruned (or is pruned without removing mutation events), then branches are colored according to the attribute specified by
color_by, extinct lineages are indicated as dotted branches, unsampled non-extint lineages are indicated as solid branches, and sampled lineages are indicated as thick solid branches. Sampled leaves are indicated with a circle.If tree is pruned without retaining mutation events, then nodes are colored according to the attribute specified by
color_by, branches are annotated above with branch length (in black text) and below with number of mutations (in green text).- Parameters:
file_name (
str) – Filename to save the rendered tree to. Use"%%inline"to render inline in a notebook.color_by (
str|None) – If notNone, color tree by this numerical node attribute.color_map (
Mapping[Any,str] |None) – mapping from node attribute values to color names or hex codes.mode (
Literal['c','r']) –"c"for circular tree,"r"for rectangular tree.scale (
float|None) – Scale branch lengths by this factor (ignored if atree_styleis passed inkwargs). IfNone, then the scale is chosen automatically.kwargs (
Any) – Keyword arguments to pass toete3.TreeNode.render().
- Return type:
- Returns:
The return value of
ete3.TreeNode.render().