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

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