0
|
1 #!/usr/bin/env python
|
|
2
|
|
3
|
|
4 import subprocess, tempfile, sys, os, glob, shutil, time
|
|
5 from optparse import OptionParser
|
1
|
6 #from commons.core.utils.RepetOptionParser import RepetOptionParser
|
0
|
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):
|
1
|
21 ## self._toolVersion = "1.0"
|
|
22 description = "GFFToBed version"
|
|
23 epilog = "\n parses a GFF3 file and create a bed file. \n"
|
0
|
24 epilog += "example: GFFToBed.py -i <inputFile> -o <outputFile>\n"
|
1
|
25 #parser = RepetOptionParser(description = description, epilog = epilog, version = self._toolVersion)
|
|
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 = "")
|
0
|
28 parser.add_option("-o", "--outputFile", dest = "outputFile", action = "store", type = "string", help = "output Bed File name", default = "")
|
|
29 parser.add_option("-v", "--verbosity", dest = "verbosity", action = "store", type = "int", help = "Verbosity [optional] [default: 3]",default = 3)
|
|
30 options = parser.parse_args()[0]
|
|
31 self._setAttributesFromOptions(options)
|
|
32
|
|
33 def _setAttributesFromOptions(self, options):
|
|
34 self._options = options
|
|
35
|
|
36 def run(self):
|
1
|
37 tmp = "%s_tmp" % ((os.path.splitext(self._options.outputFile)[0]))
|
|
38 prg = "GFFToBed.py"
|
0
|
39 args = ""
|
|
40 args += "-i %s" % self._options.inputFile
|
1
|
41 args += " "
|
|
42 args += "-o %s" % tmp
|
0
|
43 cmd = "%s %s" %(prg, args)
|
|
44 print cmd
|
1
|
45
|
0
|
46 try:
|
|
47 tmp_err = tempfile.NamedTemporaryFile().name
|
|
48 tmp_stderr = open( tmp_err, 'wb' )
|
|
49 proc = subprocess.Popen( args=cmd, shell=True, cwd=".", stderr=tmp_stderr )
|
|
50 returncode = proc.wait()
|
|
51 tmp_stderr.close()
|
|
52 # get stderr, allowing for case where it's very large
|
|
53 tmp_stderr = open( tmp_err, 'rb' )
|
|
54 stderr = ''
|
|
55 buffsize = 1048576
|
|
56 try:
|
|
57 while True:
|
|
58 stderr += tmp_stderr.read( buffsize )
|
|
59 if not stderr or len( stderr ) % buffsize != 0:
|
|
60 break
|
|
61 except OverflowError:
|
|
62 pass
|
|
63 tmp_stderr.close()
|
|
64 if stderr:
|
|
65 raise Exception, stderr
|
|
66 except Exception, e:
|
|
67 self.stop_err( 'Error in GFFToBed:\n' + str( e ) )
|
1
|
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
|
0
|
78 if __name__ == "__main__":
|
|
79 iWrapper = GFFToBedWrapper()
|
|
80 iWrapper.setAttributesFromCmdLine()
|
|
81 iWrapper.run()
|