File: varfilter.py

package info (click to toggle)
samtools 0.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,168 kB
  • ctags: 2,085
  • sloc: ansic: 12,759; perl: 1,669; makefile: 156; python: 141
file content (205 lines) | stat: -rwxr-xr-x 5,783 bytes parent folder | download | duplicates (17)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/software/bin/python

# Author: lh3, converted to python and modified to add -C option by Aylwyn Scally
#
# About:
#   varfilter.py is a port of Heng's samtools.pl varFilter script into 
#   python, with an additional -C INT option. This option sets a minimum 
#   consensus score, above which the script will output a pileup line 
#   wherever it _could have_ called a variant, even if none is actually 
#   called (i.e. hom-ref positions). This is important if you want to
#   subsequently merge the calls with those for another individual to get a
#   synoptic view of calls at each site. Without this option, and in all 
#   other respects, it behaves like samtools.pl varFilter.
#   
#   Aylwyn Scally as6@sanger.ac.uk


# Filtration code:
#
# C low CNS quality (hom-ref only)
# d low depth
# D high depth
# W too many SNPs in a window (SNP only)
# G close to a high-quality indel (SNP only)
# Q low RMS mapping quality (SNP only)
# g close to another indel with higher quality (indel only)
# s low SNP quality (SNP only)
# i low indel quality (indel only)


import sys
import getopt

def usage():
	print '''usage: varfilter.py [options] [cns-pileup]

Options: -Q INT	minimum RMS mapping quality for SNPs
		 -q INT	minimum RMS mapping quality for gaps
		 -d INT	minimum read depth 
		 -D INT	maximum read depth
		 -S INT	minimum SNP quality
		 -i INT	minimum indel quality
		 -C INT	minimum consensus quality for hom-ref sites

		 -G INT	min indel score for nearby SNP filtering
		 -w INT	SNP within INT bp around a gap to be filtered

		 -W INT	window size for filtering dense SNPs
		 -N INT	max number of SNPs in a window

		 -l INT	window size for filtering adjacent gaps

		 -p print filtered variants'''

def varFilter_aux(first, is_print):
	try:
		if first[1] == 0:
			sys.stdout.write("\t".join(first[4:]) + "\n")
		elif is_print:
			sys.stderr.write("\t".join(["UQdDWGgsiCX"[first[1]]] + first[4:]) + "\n")
	except IOError:
		sys.exit()
 
mindepth = 3
maxdepth = 100
gapgapwin = 30
minsnpmapq = 25
mingapmapq = 10
minindelscore = 25
scorefactor = 100
snpgapwin = 10
densesnpwin = 10
densesnps = 2
printfilt = False
minsnpq = 0
minindelq = 0
mincnsq = 0

try:
	options, args = getopt.gnu_getopt(sys.argv[1:], 'pq:d:D:l:Q:w:W:N:G:S:i:C:', [])
except getopt.GetoptError:
	usage()
	sys.exit(2)
for (oflag, oarg) in options:
	if oflag == '-d': mindepth = int(oarg)
	if oflag == '-D': maxdepth = int(oarg)
	if oflag == '-l': gapgapwin = int(oarg)
	if oflag == '-Q': minsnpmapq = int(oarg)
	if oflag == '-q': mingapmapq = int(oarg)
	if oflag == '-G': minindelscore = int(oarg)
	if oflag == '-s': scorefactor = int(oarg)
	if oflag == '-w': snpgapwin = int(oarg)
	if oflag == '-W': densesnpwin = int(oarg)
	if oflag == '-C': mincnsq = int(oarg)
	if oflag == '-N': densesnps = int(oarg)
	if oflag == '-p': printfilt = True
	if oflag == '-S': minsnpq = int(oarg)
	if oflag == '-i': minindelq = int(oarg)

if len(args) < 1:
	inp = sys.stdin
else:
	inp = open(args[0])

# calculate the window size
max_dist = max(gapgapwin, snpgapwin, densesnpwin)

staging = []
for t in (line.strip().split() for line in inp):
	(flt, score) = (0, -1)
	# non-var sites
	if t[3] == '*/*':
		continue
	is_snp = t[2].upper() != t[3].upper()
	if not (is_snp or mincnsq):
		continue
	# clear the out-of-range elements
	while staging:
		# Still on the same chromosome and the first element's window still affects this position?  
		if staging[0][4] == t[0] and int(staging[0][5]) + staging[0][2] + max_dist >= int(t[1]):
			break
		varFilter_aux(staging.pop(0), printfilt)
	
	# first a simple filter
	if int(t[7]) < mindepth:
		flt = 2
	elif int(t[7]) > maxdepth:
		flt = 3
	if t[2] == '*': # an indel
		if minindelq and minindelq > int(t[5]):
			flt = 8
	elif is_snp:
		if minsnpq and minsnpq> int(t[5]):
			flt = 7
	else:
		if mincnsq and mincnsq > int(t[4]):
			flt = 9

	# site dependent filters
	dlen = 0
	if flt == 0:
		if t[2] == '*': # an indel
			# If deletion, remember the length of the deletion
			(a,b) = t[3].split('/')
			alen = len(a) - 1
			blen = len(b) - 1
			if alen>blen:
				if a[0] == '-': dlen=alen 
			elif b[0] == '-': dlen=blen 

			if int(t[6]) < mingapmapq:
				flt = 1
			# filtering SNPs
			if int(t[5]) >= minindelscore:
				for x in (y for y in staging if y[3]):
					# Is it a SNP and is it outside the SNP filter window?
					if x[0] >= 0 or int(x[5]) + x[2] + snpgapwin < int(t[1]):
						continue
					if x[1] == 0:
						x[1] = 5
			
			# calculate the filtering score (different from indel quality)
			score = int(t[5])
			if t[8] != '*':
				score += scorefactor * int(t[10])
			if t[9] != '*':
				score += scorefactor * int(t[11])
			# check the staging list for indel filtering
			for x in (y for y in staging if y[3]):
			  # Is it a SNP and is it outside the gap filter window
				if x[0] < 0 or int(x[5]) + x[2] + gapgapwin < int(t[1]):
					continue
				if x[0] < score:
					x[1] = 6
				else:
					flt = 6
					break
		else: # a SNP or hom-ref
			if int(t[6]) < minsnpmapq:
				flt = 1
			# check adjacent SNPs
			k = 1
			for x in (y for y in staging if y[3]):
				if x[0] < 0 and int(x[5]) + x[2] + densesnpwin >= int(t[1]) and (x[1] == 0 or x[1] == 4 or x[1] == 5):
					k += 1
			
			# filtering is necessary
			if k > densesnps:
				flt = 4
				for x in (y for y in staging if y[3]):
					if x[0] < 0 and int(x[5]) + x[2] + densesnpwin >= int(t[1]) and x[1] == 0:
						x[1] = 4
			else: # then check gap filter
				for x in (y for y in staging if y[3]):
					if x[0] < 0 or int(x[5]) + x[2] + snpgapwin < int(t[1]):
						continue
					if x[0] >= minindelscore:
						flt = 5
						break
	
	staging.append([score, flt, dlen, is_snp] + t)
  
# output the last few elements in the staging list
while staging:
	varFilter_aux(staging.pop(0), printfilt)