def SpeciationEvent_effi(u, x, S_cost, subtreeLoss, k, H, nodes_table): heap = [] if tree_operations.has_right_child(u) and tree_operations.has_left_child( u): w = u.adjacent_nodes()[0] v = u.adjacent_nodes()[1] if not tree_operations.is_a_leaf(x): if (tree_operations.has_left_child(x)) and ( tree_operations.has_right_child(x)): y = x.adjacent_nodes()[0] z = x.adjacent_nodes()[1] list_v_to_left = utiles.kmin_positive( subtreeLoss[v.label][y.label], k, H, nodes_table, 'cost_with_losses') list_w_to_left = utiles.kmin_positive( subtreeLoss[w.label][y.label], k, H, nodes_table, 'cost_with_losses') list_v_to_right = utiles.kmin_positive( subtreeLoss[v.label][z.label], k, H, nodes_table, 'cost_with_losses') list_w_to_right = utiles.kmin_positive( subtreeLoss[w.label][z.label], k, H, nodes_table, 'cost_with_losses') for hyper_node1 in list_v_to_right: for hyper_node2 in list_w_to_left: cost_without_losses = hyper_node1[1][ 'cost_without_losses'] + hyper_node2[1][ 'cost_without_losses'] + S_cost cost_with_losses = hyper_node1[1][ 'cost_with_losses'] + hyper_node2[1][ 'cost_with_losses'] + S_cost heapq.heappush( heap, utiles.heap_items(cost_without_losses, cost_with_losses, hyper_node1, hyper_node2)) for hyper_node1 in list_v_to_left: for hyper_node2 in list_w_to_right: cost_without_losses = hyper_node1[1][ 'cost_without_losses'] + hyper_node2[1][ 'cost_without_losses'] + S_cost cost_with_losses = hyper_node1[1][ 'cost_with_losses'] + hyper_node2[1][ 'cost_with_losses'] + S_cost heapq.heappush( heap, utiles.heap_items(cost_without_losses, cost_with_losses, hyper_node1, hyper_node2)) return heap
def rec_multi2bi(non_binary_tree): if not non_binary_tree or tree_operations.is_a_leaf(non_binary_tree.seed_node): return non_binary_tree seed = non_binary_tree.seed_node left_binary, right_binary = split_randomly_into_two_sets(seed.child_nodes()) left_binary_tree = rec_multi2bi(left_binary) right_binary_tree = rec_multi2bi(right_binary) new_bin = tr.Tree() if seed.taxon or seed.label: if seed.taxon: new_bin.seed_node.taxon = non_binary_tree.taxon else: new_bin.seed_node.label = seed.label if (left_binary_tree and (left_binary_tree.seed_node.taxon or not tree_operations.is_a_leaf(left_binary_tree.seed_node))) and\ (right_binary_tree and (right_binary_tree.seed_node.taxon or not tree_operations.is_a_leaf(right_binary_tree.seed_node))): new_bin.seed_node.set_child_nodes([left_binary_tree.seed_node,right_binary_tree.seed_node]) elif not left_binary_tree: new_bin.seed_node.set_child_nodes([right_binary_tree.seed_node]) else: new_bin.seed_node.set_child_nodes([left_binary_tree.seed_node]) return new_bin
def number_of_scpecies_doup(G,old_sigma): leafs_names = {} should_be_found = [] for u in G.postorder_node_iter(): if tree_operations.is_a_leaf(u): specie = old_sigma[u.taxon.label] if specie in leafs_names: leafs_names[specie] += 1 else: leafs_names.update({specie:1}) for name,score in leafs_names.items(): if score > number_of_douplications: should_be_found.append(name) return should_be_found
def DuplicationEvent(H, u, x, D_cost, nodes_table): heap = [] if (not tree_operations.is_a_leaf(x)): v = [] w = [] list_v_to_right = [] list_w_to_right = [] list_v_to_left = [] list_w_to_left = [] if (tree_operations.has_right_child(u)): v = u.adjacent_nodes()[0] if (tree_operations.has_left_child(u)): w = u.adjacent_nodes()[1] if (tree_operations.has_right_child(x)): for y in x.adjacent_nodes()[0].postorder_iter(): # left subtree list_v_to_left = list_v_to_left + find_nodes_in_hypergraph( H, v.label, y.label, 0, nodes_table) list_w_to_left = list_w_to_left + find_nodes_in_hypergraph( H, w.label, y.label, 0, nodes_table) if (tree_operations.has_left_child(x)): for z in x.adjacent_nodes()[1].postorder_iter(): # right subtree list_v_to_right = list_v_to_right + find_nodes_in_hypergraph( H, v.label, z.label, 0, nodes_table) list_w_to_right = list_w_to_right + find_nodes_in_hypergraph( H, w.label, z.label, 0, nodes_table) for hyper_node1 in list_v_to_right: for hyper_node2 in list_w_to_right: heapq.heappush( heap, utiles.heap_items( hyper_node1[1]['cost'] + hyper_node2[1]['cost'] + D_cost, hyper_node1, hyper_node2)) for hyper_node1 in list_v_to_left: for hyper_node2 in list_w_to_left: heapq.heappush( heap, utiles.heap_items( hyper_node1[1]['cost'] + hyper_node2[1]['cost'] + D_cost, hyper_node1, hyper_node2)) return heap
def DuplicationEvent_effi(H, u, x, D_cost, loss_cost, nodes_table, subtreeLoss, k): heap = [] if not tree_operations.is_a_leaf(x): list_v_to_right = [] list_w_to_right = [] list_v_to_left = [] list_w_to_left = [] if tree_operations.has_right_child( x) and tree_operations.has_right_child(u): y = x.adjacent_nodes()[0] v = u.adjacent_nodes()[0] list_v_to_left = utiles.kmin_positive( subtreeLoss[v.label][y.label], k, H, nodes_table, 'cost_with_losses') if tree_operations.has_right_child( x) and tree_operations.has_left_child(u): y = x.adjacent_nodes()[0] w = u.adjacent_nodes()[1] list_w_to_left = utiles.kmin_positive( subtreeLoss[w.label][y.label], k, H, nodes_table, 'cost_with_losses') if tree_operations.has_left_child( x) and tree_operations.has_right_child(u): z = x.adjacent_nodes()[1] v = u.adjacent_nodes()[0] list_v_to_right = utiles.kmin_positive( subtreeLoss[v.label][z.label], k, H, nodes_table, 'cost_with_losses') if tree_operations.has_left_child( x) and tree_operations.has_left_child(u): z = x.adjacent_nodes()[1] w = u.adjacent_nodes()[1] list_w_to_right = utiles.kmin_positive( subtreeLoss[w.label][z.label], k, H, nodes_table, 'cost_with_losses') for hyper_node1 in list_v_to_right: for hyper_node2 in list_w_to_right: cost_without_losses = hyper_node1[1][ 'cost_without_losses'] + hyper_node2[1][ 'cost_without_losses'] + D_cost cost_with_losses = hyper_node1[1][ 'cost_with_losses'] + hyper_node2[1][ 'cost_with_losses'] + D_cost + 2 * loss_cost heapq.heappush( heap, utiles.heap_items(cost_without_losses, cost_with_losses, hyper_node1, hyper_node2)) hyper_node3 = find_nodes_in_hypergraph(H, w.label, x.label, 0, nodes_table) if hyper_node3: hyper_node3 = hyper_node3[0] cost_without_losses = hyper_node1[1][ 'cost_without_losses'] + hyper_node3[1][ 'cost_without_losses'] + D_cost cost_with_losses = hyper_node1[1][ 'cost_with_losses'] + hyper_node3[1][ 'cost_with_losses'] + D_cost + loss_cost heapq.heappush( heap, utiles.heap_items(cost_without_losses, cost_with_losses, hyper_node1, hyper_node3)) for hyper_node1 in list_v_to_left: for hyper_node2 in list_w_to_left: cost_without_losses = hyper_node1[1][ 'cost_without_losses'] + hyper_node2[1][ 'cost_without_losses'] + D_cost cost_with_losses = hyper_node1[1][ 'cost_with_losses'] + hyper_node2[1][ 'cost_with_losses'] + D_cost + 2 * loss_cost heapq.heappush( heap, utiles.heap_items(cost_without_losses, cost_with_losses, hyper_node1, hyper_node2)) hyper_node3 = find_nodes_in_hypergraph(H, w.label, x.label, 0, nodes_table) if hyper_node3: hyper_node3 = hyper_node3[0] cost_without_losses = hyper_node1[1][ 'cost_without_losses'] + hyper_node3[1][ 'cost_without_losses'] + D_cost cost_with_losses = hyper_node1[1][ 'cost_with_losses'] + hyper_node3[1][ 'cost_with_losses'] + D_cost + loss_cost heapq.heappush( heap, utiles.heap_items(cost_without_losses, cost_with_losses, hyper_node1, hyper_node3)) for hyper_node1 in list_w_to_right: hyper_node2 = find_nodes_in_hypergraph(H, v.label, x.label, 0, nodes_table) if hyper_node2: hyper_node2 = hyper_node2[0] cost_without_losses = hyper_node1[1][ 'cost_without_losses'] + hyper_node2[1][ 'cost_without_losses'] + D_cost cost_with_losses = hyper_node1[1][ 'cost_with_losses'] + hyper_node2[1][ 'cost_with_losses'] + D_cost + loss_cost heapq.heappush( heap, utiles.heap_items(cost_without_losses, cost_with_losses, hyper_node1, hyper_node2)) for hyper_node1 in list_w_to_left: hyper_node2 = find_nodes_in_hypergraph(H, v.label, x.label, 0, nodes_table) if hyper_node2 != []: hyper_node2 = hyper_node2[0] cost_without_losses = hyper_node1[1][ 'cost_without_losses'] + hyper_node2[1][ 'cost_without_losses'] + D_cost cost_with_losses = hyper_node1[1][ 'cost_with_losses'] + hyper_node2[1][ 'cost_with_losses'] + D_cost + loss_cost heapq.heappush( heap, utiles.heap_items(cost_without_losses, cost_with_losses, hyper_node1, hyper_node2)) for hyper_node1 in list_v_to_left: for hyper_node2 in list_w_to_right: cost_without_losses = hyper_node1[1][ 'cost_without_losses'] + hyper_node2[1][ 'cost_without_losses'] + D_cost cost_with_losses = hyper_node1[1][ 'cost_with_losses'] + hyper_node2[1][ 'cost_with_losses'] + D_cost + 2 * loss_cost heapq.heappush( heap, utiles.heap_items(cost_without_losses, cost_with_losses, hyper_node1, hyper_node2)) for hyper_node1 in list_w_to_left: for hyper_node2 in list_v_to_right: cost_without_losses = hyper_node1[1][ 'cost_without_losses'] + hyper_node2[1][ 'cost_without_losses'] + D_cost cost_with_losses = hyper_node1[1][ 'cost_with_losses'] + hyper_node2[1][ 'cost_with_losses'] + D_cost + 2 * loss_cost heapq.heappush( heap, utiles.heap_items(cost_without_losses, cost_with_losses, hyper_node1, hyper_node2)) hyper_node1 = find_nodes_in_hypergraph(H, w.label, x.label, 0, nodes_table) hyper_node2 = find_nodes_in_hypergraph(H, v.label, x.label, 0, nodes_table) if hyper_node2 and hyper_node1: hyper_node1 = hyper_node1[0] hyper_node2 = hyper_node2[0] cost_without_losses = hyper_node1[1][ 'cost_without_losses'] + hyper_node2[1][ 'cost_without_losses'] + D_cost cost_with_losses = hyper_node1[1]['cost_with_losses'] + hyper_node2[ 1]['cost_with_losses'] + D_cost heapq.heappush( heap, utiles.heap_items(cost_without_losses, cost_with_losses, hyper_node1, hyper_node2)) return heap
def build_hyper_garph(S, G, k, nodes_table, D_cost, S_cost, loss_cost, HT_cost, sigma, S_dis_matrix, track_solution, res): H = nx.MultiDiGraph() H.clear() H, H_number_of_nodes, nodes_table = inits.init_leafs_efficient( G, H, k, 0, sigma, nodes_table) incomp = inits.init_dict_inf(H, S, G, k, nodes_table, 'incomp', sigma, S_dis_matrix, loss_cost) subtree = inits.init_dict_inf(H, S, G, k, nodes_table, 'subtree', sigma, S_dis_matrix, loss_cost) subtreeLoss = inits.init_dict_inf(H, S, G, k, nodes_table, 'subtreeLoss', sigma, S_dis_matrix, loss_cost) for u in G.postorder_node_iter(): if not tree_operations.is_a_leaf(u): for x in S.postorder_node_iter(): key_counter = 0 S_list = SpeciationEvent_effi(u, x, S_cost, subtreeLoss, k, H, nodes_table) SE = list(map(lambda nd: (nd, 'S'), S_list)) D_list = DuplicationEvent_effi(H, u, x, D_cost, loss_cost, nodes_table, subtreeLoss, k) DE = list(map(lambda nd: (nd, 'D'), D_list)) HT_list = HTEvent_effi(u, x, HT_cost, subtreeLoss, incomp, k, H, nodes_table) HTE = list(map(lambda nd: (nd, 'HT'), HT_list)) Kbest = SE + DE + HTE random.shuffle(Kbest, random.random) heapq.heapify(Kbest) if len(Kbest) > 0: H.add_node(H_number_of_nodes, s=u.label, t=x.label, l=[]) nodes_table[x.label][u.label] = H_number_of_nodes big_node = H_number_of_nodes H_number_of_nodes += 1 for i in range(0, k): if len(Kbest) > 0: match = heapq.heappop(Kbest) new_cost_no_losses = match[0].val[0] new_cost_with_losses = match[0].val[1] event = match[1] match1 = match[0].val[2] match2 = match[0].val[3] if event == 'S': H.nodes[big_node]['l'] = H.nodes[big_node]['l'] + [ { 's': u.label, 't': x.label, 'cost_without_losses': new_cost_no_losses, 'cost_with_losses': new_cost_with_losses, 'event': "S", 'list_place': len(H.nodes[big_node]['l']) } ] elif event == 'D': H.nodes[big_node]['l'] = H.nodes[big_node]['l'] + [ { 's': u.label, 't': x.label, 'cost_without_losses': new_cost_no_losses, 'cost_with_losses': new_cost_with_losses, 'event': "D", 'list_place': len(H.nodes[big_node]['l']) } ] elif event == 'HT': H.nodes[big_node]['l'] = H.nodes[big_node]['l'] + [ { 's': u.label, 't': x.label, 'cost_without_losses': new_cost_no_losses, 'cost_with_losses': new_cost_with_losses, 'event': "HT", 'list_place': len(H.nodes[big_node]['l']) } ] H.add_edge(match1[0], big_node, key=key_counter, source=match1[1]['list_place'], target=len(H.nodes[big_node]['l']) - 1, probability=0) key_counter += 1 H.add_edge(match2[0], big_node, key=key_counter, source=match2[1]['list_place'], target=len(H.nodes[big_node]['l']) - 1, probability=0) key_counter += 1 new_node1 = find_nodes_in_hypergraph( H, match1[1]['s'], match1[1]['t'], match1[1]['list_place'] + 1, nodes_table) new_node2 = find_nodes_in_hypergraph( H, match2[1]['s'], match2[1]['t'], match2[1]['list_place'] + 1, nodes_table) if new_node1 and new_node2: new_node1 = new_node1[0] new_node2 = new_node2[0] if event == 'D': additional_cost = D_cost elif event == 'S': additional_cost = S_cost else: additional_cost = HT_cost cost_with_losses = match1[1][ 'cost_with_losses'] + new_node2[1][ 'cost_with_losses'] + additional_cost cost_without_losses = match1[1][ 'cost_with_losses'] + new_node2[1][ 'cost_without_losses'] + additional_cost new_node12 = (utiles.heap_items( cost_without_losses, cost_with_losses, match1, new_node2), event) new_node21 = (utiles.heap_items( cost_without_losses, cost_with_losses, match2, new_node1), event) if new_node12[0] == new_node21[0]: Kbest.insert(len(Kbest), new_node12) Kbest.insert(len(Kbest), new_node21) else: heapq.heappush(Kbest, tuple(new_node12)) heapq.heappush(Kbest, tuple(new_node21)) if not tree_operations.is_a_leaf(x): y = x.adjacent_nodes()[0] z = x.adjacent_nodes()[1] subtree[u.label][x.label] += utiles.kmin_list( find_nodes_in_hypergraph(H, u.label, x.label, -1, nodes_table), subtree[u.label][y.label], subtree[u.label][z.label], H, nodes_table, 'cost_without_losses') list1 = [ deepcopy(tocopy) for tocopy in subtreeLoss[u.label][y.label] ] list2 = [ deepcopy(tocopy) for tocopy in subtreeLoss[u.label][z.label] ] for lst in (list1, list2): for node in lst: node[1]['cost_with_losses'] += loss_cost #print('u: %s, x: %s\nsubtree[u.label][y.label]:%s\nsubtree[u.label][z.label]:%s\nc(u,x): %s\nsubtreeLoss[u.label][y.label]: %s\nsubtreeLoss[u.label][z.label]: %s\n' % # (str(u.label),str(x.label),str(subtree[u.label][y.label]),str(subtree[u.label][z.label]), # str(find_nodes_in_hypergraph(H,u.label,x.label,-1,nodes_table)),str(subtreeLoss[u.label][y.label]),subtreeLoss[u.label][z.label])) subtreeLoss[u.label][x.label] += utiles.kmin_list( find_nodes_in_hypergraph(H, u.label, x.label, -1, nodes_table), list1, list2, H, nodes_table, 'cost_with_losses') else: subtree[u.label][x.label] += utiles.kmin_list( find_nodes_in_hypergraph(H, u.label, x.label, -1, nodes_table), [], [], H, nodes_table, 'cost_without_losses') subtreeLoss[u.label][x.label] += utiles.kmin_list( find_nodes_in_hypergraph(H, u.label, x.label, -1, nodes_table), [], [], H, nodes_table, 'cost_with_losses') #print('u: %s, x: %s\nsubtree[u.label][y.label]:%s\nsubtree[u.label][z.label]:%s\nc(u,x): %s\nsubtreeLoss[u.label][y.label]: %s\nsubtreeLoss[u.label][z.label]: %s\n' % # (str(u.label),str(x.label),str(subtree[u.label][y.label]),str(subtree[u.label][z.label]), # str(find_nodes_in_hypergraph(H,u.label,x.label,-1,nodes_table)),str(subtreeLoss[u.label][y.label]),subtreeLoss[u.label][z.label])) for x in S.preorder_node_iter(): if not tree_operations.is_a_leaf(x): y = x.adjacent_nodes()[0] z = x.adjacent_nodes()[1] incomp[u.label][y.label] += utiles.kmin_positive( incomp[u.label][x.label] + subtree[u.label][z.label], k, H, nodes_table, 'cost_without_losses') incomp[u.label][z.label] += utiles.kmin_positive( incomp[u.label][x.label] + subtree[u.label][y.label], k, H, nodes_table, 'cost_without_losses') H_root = [ nd for nd in list(H.node(data=True)) if nd[1]['s'] == G.seed_node.label and nd[1]['t'] == S.seed_node.label ] if track_solution: res['solution'] += 'Solution number ' + str( track_solution) + '\n\n' + format_solution( track_a_solution(H_root, H, S, G, nx.DiGraph(), int(track_solution), -1)[0].nodes(data=True)) return H, H_number_of_nodes, nodes_table
def build_hyper_garph(S, G, test, k, nodes_table, D_cost, S_cost, HT_cost, path, alpha, sigma, save_data): #print('Building hypergraph...') H = nx.MultiDiGraph() H.clear() if test: print(" Reading file 'H_edges.txt'...") input = open(path + '/saved_data/H_edges_k=' + str(k) + '.txt', 'r') new_edges = [] for line in input: new_edges.append(eval(line)) new_edges = new_edges[0] print(" Finished reading file 'H_edges.txt'") print(" Reading file 'H_nodes.txt'...") input = open( path + '/saved_data/H_nodes_k=' + str(k) + '_alpha=' + str(alpha) + '.txt', 'r') new_nodes = [] for line in input: new_nodes.append(eval(line)) new_nodes = new_nodes[0] print(" Finished reading file 'H_nodes.txt'.") print(" Reading file 'nodes_table.txt'...") input = open(path + '/saved_data/nodes_table_k=' + str(k) + '.txt', 'r') nodes = {} for line in input: nodes.update(eval(line)) nodes_table = nodes print(" Finished reading file 'nodes_table_k=" + str(k) + ".txt'") H.add_nodes_from(new_nodes) H.add_edges_from(new_edges) H_number_of_nodes = (len(H.nodes())) else: H, H_number_of_nodes, nodes_table = inits.init_leafs( G, H, k, 0, sigma, nodes_table) for u in G.postorder_node_iter(): if (not tree_operations.is_a_leaf(u)): for x in S.postorder_node_iter(): key_counter = 0 SE = list( map(lambda nd: (nd, 'S'), SpeciationEvent(H, u, x, S_cost, nodes_table))) DE = list( map(lambda nd: (nd, 'D'), DuplicationEvent(H, u, x, D_cost, nodes_table))) HTE = list( map(lambda nd: (nd, 'HT'), HTEvent(S, H, u, x, HT_cost, nodes_table))) Kbest = SE + DE + HTE random.shuffle(Kbest, random.random) heapq.heapify(Kbest) if (len(Kbest) > 0): H.add_node(H_number_of_nodes, s=u.label, t=x.label, l=[]) nodes_table[x.label][u.label] = H_number_of_nodes big_node = H_number_of_nodes H_number_of_nodes += 1 for i in range(0, k): if (len(Kbest) > 0): match = heapq.heappop(Kbest) new_cost = match[0].val[0] event = match[1] match1 = match[0].val[1] match2 = match[0].val[2] if (event == 'S'): H.nodes[big_node][ 'l'] = H.nodes[big_node]['l'] + [{ 's': u.label, 't': x.label, 'cost': new_cost, 'event': "S", 'list_place': len(H.nodes[big_node]['l']) }] elif (event == 'D'): H.nodes[big_node][ 'l'] = H.nodes[big_node]['l'] + [{ 's': u.label, 't': x.label, 'cost': new_cost, 'event': "D", 'list_place': len(H.nodes[big_node]['l']) }] else: H.nodes[big_node][ 'l'] = H.nodes[big_node]['l'] + [{ 's': u.label, 't': x.label, 'cost': new_cost, 'event': "HT", 'list_place': len(H.nodes[big_node]['l']) }] H.add_edge(match1[0], big_node, key=key_counter, source=match1[1]['list_place'], target=len(H.nodes[big_node]['l']) - 1, probability=0) key_counter += 1 H.add_edge(match2[0], big_node, key=key_counter, source=match2[1]['list_place'], target=len(H.nodes[big_node]['l']) - 1, probability=0) key_counter += 1 new_node1 = find_nodes_in_hypergraph( H, match1[1]['s'], match1[1]['t'], match1[1]['list_place'] + 1, nodes_table) new_node2 = find_nodes_in_hypergraph( H, match2[1]['s'], match2[1]['t'], match2[1]['list_place'] + 1, nodes_table) if (new_node1 != [] and new_node2 != []): new_node1 = new_node1[0] new_node2 = new_node2[0] if event == 'D': additional_cost = D_cost elif event == 'S': additional_cost = S_cost else: additional_cost = HT_cost new_node12 = (utiles.heap_items( match1[1]['cost'] + new_node2[1]['cost'] + additional_cost, match1, new_node2), event) new_node21 = (utiles.heap_items( match2[1]['cost'] + new_node1[1]['cost'] + additional_cost, match2, new_node1), event) if (new_node12[0] == new_node21[0]): Kbest.insert(len(Kbest), new_node12) Kbest.insert(len(Kbest), new_node21) else: heapq.heappush(Kbest, tuple(new_node12)) heapq.heappush(Kbest, tuple(new_node21)) if save_data: print(' Writing nodes...') file = open(path + '/saved_data/H_nodes_naive.txt', 'w') file.write(str(H.nodes(data=True))) file.close() print(' Finished writing nodes.\n') print(' Writing edges...') file = open(path + '/saved_data/H_edges_k=' + str(k) + '.txt', 'w') file.write(str(H.edges(data=True))) file.close() print(' Finished writing edges.\n') print(' Writing nodes table...') file = open(path + '/saved_data/nodes_table_k=' + str(k) + '.txt', 'w') file.write(str(nodes_table)) file.close() print(' Finished writing nodes table.\n') #print(' No. of nodes: '+str(H_number_of_nodes * k)+' No. on edges: '+str(len(H.edges()))) #print('Finished building hypergraph.\n') return H, H_number_of_nodes, nodes_table
def draw_new_G(G, G_nodes_identified, colors, sigma, new_G): print('Drawing new G...') plt.clf() tree_to_draw = nx.DiGraph() index = 1 for u in G.postorder_node_iter(): tree_to_draw.add_node(index, label=u.label) if not tree_operations.is_a_leaf(u): child = u.child_nodes() i = 0 while i < len(child): if len(child) >= i + 1: tree_to_draw.add_edge( index, list(G.postorder_node_iter()).index(child[i]) + 1) i = i + 1 index += 1 labels1 = nx.get_node_attributes(new_G, 'label') pos1 = graphviz_layout(tree_to_draw, prog='dot') plt.figure(12, figsize=(40, 40)) # size of fig print("G_nodes_identified = %s" % str(G_nodes_identified)) nodes_color = [] nodes_size = [] for nd in new_G.nodes(data=True): if new_G.out_degree(nd[0]) == 0 and not new_G.in_degree(nd[0]) == 0: if colors[sigma[nd[1]['label']]] == 'red': nodes_color.append('red') else: nodes_color.append('black') nodes_size.append(200) elif G_nodes_identified[nd[1]['label']] > 0: nodes_color.append('blue') nodes_size.append(G_nodes_identified[nd[1]['label']] * 350) else: nodes_color.append('white') nodes_size.append(200) for r, l in labels1.items(): for x in G.postorder_node_iter(): if x.taxon != None: if x.label == l: l = l + "\n (" + str(x.taxon) + ")" labels1.update({r: l}) #edges_width = [] edges_color = [] for e in new_G.edges(data=True): if e[2]['weight'][0] == 0 and e[2]['weight'][1] == 0: edges_color.append('grey') #edges_width.append(1) elif e[2]['weight'][0] > e[2]['weight'][1]: #more red HT #edges_width.append(e[2]['weight'][0]*10) edges_color.append('red') else: #more black HT #edges_width.append(e[2]['weight'][1]*10) edges_color.append('black') #edges_width = utile.normlize (edges_width,20) print("len(nodes_size) = %s, len(nodes_color) = %s" % (len(nodes_size), len(nodes_color))) nx.draw(tree_to_draw, pos1, arrows=True, node_size=nodes_size, node_color=nodes_color, edge_color=edges_color, width=1) nx.draw_networkx_labels( tree_to_draw, pos1, labels1, font_size=7, ) #nx.draw_networkx_edges(tree_to_draw, pos1, ) #nx.draw_networkx_edge_labels(tree_to_draw, pos1, font_size=6, labels=lables2) plt.savefig('new_G.png') print('Finished drawing new G.\n')
def draw_G_diffrent_optimal_solutions(marked_nodes, colors, sigma, old_sigma, new_G, G, k, path, both, alpha, labels, TH_compare_subtrees, TH_both, TH_pattern_in_subtree, compare_subtrees, evolutinary_event, pattern, iterations, factor, size): print('Drawing new G...') plt.clf() plt.figure(figsize=(80, 40)) tree_to_draw = nx.DiGraph() index = 1 for u in G.postorder_node_iter(): tree_to_draw.add_node(index, label=u.label) if not tree_operations.is_a_leaf(u): child = u.child_nodes() i = 0 while i < len(child): if len(child) >= i + 1: tree_to_draw.add_edge( index, list(G.postorder_node_iter()).index(child[i]) + 1) i = i + 1 index += 1 labels1 = nx.get_node_attributes(new_G[0], 'label') pos1 = graphviz_layout(tree_to_draw, prog='dot') marked_counter = inits.init_dic(G.nodes(), 0) max_counts = 0 i = 1 for solution in marked_nodes: print('solution number ' + str(i) + ":") for item in solution: marked_node = item[0] scoring = item[1] temp_high_score = 0 for j in range(0, 2): for m in range(0, 2): if temp_high_score < scoring[j][m]: temp_high_score = scoring[j][m] print(' %s with score: %s' % (str(marked_node), str(temp_high_score))) marked_counter[marked_node] += temp_high_score if marked_counter[marked_node] > max_counts: max_counts = marked_counter[marked_node] i += factor for u, counter in marked_counter.items(): if max_counts > 0: marked_counter.update({u: (counter / max_counts) * size}) #print('max_counter = %s, marked_counter = %s' % (str(max_counts),str(marked_counter))) nodes_color = [] nodes_size = [] for nd in new_G[0].nodes(data=True): if new_G[0].out_degree( nd[0]) == 0 and not new_G[0].in_degree(nd[0]) == 0: if colors[sigma[nd[1]['label']]] == 'red': nodes_color.append('red') else: nodes_color.append('grey') nodes_size.append(200) elif marked_counter[nd[1]['label']] > 0: nodes_color.append('blue') nodes_size.append(marked_counter[nd[1]['label']]) else: nodes_color.append('white') nodes_size.append(200) if labels: for r, l in labels1.items(): for x in G.postorder_node_iter(): if x.taxon != None: if x.label == l: l = l + "\n (" + str(x.taxon) + ")" l = l + '\n' + str(old_sigma[x.taxon.label]) labels1.update({r: l}) nx.draw(tree_to_draw, pos1, arrows=True, node_size=nodes_size, node_color=nodes_color, width=1) nx.draw_networkx_labels(tree_to_draw, pos1, labels1, font_size=10) plt.savefig(path + '/figures/G_different_optimal. k=' + str(k) + '_TH_compare_subtrees = ' + str(TH_compare_subtrees) + '_TH_pattern_in_subtree = ' + str(TH_pattern_in_subtree) + "_pattern=" + pattern + "_" + evolutinary_event + "compare_subtrees=" + str(compare_subtrees) + '.png') print('Finished drawing new G.\n')
def draw_S_and_G(S, G, old_sigma, colors, sigma, path, sol, ext, to_color): plt.clf() S_to_draw = nx.DiGraph() G_to_draw = nx.DiGraph() index = 1 nodes_color_S = [] nodes_color_G = [] ##FOR S for u in S.postorder_node_iter(): S_to_draw.add_node(index, label=u.label) if not tree_operations.is_a_leaf(u): child = u.child_nodes() i = 0 while i < len(child): if len(child) >= i + 1: S_to_draw.add_edge( index, list(S.postorder_node_iter()).index(child[i]) + 1) i = i + 1 index += 1 labels_S = nx.get_node_attributes(S_to_draw, 'label') for k, l in labels_S.items(): for x in S.postorder_node_iter(): if x.taxon != None: if x.label == l: l = l + "\n (" + str(x.taxon) + ")" labels_S.update({k: l}) for u in S.postorder_node_iter(): if u.label != None and u.label in colors and to_color: if colors[u.label] == 'red': nodes_color_S.append('red') elif colors[u.label] == 'black': nodes_color_S.append('grey') else: nodes_color_S.append('pink') else: nodes_color_S.append('white') ## FOR G index = 1 for u in G.postorder_node_iter(): G_to_draw.add_node(index, label=u.label) if not tree_operations.is_a_leaf(u): child = u.child_nodes() i = 0 while i < len(child): if len(child) >= i + 1: G_to_draw.add_edge( index, list(G.postorder_node_iter()).index(child[i]) + 1) i = i + 1 index += 1 labels_G = nx.get_node_attributes(G_to_draw, 'label') for k, l in labels_G.items(): for x in G.postorder_node_iter(): if x.taxon != None: if x.label == l and (x.taxon.label in old_sigma): l = l + "\n (" + str(x.taxon) + ")" l = l + '\n' + str(old_sigma[x.taxon.label]) labels_G.update({k: l}) for u in G.postorder_node_iter(): degel = False if sol != None: for int, temp_sol in sol.items(): for p in range(0, len(temp_sol['list_of_couples'])): if (u.label == temp_sol['Marked'] or u.label == temp_sol['list_of_couples'][p][0] or u.label == temp_sol['list_of_couples'][p][1] ) and not degel: nodes_color_G.append('blue') degel = True if not degel and tree_operations.is_a_leaf( u) and u.label in sigma and not tree_operations.isolated( u) and sigma[u.label] in colors and to_color: if colors[sigma[u.label]] == 'red': nodes_color_G.append('red') elif colors[sigma[u.label]] == 'black': nodes_color_G.append('grey') else: nodes_color_G.append('pink') elif not degel: nodes_color_G.append('white') postree_S = graphviz_layout(S_to_draw, prog='dot') postree_G = graphviz_layout(G_to_draw, prog='dot') for k, v in postree_G.items(): # Shift the x values of every node by 10 to the right lst = list(v) lst[0] = lst[0] + 100 postree_G.update({k: tuple(lst)}) fig, axes = plt.subplots(1, 2, figsize=(90, 50)) ax = axes.flatten() ax[0].set_title('Species tree', fontsize=50, rotation='vertical', x=-0.1, y=0.5) ax[1].set_title('Gene tree', fontsize=50, rotation='vertical', x=-0.1, y=0.5) nx.draw(S_to_draw, postree_S, arrows=True, node_color=nodes_color_S, ax=ax[0]) nx.draw(G_to_draw, postree_G, arrows=True, node_color=nodes_color_G, ax=ax[1]) t1 = nx.draw_networkx_labels(S_to_draw, postree_S, labels_S, font_size=7, ax=ax[0]) t2 = nx.draw_networkx_labels(G_to_draw, postree_G, labels_G, font_size=7, ax=ax[1]) for _, t in t1.items(): t.set_rotation('vertical') for _, t in t2.items(): t.set_rotation('vertical') nx.draw_networkx_edges(S_to_draw, postree_S, ax=ax[0]) nx.draw_networkx_edges(G_to_draw, postree_G, ax=ax[1]) to_create = path + '/figures/' os.makedirs(os.path.dirname(to_create), exist_ok=True) plt.savefig(path + '/figures/S+G' + ext + '.png')
def draw_tree(tree, name, old_sigma, colors, sigma, path, color_tree, x_axis, y_axis, label_flag): tree_to_draw = nx.DiGraph() index = 1 nodes_color = [] for u in tree.postorder_node_iter(): tree_to_draw.add_node(index, label=u.label) if not tree_operations.is_a_leaf(u): child = u.child_nodes() i = 0 while i < len(child): if len(child) >= i + 1: tree_to_draw.add_edge( index, list(tree.postorder_node_iter()).index(child[i]) + 1) i = i + 1 index += 1 labels = nx.get_node_attributes(tree_to_draw, 'label') for k, l in labels.items(): for x in tree.postorder_node_iter(): if x.taxon != None: if x.label == l: l = l + "\n (" + str(x.taxon) + ")" if name == 'G': l = l + '\n' + str(old_sigma[x.taxon.label]) labels.update({k: l}) else: labels.update({k: l}) if name == 'S': for u in tree.postorder_node_iter(): if color_tree: if u.label != None and u.label in colors: if colors[u.label] == 'red': nodes_color.append('red') elif colors[u.label] == 'black': nodes_color.append('grey') else: nodes_color.append('pink') else: nodes_color.append('white') else: nodes_color.append('white') else: for u in tree.postorder_node_iter(): if tree_operations.is_a_leaf(u) and not tree_operations.isolated( u) and sigma[u.label] in colors: if colors[sigma[u.label]] == 'red': nodes_color.append('red') else: nodes_color.append('grey') else: nodes_color.append('white') postree = graphviz_layout(tree_to_draw, prog='dot') plt.figure(12, figsize=(x_axis, y_axis)) # size of fig nx.draw(tree_to_draw, postree, arrows=True, node_color=nodes_color) if label_flag: text = nx.draw_networkx_labels(tree_to_draw, postree, labels, font_size=7) for _, t in text.items(): t.set_rotation('vertical') nx.draw_networkx_edges(tree_to_draw, postree) plt.savefig(path + name + '.png') print('Drawing' + name)
def draw_new_doup(marked_nodes, colors, sigma, new_G, G,old_sigma,k,TH_compare_subtrees, path, lables, glob,spec,pattern,size,evol,compare,number_of_fields,S_labels_table): print('Drawing new G...') plt.clf() special_colors = [] should_be_found = number_of_scpecies_doup(G,old_sigma) print() for i in range(0, len(should_be_found)): special_colors.append(hex_code_colors()) tree_to_draw = nx.DiGraph() index = 1 for u in G.postorder_node_iter(): tree_to_draw.add_node(index, label = u.label) if not tree_operations.is_a_leaf(u): child = u.child_nodes() i = 0 while i < len(child): if len(child) >= i + 1: tree_to_draw.add_edge(index, list(G.postorder_node_iter()).index(child[i]) + 1) i = i + 1 index += 1 labels1 = nx.get_node_attributes(new_G, 'label') pos1 = graphviz_layout(tree_to_draw, prog='dot') plt.figure(figsize=(x_axis, y_axis)) nodes_color = [] nodes_size = [] if draw_marked: max_size = max([x[0] for u,x in marked_nodes.items()]) if double_mode and draw_marked: max_size = max([x[1]+x[0] for u,x in marked_nodes.items()]) exp_facor = math.log(size) for nd in new_G.nodes(data=True): flag = False if new_G.out_degree(nd[0]) == 0 and not new_G.in_degree(nd[0]) == 0 and color: temp_name = list(S_labels_table.keys())[list(S_labels_table.values()).index(sigma[nd[1]['label']])] for should in should_be_found: if temp_name.find(should) != -1 and (not flag): nodes_color.append(special_colors[should_be_found.index(should)]) nodes_size.append(200) flag = True if nd[1]['label'] in marked_nodes and (not flag) and draw_marked: nodes_color.append('blue') temp_size = max(marked_nodes[nd[1]['label']][0],marked_nodes[nd[1]['label']][0]) if double_mode: temp_size = marked_nodes[nd[1]['label']][0]+marked_nodes[nd[1]['label']][1] nodes_size.append(math.exp((temp_size/max_size)*exp_facor)+500) elif not flag: nodes_color.append('#FFFFFF') nodes_size.append(50) if lables: for r, l in labels1.items(): for x in G.postorder_node_iter(): if x.taxon != None: if x.label == l: l = l + "\n (" + str(x.taxon) + ")" l = l+'\n'+str(old_sigma[x.taxon.label]) labels1.update({r: l}) nx.draw(tree_to_draw, pos1, arrows=True, node_size=nodes_size, node_color=nodes_color, width=1) if lables_flag: text = nx.draw_networkx_labels(tree_to_draw, pos1, labels1, font_size=7) for _, t in text.items(): t.set_rotation('vertical') plt.savefig(path + '/figures/new_G_pattern=' + pattern + '_' + ext + '.png') print('Finished drawing new G.\n')