-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimap_to_vcf.py
executable file
·276 lines (211 loc) · 8.95 KB
/
minimap_to_vcf.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
import sys
import re
from Bio import SeqIO
current_set = []
current_node = ""
current_chrom = ""
unique_insertions = []
non_unique_insertions = []
unique_deletions = []
non_unique_deletions = []
class Deletion(object):
def __init__(self, node, contig_pos, rc, ref_id, ref_pos1, ref_pos2, anchor1, anchor2):
self.node = node
self.contig_pos = contig_pos
self.rc = rc
self.ref_id = ref_id
self.ref_pos1 = ref_pos1
self.ref_pos2 = ref_pos2
self.anchor1 = anchor1
self.anchor2 = anchor2
def __eq__(self, other):
return (abs(self.ref_pos1 - other.ref_pos1) < 100 or abs(self.ref_pos2 - other.ref_pos2) < 100 or abs(self.ref_pos2 - other.ref_pos1) < 100 or abs(self.ref_pos1 - other.ref_pos2) < 100)
def size(self):
return abs(self.ref_pos2 - self.ref_pos1) - 1
class Insertion(object):
def __init__(self, node, pos1, pos2, rc, ref_id, ref_pos, anchor1, anchor2):
self.node = node
self.pos1 = pos1
self.pos2 = pos2
self.rc = rc
self.ref_id = ref_id
self.ref_pos = ref_pos
self.anchor1 = anchor1
self.anchor2 = anchor2
def __eq__(self, other):
return abs(self.ref_pos - other.ref_pos) < 100
def size(self):
return abs(self.pos2 - self.pos1) - 1
def Overlaps(al1, al2):
cont11 = al1.cont1
cont12 = al1.cont2
cont21 = al2.cont1
cont22 = al2.cont2
if cont11 > cont12:
cont11, cont12 = cont12, cont11
if cont21 > cont22:
cont21, cont22 = cont22, cont21
return (cont11 >= cont21 and cont11 <= cont22) or (cont12 <= cont21 and cont12 >= cont22)
def Near(sv1, sv2):
return sv2 - sv1 < 50 and sv2 - sv1 > -10
def BigAnchors(al1, al2):
return abs(al1.cont1 - al1.cont2) > 300 or abs(al2.cont1 - al2.cont2) > 300
def IsDeletion(al1, al2):
ref11 = al1.ref1
ref12 = al1.ref2
is_rc = ref12 - ref11 < 0
ref21 = al2.ref1
ref22 = al2.ref2
is_rc2 = ref22 - ref21 < 0
if is_rc != is_rc2:
return False
if ref11 > ref12:
ref11, ref12 = ref12, ref11
if ref21 > ref22:
ref21, ref22 = ref22, ref21
if is_rc:
contig_breakpoint = al1.cont1
else:
contig_breakpoint = al1.cont2
if cont21 - cont12 < 5:
if ref21 - ref12 > 5:
deletion = Deletion(al1.node, contig_breakpoint, is_rc, al1.chrom, ref12, ref21, abs(cont22 - cont21), abs(cont12 - cont11))
if deletion.size() > 1000:
print(deletion.size())
print("Deletion: " + str(al1) + " \t " + str(al2))
if deletion not in unique_deletions:
unique_deletions.append(deletion)
else:
non_unique_deletions.append(deletion)
return True
if cont22 < cont11:
deletion = Deletion(al1.node, contig_breakpoint, is_rc, al1.chrom, ref12, ref21, abs(cont22 - cont21), abs(cont12 - cont11))
if deletion.size() > 1000:
print(deletion.size())
print("Deletion: " + str(al1) + " \t " + str(al2))
if deletion in non_unique_deletions:
return False
if cont11 - cont22 > 5:
if deletion not in unique_deletions:
unique_deletions.append(deletion)
else:
non_unique_deletions.append(deletion)
return True
return False
def IsInsertion(al1, al2):
cont11 = al1.cont1
cont12 = al1.cont2
is_rc = cont12 - cont11 < 0
cont21 = al2.cont1
cont22 = al2.cont2
is_rc2 = cont22 - cont21 < 0
if is_rc != is_rc2:
return False
if cont11 > cont12:
cont11, cont12 = cont12, cont11
if cont21 > cont22:
cont21, cont22 = cont22, cont21
if is_rc:
ref_breakpoint = al1.ref1
else:
ref_breakpoint = al1.ref2
if cont12 < cont21:
if cont21 - cont12 > 5:
ins = Insertion(al1.node, cont12, cont21, is_rc, al1.chrom, ref_breakpoint, abs(cont22 - cont21), abs(cont12 - cont11))
if ins.size() > 500:
print(ins.size())
print("Insertion: " + str(al1) + " \t " + str(al2))
if ins not in unique_insertions:
unique_insertions.append(ins)
else:
non_unique_insertions.append(ins)
return True
if cont22 < cont11:
ins = Insertion(al1.node, cont22, cont11, is_rc, al1.chrom, ref_breakpoint, abs(cont22 - cont21), abs(cont12 - cont11))
if ins.size() > 500:
print(ins.size())
print("Insertion: " + str(al1) + " \t " + str(al2))
if cont11 - cont22 > 5:
if ins not in unique_insertions:
unique_insertions.append(ins)
else:
non_unique_insertions.append(ins)
return True
return False
def AnalyzeCurrentSet(al1, al2):
if BigAnchors(al1, al2):
if IsInsertion(al1, al2):
pass
#print(str(al1) + " \t " + str(al2))
if IsDeletion(al1, al2):
pass
#print(str(al1) + " \t " + str(al2))
class ContigFilter(object):
def __init__(self):
self.coverage_cutoff = 0.0
self.length_cutoff = 0
def ParseCoverage(self, contig_name):
m = re.search("cov_([\d\.]+)", contig_name)
return float(m.group(1))
def ParseLength(self, contig_name):
m = re.search("length_([\d\.]+)_", contig_name)
return int(m.group(1))
def PassFilter(self, contig_name):
return self.ParseCoverage(contig_name) > self.coverage_cutoff \
and self.ParseLength(contig_name) > self.length_cutoff
class Alignment(object):
def __init__(self, ref1, ref2, cont1, cont2, chrom, node):
self.ref1 = ref1
self.ref2 = ref2
self.cont1 = cont1
self.cont2 = cont2
self.chrom = chrom
self.node = node
def __str__(self):
return str(self.ref1) + "\t" + str(self.ref2) + "\t" + str(self.cont1) + "\t" + str(self.cont2) + "\t" + self.chrom + "\t" + self.node
i = 0
filter = ContigFilter()
with open(sys.argv[1], "r") as nucmer_file:
lines = nucmer_file.readlines()
for i in range(1, len(lines) - 1):
line = lines[i]
if line.startswith("local misassembly") or line.startswith("transloc") or line.startswith("relocation") or line.startswith("indel") or line.startswith("fake"):
line1 = lines[i-1]
line2 = lines[i+1]
if line2.startswith("CON"):
continue
chrom = line1.split("\t")[4].strip()
ref11 = int(line1.split("\t")[0].strip())
ref12 = int(line1.split("\t")[1].strip())
ref21 = int(line2.split("\t")[0].strip())
ref22 = int(line2.split("\t")[1].strip())
cont11 = int(line1.split("\t")[2].strip())
cont12 = int(line1.split("\t")[3].strip())
cont21 = int(line2.split("\t")[2].strip())
cont22 = int(line2.split("\t")[3].strip())
contig_name = line1.split("\t")[5].strip()
if not filter.PassFilter(contig_name):
continue
i += 1
if i > 100000:
pass
#break
al1 = Alignment(ref11, ref12, cont11, cont12, chrom, contig_name)
al2 = Alignment(ref21, ref22, cont21, cont22, chrom, contig_name)
AnalyzeCurrentSet(al1, al2)
records = list(SeqIO.parse(sys.argv[2], "fasta"))
record_dict = {}
for record in records:
if record.id not in record_dict.keys():
record_dict[record.id] = record
with open(sys.argv[3], "w") as vcf, open("insertions_with_anchors.fasta", "w") as fasta:
for insertion in unique_insertions:
if insertion.size() >= 300:
fasta.write(">" + insertion.node + "\n")
fasta.write(str(record_dict[insertion.node].seq))
fasta.write("\n")
ins_seq = str(record_dict[insertion.node].seq[insertion.pos1 : insertion.pos2] if not insertion.rc else record_dict[insertion.node].seq[insertion.pos1 : insertion.pos2].reverse_complement())
vcf.write(str(insertion.ref_id) + "\t" + str(insertion.ref_pos) + "\t" + "." + "\t" + str(record_dict[insertion.node].seq[insertion.pos1]) + "\t" + ins_seq + "\t" + "." + "\t" + "PASS" + "\t" + "DP=100" + "\t" + insertion.node + "\t" + str(insertion.anchor1) + "\t" + str(insertion.anchor2) + "\t" + str(insertion.pos1) + "\t" + str(insertion.pos2) + "\n")
# for deletion in unique_deletions:
# del_seq = str(record_dict[deletion.node].seq[deletion.pos1 : deletion.pos2] if not insertion.rc else record_dict[deletion.node].seq[deletion.pos1 : deletion.pos2].reverse_complement())
# vcf.write(str(deletion.ref_id) + "\t" + str(deletion.ref_pos1) + "\t" + "." + "\t" + str(record_dict[deletion.node].seq[deletion.pos1]) + "\t" + del_seq + "\t" + "." + "\t" + "PASS" + "\t" + "DP=100" + "\t" + deletion.node + "\t" + str(deletion.anchor1) + "\t" + str(deletion.anchor2) + "\t" + str(deletion.pos1) + "\t" + str(deletion.pos2) + "\n")