/lib/galaxy/datatypes/converters/wiggle_to_simple_converter.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 44 lines · 32 code · 7 blank · 5 comment · 9 complexity · 2a6fffeb28eebaa76a952fa5900a556f MD5 · raw file

  1. #!/usr/bin/env python
  2. #code is same as ~/tools/stats/wiggle_to_simple.py
  3. """
  4. Read a wiggle track and print out a series of lines containing
  5. "chrom position score". Ignores track lines, handles bed, variableStep
  6. and fixedStep wiggle lines.
  7. """
  8. import sys
  9. from galaxy import eggs
  10. import pkg_resources; pkg_resources.require( "bx-python" )
  11. import bx.wiggle
  12. from galaxy.tools.exception_handling import *
  13. def stop_err( msg ):
  14. sys.stderr.write( msg )
  15. sys.exit()
  16. def main():
  17. if len( sys.argv ) > 1:
  18. in_file = open( sys.argv[1] )
  19. else:
  20. in_file = open( sys.stdin )
  21. if len( sys.argv ) > 2:
  22. out_file = open( sys.argv[2], "w" )
  23. else:
  24. out_file = sys.stdout
  25. try:
  26. for fields in bx.wiggle.IntervalReader( UCSCOutWrapper( in_file ) ):
  27. out_file.write( "%s\n" % "\t".join( map( str, fields ) ) )
  28. except UCSCLimitException:
  29. # Wiggle data was truncated, at the very least need to warn the user.
  30. print 'Encountered message from UCSC: "Reached output limit of 100000 data values", so be aware your data was truncated.'
  31. except ValueError, e:
  32. in_file.close()
  33. out_file.close()
  34. stop_err( str( e ) )
  35. in_file.close()
  36. out_file.close()
  37. if __name__ == "__main__": main()