# print("\t#", scaffold_name) print("#", scaffold_name, file=target) # print("\t", " ".join( # strand + str(gene_name) for strand, gene_name in grimm_formatted_genomes[genome][scaffold_name]), # "$") print(" ".join(strand + str(gene_name) for strand, gene_name in grimm_formatted_genomes[genome][scaffold_name]), "$", file=target) # print() bg_graphs = dict() for file_name in os.listdir(target_directory): file_name = os.path.join(target_directory, file_name) with open(file_name, "r") as source: bg = GRIMMReader.get_breakpoint_graph(source) bg_graphs[file_name.split(".")[0]] = bg bg = BreakpointGraph() for br_gr in bg_graphs.values(): bg.update(br_gr, merge_edges=True) target_multicolor = Multicolor("Anguilla_japonica") print("Breakpoint graph stats:") print( "\t", "non-infinity nodes count:", len( list(node for node in bg.nodes() if not BGVertex.is_infinity_vertex(node)))) normal_edges, infinity_edges = [], [] for edge in bg.edges():
################################################################################################################ # # END OF experiment set up (data) # ################################################################################################################ if __name__ == "__main__": length = sys.argv[1] identity = sys.argv[2] for i in range(0, len(TARGET_ORGANISM_NAMES)): GRIMM_FILES.append(PATH + length + "_" + identity + "/" + TARGET_ORGANISM_NAMES[i] + ".txt") print("Reading data into breakpoint graph...") graph = BreakpointGraph() for file in GRIMM_FILES: with open(file, "rt") as source: graph.update(GRIMMReader.get_breakpoint_graph(source), merge_edges=True) print("Getting a tree...") bgtree = NewickReader.from_string(NEWICK_STRING_TREE) print("Preparing organisms for assembling...") target_organisms = [BGGenome(organism) for organism in TARGET_ORGANISM_NAMES] exclude = [BGGenome(organism) for organism in COMPLETE_ORGANISM_NAMES] print("Staring the assembly process...") result = assemble_scaffolds(graph=graph, bgtree=bgtree, target_organisms=target_organisms, exclude=exclude, verbose=True) print("Finished assembling!") print("Were identified", sum([len(multicolor.colors) for v1, v2, weight, repeat_name, multicolor in result]), "assembly points")
def get_html_report_experiment_entry(experiment): try: module_path, file_name = os.path.split(experiment.config_file_path) if module_path not in sys.path: sys.path.insert(0, module_path) module_name = file_name[:file_name.rfind(".")] if module_name in sys.modules: del sys.modules[module_name] module = importlib.import_module(module_name) config = module.configuration except Exception: raise config = recursive_dict_update(default_configuration.configuration, config) result = [] result.append("<hr>") result.append("<hr>") result.append("<h1>{experiment_name}</h1>".format(experiment_name=config.get("experiment_name", "Experiment XXX"))) reference_chr_fragments_order_full_path = os.path.join(experiment.evaluation_dir_path, "chr_fragments_order.txt") assembled_chains_full_path = os.path.join(config["gos-asm"]["output"]["dir"], config["gos-asm"]["output"]["chains_file"]) assembly_points_file_full_path = os.path.join(config["gos-asm"]["output"]["dir"], config["gos-asm"]["output"]["assembly_points_file"]) assembly_point_evaluation = [] with open(assembly_points_file_full_path, newline="") as csv_file: reader = csv.reader(csv_file, delimiter="|") headers = next(reader) headers = [header.strip() for header in headers] for row in reader: entry = {key: value for key, value in zip(headers, row)} ap = AssemblyPoint.from_assembly_points_file(separated_values=entry) assembly_point_evaluation.append(AssemblyPointEvaluation(ap=ap)) genomes_ref_chr_fragments_orders = defaultdict(list) current_genome = None with open(reference_chr_fragments_order_full_path, "rt") as source: for line in source: if len(line.strip()) == 0 or line.strip().startswith("#"): continue elif line.strip().startswith(">"): current_genome = BGGenome(line.strip()[1:]) genomes_ref_chr_fragments_orders[current_genome].append(line.strip()) elif current_genome is not None: genomes_ref_chr_fragments_orders[current_genome].append(line.strip()) assembled_genome_fragments_orders = defaultdict(list) current_genome = None with open(assembled_chains_full_path, "rt") as source: for line in source: if len(line.strip()) == 0 or line.strip().startswith("#"): continue elif line.strip().startswith(">"): current_genome = BGGenome(line.strip()[1:]) assembled_genome_fragments_orders[current_genome].append(line.strip()) elif current_genome is not None: assembled_genome_fragments_orders[current_genome].append(line.strip()) bg = BreakpointGraph() for file_path in [reference_chr_fragments_order_full_path, assembled_chains_full_path]: with open(file_path, "rt") as source: bg.update(GRIMMReader.get_breakpoint_graph(source, merge_edges=False)) scjs = {} genomes_fragmentation_total = {} target_genomes = [BGGenome(genome_name) for genome_name in config["gos-asm"]["input"]["target_organisms"]] for genome in target_genomes: assembled_chains = assembled_genome_fragments_orders[genome] reference_chains = genomes_ref_chr_fragments_orders[genome] source = assembled_chains + reference_chains bg = GRIMMReader.get_breakpoint_graph(source, merge_edges=False) scjs[genome] = single_cut_and_join_distance(bg) reference_assembly_graph = {} current_genome = None with open(reference_chr_fragments_order_full_path, "rt") as source: for line in source: if len(line.strip()) == 0 or line.strip().startswith("#"): continue elif line.strip().startswith(">"): current_genome = BGGenome(line.strip()[1:]) if current_genome not in reference_assembly_graph: reference_assembly_graph[current_genome] = Graph() genomes_fragmentation_total[current_genome] = 0 elif current_genome is not None: fragments = line.strip().split() *fragments, chr_type = fragments genomes_fragmentation_total[current_genome] += len(fragments) - 1 for fragment1, fragment2 in zip(fragments[:-1], fragments[1:]): v1, v2 = get_pair_of_fragments_vertices(fragment1, fragment2) reference_assembly_graph[current_genome].add_edge(v1, v2, attr_dict={"type": "HC"}) if chr_type == "@": v1, v2 = get_pair_of_fragments_vertices(fragments[-1], fragments[0]) reference_assembly_graph[current_genome].add_edge(v1, v2, attr_dict={"type": "HC"}) for cnt, fragment in enumerate(fragments): closure_fragments = get_closure_fragments(fragments, cnt, chr_type) for fr in closure_fragments: v1, v2 = get_pair_of_fragments_vertices(fragment, fr) if not reference_assembly_graph[current_genome].has_edge(v1, v2): reference_assembly_graph[current_genome].add_edge(v1, v2, attr_dict={"type": "GOC"}) for ap_eval in assembly_point_evaluation: fragment1 = ap_eval.ap.fragment1 fragment2 = ap_eval.ap.fragment2 fragment1_sign = ap_eval.ap.fragment1_sign fragment2_sign = ap_eval.ap.fragment2_sign fr1_suffix = "h" if fragment1_sign == "+" else "t" fr2_suffix = "t" if fragment2_sign == "+" else "h" v1, v2 = fragment1 + fr1_suffix, fragment2 + fr2_suffix graph = reference_assembly_graph[ap_eval.ap.info.target_color.colors.pop()] if graph.has_edge(v1, v2): type_ = graph[v1][v2]["type"] if type_ == "HC": ap_eval.HC = True ap_eval.GOC = True elif type_ == "GOC": ap_eval.GOC = True exp_id = config.get("experiment_name", "Experiment XXX") exp_id = "_".join(exp_id.split()) result.append("<h2>Experiment info</h2>") result.append("<div class='well'>") result.append("<p>" + config.get("experiment_info", "No info").replace("\n", "<br>") + "</p>") result.append("</div>") result.append("<h3>Evaluation</h3>") result.append("<h4>Per target genome</h4>") for genome in target_genomes: g_suffix = exp_id + "_" + genome.name genome_ap = [ap_eval for ap_eval in assembly_point_evaluation if ap_eval.ap.info.target_color.colors.pop().name == genome.name] add_collapse_header(heading=genome.name, suffix=g_suffix, result=result) result.append("<div class='container' style='width:100%;'>") add_collapse_header(heading="Assembly points", suffix=g_suffix + "_assembly_point", result=result, parent_data="collapse_{suffix}".format(suffix=g_suffix)) add_ap_table(genome_ap, result) add_collapse_footer(result=result) result.append("</div>") result.append("<p>SCJ distance between produced assembly and a reference one: <b>{scj_distance}</b></p>".format(scj_distance=scjs[genome])) result.append("<ul>") total_cnt = len(genome_ap) result.append("<li><p>Total # of identified assembly points: <b>{ap_cnt}</b></p></li>".format(ap_cnt=total_cnt)) hc_cnt = len([ap for ap in genome_ap if ap.HC]) result.append("<li><p>Correct assembly points: <b>{HC_cnt}</b></p></li>".format(HC_cnt=hc_cnt)) goc_cnt = len([ap for ap in genome_ap if ap.GOC and not ap.HC]) result.append( "<li><p>Correct from Global Gene Order (GOC) perspective assembly points: <b>{GOC_cnt}</b></p></li>".format(GOC_cnt=goc_cnt)) incorrect_cnt = len([ap for ap in genome_ap if not ap.HC and not ap.GOC]) result.append("<li><p>Incorrect assembly points: <b>{ic_cnt}</b></p></li>".format(ic_cnt=incorrect_cnt)) result.append("</ul>") # result.append("</div>") result.append("<h5>Relative portions of identified assembly points</h5>") result.append("<div class='progress'>") for value, style in zip([hc_cnt, goc_cnt, incorrect_cnt], ["success", "info", "danger"]): value_prs = value * 100 / total_cnt result.append( '<div class="progress-bar progress-bar-{style}" role="progressbar" aria-valuenow="{value}" aria-valuemin="0" aria-valuemax="100" style="width: {value}%">' ''.format(style=style, value=value_prs)) result.append("<span>{value:.2f}%</span>".format(value=value_prs, style=style)) result.append("</div>") result.append("</div>") overall_cnt = genomes_fragmentation_total[genome] result.append("<h5>Absolute portions of identified assembly points</h5>") result.append("<div class='progress'>") for value, style in zip([hc_cnt, goc_cnt, incorrect_cnt], ["success", "info", "danger"]): value_prs = value * 100 / overall_cnt result.append( '<div class="progress-bar progress-bar-{style}" role="progressbar" aria-valuenow="{value}" aria-valuemin="0" aria-valuemax="100" style="width: {value}%">' ''.format(style=style, value=value_prs)) result.append("<span>{value:.2f}%</span>".format(value=value_prs, style=style)) result.append("</div>") add_collapse_footer(result=result) result.append("<h4>Overall results</h4>") add_collapse_header(heading="Assembly_points", suffix=exp_id, result=result) add_ap_table(assembly_point_evaluation, result) add_collapse_footer(result=result) # result.append("<div class='row'>") result.append("<ul>") total_cnt = len(assembly_point_evaluation) result.append("<li><p>Total # of identified assembly points: <b>{ap_cnt}</b></p></li>".format(ap_cnt=total_cnt)) hc_cnt = len([ap for ap in assembly_point_evaluation if ap.HC]) result.append("<li><p>Correct assembly points: <b>{HC_cnt}</b></p></li>".format(HC_cnt=hc_cnt)) goc_cnt = len([ap for ap in assembly_point_evaluation if ap.GOC and not ap.HC]) result.append( "<li><p>Correct from Global Gene Order (GOC) perspective assembly points: <b>{GOC_cnt}</b></p></li>".format(GOC_cnt=goc_cnt)) incorrect_cnt = len([ap for ap in assembly_point_evaluation if not ap.HC and not ap.GOC]) result.append("<li><p>Incorrect assembly points: <b>{ic_cnt}</b></p></li>".format(ic_cnt=incorrect_cnt)) result.append("</ul>") result.append("<h5>Relative portions of identified assembly points</h5>") result.append("<div class='progress'>") for value, style in zip([hc_cnt, goc_cnt, incorrect_cnt], ["success", "info", "danger"]): value_prs = value * 100 / total_cnt result.append( '<div class="progress-bar progress-bar-{style}" role="progressbar" aria-valuenow="{value}" aria-valuemin="0" aria-valuemax="100" style="width: {value}%">' ''.format(style=style, value=value_prs)) result.append("<span>{value:.2f}%</span>".format(value=value_prs, style=style)) result.append("</div>") result.append("</div>") overall_cnt = sum([value for key, value in genomes_fragmentation_total.items() if key in target_genomes]) result.append("<h5>Absolute portions of identified assembly points</h5>") result.append("<div class='progress'>") for value, style in zip([hc_cnt, goc_cnt, incorrect_cnt], ["success", "info", "danger"]): value_prs = value * 100 / overall_cnt result.append( '<div class="progress-bar progress-bar-{style}" role="progressbar" aria-valuenow="{value}" aria-valuemin="0" aria-valuemax="100" style="width: {value}%">' ''.format(style=style, value=value_prs)) result.append("<span>{value:.2f}%</span>".format(value=value_prs, style=style)) result.append("</div>") result.append("</div>") return "\n".join(result)