annotate TEiso/ClosestToStartSite_Wrapper.py @ 12:22b0494ec883 draft

Uploaded
author urgi-team
date Wed, 20 Jul 2016 08:18:51 -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
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
7
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
8 class ClosestToStartSiteWrapper(object):
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
9
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
10 def __init__(self):
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
11 self._options = None
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
12
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
13
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
14 def stop_err(self, msg ):
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
15 sys.stderr.write( "%s\n" % msg )
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
16 sys.exit()
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
17
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
18
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
19 def setAttributesFromCmdLine(self):
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
20 description = "ClosestToStartSite"
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
21 epilog = "\nParser a bed file and create a bed file to create a report about positions of features A to features B. \n"
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
22 epilog +="it can also add the class code of features A. \n"
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
23 epilog += "example: ClosestToStartSite.py -i <inputFile> -c <cuff_in.tmap> -o <outputFile>\n"
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
24 parser = OptionParser(description = description, version = "1.0")
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
25 parser.add_option("-i", "--inputFile", dest = "inputFile", action = "store", type = "string", help = "input bed file", default = "")
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
26 parser.add_option("-c", "--cuffcom_tmap", dest = "cuffcom_tmap", action = "store", type = "string", help = "input gtf file", default = "")
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
27 parser.add_option("-o", "--outputFile", dest = "outputFile", action = "store", type = "string", help = "output Bed File name", default = "")
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
28 #parser.add_option("-t", "--outputFileclasscode", dest = "outputFile_classcode", action = "store", type = "string", help = "output Bed File name with class code.", 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 prg = "ClosestToStartSite.py"
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
38 args = ""
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
39 args += "-i %s" % self._options.inputFile
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
40 args += " "
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
41 args += "-o %s" % self._options.outputFile
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
42 if self._options.cuffcom_tmap != "":
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
43 args += " "
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
44 args += "-c %s" % self._options.cuffcom_tmap
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
45 cmd = "%s %s" %(prg, args)
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
46 print cmd
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
47
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
48 try:
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
49 tmp_err = tempfile.NamedTemporaryFile().name
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
50 tmp_stderr = open( tmp_err, 'wb' )
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
51 proc = subprocess.Popen( args=cmd, shell=True, cwd=".", stderr=tmp_stderr )
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
52 returncode = proc.wait()
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
53 tmp_stderr.close()
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
54 # get stderr, allowing for case where it's very large
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
55 tmp_stderr = open( tmp_err, 'rb' )
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
56 stderr = ''
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
57 buffsize = 1048576
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
58 try:
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
59 while True:
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
60 stderr += tmp_stderr.read( buffsize )
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
61 if not stderr or len( stderr ) % buffsize != 0:
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
62 break
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
63 except OverflowError:
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
64 pass
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
65 tmp_stderr.close()
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
66 if stderr:
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
67 raise Exception, stderr
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
68 except Exception, e:
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
69 self.stop_err( 'Error in ClosestToStartSite:\n' + str( e ) )
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
70
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
71 if __name__ == "__main__":
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
72 iWrapper = ClosestToStartSiteWrapper()
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
73 iWrapper.setAttributesFromCmdLine()
782306d67e39 Uploaded
urgi-team
parents:
diff changeset
74 iWrapper.run()