def filter(args):
    """
    %prog filter bedfile

    Filter the bedfile to retain records between certain size range.
    """
    p = OptionParser(filter.__doc__)
    p.add_option("--minsize", default=0, type="int",
                 help="Minimum feature length")
    p.add_option("--maxsize", default=1000000000, type="int",
                 help="Minimum feature length")
    p.add_option("--minaccn", type="int",
                 help="Minimum value of accn, useful to filter based on coverage")
    p.add_option("--minscore", type="int", help="Minimum score")
    p.set_outfile()

    opts, args = p.parse_args(args)

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

    bedfile, = args
    fp = must_open(bedfile)
    fw = must_open(opts.outfile, "w")
    minsize, maxsize = opts.minsize, opts.maxsize
    minaccn = opts.minaccn
    minscore = opts.minscore
    total = []
    keep = []
    for row in fp:
        try:
            b = BedLine(row)
        except IndexError:
            print(row.strip(), file=fw)
            continue
        span = b.span
        total.append(span)
        if not minsize <= span <= maxsize:
            continue
        if minaccn and int(b.accn) < minaccn:
            continue
        if minscore and int(b.score) < minscore:
            continue
        print(b, file=fw)
        keep.append(span)

    logging.debug("Stats: {0} features kept.".\
                    format(percentage(len(keep), len(total))))
    logging.debug("Stats: {0} bases kept.".\
                    format(percentage(sum(keep), sum(total))))
def filter ( args ) : p = OptionParser ( filter . __doc__ ) p . add_option ( "--minsize" , default = 0 , type = "int" , help = "Minimum feature length" ) p . add_option ( "--maxsize" , default = 1000000000 , type = "int" , help = "Minimum feature length" ) p . add_option ( "--minaccn" , type = "int" , help = "Minimum value of accn, useful to filter based on coverage" ) p . add_option ( "--minscore" , type = "int" , help = "Minimum score" ) p . set_outfile ( ) opts , args = p . parse_args ( args ) if len ( args ) != 1 : sys . exit ( not p . print_help ( ) ) bedfile , = args fp = must_open ( bedfile ) fw = must_open ( opts . outfile , "w" ) minsize , maxsize = opts . minsize , opts . maxsize minaccn = opts . minaccn minscore = opts . minscore total = [ ] keep = [ ] for row in fp : try : b = BedLine ( row ) except IndexError : print ( row . strip ( ) , file = fw ) continue span = b . span total . append ( span ) if not minsize <= span <= maxsize : continue if minaccn and int ( b . accn ) < minaccn : continue if minscore and int ( b . score ) < minscore : continue print ( b , file = fw ) keep . append ( span ) logging . debug ( "Stats: {0} features kept." . format ( percentage ( len ( keep ) , len ( total ) ) ) ) logging . debug ( "Stats: {0} bases kept." . format ( percentage ( sum ( keep ) , sum ( total ) ) ) )
%prog filter bedfile

    Filter the bedfile to retain records between certain size range.