Exemple #1
0
    def run(self, manager):
        output_dir = manager.configuration["gos-asm"]["output"]["dir"]

        output_chains = manager.configuration["gos-asm"]["output"]["output_chains"]
        chains_file_name = manager.configuration["gos-asm"]["output"]["chains_file"]
        full_chains_file_path = os.path.join(output_dir, chains_file_name)

        output_filtered_chains = manager.configuration["gos-asm"]["output"]["output_filtered_chains"]
        filtered_chains_file = manager.configuration["gos-asm"]["output"]["filtered_chains_file"]
        full_filtered_chains_file = os.path.join(output_dir, filtered_chains_file)

        assembly_points_file_name = manager.configuration["gos-asm"]["output"]["assembly_points_file"]
        full_assembly_points_file_path = os.path.join(output_dir, assembly_points_file_name)

        manager.logger.info("Writing information about {ap_cnt} identified assembly points into {file_name}"
                            "".format(file_name=full_assembly_points_file_path,
                                      ap_cnt=len(manager.data["gos-asm"]["assembly_points"])))
        with open(full_assembly_points_file_path, "wt") as assembly_points_destination:
            print(AssemblyPoint.assembly_file_header_string(), file=assembly_points_destination)
            for ap in manager.data["gos-asm"]["assembly_points"]:
                print(ap.as_assembly_points_file_entry_string(), file=assembly_points_destination)

        if output_chains:
            manager.logger.info("Writing information about fragments in targeted for assembly genomes into file {file_name}"
                                "".format(file_name=full_chains_file_path))
            bg = manager.data["gos-asm"]["bg"]
            with open(full_chains_file_path, "wt") as chains_destination:
                for genome in bg.get_overall_set_of_colors():
                    if genome.name not in manager.configuration["gos-asm"]["input"]["target_organisms"]:
                        continue
                    genome_graph = bg.get_genome_graph(color=genome)
                    fragments_orders = genome_graph.get_fragments_orders()
                    fragments_orders = fragments_orders[genome]
                    if len(fragments_orders) > 0 and any(map(lambda entry: len(entry[1]) > 0, fragments_orders)):
                        print(">{genome_name}".format(genome_name=genome.name), file=chains_destination)
                    for chr_type, fragments_order in fragments_orders:
                        string = " ".join(value if sign == "+" else (sign + value) for sign, value in fragments_order)
                        string += " {chr_type}".format(chr_type=chr_type)
                        print(string, file=chains_destination)

        if output_filtered_chains:
            manager.logger.info("Writing information about \"chained\" fragments in targeted for assembly genomes into file {file_name}"
                                "".format(file_name=full_chains_file_path))
            bg = manager.data["gos-asm"]["bg"]
            with open(full_filtered_chains_file, "wt") as filtered_chains_destination:
                for genome in bg.get_overall_set_of_colors():
                    if genome.name not in manager.configuration["gos-asm"]["input"]["target_organisms"]:
                        continue
                    genome_graph = bg.get_genome_graph(color=genome)
                    fragments_orders = genome_graph.get_fragments_orders()
                    fragments_orders = fragments_orders[genome]
                    if len(fragments_orders) > 0 and any(map(lambda entry: len(entry[1]) > 1, fragments_orders)):
                        print(">{genome_name}".format(genome_name=genome.name), file=filtered_chains_destination)
                    for chr_type, fragments_order in fragments_orders:
                        if len(fragments_order) == 1:
                            continue
                        string = " ".join(value if sign == "+" else (sign + value) for sign, value in fragments_order)
                        string += " {chr_type}".format(chr_type=chr_type)
                        print(string, file=filtered_chains_destination)
Exemple #2
0
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)