def animation(args):
    p = OptionParser(animation.__doc__)
    p.add_option("--gapsize", default=100, type="int",
                 help="Insert gaps of size between scaffolds")
    add_allmaps_plot_options(p)
    opts, args = p.parse_args(args)

    if len(args) != 3:
        sys.exit(not p.print_help())

    inputbed, scaffoldsfasta, seqid = args
    gapsize = opts.gapsize
    pf = inputbed.rsplit(".", 1)[0]
    agpfile = pf + ".chr.agp"
    tourfile = pf + ".tour"

    fp = open(tourfile)
    sizes = Sizes(scaffoldsfasta).mapping
    ffmpeg = "ffmpeg"
    mkdir(ffmpeg)
    score = cur_score = None
    i = 1
    for header, block in read_block(fp, ">"):
        s, tag, label = header[1:].split()
        if s != seqid:
            continue
        tour = block[0].split()
        tour = [(x[:-1], x[-1]) for x in tour]
        if label.startswith("GA"):
            cur_score = label.split("-")[-1]
            if cur_score == score:
                i += 1
                continue
            score = cur_score

        image_name = ".".join((seqid, "{0:04d}".format(i), label, "pdf"))
        if need_update(tourfile, image_name):
            fwagp = must_open(agpfile, "w")
            order_to_agp(seqid, tour, sizes, fwagp, gapsize=gapsize,
                         gaptype="map")
            fwagp.close()
            logging.debug("{0} written to `{1}`".format(header, agpfile))
            build([inputbed, scaffoldsfasta, "--cleanup"])
            pdf_name = plot([inputbed, seqid, "--title={0}".format(label)])
            sh("mv {0} {1}".format(pdf_name, image_name))
        if label in ("INIT", "FLIP", "TSP", "FINAL"):
            for j in xrange(5):  # Delay for 5 frames
                image_delay = image_name.rsplit(".", 1)[0] + \
                              ".d{0}.pdf".format(j)
                sh("cp {0} {1}/{2}".format(image_name, ffmpeg, image_delay))
        else:
            sh("cp {0} {1}/".format(image_name, ffmpeg))
        i += 1

    # Make the movie
    os.chdir(ffmpeg)
    cmd = "parallel convert -density 300 {} {.}.png ::: *.pdf"
    sh(cmd)
    cmd = "ffmpeg -framerate 1 -pattern_type glob -i '*.png' {0}.mp4".format(pf)
    sh(cmd)
%prog animation input.bed scaffolds.fasta chr1
dataset/ETHPy150Open tanghaibao/jcvi/assembly/allmaps.py/animation