annotate VCFGandalfTools/VCFStorage_wrapper.py @ 3:1fd1f727c330 draft default tip

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