-
Notifications
You must be signed in to change notification settings - Fork 9
/
assembly_graph.py
executable file
·311 lines (257 loc) · 10.1 KB
/
assembly_graph.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
#!/usr/bin/env python
import sys
import getopt
from igraph import *
base_for = "ACGT"
base_rev = "TGCA"
comp_tab = str.maketrans(base_for, base_rev)
neighborhood = 3
output_prefix = "elba"
def usage():
global neighborhood, output_prefix
sys.stderr.write("Usage: {} [options] <elba.string.paf> <elba.overlap.paf> <reads.fa>\n".format(sys.argv[0]))
sys.stderr.write("Options: -l INT branch/bridge neighborhood value [{}]\n".format(neighborhood))
sys.stderr.write(" -o STR output file prefix [{}]\n".format(output_prefix))
sys.stderr.write(" -h help message\n")
return -1
def revcomp(s): return s.translate(comp_tab)[::-1]
def read_fasta(fasta_fname):
readseqs = list()
readnames = list()
seq = []
readname = ""
for line in open(fasta_fname, "r"):
if line.startswith(">"):
if len(seq) > 0:
readseqs.append("".join(seq))
readnames.append(readname)
readname = line.lstrip(">").split()[0].rstrip()
seq = []
else:
seq.append(line.rstrip())
if len(seq) > 0:
readseqs.append("".join(seq))
readnames.append(readname)
namemap = dict()
for i, readname in enumerate(readnames):
namemap[readname] = i
return readseqs, readnames, namemap
def read_paf(paf_fname, readlens, namemap):
overlaps = []
attrs = []
types = (str, int, int, int, lambda x: int(x[0] == '-'), str, int, int, int)
for line in open(paf_fname, "r"):
tokens = line.rstrip().split()[:9]
nameQ, lenQ, begQ, endQ, strand, nameT, lenT, begT, endT = [t(tok) for t, tok in zip(types, tokens)]
assert nameQ in namemap and nameT in namemap
idQ, idT = namemap[nameQ], namemap[nameT]
assert lenQ == readlens[idQ] and lenT == readlens[idT]
overlaps.append((idQ, idT))
attrs.append((begQ, endQ, strand, begT, endT))
return overlaps, attrs
def create_overlap_graph(readlens, readnames, overlaps, attrs):
n = len(readlens)
G = Graph(n, directed=True)
G.vs["readlen"] = readlens
G.vs["readname"] = readnames
edges, directions, suffixes, prefixes = [], [], [], []
for overlap, attr in zip(overlaps, attrs):
idQ, idT = overlap
if idQ >= idT: continue
begQ, endQ, strand, begT, endT = attr
lenQ, lenT = readlens[idQ], readlens[idT]
begTr = begT if strand == 0 else lenT - endT
endTr = endT if strand == 0 else lenT - begT
if begQ <= begTr and lenQ - endQ <= lenT - endTr:
pass
elif begQ >= begTr and lenQ - endQ >= lenT - endTr:
pass
else:
edges += [(idQ, idT), (idT, idQ)]
if begQ > begTr:
lengths = [(lenT - endTr) - (lenQ - endQ), begQ - begTr]
suffixes += lengths
prefixes += lengths[::-1]
if strand == 0:
directions += [1, 2]
else:
directions += [0, 0]
else:
lengths = [begTr - begQ, (lenQ - endQ) - (lenT - endTr)]
suffixes += lengths
prefixes += lengths[::-1]
if strand == 0:
directions += [2, 1]
else:
directions += [3, 3]
G.add_edges(edges)
G.es["direction"] = directions
G.es["suffix"] = suffixes
G.es["prefix"] = prefixes
return G
def generate_contig_chains(S):
G = S.copy()
branches = G.vs.select(_indegree_ge=3)
branch_edges = G.es.select(_between=(branches, G.vs))
G.delete_edges(branch_edges)
contig_components = G.components()
contig_chains = []
for contig_component in contig_components:
q = len(contig_component)
if q <= 1: continue
vertices = G.vs.select(contig_component)
roots = vertices.select(_indegree_eq=1)
assert len(roots) == 2
s, t = roots
topsort = G.bfs(s)[0]
assert topsort[0] == s.index and topsort[-1] == t.index
chain = []
last_dir = None
for i in range(q-1):
e = G.es[G.get_eid(topsort[i], topsort[i+1])]
strand = int((e["direction"]>>1)&1)
chain.append((e.source, e["prefix"], strand))
last_dir = e["direction"]
chain.append((t.index, t["readlen"], 1 - int(last_dir&1)))
contig_chains.append(chain)
return contig_chains
def assemble_chain(chain, readseqs):
parts = []
for readid, pre, strand in chain:
s = readseqs[readid]
if strand == 1: s = revcomp(s)
parts.append(s[:pre])
return "".join(parts)
def identify_tips(G):
roots = G.vs.select(_indegree_eq=1)
branches = G.vs.select(_indegree_ge=3)
tips = G.es.select(_between=(roots, branches))
return tips
def identify_bridges(G):
branches = G.vs.select(_indegree_ge=3)
bridges = []
targets = set()
for branch in branches:
for t in branch.successors():
if t.index in targets:
bridges.append(t.index)
else:
targets.add(t.index)
return bridges
def extend_chain(G, root, visited, l):
chain = [root]
cur = root
for i in range(l-1):
front = G.vs[cur].successors()
neighs = [x.index for x in front if x.index not in visited]
if len(neighs) != 1: break
visited.add(cur)
cur = neighs.pop()
chain.append(cur)
return chain
def extend_chain(G, root, visited, l):
chain = [root]
cur = root
for i in range(l-1):
front = G.vs[cur].successors()
neighs = [x.index for x in front if x.index not in visited]
if len(neighs) != 1: break
visited.add(cur)
cur = neighs.pop()
chain.append(cur)
return chain
def bridge_neighborhood_chains(G, bridge, l):
b = G.vs[bridge]
if b.indegree() != 2: return None
westend, eastend = [x.index for x in b.successors()]
if G.vs[westend].indegree() != 3 or G.vs[eastend].indegree() != 3: return None
visited = {bridge, westend, eastend}
west_chain_roots = [x.index for x in G.vs[westend].successors() if x.index not in visited]
east_chain_roots = [x.index for x in G.vs[eastend].successors() if x.index not in visited]
if len(west_chain_roots) != 2 or len(east_chain_roots) != 2: return None
northwest_root, southwest_root = west_chain_roots
northeast_root, southeast_root = east_chain_roots
northwest_chain = extend_chain(G, northwest_root, visited, l)
southwest_chain = extend_chain(G, southwest_root, visited, l)
northeast_chain = extend_chain(G, northeast_root, visited, l)
southeast_chain = extend_chain(G, southeast_root, visited, l)
chains = [northwest_chain, southwest_chain, northeast_chain, southeast_chain]
for chain in chains:
if len(chain) != l: return None
return chains
def main(argc, argv):
global neighborhood, output_prefix
try: opts, args = getopt.gnu_getopt(argv[1:], "l:o:h")
except getopt.GetoptError as err:
sys.stderr.write("error: {}\n".format(err))
return usage()
for o, a in opts:
if o == "-l": neighborhood = int(a)
elif o == "-o": output_prefix = a
elif o == "-h": return usage()
if len(args) != 3:
return usage()
string_paf_fname = args[0]
overlap_paf_fname = args[1]
fasta_fname = args[2]
readseqs, readnames, namemap = read_fasta(fasta_fname)
readlens = [len(s) for s in readseqs]
overlaps, attrs = read_paf(string_paf_fname, readlens, namemap)
S = create_overlap_graph(readlens, readnames, overlaps, attrs)
overlaps, attrs = read_paf(overlap_paf_fname, readlens, namemap)
R = create_overlap_graph(readlens, readnames, overlaps, attrs)
itr = 1
while True:
# step one: remove tips
tips = identify_tips(S)
tips_found = len(tips)
tips.delete()
sys.stderr.write("Iteration {}: Found and removed {} tips\n".format(itr, tips_found//2))
# step two: identify bridges
bridges = identify_bridges(S)
sys.stderr.write("Iteration {}: Found {} bridges\n".format(itr, len(bridges)))
# step 3: identify edges between bridges and delete them
double_bridges = S.es.select(_within=bridges)
if len(double_bridges) == 0:
break
else:
sys.stderr.write("Iteration {}: Found and removed {} double bridges\n".format(itr, len(double_bridges)))
double_bridges.delete()
itr += 1
while True:
S.vs["bridge"] = 0
bridges = identify_bridges(S)
S.vs[bridges]["bridge"] = 1
S.es["added"] = 0
S.es["remove"] = 0
for bridge in bridges:
bridge_chains = bridge_neighborhood_chains(S, bridge, 2)
if bridge_chains is None: continue
# f.write("bridge={}\tchains={}\n".format(bridge, [c[0] for c in bridge_chains]))
counts = []
for i in range(3):
for j in range(i+1, 4):
num = len(R.es.select(_between=(bridge_chains[i], bridge_chains[j])))//2
counts.append(num)
# if num > 0:
# S.add_edge(bridge_chains[i][0], bridge_chains[j][0], added=1)
# S.add_edge(bridge_chains[j][0], bridge_chains[i][0], added=1)
# f.write("count({}, {}) = {}\n".format(i, j, num))
if counts[0] > 0 and counts[5] > 0 and sum(counts[1:5]) == 0:
toremove = S.es.select(_from=bridge)
for e in toremove: e["remove"] = 1
toremove = S.es.select(_to=bridge)
for e in toremove: e["remove"] = 1
es = S.es.select(remove_eq=1)
if len(es) == 0: break
es.delete()
tips = identify_tips(S)
tips.delete()
chains = generate_contig_chains(S)
contigs = [assemble_chain(chain, readseqs) for chain in chains]
with open("contigs.fa", "w") as f:
for i, contig in enumerate(contigs):
f.write(">contig{}\n{}\n".format(i+1, contig))
return 0
if __name__ == "__main__":
sys.exit(main(len(sys.argv), sys.argv))