-
Notifications
You must be signed in to change notification settings - Fork 16
/
IORobot.py
642 lines (446 loc) · 19.6 KB
/
IORobot.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
import os
import houseKeeper
import graphLib
import alignerRobot
from operator import itemgetter
from itertools import groupby
# ## 0) Preprocess by removing embedded contigs (I: contigs.fasta ; O : noEmbed.fasta)
def obtainLength(folderName, fileName):
f = open(folderName + fileName, 'r')
tmp = f.readline().rstrip()
lenDic = {}
tmplen = 0
tmpName = ""
while len(tmp) > 0:
if tmp[0] == '>':
if tmplen != 0:
lenDic[tmpName] = tmplen
tmplen = 0
tmpName = tmp[1:]
else:
tmplen += len(tmp)
tmp = f.readline().rstrip()
lenDic[tmpName] = tmplen
f.close()
return lenDic
def findContigLength(folderName, option):
if option == "contigs":
print "\n\nfindContigLength(folderName)\n"
contigLength = {}
f = open(folderName + "smaller_contigs_Double.fasta", 'r')
tmp1 = f.readline().rstrip()
if len(tmp1) > 0:
tmp2 = f.readline().rstrip()
while len(tmp1) > 0 and len(tmp2) > 0:
contigLength[tmp1[1:]] = len(tmp2)
tmp1 = f.readline().rstrip()
if len(tmp1) > 0:
tmp2 = f.readline().rstrip()
f.close()
else:
print "\n\nfindContigLength(folderName)\n"
contigLength = {}
f = open(folderName + "improved_Double.fasta", 'r')
tmp1 = f.readline().rstrip()
if len(tmp1) > 0:
tmp2 = f.readline().rstrip()
while len(tmp1) > 0 and len(tmp2) > 0:
contigLength[tmp1[1:]] = len(tmp2)
tmp1 = f.readline().rstrip()
if len(tmp1) > 0:
tmp2 = f.readline().rstrip()
f.close()
f = open(folderName + "relatedReads_Double.fasta", 'r')
tmp1 = f.readline().rstrip()
if len(tmp1) > 0:
tmp2 = f.readline().rstrip()
while len(tmp1) > 0 and len(tmp2) > 0:
contigLength[tmp1[1:]] = len(tmp2)
tmp1 = f.readline().rstrip()
if len(tmp1) > 0:
tmp2 = f.readline().rstrip()
f.close()
return contigLength
def putListToFileO(folderName, sourceFileName, targetFileName, myList):
f = open(folderName + targetFileName + ".txt", 'w')
for eachitem in myList:
f.write(eachitem + '\n')
f.close()
command = "perl -ne 'if(/^>(\S+)/){$c=$i{$1}}$c?print:chomp;$i{$_}=1 if @ARGV' " + folderName + targetFileName + ".txt " + folderName + sourceFileName + " > " + folderName + targetFileName + ".fasta"
os.system(command)
def writeToFile_Double1(folderName, fileName1, fileName2, option="contig"):
f2 = open(folderName + fileName2, 'w')
fOriginal = open(folderName + fileName1, 'r')
readSet = []
tmp = fOriginal.readline().rstrip()
tmpRead = ""
while len(tmp) > 0:
if tmp[0] == '>':
if len(tmpRead) > 0:
readSet.append(tmpRead)
tmpRead = ""
else:
tmpRead = tmpRead + tmp
tmp = fOriginal.readline().rstrip()
readSet.append(tmpRead)
print "len(readSet)", len(readSet)
fOriginal.close()
if option == "contig":
header = ">Contig"
else:
header = ">Read"
for eachcontig, dum in zip(readSet, range(len(readSet))):
f2.write(header + str(dum) + "_p\n")
f2.write(eachcontig + '\n')
f2.write(header + str(dum) + "_d\n")
f2.write(houseKeeper.reverseComplement(eachcontig) + '\n')
f2.close()
def loadContigsFromFile(folderName, fileName):
f = open(folderName + fileName, 'r')
tmp = f.readline().rstrip()
dataDic = {}
tmpSeq = ""
tmpName = ""
while len(tmp) > 0:
if tmp[0] == '>':
if len(tmpSeq) != 0:
dataDic[tmpName] = tmpSeq
tmpSeq = ""
tmpName = tmp[1:]
else:
tmpSeq += tmp
tmp = f.readline().rstrip()
dataDic[tmpName] = tmpSeq
f.close()
return dataDic
def writeToFile(f2, runningIndex, seq):
f2.write(">Seg_" + str(runningIndex))
f2.write('\n')
f2.write(seq)
f2.write('\n')
# ## 5) Read the contigs out (I: startList, graphNodes, ; O:improved.fasta, openZone.txt)
def useAlignToGetLen(eachnode, i , nameDic, orientation, myContigsDic, readNum, folderName , mummerLink):
indexToAddNext = eachnode.nodeIndexList[i+1]
readNumNext = indexToAddNext / 2
orientationNext = indexToAddNext % 2
if len(nameDic) > 0:
orientationNext = nameDic[indexToAddNext]%2
readNumNext = nameDic[indexToAddNext]/2
if orientation == 0:
leftSeg= myContigsDic['Contig' + str(readNum) + '_' + 'p']
else:
leftSeg = myContigsDic['Contig' + str(readNum) + '_' + 'd']
if orientationNext == 0 :
rightSeg = myContigsDic['Contig' + str(readNumNext) + '_' + 'p']
else:
rightSeg = myContigsDic['Contig' + str(readNumNext) + '_' + 'd']
overlapInfo= align(leftSeg, rightSeg, folderName, mummerLink)
overlapLen = overlapInfo[0]
return overlapLen
def readContigOut(folderName, mummerLink, graphFileName, contigFile, outContigFile, outOpenList, nameDic={}):
print "readContigOut"
G = graphLib.seqGraph(0)
G.loadFromFile(folderName, graphFileName)
G.findStartEndList()
myContigsDic = loadContigsFromFile(folderName, contigFile)
contigUsed = [False for i in range(len(G.graphNodesList) / 2)]
seqToPrint = []
openList = []
noForRevMismatch = True
print len(G.graphNodesList)
for eachnode in G.graphNodesList:
print eachnode.nodeIndexList
if len(eachnode.nodeIndexList) > 0:
tmpSeq = ""
# ## debug consistency of t/f
ckList = []
for dummy in eachnode.nodeIndexList:
indexToAdd = dummy
readNum = indexToAdd / 2
ckList.append(contigUsed[readNum])
if (len(ckList) > 0 and not all(ckList) and any(ckList)):
noForRevMismatch = False
# ## end debug
if contigUsed[eachnode.nodeIndexList[0]/2] == False:
contigUsed[eachnode.nodeIndexList[0]/2] = True
contigUsed[eachnode.nodeIndexList[-1]/2] = True
for i in range(len(eachnode.nodeIndexList)):
indexToAdd = eachnode.nodeIndexList[i]
readNum = indexToAdd / 2
orientation = indexToAdd % 2
#print nameDic[indexToAdd]
if len(nameDic) > 0:
orientation = nameDic[indexToAdd]%2
readNum = nameDic[indexToAdd]/2
#print readNum
if i != len(eachnode.nodeIndexList) - 1:
overlapLenOld = eachnode.overlapList[i]
# Can we hijack here for the overlap Length... seems like minimal changes
overlapLen = useAlignToGetLen(eachnode, i , nameDic, orientation, myContigsDic, readNum, folderName , mummerLink)
# End Hijacking
print overlapLen, overlapLenOld
if orientation == 0:
tmpSeq = tmpSeq + myContigsDic['Contig' + str(readNum) + '_' + 'p'][0:-overlapLen]
else:
tmpSeq = tmpSeq + myContigsDic['Contig' + str(readNum) + '_' + 'd'][0:-overlapLen]
else:
if orientation == 0:
tmpSeq = tmpSeq + myContigsDic['Contig' + str(readNum) + '_' + 'p']
else:
tmpSeq = tmpSeq + myContigsDic['Contig' + str(readNum) + '_' + 'd']
if len(tmpSeq) > 0:
if eachnode.nodeIndex in G.myStartList:
openList.append('Segkk' + str(len(seqToPrint)) + ',noprev')
if eachnode.nodeIndex in G.myEndList:
openList.append('Segkk' + str(len(seqToPrint)) + ',nonext')
seqToPrint.append(tmpSeq)
print "No forward/reverse mismatch ?", noForRevMismatch
fImproved = open(folderName + outContigFile, 'w')
for eachcontig, dummyIndex in zip(seqToPrint, range(len(seqToPrint))):
print len(eachcontig)
fImproved.write(">Segkk" + str(dummyIndex) + '\n')
fImproved.write(eachcontig + '\n')
fImproved.close()
print "All contigs used? ", all(contigUsed)
print "NContig", len(seqToPrint)
f = open(folderName + outOpenList, 'w')
f.write(str(len(seqToPrint)) + '\n')
for eachitem in openList:
f.write(str(eachitem) + str('\n'))
f.close()
def obtainLinkInfo(folderName, mummerLink, inputFile, mummerFile):
thres = 5
minLen = 400
# thres = 10
# minLen = 200
writeToFile_Double1(folderName, inputFile + ".fasta", inputFile + "_Double.fasta", "contig")
fmyFile = open(folderName + inputFile + "_Double.fasta", 'r')
fSmaller = open(folderName + inputFile + "_contigs_Double.fasta", 'w')
tmp = fmyFile.readline().rstrip()
maxSize = 50000
myName = ""
while len(tmp) > 0:
if tmp[0] == '>':
fSmaller.write(tmp + '\n')
myName = tmp[1:]
else:
component = tmp[0:min(len(tmp), maxSize)]
countComp = len(component)
fSmaller.write(component)
component = tmp[max(0, len(tmp) - maxSize):len(tmp)]
fSmaller.write(component)
countComp = countComp + len(component)
print "DebugName", myName, countComp
fSmaller.write('\n')
tmp = fmyFile.readline().rstrip()
fSmaller.close()
fmyFile.close()
if True:
alignerRobot.useMummerAlignBatch(mummerLink, folderName, [[mummerFile, inputFile + "_contigs_Double.fasta", inputFile + "_contigs_Double.fasta", ""]], houseKeeper.globalParallel )
# alignerRobot.useMummerAlign(mummerLink, folderName, mummerFile, inputFile + "_contigs_Double.fasta", inputFile + "_contigs_Double.fasta")
lengthDic = obtainLength(folderName, inputFile + "_contigs_Double.fasta")
dataSetRaw = alignerRobot.extractMumData(folderName, mummerFile + "Out")
# ## Format [ helperStart, helperEnd , readStart, readEnd,matchLen1,matchLen2,percentMatch,helperName,readName]
dataSet = []
for eachitem in dataSetRaw:
helperStart, helperEnd , readStart, readEnd, matchLen1, matchLen2, percentMatch, helperName, readName = eachitem
detailHelper = helperName.split('_')
detailRead = readName.split('_')
if detailHelper[0] != detailRead[0] and helperName != readName and max(matchLen1, matchLen2) > minLen and readStart < readEnd and min(helperStart, readStart) < thres and min(lengthDic[helperName] - helperEnd, lengthDic[readName] - readEnd) + 1 < thres:
conditionForMatch = True
else:
conditionForMatch = False
if conditionForMatch :
if helperStart < thres:
dataSet.append((max(matchLen1, matchLen2), readName, helperName))
dataSet.sort(reverse=True)
numberOfContig = len(lengthDic)
return numberOfContig, dataSet
def truncateEndOfContigs(folderName, filenameIn, filenameOut, maxSize, lengthDic):
'''
fmyFile = open(folderName + "improved_Double.fasta", 'r')
fSmaller = open(folderName + "smaller_improvedContig.fasta", 'w')
maxSize = 25000
truncateEndOfContigs(folderName, "improved_Double.fasta", "smaller_improvedContig.fasta", 25000, lengthDic)
'''
fmyFile = open(folderName + filenameIn, 'r')
fSmaller = open(folderName + filenameOut, 'w')
tmp = fmyFile.readline().rstrip()
myName = ""
while len(tmp) > 0:
if tmp[0] == '>':
fSmaller.write(tmp + '\n')
myName = tmp[1:]
else:
component = tmp[0:min(len(tmp), maxSize)]
countComp = len(component)
fSmaller.write(component)
component = tmp[max(0, len(tmp) - maxSize):len(tmp)]
fSmaller.write(component)
countComp = countComp + len(component)
lengthDic[myName] = countComp
print "DebugName", myName, countComp
fSmaller.write('\n')
tmp = fmyFile.readline().rstrip()
fSmaller.close()
fmyFile.close()
def obtainLinkInfoReadContig(dummyI, mummerLink, folderName,thres, lengthDic, K):
dataSet = []
indexOfMum = ""
if dummyI < 10:
indexOfMum = "0" + str(dummyI)
else:
indexOfMum = str(dummyI)
'''
command = mummerLink + "nucmer --maxmatch --simplify -p " + folderName + "outRefine " + folderName + "smaller_improvedContig.fasta " + "relatedReads_Double.part-" + indexOfMum + ".fasta"
os.system(command)
command = mummerLink + "show-coords -r " + folderName + "outRefine.delta > " + folderName + "fromMumRefine" + indexOfMum
os.system(command)
'''
f = open(folderName + "fromMumRefine" + indexOfMum, 'r')
for i in range(6):
tmp = f.readline()
while len(tmp) > 0:
info = tmp.split('|')
filterArr = info[1].split()
rdGpArr = info[-1].split('\t')
firstArr = info[0].split()
matchLenArr = info[2].split()
matchLen = int(matchLenArr[1])
contigStart, contigEnd = int(firstArr[0]), int(firstArr[1])
readStart, readEnd = int(filterArr[0]) , int(filterArr[1])
contigName = rdGpArr[0].rstrip().lstrip()
readName = rdGpArr[1].rstrip().lstrip()
if readStart < readEnd and matchLen > K and min(contigStart, readStart) < thres and min(lengthDic[contigName] - contigEnd , lengthDic[readName] - readEnd) + 1 < thres:
conditionForMatch = True
else:
conditionForMatch = False
if conditionForMatch :
if contigStart < thres:
dataSet.append((readName, contigName, 'L', matchLen))
if lengthDic[contigName] - contigEnd + 1 < thres :
dataSet.append((readName, contigName, 'R', matchLen))
tmp = f.readline()
f.close()
return dataSet
### read from overlap
def writeSegOut(ctgList, folderName, fileout):
f = open(folderName + fileout, 'w')
for i in range(len(ctgList)):
f.write(">Segkk" + str(i) +'\n')
f.write(ctgList[i])
f.write("\n")
f.close()
def checkIncluded(tmp, markedList):
isIncluded = False
for i in tmp:
if markedList[i/2] == True:
isIncluded = True
return isIncluded
def align(leftSeg, rightSeg, folderName, mummerLink):
overlap = [0, 0 ]
lLen = 0
f = open(folderName + "leftSeg.fasta", 'w')
f.write(">SegL\n")
if len(leftSeg) < 50000:
f.write(leftSeg)
lLen = len(leftSeg)
else:
f.write(leftSeg[-50000:])
lLen = 50000
f.close()
rLen = 0
f = open(folderName + "rightSeg.fasta", 'w')
f.write(">SegR\n")
if len(rightSeg) < 50000:
f.write(rightSeg)
rLen = len(rightSeg)
else:
f.write(rightSeg[0:50000])
rLen = 50000
f.close()
alignerRobot.useMummerAlign(mummerLink, folderName, "overlap", "leftSeg.fasta", "rightSeg.fasta", False)
dataList = alignerRobot.extractMumData(folderName , "overlapOut")
thres = 10
if len(dataList) == 0:
overlap = [0, 0 ]
else:
myMax = [0, 0]
for eachitem in dataList:
if eachitem[1] > lLen - thres and eachitem[2] < thres:
if eachitem[5] > myMax[1]:
myMax[0] = eachitem[4]
myMax[1] = eachitem[5]
overlap = myMax
return overlap
def joinSeg(tmp, folderName, segLookUp, mummerLink):
ctg = segLookUp[tmp[0]]
tmpList = []
for i in range(len(tmp)-1):
overlapArr = align(segLookUp[tmp[i]], segLookUp[tmp[i+1]], folderName, mummerLink)
overlap = overlapArr[1]
print "overlap : ", overlap
if overlap >= 0 :
ctg = ctg + segLookUp[tmp[i+1]][overlap:]
else:
tmpList.append(ctg)
ctg = segLookUp[tmp[i+1]]
tmpList.append(ctg)
return tmpList
def readContigsFromFile(folderName, filename):
segLookUp = []
f = open(folderName + filename, 'r')
tmp = f.readline().rstrip()
tmpStr = ""
while len(tmp) > 0:
if tmp[0] == '>':
if len(tmpStr) > 0:
segLookUp.append(tmpStr)
tmpStr = ""
else:
tmpStr = tmpStr + tmp
tmp = f.readline().rstrip()
if len(tmpStr) > 0:
segLookUp.append(tmpStr)
tmpStr = ""
f.close()
return segLookUp
def extractGraphToContigs(G, folderName, mummerLink, fileout, filein):
N1 = len(G.graphNodesList)
markedList = [False for i in range(N1/2)]
segLookUp = readContigsFromFile(folderName, filein)
ctgList = []
for eachnode in G.graphNodesList:
if len(eachnode.nodeIndexList) > 0:
tmp = eachnode.nodeIndexList
isIncluded = checkIncluded(tmp, markedList)
for eachitem in tmp:
markedList[eachitem/2] = True
if not isIncluded :
ctgtmpList = joinSeg(tmp, folderName, segLookUp, mummerLink)
ctgList = ctgList + ctgtmpList
writeSegOut(ctgList, folderName, fileout)
def fillInMissed(folderName, mummerLink, filerefname, filequeryname, fileoutname):
os.system("mv " + folderName + fileoutname + " " + folderName + filequeryname )
alignerRobot.useMummerAlignBatch(mummerLink, folderName, [[fileoutname+"fillmiss", filerefname, filequeryname, ""]], houseKeeper.globalParallel)
dataList = alignerRobot.extractMumData(folderName, fileoutname+"fillmissOut")
lenDic = obtainLength(folderName, filerefname)
### Check if there is any missing parts
# Format of the dataList : 1 765 | 11596 10822 | 765 775 | 84.25 | ref_NC_001133_ scf7180000000702"
dataList.sort(key = itemgetter(-2))
thres = 100
extraList = []
for key, items in groupby(dataList, itemgetter(-2)):
isFound = False
for eachitem in items:
if abs(int(eachitem[4]) - lenDic[key]) < thres:
isFound = True
break
if not isFound:
extraList.append(key)
### Fill in any missing items
referenceDic = loadContigsFromFile(folderName, filerefname)
queryDic = loadContigsFromFile(folderName, filequeryname)
ctgList = [referenceDic[eachitem] for eachitem in extraList] + [queryDic[eachitem] for eachitem in queryDic]
writeSegOut(ctgList, folderName, fileoutname)
print "fileoutname: len(extraList)",fileoutname, len(extraList), len(ctgList)