def graph(args):
    p = OptionParser(graph.__doc__)
    p.add_option("--query", default=-1, type="int",
                 help="Search from node, -1 to select random node, 0 to disable")
    p.add_option("--contig", help="Search from contigs, use comma to separate")
    p.add_option("--largest", default=0, type="int", help="Only show largest components")
    p.add_option("--maxsize", default=500, type="int", help="Max graph size")
    p.add_option("--nomutualbest", default=False, action="store_true",
                help="Do not plot mutual best edges as heavy")
    add_graph_options(p)
    opts, args = p.parse_args(args)

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

    bestedges, = args
    query = opts.query
    contig = opts.contig
    largest = opts.largest
    frgctg = opts.frgctg
    edgeweight = not opts.nomutualbest
    G = read_graph(bestedges, maxerr=opts.maxerr)

    if largest:
        H = list(nx.connected_component_subgraphs(G))
        c = min(len(H), largest)
        logging.debug("{0} components found, {1} retained".format(len(H), c))
        G = nx.Graph()
        for x in H[:c]:
            G.add_edges_from(x.edges())

    if query:
        if query == -1:
            query = choice(G.nodes())
        reads_to_ctgs = parse_ctgs(bestedges, frgctg)
        if contig:
            contigs = set(contig.split(","))
            core = [k for k, v in reads_to_ctgs.items() if v in contigs]
        else:
            ctg = reads_to_ctgs.get(query)
            core = [k for k, v in reads_to_ctgs.items() if v == ctg]
            logging.debug("Reads ({0}) extended from the same contig {1}".\
                          format(len(core), ctg))

        # Extract a local neighborhood
        SG = nx.Graph()
        H = graph_local_neighborhood(G, query=core, maxsize=opts.maxsize)
        SG.add_edges_from(H.edges(data=edgeweight))
        G = SG

        seen = []
        for n, attrib in G.nodes_iter(data=True):
            contig = reads_to_ctgs.get(n, "na")
            attrib['label'] = contig
            seen.append(contig)
        c = Counter(seen)
        cc = ["{0}({1})".format(k, v) for k, v in c.most_common()]
        print >> sys.stderr, "Contigs: {0}".format(" ".join(cc))

    gexf = "best"
    if query >= 0:
        gexf += ".{0}".format(query)
    gexf += ".gexf"
    nx.write_gexf(G, gexf)
    logging.debug("Graph written to `{0}` (|V|={1}, |E|={2})".\
                    format(gexf, len(G), G.size()))
Creates an immediate state transition.
dataset/ETHPy150Open tanghaibao/jcvi/assembly/ca.py/graph--crossover with--dataset/ETHPy150Open chop-dbhi/django-sts/sts/shortcuts.py/transition