annotate TEiso/CufflinksGTFToBed_Wrapper.py @ 4:e59fb2fcb65e draft

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