/tools/maf/maf_to_fasta_concat.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 59 lines · 44 code · 8 blank · 7 comment · 19 complexity · f88c307004579c192680dcbd9721284a MD5 · raw file

  1. #!/usr/bin/env python
  2. """
  3. Read a maf and output a single block fasta file, concatenating blocks
  4. usage %prog species1,species2 maf_file out_file
  5. """
  6. #Dan Blankenberg
  7. import sys
  8. from galaxy import eggs
  9. import pkg_resources; pkg_resources.require( "bx-python" )
  10. from bx.align import maf
  11. from galaxy.tools.util import maf_utilities
  12. assert sys.version_info[:2] >= ( 2, 4 )
  13. def __main__():
  14. try:
  15. species = maf_utilities.parse_species_option( sys.argv[1] )
  16. except Exception, e:
  17. maf_utilities.tool_fail( "Error determining species value: %s" % e )
  18. try:
  19. input_filename = sys.argv[2]
  20. except Exception, e:
  21. maf_utilities.tool_fail( "Error reading MAF filename: %s" % e )
  22. try:
  23. file_out = open( sys.argv[3], 'w' )
  24. except Exception, e:
  25. maf_utilities.tool_fail( "Error opening file for output: %s" % e )
  26. if species:
  27. print "Restricted to species: %s" % ', '.join( species )
  28. else:
  29. print "Not restricted to species."
  30. if not species:
  31. try:
  32. species = maf_utilities.get_species_in_maf( input_filename )
  33. except Exception, e:
  34. maf_utilities.tool_fail( "Error determining species in input MAF: %s" % e )
  35. for spec in species:
  36. file_out.write( ">" + spec + "\n" )
  37. try:
  38. for start_block in maf.Reader( open( input_filename, 'r' ) ):
  39. for block in maf_utilities.iter_blocks_split_by_species( start_block ):
  40. block.remove_all_gap_columns() #remove extra gaps
  41. component = block.get_component_by_src_start( spec ) #blocks only have one occurrence of a particular species, so this is safe
  42. if component:
  43. file_out.write( component.text )
  44. else:
  45. file_out.write( "-" * block.text_size )
  46. except Exception, e:
  47. maf_utilities.tool_fail( "Your MAF file appears to be malformed: %s" % e )
  48. file_out.write( "\n" )
  49. file_out.close()
  50. if __name__ == "__main__": __main__()