annotate filter.py @ 0:4502baf7ac42 draft default tip

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