PageRenderTime 10ms CodeModel.GetById 6ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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

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