Changeset 572 for mds-and-trees


Ignore:
Timestamp:
08/08/16 20:13:29 (8 years ago)
Author:
konrad
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • mds-and-trees/tree-genealogy.py

    r571 r572  
    8585                #print("B" +str(creature))
    8686                if "FromIDs" in creature:
    87                     #assert(len(creature["FromIDs"]) == 1)
    88                     nodes[creature["ID"]] = creature["FromIDs"][0]
     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
    8997                    if not creature["FromIDs"][0] in nodes:
    9098                        firstnode = creature["FromIDs"][0]
     99
    91100                if "Time" in creature:
    92101                    time[creature["ID"]] = creature["Time"]
    93102
    94103    for k, v in sorted(nodes.items()):
    95         inv_nodes[v] = inv_nodes.get(v, [])
    96         inv_nodes[v].append(k)
     104        for val in sorted(v):
     105            inv_nodes[val] = inv_nodes.get(val, [])
     106            inv_nodes[val].append(k)
    97107
    98108
     
    157167
    158168def prepos_children_reccurent(node):
     169    global visited
    159170    for c in inv_nodes[node]:
    160         #print(node + "->" + c)
    161         if JITTER == True:
    162             dissimilarity = random.gauss(0,1)
     171
     172        # we want to visit the node just once, after all of its parents
     173        if not all_parents_visited(c):
     174            continue
    163175        else:
    164             dissimilarity = 1
    165             #TODO take this info from proper fields
    166 
     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
    167185        if TIME == "BIRTHS":
    168             id = ""
    169186            if c[0] == "c":
    170                 id = int(c[1:])
     187                cy = int(c[1:])
    171188            else:
    172                 id = int(c)
    173             positions[c] = [xmin_crowd(positions[node][0]-dissimilarity, positions[node][0]+dissimilarity, id), id]
     189                cy = int(c)
    174190        elif TIME == "GENERATIONAL":
    175             positions[c] = [xmin_crowd(positions[node][0]-dissimilarity, positions[node][0]+dissimilarity, positions[node][1]+1), positions[node][1]+1]
     191            cy = positions[node][1]+1
    176192        elif TIME == "REAL":
    177             positions[c] = [xmin_crowd(positions[node][0]-dissimilarity, positions[node][0]+dissimilarity, time[c]), time[c]]
    178 
    179     for c in inv_nodes[node]:
     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
    180212        if c in inv_nodes:
    181213            prepos_children_reccurent(c)
    182214
    183215def prepos_children():
    184     global max_height, max_width, min_width
     216    global max_height, max_width, min_width, visited
    185217
    186218    if not bool(time):
     
    190222    positions[firstnode] = [0, 0]
    191223
     224    visited = {}
     225    visited[firstnode] = True
    192226    prepos_children_reccurent(firstnode)
    193227
     
    199233# ------------------------------------
    200234
     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
    201244def draw_children_recurrent(node, max_depth):
    202     global max_height, max_width, min_width
     245    global visited
     246
    203247    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
    204255        if c in inv_nodes:
    205256            draw_children_recurrent(c, max_depth)
    206257
    207258        line_style = (svg_line_style if args.mono_tree else svg_generate_line_style(depth[c]/max_depth))
    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)
     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)
    210262
    211263        if DOT_STYLE == "NONE":
     
    217269        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)
    218270def draw_children():
     271    global visited
     272    visited = {}
     273    visited[firstnode] = True
     274
    219275    max_depth = 0
    220276    for k, v in depth.items():
     
    231287
    232288def draw_spine_recurrent(node):
    233     global max_height, max_width, min_width
    234289    for c in inv_nodes[node]:
    235290        if depth[c] == depth[node] - 1:
     
    246301
    247302def draw_skeleton_reccurent(node, max_depth):
    248     global max_height, max_width, min_width
    249303    for c in inv_nodes[node]:
    250304        if depth[c] >= min_skeleton_depth or depth[c] == max([depth[q] for q in inv_nodes[node]]):
     
    289343inv_nodes = {}
    290344positions = {}
     345visited= {}
    291346depth = {}
    292347time = {}
Note: See TracChangeset for help on using the changeset viewer.