PageRenderTime 53ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/tools/filters/wiggle_to_simple.py

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