def subset(args):
    p = OptionParser(subset.__doc__)
    p.add_option("--qchrs", default=None,
                help="query chrs to extract, comma sep [default: %default]")
    p.add_option("--schrs", default=None,
                help="subject chrs to extract, comma sep [default: %default]")
    p.add_option("--convert", default=False, action="store_true",
            help="convert accns to chr_rank [default: %default]")
    opts, args = p.parse_args(args)

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

    blastfile, qbedfile, sbedfile = args
    qchrs = opts.qchrs
    schrs = opts.schrs
    assert qchrs or schrs, p.print_help()
    convert = opts.convert

    outfile = blastfile + "."
    if qchrs:
        outfile += qchrs + "."
        qchrs = set(qchrs.split(","))
    else:
        qchrs = set(Bed(qbedfile).seqids)
    if schrs:
        schrs = set(schrs.split(","))
        if qbedfile != sbedfile or qchrs != schrs:
            outfile += ",".join(schrs) + "."
    else:
        schrs = set(Bed(sbedfile).seqids)
    outfile += "blast"

    qo = Bed(qbedfile).order
    so = Bed(sbedfile).order

    fw = must_open(outfile, "w")
    for b in Blast(blastfile):
        q, s = b.query, b.subject
        if qo[q][1].seqid in qchrs and so[s][1].seqid in schrs:
            if convert:
                b.query = qo[q][1].seqid + "_" + "{0:05d}".format(qo[q][0])
                b.subject = so[s][1].seqid + "_" + "{0:05d}".format(so[s][0])
            print >> fw, b
    fw.close()
    logging.debug("Subset blastfile written to `{0}`".format(outfile))
%prog subset blastfile qbedfile sbedfile
dataset/ETHPy150Open tanghaibao/jcvi/formats/blast.py/subset