summon.treelib
index
/home/raz/projects/summon/lib/summon/treelib.py

Tree data structures 
#
# Contains special features for representing phylogeny.  
# See rasmus.bio.phylo for more.
#
#

 
Modules
       
StringIO
copy
math
os
random
sys
rasmus.textdraw
rasmus.treelib_parser
rasmus.util

 
Classes
       
__builtin__.object
BranchData
Tree
TreeNode

 
class BranchData(__builtin__.object)
    A class for managing branch specific data for a Tree
 
By default, this class implements bootstrap data for TreeNode's.  
 
To incorporate new kinds of branch data, do the following.  Subclass this
class (say, MyBranchData).  Create Tree's with
Tree(branch_data=MyBranchData()).  This will ensure your new branch data
manager is used for manipulations to the tree.  Any tree's copied from the
tree (via tree.copy()) will also use the same branch manager.
 
  Methods defined here:
__init__(self)
get_branch_data(self, node)
Returns branch specific data from a node
merge_branch_data(self, data1, data2)
Merges the branch data from two neighboring branches into one
set_branch_data(self, node, data)
Set the branch specific data from 'data' to node.data
split_branch_data(self, node)
Split a branch's data into two copies

Data and other attributes defined here:
__dict__ = <dictproxy object>
dictionary for instance variables (if defined)
__weakref__ = <attribute '__weakref__' of 'BranchData' objects>
list of weak references to the object (if defined)

 
class Tree
    Basic rooted tree
 
Well suited for phylogenetic trees
 
  Methods defined here:
__init__(self, nextname=1, branch_data=<summon.treelib.BranchData object>)
__iter__(self)
Iterate through nodes of tree
add(self, node)
Add a node to the tree
Does not add node to any specific location (use add_child instead).
addTree = add_tree(self, parent, childTree)
add_child(self, parent, child)
Add a child node to an existing node 'parent' in the tree
add_tree(self, parent, childTree)
Add a subtree to the tree.
clear(self)
Clear all nodes from tree
clear_data(self, *keys)
Clear tree data
copy(self)
Returns a copy of the tree
copy_data(self, tree)
Copy tree data to another
copy_node_data(self, tree)
Copy node data to another tree
findDepths = find_depths(self, node=None)
find_depths(self, node=None)
DEPRECATED
getOnelineNewick = get_one_line_newick(self)
get_branch_data(self, node)
Returns branch specific data from a node
get_one_line_newick(self)
Get a presentation of the tree in a oneline string newick format
hasData = has_data(self, dataname)
has_data(self, dataname)
Does the tree contain 'dataname' in its extra data
leafNames = leaf_names(self, node=None)
leaf_names(self, node=None)
Returns the leaf names of the tree in order
leaves(self, node=None)
Return the leaves of the tree in order
make_root(self, name=None)
Create a new root node
merge_branch_data(self, data1, data2)
Merges the branch data from two neighboring branches into one
merge_names(self, tree2)
Merge the node names from tree2 into this tree.
Change any names that conflict
newName = new_name(self)
new_name(self)
Returns a new node name that should be unique in the tree
postorder(self, node=None)
Iterate through nodes in post-order traversal
preorder(self, node=None)
Iterate through nodes in pre-order traversal
readData = read_data(self, node, data)
readNewick = read_newick(self, filename, readData=None)
readParentTree = read_parent_tree(self, treeFile, labelFile=None, labels=None)
read_big_newick(self, filename)
Reads a big newick file with a custom parser
read_data(self, node, data)
Default data reader: reads optional bootstrap and branch length
read_newick(self, filename, readData=None)
Reads newick tree format from a file stream
 
You can specify a specialized node data reader with 'readData'
read_parent_tree(self, treeFile, labelFile=None, labels=None)
Reads a parent array from a file
remove(self, node)
Removes a node from a tree.
Notifies parent (if it exists) that node has been removed.
removeTree = remove_tree(self, node)
remove_tree(self, node)
Removes subtree rooted at 'node' from tree.
Notifies parent (if it exists) that node has been removed.
rename(self, oldname, newname)
Rename a node in the tree
replaceTree = replace_tree(self, node, childTree)
replace_tree(self, node, childTree)
Remove node and replace it with the root of childTree
set_branch_data(self, node, data)
Set the branch specific data from 'data' to node.data
set_default_data(self)
Set default values in each node's data
split_branch_data(self, node)
Split a branch's data into two copies
write(self, out=<open file '<stdout>', mode 'w'>, writeData=None, oneline=False, rootData=False)
Write the tree in newick notation
writeData = write_data(self, node)
writeNewick = write_newick(self, out=<open file '<stdout>', mode 'w'>, writeData=None, oneline=False, rootData=False)
writeParentTree = write_parent_tree(self, treeFile, labels)
write_data(self, node)
Default data writer: writes optional bootstrap and branch length
write_newick(self, out=<open file '<stdout>', mode 'w'>, writeData=None, oneline=False, rootData=False)
Write the tree in newick notation
write_newick_node(self, node, out=<open file '<stdout>', mode 'w'>, depth=0, writeData=None, oneline=False, rootData=False)
Write the node in newick format to the out file stream
write_parent_tree(self, treeFile, labels)
Writes tree to the  parent array format

 
class TreeNode
    A class for nodes in a rooted Tree
 
Contains fields for branch length 'dist' and custom data 'data'
 
  Methods defined here:
__init__(self, name)
__iter__(self)
Iterate through child nodes
copy(self, parent=None, copyChildren=True)
Returns a copy of a TreeNode and all of its children
isLeaf = is_leaf(self)
is_leaf(self)
Returns True if the node is a leaf (no children)
leafNames = leaf_names(self)
leaf_names(self)
Returns the leaf names beneath the node in traversal order
leaves(self)
Returns the leaves beneath the node in traversal order
recurse(self, func, *args)
Applies a function 'func' to the children of a node
writeData = write_data(self, out)
write_data(self, out)
Writes the data of the node to the file stream 'out'

 
Functions
       
assertTree = assert_tree(tree)
Assert that the tree is internally consistent
assert_tree(tree)
Assert that the tree is internally consistent
color_mix(colors)
Mixes together several color vectors into one
countDescendants = count_descendants(node, sizes=None)
Returns a dict with number of leaves beneath each node
count_descendants(node, sizes=None)
Returns a dict with number of leaves beneath each node
descendants(node, lst=None)
Return a list of all the descendants beneath a node
drawTree = draw_tree(tree, labels={}, scale=40, spacing=2, out=<open file '<stdout>', mode 'w'>, canvas=None, x=0, y=0, display=True, labelOffset=-1, minlen=1, maxlen=10000)
drawTreeBootLens = draw_tree_boot_lens(tree, *args, **kargs)
drawTreeLens = draw_tree_lens(tree, *args, **kargs)
drawTreeNameLens = draw_tree_name_lens(tree, *args, **kargs)
drawTreeNames = draw_tree_names(tree, *args, **kargs)
draw_tree(tree, labels={}, scale=40, spacing=2, out=<open file '<stdout>', mode 'w'>, canvas=None, x=0, y=0, display=True, labelOffset=-1, minlen=1, maxlen=10000)
draw_tree_boot_lens(tree, *args, **kargs)
draw_tree_lens(tree, *args, **kargs)
draw_tree_name_lens(tree, *args, **kargs)
draw_tree_names(tree, *args, **kargs)
ensemblTreeColorMap(tree)
Example of a color map for a tree of Enseml genes
findDist = find_dist(tree, name1, name2)
Returns the branch distance between two nodes in a tree
find_dist(tree, name1, name2)
Returns the branch distance between two nodes in a tree
graph2tree(mat, root, closedset=None)
Convert a graph to a tree data structure
isRooted = is_rooted(tree)
is_rooted(tree)
layoutTree(tree, xscale, yscale, minlen=-inf, maxlen=inf, rootx=0, rooty=0)
Determines the x and y coordinates for every branch in the tree.
 
Branch lengths are determined by node.dist
layoutTreeHierarchical(tree, xscale, yscale, rootx=0, rooty=0)
Determines the x and y coordinates for every branch in the tree.
 
Leaves are drawn to line up.  Best used for hierarchical clustering.
lca(nodes)
Returns the Least Common Ancestor (LCA) of a list of nodes
makePtree = make_ptree(tree)
Make parent tree array from tree
make_expr_mapping(maps)
Returns a function that maps strings matching an expression to a value
 
maps -- a list of pairs (expr, value)
make_ptree(tree)
Make parent tree array from tree
maxDisjointSubtrees = max_disjoint_subtrees(tree, subroots)
Returns a list of rooted subtrees with atmost one node from 
the list 'subroots'
max_disjoint_subtrees(tree, subroots)
Returns a list of rooted subtrees with atmost one node from 
the list 'subroots'
midpoint_root(tree)
parent_table2tree(parentTable)
Converts a parent table to a Tree
 
See tree2parentTable for details
parseNewick = parse_newick(newick)
Read a tree from newick notation stored in a string
parse_newick(newick)
Read a tree from newick notation stored in a string
readTree = read_tree(filename)
Read a tree from a newick file
read_parent_table(filename)
Reads a parent table from the file 'filename'
 
filename can also be an open file stream
read_tree(filename)
Read a tree from a newick file
read_tree_color_map(filename)
Reads a tree colormap from a file
removeExposedInternalNodes = remove_exposed_internal_nodes(tree, leaves=None)
Remove all leaves that were originally internal nodes
 
leaves -- a list of original leaves that should stay
 
if leaves is not specified, only leaves with strings as names will be kept
removeSingleChildren = remove_single_children(tree)
Remove all nodes from the tree that have exactly one child
 
Branch lengths are added together when node is removed.
remove_exposed_internal_nodes(tree, leaves=None)
Remove all leaves that were originally internal nodes
 
leaves -- a list of original leaves that should stay
 
if leaves is not specified, only leaves with strings as names will be kept
remove_single_children(tree)
Remove all nodes from the tree that have exactly one child
 
Branch lengths are added together when node is removed.
reorder_tree(tree, tree2)
Reorders the branches of tree to match tree2
reroot(tree, newroot, onBranch=True, newCopy=True)
Change the rooting of a tree
smallSubtrees(tree, maxsize)
subtree(tree, node)
Return a copy of a subtree of 'tree' rooted at 'node'
subtreeByLeafNames = subtree_by_leaf_names(tree, leaf_names, newCopy=False)
Returns a subtree with only the leaves specified
subtree_by_leaf_names(tree, leaf_names, newCopy=False)
Returns a subtree with only the leaves specified
tree2graph(tree)
Convert a tree to a graph data structure (sparse matrix)
tree2parent_table(tree, leaf_names=None)
Converts tree to a parent table
 
parent table is a standard format of the Compbio Lab as of 02/01/2007.
It is a list of triples (node_name, parent_name, dist)
 
* If the node is a leaf node_name is the leaf name (a string)
* If the node is internal node_name is an int representing which row
  (0-based) the node is in the table.
 
* parent_name indicates the parent of the node.  If the parent is root, a 
  -1 is used as the parent_name.
 
* dist is the distance between the node and its parent.
 
Arguments:
leaf_names -- specifies that a tree with only a subset of the leaves 
              should be used
 
NOTE: root is not given a row, because root does not have a distance
the nodeid of the root is -1
tree_color_map(leafmap=<function <lambda>>)
Returns a simple color mixing colormap
unroot(tree, newCopy=True)
Return an unrooted copy of tree
write_parent_table(parentTable, out=<open file '<stdout>', mode 'w'>)
Writes a parent table to out
 
out can be a filename or file stream