comparison TEiso/GFFToBed_Wrapper.py @ 1:15d6811e6bf5 draft

Uploaded
author urgi-team
date Tue, 24 May 2016 08:59:28 -0400
parents 3d22562b4489
children
comparison
equal deleted inserted replaced
0:3d22562b4489 1:15d6811e6bf5
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 3
4 import subprocess, tempfile, sys, os, glob, shutil, time 4 import subprocess, tempfile, sys, os, glob, shutil, time
5 from optparse import OptionParser 5 from optparse import OptionParser
6 from commons.core.utils.RepetOptionParser import RepetOptionParser 6 #from commons.core.utils.RepetOptionParser import RepetOptionParser
7 7
8 8
9 class GFFToBedWrapper(object): 9 class GFFToBedWrapper(object):
10 10
11 def __init__(self): 11 def __init__(self):
16 sys.stderr.write( "%s\n" % msg ) 16 sys.stderr.write( "%s\n" % msg )
17 sys.exit() 17 sys.exit()
18 18
19 19
20 def setAttributesFromCmdLine(self): 20 def setAttributesFromCmdLine(self):
21 self._toolVersion = "1.0" 21 ## self._toolVersion = "1.0"
22 description = "GFFToBed version %s" % self._toolVersion 22 description = "GFFToBed version"
23 epilog = "\n parses a GFF file and create a bed file. \n" 23 epilog = "\n parses a GFF3 file and create a bed file. \n"
24 epilog += "example: GFFToBed.py -i <inputFile> -o <outputFile>\n" 24 epilog += "example: GFFToBed.py -i <inputFile> -o <outputFile>\n"
25 parser = RepetOptionParser(description = description, epilog = epilog, version = self._toolVersion) 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 = "") 26 parser = OptionParser(description = description, version = "1.0")
27 parser.add_option("-i", "--inputFile", dest = "inputFile", action = "store", type = "string", help = "Input GFF3 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("-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 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 options = parser.parse_args()[0]
30 self._setAttributesFromOptions(options) 31 self._setAttributesFromOptions(options)
31 32
32 def _setAttributesFromOptions(self, options): 33 def _setAttributesFromOptions(self, options):
33 self._options = options 34 self._options = options
34 35
35 def run(self): 36 def run(self):
36 prg = "GFFToBed.py" 37 tmp = "%s_tmp" % ((os.path.splitext(self._options.outputFile)[0]))
38 prg = "GFFToBed.py"
37 args = "" 39 args = ""
38 args += "-i %s" % self._options.inputFile 40 args += "-i %s" % self._options.inputFile
41 args += " "
42 args += "-o %s" % tmp
39 cmd = "%s %s" %(prg, args) 43 cmd = "%s %s" %(prg, args)
40 print cmd 44 print cmd
41 45
42 try: 46 try:
43 tmp_err = tempfile.NamedTemporaryFile().name 47 tmp_err = tempfile.NamedTemporaryFile().name
44 tmp_stderr = open( tmp_err, 'wb' ) 48 tmp_stderr = open( tmp_err, 'wb' )
45 proc = subprocess.Popen( args=cmd, shell=True, cwd=".", stderr=tmp_stderr ) 49 proc = subprocess.Popen( args=cmd, shell=True, cwd=".", stderr=tmp_stderr )
46 returncode = proc.wait() 50 returncode = proc.wait()
59 tmp_stderr.close() 63 tmp_stderr.close()
60 if stderr: 64 if stderr:
61 raise Exception, stderr 65 raise Exception, stderr
62 except Exception, e: 66 except Exception, e:
63 self.stop_err( 'Error in GFFToBed:\n' + str( e ) ) 67 self.stop_err( 'Error in GFFToBed:\n' + str( e ) )
64 68
69
70 try:
71 cmdsort= "bedtools sort -i %s > %s" % (tmp, self._options.outputFile)
72 os.system(cmdsort)
73 except Exception, e:
74 self.stop_err( 'Error in bedtools sort:\n' + str( e ) )
75
76
77
65 if __name__ == "__main__": 78 if __name__ == "__main__":
66 iWrapper = GFFToBedWrapper() 79 iWrapper = GFFToBedWrapper()
67 iWrapper.setAttributesFromCmdLine() 80 iWrapper.setAttributesFromCmdLine()
68 iWrapper.run() 81 iWrapper.run()