comparison VCFCarto_wrapper.py @ 7:a6b557df86db draft

Uploaded
author urgi-team
date Tue, 15 Dec 2015 05:35:36 -0500
parents
children
comparison
equal deleted inserted replaced
6:7557b71a92e7 7:a6b557df86db
1 #!/usr/bin/env python
2
3
4 import subprocess, tempfile, sys, os, glob, shutil, time
5 from optparse import OptionParser
6
7
8 class VCFCartoWrapper(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 = "VCFcarto can convert your tabulated file into a file with only the SNP from refA and refH.\n"
21 description += "2 formats are possible, either the input format is conserved, or the format is changed into a 3 letter format\n"
22 description += "(\"A\" for refA, \"H\" for refH and \"-\" when the base do not correspond to any parent)\n\n"
23 description += "example 1 : VCFcarto.py -f Storage.out -A G15 -H G23 -o FilteredStorage.out\n"
24 description += "example 2 : VCFcarto.py -f Storage.out -A ref1 -H ref2 -p -s -g -m -o cartoTable.out\n"
25 parser = OptionParser(description = description, version = "0.1")
26 parser.add_option("-f", "--file", dest = "tableName", action = "store", type = "string", help = "Input TSV File name [compulsory] [format: TSV]", default = "")
27 parser.add_option("-o", "--output", dest = "outFile", action = "store", type = "string", help = "output TSV File name [compulsory] [format: TSV]", default = "")
28 parser.add_option("-A", "--refA", dest = "refA", action = "store", type = "string", help = "name of the reference genome A [compulsory] ", default = "")
29 parser.add_option("-H", "--refH", dest = "refH", action = "store", type = "string", help = "name of the reference genome H [compulsory] ", default = "")
30 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)
31 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)
32 parser.add_option("-M", "--mergeFile", dest = "mergeFile", action = "store", type = "string", help = "name for the mergeFile ", default = "")
33 parser.add_option("-g", "--graphics", dest = "graphs", action = "store_true", help = "create graphs. Only works with -p[optional] [default: False]", default = False)
34 parser.add_option("-G", "--graphHTML", dest = "graphHTML", action = "store", type = "string", help = "name of the HTML linking to graphs ", default = "")
35 parser.add_option("-d", "--dirGraphs", dest = "dirGraphs", action = "store", type = "string", help = "name of the folder containing graphs ", default = "")
36 options = parser.parse_args()[0]
37 self._setAttributesFromOptions(options)
38
39
40 def _setAttributesFromOptions(self, options):
41 self._options = options
42
43 def run(self):
44 prg = "VCFCarto.py"
45 args = ""
46 args += "-f %s" % self._options.tableName
47 args += " "
48 args += "-o %s" % self._options.outFile
49 args += " "
50 args += "-A %s" % self._options.refA
51 args += " "
52 args += "-H %s" % self._options.refH
53 args += " "
54 args += "-v 2 "
55 if self._options.onlyPar :
56 args += " "
57 args += "-p"
58 if self._options.mergeMarkers :
59 args += " "
60 args += "-m"
61 if self._options.graphs :
62 args += " "
63 args += "-g"
64 cmd = "%s %s" %(prg, args)
65
66 print cmd
67
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 self.stop_err( 'Error in VCFCarto:\n' + str( e ) )
90
91 if self._options.mergeMarkers :
92 shutil.move("markerList.bed" ,self._options.mergeFile)
93 if self._options.graphs :
94 html = open(self._options.graphHTML, "w")
95
96 os.mkdir(self._options.dirGraphs)
97 lGraphsFiles = glob.glob("VCFCarto_graphs/*")
98 for file in lGraphsFiles :
99 baseName = os.path.basename(file)
100 shutil.move( file ,"%s/%s" %(self._options.dirGraphs, baseName))
101 line = "<img src=\"%s\" > \n" %(baseName)
102 html.write(line)
103
104 if __name__ == "__main__":
105 iWrapper = VCFCartoWrapper()
106 iWrapper.setAttributesFromCmdLine()
107 iWrapper.run()