-
Notifications
You must be signed in to change notification settings - Fork 9
/
caller.py
executable file
·296 lines (245 loc) · 10.2 KB
/
caller.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
# caller.py
# Python 2/3 friendly
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
# Imports
import os
import sys
import vcf
import h5py
import time
import pysam
import models
import defines
import operator
import arguments
import numpy as np
import training_data as td
import inference as inf
from Bio import Seq, SeqIO
from collections import Counter, defaultdict
def run():
args = arguments.parse_args()
args.labels = defines.calling_labels
if args.mode == 'tensors':
infer_tensor(args)
elif args.mode == 'vcf':
infer_vcf(args)
else:
raise ValueError('Unknown calling mode:', args.mode)
def infer_tensor(args):
stats = Counter()
model = models.load_model(args.weights_hd5, custom_objects=models.get_all_custom_objects(args.labels))
vcf_reader = pysam.VariantFile(args.negative_vcf, 'r')
vcf_writer = pysam.VariantFile(args.output_vcf, 'w', header=vcf_reader.header)
print('got vcfs.')
tensor_paths = [args.data_dir+tp for tp in sorted(os.listdir(args.data_dir))]
print('found tensors: ', len(tensor_paths))
tensor_batch = np.zeros((args.batch_size,)+defines.tensor_shape_from_args(args))
gpos_batch = []
for tp in tensor_paths:
with h5py.File(tp, 'r') as hf:
tensor_batch[stats['cur_tensor']] = np.array(hf.get(args.tensor_map))
gpos_batch.append(td.position_string_from_tensor_name(tp).split('_'))
stats['cur_tensor'] += 1
if stats['cur_tensor'] == args.batch_size:
## Evaluate the model
predictions = model.predict(tensor_batch) # predictions is a numpy arra
predictions_to_variants(args, predictions, gpos_batch, tensor_batch, vcf_writer)
stats['cur_tensor'] = 0
gpos_batch = []
def infer_vcf(args):
stats = Counter()
model = models.load_model(args.weights_hd5, custom_objects=models.get_all_custom_objects(args.labels))
vcf_reader = pysam.VariantFile(args.negative_vcf, 'r')
vcf_writer = pysam.VariantFile(args.output_vcf, 'w', header=vcf_reader.header)
print('got vcfs.')
reference = SeqIO.to_dict(SeqIO.parse(args.reference_fasta, "fasta"))
print('Loaded reference FASTA:', args.reference_fasta)
samfile = pysam.AlignmentFile(args.bam_file, "rb")
print('got sam.')
if args.chrom:
intervals = { args.chrom : [int(args.start_pos), int(args.end_pos)] }
elif args.bed_file:
intervals = td.bed_file_to_dict(args.bed_file)
else:
raise ValueError('What do you want to iterate over? Use arguments --bed_file or --chrom --start_pos --end_pos')
tensor_batch = np.zeros((args.batch_size,)+defines.tensor_shape_from_args(args))
gpos_batch = []
print(len(intervals), 'intervals to iterate over, contigs:', intervals.keys())
start_time = time.time()
for k in intervals:
contig = reference[k]
args.chrom = k
for start,stop in zip(intervals[k][0], intervals[k][1]):
cur_pos = start
for cur_pos in range(start, stop, args.window_size):
record = contig[cur_pos: cur_pos+args.window_size]
t = td.make_calling_tensor(args, samfile, record, cur_pos, stats)
if not t is None:
tensor_batch[stats['cur_tensor']] = t
gpos_batch.append((k, cur_pos, record))
stats['cur_tensor'] += 1
if stats['cur_tensor'] == args.batch_size:
predictions = model.predict(tensor_batch) # predictions is a numpy arra
predictions_to_variants(args, predictions, gpos_batch, tensor_batch, vcf_writer, contig)
tensor_batch = np.zeros((args.batch_size,)+defines.tensor_shape_from_args(args))
stats['cur_tensor'] = 0
stats['batches_processed'] += 1
gpos_batch = []
if stats['batches_processed'] % 100 == 0:
elapsed = time.time() - start_time
t_per_minute = stats['batches_processed']*args.batch_size / (elapsed/60)
print('At genomic position:', k, cur_pos, 'Tensors per minute:', t_per_minute,'Batches processed:', stats['batches_processed'])
for s in stats.keys():
print(s, 'has:', stats[s])
for s in stats.keys():
print(s, 'has:', stats[s])
def predictions_to_variants(args, predictions, gpos_batch, tensor_batch, vcf_writer, contig=None):
index2labels = {v:k for k,v in defines.calling_labels.items()}
indel_start = -1
ref_offset = 0
for i,gpos in enumerate(gpos_batch):
guess = np.argmax(predictions[i], axis=1)
cur_tensor = tensor_batch[i]
for j in range(guess.shape[0]):
if index2labels[guess[j]] == 'REFERENCE':
continue
ref_start = int(gpos[1])-ref_offset
# Does NOT properly handle multiallelics
if contig is not None:
ref_allele = str(contig[ref_start+j])
if ref_allele == 'N':
continue
else:
ref_allele = reference_base_from_tensor(args, cur_tensor, j)
alt = strongest_alt_allele_from_tensor(args, cur_tensor, j, ref_allele)
is_indel = 'DELETION' in index2labels[guess[j]] or 'INSERTION' in index2labels[guess[j]]
if is_indel and indel_start == -1:
indel_start = j
if index2labels[guess[j]] == 'HET_SNP':
v = vcf_writer.new_record(contig=gpos[0],
start=ref_start+j,
stop=ref_start+j+1,
alleles=[ref_allele, alt],
qual=predictions[i][j][guess[j]])
elif index2labels[guess[j]] == 'HOM_SNP':
v = vcf_writer.new_record(contig=gpos[0],
start=ref_start+j,
stop=ref_start+j+1,
alleles=[ref_allele, alt],
qual=predictions[i][j][guess[j]])
elif index2labels[guess[j]] == 'HET_DELETION' and variant_edge(index2labels, guess, j):
d = str(contig[ref_start+indel_start-1:ref_start+indel_start+(j-indel_start)+2].seq)
if len(d) < 2 or d[0] == 'N':
continue
v = vcf_writer.new_record(contig=gpos[0],
start=ref_start+indel_start-1,
stop=ref_start+indel_start+(j-indel_start)+2,
alleles=[d, d[0]],
qual=predictions[i][j][guess[j]])
indel_start = -1
elif index2labels[guess[j]] == 'HOM_DELETION' and variant_edge(index2labels, guess, j):
d = str(contig[ref_start+indel_start-1:ref_start+indel_start+(j-indel_start)+2].seq)
if len(d) < 2 or d[0] == 'N':
continue
v = vcf_writer.new_record(contig=gpos[0],
start=ref_start+indel_start-1,
stop=ref_start+indel_start+(j-indel_start)+2,
alleles=[d, d[0]],
qual=predictions[i][j][guess[j]])
indel_start = -1
elif index2labels[guess[j]] == 'HOM_INSERTION' and variant_edge(index2labels, guess, j):
if contig:
ref = str(contig[(ref_start+indel_start)-1])
if ref == 'N':
continue
else:
ref = reference_base_from_tensor(args, cur_tensor, j)
if ref == defines.indel_char:
print('Looked for reference but found only insertions at:', (ref_offset+(indel_start-1)))
insert = get_inserted(args, cur_tensor, indel_start, j)
v = vcf_writer.new_record(contig=gpos[0],
start=ref_start+indel_start,
stop=ref_start+indel_start+1,
alleles=[ref, insert],
qual=predictions[i][j][guess[j]])
ref_offset += j-indel_start
indel_start = -1
elif index2labels[guess[j]] == 'HET_INSERTION' and variant_edge(index2labels, guess, j):
if contig:
ref = str(contig[(ref_start+indel_start)-1])
if ref == 'N':
continue
else:
ref = reference_base_from_tensor(args, cur_tensor, j)
if ref == defines.indel_char:
print('Looked for reference but found only insertions at:', (ref_offset+(indel_start-1)))
insert = get_inserted(args, cur_tensor, indel_start, j)
v = vcf_writer.new_record(contig=gpos[0],
start=ref_start+indel_start,
stop=ref_start+indel_start+1,
alleles=[ref, insert],
qual=predictions[i][j][guess[j]])
ref_offset += j-indel_start
indel_start = -1
else:
continue
vcf_writer.write(v)
def variant_edge(index2labels, guess, j):
return (j == guess.shape[0]-1) or (index2labels[guess[j]] != index2labels[guess[j+1]])
def get_deleted(args, tensor, indel_start, j):
return ''.join([reference_base_from_tensor(args, tensor, deleted_i) for deleted_i in range(indel_start-1, j+1)])
def get_inserted(args, tensor, indel_start, j):
return ''.join([strongest_allele_from_tensor(args, tensor, tensor_site) for tensor_site in range(indel_start-1, j+1)])
def reference_base_from_tensor(args, tensor, tensor_site):
channels = defines.get_tensor_channel_map_from_args(args)
for c in channels:
if c[-1] != defines.indel_char and 'reference' in c:
if args.channels_last and tensor[0, tensor_site, channels[c]] > 0:
return c[-1].upper() # reference channels are strings like reference_A or reference_C
# Here we want just the nucleic acid.
elif not args.channels_last and tensor[channels[c], 0, tensor_site] > 0:
return c[-1].upper()
return defines.indel_char # No evidence of reference, insertion perhaps
def reference_base_from_tensor(args, tensor, tensor_site):
channels = defines.get_tensor_channel_map_from_args(args)
for c in channels:
if c[-1] != defines.indel_char and 'reference' in c:
if args.channels_last and tensor[0, tensor_site, channels[c]] > 0:
return c[-1].upper() # reference channels are strings like reference_A or reference_C
# Here we want just the nucleic acid.
elif not args.channels_last and tensor[channels[c], 0, tensor_site] > 0:
return c[-1].upper()
return defines.indel_char # No evidence of reference, insertion perhaps
def strongest_allele_from_tensor(args, tensor, tensor_site):
channels = defines.get_tensor_channel_map_from_args(args)
max_count = -1
strongest_allele = 'N'
for c in channels:
if c[-1] != defines.indel_char and 'read' in c:
if args.channels_last:
cur_count = np.sum(tensor[:, tensor_site, channels[c]])
else:
cur_count = np.sum(tensor[channels[c], :, tensor_site])
if cur_count > max_count:
max_count = cur_count
strongest_allele = c[-1].upper()
return strongest_allele
def strongest_alt_allele_from_tensor(args, tensor, tensor_site, ref_allele):
channels = defines.get_tensor_channel_map_from_args(args)
max_count = -1
strongest_allele = 'N'
for c in channels:
if c[-1] != defines.indel_char and 'read' in c:
if args.channels_last:
cur_count = np.sum(tensor[:, tensor_site, channels[c]])
else:
cur_count = np.sum(tensor[channels[c], :, tensor_site])
if cur_count > max_count and c[-1].upper() != ref_allele:
max_count = cur_count
strongest_allele = c[-1].upper()
return strongest_allele
if __name__=='__main__':
run()