TreeWrapper Objects
Introduction
We have created a subclass of the Dendropy base Tree class that adds a few convenience functions that are useful to several Lifemapper analyses. We found that the existing method for generating a distance matrix consumed far too much memory for very large trees that we often work with, therefore, we have provided an alternative method for generating those distance matrices that runs in slightly more time, but requires significantly less memory. You can use all of the same functions available to the base Tree class as well as those described.
Creating TreeWrapper Instances
From an existing Dendropy Tree
See: TreeWrapper.from_base_tree
>>> tree = TreeWrapper.from_base_tree(dendropy_tree)
Reading a file
See: TreeWrapper.from_filename
>>> tree = TreeWrapper.from_filename('my_tree_file.tre')
Annotations
Adding node labels
Adds labels to tree nodes the same way that R does
See: TreeWrapper.add_node_labels
>>> tree.add_node_labels()
Annotating tips and nodes
See: TreeWrapper.annotate_tree
>>> ann_dict = {'A' : 1, 'B' : 2, 'C' : 3}
>>> tree.annotate_tree(ann_dict)
Annotating tree tips
See: TreeWrapper.annotate_tree_tips
>>> ann_pairs = {'Sp_1': 1999, 'Sp_2': 1973, 'Sp_3' : 1934}
>>> tree.annotate_tree_tips('year', ann_pairs)
Retrieving annotations
See: TreeWrapper.get_annotations
>>> annotations = tree.get_annotations('my_attribute')
Retrieving tip labels
>>> labels = tree.get_labels()
Getting matrices
Get distance matrix
Get a matrix of phylogenetic distances from each tip to all other tips. This version has a smaller memory footprint than the original Dendropy method at the trade-off of a slightly longer running time for large matrices.
See: TreeWrapper.get_distance_matrix
>>> d_mtx = tree.get_distance_matrix()
Get distance matrix using Dendropy method
Get a matrix of phylogenetic distances from each tip to all other tips. This version uses the built-in function from Dendropy to generate the matrix and has a much larger memory footprint but runs slightly faster at large scales.
See: TreeWrapper.get_distance_matrix_dendropy
>>> d_mtx = tree.get_distance_matrix_dendropy()
Get variance covariance matrix
See: TreeWrapper.get_variance_covariance_matrix
>>> var_cov_mtx = tree.get_variance_covariance_matrix()
Inspecting the tree
Checking for branch lengths
See: TreeWrapper.has_branch_lengths
>>> bool(tree.has_branch_lengths())
True
Checking for polytomies
See: TreeWrapper.has_polytomies
>>> bool(tree.has_polytomies())
False
Checking if a tree is binary
>>> bool(tree.is_binary())
True
Checking if a tree is ultrametric
See: TreeWrapper.is_ultrametric
>>> bool(tree.is_ultrametric())
True
Pruning tree tips without an attribute
See: TreeWrapper.prune_tips_without_attribute
>>> tree.prune_tips_without_attribute(search_attribute='my_att')