Sunday, November 14, 2010

New plotter for phylogenetic trees: customization


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

I've got a somewhat bigger tree here, using sequences from a project we're working on. I used clustal and R to make a neighbor-joining tree. As you can see in the code, there is a lot of flexibility in terms of customized colors, etc.

code listing:


import tree_utils as TU
import tree_plotter as TP

fn = 'veillonella.tree.txt'
D = TU.make_tree_dict(fn=fn)
tr = D['meta']['tree']

attr = TP.get_default_attr()
attr['e_node_dot_size'] = 35
attr['figure_type'] = 'png'
attr['e_node_label_size'] = 10
attr['e_node_bar_color'] = 'k'

attr['using_alternate_names'] = True
e_node_names = D['meta']['e_node_names']
#L = [e.replace('_',' ') for e in e_node_names]
L = [e.split('_')[-1] for e in e_node_names]
# lose the version numbers on Genbank seqs
L = [e.split('.')[0] for e in e_node_names]
alt_name_dict = dict(zip(e_node_names,L))
attr['alternate_name_dict'] = alt_name_dict

attr['using_e_node_specific_colors'] = True
def f(name):
if 'DB' in name: return 'b'
return 'r'
L = [f(e) for e in e_node_names]
node_label_color_dict = dict(zip(e_node_names,L))
attr['node_label_color_dict'] = node_label_color_dict

def f(name):
if 'DB' in name: return 'b'
return 'r'
L = [f(e) for e in e_node_names]
dot_color_dict = dict(zip(e_node_names,L))
attr['dot_color_dict'] = dot_color_dict

TP.plot(D,attr=attr)

'''
Rcode:
library(ape)
setwd('Desktop')
dna = read.dna('veillonella.fasta',format='fasta')
tree = nj(dist.dna(dna))
plot(tree,cex=1)
axisPhylo()
write.tree(tree,'veillonella.tree.txt')
'''