/tools/maf/maf_split_by_species.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 44 lines · 34 code · 6 blank · 4 comment · 12 complexity · 7401437a81702d5b8495e204415704fe MD5 · raw file

  1. #!/usr/bin/env python
  2. """
  3. Read a maf and split blocks by unique species combinations
  4. """
  5. import sys
  6. from galaxy import eggs
  7. import pkg_resources; pkg_resources.require( "bx-python" )
  8. from bx.align import maf
  9. from galaxy.tools.util import maf_utilities
  10. from galaxy.util import string_as_bool
  11. assert sys.version_info[:2] >= ( 2, 4 )
  12. def __main__():
  13. try:
  14. maf_reader = maf.Reader( open( sys.argv[1] ) )
  15. except Exception, e:
  16. maf_utilities.tool_fail( "Error opening MAF: %s" % e )
  17. try:
  18. out = maf.Writer( open( sys.argv[2], "w") )
  19. except Exception, e:
  20. maf_utilities.tool_fail( "Error opening file for output: %s" % e )
  21. try:
  22. collapse_columns = string_as_bool( sys.argv[3] )
  23. except Exception, e:
  24. maf_utilities.tool_fail( "Error determining collapse columns value: %s" % e )
  25. start_count = 0
  26. end_count = 0
  27. for start_count, start_block in enumerate( maf_reader ):
  28. for block in maf_utilities.iter_blocks_split_by_species( start_block ):
  29. if collapse_columns:
  30. block.remove_all_gap_columns()
  31. out.write( block )
  32. end_count += 1
  33. out.close()
  34. if end_count:
  35. print "%i alignment blocks created from %i original blocks." % ( end_count, start_count + 1 )
  36. else:
  37. print "No alignment blocks were created."
  38. if __name__ == "__main__": __main__()