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