-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlabel_windows.py
466 lines (355 loc) · 16.1 KB
/
label_windows.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
import argparse
import gzip
import json
import logging
import os
import sys
from collections import Counter, defaultdict
from time import time
import pysam
from intervaltree import IntervalTree
from functions import *
from label_classes import SVRecord
def read_vcf(invcf):
# Check file existence
assert os.path.isfile(invcf), invcf + ' not found!'
# Dictionary with chromosome keys to store SVs
sv_list = []
vcf_in = pysam.VariantFile(invcf, 'r')
for rec in vcf_in.fetch():
var = SVRecord(rec, 'gridss')
chrom1 = var.chrom
pos1_start = var.start + var.cipos[0]
pos1_end = var.start + var.cipos[1] + 1
chrom2 = var.chrom2
pos2_start = var.end + var.ciend[0]
pos2_end = var.end + var.ciend[1] + 1
svtype = var.svtype
sv_list.append((chrom1, pos1_start, pos1_end, chrom2, pos2_start,
pos2_end, svtype))
logging.info('{} SVs'.format(len(sv_list)))
return sv_list
def read_bedpe(inbedpe, svtype_to_select):
# Check file existence
assert os.path.isfile(inbedpe), inbedpe + ' not found!'
# Dictionary with chromosome keys to store SVs
sv_list = []
with (open(inbedpe, 'r')) as bed:
for line in bed:
columns = line.rstrip().split("\t")
chrom1, pos1_start, pos1_end = str(columns[0]), int(
columns[1]), int(columns[2])
chrom2, pos2_start, pos2_end = str(columns[3]), int(
columns[4]), int(columns[5])
svtype = columns[-1]
if svtype == "TYPE:DELETION":
svtype = "DEL"
if svtype_to_select == svtype:
if svtype in ['DEL', 'INV', 'DUP', 'CTX']:
sv_list.append((chrom1, pos1_start, pos1_end, chrom2,
pos2_start, pos2_end, svtype))
elif svtype == "INS":
sv_list.append((chrom1, pos1_start, pos1_end, chrom1,
pos1_start + 1, pos1_end + 1, svtype))
logging.info('{} SVs'.format(len(sv_list)))
return sv_list
def filter_bedpe(inbedpe, sv_id_list, outDir):
# Check file existence
assert os.path.isfile(inbedpe), inbedpe + ' not found!'
# Dictionary with chromosome keys to store SVs
logging.info('{} SVs to filter out'.format(len(sv_id_list)))
lines_to_keep = []
with (open(inbedpe, 'r')) as bed:
for line in bed:
columns = line.rstrip().split("\t")
chrom1, pos1_start, pos1_end = str(columns[0]), int(
columns[1]), int(columns[2])
chrom2, pos2_start, pos2_end = str(columns[3]), int(
columns[4]), int(columns[5])
svtype = columns[-1]
svtype = "DEL" if svtype == "TYPE:DELETION" else svtype
sv_id = '_'.join(
(svtype, chrom1, str(pos1_start), chrom2, str(pos2_start)))
if svtype in ['DEL', 'INS', 'INV', 'DUP', 'CTX'] and sv_id not in sv_id_list:
lines_to_keep.append(line)
fileout = os.path.join(outDir, 'uncaptured_SVs.bedpe')
logging.info('Writing {}'.format(fileout))
with (open(fileout, 'w')) as fout:
for line in lines_to_keep:
fout.write(line)
logging.info('{} SVs written'.format(len(lines_to_keep)))
def read_svcaller_bedpe(inbedpe):
# Check file existence
assert os.path.isfile(inbedpe), inbedpe + ' not found!'
# Dictionary with chromosome keys to store SVs
cr_pos = []
with (open(inbedpe, 'r')) as bed:
for line in bed:
columns = line.rstrip().split("\t")
chrom1, pos1_start, pos1_end = str(columns[0]), int(
columns[1]), int(columns[2])
chrom2, pos2_start, pos2_end = str(columns[3]), int(
columns[4]), int(columns[5])
cr_pos.append((chrom1, pos1_start, chrom2, pos2_start, '**'))
logging.info('{} candidate positions'.format(len(cr_pos)))
return cr_pos
def make_gtrees_from_svlist(sv_list):
logging.info('Building SV GenomicTrees...')
# Tree with windows for candidate positions
trees_start = defaultdict(IntervalTree)
trees_end = defaultdict(IntervalTree)
# Populate tree
for sv in sv_list:
chrom1, pos1_start, pos1_end, chrom2, pos2_start, pos2_end, svtype = sv
sv_id = '_'.join(
(svtype, chrom1, str(pos1_start), chrom2, str(pos2_start)))
trees_start[chrom1][pos1_start:pos1_end] = (svtype, sv_id)
trees_end[chrom2][pos2_start:pos2_end] = (svtype, sv_id)
# print('Tree start')
# for k in trees_start.keys():
# print('{} : {}'.format( k, len(trees_start[k])))
# print('Tree end')
# for k in trees_end.keys():
# print('{} : {}'.format( k, len(trees_end[k])))
return trees_start, trees_end
def search_tree_with_cpos(cpos, trees_start, trees_end, win_hlen):
logging.info('Searching SV GenomicTrees with candidate positions...')
lookup_start = []
lookup_end = []
# Log info every n_r times
n_r = 10**6
last_t = time()
for i, p in enumerate(cpos, start=1):
if not i % n_r:
now_t = time()
# print(type(now_t))
logging.info(
"%d candidate positions processed (%f positions / s)" %
(i, n_r / (now_t - last_t)))
last_t = time()
chrom1, pos1, chrom2, pos2, strand_info = p
lookup_start.append(trees_start[chrom1].envelop(pos1 - win_hlen,
pos1 + win_hlen + 1))
lookup_end.append(trees_end[chrom2].envelop(pos2 - win_hlen,
pos2 + win_hlen + 1))
return lookup_start, lookup_end
def overlap(svtype, sv_list, cpos_list, win_hlen, ground_truth, outDir):
'''
:param sv_list: list, list of SVs
:param cr_pos: list, list of clipped read positions
:return: list, list of clipped read positions whose window completely overlap either the CIPOS interval
or the CIEND interval
'''
trees_start, trees_end = make_gtrees_from_svlist(sv_list)
lookup_start, lookup_end = search_tree_with_cpos(cpos_list, trees_start,
trees_end, win_hlen)
# print([l for l in lookup_start if len(l) > 0])
# print([l for l in lookup_end if len(l) > 0])
labels = dict()
sv_covered = set()
for p, lu_start, lu_end in zip(cpos_list, lookup_start, lookup_end):
chrom1, pos1, chrom2, pos2, strand_info = p
pos_id = '_'.join((chrom1, str(pos1), chrom2, str(pos2), strand_info))
l1 = len(lu_start)
l2 = len(lu_end)
if l1 == 1 and l1 == l2:
# print(lu_start)
# print(lu_end)
lu_start_elem_start, lu_start_elem_end, lu_start_elem_data = lu_start.pop(
)
lu_end_elem_start, lu_end_elem_end, lu_end_elem_data = lu_end.pop()
lu_start_elem_svtype, lu_start_elem_svid = lu_start_elem_data
lu_end_elem_svtype, lu_end_elem_svid = lu_end_elem_data
# if lu_start_elem_svtype != 'TRA':
if pos1 - win_hlen <= lu_start_elem_start and lu_start_elem_end <= pos1 + win_hlen and \
pos2 - win_hlen <= lu_end_elem_start and lu_end_elem_end <= pos2 + win_hlen and \
lu_start_elem_svid == lu_end_elem_svid:
# logging.info(
# 'Chr1:{}\tpos1:{}-{}\tChr2:{}\tpos2:{}-{}'.format(
# chrom1, pos1 - win_hlen, pos1 + win_hlen, chrom2, pos2 - win_hlen, pos2 + win_hlen
# )
# )
# logging.info(
# 'LookUp_start:{}-{}_{}\tLookUp_end:{}-{}_{}'.format(
# lu_start_elem_start, lu_start_elem_end, lu_start_elem_data,
# lu_end_elem_start, lu_end_elem_end, lu_end_elem_data
# )
# )
# if pos1 in np.arange(lu_start_elem_start-2, lu_start_elem_end+2) and \
# pos2 in np.arange(lu_end_elem_start-2, lu_end_elem_end+2):
sv_covered.add(lu_start_elem_svid)
labels[pos_id] = lu_start_elem_svtype
# else:
# sv_covered.add(lu_start_elem_svid)
# labels[pos_id] = 'UK_overlap_not_matching'
else:
labels[pos_id] = 'no'+svtype
# else:
#
# sv_covered.add(lu_start_elem_svid)
# labels[pos_id] = lu_start_elem_svtype
elif l1 > 1 or l2 > 1:
lu_start_set = set()
lu_end_set = set()
for s in lu_start:
lu_start_elem_start, lu_start_elem_end, lu_start_elem_data = s
lu_start_elem_svtype, lu_start_elem_svid = lu_start_elem_data
lu_start_set.add(lu_start_elem_svid)
for s in lu_end:
lu_end_elem_start, lu_end_elem_end, lu_end_elem_data = s
lu_end_elem_svtype, lu_end_elem_svid = lu_end_elem_data
lu_end_set.add(lu_end_elem_svid)
sv_covered = sv_covered | (lu_start_set & lu_end_set)
# if svtype in ['DEL', 'INV', 'DUP', 'TRA']:
# labels[pos_id] = 'UK_multiple_on_either_windows'
# elif svtype == 'INS':
# labels[pos_id] = svtype
labels[pos_id] = svtype
elif l1 == 0 and l1 == l2:
# logging.info('CPOS->Partial: %s\t%d\t%d' % (elem, start, end))
labels[pos_id] = 'no'+svtype
elif (l1 == 1 and l2 > 1) or (l2 == 1 and l1 > 1) or (l2 > 1 and l1 > 1):
lu_start_set = set()
lu_end_set = set()
for s in lu_start:
lu_start_elem_start, lu_start_elem_end, lu_start_elem_data = s
lu_start_elem_svtype, lu_start_elem_svid = lu_start_elem_data
lu_start_set.add(lu_start_elem_svid)
for s in lu_end:
lu_end_elem_start, lu_end_elem_end, lu_end_elem_data = s
lu_end_elem_svtype, lu_end_elem_svid = lu_end_elem_data
lu_end_set.add(lu_end_elem_svid)
sv_covered = sv_covered | (lu_start_set & lu_end_set)
# labels[pos_id] = 'UK_single_and_multiple'
labels[pos_id] = svtype
else:
# (l1 == 1 and l2 == 0) or (l1 == 0 and l2 == 1)
labels[pos_id] = 'no'+svtype
logging.info(Counter(labels.values()))
sv_coverage = int(len(sv_covered) / len(sv_list) * 100)
logging.info('SV coverage: {}/{}={}%'.format(len(sv_covered), len(sv_list),
sv_coverage))
filename, file_extension = os.path.splitext(ground_truth)
if file_extension == '.bedpe':
# print(sv_covered)
filter_bedpe(ground_truth, sv_covered, outDir)
elif file_extension == '.sur':
filter_survivor_output(ground_truth, sv_covered, outDir)
return labels
# Get labels
def get_labels(chrlist, chr_dict, win_len, svtype, ground_truth, sv_positions,
channelDataDir, outFile, outDir):
# windows half length
win_hlen = int(int(win_len) / 2)
sv_caller_file = os.path.join('..', '..', 'data', sv_positions + '.bedpe')
sv_caller_name = os.path.basename(sv_positions)
if os.path.exists(sv_caller_file):
cpos_list = read_svcaller_bedpe(sv_caller_file)
elif sv_caller_name == 'split_reads':
cpos_list = load_all_clipped_read_positions(win_hlen, svtype, chr_dict,
channelDataDir)
else:
sys.exit('I cannot find {} nor {}'.format(sv_caller_file, sv_caller_name))
# Keep only positions that can be used to create windows
cpos_list = [
(chrom1, pos1, chrom2, pos2, strand_info)
for chrom1, pos1, chrom2, pos2, strand_info, in cpos_list if chrom1 in chrlist
and chrom2 in chrlist and win_hlen <= pos1 <= chr_dict[chrom1] -
win_hlen and win_hlen <= pos2 <= chr_dict[chrom2] - win_hlen
]
filename, file_extension = os.path.splitext(ground_truth)
if file_extension == '.bedpe':
sv_list = read_bedpe(ground_truth, svtype)
elif file_extension == '.vcf' or file_extension == '.gz':
sv_list = read_vcf(ground_truth)
# Get overlap of candidate positions with all SV breakpoints (all 4 SV callers)
# crpos_all_sv = get_crpos_overlap_with_sv_callsets(sv_dict, cr_pos_dict)
# filename, file_extension = os.path.splitext(ground_truth)
# trees_start, trees_end = make_gtrees_from_truth_set(sv_list, file_extension.upper())
# print(sv_list)
labels = overlap(svtype, sv_list, cpos_list, win_hlen, ground_truth, outDir)
with gzip.GzipFile(outFile, 'wb') as fout:
fout.write(json.dumps(labels).encode('utf-8'))
def main():
'''
Label windows according to truth set
:return: None
'''
parser = argparse.ArgumentParser(description='Create labels')
parser.add_argument('-b',
'--bed',
type=str,
default='../../data/seqs.bed',
help="Specify chromosome regions to consider (BED)")
parser.add_argument('-l',
'--logfile',
type=str,
default='labels.log',
help="Specify log file")
parser.add_argument('-c',
'--chrlist',
type=str,
default='12,22',
help="Comma separated list of chromosomes to consider")
parser.add_argument('-w',
'--window',
type=str,
default=200,
help="Specify window size")
parser.add_argument('-s',
'--svtype',
type=str,
default='DEL',
help="Specify SV type")
parser.add_argument('-sv',
'--sv_positions',
type=str,
#default=os.path.join('..', '..', 'data', 'gridss'),
default=os.path.join('..', '..', 'data', 'split_reads'),
help="Specify Manta/GRIDSS BEDPE file")
parser.add_argument('-gt',
'--ground_truth',
type=str,
default='../../data/test.bedpe',
help="Specify ground truth VCF/BEDPE file")
parser.add_argument('-o',
'--out',
type=str,
default='labels.json.gz',
help="Specify output")
parser.add_argument('-p',
'--outputpath',
type=str,
default='',
help="Specify output path")
args = parser.parse_args()
sv_caller_name = os.path.basename(args.sv_positions)
output_dir = os.path.join(args.outputpath, 'cnn',
'win' + str(args.window),
sv_caller_name, 'windows',
args.svtype
)
os.makedirs(output_dir, exist_ok=True)
# Log file
logfilename = os.path.join(output_dir, args.logfile)
output_file = os.path.join(output_dir, args.out)
FORMAT = '%(asctime)s %(message)s'
logging.basicConfig(format=FORMAT,
filename=logfilename,
filemode='w',
level=logging.INFO)
t0 = time()
# Get dictionary of chromosome lengths
chr_dict = chr_dict_from_bed(args.bed)
get_labels(chrlist=args.chrlist.split(','),
chr_dict=chr_dict,
win_len=args.window,
svtype=args.svtype,
ground_truth=args.ground_truth,
sv_positions=args.sv_positions,
channelDataDir=args.outputpath,
outFile=output_file,
outDir=output_dir)
logging.info('Elapsed time making labels = %f' % (time() - t0))
if __name__ == '__main__':
main()