-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_backgrounds.py
64 lines (54 loc) · 1.62 KB
/
node_backgrounds.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from ete4 import Tree
from ete4.smartview import TreeLayout, NodeStyle, TextFace
t = Tree('((((a1,a2),a3), ((b1,b2),(b3,b4))), ((c1,c2),c3));')
# find common ancestors
n1 = t.common_ancestor(["a1", "a2", "a3"])
n2 = t.common_ancestor(["b1", "b2", "b3", "b4"])
n3 = t.common_ancestor(["c1", "c2", "c3"])
n4 = t.common_ancestor(["b3", "b4"])
# set background color for difference node style
nst1 = NodeStyle()
nst1["bgcolor"] = "LightSteelBlue"
nst2 = NodeStyle()
nst2["bgcolor"] = "Moccasin"
nst3 = NodeStyle()
nst3["bgcolor"] = "DarkSeaGreen"
nst4 = NodeStyle()
nst4["bgcolor"] = "Khaki"
# set color map dictionary
colormap = {
"ancestor_a": "LightSteelBlue",
"ancestor_b": "Moccasin",
"ancestor_c": "DarkSeaGreen",
"ancestor_d": "Khaki"
}
def get_tree_style(colormap):
def add_legend(tree_style):
tree_style.add_legend(
title = "MyLegend",
variable = "discrete",
colormap = colormap
)
return add_legend
def get_background(node):
# make node name with bigger text
node.add_face(TextFace(node.name, min_fsize=6, max_fsize=25), column=0, position="branch_right")
# set node style
if node == n1:
node.set_style(nst1)
elif node == n2:
node.set_style(nst2)
elif node == n3:
node.set_style(nst3)
elif node == n4:
node.set_style(nst4)
return
# Create a TreeLayout object, passing in the function
tree_layout = TreeLayout(
name="MyTreeLayout",
ns=get_background,
ts=get_tree_style(colormap),
active=True)
layouts = []
layouts.append(tree_layout)
t.explore(keep_server=True, layouts=layouts)