comparison VCFGandalfTools/VCFFiltering_wrapper.py @ 2:6bebeb76fa8d draft

Uploaded
author urgi-team
date Tue, 05 Apr 2016 08:33:41 -0400
parents
children
comparison
equal deleted inserted replaced
1:cfd4eaadad42 2:6bebeb76fa8d
1 #!/usr/bin/env python
2
3
4 import subprocess, tempfile, sys, os, glob, shutil, time
5 from optparse import OptionParser
6 from optparse import Option, OptionValueError
7
8 class VCFFilteringWrapper(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 = "VCFFiltering_wrapper"
21 description += "\nWrapper for VCFFiltering ;\n VCFFiltering filters SNP on a VCF depending on depth (DP) allele number (AN), allele frequency (AF) and SNP quality.\n"
22 description += "example 1 : VCFFiltering.py -f myVCF.vcf -o FilteredVCF.vcf\n"
23 description += "example 2 : VCFFiltering.py -f myVCF.vcf -N 2 -F 0.87 -b bed1.bed bed2.bed -o FilteredVCF.vcf\n"
24 parser = OptionParser(description = description, version = "0.2")
25 parser.add_option("-f", "--vcf", dest = "VCFFile", action = "store", type = "string", help = "Input VCF File name [compulsory] [format: VCF]", default = "")
26 parser.add_option("-o", "--output", dest = "outFile", action = "store", type = "string", help = "output VCF File name [compulsory] [format: VCF]", default = "")
27 parser.add_option("-m", "--minDP", dest = "minDP", action = "store", type = "int", help = "minimum of depth ; if both minDP and maxDP are set, optimal DP will not be calculated ", default = 0)
28 parser.add_option("-M", "--maxDP", dest = "maxDP", action = "store", type = "int", help = "maximum of depth ; if both minDP and maxDP are set, optimal DP will not be calculated ", default = 0)
29 parser.add_option("-N", "--AN", dest = "AN", action = "store", type = "int", help = "maximum number of allele for a SNP; default = 2", default = 2)
30 parser.add_option("-F", "--AF", dest = "AF", action = "store", type = "float", help = "minimum frequency for the alternative allele of a SNP; default = 0.9", default = 0.9)
31 parser.add_option("-b", "--bed", dest = "bedFiles", action = "append", type = "string", help = "bed files: list of coordinates to filter, multiple arguments allowed '-b file1 file2' ", default = [])
32 parser.add_option("-G", "--graphHTML", dest = "graphHTML", action = "store", type = "string", help = "name of the HTML linking to graphs ", default = "")
33 parser.add_option("-d", "--dirGraphs", dest = "dirGraphs", action = "store", type = "string", help = "name of the folder containing graphs ", default = "")
34 options = parser.parse_args()[0]
35 self._setAttributesFromOptions(options)
36
37
38 def _setAttributesFromOptions(self, options):
39 self._options = options
40
41 def run(self):
42 if self._options.minDP and self._options.maxDP :
43 if self._options.minDP > self._options.maxDP :
44 self.stop_err( 'error in options : minDP > max DP (%s > %s)' %(self._options.minDP,self._options.maxDP))
45
46 prg = "VCFFiltering.py -g -G 'png' "
47 args = ""
48 args += "-f %s" % self._options.VCFFile
49 args += " "
50 args += "-o %s" % self._options.outFile
51 if self._options.AF :
52 args += " "
53 args += "-F %s" % self._options.AF
54 if self._options.AN :
55 args += " "
56 args += "-N %s" % self._options.AN
57 if self._options.minDP :
58 args += " "
59 args += "-m %s" % self._options.minDP
60 if self._options.maxDP :
61 args += " "
62 args += "-M %s" % self._options.maxDP
63 for bedfile in self._options.bedFiles :
64 args += " "
65 args += "-b %s" % bedfile
66 cmd = "%s %s" %(prg, args)
67
68 print cmd
69
70 try:
71 tmp_err = tempfile.NamedTemporaryFile().name
72 tmp_stderr = open( tmp_err, 'wb' )
73 proc = subprocess.Popen( args=cmd, shell=True, cwd=".", stderr=tmp_stderr )
74 returncode = proc.wait()
75 tmp_stderr.close()
76 # get stderr, allowing for case where it's very large
77 tmp_stderr = open( tmp_err, 'rb' )
78 stderr = ''
79 buffsize = 1048576
80 try:
81 while True:
82 stderr += tmp_stderr.read( buffsize )
83 if not stderr or len( stderr ) % buffsize != 0:
84 break
85 except OverflowError:
86 pass
87 tmp_stderr.close()
88 if stderr:
89 raise Exception, stderr
90 except Exception, e:
91 self.stop_err( 'Error in VCFFiltering:\n' + str( e ) )
92
93 if True :
94 html = open(self._options.graphHTML, "w")
95
96 os.mkdir(self._options.dirGraphs)
97 lGraphsFiles = glob.glob("VCFFiltering_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
105 if __name__ == "__main__":
106 iWrapper = VCFFilteringWrapper()
107 iWrapper.setAttributesFromCmdLine()
108 iWrapper.run()