-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
481 lines (441 loc) · 21.4 KB
/
utils.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import bisect
import csv
import string
import pysam
import numpy as np
MATCH_SCORE = 1 # score for a match
MISMATCH_SCORE = -6 # score penalty for a mismatch
FAIL_SCORE = -1 # score value to stop searching
MIN_SCORE = 4 # minimum score value to pass. The minimum length of MS repeats
# https://stackoverflow.com/questions/212358/binary-search-bisection-in-python
def binary_search(a, x, lo=0,
hi=None): # can't use a to specify default for hi
hi = hi if hi is not None else len(a) # hi defaults to len(a)
pos = bisect.bisect_left(a, x, lo, hi) # find insertion position
return (pos if pos != hi and a[pos] == x else -1)
def find_repeats(seq, flank_size, repeat_units, min_score=MIN_SCORE):
bases = len(seq)
flank_size = flank_size - 1
# save output as a list of lists
out = [];
exclude = set() # use sets: they are much faster to apply 'in'
for ru in repeat_units:
positions_motif = range(0, ru)
nb_positions_motif = len(positions_motif)
not_found = True
base = flank_size
while base < bases - flank_size: # and base not in exclude:
if base in exclude:
base += 1
continue
elif not_found:
test_pos = base + ru
current_pos = base
else:
current_pos = base
not_found = True
test_pos = test_pos + ru
pos_in_motif = 0
score = 0;
depth = 0;
keep = 0
max_observed_score = 0
scores = []
while (
(test_pos) < (
bases - flank_size)) and score > FAIL_SCORE and \
test_pos not in exclude:
match = (seq[current_pos + pos_in_motif] == seq[test_pos])
if match:
test_pos += 1
pos_in_motif = positions_motif[
(pos_in_motif + 1) % nb_positions_motif]
score += MATCH_SCORE
scores.append(score)
depth = 0
else:
score += MISMATCH_SCORE
scores.append(score)
pos_in_motif = positions_motif[
(pos_in_motif + 1) % nb_positions_motif]
if score > FAIL_SCORE and depth < 5:
depth += 1
test_pos += 1
# keep track of the best observed score
if score > max_observed_score:
max_observed_score = score
# debugging
# print "RU current_pos pos_in_motif test_pos bases, score"
# print ru, current_pos, pos_in_motif, test_pos, bases,score,"\n" #, current_pos + pos_in_motif, bases
if max_observed_score >= min_score: # and test_pos <= bases-flank_size:
mm = scores.index(max(scores))
mm = mm + ru
if base + mm < (
bases - flank_size): # repeat not overlapping flanking region
out.append([ru, base, base + mm, seq[base:base + mm + 1]])
not_found = False
exclude.update(range(base, base + mm + 1))
test_pos = base + mm
base = test_pos
else:
pass
base += 1
else:
base += 1
return out
def find_repeats_target(seq, flank_size, repeat_units):
MATCH_SCORE = 1 # score for a match
MISMATCH_SCORE = -6 # score penalty for a mismatch
FAIL_SCORE = -1 # score value to stop searching
MIN_SCORE = 4 # minimum score value to pass. The minimum length of MS repeats
bases = len(seq) # number of bases in the input sequence
# save output as a list of lists
out = []
# use sets: they are much faster with 'in'[]#np.array([], dtype='int')
exclude = set()
for ru in [repeat_units]:
positions_motif = range(0, ru)
nb_positions_motif = len(positions_motif)
# note that the flank is a range, whether python is zero-based
not_found = True
base = flank_size
while base < bases - flank_size: # and base not in exclude:
if base in exclude:
base += 1
continue
elif not_found:
test_pos = base + ru
current_pos = base
else:
current_pos = base
not_found = True
test_pos = test_pos + ru
pos_in_motif = 0 # update_current_pos(ru)
score = 0
depth = 0
max_observed_score = 0
scores = []
while ((test_pos) < (bases - flank_size)) and \
score > FAIL_SCORE and \
test_pos not in exclude: # XX the minus one check
match = (seq[current_pos + pos_in_motif] == seq[test_pos])
if match:
test_pos += 1
pos_in_motif = positions_motif[
(pos_in_motif + 1) % nb_positions_motif]
score += MATCH_SCORE
scores.append(score)
depth = 0
# no mismatch: check for N, insertions, deletions and missense
else:
score += MISMATCH_SCORE
scores.append(score)
pos_in_motif = positions_motif[
(pos_in_motif + 1) % nb_positions_motif]
if score > FAIL_SCORE and depth < 5:
depth += 1
test_pos += 1
# keep track of the best observed score
if score > max_observed_score:
max_observed_score = score
test_pos = test_pos # - depth
if max_observed_score >= MIN_SCORE:
mm = scores.index(max(scores))
mm = mm + ru
out.append([ru, base, base + mm,
seq[base:base + mm + 1]])
not_found = False
exclude.update(range(base, base + mm + 1))
test_pos = base + mm
base = test_pos
# increment base
base += 1
else:
base += 1
return out
def loadcsv(filename, criterion1, criterion2, repeat_units):
with open(filename, "rb") as csvfile:
datareader = csv.reader(csvfile, delimiter="\t")
for row in datareader:
if int(row[5]) >= criterion1 and int(row[5]) <= criterion2 and \
int(row[4]) in repeat_units:
yield row
def phased(msi_obj, sites, bam_path):
with pysam.AlignmentFile(bam_path, "rb") as bamfile:
dict_out = {}
for site in sites:
start = int(site[1])
end = int(site[2])
chr = site[0]
#chr = str(site[0])
bases = [site[3], site[4]]
reads = [
read for read in bamfile.fetch(
chr,
start,
end,
multiple_iterators=True
)
] ## keep this as is, do not put conditions inside here
reads = [
read for read in reads if read.is_proper_pair and
read.is_duplicate == False and
read.mapping_quality >= msi_obj.mapping_quality
]
if len(reads) > msi_obj.min_coverage:
for read in reads:
read_sequence = read.seq
# read_sequence = read.query_alignment_sequence
reps = find_repeats(
read_sequence,
msi_obj.flank_size,
msi_obj.repeat_units
)
if len(reps) > 0:
# get the SNP allele in this read
start_read = read.reference_start
end_read = read.reference_end
aligned_pos = read.get_reference_positions(
full_length=True
) # True) reports none for soft-clipped positions
try:
idx = aligned_pos.index(start)
except:
continue
snp_read = read_sequence[idx]
if snp_read not in bases:
continue
for microsatellite in reps:
rs = microsatellite[1]
re = microsatellite[2]
difference = re - rs + 1
# use the reference set here to get the
# position on the right
ini = start_read + rs #
#chr=string.strip(chr,"chr")
chr=chr.strip("chr")
idx2 = binary_search(
msi_obj.reference_set_ini_end_dict[chr],
str(ini + 1)
)
if idx2 == -1:
continue
refset_now = msi_obj.reference_set_dict[chr][idx2]
diff_ref = int(refset_now[2]) - int(
refset_now[1]) + 1
with pysam.FastaFile(
filename=msi_obj.fasta_dict[chr]
) as fasta_file:
flank_right_ref = fasta_file.fetch(
"chr" + str(site[0]), ini + diff_ref, #XX
ini + diff_ref + msi_obj.flank_size).upper()
flank_left_ref = fasta_file.fetch(
"chr" + str(site[0]), ini - #XX
msi_obj.flank_size,
ini).upper()
posfl = (start_read + rs - msi_obj.flank_size)
if posfl >= start_read:
flank_left = read_sequence[
rs - msi_obj.flank_size:rs]
mismatches_left = sum(a != b for a, b in
zip(flank_left,
flank_left_ref))
else:
mismatches_left = 10000
posflr = start_read + re + msi_obj.flank_size
if posflr <= end_read:
flank_right = read_sequence[
re + 1:re + 1 +
msi_obj.flank_size]
mismatches_right = sum(a != b for a, b in
zip(flank_right,
flank_right_ref))
else:
mismatches_right = 10000
mismatches = mismatches_left + mismatches_right
if mismatches <= msi_obj.tolerated_mismatches:
key_now = site[0] + "\t" + str(ini) + "\t" + \
refset_now[3] + "\t" + refset_now[
4] + "\t" + refset_now[
5] + "\t" + refset_now[
6] + "\t" + snp_read + "\t" + \
str(site[1])
if dict_out.has_key(key_now):
dict_out[key_now] = np.append(
dict_out[key_now], difference)
else:
dict_out[key_now] = difference
return dict_out
#@profile
def unphased(msi_obj, sites, bam_path):
with pysam.AlignmentFile(bam_path, "rb") as bamfile:
dict_out = {}
visited_reads = []
for site in sites:
start = int(site[1])
#end = int(site[2]) + 1
chr = site[0]
ru = int(site[4])
reads = [read for read in bamfile.fetch(chr, start, start + 1,
multiple_iterators=True)]
reads = [read for read in reads if
read.is_proper_pair and read.is_duplicate == False and
read.mapping_quality >= msi_obj.mapping_quality]
if len(reads) > msi_obj.min_coverage:
for read in reads:
start_read = read.reference_start;
end_read = read.reference_end
read_sequence = read.seq
reps = find_repeats_target(read_sequence,
msi_obj.flank_size,
ru)
if len(reps) > 0:
aligned_pos = read.get_reference_positions(
full_length=True)
try:
idx = aligned_pos.index(start)
except:
continue
for microsatellite in reps:
ru = microsatellite[0];
rs = microsatellite[1];
re = microsatellite[2]
if start != start_read + rs + 1: # do not consider if there are ins/del upstream of the repeat
continue
difference = re - rs + 1
#chr=string.strip(chr,"chr")
chr=chr.strip("chr")
# get flinking sequence from reference
fasta_file = pysam.FastaFile(
filename=msi_obj.fasta_dict[chr]
)
flank_left_ref = fasta_file.fetch(
"chr" + chr, #string.strip(chr,"chr"), #X
start_read + rs -
msi_obj.flank_size,
start_read + rs).upper()
flank_right_ref = fasta_file.fetch("chr" + chr, #string.strip(chr,"chr"), #X
int(site[
2]) - 1,
int(site[
2]) - 1
+ msi_obj.flank_size).upper()
# get flinking sequence from the reads
posfl = (start_read + rs - msi_obj.flank_size)
if posfl >= start_read:
flank_left = read_sequence[
rs - msi_obj.flank_size:rs];
mismatches_left = sum(a != b for a, b in
zip(flank_left,
flank_left_ref))
else:
flank_left = "";
mismatches_left = 10000
posflr = start_read + re + msi_obj.flank_size
if posflr <= end_read:
flank_right = read_sequence[
re:re + msi_obj.flank_size];
mismatches_right = sum(a != b for a, b in
zip(flank_right,
flank_right_ref))
else:
flank_right = "";
mismatches_right = 10000
mismatches = mismatches_left + mismatches_right
if mismatches <= msi_obj.tolerated_mismatches:
key_now = site[0] + "\t" + site[1] + "\t" + \
site[
2] + "\t" + site[3] + "\t" + \
site[4] + "\t" + \
site[5] + "\t" + site[6]
if dict_out.has_key(key_now):
dict_out[key_now] = np.append(
dict_out[key_now], difference)
else:
dict_out[key_now] = difference
return dict_out
def multiprocessing_lock_init(l):
global lock
lock = l
rus={1,2,3,4,5,6} # repeat units considered
match_score = 1 # score for a match
mismatch_score = -6 # score penalty for a mismatch # it detects min length of abs(mismatch_score) + 1
fail_score = -1 # score value to stop search at a given current_pos
#min_score = 5 ##minimum score value to pass; which corresponds to length 6 for mono
def find_repeats_reference(seq,flank_size,chromo,min_score):
MATCH_SCORE = 1 # score for a match
MISMATCH_SCORE = -6 # score penalty for a mismatch
FAIL_SCORE = -1 # score value to stop searching
MIN_SCORE = 4 # minimum score value to pass. The minimum length of MS repeats
cigar = seq #XX change
bases=len(seq) # number of bases in the input sequence
# save output as a list of lists
out = []; exclude=set() # use sets: they are much faster with 'in'[]#np.array([],dtype='int')
for ru in rus:
positions_motif = range(0,ru)
nb_positions_motif = len(positions_motif)
# note that the flank is a range, whether python is zero-based
not_found = True
base = flank_size
while base < bases-flank_size: #and base not in exclude:
#print base, "BASE"
if base in exclude:
base+=1
continue
elif not_found:# and base != flank_size:
# base=test_pos+1
test_pos=base+ru#+1flank_size
current_pos=base
else:
#base=1+base+mm#test_pos#+1
#print base
current_pos=base
not_found=True
test_pos=test_pos+ru#+1#2
pos_in_motif = 0 #update_current_pos(ru)
score = 0; depth = 0; keep = 0
max_observed_score = 0
scores = []
while ( (test_pos ) < (bases-flank_size) ) and score > fail_score and test_pos not in exclude: #XX the minus one check
#while score > fail_score and test_pos not in exclude: #XX the minus one check
#print base, test_pos," ", score, max_observed_score, seq[base:test_pos], exclude, bases-flank_size, "RU",ru
#print base, " ", score, max_observed_score, seq[base:test_pos], depth, exclude, bases-flank_size, "RU",ru
match = (seq[current_pos + pos_in_motif] == seq[test_pos])
if match:
test_pos+=1
pos_in_motif = positions_motif[(pos_in_motif + 1) % nb_positions_motif]
score+=match_score
scores.append(score)
depth = 0
else: # no mismatch: check for N, insertions, deletions and missense
score+=mismatch_score
scores.append(score)
pos_in_motif = positions_motif[(pos_in_motif + 1) % nb_positions_motif]
if score > fail_score and depth < 5:
depth+=1
test_pos +=1
# keep track of the best observed score
if score > max_observed_score:
max_observed_score = score
#print "RU current_pos pos_in_motif test_pos bases, score"
#print ru, current_pos, pos_in_motif, test_pos, bases,score,"\n" #, current_pos + pos_in_motif, bases
#if test_pos >= bases-flank_size: ## nos metemos en el flaking de la derecha
#print base, current_pos,test_pos, bases-flank_size, "test pos bases-flank_size"
# max_observed_score=-100
test_pos = test_pos #- depth
if max_observed_score >= min_score:# and test_pos <= bases-flank_size:
mm = scores.index(max(scores))
mm = mm +ru
seq_now = seq[base:base+mm+1]
len_seq = len(seq_now)
if len_seq <= 60:
out.append( [chromo,base+1, base+mm+1, seq_now,ru, len_seq])#test_pos]] ) ## chr, start, end, seq, ru, diff
not_found = False
exclude.update(range(base,base+mm+1)) #np.unique(np.append(exclude,np.arange(base,test_pos)))
test_pos = base + mm ##X
base = test_pos
# increment base
base+=1
else:
base+=1
return out