def path(args):
    oargs = args
    p = OptionParser(path.__doc__)
    p.add_option("-b", "--bedfile", help=SUPPRESS_HELP)
    p.add_option("-s", "--fastafile", help=SUPPRESS_HELP)
    p.add_option("-w", "--weightsfile", default="weights.txt",
                 help="Use weights from file")
    p.add_option("--compress", default=1e-6, type="float",
                 help="Compress markers with distance <=")
    p.add_option("--noremoveoutliers", default=False, action="store_true",
                 help="Don't remove outlier markers")
    p.add_option("--distance", default="rank", choices=distance_choices,
                 help="Distance function when building initial consensus")
    p.add_option("--linkage", default="double", choices=linkage_choices,
                 help="Linkage function when building initial consensus")
    p.add_option("--gapsize", default=100, type="int",
                 help="Insert gaps of size between scaffolds")
    p.add_option("--seqid", help="Only run partition with this seqid")
    p.add_option("--partitions", help="Use predefined partitions of LGs")
    p.add_option("--links", default=10, type="int",
                 help="Only plot matchings more than")
    p.add_option("--mincount", default=1, type="int",
                 help="Minimum markers on a contig")
    p.add_option("--noplot", default=False, action="store_true",
                 help="Do not visualize the alignments")
    p.add_option("--skipconcorde", default=False, action="store_true",
                 help="Skip TSP optimizer, can speed up large cases")
    p.add_option("--renumber", default=False, action="store_true",
                 help="Renumber chromosome based on decreasing sizes")
    p.set_cpus(cpus=16)

    q = OptionGroup(p, "Genetic algorithm options")
    p.add_option_group(q)
    q.add_option("--ngen", default=500, type="int",
                 help="Iterations in GA, higher ~ slower")
    q.add_option("--npop", default=100, type="int",
                 help="Population size in GA, higher ~ slower")
    q.add_option("--seed", default=666, type="int",
                 help="Random seed number")
    opts, args, iopts = p.set_image_options(args, figsize="10x6")

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

    inputbed, fastafile = args
    inputbed = opts.bedfile or inputbed
    fastafile = opts.fastafile or fastafile
    pf = inputbed.rsplit(".", 1)[0]
    bedfile = pf + ".bed"
    weightsfile = opts.weightsfile
    partitionsfile = opts.partitions
    gapsize = opts.gapsize
    mincount = opts.mincount
    skipconcorde = opts.skipconcorde
    ngen = opts.ngen
    npop = opts.npop
    cpus = opts.cpus
    seed = opts.seed
    if sys.version_info[:2] < (2, 7):
        logging.debug("Python version: {0}. CPUs set to 1.".\
                        format(sys.version.splitlines()[0].strip()))
        cpus = 1

    function = get_function(opts.distance)
    cc = Map(bedfile, function=function, compress=opts.compress, \
             remove_outliers=(not opts.noremoveoutliers))
    mapnames = cc.mapnames
    allseqids = cc.seqids
    weights = Weights(weightsfile, mapnames)
    pivot = weights.pivot
    ref = weights.ref
    linkage = opts.linkage
    oseqid = opts.seqid
    logging.debug("Linkage function: {0}-linkage".format(linkage))
    linkage = {"single": min, "double": double_linkage, "complete": max,
               "average": np.mean, "median": np.median}[linkage]

    # Partition the linkage groups into consensus clusters
    C = Grouper()
    # Initialize the partitions
    for mlg in cc.mlgs:
        C.join(mlg)

    if partitionsfile:
        logging.debug("Partition LGs based on `{}`".format(partitionsfile))
        fp = open(partitionsfile)
        for row in fp:
            C.join(*row.strip().split(","))
    else:
        logging.debug("Partition LGs based on {0}".format(ref))
        for mapname in mapnames:
            if mapname == ref:
                continue
            # Compute co-occurrence between LG pairs
            G = defaultdict(int)
            for s in allseqids:
                s = Scaffold(s, cc)
                s.add_LG_pairs(G, (ref, mapname))
            # Convert edge list to adj list
            nodes = defaultdict(list)
            for (a, b), w in G.items():
                nodes[a].append((b, w))
            # Find the best ref LG every non-ref LG matches to
            for n, neighbors in nodes.items():
                if n.split("-")[0] == ref:
                    continue
                neighbors = dict(neighbors)
                best_neighbor, best_value = best_no_ambiguous(neighbors, n)
                if best_neighbor is None:
                    continue
                C.join(n, best_neighbor)

    partitions = defaultdict(list)
    # Partition the scaffolds and assign them to one consensus
    for s in allseqids:
        s = Scaffold(s, cc)
        seqid = s.seqid
        counts = {}
        for mlg, count in s.mlg_counts.items():
            consensus = C[mlg]
            mapname = mlg.split("-")[0]
            mw = weights[mapname]
            if consensus not in counts:
                counts[consensus] = 0
            if count < mincount:
                continue
            counts[consensus] += count * mw
        best_consensus, best_value = best_no_ambiguous(counts, seqid)
        if best_consensus is None:
            continue
        partitions[best_consensus].append(seqid)

    # Perform OO within each partition
    agpfile = pf + ".chr.agp"
    tourfile = pf + ".tour"
    sizes = Sizes(fastafile).mapping
    fwagp = must_open(agpfile, "w")
    fwtour = must_open(tourfile, "w")
    solutions = []
    for lgs, scaffolds in natsorted(partitions.items()):
        if oseqid and oseqid not in lgs:
            continue
        tag = "|".join(lgs)
        lgs_maps = set(x.split("-")[0] for x in lgs)
        if pivot not in lgs_maps:
            logging.debug("Skipping {0} ...".format(tag))
            continue
        logging.debug("Working on {0} ...".format(tag))
        s = ScaffoldOO(lgs, scaffolds, cc, pivot, weights, sizes,
                       function=function, linkage=linkage, fwtour=fwtour,
                       skipconcorde=skipconcorde,
                       ngen=ngen, npop=npop, cpus=cpus, seed=seed)

        solutions.append(s)
    fwtour.close()

    # Renumber chromosome based on decreasing size
    if opts.renumber:
        chrsizes = {}
        conversion = {}
        for s in solutions:
            chrsizes[s.object] = sum(sizes[x] for (x, o) in s.tour) + \
                                 (len(s.tour) - 1) * gapsize
        for i, (c, size) in enumerate(sorted(chrsizes.items(), key=lambda x: -x[1])):
            newc = "chr{0}".format(i + 1)
            logging.debug("{0}: {1} => {2}".format(c, size, newc))
            conversion[c] = newc
        for s in solutions:
            s.object = conversion[s.object]

    # meta-data about the run parameters
    command = "# COMMAND: python -m jcvi.assembly.allmaps path {0}".\
                     format(" ".join(oargs))
    comment = "Generated by ALLMAPS v{0} ({1})\n{2}".\
                     format(version, get_today(), command)
    AGP.print_header(fwagp, comment=comment)

    for s in natsorted(solutions, key=lambda x: x.object):
        order_to_agp(s.object, s.tour, sizes, fwagp, gapsize=gapsize,
                     gaptype="map")
    fwagp.close()

    logging.debug("AGP file written to `{0}`.".format(agpfile))
    logging.debug("Tour file written to `{0}`.".format(tourfile))

    build([inputbed, fastafile])

    summaryfile = pf + ".summary.txt"
    summary([inputbed, fastafile, "--outfile={0}".format(summaryfile)])

    if not opts.noplot:
        plotall([inputbed, "--links={0}".format(opts.links),
                           "--figsize={0}".format(opts.figsize)])
Run the main Application.
dataset/ETHPy150Open tanghaibao/jcvi/assembly/allmaps.py/path--crossover with--dataset/ETHPy150Open openstack/openstack-ansible/scripts/pw-token-gen.py/main