annotate VCFCarto_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
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
5 from optparse import OptionParser
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
6
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
7
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
8 class VCFCartoWrapper(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 = "VCFcarto can convert your tabulated file into a file with only the SNP from refA and refH.\n"
a6b557df86db 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"
a6b557df86db 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"
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
23 description += "example 1 : VCFcarto.py -f Storage.out -A G15 -H G23 -o FilteredStorage.out\n"
a6b557df86db 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"
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
25 parser = OptionParser(description = description, version = "0.1")
a6b557df86db 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 = "")
a6b557df86db 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 = "")
a6b557df86db 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 = "")
a6b557df86db 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 = "")
a6b557df86db 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)
a6b557df86db 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)
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
32 parser.add_option("-M", "--mergeFile", dest = "mergeFile", action = "store", type = "string", help = "name for the mergeFile ", default = "")
a6b557df86db 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)
a6b557df86db 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 = "")
a6b557df86db 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 = "")
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
36 options = parser.parse_args()[0]
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
37 self._setAttributesFromOptions(options)
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
38
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
39
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
40 def _setAttributesFromOptions(self, options):
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
41 self._options = options
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
42
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
43 def run(self):
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
44 prg = "VCFCarto.py"
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
45 args = ""
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
46 args += "-f %s" % self._options.tableName
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
47 args += " "
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
48 args += "-o %s" % self._options.outFile
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
49 args += " "
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
50 args += "-A %s" % self._options.refA
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
51 args += " "
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
52 args += "-H %s" % self._options.refH
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
53 args += " "
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
54 args += "-v 2 "
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
55 if self._options.onlyPar :
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
56 args += " "
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
57 args += "-p"
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
58 if self._options.mergeMarkers :
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
59 args += " "
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
60 args += "-m"
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
61 if self._options.graphs :
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
62 args += " "
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
63 args += "-g"
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
64 cmd = "%s %s" %(prg, args)
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
65
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
66 print cmd
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
67
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 self.stop_err( 'Error in VCFCarto:\n' + str( e ) )
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
90
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
91 if self._options.mergeMarkers :
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
92 shutil.move("markerList.bed" ,self._options.mergeFile)
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
93 if self._options.graphs :
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
94 html = open(self._options.graphHTML, "w")
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
95
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
96 os.mkdir(self._options.dirGraphs)
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
97 lGraphsFiles = glob.glob("VCFCarto_graphs/*")
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
98 for file in lGraphsFiles :
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
99 baseName = os.path.basename(file)
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
100 shutil.move( file ,"%s/%s" %(self._options.dirGraphs, baseName))
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
101 line = "<img src=\"%s\" > \n" %(baseName)
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
102 html.write(line)
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
103
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
104 if __name__ == "__main__":
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
105 iWrapper = VCFCartoWrapper()
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
106 iWrapper.setAttributesFromCmdLine()
a6b557df86db Uploaded
urgi-team
parents:
diff changeset
107 iWrapper.run()