annotate tools/vcf_tools/filter.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 import os.path
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 import optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 import vcfClass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 from vcfClass import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 import tools
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 from tools import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 main()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 def filterFail(text, file):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 print >> sys.stderr, text
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 if file != None: os.remove(file)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 # Parse the command line options
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 usage = "Usage: vcfPytools.py filter [options]"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 parser = optparse.OptionParser(usage = usage)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 parser.add_option("-i", "--in",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 action="store", type="string",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 dest="vcfFile", help="input vcf file")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 parser.add_option("-o", "--out",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 action="store", type="string",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 dest="output", help="output vcf file")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 parser.add_option("-q", "--quality",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 action="store", type="int",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 dest="quality", help="filter out SNPs with qualities lower than selected value")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 parser.add_option("-n", "--info",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 action="append", type="string", nargs=3,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 dest="infoFilters", help="filter based on entries in the info string")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 parser.add_option("-r", "--remove-genotypes",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 action="store_true", default=False,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 dest="removeGeno", help="remove the genotype strings from the vcf file")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 parser.add_option("-m", "--mark-as-pass",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 action="store_true", default=False,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 dest="markPass", help="Mark all records as having passed filters")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 (options, args) = parser.parse_args()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 # Check that a single vcf file is given.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 if options.vcfFile == None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 parser.print_help()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 print >> sys.stderr, "\nInput vcf file (-i, --input) is required for vcf filtering."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 # The --mark-as-pass option can only be used if no actual filters
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 # have been specified.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 if options.markPass and options.infoFilters:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 print >> sys.stderr, "--mark-as-pass cannot be used in conjunction with filters."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 # Set the output file to stdout if no output file was specified.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 outputFile, writeOut = setOutput(options.output) # tools.py
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 v = vcf() # Define vcf object.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 # Open the vcf file.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 v.openVcf(options.vcfFile)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 # Read in the header information.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 v.parseHeader(options.vcfFile, writeOut)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 taskDescriptor = "##vcfPytools="
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 if options.infoFilters:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 taskDescriptor += "filtered using the following filters: "
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 for filter, value, logic in options.infoFilters: taskDescriptor += str(filter) + str(value) + ","
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 taskDescriptor = taskDescriptor.rstrip(",")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 if options.markPass: taskDescriptor += "marked all records as PASS"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 writeHeader(outputFile, v, options.removeGeno, taskDescriptor)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 # Check that specified filters from the info field are either integers or floats.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 if options.infoFilters:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 v.processInfo = True # Process the info string
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 filters = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 filterValues = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 filterLogic = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 for filter, value, logic in options.infoFilters:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 filterName = str(filter) + str(value)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 if "-" in filter or "-" in value or "-" in logic:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 print >> sys.stderr, "\n--info (-n) requires three arguments, for example:"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 print >> sys.stderr, "\t--info DP 5 lt: filter records with DP less than (lt) 5.\n"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 print >> sys.stderr, "allowed logic arguments:\n\tgt: greater than\n\tlt: less than."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 print >> sys.stderr, "\nError in:", filter
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 if logic != "gt" and logic != "lt":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 print >> sys.stderr, "\nfilter logic not recognised."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 print >> sys.stderr, "allowed logic arguments:\n\tgt: greater than\n\tlt: less than."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 print >> sys.stderr, "\nError in:", filter
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 if v.infoHeaderTags.has_key(filter):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 if v.infoHeaderTags[filter][1].lower() == "integer":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 filters[filterName] = filter
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 filterValues[filterName] = int(value)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 filterLogic[filterName] = logic
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 #filterLogic[filterName] = logic
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 except ValueError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 text = "Filter " + filter + " requires an integer entry, not " + str(type(value))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 filterFail(text, options.output)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 if v.infoHeaderTags[filter][1].lower() == "float":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 filters[filterName] = filter
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 filterValues[filterName] = float(value)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 filterLogic[filterName] = logic
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 #filters[filterName] = float(value)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 #filterLogic[filterName] = logic
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 except ValueError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 text = "Filter " + filter + " requires an float entry, not " + str(type(value))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 filterFail(text, options.output)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 text = "Filter " + filter + " has no explanation in the header. Unknown type for the entry."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 filterFail(text, options.output)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 # Parse the vcf file and check if any of the filters are failed. If
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 # so, build up a string of failed filters.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 while v.getRecord():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 filterString = ""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 # Mark the record as "PASS" if --mark-as-pass was applied.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 if options.markPass: v.filters = "PASS"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 # Check for quality filtering.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 if options.quality != None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 if v.quality < options.quality:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 filterString = filterString + ";" + "Q" + str(options.quality) if filterString != "" else "Q" + str(options.quality)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 # Check for filtering on info string filters.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 if options.infoFilters:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 for filterName, filter in filters.iteritems():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 value = filterValues[filterName]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 logic = filterLogic[filterName]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 if v.infoTags.has_key(filter):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 if type(value) == int:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 if logic == "lt" and int(v.infoTags[filter]) < value:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 filterString = filterString + ";" + filter + str(value) if filterString != "" else filter + str(value)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 if logic == "gt" and int(v.infoTags[filter]) > value:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 filterString = filterString + ";" + filter + str(value) if filterString != "" else filter + str(value)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 elif type(value) == float:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 if logic == "lt" and float(v.infoTags[filter]) < value:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 filterString = filterString + ";" + filter + str(value) if filterString != "" else filter + str(value)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 if logic == "gt" and float(v.infoTags[filter]) > value:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 filterString = filterString + ";" + filter + str(value) if filterString != "" else filter + str(value)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 filterString = "PASS" if filterString == "" else filterString
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 v.filters = filterString
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155 record = v.buildRecord(options.removeGeno)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156 outputFile.write(record)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 # Close the vcf files.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 v.closeVcf(options.vcfFile)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161 # Terminate the program.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 return 0