annotate VCFStorage_wrapper.py @ 7:a6b557df86db draft

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