-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorstrip.py
71 lines (61 loc) · 1.87 KB
/
colorstrip.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
65
66
67
68
69
70
71
#!/usr/bin/env python3
from ete4 import Tree
from ete4.smartview import TreeLayout, RectFace, TextFace
import random
t = Tree('((((a1,a2),a3), ((b1,b2),(d1,d2))), ((c1,c2),c3));')
# find common ancestors and annotate them
n1 = t.common_ancestor(["a1", "a2", "a3"])
n2 = t.common_ancestor(["b1", "b2"])
n3 = t.common_ancestor(["c1", "c2", "c3"])
n4 = t.common_ancestor(["d1", "d2"])
n1.name = "ancestor_a"
n2.name = "ancestor_b"
n3.name = "ancestor_c"
n4.name = "ancestor_d"
# set color map dictionary
colormap = {
"ancestor_a": "LightSteelBlue",
"ancestor_b": "Moccasin",
"ancestor_c": "DarkSeaGreen",
"ancestor_d": "Brown"
}
def get_tree_style(colormap):
def add_legend(tree_style):
tree_style.add_legend(
title = "MyLegend",
variable = "discrete",
colormap = colormap
)
return
return add_legend
def get_node_face(colormap):
def get_background(node):
# make rectangle face
if node.name in colormap:
lca_face = RectFace(
width=20,
height=None, # circular
color=colormap.get(node.name),
opacity=0.7,
text=node.name,
fgcolor='white',
min_fsize=6,
max_fsize=15,
ftype='sans-serif',
padding_x=1,
padding_y=1,
tooltip=None)
lca_face.rotate_text = True
node.add_face(lca_face, position='aligned', column=0)
return
return get_background
# Create a TreeLayout object, passing in the function
tree_layout = TreeLayout(
name="MyTreeLayout",
ns=get_node_face(colormap),
ts=get_tree_style(colormap),
active=True,
aligned_faces=True)
layouts = []
layouts.append(tree_layout)
t.explore(keep_server=True, layouts=layouts)