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

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

Added crossover and support for Inheritance table (but the table itself is not used currently, we are assuming Inheritance = 1!)

File size: 16.2 KB
Line 
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 = "" # BIRTHS / GENERATIONAL / REAL
9BALANCE = "" # MIN / DENSITY
10
11DOT_STYLE = "" # NONE / NORMAL / CLEAR
12
13JITTER = "" #
14
15# ------SVG---------
16svg_file = 0
17
18svg_line_style = 'stroke="rgb(90%,10%,16%)" stroke-width="1" stroke-opacity="0.8"'
19svg_dot_style = 'r="2" stroke="black" stroke-width="0.2" fill="red"'
20svg_clear_dot_style = 'r="2" stroke="black" stroke-width="0.4" fill="none"'
21
22svg_spine_line_style = 'stroke="rgb(0%,0%,80%)" stroke-width="2" stroke-opacity="1"'
23svg_spine_dot_style = 'r="1" stroke="black" stroke-width="0.2" fill="rgb(50%,50%,100%)"'
24
25def svg_add_line(from_pos, to_pos, style=svg_line_style):
26    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]) + '" />')
27
28def svg_add_dot(pos, style=svg_dot_style):
29    svg_file.write('<circle ' + style + ' cx="' + str(pos[0]) + '" cy="' + str(pos[1]) + '" />')
30
31def svg_generate_line_style(percent):
32    # hotdog
33    from_col = [100, 70, 0]
34    to_col = [60, 0, 0]
35    # lava
36    # from_col = [100, 80, 0]
37    # to_col = [100, 0, 0]
38    # neon
39    # from_col = [30, 200, 255]
40    # to_col = [240, 0, 220]
41
42    from_opa = 0.2
43    to_opa = 1.0
44    from_stroke = 1
45    to_stroke = 3
46
47    opa = from_opa*(1-percent) + to_opa*percent
48    stroke = from_stroke*(1-percent) + to_stroke*percent
49
50    percent = 1 - ((1-percent)**20)
51
52    return 'stroke="rgb(' + str(from_col[0]*(1-percent) + to_col[0]*percent) + '%,' \
53           + str(from_col[1]*(1-percent) + to_col[1]*percent) + '%,' \
54           + str(from_col[2]*(1-percent) + to_col[2]*percent) + '%)" stroke-width="' + str(stroke) + '" stroke-opacity="' + str(opa) + '"'
55
56def svg_generate_dot_style(percent):
57    from_col = [100, 70, 0]
58    to_col = [60, 0, 0]
59    # neon
60    # from_col = [30, 200, 255]
61    # to_col = [240, 0, 220]
62
63    from_opa = 0.2
64    to_opa = 1.0
65
66    opa = from_opa*(1-percent) + to_opa*percent
67
68    percent = 1 - ((1-percent)**20)
69
70    return 'fill="rgb(' + str(from_col[0]*(1-percent) + to_col[0]*percent) + '%,' \
71           + str(from_col[1]*(1-percent) + to_col[1]*percent) + '%,' \
72           + str(from_col[2]*(1-percent) + to_col[2]*percent) + '%)" r="1.5" stroke="black" stroke-width="0.2" fill-opacity="' + str(opa) + '" ' \
73           'stroke-opacity="' + str(opa) + '"'
74
75# -------------------
76
77def load_data(dir):
78    global firstnode, nodes, inv_nodes, time
79    f = open(dir)
80    for line in f:
81        sline = line.split(' ', 1)
82        if len(sline) == 2:
83            if sline[0] == "[OFFSPRING]":
84                creature = json.loads(sline[1])
85                #print("B" +str(creature))
86                if "FromIDs" in creature:
87                    if not creature["ID"] in nodes:
88                        nodes[creature["ID"]] = {}
89                        # we assign to each parent its contribution to the genotype of the child
90                        for i in range(0, len(creature["FromIDs"])):
91                            inherited = 1 #(creature["Inherited"][i] if 'Inherited' in creature else 1) #ONLY FOR NOW
92                            nodes[creature["ID"]][creature["FromIDs"][i]] = inherited
93                    else:
94                        print("Doubled entry for " + creature["ID"])
95                        quit()
96
97                    if not creature["FromIDs"][0] in nodes:
98                        firstnode = creature["FromIDs"][0]
99
100                if "Time" in creature:
101                    time[creature["ID"]] = creature["Time"]
102
103    for k, v in sorted(nodes.items()):
104        for val in sorted(v):
105            inv_nodes[val] = inv_nodes.get(val, [])
106            inv_nodes[val].append(k)
107
108
109def load_simple_data(dir):
110    global firstnode, nodes, inv_nodes
111    f = open(dir)
112    for line in f:
113        sline = line.split()
114        if len(sline) > 1:
115            #if int(sline[0]) > 15000:
116            #    break
117            if sline[0] == firstnode:
118                continue
119            nodes[sline[0]] = str(max(int(sline[1]), int(firstnode)))
120        else:
121            firstnode = sline[0]
122
123    for k, v in sorted(nodes.items()):
124        inv_nodes[v] = inv_nodes.get(v, [])
125        inv_nodes[v].append(k)
126
127    #print(str(inv_nodes))
128    #quit()
129
130def compute_depth(node):
131    my_depth = 0
132    if node in inv_nodes:
133        for c in inv_nodes[node]:
134            my_depth = max(my_depth, compute_depth(c)+1)
135    depth[node] = my_depth
136    return my_depth
137
138# ------------------------------------
139
140def xmin_crowd(x1, x2, y):
141    if BALANCE == "RANDOM":
142        return (x1 if random.randrange(2) == 0 else x2)
143    elif BALANCE == "MIN":
144        x1_closest = 999999
145        x2_closest = 999999
146        for pos in positions:
147            pos = positions[pos]
148            if pos[1] == y:
149                x1_closest = min(x1_closest, abs(x1-pos[0]))
150                x2_closest = min(x2_closest, abs(x2-pos[0]))
151        return (x1 if x1_closest > x2_closest else x2)
152    elif BALANCE == "DENSITY":
153        x1_dist = 0
154        x2_dist = 0
155        for pos in positions:
156            pos = positions[pos]
157            if pos[1] > y-10 or pos[1] < y+10:
158                dy = pos[1]-y
159                dx1 = pos[0]-x1
160                dx2 = pos[0]-x2
161
162                x1_dist += math.sqrt(dy**2 + dx1**2)
163                x2_dist += math.sqrt(dy**2 + dx2**2)
164        return (x1 if x1_dist > x2_dist else x2)
165
166# ------------------------------------
167
168def prepos_children_reccurent(node):
169    global visited
170    for c in inv_nodes[node]:
171
172        # we want to visit the node just once, after all of its parents
173        if not all_parents_visited(c):
174            continue
175        else:
176            visited[c] = True
177
178        # if JITTER == True:
179        #     dissimilarity = random.gauss(0,1)
180        # else:
181        #     dissimilarity = 1
182        #     #TODO take this info from proper fields
183
184        cy = 0
185        if TIME == "BIRTHS":
186            if c[0] == "c":
187                cy = int(c[1:])
188            else:
189                cy = int(c)
190        elif TIME == "GENERATIONAL":
191            cy = positions[node][1]+1
192        elif TIME == "REAL":
193            cy = time[c]
194
195        if len(nodes[c]) == 1:
196            dissimilarity = 0
197            if JITTER == True:
198                dissimilarity = random.gauss(0,1)
199            else:
200                dissimilarity = 1
201            positions[c] = [xmin_crowd(positions[node][0]-dissimilarity, positions[node][0]+dissimilarity, cy), cy]
202        else:
203            vsum = sum([v for k, v in nodes[c].items()])
204            cx = sum([positions[k][0]*v/vsum for k, v in nodes[c].items()])
205
206            if JITTER == True:
207                positions[c] = [cx + random.gauss(0, 0.1), cy]
208            else:
209                positions[c] = [cx, cy]
210
211
212        if c in inv_nodes:
213            prepos_children_reccurent(c)
214
215def prepos_children():
216    global max_height, max_width, min_width, visited
217
218    if not bool(time):
219        print("REAL time requested, but no real time data provided. Assuming BIRTHS time instead.")
220        TIME = "BIRTHS"
221
222    positions[firstnode] = [0, 0]
223
224    visited = {}
225    visited[firstnode] = True
226    prepos_children_reccurent(firstnode)
227
228    for pos in positions:
229        max_height = max(max_height, positions[pos][1])
230        max_width = max(max_width, positions[pos][0])
231        min_width = min(min_width, positions[pos][0])
232
233# ------------------------------------
234
235def all_parents_visited(node):
236    apv = True
237    for k, v in sorted(nodes[node].items()):
238        if not k in visited:
239            apv = False
240            break
241    return apv
242# ------------------------------------
243
244def draw_children_recurrent(node, max_depth):
245    global visited
246
247    for c in inv_nodes[node]:
248
249        # we want to draw the node just once
250        if not all_parents_visited(c):
251            continue
252        else:
253            visited[c] = True
254
255        if c in inv_nodes:
256            draw_children_recurrent(c, max_depth)
257
258        line_style = (svg_line_style if args.mono_tree else svg_generate_line_style(depth[c]/max_depth))
259        for k, v in sorted(nodes[c].items()):
260            svg_add_line( (w_margin+w_no_margs*(positions[k][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[k][1]/max_height),
261                (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)
262
263        if DOT_STYLE == "NONE":
264            continue
265        elif DOT_STYLE == "CLEAR":
266            dot_style = svg_clear_dot_style
267        else: # NORMAL, default
268            dot_style = svg_generate_dot_style(depth[c]/max_depth)
269        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)
270def draw_children():
271    global visited
272    visited = {}
273    visited[firstnode] = True
274
275    max_depth = 0
276    for k, v in depth.items():
277            max_depth = max(max_depth, v)
278    draw_children_recurrent(firstnode, max_depth)
279
280    if DOT_STYLE == "NONE":
281        return
282    elif DOT_STYLE == "CLEAR":
283        dot_style = svg_clear_dot_style
284    else: # NORMAL, default
285        dot_style = svg_generate_dot_style(depth[firstnode]/max_depth)
286    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)
287
288def draw_spine_recurrent(node):
289    for c in inv_nodes[node]:
290        if depth[c] == depth[node] - 1:
291            if c in inv_nodes:
292                draw_spine_recurrent(c)
293
294            line_style = svg_spine_line_style
295            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),
296                (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)
297            #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)
298def draw_spine():
299    draw_spine_recurrent(firstnode)
300    #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)
301
302def draw_skeleton_reccurent(node, max_depth):
303    for c in inv_nodes[node]:
304        if depth[c] >= min_skeleton_depth or depth[c] == max([depth[q] for q in inv_nodes[node]]):
305            if c in inv_nodes:
306                draw_skeleton_reccurent(c, max_depth)
307
308            line_style = svg_spine_line_style
309            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),
310                (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)
311            #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),
312            #             svg_spine_dot_style)
313def draw_skeleton():
314    max_depth = 0
315    for k, v in depth.items():
316            max_depth = max(max_depth, v)
317
318    draw_skeleton_reccurent(firstnode, max_depth)
319    #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),
320    #             svg_spine_dot_style)
321
322
323
324##################################################### main #####################################################
325
326args = 0
327
328h = 800
329w = 600
330h_margin = 10
331w_margin = 10
332h_no_margs = h - 2* h_margin
333w_no_margs = w - 2* w_margin
334
335max_height = 0
336max_width = 0
337min_width = 9999999999
338
339min_skeleton_depth = 0
340
341firstnode = ""
342nodes = {}
343inv_nodes = {}
344positions = {}
345visited= {}
346depth = {}
347time = {}
348
349def main():
350    global svg_file, min_skeleton_depth, args, TIME, BALANCE, DOT_STYLE, JITTER
351
352    parser = argparse.ArgumentParser(description='Process some integers.')
353    parser.add_argument('--in', dest='input', required=True, help='input file with stuctured evolutionary data')
354    parser.add_argument('--out', dest='output', required=True, help='output file for the evolutionary tree')
355    draw_tree_parser = parser.add_mutually_exclusive_group(required=False)
356    draw_tree_parser.add_argument('--draw-tree', dest='draw_tree', action='store_true', help='whether drawing the full tree should be skipped')
357    draw_tree_parser.add_argument('--no-draw-tree', dest='draw_tree', action='store_false')
358
359    draw_skeleton_parser = parser.add_mutually_exclusive_group(required=False)
360    draw_skeleton_parser.add_argument('--draw-skeleton', dest='draw_skeleton', action='store_true', help='whether the skeleton of the tree should be drawn')
361    draw_skeleton_parser.add_argument('--no-draw-skeleton', dest='draw_skeleton', action='store_false')
362
363    draw_spine_parser = parser.add_mutually_exclusive_group(required=False)
364    draw_spine_parser.add_argument('--draw-spine', dest='draw_spine', action='store_true', help='whether the spine of the tree should be drawn')
365    draw_spine_parser.add_argument('--no-draw-spine', dest='draw_spine', action='store_false')
366
367    #TODO: better names for those parameters
368    parser.add_argument('--time', default='BIRTHS', dest='time', help='values on vertical axis (BIRTHS/GENERATIONAL/REAL); '
369                                                                      'BIRTHS: time measured as the number of births since the beggining; '
370                                                                      'GENERATIONAL: time measured as number of ancestors; '
371                                                                      'REAL: real time of the simulation')
372    parser.add_argument('--balance', default='MIN', dest='balance', help='method of placing node in the tree (RANDOM/MIN/DENSITY)')
373
374    parser.add_argument('--dots', default='NORMAL', dest='dots', help='method of drawing dots (individuals) (NONE/NORMAL/CLEAR)')
375
376    parser.add_argument('-j', '--jitter', dest="jitter", action='store_true', help='draw horizontal positions of children from the normal distribution')
377
378    mono_tree_parser = parser.add_mutually_exclusive_group(required=False)
379    mono_tree_parser.add_argument('--mono-tree', dest='mono_tree', action='store_true', help='whether the tree should be drawn with a single color')
380    mono_tree_parser.add_argument('--no-mono-tree', dest='mono_tree', action='store_false')
381
382    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')
383    parser.add_argument('--seed', type=int, dest='seed', help='seed for the random number generator (-1 for random)')
384
385    parser.add_argument('--simple-data', type=bool, dest='simple_data', help='input data are given in a simple format (#child #parent)')
386
387    parser.set_defaults(mono_tree=False)
388    parser.set_defaults(draw_tree=True)
389    parser.set_defaults(draw_skeleton=False)
390    parser.set_defaults(draw_spine=False)
391
392    parser.set_defaults(seed=-1)
393
394    args = parser.parse_args()
395
396    TIME = args.time
397    BALANCE = args.balance
398    DOT_STYLE = args.dots
399    JITTER = args.jitter
400
401    dir = args.input
402    min_skeleton_depth = args.min_skeleton_depth
403    seed = args.seed
404    if seed == -1:
405        seed = random.randint(0, 10000)
406    random.seed(seed)
407    print("seed:", seed)
408
409    if args.simple_data:
410        load_simple_data(dir)
411    else:
412        load_data(dir)
413
414    compute_depth(firstnode)
415
416    svg_file = open(args.output, "w")
417    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" '
418                   'width="' + str(w) + '" height="' + str(h) + '">')
419
420    prepos_children()
421
422    if args.draw_tree:
423        draw_children()
424    if args.draw_skeleton:
425        draw_skeleton()
426    if args.draw_spine:
427        draw_spine()
428
429    svg_file.write("</svg>")
430    svg_file.close()
431
432main()
433
Note: See TracBrowser for help on using the repository browser.