Mercurial > repos > urgi-team > gandalfworkflow
view VCFStorage_wrapper.py @ 7:a6b557df86db draft
Uploaded
author | urgi-team |
---|---|
date | Tue, 15 Dec 2015 05:35:36 -0500 |
parents | |
children |
line wrap: on
line source
#!/usr/bin/env python import subprocess, tempfile, sys, os, glob, shutil, time, random from optparse import OptionParser from optparse import Option, OptionValueError class VCFStorageWrapper(object): def __init__(self): self._options = None def stop_err(self, msg ): sys.stderr.write( "%s\n" % msg ) sys.exit() def setAttributesFromCmdLine(self): description = "VCFStorage_wrapper" description += "\nWrapper for VCFStorage\n VCFStorage " description += "VCFStorage stores info from variant calling into a table. It will create a tabulate file with the different infos\n" description += "example : VCFStorage -f fasta.fa -l genomelist.list -w workdir -o output.tab \n" parser = OptionParser(description = description, version = "0.1") parser.add_option("-f", "--fasta", dest = "fastaFile", action = "store", type = "string", help = "Input fasta file name [compulsory] [format: Fasta]", default = "") parser.add_option("-l", "--genomeNameList", dest = "genomeNameList", action = "append", type = "string", help = "Input list of genome name ") parser.add_option("-L", "--genomeFileList", dest = "genomeFileList", action = "append", type = "string", help = "Input list of genome VCF file ") parser.add_option("-w", "--workDir", dest = "workDir", action = "store", type = "string", help = "name of the workingDirectory", default = "") parser.add_option("-o", "--out", dest = "outFile", action = "store", type = "string", help = "Output file name [compulsory] [format: tab]", default = "") options = parser.parse_args()[0] self._setAttributesFromOptions(options) def _setAttributesFromOptions(self, options): self._options = options def run(self): self.createGenomeList() cmd = self.createCmdLine() self.launchCmdLine(cmd) def createGenomeList(self): self.genomelistFile = "%s.genomeListFile" % self._options.outFile lGenomeName = self._options.genomeNameList lGenomeVCF = self._options.genomeFileList output = open(self.genomelistFile, "w") for i,genomeName in enumerate(lGenomeName) : output.write("%s\t%s\n" % (lGenomeName[i],lGenomeVCF[i])) output.close() def createCmdLine(self): workdir = "VCFStorage_%s_%d" % (time.strftime("%d%H%M%S"), random.randint(0, 10000)) prg = "VCFStorage.py" args = "" args += "-f %s" % self._options.fastaFile args += " " args += "-o %s" % self._options.outFile args += " " args += "-l %s" % self.genomelistFile args += " " args += "-w %s" % workdir cmd = "%s %s" %(prg, args) print cmd return cmd def launchCmdLine(self, cmd): try: tmp_err = tempfile.NamedTemporaryFile().name tmp_stderr = open( tmp_err, 'wb' ) proc = subprocess.Popen( args=cmd, shell=True, cwd=".", stderr=tmp_stderr ) returncode = proc.wait() tmp_stderr.close() # get stderr, allowing for case where it's very large tmp_stderr = open( tmp_err, 'rb' ) stderr = '' buffsize = 1048576 try: while True: stderr += tmp_stderr.read( buffsize ) if not stderr or len( stderr ) % buffsize != 0: break except OverflowError: pass tmp_stderr.close() if stderr: raise Exception, stderr except Exception, e: os.remove(self.genomelistFile) self.stop_err( 'Error in VCFStorage:\n' + str( e ) ) os.remove(self.genomelistFile) if __name__ == "__main__": iWrapper = VCFStorageWrapper() iWrapper.setAttributesFromCmdLine() iWrapper.run()