diff VCFGandalfTools/VCFCarto_wrapper.py @ 2:6bebeb76fa8d draft

Uploaded
author urgi-team
date Tue, 05 Apr 2016 08:33:41 -0400
parents
children 1fd1f727c330
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VCFGandalfTools/VCFCarto_wrapper.py	Tue Apr 05 08:33:41 2016 -0400
@@ -0,0 +1,107 @@
+#!/usr/bin/env python
+
+
+import subprocess, tempfile, sys, os, glob, shutil, time
+from optparse import OptionParser
+
+
+class VCFCartoWrapper(object):
+
+    def __init__(self):
+        self._options = None
+        
+        
+    def stop_err(self, msg ):
+        sys.stderr.write( "%s\n" % msg )
+        sys.exit()
+        
+        
+    def setAttributesFromCmdLine(self):
+        description = "VCFcarto can convert your tabulated file into a file with only the SNP from refA and refH.\n"
+        description += "2 formats are possible, either the input format is conserved, or the format is changed into a 3 letter format\n"
+        description += "(\"A\" for refA, \"H\" for refH and \"-\" when the base do not correspond to any parent)\n\n"
+        description += "example 1 : VCFcarto.py -f Storage.out -A G15 -H G23 -o FilteredStorage.out\n"
+        description += "example 2 : VCFcarto.py -f Storage.out -A ref1 -H ref2 -p -s -g -m -o cartoTable.out\n"
+        parser = OptionParser(description = description, version = "0.2") 
+        parser.add_option("-f", "--file",      dest = "tableName", action = "store", type = "string", help = "Input TSV File name [compulsory] [format: TSV]",                                                                                                    default = "")
+        parser.add_option("-o", "--output",    dest = "outFile",   action = "store", type = "string", help = "output TSV File name [compulsory] [format: TSV]",                                                                                                   default = "")
+        parser.add_option("-A", "--refA",      dest = "refA",      action = "store", type = "string", help = "name of the reference genome A [compulsory] ",                                                                                                      default = "")
+        parser.add_option("-H", "--refH",      dest = "refH",      action = "store", type = "string", help = "name of the reference genome H [compulsory] ",                                                                                                      default = "")
+        parser.add_option("-p", "--onlyParents",   dest = "onlyPar",   action = "store_true",         help = "Will change every letters by either A or H depending on which parents the strain correspond to for that base[optional] [default: False]",           default = False)
+        parser.add_option("-m", "--mergeMarkers",   dest = "mergeMarkers",   action = "store_true",   help = "Will merge sequential markers with the same information ; option -p is needed [optional] [default: False]",                                         default = False)
+        parser.add_option("-M", "--mergeFile", dest = "mergeFile", action = "store", type = "string", help = "name for the mergeFile ",                                                                                                                           default = "")
+        parser.add_option("-g", "--graphics",  dest = "graphs",    action = "store_true",             help = "create graphs. Only works with -p[optional] [default: False]",                                                                                      default = False)
+        parser.add_option("-G", "--graphHTML", dest = "graphHTML", action = "store", type = "string", help = "name of the HTML linking to graphs ",                                                                                                              default = "")
+        parser.add_option("-d", "--dirGraphs", dest = "dirGraphs", action = "store", type = "string", help = "name of the folder containing graphs ",                                                                                                              default = "")
+        options = parser.parse_args()[0]
+        self._setAttributesFromOptions(options)
+
+
+    def _setAttributesFromOptions(self, options):
+        self._options = options
+
+    def run(self):
+        prg = "VCFCarto.py"
+        args = ""
+        args += "-f %s" % self._options.tableName
+        args += " "
+        args += "-o %s" % self._options.outFile
+        args += " "
+        args += "-A %s" % self._options.refA
+        args += " "
+        args += "-H %s" % self._options.refH
+        args += " "
+        args += "-v 2 "
+        if self._options.onlyPar :
+            args += " "
+            args += "-p"
+            if self._options.mergeMarkers :
+                args += " "
+                args += "-m"
+            if self._options.graphs :
+                args += " "
+                args += "-g"
+        cmd = "%s %s" %(prg, args)
+        
+        print 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:
+            self.stop_err( 'Error in VCFCarto:\n' + str( e ) ) 
+        
+        if self._options.mergeMarkers :
+            shutil.move("markerList.bed" ,self._options.mergeFile)
+        if self._options.graphs :
+            html = open(self._options.graphHTML, "w")
+            
+            os.mkdir(self._options.dirGraphs)
+            lGraphsFiles = glob.glob("VCFCarto_graphs/*")
+            for file in lGraphsFiles :
+                baseName = os.path.basename(file)
+                shutil.move( file ,"%s/%s" %(self._options.dirGraphs, baseName))
+                line = "<img src=\"%s\" > \n" %(baseName)
+                html.write(line)
+                
+if __name__ == "__main__":
+    iWrapper = VCFCartoWrapper()
+    iWrapper.setAttributesFromCmdLine()
+    iWrapper.run()