1 | # Draws a genealogical tree (generates a SVG file) based on parent-child relationship information.
|
---|
2 |
|
---|
3 | import json
|
---|
4 | import random
|
---|
5 | import math
|
---|
6 | import argparse
|
---|
7 |
|
---|
8 | TIME = "GENERATIONAL"
|
---|
9 | BALANCE = "MIN"
|
---|
10 |
|
---|
11 | # ------SVG---------
|
---|
12 | svg_file = 0
|
---|
13 |
|
---|
14 | svg_line_style = 'stroke="rgb(90%,10%,16%)" stroke-width="1" stroke-opacity="0.8"'
|
---|
15 | svg_dot_style = 'r="2" stroke="black" stroke-width="0.2" fill="red"'
|
---|
16 | svg_spine_line_style = 'stroke="rgb(0%,0%,80%)" stroke-width="2" stroke-opacity="1"'
|
---|
17 | #svg_spine_dot_style = 'r="3" stroke="black" stroke-width="0.4" fill="rgb(50%,50%,100%)"'
|
---|
18 | svg_spine_dot_style = 'r="1" stroke="black" stroke-width="0.2" fill="rgb(50%,50%,100%)"'
|
---|
19 |
|
---|
20 | def svg_add_line(from_pos, to_pos, style=svg_line_style):
|
---|
21 | svg_file.write('<line ' + style + ' x1="' + str(from_pos[0]) + '" x2="' + str(to_pos[0]) + '" y1="' + str(from_pos[1]) + '" y2="' + str(to_pos[1]) + '" />')
|
---|
22 |
|
---|
23 | def svg_add_dot(pos, style=svg_dot_style):
|
---|
24 | svg_file.write('<circle ' + style + ' cx="' + str(pos[0]) + '" cy="' + str(pos[1]) + '" />')
|
---|
25 |
|
---|
26 | def svg_generate_line_style(percent):
|
---|
27 | # hotdog
|
---|
28 | from_col = [100, 70, 0]
|
---|
29 | to_col = [60, 0, 0]
|
---|
30 | # neon
|
---|
31 | # from_col = [30, 200, 255]
|
---|
32 | # to_col = [240, 0, 220]
|
---|
33 |
|
---|
34 | from_opa = 0.2
|
---|
35 | to_opa = 1.0
|
---|
36 | from_stroke = 1
|
---|
37 | to_stroke = 3
|
---|
38 |
|
---|
39 | opa = from_opa*(1-percent) + to_opa*percent
|
---|
40 | stroke = from_stroke*(1-percent) + to_stroke*percent
|
---|
41 |
|
---|
42 | percent = 1 - ((1-percent)**20)
|
---|
43 |
|
---|
44 | return 'stroke="rgb(' + str(from_col[0]*(1-percent) + to_col[0]*percent) + '%,' \
|
---|
45 | + str(from_col[1]*(1-percent) + to_col[1]*percent) + '%,' \
|
---|
46 | + str(from_col[2]*(1-percent) + to_col[2]*percent) + '%)" stroke-width="' + str(stroke) + '" stroke-opacity="' + str(opa) + '"'
|
---|
47 |
|
---|
48 | def svg_generate_dot_style(percent):
|
---|
49 | from_col = [100, 90, 0]
|
---|
50 | to_col = [60, 0, 0]
|
---|
51 | # neon
|
---|
52 | # from_col = [30, 200, 255]
|
---|
53 | # to_col = [240, 0, 220]
|
---|
54 |
|
---|
55 | from_opa = 0.2
|
---|
56 | to_opa = 1.0
|
---|
57 |
|
---|
58 | opa = from_opa*(1-percent) + to_opa*percent
|
---|
59 |
|
---|
60 | percent = 1 - ((1-percent)**20)
|
---|
61 |
|
---|
62 | return 'fill="rgb(' + str(from_col[0]*(1-percent) + to_col[0]*percent) + '%,' \
|
---|
63 | + str(from_col[1]*(1-percent) + to_col[1]*percent) + '%,' \
|
---|
64 | + str(from_col[2]*(1-percent) + to_col[2]*percent) + '%)" r="1.5" stroke="black" stroke-width="0.2" fill-opacity="' + str(opa) + '" ' \
|
---|
65 | 'stroke-opacity="' + str(opa) + '"'
|
---|
66 |
|
---|
67 | # -------------------
|
---|
68 |
|
---|
69 | def load_data(dir):
|
---|
70 | global firstnode, nodes, inv_nodes
|
---|
71 | f = open(dir)
|
---|
72 | for line in f:
|
---|
73 | sline = line.split(' ', 2)
|
---|
74 | if len(sline) == 3:
|
---|
75 | if sline[1] == "[OFFSPRING]":
|
---|
76 | creature = json.loads(sline[2])
|
---|
77 | #print("B" +str(creature))
|
---|
78 | if "FromIDs" in creature:
|
---|
79 | assert(len(creature["FromIDs"]) == 1)
|
---|
80 | nodes[creature["ID"]] = creature["FromIDs"][0]
|
---|
81 | if not creature["FromIDs"][0] in nodes:
|
---|
82 | firstnode = creature["FromIDs"][0]
|
---|
83 |
|
---|
84 | for k, v in sorted(nodes.items()):
|
---|
85 | inv_nodes[v] = inv_nodes.get(v, [])
|
---|
86 | inv_nodes[v].append(k)
|
---|
87 |
|
---|
88 |
|
---|
89 | def load_simple_data(dir):
|
---|
90 | global firstnode, nodes, inv_nodes
|
---|
91 | f = open(dir)
|
---|
92 | for line in f:
|
---|
93 | sline = line.split()
|
---|
94 | if len(sline) > 1:
|
---|
95 | #if int(sline[0]) > 15000:
|
---|
96 | # break
|
---|
97 | if sline[0] == firstnode:
|
---|
98 | continue
|
---|
99 | nodes[sline[0]] = str(max(int(sline[1]), int(firstnode)))
|
---|
100 | else:
|
---|
101 | firstnode = sline[0]
|
---|
102 |
|
---|
103 | for k, v in sorted(nodes.items()):
|
---|
104 | inv_nodes[v] = inv_nodes.get(v, [])
|
---|
105 | inv_nodes[v].append(k)
|
---|
106 |
|
---|
107 | #print(str(inv_nodes))
|
---|
108 | #quit()
|
---|
109 |
|
---|
110 | def compute_depth(node):
|
---|
111 | my_depth = 0
|
---|
112 | if node in inv_nodes:
|
---|
113 | for c in inv_nodes[node]:
|
---|
114 | my_depth = max(my_depth, compute_depth(c)+1)
|
---|
115 | depth[node] = my_depth
|
---|
116 | return my_depth
|
---|
117 |
|
---|
118 | # ------------------------------------
|
---|
119 |
|
---|
120 | def xmin_crowd(x1, x2, y):
|
---|
121 | if BALANCE == "RANDOM":
|
---|
122 | return (x1 if random.randrange(2) == 0 else x2)
|
---|
123 | elif BALANCE == "MIN":
|
---|
124 | x1_closest = 999999
|
---|
125 | x2_closest = 999999
|
---|
126 | for pos in positions:
|
---|
127 | pos = positions[pos]
|
---|
128 | if pos[1] == y:
|
---|
129 | x1_closest = min(x1_closest, abs(x1-pos[0]))
|
---|
130 | x2_closest = min(x2_closest, abs(x2-pos[0]))
|
---|
131 | return (x1 if x1_closest > x2_closest else x2)
|
---|
132 | elif BALANCE == "DENSITY":
|
---|
133 | x1_dist = 0
|
---|
134 | x2_dist = 0
|
---|
135 | for pos in positions:
|
---|
136 | pos = positions[pos]
|
---|
137 | if pos[1] > y-10 or pos[1] < y+10:
|
---|
138 | dy = pos[1]-y
|
---|
139 | dx1 = pos[0]-x1
|
---|
140 | dx2 = pos[0]-x2
|
---|
141 |
|
---|
142 | x1_dist += math.sqrt(dy**2 + dx1**2)
|
---|
143 | x2_dist += math.sqrt(dy**2 + dx2**2)
|
---|
144 | return (x1 if x1_dist > x2_dist else x2)
|
---|
145 |
|
---|
146 | # ------------------------------------
|
---|
147 |
|
---|
148 | def prepos_children_reccurent(node):
|
---|
149 | for c in inv_nodes[node]:
|
---|
150 | #print(node + "->" + c)
|
---|
151 | dissimilarity = 0.5 #random.gauss(0,0.3)
|
---|
152 | if TIME == "REAL":
|
---|
153 | id = ""
|
---|
154 | if c[0] == "c":
|
---|
155 | id = int(c[1:])
|
---|
156 | else:
|
---|
157 | id = int(c)
|
---|
158 | positions[c] = [xmin_crowd(positions[node][0]-dissimilarity, positions[node][0]+dissimilarity, id), id]
|
---|
159 | elif TIME == "GENERATIONAL":
|
---|
160 | positions[c] = [xmin_crowd(positions[node][0]-dissimilarity, positions[node][0]+dissimilarity, positions[node][1]+1), positions[node][1]+1]
|
---|
161 |
|
---|
162 | for c in inv_nodes[node]:
|
---|
163 | if c in inv_nodes:
|
---|
164 | prepos_children_reccurent(c)
|
---|
165 |
|
---|
166 | def prepos_children():
|
---|
167 | global max_height, max_width, min_width
|
---|
168 |
|
---|
169 | positions[firstnode] = [0, 0]
|
---|
170 |
|
---|
171 | prepos_children_reccurent(firstnode)
|
---|
172 |
|
---|
173 | for pos in positions:
|
---|
174 | max_height = max(max_height, positions[pos][1])
|
---|
175 | max_width = max(max_width, positions[pos][0])
|
---|
176 | min_width = min(min_width, positions[pos][0])
|
---|
177 |
|
---|
178 | # ------------------------------------
|
---|
179 |
|
---|
180 | def draw_children_recurrent(node, max_depth):
|
---|
181 | global max_height, max_width, min_width
|
---|
182 | for c in inv_nodes[node]:
|
---|
183 | if c in inv_nodes:
|
---|
184 | draw_children_recurrent(c, max_depth)
|
---|
185 |
|
---|
186 | line_style = (svg_line_style if args.mono_tree else svg_generate_line_style(depth[c]/max_depth))
|
---|
187 | dot_style = (svg_dot_style if args.mono_tree else svg_generate_dot_style(depth[c]/max_depth))
|
---|
188 |
|
---|
189 | svg_add_line( (w_margin+w_no_margs*(positions[node][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[node][1]/max_height),
|
---|
190 | (w_margin+w_no_margs*(positions[c][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[c][1]/max_height), line_style)
|
---|
191 | svg_add_dot( (w_margin+w_no_margs*(positions[c][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[c][1]/max_height), dot_style)
|
---|
192 | def draw_children():
|
---|
193 | max_depth = 0
|
---|
194 | for k, v in depth.items():
|
---|
195 | max_depth = max(max_depth, v)
|
---|
196 | draw_children_recurrent(firstnode, max_depth)
|
---|
197 | dot_style = (svg_dot_style if args.mono_tree else svg_generate_dot_style(depth[firstnode]/max_depth))
|
---|
198 | svg_add_dot( (w_margin+w_no_margs*(positions[firstnode][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[firstnode][1]/max_height), dot_style)
|
---|
199 |
|
---|
200 | def draw_spine_recurrent(node):
|
---|
201 | global max_height, max_width, min_width
|
---|
202 | for c in inv_nodes[node]:
|
---|
203 | if depth[c] == depth[node] - 1:
|
---|
204 | if c in inv_nodes:
|
---|
205 | draw_spine_recurrent(c)
|
---|
206 |
|
---|
207 | line_style = svg_spine_line_style
|
---|
208 | svg_add_line( (w_margin+w_no_margs*(positions[node][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[node][1]/max_height),
|
---|
209 | (w_margin+w_no_margs*(positions[c][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[c][1]/max_height), line_style)
|
---|
210 | #svg_add_dot( (w_margin+w_no_margs*(positions[c][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[c][1]/max_height), svg_spine_dot_style)
|
---|
211 | def draw_spine():
|
---|
212 | draw_spine_recurrent(firstnode)
|
---|
213 | #svg_add_dot( (w_margin+w_no_margs*(positions[firstnode][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[firstnode][1]/max_height), svg_spine_dot_style)
|
---|
214 |
|
---|
215 | def draw_skeleton_reccurent(node, max_depth):
|
---|
216 | global max_height, max_width, min_width
|
---|
217 | for c in inv_nodes[node]:
|
---|
218 | if depth[c] >= min_skeleton_depth or depth[c] == max([depth[q] for q in inv_nodes[node]]):
|
---|
219 | if c in inv_nodes:
|
---|
220 | draw_skeleton_reccurent(c, max_depth)
|
---|
221 |
|
---|
222 | line_style = svg_spine_line_style
|
---|
223 | svg_add_line( (w_margin+w_no_margs*(positions[node][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[node][1]/max_height),
|
---|
224 | (w_margin+w_no_margs*(positions[c][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[c][1]/max_height), line_style)
|
---|
225 | #svg_add_dot( (w_margin+w_no_margs*(positions[c][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[c][1]/max_height),
|
---|
226 | # svg_spine_dot_style)
|
---|
227 | def draw_skeleton():
|
---|
228 | max_depth = 0
|
---|
229 | for k, v in depth.items():
|
---|
230 | max_depth = max(max_depth, v)
|
---|
231 |
|
---|
232 | draw_skeleton_reccurent(firstnode, max_depth)
|
---|
233 | #svg_add_dot( (w_margin+w_no_margs*(positions[firstnode][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[firstnode][1]/max_height),
|
---|
234 | # svg_spine_dot_style)
|
---|
235 |
|
---|
236 |
|
---|
237 |
|
---|
238 | ##################################################### main #####################################################
|
---|
239 |
|
---|
240 | args = 0
|
---|
241 |
|
---|
242 | h = 800
|
---|
243 | w = 600
|
---|
244 | h_margin = 10
|
---|
245 | w_margin = 10
|
---|
246 | h_no_margs = h - 2* h_margin
|
---|
247 | w_no_margs = w - 2* w_margin
|
---|
248 |
|
---|
249 | max_height = 0
|
---|
250 | max_width = 0
|
---|
251 | min_width = 9999999999
|
---|
252 |
|
---|
253 | min_skeleton_depth = 0
|
---|
254 |
|
---|
255 | firstnode = ""
|
---|
256 | nodes = {}
|
---|
257 | inv_nodes = {}
|
---|
258 | positions = {}
|
---|
259 | depth = {}
|
---|
260 |
|
---|
261 | def main():
|
---|
262 | global svg_file, min_skeleton_depth, args, TIME, BALANCE
|
---|
263 |
|
---|
264 | parser = argparse.ArgumentParser(description='Process some integers.')
|
---|
265 | parser.add_argument('--in', dest='input', required=True, help='input file with stuctured evolutionary data')
|
---|
266 | parser.add_argument('--out', dest='output', required=True, help='output file for the evolutionary tree')
|
---|
267 | draw_tree_parser = parser.add_mutually_exclusive_group(required=False)
|
---|
268 | draw_tree_parser.add_argument('--draw-tree', dest='draw_tree', action='store_true', help='whether drawing the full tree should be skipped')
|
---|
269 | draw_tree_parser.add_argument('--no-draw-tree', dest='draw_tree', action='store_false')
|
---|
270 |
|
---|
271 | draw_skeleton_parser = parser.add_mutually_exclusive_group(required=False)
|
---|
272 | draw_skeleton_parser.add_argument('--draw-skeleton', dest='draw_skeleton', action='store_true', help='whether the skeleton of the tree should be drawn')
|
---|
273 | draw_skeleton_parser.add_argument('--no-draw-skeleton', dest='draw_skeleton', action='store_false')
|
---|
274 |
|
---|
275 | draw_spine_parser = parser.add_mutually_exclusive_group(required=False)
|
---|
276 | draw_spine_parser.add_argument('--draw-spine', dest='draw_spine', action='store_true', help='whether the spine of the tree should be drawn')
|
---|
277 | draw_spine_parser.add_argument('--no-draw-spine', dest='draw_spine', action='store_false')
|
---|
278 |
|
---|
279 | #TODO: better names for those parameters
|
---|
280 | parser.add_argument('--time', default='REAL', dest='time', help='values on vertical axis (REAL/GENERATIONAL)')
|
---|
281 | parser.add_argument('--balance', default='MIN',dest='balance', help='method of placing node in the tree (RANDOM/MIN/DENSITY)')
|
---|
282 |
|
---|
283 | mono_tree_parser = parser.add_mutually_exclusive_group(required=False)
|
---|
284 | mono_tree_parser.add_argument('--mono-tree', dest='mono_tree', action='store_true', help='whether the tree should be drawn with a single color')
|
---|
285 | mono_tree_parser.add_argument('--no-mono-tree', dest='mono_tree', action='store_false')
|
---|
286 |
|
---|
287 | parser.add_argument('--min-skeleton-depth', type=int, default=2, dest='min_skeleton_depth', help='minimal distance from the leafs for the nodes in the skeleton')
|
---|
288 | parser.add_argument('--seed', type=int, dest='seed', help='seed for the random number generator (-1 for random)')
|
---|
289 |
|
---|
290 | parser.add_argument('--simple-data', type=bool, dest='simple_data', help='input data are given in a simple format (#child #parent)')
|
---|
291 |
|
---|
292 | parser.set_defaults(mono_tree=False)
|
---|
293 | parser.set_defaults(draw_tree=True)
|
---|
294 | parser.set_defaults(draw_skeleton=False)
|
---|
295 | parser.set_defaults(draw_spine=False)
|
---|
296 |
|
---|
297 | parser.set_defaults(seed=-1)
|
---|
298 |
|
---|
299 | args = parser.parse_args()
|
---|
300 |
|
---|
301 | TIME = args.time
|
---|
302 | BALANCE = args.balance
|
---|
303 |
|
---|
304 | dir = args.input
|
---|
305 | min_skeleton_depth = args.min_skeleton_depth
|
---|
306 | seed = args.seed
|
---|
307 | if seed == -1:
|
---|
308 | seed = random.randint(0, 10000)
|
---|
309 | random.seed(seed)
|
---|
310 | print("seed:", seed)
|
---|
311 |
|
---|
312 | if args.simple_data:
|
---|
313 | load_simple_data(dir)
|
---|
314 | else:
|
---|
315 | load_data(dir)
|
---|
316 |
|
---|
317 | compute_depth(firstnode)
|
---|
318 |
|
---|
319 | svg_file = open(args.output, "w")
|
---|
320 | svg_file.write('<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.0" '
|
---|
321 | 'width="' + str(w) + '" height="' + str(h) + '">')
|
---|
322 |
|
---|
323 | prepos_children()
|
---|
324 |
|
---|
325 | if args.draw_tree:
|
---|
326 | draw_children()
|
---|
327 | if args.draw_skeleton:
|
---|
328 | draw_skeleton()
|
---|
329 | if args.draw_spine:
|
---|
330 | draw_spine()
|
---|
331 |
|
---|
332 | svg_file.write("</svg>")
|
---|
333 | svg_file.close()
|
---|
334 |
|
---|
335 | main()
|
---|
336 |
|
---|