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