/tools/maf/maf_by_block_number.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 46 lines · 35 code · 4 blank · 7 comment · 9 complexity · fcfbd12d42530b0f6579ba9333b68162 MD5 · raw file

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