Saturday, November 27, 2010

Phylogenetic tree surgery in Python: NNI


I implemented this relatively simple type of tree surgery in Python. The example is the one we talked about last time (here). The plot of the result is above, and for reference, the original tree is below:



The output shows the logic:

0 ['A', 'B', '1']
1 ['C', '0', '3']
2 ['E', 'D', '3']
3 ['F', '1', '2']
--------------------
internal nodes: 1 3
exchanging: C 2
1 ['0', '2', '3']
3 ['C', 'F', '1']
fixing e2: 2
before: ['E', 'D', '3']
after: ['D', 'E', '1']
--------------------
0 ['A', 'B', '1']
1 ['0', '2', '3']
2 ['D', 'E', '1']
3 ['C', 'F', '1']

The first section is the initial tree. The middle part documents the changes we're making and the last prints out the result. I parsed the tree using our previous code, and plotted it using R's ape. I also added the code (nni.py) to the zipped files for the project. It's been fun, but I probably won't do the others for a while, if at all. I'm ready for new challenges.