comparison TEiso/GFFToBed_Wrapper.py @ 0:3d22562b4489 draft

Uploaded
author urgi-team
date Fri, 29 Apr 2016 09:11:18 -0400
parents
children 15d6811e6bf5
comparison
equal deleted inserted replaced
-1:000000000000 0:3d22562b4489
1 #!/usr/bin/env python
2
3
4 import subprocess, tempfile, sys, os, glob, shutil, time
5 from optparse import OptionParser
6 from commons.core.utils.RepetOptionParser import RepetOptionParser
7
8
9 class GFFToBedWrapper(object):
10
11 def __init__(self):
12 self._options = None
13
14
15 def stop_err(self, msg ):
16 sys.stderr.write( "%s\n" % msg )
17 sys.exit()
18
19
20 def setAttributesFromCmdLine(self):
21 self._toolVersion = "1.0"
22 description = "GFFToBed version %s" % self._toolVersion
23 epilog = "\n parses a GFF file and create a bed file. \n"
24 epilog += "example: GFFToBed.py -i <inputFile> -o <outputFile>\n"
25 parser = RepetOptionParser(description = description, epilog = epilog, version = self._toolVersion)
26 parser.add_option("-i", "--inputFile", dest = "inputFile", action = "store", type = "string", help = "Input GFF File name.", default = "")
27 parser.add_option("-o", "--outputFile", dest = "outputFile", action = "store", type = "string", help = "output Bed File name", default = "")
28 parser.add_option("-v", "--verbosity", dest = "verbosity", action = "store", type = "int", help = "Verbosity [optional] [default: 3]",default = 3)
29 options = parser.parse_args()[0]
30 self._setAttributesFromOptions(options)
31
32 def _setAttributesFromOptions(self, options):
33 self._options = options
34
35 def run(self):
36 prg = "GFFToBed.py"
37 args = ""
38 args += "-i %s" % self._options.inputFile
39 cmd = "%s %s" %(prg, args)
40 print cmd
41
42 try:
43 tmp_err = tempfile.NamedTemporaryFile().name
44 tmp_stderr = open( tmp_err, 'wb' )
45 proc = subprocess.Popen( args=cmd, shell=True, cwd=".", stderr=tmp_stderr )
46 returncode = proc.wait()
47 tmp_stderr.close()
48 # get stderr, allowing for case where it's very large
49 tmp_stderr = open( tmp_err, 'rb' )
50 stderr = ''
51 buffsize = 1048576
52 try:
53 while True:
54 stderr += tmp_stderr.read( buffsize )
55 if not stderr or len( stderr ) % buffsize != 0:
56 break
57 except OverflowError:
58 pass
59 tmp_stderr.close()
60 if stderr:
61 raise Exception, stderr
62 except Exception, e:
63 self.stop_err( 'Error in GFFToBed:\n' + str( e ) )
64
65 if __name__ == "__main__":
66 iWrapper = GFFToBedWrapper()
67 iWrapper.setAttributesFromCmdLine()
68 iWrapper.run()