-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcuteSV_output.py
296 lines (282 loc) · 13 KB
/
cuteSV_output.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
import time
'''
var_list: [[a, b, info_dict], ...] info_dict: {'GENE_ID': str, 'TRANSCRIPT_ID': str, ...}
[98881292, 98881515, {'gene_id': 'MATN2', 'transcript_id': 'NM_002380', ...}]
return info_dict for which a <= breakpoint < b
'''
def add_anotation_in(var_list, breakpoint):
left = 0
right = len(var_list) - 1
mid = 0
info = dict()
while left < right:
mid = (left + right + 1) >> 1
if var_list[mid][0] <= breakpoint:
left = mid
else:
right = mid - 1
for i in range(left, -1, -1):
if breakpoint < var_list[i][1]:
#info.append(var_list[i][2])
for info_item in var_list[i][2]:
if info_item not in info:
info[info_item] = set()
info[info_item].add(var_list[i][2][info_item])
if breakpoint - var_list[i][0] > 3000000:
break
return info
'''
var_list: [[a, b, info_dict], ...]
return info_dict for which [start, end] and [a, b] overlap
'''
def add_anotation_overlap(var_list, start, end):
left = 0
right = len(var_list) - 1
mid = 0
info = dict()
while left < right:
mid = (left + right + 1) >> 1
if var_list[mid][0] <= start:
left = mid
else:
right = mid - 1
for i in range(left, -1, -1):
if start < var_list[i][1]:
for info_item in var_list[i][2]:
if info_item not in info:
info[info_item] = set()
info[info_item].add(var_list[i][2][info_item])
if start - var_list[i][0] > 3000000:
break
for i in range(left + 1, len(var_list), 1):
if var_list[i][0] < end:
for info_item in var_list[i][2]:
if info_item not in info:
info[info_item] = set()
info[info_item].add(var_list[i][2][info_item])
else:
break
return info
def solve_annotation(sv_type, anno_list, sv_start, sv_end):
annotation_dict = dict()
if anno_list == None or len(anno_list) == 0:
return annotation_dict
if sv_type == 'INS':
annotation_dict = add_anotation_in(anno_list, sv_start)
elif sv_type == 'DEL':
annotation_dict = add_anotation_overlap(anno_list, sv_start, sv_start + sv_end)
elif sv_type == 'INV' or sv_type == 'DUP':
annotation_dict = add_anotation_overlap(anno_list, sv_start, sv_end)
elif sv_type == 'TRA':
annotation_dict = add_anotation_in(anno_list, sv_start)
return annotation_dict
def solve_annotation_tra(anno_list, sv_end, anno_pre):
anno_list = add_anotation_in(anno_list, sv_end)
for info_item in anno_list:
if info_item not in anno_pre:
anno_pre[info_item] = set()
anno_pre[info_item].add(anno_list[info_item])
print('tra')
print(anno_pre)
return anno_pre
def parse_annotation_dict(anno):
str = ''
if 'gene_id' in anno:
str += 'GENE_ID='
for item in anno['gene_id']:
str += item + ','
str = str[:-1] + ';'
if 'gene_name' in anno:
str += 'GENE_NAME='
for item in anno['gene_name']:
str += item + ','
str = str[:-1] + ';'
if 'transcript_id' in anno:
str += 'TRANSCRIPT_ID='
for item in anno['transcript_id']:
str += item + ','
str = str[:-1] + ';'
if 'exon_number' in anno:
str += 'EXON_NUMBER='
for item in anno['exon_number']:
str += item + ','
str = str[:-1] + ';'
if 'exon_id' in anno:
str += 'EXON_ID='
for item in anno['exon_id']:
str += item + ','
str = str[:-1] + ';'
if str != '' and str[-1] == ';':
str = str[:-1]
return str
# add semi_result into output_file in vcf format
def output_result(semi_result, samples_num, output_file):
file = open(output_file, 'a')
for item in semi_result: # [CHROM, POS, CANDIDATE_RECORD, CIPOS, CILEN, LIST(RECORD), ANNOTATION]
supp_vec = ''
#supp_id = []
for i in range(samples_num):
if i in item[5]:
supp_vec += '1'
#supp_id.append(sample_ids[i])
else:
supp_vec += '0'
can_record = item[2]
if can_record.qual == "." or can_record.qual is None:
filter_lable = "PASS"
else:
filter_lable = "PASS" if float(can_record.qual) >= 5.0 else "q5"
annotation_dict = item[6]
anno_str = parse_annotation_dict(annotation_dict)
if 'INS' in can_record.type:
sv_len = can_record.end
sv_end = can_record.start
info_list = "SVTYPE={SVTYPE};SVLEN={SVLEN};END={END};CIPOS={CIPOS};CILEN={CILEN};SUPP={SUPP};SUPP_VEC={SUPP_VEC}".format(
SUPP = len(item[5]),
#SUPP_ID = ','.join(supp_id),
SUPP_VEC = supp_vec,
SVTYPE = can_record.type,
SVLEN = sv_len,
END = sv_end,
CIPOS = str(item[3]),
CILEN = str(item[4]))
if can_record.strand != '.':
info_list += ';STRAND=' + can_record.strand
if anno_str != '':
info_list += ';' + anno_str
elif 'DEL' in can_record.type:
sv_len = -can_record.end
sv_end = can_record.start + can_record.end
info_list = "SVTYPE={SVTYPE};SVLEN={SVLEN};END={END};CIPOS={CIPOS};CILEN={CILEN};SUPP={SUPP};SUPP_VEC={SUPP_VEC}".format(
SUPP = len(item[5]),
SUPP_VEC = supp_vec,
SVTYPE = can_record.type,
SVLEN = sv_len,
END = sv_end,
CIPOS = str(item[3]),
CILEN = str(item[4]))
if can_record.strand != '.':
info_list += ';STRAND=' + can_record.strand
if anno_str != '':
info_list += ';' + anno_str
elif 'INV' in can_record.type or 'DUP' in can_record.type:
#sv_len = can_record.end - can_record.start + 1
sv_len = can_record.end
sv_end = can_record.end + can_record.start - 1
info_list = "SVTYPE={SVTYPE};SVLEN={SVLEN};END={END};SUPP={SUPP};SUPP_VEC={SUPP_VEC}".format(
SUPP = len(item[5]),
SUPP_VEC = supp_vec,
SVTYPE = can_record.type,
SVLEN = sv_len,
END = sv_end)
if can_record.strand != '.':
info_list += ';STRAND=' + can_record.strand
if anno_str != '':
info_list += ';' + anno_str
elif can_record.type == 'BND':
info_list = "SVTYPE={SVTYPE};END={END};CHR2={CHR2};SUPP={SUPP};SUPP_VEC={SUPP_VEC}".format(
SUPP = len(item[5]),
END = can_record.end,
CHR2 = can_record.chrom2,
SUPP_VEC = supp_vec,
SVTYPE = can_record.type)
if anno_str != '':
info_list += ';' + anno_str
else:
sv_end = can_record.end
sv_len = can_record.end - can_record.start
info_list = "SVTYPE={SVTYPE};SVLEN={SVLEN};END={END};SUPP={SUPP};SUPP_VEC={SUPP_VEC}".format(
SUPP = len(item[5]),
SUPP_VEC = supp_vec,
SVTYPE = can_record.type,
SVLEN = sv_len,
END = sv_end)
if can_record.strand != '.':
info_list += ';STRAND=' + can_record.strand
if anno_str != '':
info_list += ';' + anno_str
#print(info_list)
# calculate AF
af = 0
for i in range(samples_num):
if i in item[5]:
if item[5][i].gt == '0/1':
af += 1
elif item[5][i].gt == '1/1':
af += 2
af = af / samples_num / 2
info_list += ';AF=' + str(round(af, 4))
file.write("{CHR}\t{POS}\t{ID}\t{REF}\t{ALT}\t{QUAL}\t{PASS}\t{INFO}\t{FORMAT}\t".format(
CHR = can_record.chrom1,
POS = can_record.start,
ID = can_record.name,
REF = can_record.ref,
ALT = ','.join(can_record.alt),
QUAL = '.' if can_record.qual is None else can_record.qual,
PASS = filter_lable,
INFO = info_list,
FORMAT = "GT:RAL:AAL:CO",
))
'''
for i in range(len(sample_ids)):
if i in item[5]:
file.write("%s:%s:%s:"%(item[5][i].gt, item[5][i].ref.replace(':', '_'), (','.join(item[5][i].alt)).replace(':', '_')))
file.write("%s_%d-%s_%d\t"%(item[5][i].chrom1, item[5][i].start, item[5][i].chrom2, item[5][i].end))
else:
file.write("./.:NAN:NAN:NAN\t")
'''
for i in range(samples_num):
if i in item[5]:
file.write("%s:"%(item[5][i].gt))
file.write("%s_%d-%s_%d\t"%(item[5][i].chrom1, item[5][i].start, item[5][i].chrom2, item[5][i].end))
else:
file.write("./.:NAN:NAN:NAN\t")
file.write('\n')
file.close()
def generate_header(file, contiginfo, sample_ids):
# General header
file.write('##fileformat=VCFv4.2\n')
file.write("##fileDate=%s\n"%(time.strftime('%Y-%m-%d %H:%M:%S %w-%Z',time.localtime())))
for i in contiginfo:
file.write("##contig=<ID=%s,length=%d>\n"%(i, contiginfo[i]))
# Specific header
# ALT
file.write("##ALT=<ID=INS,Description=\"Insertion of novel sequence relative to the reference\">\n")
file.write("##ALT=<ID=DEL,Description=\"Deletion relative to the reference\">\n")
file.write("##ALT=<ID=DUP,Description=\"Region of elevated copy number relative to the reference\">\n")
file.write("##ALT=<ID=INV,Description=\"Inversion of reference sequence\">\n")
file.write("##ALT=<ID=BND,Description=\"Breakend of translocation\">\n")
# INFO
#file.write("##INFO=<ID=PRECISE,Number=0,Type=Flag,Description=\"Precise structural variant\">\n")
#file.write("##INFO=<ID=IMPRECISE,Number=0,Type=Flag,Description=\"Imprecise structural variant\">\n")
file.write("##INFO=<ID=SVTYPE,Number=1,Type=String,Description=\"Type of structural variant\">\n")
file.write("##INFO=<ID=SVLEN,Number=1,Type=Integer,Description=\"Difference in length between REF and ALT alleles\">\n")
file.write("##INFO=<ID=CHR2,Number=1,Type=String,Description=\"Chromosome for END coordinate in case of a translocation\">\n")
file.write("##INFO=<ID=END,Number=1,Type=Integer,Description=\"End position of the variant described in this record\">\n")
file.write("##INFO=<ID=CIPOS,Number=2,Type=Integer,Description=\"Confidence interval around POS for imprecise variants\">\n")
file.write("##INFO=<ID=CILEN,Number=2,Type=Integer,Description=\"Confidence interval around inserted/deleted material between breakends\">\n")
# file.write("##INFO=<ID=MATEID,Number=.,Type=String,Description=\"ID of mate breakends\">\n")
file.write("##INFO=<ID=RE,Number=1,Type=Integer,Description=\"Number of read support this record\">\n")
file.write("##INFO=<ID=STRAND,Number=A,Type=String,Description=\"Strand orientation of the adjacency in BEDPE format (DEL:+-, DUP:-+, INV:++/--)\">\n")
#file.write("##INFO=<ID=RNAMES,Number=.,Type=String,Description=\"Supporting read names of SVs (comma separated)\">\n")
file.write("##INFO=<ID=SUPP,Number=1,Type=Integer,Description=\"Number of samples supporting the variant\">\n")
#file.write("##INFO=<ID=SUPP_ID,Number=.,Type=String,Description=\"Samples supporting the variant\">\n")
file.write("##INFO=<ID=SUPP_VEC,Number=1,Type=String,Description=\"Samples id supporting the variant\">\n")
file.write("##INFO=<ID=AF,Number=1,Type=Float,Description=\"Allele frequency\">\n")
file.write("##FILTER=<ID=q5,Description=\"Quality below 5\">\n")
# FORMAT
file.write("##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">\n")
'''
file.write("##FORMAT=<ID=DR,Number=1,Type=Integer,Description=\"# High-quality reference reads\">\n")
file.write("##FORMAT=<ID=DV,Number=1,Type=Integer,Description=\"# High-quality variant reads\">\n")
file.write("##FORMAT=<ID=PL,Number=G,Type=Integer,Description=\"# Phred-scaled genotype likelihoods rounded to the closest integer\">\n")
file.write("##FORMAT=<ID=GQ,Number=1,Type=Integer,Description=\"# Genotype quality\">\n")
'''
file.write("##FORMAT=<ID=CO,Number=1,Type=String,Description=\"Sequence coordinates\">\n")
file.write("##FORMAT=<ID=RAL,Number=1,Type=String,Description=\"Reference allele sequence\">\n")
file.write("##FORMAT=<ID=AAL,Number=1,Type=String,Description=\"Alternative allele sequence\">\n")
#file.write("##CommandLine=\"cuteSV %s\"\n"%(" ".join(argv)))
file.write("#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT")
for sample_id in sample_ids:
file.write('\t' + sample_id)
file.write('\n')