source: mds-and-trees/tree-genealogy.py @ 568

Last change on this file since 568 was 566, checked in by konrad, 8 years ago

Added REAL time option for tree-genealogy.py, renamed previous REAL time to BIRTHS time

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