comparison TEiso/CufflinksGTFToBed_Wrapper.py @ 16:836ce3d9d47a draft default tip

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