VirtualMicrobes.Tree package¶
Submodules¶
VirtualMicrobes.Tree.PhyloTree module¶
Created on Nov 11, 2014
@author: thocu
-
class
VirtualMicrobes.Tree.PhyloTree.
PhyloNode
(val, time)[source]¶ Bases:
object
Version: Author: -
children
¶
-
excise
()[source]¶ remove references to this node from other tree nodes and reconnect remaining nodes.
-
id
= 0¶
-
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. :param with_leafs: :param _format: :param formatter:
-
nhx_features
¶ additional node features used in the ‘extended’ newick format (nhx)
-
parents
¶
-
-
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.
-
PhyloTree.
class_version
= '1.0'¶
-
PhyloTree.
coalescent
()[source]¶ Find coalescent node and depth in the tree.
Returns: (class Return type: Tree.PhyloTree.PhyloNode, time) : LCA, depth
-
PhyloTree.
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.
-
PhyloTree.
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 –
-
PhyloTree.
distances
(ete_tree, nodes)[source]¶ Pairwise distances between all nodes.
Parameters: - ete_tree (class: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)
-
PhyloTree.
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.
-
PhyloTree.
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
-
PhyloTree.
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.
-
PhyloTree.
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.
-
PhyloTree.
ete_cummulative_features
(ete_tree_struct, features=[], func_features={}, ete_root=None)[source]¶
-
PhyloTree.
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 class:ete3.TreeNode) – nodes in tree for which to find LCA
Returns: TreeNode
Return type: the last common ancestor
-
PhyloTree.
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: dictionary from class Return type: `virtual_cell.PhyloUnit.PhyloUnit`s to list of class:`ete3.TreeNode`s
-
PhyloTree.
ete_init_mappings
(ete_tree)[source]¶ Construct helper dictionaries for converting TreeNode names to TreeNodes and TreeNode names to PhyloUnits.
-
PhyloTree.
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)
-
PhyloTree.
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)
Parameters: root – phylo unit used as root
-
PhyloTree.
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)
-
PhyloTree.
ete_named_node_dict
¶ Return the ete_named_node_dict belonging to the first ete tree.
See also
func
- ete_tree
-
PhyloTree.
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
-
PhyloTree.
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
-
PhyloTree.
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.
-
PhyloTree.
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 (class:virtual_cell.PhyloUnit.PhyloUnit) – phylounit to map Returns: iterator of class Return type: ete3.TreeNode
-
PhyloTree.
ete_phylo_to_ete_death_node
(ete_tree_struct, phylo_unit)[source]¶ Return TreeNode representing the death of a phylo_unit.
-
PhyloTree.
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 (class:virtual_cell.PhyloUnit.PhyloUnit) – phylounit to map Returns: iterator of class Return type: ete3.TreeNode
-
PhyloTree.
ete_prune_internal
(ete_tree_struct)[source]¶ Excise all internal nodes that have a single child, while preserving the length of the branch.
-
PhyloTree.
ete_prune_leafs
(ete_struct)[source]¶ Prune the external nodes of an ‘ete tree’.
Parameters: ete_struct (class:my_tools.utility.ETEtreeStruct) – struct holding data for the ete tree
-
PhyloTree.
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.
-
PhyloTree.
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
-
PhyloTree.
phylo_to_ete_nodes
(ete_tree_struct, phylo_unit)[source]¶ Return TreeNode objects in the ete_tree representing the birth and death nodes for this phylo_unit (e.g. cell or gene).
Parameters: - ete_tree_struct (class:my_tools.utility.ETEtreeStruct) – ete tree struct in which to find the phylo_unit
- phylo_unit (class:virtual_cell.PhyloUnit.PhyloUnit) – phylounit to map
Returns: list of class
Return type: ete3.TreeNode
-
PhyloTree.
to_ete_trees
(with_leafs=False, supertree=None)[source]¶ Construct TreeNode representations for all roots in the PhyloTree.
ete3.TreeNode representations can be used for advanced plotting and tree analysis.
Parameters: - with_leafs (bool) – construct with or without terminal nodes
- supertree (bool) – use a supernode as an artificial root node (useful when simulation starts from independent lineages)
Returns: OrderedDict of name -> TreeNode
Return type: TreeNodes by name for all phylo roots
-
PhyloTree.
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/
-