-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSeqAlignParallel.py
442 lines (390 loc) · 15.1 KB
/
SeqAlignParallel.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
from multiprocessing import Process, Queue, Manager
from time import time, sleep
from sys import stderr
class AlignRecord(object):
"""class to store single align result"""
def __init__(self):
self.id = ''
self.des = ''
self.ls = 0
self.le = 0
self.lscore = 0
self.rs = 0
self.re = 0
self.rscore = 0
self.dir = ''
class AlignedSeq(object):
"""class to store multiple align results for one seq"""
def __init__(self,seq):
self.seq = seq
self.seqid = seq.id
self.barcode = ""
self.strain = ""
self.gene = ""
self.alnBarcode = ""
self.alnPrimer = ""
def TrimBarcode(self):
if(self.alnBarcode == ""):
raise ValueError ("Read %s has not been aligned with barcode" %(self.seqid))
else:
seq_s = self.alnBarcode.le + 1
seq_e = self.alnBarcode.rs
trimseq = self.seq[seq_s:seq_e]
if(self.alnBarcode.dir == "-"):
trimseq = trimseq.reverse_complement(id=True,name=True,description=True)
return trimseq
def TrimPrimer(self):
if(self.alnPrimer == ""):
raise ValueError ("Read %s has not been aligned with primer" %(self.seqid))
else:
seq_s = self.alnPrimer.le + 1
seq_e = self.alnPrimer.rs + 1
trimseq = self.seq[seq_s:seq_e]
if(self.alnPrimer.dir == "-"):
trimseq = trimseq.reverse_complement(id=True,name=True,description=True)
return trimseq
def BarcodeFreeRegion(self):
seq_s = self.alnBarcode.le + 1
seq_e = self.alnBarcode.rs
return (seq_s,seq_e)
def LocusLength(self):
seq_s = self.alnPrimer.le + 1
seq_e = self.alnPrimer.rs + 1
length = seq_e - seq_s
return (length)
class AlignRes(object):
"""class to store several align results for single sequence"""
def __init__(self,seq):
self.seq = seq
self.aligns = []
def __len__(self):
return len(self.aligns)
def __getitem__(self, index):
return self.aligns[index]
def append(self,alignRes):
self.aligns.append(alignRes)
def __best(self):
if(len(self.aligns) < 1):
raise ValueError ("Error: AlignRes does not contain any record")
else:
best = 0
bestscore = self.aligns[0].lscore + self.aligns[0].rscore
for i in range(0,len(self.aligns)):
score = self.aligns[i].lscore + self.aligns[i].rscore
if(score > bestscore):
best = i
bestscore = score
return i
def BestAlign(self):
index = self.__best()
alnRec = self.aligns[index]
return alnRec
class SeqAlignments(object):
"""class to store all aligned sequences"""
def __init__(self, projenv):
self.alignedseqs = []
self.seqgroups = []
self.workers = []
self.paras = projenv.parameters
self.seqs = projenv.Seqs
self.barcodes = projenv.Barcodes
self.primers = projenv.Primers
self.msgHandle = projenv
self.threadNum = self.paras.Threads
self.symbarcode = projenv.SymBarcode
# def Run(self):
# stats = list()
# alignedseqs = list()
# unbarcode = list()
# unprimer = list()
# primeredseqs = list()
#
# self.msgHandle.showMsg("Searching for barcodes in reads...")
# BarcodeSearch(self.paras, self.seqs, self.barcodes, self.symbarcode, stats, alignedseqs,
# unbarcode)
#
# self.msgHandle.showMsg("Searching for primers in reads...")
# PrimerSearch(self.paras, alignedseqs, self.primers, stats, unprimer, primeredseqs)
#
# self.alignedseqs = alignedseqs
# self.num_unbarcode = sum(unbarcode)
# self.num_unprimer = sum(unprimer)
# self.msgHandle.showMsg('Done!')
def Run(self):
self.msgHandle.showMsg("Searching for barcodes in reads...")
groupnum = int(len(self.seqs)/self.threadNum)
for i in range(self.threadNum):
startnum = groupnum * i
endnum = groupnum * (i+1)
if(i == self.threadNum - 1):
endnum = len(self.seqs)
self.seqgroups.append(self.seqs[startnum:endnum])
manager = Manager()
alignedseqs = manager.list()
primeredseqs = manager.list()
stats = manager.list()
unbarcode = manager.list()
unprimer = manager.list()
for i in range(self.threadNum):
self.msgHandle.showMsg("Open thread +1")
child = Process(target=BarcodeSearch,
args=(self.paras, self.seqgroups[i],
self.barcodes, self.symbarcode, stats, alignedseqs, unbarcode))
self.msgHandle.showMsg("Searching for barcodes in reads...")
child.start()
self.workers.append(child)
totalcount = 0
while any(i.is_alive() for i in self.workers):
sleep(0.1)
while len(stats) > 0:
totalcount += 100
if(totalcount % 1000 == 0):
self.msgHandle.showMsg("%s reads have been processed..."
%(totalcount))
stats.pop()
self.msgHandle.showMsg('Done!')
for self.worker in self.workers:
self.worker.join()
self.workers = []
self.msgHandle.showMsg("Searching for primers in reads...")
for i in range(self.threadNum):
child = Process(target=PrimerSearch,
args=(self.paras, alignedseqs[i],
self.primers, stats, unprimer,primeredseqs))
child.start()
self.workers.append(child)
totalcount = 0
while any(i.is_alive() for i in self.workers):
sleep(0.1)
while len(stats) > 0:
totalcount += 100
if(totalcount % 1000 == 0):
self.msgHandle.showMsg("%s reads have been processed..."
%(totalcount))
stats.pop()
for self.worker in self.workers:
self.worker.join()
self.alignedseqs = [aligned for aligns in primeredseqs for aligned in aligns]
self.num_unbarcode = sum(unbarcode)
self.num_unprimer = sum(unprimer)
self.msgHandle.showMsg('Done!')
def Stop(self):
for worker in self.workers:
if(worker.is_alive()):
worker.terminate()
worker.join()
return
#def BarcodeSearch(self):
def BarcodeSearch(paras, seqs, barcodes, symbarcode, stats, result, unbarcode):
seqcount = 0
unmapcount = 0
alignedseqs = []
padlen = paras.PadLength - paras.FlankingLength
if(padlen < 0):
padlen = 0
reglen = 2*paras.FlankingLength + paras.BarcodeLen
for seq in seqs:
# print(seq)
seqcount += 1
if(seqcount % 100 == 0):
stats.append(1)
isMatch,alnrec = _SeqSearch(paras, seq, barcodes, padlen, reglen)
if(isMatch):
alignSeq = AlignedSeq(seq)
alignSeq.barcode = alnrec.id
alignSeq.strain = alnrec.des
alignSeq.alnBarcode = alnrec
alignedseqs.append(alignSeq)
else:
if(symbarcode):
alignSeq = AlignedSeq(seq)
alignSeq.barcode = ''
alignSeq.strain = ''
alignSeq.alnBarcode = ''
alignedseqs.append(alignSeq)
unmapcount += 1
else:
seq_rev = seq.reverse_complement()
isMatch,alnrec = _SeqSearch(paras, seq_rev, barcodes, padlen, reglen)
if(isMatch):
alignSeq.barcode = alnrec.id
alignSeq.strain = alnrec.des
alignSeq.alnBarcode = alnrec
alignedseqs.append(alignSeq)
else:
alignSeq = AlignedSeq(seq)
alignSeq.barcode = ''
alignSeq.strain = ''
alignSeq.alnBarcode = ''
alignedseqs.append(alignSeq)
unmapcount += 1
result.append(alignedseqs)
unbarcode.append(unmapcount)
def PrimerSearch(paras, alignedseqs, primers, stats, unprimer, primeredseqs):
#stderr.write ('\nSearching for self.primers in reads...\n')
seqcount = 0
unmappcount = 0
maxprimerlen = _MaxPrimerLen(primers)
padlen = paras.UniLength - paras.FlankingLength
if(padlen < 0):
padlen = 0
reglen = 2*paras.FlankingLength + maxprimerlen
primered = []
for barcodedseq in alignedseqs:
seqcount += 1
if(seqcount % 100 == 0):
stats.append(1)
#stderr.write (unicode(seqcount) + " reads have been processed...\n")
if(barcodedseq.barcode == ''): continue
seqs,seqe = barcodedseq.BarcodeFreeRegion()
seqr = len(barcodedseq.seq) - seqe
trimed_seq = barcodedseq.TrimBarcode()
isMatch,alnrec = _SeqSearch(paras,trimed_seq,primers,padlen,reglen)
if(isMatch):
alnrec = _AlignAddPad(alnrec,seqs)
barcodedseq.alnPrimer = alnrec
barcodedseq.gene = alnrec.id
else:
trimed_seq_rv = trimed_seq.reverse_complement()
#print (trimed_seq_rv.format("fasta"))
isMatch,alnrec = _SeqSearch(paras,trimed_seq_rv,primers,padlen,reglen)
if(isMatch):
alnrec = _AlignAddPad(alnrec,seqr)
seq_len = len(barcodedseq.seq) - 1
ls = seq_len - alnrec.re
le = seq_len - alnrec.rs
rs = seq_len - alnrec.le
re = seq_len - alnrec.ls
alnrec.lscore, alnrec.rscore = alnrec.rscore,alnrec.lscore
alnrec.ls = ls
alnrec.le = le
alnrec.rs = rs
alnrec.re = re
alnrec.dir = "-"
barcodedseq.gene = alnrec.id
barcodedseq.alnPrimer = alnrec
primered.append(barcodedseq)
if(not isMatch): unmappcount += 1
primeredseqs.append(primered)
unprimer.append(unmappcount)
def _SeqSearch(paras,seq,refseqs,padlen,reglen):
import SWAlign
seq_len = len(seq)
totallen = padlen + reglen
seq_L = seq[padlen:totallen]
seq_R = seq[(seq_len - totallen):(seq_len - padlen)]
seq_Rr = seq_R.reverse_complement()
trim_len = seq_len - 2*padlen
alignRes = AlignRes(seq)
sw = SWAlign.LocalAlignment(SWAlign.NucleotideScoringMatrix
(paras.MatchScore, paras.MismatchScore), paras.GapScore)
isMatch = 0
isMatch,qrec = _QuickSearch(seq_L,seq_Rr,refseqs,sw,paras,trim_len)
if(isMatch == 1):
qrec = _AlignAddPad(qrec,padlen)
alignRes.append(qrec)
elif(isMatch == 0):
for refseq in refseqs:
alnrec = AlignRecord()
l_align = _SeqAlign(refseq.f,seq_L,sw)
r_align = _SeqAlign(refseq.r,seq_Rr,sw)
if(l_align['mismatch'] <= paras.MaxMisMatch and r_align['mismatch'] <= paras.MaxMisMatch):
#if((l_align['score'] + r_align['score'])/2 >= self.paras.MinBarcodeScore):
if(l_align['score'] >= refseq.fs and r_align['score'] >= refseq.rs):
#print ("###",barcode)
isMatch = 1
r_s = seq_len - r_align['e'] - 1
r_e = seq_len - r_align['s'] - 1
alnrec.id = refseq.id
alnrec.des = refseq.des
alnrec.ls = l_align['s']
alnrec.le = l_align['e']
alnrec.lscore = l_align['score']
alnrec.rs = r_s
alnrec.re = r_e
alnrec.rscore = r_align['score']
alnrec.dir = '+'
alnrec = _AlignAddPad(alnrec,padlen)
alignRes.append(alnrec)
if(isMatch == 1):
alnrec = alignRes.BestAlign()
return (True,alnrec)
else:
return (False,"")
def _QuickSearch(seqL,seqR,refseqs,sw,paras,seqlen):
matchscore = paras.MatchScore
mismatchscore = paras.MismatchScore
gapscore = paras.GapScore
maxmismatch = paras.MaxMisMatch
alignRes = AlignRecord()
Matched = 0
lseq = seqL.seq
rseq = seqR.seq
for refseq in refseqs:
posL = lseq.find(refseq.f)
posR = rseq.find(refseq.r)
if(posL >= 0):
alignRes.id = refseq.id
alignRes.des = refseq.des
alignRes.ls = posL
alignRes.le = posL + refseq.fl -1
alignRes.lscore = refseq.fl * matchscore
alignRes.dir = '+'
if(posR >= 0):
Matched = 1
rs = posR
re = posR + refseq.rl -1
alignRes.rs = seqlen - re -1
alignRes.re = seqlen - rs -1
alignRes.rscore = refseq.rl * matchscore
break
else:
align = _SeqAlign(refseq.r,seqR,sw)
if(align['mismatch'] <= maxmismatch and align['score'] >= refseq.fs):
Matched = 1
alignRes.rs = seqlen - align['e'] -1
alignRes.re = seqlen - align['s'] -1
alignRes.rscore = align['score']
else:
Matched = 2
break
else:
if(posR >= 0):
alignRes.id = refseq.id
alignRes.des = refseq.des
Matched = 1
alignRes.dir = '+'
rs = posR
re = posR + refseq.rl -1
alignRes.rs = seqlen - re -1
alignRes.re = seqlen - rs -1
alignRes.rscore = refseq.rl * matchscore
align = _SeqAlign(refseq.f,seqL,sw)
if(align['mismatch'] <= maxmismatch and align['score'] >= refseq.fs):
Matched = 1
alignRes.ls = align['s']
alignRes.le = align['e']
alignRes.lscore = align['score']
else:
Matched = 2
break
return (Matched,alignRes)
def _AlignAddPad(alnrec,padlen):
alnrec.ls = alnrec.ls + padlen
alnrec.le = alnrec.le + padlen
alnrec.rs = alnrec.rs + padlen
alnrec.re = alnrec.re + padlen
return alnrec
def _SeqAlign(ref,query,sw):
query = query.seq
align = sw.align(ref,query)
start = align.q_pos
end = align.q_end - 1
#align.dump()
return {'s':start,'e':end,'score':align.score,'mismatch':align.mismatches}
def _MaxPrimerLen(primers):
maxlen = 0
for primer in primers:
if (primer.fl > maxlen): maxlen = primer.fl
if (primer.rl > maxlen): maxlen = primer.rl
return maxlen