annotate TEiso/GFFToBed_Wrapper.py @ 16:836ce3d9d47a draft default tip

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