PageRenderTime 15ms CodeModel.GetById 11ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/tools/maf/maf_limit_size.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 36 lines | 25 code | 6 blank | 5 comment | 5 complexity | 2a7d872c4a9f2ee6795b11a111a468e4 MD5 | raw file
 1#!/usr/bin/env python
 2#Dan Blankenberg
 3"""
 4Removes blocks that fall outside of specified size range.
 5"""
 6
 7import sys
 8from galaxy import eggs
 9import pkg_resources; pkg_resources.require( "bx-python" )
10import bx.align.maf
11
12assert sys.version_info[:2] >= ( 2, 4 )
13
14def __main__():
15
16    input_maf_filename = sys.argv[1].strip()
17    output_filename1 = sys.argv[2].strip()
18    min_size = int( sys.argv[3].strip() )
19    max_size = int( sys.argv[4].strip() )
20    if max_size < 1: max_size = sys.maxint
21    maf_writer = bx.align.maf.Writer( open( output_filename1, 'w' ) )
22    try:
23        maf_reader = bx.align.maf.Reader( open( input_maf_filename, 'r' ) )
24    except:
25        print >>sys.stderr, "Your MAF file appears to be malformed."
26        sys.exit()
27    
28    blocks_kept = 0
29    i = 0
30    for i, m in enumerate( maf_reader ):
31        if min_size <= m.text_size <= max_size:
32            maf_writer.write( m )
33            blocks_kept += 1
34    print 'Kept %s of %s blocks (%.2f%%).' % ( blocks_kept, i + 1, float( blocks_kept ) / float( i + 1 ) * 100.0 )
35
36if __name__ == "__main__": __main__()