VirtualMicrobes.Tree package

Submodules

VirtualMicrobes.Tree.PhyloTree module

class VirtualMicrobes.Tree.PhyloTree.PhyloNode(val, time)[source]

Bases: object

Version:
Author:
add_root(root_node)[source]
children
connect_phylo_offspring(offspring)[source]
dist_to_parent(parent)[source]
excise()[source]

remove references to this node from other tree nodes and reconnect remaining nodes.

has_root()[source]
id = 0
inherit_root(node)[source]
is_leaf

is this a leaf of the tree

iter_prepostorder(is_leaf_fn=None)[source]

Iterate over all nodes in a tree yielding every node in both pre and post order. Each iteration returns a postorder flag (True if node is being visited in postorder) and a node instance.

max_node_depth = 0
newick_id

id tag used in new hampshire (newick) tree format

nh_format_nr(_format='newick', formatter=<function nh_branch>)[source]

Format a newick string non-recursively, including internal nodes. (ete3, ape and iTol compatible!) NOTE: iTol doesn’t like trees without leafs, so if you have a nh tree like (((A)B)C)D it will start complaining Add a comma to the deepest leaf to fix this, and delete it via iTol if you really want it. (((A,)B)C)D :) :param with_leafs: :param _format: :param formatter:

nhx_features

additional node features used in the ‘extended’ newick format (nhx)

parents
push_onto_internal_child(child)[source]

makes this node an internal parent node of child :param child:

push_up_root()[source]

Iteratively push up the root to the internal_parent_node.

remove_root_stem()[source]
class VirtualMicrobes.Tree.PhyloTree.PhyloTree(supertree=False)[source]

Bases: object

Primary use is a phylogenetic tree, representing reproduction/speciation events at particular points in time. Because generations are overlapping, a parent may have offspring at various time points. Therefore Nodes are not strictly parents, but rather, parent-reproduction-time tuples.

class SuperNode[source]

Bases: object

add_leaf(leaf)[source]
add_node(phylo_unit, time)[source]
add_phylo_history(phylo_unit)[source]
annotate_leafs(ete_tree_struct, leaf)[source]

Annotate leaf labels

annotate_phylo_units_feature(ete_tree_struct, phylo_units, feature_name)[source]
check_ete_mapping(leafs=False)[source]
class_version = '1.0'
clear()[source]
coalescent()[source]

Find coalescent node and depth in the tree.

Returns:(class
Return type:Tree.PhyloTree.PhyloNode, time) : LCA, depth
connect_internal_node(int_node)[source]

Connect internal node ‘int_node’ with nodes above and below it on its own branch (representing births and/or death in this phylogenetic unit’s life history branch)

Parameters:int_node – newly made internal node that should get connected up

and down in the tree.

connect_leaf(leaf_node)[source]
connect_phylo_parent_child(phylo_parent_node, phylo_child_node)[source]
create_leaf(phylo_unit)[source]
create_root_stem(phylo_root)[source]
create_stems(phylo_unit)[source]
delete_empty_phylo_stems()[source]
delete_node(tree_node)[source]
delete_phylo_hist(phylo_unit)[source]

Remove the branch representing the phylogenetic unit and disconnect it from all sub-branches (children) in the tree.

Parameters:phylo_unit
distances(ete_tree, nodes)[source]

Pairwise distances between all nodes.

Parameters:
  • ete_tree (ete3.TreeNode) – ete tree root node
  • nodes (sequence of (phylo_node, ete_node) pairs) –
Returns:

  • tree_dist is phylogenetic distance.
  • top_dist is topological distance

Return type:

sequence of (phylo_node1, phylo_node2, tree_dist, top_dist)

ete_annotate_tree(ete_tree_struct, features=[], func_features={}, ete_root=None)[source]

Annotate the phylogenetic tree with cell data for tree plotting.

Assumes that the ete_tree has been constructed/updated. Creates a dictionary of feature dictionaries, keyed by the cells in the tree. Attaches the feature dictionaries to nodes in the ete_tree (annotate_ete_tree). Transforms some data to cummulative data along the branches of the tree. Optionally, prunes internal tree nodes (this will greatly simplify the tree drawing algorithm). Finally, transforms some data to rate of change data, using branch length for rate calculation.

Parameters:prune_internal – if True, nodes with 1 offspring only will be

removed/collapsed and their branch length added to the preceding node on the branch.

ete_calc_lca_depth(ete_tree)[source]

Calculate the distance from the last common ancestor to the farthest leaf in an ete tree.

Parameters:ete_tree (TreeNode) – ete tree root node
Returns:int
Return type:depth of LCA
ete_convert_feature_to_cummulative(ete_tree_struct, feature, ete_root)[source]

Convert annotated feature values to cummulative values.

By starting at the root, values further down the tree can be computed by adding values calculated for the parent node. It is useful when for example the aim is to prune away internal nodes and rates of change of a feature need to be calculated on the pruned tree.

ete_convert_feature_to_rate(ete_root, feature, replace_tup=('count', 'rate'))[source]

Convert an annotated feature on the tree nodes to a rate.

The function assumes cummulative values of the feature. The rate is calculated by dividing the difference of the feature value between a parent and child by the corresponding branch length.

ete_cummulative_features(ete_tree_struct, features=[], func_features={}, ete_root=None)[source]
ete_get_lca(ete_tree, nodes=None)[source]

Return last common ancestor (LCA) for a set of nodes.

(default: all leafs of the current tree)

Parameters:
  • ete_tree (TreeNode) – ete tree root node
  • nodes (list of ete3.TreeNode) – nodes in tree for which to find LCA
Returns:

TreeNode

Return type:

the last common ancestor

ete_get_phylo2ete_dict(ete_tree_struct, nodes=None)[source]

Return mapping from ete PhyloUnits to lists of ete TreeNode objects.

The mapping is constructed for a set of TreeNodes nodes. The default action is to map all the TreeNodes in the current ete_tree.

Parameters:nodes (sequence) – sequence of TreeNodes
Returns:
Return type:dictionary from :class:`VirtualMicrobes.virtual_cell.PhyloUnit.PhyloUnit`s to list of :class:`ete3.TreeNode`s
ete_init_mappings(ete_tree)[source]

Construct helper dictionaries for converting TreeNode names to TreeNodes and TreeNode names to PhyloUnits.

ete_n_most_distant_leafs(ete_root, n)[source]

Find n leafs that are most diverged from eachother.

First, the oldest n subtrees are determined. Then, for all subtrees the farthest lying leaf is returned.

Parameters:
  • n – number of leafs
  • root – starting point for search (default is the ete_tree root)
ete_n_most_distant_phylo_units(ete_tree_struct, n=2, root=None)[source]

Convenience function to return phylo units after finding ‘ete_n_most_distant_leafs’ (see below) :param root: phylo unit used as root

ete_n_oldest_subtrees(ete_root, n)[source]

Find n oldest subtrees under root.

Iteratively expands the oldest subtree root into its children until the desired number of subtrees is in the list. Return as a list of tuples of (subtree, distance-from-root).

Parameters:
  • n (int) – number of subtrees
  • root (TreeNode) – start point for subtree search
Returns:

subtrees

Return type:

a list of (TreeNode, distance)

ete_named_node_dict

Return the ete_named_node_dict belonging to the first ete tree.

See also

func
ete_tree
ete_node_name_to_phylo_node

Return the ete_node_name_to_phylo_node belonging to the first ete tree.

See also

func
ete_tree
ete_node_to_phylo_unit(ete_tree_struct, ete_node)[source]

Maps TreeNode to PhyloUnit.

Parameters:ete_node (TreeNode) – node to map
Returns:
Return type:PhyloUnit
ete_nodes_to_phylo_units(ete_tree_struct, nodes=None)[source]

Obtain a list of all the phylo units that are represented in the ete tree.

The ete_tree is traversed and ete node names are mapped first to PhyloTree nodes. The PhyloNodes have a reference to the PhyloUnit in their ‘val’ attribute.

ete_phylo_to_ete_birth_nodes(ete_tree_struct, phylo_unit)[source]

Return iterator over birth nodes in the ete tree for a phylo unit.

Parameters:phylo_unit (VirtualMicrobes.virtual_cell.PhyloUnit.PhyloUnit) – phylounit to map
Returns:
Return type:iterator of ete3.TreeNode
ete_phylo_to_ete_death_node(ete_tree_struct, phylo_unit)[source]

Return TreeNode representing the death of a phylo_unit.

ete_phylo_to_ete_stem_nodes(ete_tree_struct, phylo_unit)[source]

Return iterator over stem nodes in the ete tree for a phylo unit.

The stem nodes represent replication and death of the phylounit.

Parameters:phylo_unit (VirtualMicrobes.virtual_cell.PhyloUnit.PhyloUnit) – phylounit to map
Returns:
Return type:iterator of ete3.TreeNode
ete_prune_external(ete_tree_struct, prune_depth)[source]

Prune nodes of the external part of the tree beyond a certain depth.

The prune_depth is a time point in simulation time beyond which all subsequent nodes should be pruned and removed from the tree.

Parameters:
  • ete_struct (VirtualMicrobes.my_tools.utility.ETEtreeStruct) – struct holding data for the ete tree
  • prune_depth (int) – simulation time point beyond which nodes should be pruned
Returns:

Return type:

set of pruned internal ete3.TreeNode s

ete_prune_internal(ete_tree_struct)[source]

Excise all internal nodes that have a single child, while preserving the length of the branch.

Parameters:ete_struct (VirtualMicrobes.my_tools.utility.ETEtreeStruct) – struct holding data for the ete tree
Returns:
Return type:set of pruned internal ete3.TreeNode s
ete_prune_leafs(ete_tree_struct)[source]

Prune the external nodes of an ‘ete tree’.

Parameters:ete_tree_struct (VirtualMicrobes.my_tools.utility.ETEtreeStruct) – struct holding data for the ete tree
Returns:
Return type:set of pruned internal ete3.TreeNode s
ete_rate_features(ete_tree_struct, features=[], func_features={}, ete_root=None)[source]
ete_tree

Return the first ‘ete tree’ in the ete trees dict if it exists.

This is an adapter function during the transition to working with the multiple ete tree dictionary.

find_lca()[source]

Find the last common ancestor in the tree.

Start at the single root and go up until a branching point in the tree is found.

Returns:PhyloNode
Return type:LCA if it exists else None
nh_formats(roots=None, supertree=None, _format='newick')[source]
nodes_iter()[source]
phylo_to_ete_nodes(ete_tree_struct, phylo_unit, with_death=True)[source]

Return TreeNode objects in the ete_tree representing the birth and death nodes for this phylo_unit (e.g. cell or gene).

Parameters:
Returns:

Return type:

list of ete3.TreeNode

recalc_max_leaf_depth(leafs=None)[source]
reconnect_internal_nodes(internal_parent, internal_child)[source]
update(new_phylo_units=[], removed_phylo_units=[], new_roots=[])[source]
upgrade()[source]

Upgrading from older pickled version of class to latest version. Version information is saved as class variable and should be updated when class invariants (e.g. fields) are added. Adapted from recipe at http://code.activestate.com/recipes/521901-upgradable-pickles/

VirtualMicrobes.Tree.PhyloTree.newick(_id, time, branch_length)[source]
VirtualMicrobes.Tree.PhyloTree.nh_branch(_id, branch_length, features=None)[source]
VirtualMicrobes.Tree.PhyloTree.nhx(_id, time, branch_length)[source]

Module contents