def join(args):
    """
    %prog join fastafile [phasefile]

    Make AGP file for a bunch of sequences, and add gaps between, and then build
    the joined fastafile. This is useful by itself, but with --oo option this
    can convert the .oo (BAMBUS output) into AGP and a joined fasta.

    Phasefile is optional, but must contain two columns - BAC and phase (0, 1, 2, 3).
    """
    from jcvi.formats.agp import OO, Phases, build
    from jcvi.formats.sizes import Sizes

    p = OptionParser(join.__doc__)
    p.add_option("--newid", default=None,
            help="New sequence ID [default: `%default`]")
    p.add_option("--gapsize", default=100, type="int",
            help="Number of N's in between the sequences [default: %default]")
    p.add_option("--gaptype", default="contig",
            help="Gap type to use in the AGP file [default: %default]")
    p.add_option("--evidence", default="",
            help="Linkage evidence to report in the AGP file [default: %default]")
    p.add_option("--oo", help="Use .oo file generated by bambus [default: %default]")
    opts, args = p.parse_args(args)

    nargs = len(args)
    if nargs not in (1, 2):
        sys.exit(not p.print_help())

    if nargs == 2:
        fastafile, phasefile = args
        phases = DictFile(phasefile)
        phases = dict((a, Phases[int(b)]) for a, b in phases.items())
    else:
        fastafile, = args
        phases = {}

    sizes = Sizes(fastafile)
    prefix = fastafile.rsplit(".", 1)[0]
    agpfile = prefix + ".agp"
    newid = opts.newid
    oo = opts.oo

    o = OO(oo, sizes.mapping)

    if oo:
        seen = o.contigs
        # The leftover contigs not in the oo file
        logging.debug("A total of {0} contigs ({1} in `{2}`)".\
                    format(len(sizes), len(seen), oo))

        for ctg, size in sizes.iter_sizes():
            if ctg in seen:
                continue
            o.add(ctg, ctg, size)

    else:
        if newid:
            for ctg, size in sizes.iter_sizes():
                o.add(newid, ctg, size)
        else:
            for scaffold_number, (ctg, size) in enumerate(sizes.iter_sizes()):
                object_id = "scaffold{0:03d}".format(scaffold_number + 1)
                o.add(object_id, ctg, size)

    fw = open(agpfile, "w")
    o.write_AGP(fw, gapsize=opts.gapsize, gaptype=opts.gaptype,
                    evidence=opts.evidence, phases=phases)
    fw.close()

    joinedfastafile = prefix + ".joined.fasta"
    build([agpfile, fastafile, joinedfastafile])

    return joinedfastafile
dataset/ETHPy150Open tanghaibao/jcvi/formats/fasta.py/join/original