Beispiel #1
0
    def generate_output(self):
        """
        Majority of Cotranscriptional SHAPE-Seq output is created here. This includes the DG plot, best structure images, and the movie of the best structures.
        """
        draw_dir = OSU.create_directory(self.output_dir + "/draw/")
        nn_dir = OSU.create_directory(self.output_dir + "/nn/")
        OSU.create_directory(nn_dir + "distances/")
        sorted_lengths = sorted(self.file_data)
        zero_padding = int(math.floor(math.log10(sorted_lengths[-1])) + 1)

        # Parallelized function calls to generate DG plot, distance matrices for clustering of structures, and creating images of minimum distance structures
        draw_struct_nums = [
            length for length in sorted_lengths
            if self.file_data[length]["rc_flag"]
        ]
        draw_args_pool = zip([self.file_data[dsn] for dsn in draw_struct_nums],
                             draw_struct_nums, repeat(sorted_lengths[-1]),
                             range(1,
                                   len(draw_struct_nums) + 1),
                             repeat(zero_padding), repeat(draw_dir),
                             repeat(self.output_dir + "/ct/"),
                             repeat(self.draw_all),
                             repeat(self.most_count_tie_break))
        args_pool = [
            (PCSU.generate_DG_output, (self, 1, sorted_lengths[-1]))
        ] + zip(repeat(PCSU.generate_best_struct_images), draw_args_pool)

        if self.p == 1:
            for i in range(len(args_pool)):
                PCSU.calculate_function_helper(args_pool[i])
        else:
            PCSU.generate_DG_output(
                self, 1, sorted_lengths[-1]
            )  # moved this outside of multiprocessing because Quest has issues running it in a pool
            PCSU.run_output_multiprocessing_pool(
                PCSU.calculate_function_helper, args_pool[1:], self.p)

        if not OSU.check_file_exists(
                self.output_dir + "/DG_state_plot.pdf"
        ):  # Weird error on quest that it will ignore this command if sample size is very large
            PCSU.generate_DG_output(self, 1, sorted_lengths[-1])

        # Use ffmpeg on varna_num.png's to create video of the minimum distance folding pathway
        OSU.make_symbolic_link(
            draw_dir + str(len(draw_struct_nums)).zfill(zero_padding) +
            "_structure.png",
            draw_dir + str(len(draw_struct_nums) + 1).zfill(zero_padding) +
            "_structure.png")  # ffmpeg needs a duplicate of the last frame
        VIU.generate_movie(draw_dir + "%%%dd_structure.png" % (zero_padding),
                           self.output_dir + "/movie.mp4")
        return
Beispiel #2
0
def generate_best_struct_images(cotrans_length, length, longest_length,
                                varna_num, zero_padding, draw_dir, ct_dir,
                                draw_all, most_count_tie_break):
    """
    Generates images of the minimum distance structures at a given length
    """
    print "generate_best_struct_images: " + str(length)
    fname = cotrans_length["filename"]
    rho_varna = "\"" + ";".join([str(r) for r in cotrans_length["rho"]]) + "\""

    seen_snum = []
    mult_images = []
    if cotrans_length["rc_flag"]:  # plot only structures with a good RC
        for snum in cotrans_length["min_dist_indices"]:
            seen_snum.append(snum)
    for sf in range(len(seen_snum)):
        draw_outname_pre = "%s/%s_%s_%s" % (draw_dir, fname, seen_snum[sf],
                                            str(varna_num).zfill(zero_padding))
        if len(seen_snum) > 1 and draw_all:
            draw_outname_pre += "_mult" + str(sf)
            mult_images.append(draw_outname_pre + "_structure.png")
        elif len(seen_snum) > 1 and not draw_all:
            # draw only the structure with the most supporting counts from the Boltzman samples
            structs_str = [",".join(s) for s in cotrans_length["structs"]]
            if most_count_tie_break:
                tie_break = max(
                    dict((k, v) for k, v in
                         cotrans_length["sampled_structs_count"].iteritems()
                         if structs_str.index(
                             k) in cotrans_length["min_dist_indices"]))
                tie_break_i = structs_str.index(tie_break)
            else:
                tie_break = min([
                    cotrans_length["free_energies"][sn]
                    for sn in cotrans_length["min_dist_indices"]
                ])
                tie_break_i = [
                    cotrans_length["free_energies"][sn]
                    for sn in cotrans_length["min_dist_indices"]
                ].index(tie_break)
                tie_break_i = cotrans_length["min_dist_indices"][tie_break_i]
            if tie_break_i != seen_snum[sf]:
                continue
        rho_varna = rho_varna[:-1] + ";".join([""] + ["-1"] *
                                              (longest_length - length)) + "\""
        SU.run_ct2dot(ct_dir + fname + "_unique.ct", seen_snum[sf],
                      draw_outname_pre + ".dbn")

        # determine length of .'s needed to fill in the whole length
        OSU.system_command(
            "sed '$s/$/&%s/' %s.dbn > %s%s_temp.dbn " %
            ("." *
             (longest_length - length), draw_outname_pre, draw_dir, varna_num))
        VIU.run_VARNA(draw_dir + str(varna_num) + "_temp.dbn",
                      draw_outname_pre + "_structure.png",
                      rho_varna)  # same fix this as above
        if sf == len(seen_snum) - 1 and len(
                seen_snum) > 1 and draw_all:  # vertical concat mult images
            v_outname = re.sub("_mult\d+", "", mult_images[0])
            VIU.vertical_image_concat(v_outname, mult_images)
            draw_outname_pre = re.findall("(.*)_structure.png$", v_outname)[0]
        if sf == len(seen_snum) - 1 or not draw_all:
            print draw_dir + str(varna_num).zfill(
                zero_padding) + "_structure.png"
            print "SYM LINK: " + draw_dir + str(varna_num).zfill(
                zero_padding) + "_structure.png"
            OSU.make_symbolic_link(
                re.sub("_mult\d+", "", draw_outname_pre) + "_structure.png",
                draw_dir + str(varna_num).zfill(zero_padding) +
                "_structure.png")
            VIU.convert_center_resize(
                draw_dir + str(varna_num).zfill(zero_padding) +
                "_structure.png", "1200x2800")
        OSU.remove_file(draw_dir + str(varna_num) + "_temp.dbn")
    return draw_dir + str(varna_num).zfill(zero_padding) + "_structure.png"