[562] | 1 | # Draws a genealogical tree (generates a SVG file) based on parent-child relationship information.
|
---|
[615] | 2 | # Supports files generated by Framsticks experiments.
|
---|
[562] | 3 |
|
---|
| 4 | import json
|
---|
| 5 | import random
|
---|
| 6 | import math
|
---|
| 7 | import argparse
|
---|
[621] | 8 | import time as ttime
|
---|
[562] | 9 |
|
---|
[571] | 10 | TIME = "" # BIRTHS / GENERATIONAL / REAL
|
---|
| 11 | BALANCE = "" # MIN / DENSITY
|
---|
[562] | 12 |
|
---|
[571] | 13 | DOT_STYLE = "" # NONE / NORMAL / CLEAR
|
---|
| 14 |
|
---|
| 15 | JITTER = "" #
|
---|
| 16 |
|
---|
[562] | 17 | # ------SVG---------
|
---|
| 18 | svg_file = 0
|
---|
| 19 |
|
---|
[577] | 20 | svg_line_style = 'stroke="rgb(90%,10%,16%)" stroke-width="1" stroke-opacity="0.7"'
|
---|
[585] | 21 | svg_mutation_line_style = 'stroke-width="1"'
|
---|
| 22 | svg_crossover_line_style = 'stroke-width="1"'
|
---|
[577] | 23 | svg_spine_line_style = 'stroke="rgb(0%,90%,40%)" stroke-width="2" stroke-opacity="1"'
|
---|
| 24 | svg_scale_line_style = 'stroke="black" stroke-width="0.5" stroke-opacity="1" stroke-dasharray="5, 5"'
|
---|
| 25 |
|
---|
[562] | 26 | svg_dot_style = 'r="2" stroke="black" stroke-width="0.2" fill="red"'
|
---|
[571] | 27 | svg_clear_dot_style = 'r="2" stroke="black" stroke-width="0.4" fill="none"'
|
---|
[562] | 28 | svg_spine_dot_style = 'r="1" stroke="black" stroke-width="0.2" fill="rgb(50%,50%,100%)"'
|
---|
| 29 |
|
---|
[576] | 30 | svg_scale_text_style = 'style="font-family: Arial; font-size: 12; fill: #000000;"'
|
---|
| 31 |
|
---|
[585] | 32 | def hex_to_style(hex):
|
---|
[586] | 33 | default_style = ' stroke="black" stroke-opacity="0.5" '
|
---|
| 34 |
|
---|
[585] | 35 | if hex[0] == "#":
|
---|
| 36 | hex = hex[1:]
|
---|
| 37 |
|
---|
| 38 | if len(hex) == 6 or len(hex) == 8:
|
---|
| 39 | try:
|
---|
| 40 | int(hex, 16)
|
---|
| 41 | except:
|
---|
[615] | 42 | print("Invalid characters in the color's hex #" + hex + "! Assuming black.")
|
---|
[586] | 43 | return default_style
|
---|
[585] | 44 | red = 100*int(hex[0:2], 16)/255
|
---|
| 45 | green = 100*int(hex[2:4], 16)/255
|
---|
| 46 | blue = 100*int(hex[4:6], 16)/255
|
---|
| 47 | opacity = 0.5
|
---|
| 48 | if len(hex) == 8:
|
---|
| 49 | opacity = int(hex[6:8], 16)/255
|
---|
| 50 | return ' stroke="rgb(' +str(red)+ '%,' +str(green)+ '%,' +str(blue)+ '%)" stroke-opacity="' +str(opacity)+ '" '
|
---|
| 51 | else:
|
---|
[615] | 52 | print("Invalid number of digits in the color's hex #" + hex + "! Assuming black.")
|
---|
[586] | 53 | return default_style
|
---|
[585] | 54 |
|
---|
[562] | 55 | def svg_add_line(from_pos, to_pos, style=svg_line_style):
|
---|
[589] | 56 | svg_file.write('<line ' + style + ' x1="' + str(from_pos[0]) + '" x2="' + str(to_pos[0]) +
|
---|
| 57 | '" y1="' + str(from_pos[1]) + '" y2="' + str(to_pos[1]) + '" fill="none"/>')
|
---|
[562] | 58 |
|
---|
[576] | 59 | def svg_add_text(text, pos, anchor, style=svg_scale_text_style):
|
---|
| 60 | svg_file.write('<text ' + style + ' text-anchor="' + anchor + '" x="' + str(pos[0]) + '" y="' + str(pos[1]) + '" >' + text + '</text>')
|
---|
| 61 |
|
---|
[562] | 62 | def svg_add_dot(pos, style=svg_dot_style):
|
---|
| 63 | svg_file.write('<circle ' + style + ' cx="' + str(pos[0]) + '" cy="' + str(pos[1]) + '" />')
|
---|
| 64 |
|
---|
| 65 | def svg_generate_line_style(percent):
|
---|
[564] | 66 | # hotdog
|
---|
[562] | 67 | from_col = [100, 70, 0]
|
---|
[564] | 68 | to_col = [60, 0, 0]
|
---|
[571] | 69 | # lava
|
---|
| 70 | # from_col = [100, 80, 0]
|
---|
| 71 | # to_col = [100, 0, 0]
|
---|
[564] | 72 | # neon
|
---|
| 73 | # from_col = [30, 200, 255]
|
---|
| 74 | # to_col = [240, 0, 220]
|
---|
[562] | 75 |
|
---|
[564] | 76 | from_opa = 0.2
|
---|
| 77 | to_opa = 1.0
|
---|
| 78 | from_stroke = 1
|
---|
| 79 | to_stroke = 3
|
---|
[562] | 80 |
|
---|
[564] | 81 | opa = from_opa*(1-percent) + to_opa*percent
|
---|
| 82 | stroke = from_stroke*(1-percent) + to_stroke*percent
|
---|
| 83 |
|
---|
| 84 | percent = 1 - ((1-percent)**20)
|
---|
| 85 |
|
---|
[562] | 86 | return 'stroke="rgb(' + str(from_col[0]*(1-percent) + to_col[0]*percent) + '%,' \
|
---|
| 87 | + str(from_col[1]*(1-percent) + to_col[1]*percent) + '%,' \
|
---|
[564] | 88 | + str(from_col[2]*(1-percent) + to_col[2]*percent) + '%)" stroke-width="' + str(stroke) + '" stroke-opacity="' + str(opa) + '"'
|
---|
[562] | 89 |
|
---|
[577] | 90 | def svg_generate_dot_style(kind):
|
---|
| 91 | kinds = ["red", "lawngreen", "royalblue", "magenta", "yellow", "cyan", "white", "black"]
|
---|
[562] | 92 |
|
---|
[577] | 93 | r = min(2500/len(nodes), 10)
|
---|
[562] | 94 |
|
---|
[577] | 95 | return 'fill="' + kinds[kind] + '" r="' + str(r) + '" stroke="black" stroke-width="' + str(r/10) + '" fill-opacity="1.0" ' \
|
---|
| 96 | 'stroke-opacity="1.0"'
|
---|
[564] | 97 |
|
---|
[562] | 98 | # -------------------
|
---|
| 99 |
|
---|
| 100 | def load_data(dir):
|
---|
[571] | 101 | global firstnode, nodes, inv_nodes, time
|
---|
[562] | 102 | f = open(dir)
|
---|
[622] | 103 | loaded = 0
|
---|
| 104 |
|
---|
[562] | 105 | for line in f:
|
---|
[571] | 106 | sline = line.split(' ', 1)
|
---|
| 107 | if len(sline) == 2:
|
---|
| 108 | if sline[0] == "[OFFSPRING]":
|
---|
| 109 | creature = json.loads(sline[1])
|
---|
[562] | 110 | #print("B" +str(creature))
|
---|
[563] | 111 | if "FromIDs" in creature:
|
---|
[572] | 112 | if not creature["ID"] in nodes:
|
---|
| 113 | nodes[creature["ID"]] = {}
|
---|
| 114 | # we assign to each parent its contribution to the genotype of the child
|
---|
| 115 | for i in range(0, len(creature["FromIDs"])):
|
---|
| 116 | inherited = 1 #(creature["Inherited"][i] if 'Inherited' in creature else 1) #ONLY FOR NOW
|
---|
| 117 | nodes[creature["ID"]][creature["FromIDs"][i]] = inherited
|
---|
| 118 | else:
|
---|
[615] | 119 | print("Duplicated entry for " + creature["ID"])
|
---|
[572] | 120 | quit()
|
---|
| 121 |
|
---|
[621] | 122 | if not creature["FromIDs"][0] in nodes and firstnode == None:
|
---|
[563] | 123 | firstnode = creature["FromIDs"][0]
|
---|
[572] | 124 |
|
---|
[566] | 125 | if "Time" in creature:
|
---|
| 126 | time[creature["ID"]] = creature["Time"]
|
---|
[562] | 127 |
|
---|
[577] | 128 | if "Kind" in creature:
|
---|
| 129 | kind[creature["ID"]] = creature["Kind"]
|
---|
| 130 |
|
---|
[622] | 131 | loaded += 1
|
---|
| 132 | if loaded == max_nodes and max_nodes != 0:
|
---|
| 133 | break
|
---|
| 134 |
|
---|
[562] | 135 | for k, v in sorted(nodes.items()):
|
---|
[572] | 136 | for val in sorted(v):
|
---|
| 137 | inv_nodes[val] = inv_nodes.get(val, [])
|
---|
| 138 | inv_nodes[val].append(k)
|
---|
[562] | 139 |
|
---|
[621] | 140 | print(len(nodes))
|
---|
[562] | 141 |
|
---|
[621] | 142 |
|
---|
[562] | 143 | def load_simple_data(dir):
|
---|
| 144 | global firstnode, nodes, inv_nodes
|
---|
| 145 | f = open(dir)
|
---|
[622] | 146 | loaded = 0
|
---|
| 147 |
|
---|
[562] | 148 | for line in f:
|
---|
| 149 | sline = line.split()
|
---|
| 150 | if len(sline) > 1:
|
---|
| 151 | #if int(sline[0]) > 15000:
|
---|
| 152 | # break
|
---|
| 153 | if sline[0] == firstnode:
|
---|
| 154 | continue
|
---|
| 155 | nodes[sline[0]] = str(max(int(sline[1]), int(firstnode)))
|
---|
| 156 | else:
|
---|
| 157 | firstnode = sline[0]
|
---|
| 158 |
|
---|
[622] | 159 | loaded += 1
|
---|
| 160 | if loaded == max_nodes and max_nodes != 0:
|
---|
| 161 | break
|
---|
| 162 |
|
---|
[562] | 163 | for k, v in sorted(nodes.items()):
|
---|
| 164 | inv_nodes[v] = inv_nodes.get(v, [])
|
---|
| 165 | inv_nodes[v].append(k)
|
---|
| 166 |
|
---|
| 167 | #print(str(inv_nodes))
|
---|
| 168 | #quit()
|
---|
| 169 |
|
---|
| 170 | def compute_depth(node):
|
---|
| 171 | my_depth = 0
|
---|
| 172 | if node in inv_nodes:
|
---|
| 173 | for c in inv_nodes[node]:
|
---|
| 174 | my_depth = max(my_depth, compute_depth(c)+1)
|
---|
| 175 | depth[node] = my_depth
|
---|
| 176 | return my_depth
|
---|
| 177 |
|
---|
| 178 | # ------------------------------------
|
---|
| 179 |
|
---|
| 180 | def xmin_crowd(x1, x2, y):
|
---|
| 181 | if BALANCE == "RANDOM":
|
---|
| 182 | return (x1 if random.randrange(2) == 0 else x2)
|
---|
| 183 | elif BALANCE == "MIN":
|
---|
| 184 | x1_closest = 999999
|
---|
| 185 | x2_closest = 999999
|
---|
| 186 | for pos in positions:
|
---|
| 187 | pos = positions[pos]
|
---|
| 188 | if pos[1] == y:
|
---|
| 189 | x1_closest = min(x1_closest, abs(x1-pos[0]))
|
---|
| 190 | x2_closest = min(x2_closest, abs(x2-pos[0]))
|
---|
| 191 | return (x1 if x1_closest > x2_closest else x2)
|
---|
| 192 | elif BALANCE == "DENSITY":
|
---|
| 193 | x1_dist = 0
|
---|
| 194 | x2_dist = 0
|
---|
[622] | 195 | ymin = y-10
|
---|
| 196 | ymax = y+10
|
---|
[562] | 197 | for pos in positions:
|
---|
| 198 | pos = positions[pos]
|
---|
[622] | 199 | if pos[1] > ymin or pos[1] < ymax:
|
---|
| 200 | dysq = (pos[1]-y)**2
|
---|
[562] | 201 | dx1 = pos[0]-x1
|
---|
| 202 | dx2 = pos[0]-x2
|
---|
| 203 |
|
---|
[622] | 204 |
|
---|
| 205 | x1_dist += math.sqrt(dysq + dx1**2)
|
---|
| 206 | x2_dist += math.sqrt(dysq + dx2**2)
|
---|
[562] | 207 | return (x1 if x1_dist > x2_dist else x2)
|
---|
| 208 |
|
---|
| 209 | # ------------------------------------
|
---|
| 210 |
|
---|
[621] | 211 | def prepos_children():
|
---|
| 212 | global max_height, max_width, min_width, visited, TIME
|
---|
[571] | 213 |
|
---|
[621] | 214 | print("firstnode " + firstnode)
|
---|
[562] | 215 |
|
---|
[621] | 216 | if not bool(time):
|
---|
| 217 | print("REAL time requested, but no real time data provided. Assuming BIRTHS time instead.")
|
---|
| 218 | TIME = "BIRTHS"
|
---|
[572] | 219 |
|
---|
[621] | 220 | positions[firstnode] = [0, 0]
|
---|
[572] | 221 |
|
---|
[621] | 222 | #visited = {}
|
---|
| 223 | #visited[firstnode] = True
|
---|
[572] | 224 |
|
---|
[562] | 225 |
|
---|
[621] | 226 | nodes_to_visit = [firstnode]
|
---|
[562] | 227 |
|
---|
[621] | 228 | ccc = 0
|
---|
| 229 | timet = ttime.time()
|
---|
[566] | 230 |
|
---|
[621] | 231 | while True:
|
---|
[562] | 232 |
|
---|
[621] | 233 | ccc += 1
|
---|
| 234 | if ccc%1000 == 0 :
|
---|
| 235 | print(str(ccc) + " " + str(ttime.time()-timet))
|
---|
| 236 | timet = ttime.time()
|
---|
[562] | 237 |
|
---|
[621] | 238 | current_node = nodes_to_visit[0]
|
---|
| 239 |
|
---|
| 240 | if current_node in inv_nodes:
|
---|
| 241 | for c in inv_nodes[current_node]:
|
---|
| 242 | # we want to visit the node just once, after all of its parents
|
---|
| 243 | if c not in nodes_to_visit:
|
---|
| 244 | nodes_to_visit.append(c)
|
---|
| 245 |
|
---|
| 246 | cy = 0
|
---|
| 247 | if TIME == "BIRTHS":
|
---|
| 248 | if c[0] == "c":
|
---|
| 249 | cy = int(c[1:])
|
---|
| 250 | else:
|
---|
| 251 | cy = int(c)
|
---|
| 252 | elif TIME == "GENERATIONAL":
|
---|
| 253 | cy = positions[current_node][1]+1
|
---|
| 254 | elif TIME == "REAL":
|
---|
| 255 | cy = time[c]
|
---|
| 256 |
|
---|
| 257 | if len(nodes[c]) == 1:
|
---|
| 258 | dissimilarity = 0
|
---|
| 259 | if JITTER == True:
|
---|
| 260 | dissimilarity = random.gauss(0,1)
|
---|
| 261 | else:
|
---|
| 262 | dissimilarity = 1
|
---|
| 263 | positions[c] = [xmin_crowd(positions[current_node][0]-dissimilarity, positions[current_node][0]+dissimilarity, cy), cy]
|
---|
| 264 | else:
|
---|
| 265 | vsum = sum([v for k, v in nodes[c].items()])
|
---|
| 266 | cx = sum([positions[k][0]*v/vsum for k, v in nodes[c].items()])
|
---|
| 267 |
|
---|
| 268 | if JITTER == True:
|
---|
| 269 | positions[c] = [cx + random.gauss(0, 0.1), cy]
|
---|
| 270 | else:
|
---|
| 271 | positions[c] = [cx, cy]
|
---|
| 272 |
|
---|
| 273 |
|
---|
| 274 | #if c in inv_nodes:
|
---|
| 275 | # prepos_children_reccurent(c)
|
---|
| 276 |
|
---|
| 277 | nodes_to_visit = nodes_to_visit[1:]
|
---|
| 278 | # if none left, we can stop
|
---|
| 279 | if len(nodes_to_visit) == 0:
|
---|
| 280 | break
|
---|
| 281 |
|
---|
| 282 |
|
---|
| 283 | # prepos_children_reccurent(firstnode)
|
---|
| 284 |
|
---|
[562] | 285 | for pos in positions:
|
---|
| 286 | max_height = max(max_height, positions[pos][1])
|
---|
| 287 | max_width = max(max_width, positions[pos][0])
|
---|
| 288 | min_width = min(min_width, positions[pos][0])
|
---|
| 289 |
|
---|
| 290 | # ------------------------------------
|
---|
| 291 |
|
---|
[572] | 292 | def all_parents_visited(node):
|
---|
| 293 | apv = True
|
---|
| 294 | for k, v in sorted(nodes[node].items()):
|
---|
| 295 | if not k in visited:
|
---|
| 296 | apv = False
|
---|
| 297 | break
|
---|
| 298 | return apv
|
---|
| 299 | # ------------------------------------
|
---|
| 300 |
|
---|
[619] | 301 | def draw_children():
|
---|
[617] | 302 | max_depth = 0
|
---|
| 303 | for k, v in depth.items():
|
---|
| 304 | max_depth = max(max_depth, v)
|
---|
| 305 |
|
---|
| 306 | nodes_to_visit = [firstnode]
|
---|
| 307 | while True:
|
---|
| 308 | current_node = nodes_to_visit[0]
|
---|
| 309 |
|
---|
[618] | 310 | if current_node in inv_nodes:
|
---|
| 311 | for c in inv_nodes[current_node]: # inv_node => p->c
|
---|
[617] | 312 |
|
---|
[618] | 313 | if not c in nodes_to_visit:
|
---|
| 314 | nodes_to_visit.append(c)
|
---|
[617] | 315 |
|
---|
[618] | 316 | line_style = ""
|
---|
| 317 | if COLORING == "NONE":
|
---|
| 318 | line_style = svg_line_style
|
---|
| 319 | elif COLORING == "TYPE":
|
---|
| 320 | line_style = (svg_mutation_line_style if len(nodes[c]) == 1 else svg_crossover_line_style)
|
---|
| 321 | else: # IMPORTANCE, default
|
---|
| 322 | line_style = svg_generate_line_style(depth[c]/max_depth)
|
---|
[617] | 323 |
|
---|
[618] | 324 | svg_add_line( (w_margin+w_no_margs*(positions[current_node][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[current_node][1]/max_height),
|
---|
| 325 | (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)
|
---|
[617] | 326 |
|
---|
[618] | 327 | # we want to draw the node just once
|
---|
| 328 | if DOT_STYLE == "NONE":
|
---|
| 329 | continue
|
---|
| 330 | elif DOT_STYLE == "TYPE":
|
---|
| 331 | dot_style = svg_generate_dot_style(kind[current_node] if current_node in kind else 0) #type
|
---|
| 332 | else: # NORMAL, default
|
---|
| 333 | dot_style = svg_clear_dot_style #svg_generate_dot_style(depth[c]/max_depth)
|
---|
| 334 | svg_add_dot( (w_margin+w_no_margs*(positions[current_node][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[current_node][1]/max_height), dot_style)
|
---|
| 335 | #svg_add_text( str(depth[current_node]), (w_margin+w_no_margs*(positions[current_node][0]-min_width)/(max_width-min_width),
|
---|
| 336 | # h_margin+h_no_margs*positions[current_node][1]/max_height), "end")
|
---|
[617] | 337 |
|
---|
| 338 | # we remove the current node from the list
|
---|
| 339 | nodes_to_visit = nodes_to_visit[1:]
|
---|
| 340 | # if none left, we can stop
|
---|
| 341 | if len(nodes_to_visit) == 0:
|
---|
| 342 | break
|
---|
| 343 |
|
---|
[562] | 344 | def draw_spine():
|
---|
[619] | 345 | nodes_to_visit = [firstnode]
|
---|
| 346 | while True:
|
---|
| 347 | current_node = nodes_to_visit[0]
|
---|
[594] | 348 |
|
---|
[619] | 349 | if current_node in inv_nodes:
|
---|
| 350 | for c in inv_nodes[current_node]: # inv_node => p->c
|
---|
| 351 | if depth[c] == depth[current_node] - 1:
|
---|
| 352 | if not c in nodes_to_visit:
|
---|
| 353 | nodes_to_visit.append(c)
|
---|
| 354 | line_style = svg_spine_line_style
|
---|
| 355 | svg_add_line( (w_margin+w_no_margs*(positions[current_node][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[current_node][1]/max_height),
|
---|
| 356 | (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] | 357 |
|
---|
[619] | 358 | # we remove the current node from the list
|
---|
| 359 | nodes_to_visit = nodes_to_visit[1:]
|
---|
| 360 | # if none left, we can stop
|
---|
| 361 | if len(nodes_to_visit) == 0:
|
---|
| 362 | break
|
---|
[564] | 363 |
|
---|
[619] | 364 | def draw_skeleton():
|
---|
| 365 | nodes_to_visit = [firstnode]
|
---|
| 366 | while True:
|
---|
| 367 | current_node = nodes_to_visit[0]
|
---|
[594] | 368 |
|
---|
[619] | 369 | if current_node in inv_nodes:
|
---|
| 370 | for c in inv_nodes[current_node]: # inv_node => p->c
|
---|
| 371 | if depth[c] >= min_skeleton_depth:
|
---|
| 372 | if not c in nodes_to_visit:
|
---|
| 373 | nodes_to_visit.append(c)
|
---|
| 374 | line_style = svg_spine_line_style
|
---|
| 375 | svg_add_line( (w_margin+w_no_margs*(positions[current_node][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[current_node][1]/max_height),
|
---|
| 376 | (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)
|
---|
[594] | 377 |
|
---|
[619] | 378 | # we remove the current node from the list
|
---|
| 379 | nodes_to_visit = nodes_to_visit[1:]
|
---|
| 380 | # if none left, we can stop
|
---|
| 381 | if len(nodes_to_visit) == 0:
|
---|
| 382 | break
|
---|
[615] | 383 |
|
---|
[576] | 384 | # ------------------------------------
|
---|
[562] | 385 |
|
---|
[576] | 386 | def draw_scale(filename ,type):
|
---|
[562] | 387 |
|
---|
[615] | 388 | svg_add_text("Generated from " + filename.split("\\")[-1], (5, 15), "start")
|
---|
[576] | 389 |
|
---|
| 390 | svg_add_line( (w*0.7, h_margin), (w, h_margin), svg_scale_line_style)
|
---|
| 391 | start_text = ""
|
---|
| 392 | if TIME == "BIRTHS":
|
---|
| 393 | start_text = "Birth #" + str(min([int(k[1:]) for k, v in nodes.items()]))
|
---|
| 394 | if TIME == "REAL":
|
---|
| 395 | start_text = "Time " + str(min([v for k, v in time.items()]))
|
---|
| 396 | if TIME == "GENERATIONAL":
|
---|
| 397 | start_text = "Depth " + str(min([v for k, v in depth.items()]))
|
---|
| 398 | svg_add_text( start_text, (w, h_margin + 15), "end")
|
---|
| 399 |
|
---|
| 400 | svg_add_line( (w*0.7, h-h_margin), (w, h-h_margin), svg_scale_line_style)
|
---|
| 401 | end_text = ""
|
---|
| 402 | if TIME == "BIRTHS":
|
---|
| 403 | end_text = "Birth #" + str(max([int(k[1:]) for k, v in nodes.items()]))
|
---|
| 404 | if TIME == "REAL":
|
---|
| 405 | end_text = "Time " + str(max([v for k, v in time.items()]))
|
---|
| 406 | if TIME == "GENERATIONAL":
|
---|
| 407 | end_text = "Depth " + str(max([v for k, v in depth.items()]))
|
---|
[577] | 408 | svg_add_text( end_text, (w, h-h_margin + 15), "end")
|
---|
[576] | 409 |
|
---|
| 410 |
|
---|
[562] | 411 | ##################################################### main #####################################################
|
---|
| 412 |
|
---|
| 413 | args = 0
|
---|
| 414 |
|
---|
| 415 | h = 800
|
---|
| 416 | w = 600
|
---|
[576] | 417 | h_margin = 20
|
---|
[562] | 418 | w_margin = 10
|
---|
| 419 | h_no_margs = h - 2* h_margin
|
---|
| 420 | w_no_margs = w - 2* w_margin
|
---|
| 421 |
|
---|
| 422 | max_height = 0
|
---|
| 423 | max_width = 0
|
---|
| 424 | min_width = 9999999999
|
---|
| 425 |
|
---|
| 426 | min_skeleton_depth = 0
|
---|
[622] | 427 | max_nodes = 0
|
---|
[562] | 428 |
|
---|
[621] | 429 | firstnode = None
|
---|
[562] | 430 | nodes = {}
|
---|
| 431 | inv_nodes = {}
|
---|
| 432 | positions = {}
|
---|
[572] | 433 | visited= {}
|
---|
[562] | 434 | depth = {}
|
---|
[566] | 435 | time = {}
|
---|
[577] | 436 | kind = {}
|
---|
[562] | 437 |
|
---|
| 438 | def main():
|
---|
[622] | 439 | global svg_file, min_skeleton_depth, max_nodes, args, \
|
---|
[585] | 440 | TIME, BALANCE, DOT_STYLE, COLORING, JITTER, \
|
---|
| 441 | svg_mutation_line_style, svg_crossover_line_style
|
---|
[562] | 442 |
|
---|
[615] | 443 | parser = argparse.ArgumentParser(description='Draws a genealogical tree (generates a SVG file) based on parent-child relationship information from a text file. Supports files generated by Framsticks experiments.')
|
---|
| 444 | parser.add_argument('-i', '--in', dest='input', required=True, help='input file name with stuctured evolutionary data')
|
---|
| 445 | parser.add_argument('-o', '--out', dest='output', required=True, help='output file name for the evolutionary tree (SVG format)')
|
---|
[562] | 446 | draw_tree_parser = parser.add_mutually_exclusive_group(required=False)
|
---|
| 447 | draw_tree_parser.add_argument('--draw-tree', dest='draw_tree', action='store_true', help='whether drawing the full tree should be skipped')
|
---|
| 448 | draw_tree_parser.add_argument('--no-draw-tree', dest='draw_tree', action='store_false')
|
---|
| 449 |
|
---|
| 450 | draw_skeleton_parser = parser.add_mutually_exclusive_group(required=False)
|
---|
| 451 | draw_skeleton_parser.add_argument('--draw-skeleton', dest='draw_skeleton', action='store_true', help='whether the skeleton of the tree should be drawn')
|
---|
| 452 | draw_skeleton_parser.add_argument('--no-draw-skeleton', dest='draw_skeleton', action='store_false')
|
---|
| 453 |
|
---|
| 454 | draw_spine_parser = parser.add_mutually_exclusive_group(required=False)
|
---|
| 455 | draw_spine_parser.add_argument('--draw-spine', dest='draw_spine', action='store_true', help='whether the spine of the tree should be drawn')
|
---|
| 456 | draw_spine_parser.add_argument('--no-draw-spine', dest='draw_spine', action='store_false')
|
---|
| 457 |
|
---|
| 458 | #TODO: better names for those parameters
|
---|
[620] | 459 | parser.add_argument('-t', '--time', default='GENERATIONAL', dest='time', help='values on vertical axis (BIRTHS/GENERATIONAL(d)/REAL); '
|
---|
[614] | 460 | 'BIRTHS: time measured as the number of births since the beginning; '
|
---|
[571] | 461 | 'GENERATIONAL: time measured as number of ancestors; '
|
---|
| 462 | 'REAL: real time of the simulation')
|
---|
[620] | 463 | parser.add_argument('-b', '--balance', default='DENSITY', dest='balance', help='method of placing nodes in the tree (RANDOM/MIN/DENSITY(d))')
|
---|
| 464 | parser.add_argument('-s', '--scale', default='NONE', dest='scale', help='type of timescale added to the tree (NONE(d)/SIMPLE)')
|
---|
| 465 | parser.add_argument('-c', '--coloring', default='IMPORTANCE', dest="coloring", help='method of coloring the tree (NONE/IMPORTANCE(d)/TYPE)')
|
---|
| 466 | parser.add_argument('-d', '--dots', default='TYPE', dest='dots', help='method of drawing dots (individuals) (NONE/NORMAL/TYPE(d))')
|
---|
[571] | 467 | parser.add_argument('-j', '--jitter', dest="jitter", action='store_true', help='draw horizontal positions of children from the normal distribution')
|
---|
| 468 |
|
---|
[585] | 469 | parser.add_argument('--color-mut', default="#000000", dest="color_mut", help='color of clone/mutation lines in rgba (e.g. #FF60B240) for TYPE coloring')
|
---|
| 470 | parser.add_argument('--color-cross', default="#660198", dest="color_cross", help='color of crossover lines in rgba (e.g. #FF60B240) for TYPE coloring')
|
---|
| 471 |
|
---|
[562] | 472 | 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')
|
---|
| 473 | parser.add_argument('--seed', type=int, dest='seed', help='seed for the random number generator (-1 for random)')
|
---|
| 474 |
|
---|
| 475 | parser.add_argument('--simple-data', type=bool, dest='simple_data', help='input data are given in a simple format (#child #parent)')
|
---|
| 476 |
|
---|
[622] | 477 |
|
---|
| 478 | parser.add_argument('-x', '--max-nodes', type=int, default=0, dest='max_nodes', help='maximum number of nodes drawn (starting from the first one)')
|
---|
| 479 |
|
---|
[562] | 480 | parser.set_defaults(draw_tree=True)
|
---|
| 481 | parser.set_defaults(draw_skeleton=False)
|
---|
| 482 | parser.set_defaults(draw_spine=False)
|
---|
| 483 |
|
---|
| 484 | parser.set_defaults(seed=-1)
|
---|
| 485 |
|
---|
| 486 | args = parser.parse_args()
|
---|
| 487 |
|
---|
[620] | 488 | TIME = args.time.upper()
|
---|
| 489 | BALANCE = args.balance.upper()
|
---|
| 490 | DOT_STYLE = args.dots.upper()
|
---|
| 491 | COLORING = args.coloring.upper()
|
---|
| 492 | SCALE = args.scale.upper()
|
---|
[571] | 493 | JITTER = args.jitter
|
---|
[620] | 494 | if not TIME in ['BIRTHS', 'GENERATIONAL', 'REAL']\
|
---|
| 495 | or not BALANCE in ['RANDOM', 'MIN', 'DENSITY']\
|
---|
| 496 | or not DOT_STYLE in ['NONE', 'NORMAL', 'TYPE']\
|
---|
| 497 | or not COLORING in ['NONE', 'IMPORTANCE', 'TYPE']\
|
---|
| 498 | or not SCALE in ['NONE', 'SIMPLE']:
|
---|
| 499 | print("Incorrect value of one of the parameters! Closing the program.") #TODO don't be lazy, figure out which parameter is wrong...
|
---|
| 500 | return
|
---|
[562] | 501 |
|
---|
[620] | 502 |
|
---|
[585] | 503 | svg_mutation_line_style += hex_to_style(args.color_mut)
|
---|
| 504 | svg_crossover_line_style += hex_to_style(args.color_cross)
|
---|
| 505 |
|
---|
[562] | 506 | dir = args.input
|
---|
| 507 | min_skeleton_depth = args.min_skeleton_depth
|
---|
[622] | 508 | max_nodes = args.max_nodes
|
---|
[562] | 509 | seed = args.seed
|
---|
| 510 | if seed == -1:
|
---|
| 511 | seed = random.randint(0, 10000)
|
---|
| 512 | random.seed(seed)
|
---|
| 513 | print("seed:", seed)
|
---|
| 514 |
|
---|
| 515 | if args.simple_data:
|
---|
| 516 | load_simple_data(dir)
|
---|
| 517 | else:
|
---|
| 518 | load_data(dir)
|
---|
| 519 |
|
---|
| 520 | compute_depth(firstnode)
|
---|
| 521 |
|
---|
| 522 | svg_file = open(args.output, "w")
|
---|
| 523 | 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" '
|
---|
| 524 | 'width="' + str(w) + '" height="' + str(h) + '">')
|
---|
| 525 |
|
---|
| 526 | prepos_children()
|
---|
| 527 |
|
---|
| 528 | if args.draw_tree:
|
---|
[619] | 529 | draw_children()
|
---|
[562] | 530 | if args.draw_skeleton:
|
---|
| 531 | draw_skeleton()
|
---|
| 532 | if args.draw_spine:
|
---|
| 533 | draw_spine()
|
---|
| 534 |
|
---|
[620] | 535 | draw_scale(dir, SCALE)
|
---|
[576] | 536 |
|
---|
[562] | 537 | svg_file.write("</svg>")
|
---|
| 538 | svg_file.close()
|
---|
| 539 |
|
---|
| 540 | main()
|
---|