-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRegIndels_ClassIDFile.py
executable file
·610 lines (458 loc) · 21.9 KB
/
RegIndels_ClassIDFile.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
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 16 16:12:27 2015
@author: Gillian
"""
# takes alignments from Indels and finds read partner.
# all Far Junction and Scrambled junction reads where the alignment does not overlap the
# Junction by args.window bp will be thrown out.
################
#Current categories
#linear -- genomeGood, RegGood, JuncGood, RegIndelGood
#anomaly -- genomeBad, RegBad, JuncBad, RegIndelBad
##################
import argparse
import os
import glob
def AddToDict(inputtype, line_raw_comparison, line_raw_RI):
lineRI = ReadInfoRI(line_raw_RI)
if inputtype=="RI": # if comparing Far Junc to Far Junc, they have to be identical
line2= ReadInfoRI(line_raw_comparison)
IDfileoutputR1 = str(lineRI.offset) +"\t" + str(lineRI.MAPQ) +"\t" + str(lineRI.adjAS) + "\t" + lineRI.NumN + "\t"+ str(lineRI.NumOfBases) + "\t" +lineRI.junction[:-5]+"\t"+lineRI.refstrand
IDfileoutputR2 = str(line2.offset) + "\t" + str(line2.MAPQ) + "\t"+ str(line2.adjAS) + "\t"+ line2.NumN + "\t" + str(line2.NumOfBases) + "\t" + line2.junction + "\t" + line2.refstrand
if lineRI.junction == line2.junction and lineRI.refstrand in ["0","16"] and line2.refstrand in ["0","16"] and lineRI.refstrand!=line2.refstrand:
IDfiletype = "linear,RegIndelGood,"+lineRI.junction[-4:]
else:
IDfiletype = "anomaly,RegIndelBad,"+lineRI.junction[-4:]
IDfile.write(line_raw_RI.split("\t")[0]+"\t"+IDfiletype+"\t"+IDfileoutputR1+"\t"+IDfileoutputR2+"\n")
if inputtype=="reg":
line2= ReadInfoJunc(line_raw_comparison)
IDfileoutputR1 = str(lineRI.offset) +"\t" + str(lineRI.MAPQ) +"\t" + str(lineRI.adjAS) + "\t" + lineRI.NumN + "\t"+ str(lineRI.NumOfBases) + "\t" +lineRI.junction[:-5]+"\t"+lineRI.refstrand
IDfileoutputR2 = str(line2.offset) + "\t" + str(line2.MAPQ) + "\t"+ str(line2.adjAS) + "\t"+ line2.NumN + "\t" + str(line2.NumOfBases) + "\t" + line2.junction + "\t" + line2.refstrand
IDfiletype = "linear,RegGood,"+lineRI.junction[-4:]
if lineRI.chr==line2.chr and lineRI.refstrand!=line2.refstrand and lineRI.strand==line2.strand:
if lineRI.strand=="+":
if line2.loc_right < lineRI.loc_left or line2.loc_left > lineRI.loc_right:
pass
else:
IDfiletype="anomaly,RegBad,"+lineRI.junction[-4:]
elif lineRI.strand=="-":
if line2.loc_right > lineRI.loc_left or line2.loc_left < lineRI.loc_right:
pass
else:
IDfiletype="anomaly,RegBad,"+lineRI.junction[-4:]
else:
IDfiletype="anomaly,RegBad,"+lineRI.junction[-4:]
IDfile.write(line_raw_RI.split("\t")[0]+"\t"+IDfiletype+"\t"+IDfileoutputR1+"\t"+IDfileoutputR2+"\n")
if inputtype =="junc": #if reg or junc read, then one side has to be within 100KB, and meets refstrand criteria below
line2 = ReadInfoJunc(line_raw_comparison)
IDfileoutputR1 = str(lineRI.offset) +"\t" + str(lineRI.MAPQ) +"\t" + str(lineRI.adjAS) + "\t" + lineRI.NumN + "\t"+ str(lineRI.NumOfBases) + "\t" +lineRI.junction[:-5]+"\t"+lineRI.refstrand
IDfileoutputR2 = str(line2.offset) + "\t" + str(line2.MAPQ) + "\t"+ str(line2.adjAS) + "\t"+ line2.NumN + "\t" + str(line2.NumOfBases) + "\t" + line2.junction + "\t" + line2.refstrand
IDfiletype = "linear,JuncGood,"+lineRI.junction[-4:]
if lineRI.chr==line2.chr and lineRI.refstrand!=line2.refstrand and lineRI.strand==line2.strand and min(line2.loc_left, line2.loc_right)< lineRI.loc_left and min(line2.loc_left, line2.loc_right)< lineRI.loc_right and max (line2.loc_left, line2.loc_right) > lineRI.loc_left and max(line2.loc_left, line2.loc_right)> lineRI.loc_right:
pass
else:
IDfiletype="anomaly,JuncBad,"+lineRI.junction[-4:]
IDfile.write(line_raw_RI.split("\t")[0]+"\t"+IDfiletype+"\t"+IDfileoutputR1+"\t"+IDfileoutputR2+"\n")
if inputtype == "genome": #comparing FJ to genome, has to be within 100Kbp, meet ref strand criteria (opp refstrand if + read, same refstrand if - read)
line2 = ReadInfoGenome(line_raw_comparison)
IDfileoutputR1 = str(lineRI.offset) +"\t" + str(lineRI.MAPQ) +"\t" + str(lineRI.adjAS) + "\t" + lineRI.NumN + "\t"+ str(lineRI.NumOfBases) + "\t" +lineRI.junction[:-5]+"\t"+lineRI.refstrand
IDfileoutputR2 = str(line2.loc) + "\t" + str(line2.MAPQ) + "\t"+ str(line2.adjAS) + "\t"+ line2.NumN + "\t" + str(line2.NumOfBases) + "\t" + line2.chr + "\t" + line2.refstrand
IDfiletype = "linear,genomeGood,"+lineRI.junction[-4:]
if lineRI.chr==line2.chr and lineRI.refstrand!=line2.refstrand and lineRI.strand=="+":
if line2.loc>lineRI.loc_right or line2.loc<lineRI.loc_left:
pass
else:
IDfiletype = "anomaly,genomeBad,"+lineRI.junction[-4:]
elif lineRI.chr==line2.chr and lineRI.refstrand!=line2.refstrand and lineRI.strand=="-":
if line2.loc>lineRI.loc_left or line2.loc > lineRI.loc_right:
pass
else:
IDfiletype = "anomaly,genomeBad,"+lineRI.junction[-4:]
else:
IDfiletype = "anomaly,genomeBad,"+lineRI.junction[-4:]
IDfile.write(line_raw_RI.split("\t")[0]+"\t"+IDfiletype+"\t"+IDfileoutputR1+"\t"+IDfileoutputR2+"\n")
def ID(string):
if string[-2:] == "/1" or string[-2:] == "/2":
return string[:-2]
else:
return string
class ReadInfoRI:
def __init__ (self,line_raw):
line = line_raw.strip().split("\t")
self.ID = ID(line[0])
self.refstrand = line[1]
self.junction = line[2]
if line[2][-4:-1]=="DEL":
# print "is a deletion"
# print line[2][-1:]
self.indel=-int(line[2][-1:])
if line[2][-4:-1]=="INS":
self.indel=int(line[2][-1:])
self.MAPQ = int(line[4])
self.AS= int(line[11].split(":")[2])
self.NumOfBases = len(line[9])
self.offset = int(line[3])
if "XS:i:" in line[12]:
self.NumN=line[13][5:]
self.adjAS=int(line[11].split(":")[2])+int(line[13][5:])
else:
self.NumN=line[12][5:]
self.adjAS=int(line[11].split(":")[2])+int(line[12][5:])
JuncInfo = line[2].replace(":"," ").replace("|"," ").split(" ")
self.chr=JuncInfo[0]
self.loc_left=int(JuncInfo[2])
self.loc_right = int(JuncInfo[4])
self.strand = JuncInfo[6]
class ReadInfoGenome:
def __init__ (self, line_raw):
line = line_raw.strip().split("\t")
self.ID= ID(line[0])
self.refstrand = line[1]
self.chr = line[2]
self.loc = int(line[3])
self.MAPQ = int(line[4])
self.AS = int(line[11].split(":")[2])
self.NumOfBases=len(line[9])
self.offset = int(line[3])
if "XS:i:" in line[12]:
self.NumN=line[13][5:]
self.adjAS=int(line[11].split(":")[2])+int(line[13][5:])
else:
self.NumN=line[12][5:]
self.adjAS=int(line[11].split(":")[2])+int(line[12][5:])
class ReadInfoJunc:
def __init__ (self, line_raw):
line = line_raw.strip().split("\t")
self.ID = ID(line[0])
self.refstrand = line[1]
self.junction = line[2]
self.chr = line[2].split("|")[0]
self.loc_left = int(line[2].replace(":","|").split("|")[2])
self.loc_right = int(line[2].replace(":","|").split("|")[4])
self.strand = line[2][-1]
self.MAPQ = int(line[4])
self.AS = int(line[11].split(":")[2])
self.NumOfBases = len(line[9])
self.offset = int(line[3])
if "XS:i:" in line[12]:
self.NumN=line[13][5:]
self.adjAS=int(line[11].split(":")[2])+int(line[13][5:])
else:
self.NumN=line[12][5:]
self.adjAS=int(line[11].split(":")[2])+int(line[12][5:])
#=========================================
#start here
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--stem", required=True, help = "stem name of file to generate report")
parser.add_argument("-c", "--circReads", required= True, help = "path to circReads Dir")
parser.add_argument("-i", "--origDir", required=True, help = "path to orig dir containing genome reads")
parser.add_argument("-w", "--window", required=True, help = "# of bases needed on each side of the junction")
args = parser.parse_args()
window= int(args.window)
if args.origDir[-1]!="/":
args.origDir+="/"
if args.circReads[-1]!="/":
args.circReads+="/"
stem = args.stem
regIndelfiles=[]
regfiles=[]
genomefiles=[]
junctionfiles=[]
for name in glob.glob(args.origDir + "RegIndelAlignments/"+ stem + "/*.sam"):
print name
if "All_" not in name:
regIndelfiles.append(name)
# Regfiles contains indel alignments for _1 and _2 files to indels 1-5
for name in glob.glob(os.path.join(args.origDir,"genome/*" + stem + "*.sam")):
print name
if "sorted" not in name:
genomefiles.append(name)
for name in glob.glob(os.path.join(args.origDir,"reg/*" + stem + "*.sam")):
print name
if "sorted" not in name:
regfiles.append(name)
for name in glob.glob(os.path.join(args.origDir,"junction/*" + stem + "*.sam")):
print name
if "sorted" not in name:
junctionfiles.append(name)
# opening all files for a particular stem
#print sorted(regIndelfiles)
#print sorted(genomefiles)
#print sorted(regfiles)
#print sorted(junctionfiles)
## concatenate all reg indels files into a single big indels file
regIndel1_list = sorted(regIndelfiles)[0:len(regIndelfiles)/2]
regIndel2_list = sorted(regIndelfiles)[len(regIndelfiles)/2:]
IndelsReadIDs={}
for name in regIndel1_list:
print "reg1 indels"
print name
f1= open(name, mode="rU")
for line in f1:
if line[0]=="@":
continue
read = ReadInfoRI(line)
# if the read overlaps the junction
if read.offset <= (150-int(args.window)+read.indel) and read.offset+read.NumOfBases >= (150+int(args.window)+read.indel):
# if the read isn't in dictionary then add it
if read.ID not in IndelsReadIDs:
IndelsReadIDs[read.ID]= line
# if the read is in the dictionary, compare it to existing read. If AS is better, then replace existing read
else:
compareRead= ReadInfoRI(IndelsReadIDs[read.ID])
if int(compareRead.AS) >= int(read.AS):
pass
else:
IndelsReadIDs[read.ID]= line
f1.close()
# write all distinct readIDs to an All_1_indels file
fout_regIndel1= open(args.origDir + "RegIndelAlignments/"+ stem + "/All_" + stem + "_1_Regindels.sam", mode="w")
for key in IndelsReadIDs:
fout_regIndel1.write(IndelsReadIDs[key].strip()+"\n")
fout_regIndel1.close()
## CLEAR Read IDs dictionary and do the same with FJ2 list
IndelsReadIDs={}
for name in regIndel2_list:
print "regIndel2 indels"
print name
f1= open(name, mode="rU")
for line in f1:
if line[0]=="@":
continue
read = ReadInfoRI(line)
# if the read overlaps the junction
if read.offset <= (150-int(args.window)+read.indel) and read.offset+read.NumOfBases >= (150+int(args.window)+read.indel):
# if the read isn't in dictionary then add it
if read.ID not in IndelsReadIDs:
IndelsReadIDs[read.ID]= line
# if the read is in the dictionary, compare it to existing read. If AS is better, then replace existing read
else:
compareRead= ReadInfoRI(IndelsReadIDs[read.ID])
if int(compareRead.AS) >= int(read.AS):
pass
else:
IndelsReadIDs[read.ID]= line
f1.close()
# write all distinct readIDs to an All_1_indels file
fout_regIndel2= open(args.origDir + "RegIndelAlignments/"+ stem + "/All_" + stem + "_2_Regindels.sam", mode="w")
for key in IndelsReadIDs:
fout_regIndel2.write(IndelsReadIDs[key].strip()+"\n")
fout_regIndel2.close()
## open big indels files
f1_regIndel= open(args.origDir + "RegIndelAlignments/"+ stem + "/All_" + stem + "_1_Regindels.sam", mode ="rB")
f2_regIndel= open(args.origDir + "RegIndelAlignments/"+ stem + "/All_" + stem + "_2_Regindels.sam", mode="rB")
# ID file ReadID and different buckets.
# [0] = readID
# [1] = R2 in genome
# [2] = R2 in genome anomaly
# [3] = reg
# [4] = reg anom
# [5] = junc
# [6] = junc anom
IDfile = open(args.circReads+"ids/"+stem+"_temp_output_RegIndel.txt", mode= "w")
IDfile.write("ID\tclass\tR1_offset\tR1_MAPQ\tR1_adjAS\tR1_NumN\tR1_Readlength\tR1_JuncName\tR1_strand\tR2_offset\tR2_MAPQ\tR2_adjAS\tR2_NumN\tR2_Readlength\tR2_JuncName\tR2_strand\n")
#populate all reads and junctions into separate dictionaries
AllRegIndelRead1= {}
AllRegIndelRead2= {}
#AllJunctions = {}
#genomeDict = {} # for all these dictionaries, [0] = reg, [1] = anom
#regDict = {} # [2] = sum of AS, [3] = read length
#juncDict = {}
#unmappedDict= {} # start with all readIDs. if a partner is seen, then remove from list.
# populate AllFJRead1 dictionary - all read 1's from FarJunction alignments
# in order for R1 to feed into dictionary, must overlap entire offset (userspecified)
print "opening All_regIndels_1 file"
for line_raw in f1_regIndel:
if line_raw[0] =="@":
continue
RegIndel1read = ReadInfoRI(line_raw)
if RegIndel1read.offset <= (150+RegIndel1read.indel-window) and (RegIndel1read.offset+RegIndel1read.NumOfBases)>= 150+RegIndel1read.indel+window:
AllRegIndelRead1[RegIndel1read.ID] = [line_raw, 0]
# if FJ1read.junction not in AllJunctions:
# AllJunctions[FJ1read.junction]=0
# AllJunctions[FJ1read.junction] +=1
# unmappedDict[AllRegRead1.ID] = AllRegRead1.junction
f1_regIndel.close()
IDfile.flush()
# populate AllFJRead2 dictionary - all read 2's from FarJunc alignments
# in order for R1 to feed into dictionary, must overlap entire offset (userspecified)
print "opening All_regIndels _2 file"
for line_raw in f2_regIndel:
if line_raw[0] =="@":
continue
RegIndel2read = ReadInfoRI(line_raw)
# if FJ1read.junction=="chr1:S100A4:153516097:-|chr1:IFI16:158985661:+|strandcross":
# print "ERROR AT LINE 409"
# if R1 and R2 both in Far Junc, then add to FJ-FJ list
if RegIndel2read.ID in AllRegIndelRead1:
#print "found FJ read"
#AllFJRead1[FJ2read.ID][1]="FJ"
if RegIndel2read.offset <= (150+RegIndel2read.indel-window) and (RegIndel2read.offset+RegIndel2read.NumOfBases)>= 150+RegIndel2read.indel+window and AllRegIndelRead1[RegIndel2read.ID][1]==0:
AddToDict("RI",line_raw,AllRegIndelRead1[RegIndel2read.ID][0])
AllRegIndelRead1[RegIndel2read.ID][1]="RI"
# if FJ2read.ID in unmappedDict:
# del unmappedDict[FJ2read.ID]
# otherwise add to F2 read
else:
AllRegIndelRead2[RegIndel2read.ID]= [line_raw, 0]
# unmappedDict[FJ2read.ID] = FJ2read.junction
# if RegIndel2read.junction not in AllJunctions:
# AllJunctions[FJ2read.junction]=0
# AllJunctions[FJ2read.junction]+=1
f2_regIndel.close()
IDfile.flush()
f2_genome = open(sorted(genomefiles)[1], mode="rB")
# compare FJ read 1 to genome read 2
print "comparing Indels to genome_2"
for line_raw in f2_genome:
if line_raw[0] =="@":
continue
g2read = ReadInfoGenome(line_raw)
if g2read.ID in AllRegIndelRead1 and AllRegIndelRead1[g2read.ID][1]==0:
#print "found genome R2"+ g2read.ID
# if g2read.ID in unmappedDict:
# del unmappedDict[g2read.ID]
AddToDict("genome", line_raw, AllRegIndelRead1[g2read.ID][0])
AllRegIndelRead1[g2read.ID][1]="genome"
f2_genome.close()
IDfile.flush()
f1_genome = open(sorted(genomefiles)[0], mode="rB")
# compare FJ read 2 to genome read 1
print "comparing Indels to genome_1"
for line_raw in f1_genome:
if line_raw[0] =="@":
continue
g1read = ReadInfoGenome(line_raw)
if g1read.ID in AllRegIndelRead2 and AllRegIndelRead2[g1read.ID][1]==0:
#print "found genome R1"+g1read.ID
# if g1read.ID in unmappedDict:
# del unmappedDict[g1read.ID]
AddToDict("genome", line_raw, AllRegIndelRead2[g1read.ID][0])
AllRegIndelRead2[g1read.ID][1]="genome"
f1_genome.close()
IDfile.flush()
#
f2_reg = open(sorted(regfiles)[1], mode="rB")
# compare FJ read 1 to reg read 2
print "comparing Indels to reg_2"
for line_raw in f2_reg:
if line_raw[0] =="@":
continue
reg2read = ReadInfoJunc(line_raw)
if reg2read.offset <= (150-window) and (reg2read.offset+reg2read.NumOfBases)>=( 150+window):
if reg2read.ID in AllRegIndelRead1 and AllRegIndelRead1[reg2read.ID][1]==0:
#print "found reg R2: " + reg2read.ID
# if reg2read.ID in unmappedDict:
# del unmappedDict[reg2read.ID]
AddToDict("reg", line_raw, AllRegIndelRead1[reg2read.ID][0])
AllRegIndelRead1[reg2read.ID][1]="reg"
f2_reg.close()
IDfile.flush()
f1_reg = open(sorted(regfiles)[0], mode="rB")
# compare FJ read 2 to reg read 1
print "comparing Indels to reg_1"
for line_raw in f1_reg:
if line_raw[0] =="@":
continue
reg1read = ReadInfoJunc(line_raw)
if reg1read.offset <= (150-window) and (reg1read.offset+reg1read.NumOfBases)>=( 150+window):
if reg1read.ID in AllRegIndelRead2 and AllRegIndelRead2[reg1read.ID][1]==0:
#print "found reg R1: " + reg1read.ID
# if reg1read.ID in unmappedDict:
# del unmappedDict[reg1read.ID]
AddToDict("reg", line_raw, AllRegIndelRead2[reg1read.ID][0])
AllRegIndelRead2[reg1read.ID][1]="reg"
f1_reg.close()
IDfile.flush()
f2_junc= open(sorted(junctionfiles)[1], mode="rB")
# compare FJ read 1 to junc read 2
print "comparing Indels to junc_2"
for line_raw in f2_junc:
if line_raw[0] =="@":
continue
junc2read = ReadInfoJunc(line_raw)
if junc2read.offset <= (150-window) and (junc2read.offset+junc2read.NumOfBases)>=( 150+window):
if junc2read.ID in AllRegIndelRead1 and AllRegIndelRead1[junc2read.ID][1]==0:
# print "found junc R2: " + junc2read.ID
# if junc2read.ID in unmappedDict:
# del unmappedDict[junc2read.ID]
AddToDict("junc", line_raw, AllRegIndelRead1[junc2read.ID][0])
AllRegIndelRead1[junc2read.ID][1]="junc"
f2_junc.close()
IDfile.flush()
f1_junc= open(sorted(junctionfiles)[0], mode="rB")
# compare FJ read 2 to junc read 1
print "comparing Indels to junc_1"
for line_raw in f1_junc:
if line_raw[0] =="@":
continue
junc1read = ReadInfoJunc(line_raw)
if junc1read.offset <= (150-window) and (junc1read.offset+junc1read.NumOfBases)>= (150+window):
if junc1read.ID in AllRegIndelRead2 and AllRegIndelRead2[junc1read.ID][1]==0:
# print "found junc R1: " + junc1read.ID
# if junc1read.ID in unmappedDict:
# del unmappedDict[junc1read.ID]
AddToDict("junc", line_raw, AllRegIndelRead2[junc1read.ID][0])
AllRegIndelRead2[junc1read.ID][1]="junc"
f1_junc.close()
IDfile.flush()
IDfile.close()
#
#############################################################################
## This section of code takes the written ID file above (temp_IDs_STEM.txt) and
## removes duplicate entries of genome and reg. The same readID may be found
## in both libraries and would both be in the ID file.
## The new ID file removes duplicates and only keeps the readID with the
## best alignment score.
tempIDfile = open(args.circReads+"ids/"+stem+"_temp_output_RegIndel.txt", mode= "rU")
newIDfile = open(args.circReads+"ids/"+stem+"_output_RegIndel.txt", mode= "w")
##grep col 2 for "genom", "Regular" or "RegAnomaly". if not found ,write to
## new file immediately.
## if found, feed into dictionary (key=readID, value= entire line from temp file)
## if duplicate entry, then compare R2 AS. if AS larger, then replace
## value with new value from new R2
## at completion of file, write entire dictionary into new ID file.
GenomeAndRegReadIDs={}
for line in tempIDfile:
line=line.strip()
if "unaligned" in line:
continue
if "Unmapped" in line:
continue
readID = line.split("\t")[0]
classID = line.split("\t")[1]
AS_new=line.split("\t")[11]
if "genom" in classID:
## if readID has been seen previously, then replace value in dictionary
## only if AS is greater.
if readID in GenomeAndRegReadIDs:
AS_old=GenomeAndRegReadIDs[readID].split("\t")[11]
if int(AS_new)>int(AS_old):
GenomeAndRegReadIDs[readID]=line
else:
GenomeAndRegReadIDs[readID]=line
elif "Regular" in classID:
## do the same if reg
if readID in GenomeAndRegReadIDs:
AS_old=GenomeAndRegReadIDs[readID].split("\t")[11]
if int(AS_new)>int(AS_old):
GenomeAndRegReadIDs[readID]=line
else:
GenomeAndRegReadIDs[readID]=line
elif "RegAnomaly" in classID:
## do the same if reg anomaly
if readID in GenomeAndRegReadIDs:
AS_old=GenomeAndRegReadIDs[readID].split("\t")[11]
if int(AS_new)>int(AS_old):
GenomeAndRegReadIDs[readID]=line
else:
GenomeAndRegReadIDs[readID]=line
else:
## if not genome/genome anomaly/ reg/ reg anomaly then
## write line directly in new file.
newIDfile.write(line)
for entry in GenomeAndRegReadIDs:
newIDfile.write(GenomeAndRegReadIDs[entry]+"\n")
tempIDfile.close()
newIDfile.close()