-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.py
29 lines (24 loc) · 869 Bytes
/
visualize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
Visualize a phylogenetic tree with ete
"""
from ete3 import Tree
def convert_to_string(tree):
"""Returns tree in format used by ete library, Newick format"""
string = ""
#add any children that the node may have
if tree.children:
for child in tree.children:
if child.name is None:
#the child is an internal node, so has no name
#add its children
string += "(" + convert_to_string(child) + "),"
else:
#the child is a leaf, add with name
string += str(child.name) + ","
string = string[:-1]
return string
def render_to_image(phylogeny, filename="mytree.png"):
s = convert_to_string(phylogeny)
print(s)
t = Tree(s + ";") #needs a semicolon!
t.render(filename, w=183, units="mm")