def LDT_editing(IG, n=1, deletion=False, insertion=False): IG._count_not_ldt += 1 triples_edited_G, _ = IG.triples_editing(n=n, deletion=deletion, insertion=insertion) is_properly_colored = True if (deletion ^ insertion): # only insert or delete so we are sure to make G consistent. isConsistent = True else: isConsistent = is_compatible(triples_edited_G) isCograph = is_cograph(triples_edited_G) if not isCograph: cograph_edited_G = IG.cograph_editing(G=triples_edited_G) else: IG._count_ldtEdit_success += 1 edges_remaining = len(triples_edited_G.edges()) edit_dist = gt.symmetric_diff(IG._G_perturbed, triples_edited_G) return triples_edited_G, True, edges_remaining, edit_dist color_graph(IG._G, cograph_edited_G) properClrd_cograph = make_properly_colored(cograph_edited_G) isCograph = is_cograph(properClrd_cograph) isConsistent = is_compatible(properClrd_cograph) if isConsistent and isCograph: IG._count_ldtEdit_success += 1 edges_remaining = len(properClrd_cograph.edges()) edit_dist = gt.symmetric_diff(IG._G_perturbed, properClrd_cograph) return properClrd_cograph, True, edges_remaining, edit_dist return None, False, None, None
def triples_editing(IG, a): if not IG._is_compatible: IG._count_dG_not_consistent += 1 if IG._is_cograph: IG._count_dG_cograph_notConsistent += 1 else: IG._count_dG_notCograph_notConsistent += 1 if a == 1: edited_G, _ = IG.triples_editing() elif a == 2: edited_G, _ = IG.triples_editing3(n=10) elif a == 3: edited_G, _ = IG.triples_editing(destroy_K3=1) elif a == 4: edited_G, _ = IG.triples_editing2() if edited_G == None: edited_G_is_compatible = True edited_G_is_cograph = IG._is_cograph edited_G_is_properly_colored = True else: edited_G_is_compatible = is_compatible(edited_G, IG._G) edited_G_is_cograph = is_cograph(edited_G) edited_G_is_properly_colored = is_properly_colored(edited_G, IG._G) if edited_G_is_properly_colored: IG._count_triplesEdit_remain_properly_colored += 1 if edited_G_is_compatible: IG._count_triplesEdit_success += 1 if edited_G_is_cograph and IG._is_cograph: IG._count_triplesEdit_remained_cograph += 1 elif edited_G_is_cograph and not IG._is_cograph: IG._count_triplesEdit_fixed_cograph += 1 elif not edited_G_is_cograph and IG._is_cograph: IG._count_triplesEdit_broke_cograph += 1 if edited_G_is_cograph and edited_G_is_compatible and edited_G_is_properly_colored: IG._count_triplesEdit_to_LDT += 1 number_edges_remaining = len(edited_G.edges()) edit_dist = gt.symmetric_diff(IG._G_perturbed, edited_G) return edited_G, True, number_edges_remaining, edit_dist return None, False, None, None
def generate_solutions_unique_species(n, i_p=0.5, d_p=0.5): done = False count = 0 while not done: S = te.simulate_species_tree(10, model='innovation') TGT = te.simulate_dated_gene_tree(S, dupl_rate=0.5, loss_rate=0.5, hgt_rate=0.5, prohibit_extinction="per_family", replace_prob=0.0) OGT = te.observable_tree(TGT) ldt = ldt_graph(OGT, S) if len(ldt.nodes()) == n: IG = InvestigateGraph(ldt) IG.perturb_graph(i_p, d_p) solver = LDTEditor(IG._G_perturbed) solver.build_model() solver.optimize(time_limit=None) sol_graph, sol_distance = solver.get_solution() properly_colored = is_properly_colored(sol_graph) cograph = is_cograph(sol_graph) compatible = is_compatible(sol_graph) edit_dist = gt.symmetric_diff(IG._G_perturbed, sol_graph) print("Runtime: {}".format(solver.get_solve_time())) if properly_colored and cograph and compatible: print("Saving data...") solver._save_ILP_data( IG._G_perturbed, sol_graph, solver.get_solve_time(), edit_dist, only_add=False, only_delete=False, filename="{}nodes/LDTEdit_exact_solution".format(n)) else: print("No solution found!") count += 1 if count == 100: done = True
def triples_editing(IG, n=1, deletion=False, insertion=False): if not IG._is_compatible: IG._count_dG_not_consistent += 1 if IG._is_cograph: IG._count_dG_cograph_notConsistent += 1 else: IG._count_dG_notCograph_notConsistent += 1 edited_G, triples = IG.triples_editing(n=n, deletion=deletion, insertion=insertion) if triples == None: edited_G_is_compatible = True edited_G_is_cograph = is_cograph(edited_G) edited_G_is_properly_colored = is_properly_colored(edited_G, IG._G) else: edited_G_is_compatible = is_compatible(edited_G, IG._G) edited_G_is_cograph = is_cograph(edited_G) edited_G_is_properly_colored = is_properly_colored(edited_G, IG._G) if edited_G_is_properly_colored: IG._count_triplesEdit_remain_properly_colored += 1 if edited_G_is_compatible: IG._count_triplesEdit_success += 1 if edited_G_is_cograph and IG._is_cograph: IG._count_triplesEdit_remained_cograph += 1 elif edited_G_is_cograph and not IG._is_cograph: IG._count_triplesEdit_fixed_cograph += 1 elif not edited_G_is_cograph and IG._is_cograph: IG._count_triplesEdit_broke_cograph += 1 if edited_G_is_cograph and edited_G_is_compatible and edited_G_is_properly_colored: IG._count_triplesEdit_to_LDT += 1 number_edges_remaining = len(edited_G.edges()) edit_dist = gt.symmetric_diff(IG._G_perturbed, edited_G) return edited_G, True, number_edges_remaining, edit_dist return None, False, None, None
def cograph_editing(IG): if not IG._is_cograph: IG._count_dG_not_cograph += 1 if not IG._is_compatible: # dont need to count this here since its being counted in triples edit and the same IG is used for cograph and triples editing #IG._count_dG_notCograph_notConsistent += 1 pass else: IG._count_dG_notCograph_consistent += 1 # check that the edited graph is a cograph, properly colored and has a set of triples that is compatible. edited_G = IG.cograph_editing() edited_G_is_cograph = is_cograph(edited_G) edited_G_is_properly_colored = is_properly_colored(edited_G, IG._G) edited_G_is_compatible = is_compatible(edited_G, IG._G) if edited_G_is_cograph: IG._count_cographEdit_success += 1 if edited_G_is_properly_colored: IG._count_cographEdit_remain_properly_colored += 1 if edited_G_is_compatible and IG._is_compatible: IG._count_cographEdit_remained_consistent += 1 elif edited_G_is_compatible and not IG._is_compatible: IG._count_cographEdit_fixed_consistency += 1 elif not edited_G_is_compatible and IG._is_compatible: IG._count_cographEdit_broke_consistency += 1 if edited_G_is_compatible and edited_G_is_properly_colored: IG._count_cographEdit_to_LDT += 1 number_edges_remaining = len(edited_G.edges()) edit_dist = gt.symmetric_diff(IG._G_perturbed, edited_G) return edited_G, True, number_edges_remaining, edit_dist return None, False, None, None
def __init__(self, G, only_add=False, only_delete=False): self.graph = G self.color_dict = gt.sort_by_colors(G) self._only_add = only_add self._only_delete = only_delete
}), (4, { "color": 0 })] G_edges = [(0, 1), (1, 2), (2, 3), (2, 4)] G.add_nodes_from(G_nodes) G.add_edges_from(G_edges) solver = LDTEditor(G) solver.build_model() solver.optimize(time_limit=None) sol_graph, sol_distance = solver.get_solution() # sol_distance will be 2*symmetric_diff if using obj func for directed graph since we're counting xy edges and yx edges in the objective function. the graph is undirected so only xy or yx is needed edit_dist = gt.symmetric_diff(G, sol_graph) print( "------------------------------------------------------------------------------------------------------------------------" ) print("The symmetric difference between G and G* is: {}".format(edit_dist)) print("The value for the objective function is: {}".format(sol_distance)) #print("------------------------------------------------------------------------------------------------------------------------") #print("The edges of the solution graph: {}".format(sol_graph.edges())) print( "------------------------------------------------------------------------------------------------------------------------" ) print("Runtime: {}".format(solver.get_solve_time())) print("Saving data...") solver._save_ILP_data(G, sol_graph, solver.get_solve_time(),
from asymmetree.hgt import ldt_graph from tools.LDT_ILP import LDTEditor import asymmetree.tools.GraphTools as gt import os S = te.simulate_species_tree(20, model='innovation') TGT = te.simulate_dated_gene_tree(S, dupl_rate=0.5, loss_rate=0.5, hgt_rate=0.5, prohibit_extinction="per_family", replace_prob=0.0) OGT = te.observable_tree(TGT) ldt = ldt_graph(OGT, S) colors = gt.sort_by_colors(ldt) #print("edges of G: \n{}".format(G._G.edges())) #a, b, c = get_P3_data(G._G) #print("\nThe regions of P3s: {}".format(a)) #print("\nThe amounts in the regions: {}".format(b)) #print("\nThe distance between regions: {}\n".format(c)) print("Amount of nodes: {}".format(len(ldt.nodes()))) print("Amount of colors: {}".format(len(colors))) print("Amount of edges: {}".format(len(ldt.edges()))) def run_investigation(): successes = 0 tested_graphs = 0