2
|
1 #!/usr/bin/env python
|
|
2
|
|
3
|
|
4 import subprocess, tempfile, sys, os, glob, shutil, time, random
|
|
5 from optparse import OptionParser
|
|
6 from optparse import Option, OptionValueError
|
|
7
|
|
8 class VCFStorageWrapper(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):
|
|
20 description = "VCFStorage_wrapper"
|
|
21 description += "\nWrapper for VCFStorage\n VCFStorage "
|
|
22 description += "VCFStorage stores info from variant calling into a table. It will create a tabulate file with the different infos\n"
|
|
23 description += "example : VCFStorage -f fasta.fa -l genomelist.list -w workdir -o output.tab \n"
|
|
24 parser = OptionParser(description = description, version = "0.2")
|
|
25 parser.add_option("-f", "--fasta", dest = "fastaFile", action = "store", type = "string", help = "Input fasta file name [compulsory] [format: Fasta]", default = "")
|
|
26 parser.add_option("-l", "--genomeNameList", dest = "genomeNameList", action = "append", type = "string", help = "Input list of genome name ")
|
|
27 parser.add_option("-L", "--genomeFileList", dest = "genomeFileList", action = "append", type = "string", help = "Input list of genome VCF file ")
|
|
28 parser.add_option("-w", "--workDir", dest = "workDir", action = "store", type = "string", help = "name of the workingDirectory", default = "")
|
|
29 parser.add_option("-o", "--out", dest = "outFile", action = "store", type = "string", help = "Output file name [compulsory] [format: tab]", default = "")
|
|
30 options = parser.parse_args()[0]
|
|
31 self._setAttributesFromOptions(options)
|
|
32
|
|
33
|
|
34 def _setAttributesFromOptions(self, options):
|
|
35 self._options = options
|
|
36
|
|
37 def run(self):
|
|
38 self.createGenomeList()
|
|
39 cmd = self.createCmdLine()
|
|
40 self.launchCmdLine(cmd)
|
|
41
|
|
42 def createGenomeList(self):
|
|
43 self.genomelistFile = "%s.genomeListFile" % self._options.outFile
|
|
44 lGenomeName = self._options.genomeNameList
|
|
45 lGenomeVCF = self._options.genomeFileList
|
|
46 output = open(self.genomelistFile, "w")
|
|
47 for i,genomeName in enumerate(lGenomeName) :
|
|
48 output.write("%s\t%s\n" % (lGenomeName[i],lGenomeVCF[i]))
|
|
49 output.close()
|
|
50
|
|
51 def createCmdLine(self):
|
|
52 workdir = "VCFStorage_%s_%d" % (time.strftime("%d%H%M%S"), random.randint(0, 10000))
|
|
53 prg = "VCFStorage.py"
|
|
54 args = ""
|
|
55 args += "-f %s" % self._options.fastaFile
|
|
56 args += " "
|
|
57 args += "-o %s" % self._options.outFile
|
|
58 args += " "
|
|
59 args += "-l %s" % self.genomelistFile
|
|
60 args += " "
|
|
61 args += "-w %s" % workdir
|
|
62 cmd = "%s %s" %(prg, args)
|
|
63
|
|
64 print cmd
|
|
65 return cmd
|
|
66
|
|
67 def launchCmdLine(self, cmd):
|
|
68 try:
|
|
69 tmp_err = tempfile.NamedTemporaryFile().name
|
|
70 tmp_stderr = open( tmp_err, 'wb' )
|
|
71 proc = subprocess.Popen( args=cmd, shell=True, cwd=".", stderr=tmp_stderr )
|
|
72 returncode = proc.wait()
|
|
73 tmp_stderr.close()
|
|
74 # get stderr, allowing for case where it's very large
|
|
75 tmp_stderr = open( tmp_err, 'rb' )
|
|
76 stderr = ''
|
|
77 buffsize = 1048576
|
|
78 try:
|
|
79 while True:
|
|
80 stderr += tmp_stderr.read( buffsize )
|
|
81 if not stderr or len( stderr ) % buffsize != 0:
|
|
82 break
|
|
83 except OverflowError:
|
|
84 pass
|
|
85 tmp_stderr.close()
|
|
86 if stderr:
|
|
87 raise Exception, stderr
|
|
88 except Exception, e:
|
|
89 os.remove(self.genomelistFile)
|
|
90 self.stop_err( 'Error in VCFStorage:\n' + str( e ) )
|
|
91 os.remove(self.genomelistFile)
|
|
92
|
|
93 if __name__ == "__main__":
|
|
94 iWrapper = VCFStorageWrapper()
|
|
95 iWrapper.setAttributesFromCmdLine()
|
|
96 iWrapper.run()
|