-
Notifications
You must be signed in to change notification settings - Fork 0
/
lineageTree.py
111 lines (87 loc) · 2.8 KB
/
lineageTree.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from ete3 import Tree, TreeStyle, TextFace, NodeStyle
import numpy as np
treeStruct = np.array([[1,0,2,0],
[2,3,4,1],
[3,3,4,1],
[4,5,6,2],
[5,5,6,2],
[6,5,7,3],
[7,5,7,3]],
dtype=np.int16)
#root = Tree( "((a,b),c);" )
[rows, columns] = treeStruct.shape
root = Tree()
node_cur = root
'''#######################
Tree Style Begin
'''
ts = TreeStyle()
ts.title.add_face(TextFace("Tree example", fsize=8), column=0)
ts.scale = 50
ts.mode = 'r'
# left or right
ts.orientation = 1
ts.rotation = 270
ts.show_leaf_name = False
ts.show_branch_length = True
#ts.show_branch_length = True
'''
Tree Style End
#######################'''
'''#######################
Node Style Begin
'''
ns_root = NodeStyle()
ns_root["size"] = 10
ns_root['fgcolor'] = "red"
ns = NodeStyle()
ns["size"] = 10
#ns["hz_line_width"] = 1.5
#ns["vt_line_width"] = 1.5
'''
Node Style End
#######################'''
for r in xrange(rows):
cell_id = trackResult[r, 0]
time_begin = trackResult[r, 1]
time_end = trackResult[r, 2]
parent_id = trackResult[r, 3]
time_duration = np.abs(time_begin-time_end)
# for root
if parent_id == 0:
# Add name to root for the first iteration
root.add_feature("name", str(cell_id))
# change the branch length
root.add_feature("dist", time_duration)
#change node style
root.set_style(ns_root)
# set node name to face
nameFace = TextFace(root.name)
nameFace.fgcolor = "white"
nameFace.fsize = 15
# nameFace.border.width = 1
nameFace.background.color = "green"
node_cur.add_face(nameFace, column=1, position="branch-bottom")
else: # for child
#### search the parent node by parent_id
node_cur = root.search_nodes(name=str(parent_id))
# there should be only one parent node
if len(node_cur) == 1:
#### set child with its id
node_cur = node_cur[0].add_child(name=str(cell_id))
#### set duration
node_cur.add_feature("dist", time_duration)
# set node style
node_cur.set_style(ns)
# set node name to face
nameFace = TextFace(node_cur.name)
nameFace.fgcolor = "white"
nameFace.fsize = 15
nameFace.background.color = "green"
node_cur.add_face(nameFace, column=1, position="branch-bottom")
else:
raise RuntimeError("the cell id should be unique!")
#node = root.search_nodes(name=str(5))
#node[0].add_feature("dist", 1.5)
print root.get_ascii()
root.show(tree_style=ts)