annotate tools/maf/maf_by_block_number.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
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 Reads a list of block numbers and a maf. Produces a new maf containing the
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 blocks specified by number.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 import pkg_resources; pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 from galaxy.tools.util import maf_utilities
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 import bx.align.maf
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 input_block_filename = sys.argv[1].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 input_maf_filename = sys.argv[2].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 output_filename1 = sys.argv[3].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 block_col = int( sys.argv[4].strip() ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 if block_col < 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 print >> sys.stderr, "Invalid column specified"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 sys.exit(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 species = maf_utilities.parse_species_option( sys.argv[5].strip() )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 maf_writer = bx.align.maf.Writer( open( output_filename1, 'w' ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 #we want to maintain order of block file and write blocks as many times as they are listed
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 failed_lines = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 for ctr, line in enumerate( open( input_block_filename, 'r' ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 block_wanted = int( line.split( "\t" )[block_col].strip() )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 failed_lines.append( str( ctr ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 for count, block in enumerate( bx.align.maf.Reader( open( input_maf_filename, 'r' ) ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 if count == block_wanted:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 if species:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 block = block.limit_to_species( species )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 maf_writer.write( block )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 print >>sys.stderr, "Your MAF file appears to be malformed."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 if len( failed_lines ) > 0: print "Failed to extract from %i lines (%s)." % ( len( failed_lines ), ",".join( failed_lines ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 if __name__ == "__main__": __main__()