0
+ − 1 #!/usr/bin/env python
+ − 2
+ − 3
+ − 4 import subprocess, tempfile, sys, os, glob, shutil, time
+ − 5 from optparse import OptionParser
+ − 6
+ − 7
+ − 8 class ClosestToStartSiteWrapper(object):
+ − 9
+ − 10 def __init__(self):
+ − 11 self._options = None
+ − 12
+ − 13
+ − 14 def stop_err(self, msg ):
+ − 15 sys.stderr.write( "%s\n" % msg )
+ − 16 sys.exit()
+ − 17
+ − 18
+ − 19 def setAttributesFromCmdLine(self):
1
+ − 20 description = "ClosestToStartSite"
+ − 21 epilog = "\nParser a bed file and create a bed file to create a report about positions of features A to features B. \n"
+ − 22 epilog +="it can also add the class code of features A. \n"
+ − 23 epilog += "example: ClosestToStartSite.py -i <inputFile> -c <cuff_in.tmap> -o <outputFile>\n"
+ − 24 parser = OptionParser(description = description, version = "1.0")
+ − 25 parser.add_option("-i", "--inputFile", dest = "inputFile", action = "store", type = "string", help = "input bed file", default = "")
+ − 26 parser.add_option("-c", "--cuffcom_tmap", dest = "cuffcom_tmap", action = "store", type = "string", help = "input gtf file", default = "")
0
+ − 27 parser.add_option("-o", "--outputFile", dest = "outputFile", action = "store", type = "string", help = "output Bed File name", default = "")
1
+ − 28 #parser.add_option("-t", "--outputFileclasscode", dest = "outputFile_classcode", action = "store", type = "string", help = "output Bed File name with class code.", default = "")
+ − 29 parser.add_option("-v", "--verbosity", dest = "verbosity", action = "store", type = "int", help = "verbosity [optional] [default: 3]",default = 3)
0
+ − 30 options = parser.parse_args()[0]
+ − 31 self._setAttributesFromOptions(options)
+ − 32
+ − 33 def _setAttributesFromOptions(self, options):
+ − 34 self._options = options
+ − 35
+ − 36 def run(self):
+ − 37 prg = "ClosestToStartSite.py"
+ − 38 args = ""
+ − 39 args += "-i %s" % self._options.inputFile
1
+ − 40 args += " "
+ − 41 args += "-o %s" % self._options.outputFile
+ − 42 if self._options.cuffcom_tmap != "":
+ − 43 args += " "
+ − 44 args += "-c %s" % self._options.cuffcom_tmap
0
+ − 45 cmd = "%s %s" %(prg, args)
+ − 46 print cmd
+ − 47
+ − 48 try:
+ − 49 tmp_err = tempfile.NamedTemporaryFile().name
+ − 50 tmp_stderr = open( tmp_err, 'wb' )
+ − 51 proc = subprocess.Popen( args=cmd, shell=True, cwd=".", stderr=tmp_stderr )
+ − 52 returncode = proc.wait()
+ − 53 tmp_stderr.close()
+ − 54 # get stderr, allowing for case where it's very large
+ − 55 tmp_stderr = open( tmp_err, 'rb' )
+ − 56 stderr = ''
+ − 57 buffsize = 1048576
+ − 58 try:
+ − 59 while True:
+ − 60 stderr += tmp_stderr.read( buffsize )
+ − 61 if not stderr or len( stderr ) % buffsize != 0:
+ − 62 break
+ − 63 except OverflowError:
+ − 64 pass
+ − 65 tmp_stderr.close()
+ − 66 if stderr:
+ − 67 raise Exception, stderr
+ − 68 except Exception, e:
+ − 69 self.stop_err( 'Error in ClosestToStartSite:\n' + str( e ) )
+ − 70
+ − 71 if __name__ == "__main__":
+ − 72 iWrapper = ClosestToStartSiteWrapper()
+ − 73 iWrapper.setAttributesFromCmdLine()
+ − 74 iWrapper.run()