-
Notifications
You must be signed in to change notification settings - Fork 27
/
kmer_count_filter.py
354 lines (301 loc) · 11.3 KB
/
kmer_count_filter.py
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# @file kmer_count_filter.py
# @author Esko Kautto ([email protected])
# @updated 2018-05-23
import os
import argparse
import math
import numpy
from datetime import datetime, timedelta
from helpers import iteritems, timestamp, tprint
class MANTIS_Filter(object):
class Locus(object):
def __init__(self):
self.locus = ''
self.k = set()
self.n = {}
self.t = {}
def add(self, line):
if type(line) is str:
line = line.strip().split('\t')
if self.locus == '':
self.locus = line[0]
elif line[0] != self.locus:
tprint('MANTIS_Filter.Locus error: Fed data for ' +
'{0} to locus {1}!'.format(line[0], self.locus))
return False
k = int(line[1])
n = int(line[2])
t = int(line[3])
if k in self.k:
tprint('MANTIS_Filter.Locus error: Duplicate entry for ' +
'repeat count {0}!'.format(k))
return False
self.k.add(k)
self.n[k] = n
self.t[k] = t
return True
# end .add()
"""
Generates output data so locus can be written into output file.
"""
def generate_output(self):
output = []
for k in sorted(self.k):
n = 0
t = 0
if k in self.n:
n = self.n[k]
if k in self.t:
t = self.t[k]
if (n + t) > 0:
line = [self.locus, str(k), str(n), str(t)]
output.append(line)
if len(output) == 0:
# Nothing to output.
return False
return output
# end .generate_output()
"""
Checks if both the normal and tumor data have
the minimum required coverage.
"""
def has_min_coverage(self, min_coverage):
if (sum(self.n.values()) < min_coverage) \
or (sum(self.t.values()) < min_coverage):
return False
return True
# end .has_min_coverage()
"""
Generates a list of values from the repeat counts and
support for each repeat count.
"""
def generate_value_list(self,data):
values = []
# Iterate through the data and only record values
# for k-repeats that have some support
for k, count in iteritems(data):
if count > 0:
values += [k] * count
return sorted(values)
# end .generate_value_list()
"""
Filters the data for outliers. The normal and tumor
data are treated as separate sets for the outlier
calculations, since each will likely have a different
mean if the locus is unstable.
"""
def outlier_filter(self, sds):
# Process each subset of data separately.
self.n = self.subset_outlier_filter(self.n, sds)
self.t = self.subset_outlier_filter(self.t, sds)
self.remove_unsupported_ks()
self.regenerate_k_set()
return True
# end .outlier_filter()
"""
Zeroes out any k-value repeat counts that don't meet the
minimum amount of supported reads; mainly gets rid of
one-off values that are likely to only exist due to
sequencing or alignment errors in the reads.
"""
def support_filter(self, min_support):
for k in self.k:
n_support = False
t_support = False
if k in self.n and self.n[k] >= min_support:
n_support = True
if k in self.t and self.t[k] >= min_support:
t_support = True
if not n_support and not t_support:
# Neither subset had required support.
self.n[k] = 0
self.t[k] = 0
self.remove_unsupported_ks()
self.regenerate_k_set()
# end .support_filter()
"""
Regenerates the set of k-repeat values based on the
existing normal and tumor data.
"""
def regenerate_k_set(self):
self.k = set()
for k in self.n.keys():
self.k.add(k)
for k in self.t.keys():
self.k.add(k)
return True
# end .regenerate_k_set()
"""
Removes any k-repeat values that no longer have any support.
"""
def remove_unsupported_ks(self):
unsupported = set()
for k in self.k:
n = 0
t = 0
if k in self.n:
n = self.n[k]
if k in self.t:
t = self.t[k]
if (n + t) == 0:
# Has no support
unsupported.add(k)
if len(unsupported):
for k in unsupported:
self.k.discard(k)
return True
# end .remove_unsupported_ks()
"""
Keeps only the values that fall within the acceptable
window, removing outliers.
"""
def subset_outlier_filter(self, data, sds):
values = self.generate_value_list(data)
output = {}
if len(values):
mean = numpy.mean(values)
std = numpy.std(values)
# Round the min and max for the window to allow for
# some leniency in the filter.
min_k = int(math.floor(mean - (sds * std)))
max_k = int(math.ceil(mean + (sds * std)))
for k, count in iteritems(data):
if min_k <= k <= max_k:
# Acceptable
output[k] = count
return output
# end .subset_outlier_filter()
# end MANTIS_Filter.Locus subclass definition
def __init__(self, config):
self.min_locus_coverage = config['min_locus_coverage']
self.min_repeat_reads = config['min_repeat_reads']
self.standard_deviations = config['standard_deviations']
# end .__init__()
"""
Main filtering function that calls all other methods
required by the filter.
"""
def filter(self, input_filepath, output_filepath):
self.load_loci(input_filepath)
self.filtered_loci = {}
for locus_name, locus in iteritems(self.loci):
locus = self.filter_locus(locus)
if locus is not False:
self.filtered_loci[locus_name] = locus
self.write_output(output_filepath)
# end .filter()
"""
Runs the different locus filters on the locus instance.
"""
def filter_locus(self, locus):
locus.outlier_filter(self.standard_deviations)
locus.support_filter(self.min_repeat_reads)
if not locus.has_min_coverage(self.min_locus_coverage):
return False
return locus
# end .filter_locus()
"""
Writes output data into specified file, iterating
through each (filtered) locus, generating the output
data for it and writing it out.
"""
def write_output(self, output_filepath):
fileout = open(output_filepath, 'w')
header = ['Locus', 'Repeats', 'Normal', 'Tumor']
fileout.write('\t'.join(header) + '\n')
for l in self.ordered_loci:
if l in self.filtered_loci:
locus = self.filtered_loci[l]
output = locus.generate_output()
for line in output:
fileout.write('\t'.join(line) + '\n')
fileout.close()
return True
# end .write_output()
"""
Loads the data from the input file and stores the per-locus
data for filtering.
"""
def load_loci(self, input_filepath):
self.loci = {}
self.ordered_loci = []
with open(input_filepath, 'Ur') as f:
header = True
for line in f:
if header:
# Make sure this is a header line so we don't
# discard valid data.
if self.line_is_header(line):
# Line is header line, skip
continue
line = line.strip().split('\t')
if line[0] not in self.loci:
self.loci[line[0]] = MANTIS_Filter.Locus()
self.ordered_loci.append(line[0])
self.loci[line[0]].add(line)
# end .load_loci()
"""
Tries to determine if line is a header line based on column values.
"""
def line_is_header(self, line):
line = line.upper().strip().split('\t')
if len(line) != 4:
# Malformed line, definitely not data.
return True
header = ['LOCUS', 'REPEATS', 'NORMAL', 'TUMOR']
is_header = True
for i in range(0, 4):
if line[i] != header[i]:
# Unexpected token.
is_header = False
break
return is_header
# end .line_is_header()
# end MANTIS_Filter class definition
"""
Attempts to filter out undesirable noise in the filter, in
an attempt to compare results with more confidence. The filter
will try to get rid of reads that are obvious outliers, loci that
don't meet a minimum coverage depth requirement, and repeat counts
that don't have enough supporting reads.
"""
if __name__ == "__main__":
prog_name = 'MSI Locus Kmer Counter Filter'
tprint(prog_name)
parser = argparse.ArgumentParser(description=prog_name)
parser.add_argument('-i', '--input', dest='input', type=str, required=True,
help='Input file (.kmer_counts)')
parser.add_argument('-o', '--output', dest='output', type=str, required=True,
help='Output filename.')
parser.add_argument('-mlc', '--min-locus-coverage', dest='mlc', type=int,
default=20, help='Minimum coverage required for each of the normal ' +
'and tumor results.')
parser.add_argument('-mrr', '--min-repeat-reads', dest='mrr', type=int,
default=5, help='Minimum reads supporting a specific repeat count.')
parser.add_argument('-sd', '--standard-deviations', dest='sd', type=float,
default=3.0, help='Standard deviations from mean before repeat count is ' +
'considered an outlier')
args = parser.parse_args()
input_filepath = os.path.abspath(args.input)
if not os.path.isfile(input_filepath):
tprint('Error: Input file {0} does not exist!'.format(input_filepath))
exit(1)
output_filepath = os.path.abspath(args.output)
if args.mlc < 0:
tprint('Error: Minimum locus coverage cannot be below 0.')
exit(1)
if args.mrr < 0:
tprint('Error: Minimum read count cannot be below 0.')
exit(1)
if args.sd < 0.0:
tprint('Error: Standard deviation count cannot be below 0.')
exit(1)
config = {
'min_locus_coverage': int(args.mlc),
'min_repeat_reads': int(args.mrr),
'standard_deviations': float(args.sd)
}
locus_filter = MANTIS_Filter(config)
locus_filter.filter(input_filepath, output_filepath)
# Done.
exit(0)