As discussed last time, I'm using Python lists to hold the information for each node on a phylogenetic tree. This seems as if it will have significant advantages, especially when it comes to tree "surgery." I'll leave that for another post. What I want to do today is actually draw a tree rooted at each of the internal nodes. Here is our example:
Code to traverse the tree was shown last time (here). I've modified it slightly to use a dictionary. Previously we had this representation for each node and its parent (in the traversal from a particular root):
Now these keys and values are in a dict. The results of the traversals from each internal node as root are:
The root node is now marked by having itself as its "parent."
The drawing code works by building a
repr
(representation) for each node from the farthest tips working up to the root. The method was inspired by PyCogent's ancestors()
function, which returns a list of ancestors (naturally enough). The output with debug enabled looks like this:I simply sort on the length (largest first) and work in that order. So E's
repr
is just 'E'. When we process the first i_node (2), it's repr
will be (E,D)2
. If we're using branch lengths, it will be: (E:2.25,D:2.75)2
. The distance data is in a separate dictionary:Here's the resulting Newick tree:
One of the plots is shown at the top, a second one is below. Although I had it working before, be advised there's a bug somewhere that blows up R when I try to plot the tree as 'unrooted.' So now my to_do list includes the challenge of fixing that. [UPDATE: It seems to only happen when calling R from Python using RPy---probably related to the use of a Python keyword