def filter(args):
    p = OptionParser(filter.__doc__)
    p.add_option("--type", default="mRNA",
             help="The feature to scan for the attributes [default: %default]")
    g1 = OptionGroup(p, "Filter by identity/coverage attribute values")
    g1.add_option("--id", default=95, type="float",
                 help="Minimum identity [default: %default]")
    g1.add_option("--coverage", default=90, type="float",
                 help="Minimum coverage [default: %default]")
    g1.add_option("--nocase", default=False, action="store_true",
                 help="Case insensitive lookup of attribute names [default: %default]")
    p.add_option_group(g1)
    g2 = OptionGroup(p, "Filter by child feature bp length")
    g2.add_option("--child_ftype", default=None, type="str",
                 help="Child featuretype to consider")
    g2.add_option("--child_bp", default=None, type="int",
                 help="Filter by total bp of children of chosen ftype")
    p.add_option_group(g2)
    p.set_outfile()

    opts, args = p.parse_args(args)
    otype, oid, ocov = opts.type, opts.id, opts.coverage
    cftype, clenbp = opts.child_ftype, opts.child_bp

    id_attr, cov_attr = "Identity", "Coverage"
    if opts.nocase:
        id_attr, cov_attr = id_attr.lower(), cov_attr.lower()

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

    gffile, = args

    gffdb = make_index(gffile)
    bad = set()
    ptype = None
    for g in gffdb.features_of_type(otype, order_by=('seqid', 'start')):
        if not ptype:
            parent = list(gffdb.parents(g))
            ptype = parent[0].featuretype \
                if len(parent) > 0 else otype
        if cftype and clenbp:
            if gffdb.children_bp(g, child_featuretype=cftype) < clenbp:
                bad.add(g.id)
        elif oid and ocov:
            identity = float(g.attributes[id_attr][0])
            coverage = float(g.attributes[cov_attr][0])
            if identity < oid or coverage < ocov:
                bad.add(g.id)

    logging.debug("{0} bad accns marked.".format(len(bad)))

    fw = must_open(opts.outfile, "w")
    for g in gffdb.features_of_type(ptype, order_by=('seqid', 'start')):
        if ptype != otype:
            feats = list(gffdb.children(g, featuretype=otype, order_by=('start')))
            ok_feats = [f for f in feats if f.id not in bad]
            if len(ok_feats) > 0:
                print >> fw, g
                for feat in ok_feats:
                    print >> fw, feat
                    for child in gffdb.children(feat, order_by=('start')):
                        print >> fw, child
        else:
            if g.id not in bad:
                print >> fw, g
                for child in gffdb.children(g, order_by=('start')):
                    print >> fw, child
    fw.close()
Used to modify the answer from its raw text format.
dataset/ETHPy150Open tanghaibao/jcvi/formats/gff.py/filter--crossover with--dataset/ETHPy150Open sahana/eden/modules/s3db/survey.py/S3AbstractAnalysis.castRawAnswer