/tools/filters/wiggle_to_simple.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 43 lines · 32 code · 7 blank · 4 comment · 9 complexity · 8596894e29037ebcf2b704ddc78bfbad MD5 · raw file

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