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

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