annotate tools/maf/maf_limit_size.py @ 2:c2a356708570

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:42 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 #Dan Blankenberg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Removes blocks that fall outside of specified size range.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 import pkg_resources; pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 import bx.align.maf
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 input_maf_filename = sys.argv[1].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 output_filename1 = sys.argv[2].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 min_size = int( sys.argv[3].strip() )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 max_size = int( sys.argv[4].strip() )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 if max_size < 1: max_size = sys.maxint
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 maf_writer = bx.align.maf.Writer( open( output_filename1, 'w' ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 maf_reader = bx.align.maf.Reader( open( input_maf_filename, 'r' ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 print >>sys.stderr, "Your MAF file appears to be malformed."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 blocks_kept = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 i = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 for i, m in enumerate( maf_reader ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 if min_size <= m.text_size <= max_size:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 maf_writer.write( m )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 blocks_kept += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 print 'Kept %s of %s blocks (%.2f%%).' % ( blocks_kept, i + 1, float( blocks_kept ) / float( i + 1 ) * 100.0 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 if __name__ == "__main__": __main__()