Sunday, November 14, 2010

New plotter for phylogenetic trees: re-rooting


Continuing with a tree-plotter in Python. Previous posts here and here. These examples depend on PyCogent and matplotlib.

One of the great things about building on PyCogent is we can use its re-rooting routine. In the output for this example we print the same tree as before, rooted at all possible internal nodes.

I've made a few tweaks to the code. make_tree_dict will take either a tree_string, a filename or a PyCogent tree. For the graphic above, we grab the default plot attributes, make the internal nodes visible, and then call plot. The code for this example is at the bottom. (I'm not going to post modifications to the other code until it's finalized. If you want it, shoot me an email).

To do list:
• learn matplotlib typography (e.g. italics)
• adjust for label sizes in a smart way
• provide for printing bootstrap values or internal node names

output:

$ python test_plotter.py 
original root
/-Stenotrophomonas_maltophilia
/edge.0--|
| \-Kingella_oralis
|
-root----|--Pseudomonas_aeruginosa
|
| /-Salmonella_typhi
| /edge.1--|
\edge.2--| \-Escherichia_coli
|
\-Haemophilus_parainfluenzae
------------------------------------------------------------
edge.0
/-Stenotrophomonas_maltophilia
|
|--Kingella_oralis
-root----|
| /-Pseudomonas_aeruginosa
| |
\edge.0--| /-Salmonella_typhi
| /edge.1--|
\edge.2--| \-Escherichia_coli
|
\-Haemophilus_parainfluenzae
------------------------------------------------------------
edge.2
/-Salmonella_typhi
/edge.1--|
| \-Escherichia_coli
|
-root----|--Haemophilus_parainfluenzae
|
| /-Stenotrophomonas_maltophilia
| /edge.0--|
\edge.2--| \-Kingella_oralis
|
\-Pseudomonas_aeruginosa
------------------------------------------------------------
edge.1
/-Salmonella_typhi
|
|--Escherichia_coli
-root----|
| /-Haemophilus_parainfluenzae
| |
\edge.1--| /-Stenotrophomonas_maltophilia
| /edge.0--|
\edge.2--| \-Kingella_oralis
|
\-Pseudomonas_aeruginosa
------------------------------------------------------------
balanced tree
/-Stenotrophomonas_maltophilia
/edge.0--|
| \-Kingella_oralis
|
-root----|--Pseudomonas_aeruginosa
|
| /-Salmonella_typhi
| /edge.1--|
\edge.2--| \-Escherichia_coli
|
\-Haemophilus_parainfluenzae


code listing:

import tree_utils as TU
import tree_plotter as TP

fn = 'tree.txt'
D = TU.make_tree_dict(fn=fn)
tr = D['meta']['tree']
print 'original root'
print tr.asciiArt()
print '-'*60

L = D['meta']['i_node_names']
for name in L[1:]:
print name
print tr.rootedAt(name).asciiArt()
D = TU.make_tree_dict(tr=tr)
print '-'*60

print 'balanced tree'
print tr.balanced().asciiArt()

tr = tr.rootedAt('edge.2')
D = TU.make_tree_dict(tr=tr)
attr = TP.get_default_attr()
attr['i_node_dots_visible'] = True
TP.plot(D,attr=attr,ofn='balanced')