-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamShrink.cpp
1125 lines (1099 loc) · 43.7 KB
/
bamShrink.cpp
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <seqan/file.h>
#include <seqan/bam_io.h>
#include <set>
#include <map>
#include <string>
#include <seqan/align.h>
#include <seqan/sequence.h>
#include <seqan/seq_io.h>
#include <seqan/store.h>
using namespace std;
using namespace seqan;
struct MateEditInfo {
int beginPosShift = 0;
int fragLenChange = 0;
bool mateRemoved = false;
bool matePrinted = false;
} ;
struct DeletionStats {
int nSoftClippedBp = 0;
int nQualityClippedBp = 0;
int nAdapterClippedBp = 0;
int nMatchRemovedReads = 0;
int nAdapterReads = 0;
unsigned nTotalReads = 0;
unsigned nCoverageFiltered = 0;
} ;
DeletionStats delStats;
void removeBeginReads(map<unsigned, String<BamAlignmentRecord> >& beginPosToReads, map<CharString, Pair<MateEditInfo> >& mateEditMap, unsigned readyPos, unsigned start, unsigned end)
{
bool sameOrientation;
map<unsigned, String<BamAlignmentRecord> >::const_iterator it = beginPosToReads.begin();
while (it->first<=readyPos && it->first < start && it!=beginPosToReads.end())
{
String<BamAlignmentRecord> & readsToCheck = beginPosToReads[it->first];
String<BamAlignmentRecord> readsToKeep;
for (unsigned i=0; i<length(readsToCheck); ++i)
{
BamAlignmentRecord & record = readsToCheck[i];
sameOrientation = hasFlagRC(record) == hasFlagNextRC(record);
if (hasFlagNextUnmapped(record) || hasFlagUnmapped(record))
continue;
MateEditInfo editInfo;
if (sameOrientation)
continue;
else
{
if (!hasFlagRC(record))
editInfo = mateEditMap[record.qName].i2;
else
editInfo = mateEditMap[record.qName].i1;
}
int fragLen, pNextAlnLen;
if (record.tLen>0)
{
fragLen = editInfo.fragLenChange - record.beginPos;
pNextAlnLen = fragLen - (record.pNext-record.beginPos);
}
else
pNextAlnLen = getAlignmentLengthInRef(record);
if ((record.pNext + pNextAlnLen < start && record.beginPos + getAlignmentLengthInRef(record) < start) || editInfo.mateRemoved || (record.pNext > end && record.beginPos > end))
{
mateEditMap[record.qName].i2.mateRemoved = true;
mateEditMap[record.qName].i1.mateRemoved = true;
continue;
}
else
appendValue(readsToKeep, record);
}
beginPosToReads[it->first] = readsToKeep;
++it;
}
}
void removeUnPairReads(map<unsigned, String<BamAlignmentRecord> >& beginPosToReads, map<CharString, Pair<MateEditInfo> >& mateEditMap)
{
bool sameOrientation;
map<unsigned, String<BamAlignmentRecord> >::const_iterator itEnd = beginPosToReads.end();
for (map<unsigned, String<BamAlignmentRecord> >::const_iterator it = beginPosToReads.begin(); it != itEnd; ++it)
{
String<BamAlignmentRecord> & readsToCheck = beginPosToReads[it->first];
String<BamAlignmentRecord> readsToKeep;
for (unsigned i=0; i<length(readsToCheck); ++i)
{
BamAlignmentRecord & record = readsToCheck[i];
if (hasFlagNextUnmapped(record) || hasFlagUnmapped(record))
continue;
sameOrientation = hasFlagRC(record) == hasFlagNextRC(record);
MateEditInfo editInfo;
if (sameOrientation)
continue;
else
{
if (!hasFlagRC(record))
editInfo = mateEditMap[record.qName].i2;
else
editInfo = mateEditMap[record.qName].i1;
}
if (editInfo.matePrinted)
appendValue(readsToKeep, record);
}
beginPosToReads[it->first] = readsToKeep;
}
return;
}
void removeTags(BamAlignmentRecord& record, bool keepMapQual)
{
BamTagsDict tagsDict(record.tags);
unsigned numOfTags = length(tagsDict);
String<CharString> keysToErase;
for (unsigned i=0; i<numOfTags; ++i)
{
CharString key = getTagKey(tagsDict, i);
if (!(key == "RG" || (key == "MQ" && keepMapQual)))
appendValue(keysToErase, key);
}
for (unsigned i=0; i<length(keysToErase); ++i)
eraseTag(tagsDict, keysToErase[i]);
return;
}
void makeUnpaired(BamAlignmentRecord& record, bool keepMapQual)
{
record.tLen = 0;
record.pNext = record.INVALID_POS;
record.rNextId = record.INVALID_REFID;
//unset: FlagNextUnmapped, FlagAllProper,FlagMultiple, FlagNextRC:
record.flag &= ~BAM_FLAG_NEXT_UNMAPPED;
record.flag &= ~BAM_FLAG_ALL_PROPER;
record.flag &= ~BAM_FLAG_MULTIPLE;
record.flag &= ~BAM_FLAG_NEXT_RC;
}
void printReadyReads(map<CharString, Pair<MateEditInfo> >& mateEditMap, map<unsigned, String<BamAlignmentRecord> >& beginPosToReads, unsigned readyPos, BamFileOut& bamFileOut, map<CharString, unsigned>& readNameToNum, unsigned& currIdx, bool keepMapQual, Pair<unsigned> start_end)
{
bool sameOrientation;
map<unsigned, String<BamAlignmentRecord> >::const_iterator it = beginPosToReads.begin();
while (it->first<=readyPos && it!=beginPosToReads.end())
{
String<BamAlignmentRecord> & readsToWrite = beginPosToReads[it->first];
for (unsigned i=0; i<length(readsToWrite); ++i)
{
BamAlignmentRecord & record = readsToWrite[i];
MateEditInfo editInfo;
sameOrientation = hasFlagRC(record) == hasFlagNextRC(record);
if (hasFlagMultiple(record) && !sameOrientation)
{
if (!hasFlagRC(record))
{
editInfo = mateEditMap[record.qName].i2;
if (editInfo.matePrinted)
mateEditMap.erase(record.qName);
else
mateEditMap[record.qName].i1.matePrinted = true;
}
else
{
editInfo = mateEditMap[record.qName].i1;
if (editInfo.matePrinted)
mateEditMap.erase(record.qName);
else
mateEditMap[record.qName].i2.matePrinted = true;
}
if (!editInfo.matePrinted && record.beginPos > record.pNext)
continue;
if (editInfo.mateRemoved)
{
makeUnpaired(record, keepMapQual);
mateEditMap.erase(record.qName);
if (hasFlagUnmapped(record))
continue;
}
else
{
record.pNext += editInfo.beginPosShift;
if (!hasFlagNextUnmapped(record) && !hasFlagUnmapped(record))
{
if (record.tLen>0)
record.tLen = editInfo.fragLenChange - record.beginPos;
else
record.tLen = -1 * (record.beginPos + getAlignmentLengthInRef(record) - editInfo.fragLenChange);
}
else
record.tLen = 0;
}
}
else
{
mateEditMap.erase(record.qName);
makeUnpaired(record, keepMapQual);
}
if (readNameToNum.count(record.qName) == 0)
{
if (hasFlagMultiple(record))
readNameToNum[record.qName] = currIdx;
stringstream ss;
ss << currIdx;
CharString str = ss.str();
record.qName = str;
++currIdx;
}
else
{
stringstream ss;
ss << readNameToNum[record.qName];
CharString str = ss.str();
record.qName = str;
readNameToNum.erase(record.qName);
}
removeTags(record, keepMapQual);
writeRecord(bamFileOut, record);
}
beginPosToReads.erase(it->first);
it = beginPosToReads.begin();
}
return;
}
unsigned countMatchingBases(String<CigarElement<> >& cigarString)
{
unsigned numOfMatches = 0;
for (unsigned i=0; i<length(cigarString); ++i)
{
CharString cigarOperation = cigarString[i].operation;
string cigarOperationStr = toCString(cigarOperation);
if (cigarOperationStr.compare("M")==0)
numOfMatches += cigarString[i].count;
}
return numOfMatches;
}
bool cigarAndSeqMatch(BamAlignmentRecord& record)
{
String<CigarElement<> >& cigarString = record.cigar;
unsigned counter = 0;
for (unsigned i=0; i<length(cigarString); ++i)
{
CharString cigarOperation = cigarString[i].operation;
string cigarOperationStr = toCString(cigarOperation);
if (cigarOperationStr.compare("D")!=0)
counter += cigarString[i].count;
}
if (length(record.seq)!=counter)
return false;
return true;
}
void resetCigarStringEnd(BamAlignmentRecord& record, unsigned nRemoved, String<CigarElement<> >& removed)
{
String<CigarElement<> >& cigarString = record.cigar;
CharString cigarOperation = cigarString[length(cigarString)-1].operation;
string cigarOperationStr = toCString(cigarOperation);
if (cigarOperationStr.compare("D")==0)
{
appendValue(removed, cigarString[length(cigarString)-1]);
erase(cigarString, length(cigarString)-1);
}
if (cigarString[length(cigarString)-1].count > nRemoved)
{
CigarElement<> t = cigarString[length(cigarString)-1];
t.count = nRemoved;
appendValue(removed, t);
cigarString[length(cigarString)-1].count -= nRemoved;
return;
}
else
{
if (cigarString[length(cigarString)-1].count == nRemoved)
{
appendValue(removed, cigarString[length(cigarString)-1]);
erase(cigarString, length(cigarString)-1);
cigarOperation = cigarString[length(cigarString)-1].operation;
cigarOperationStr = toCString(cigarOperation);
if (cigarOperationStr.compare("D")==0)
{
appendValue(removed, cigarString[length(cigarString)-1]);
erase(cigarString, length(cigarString)-1);
}
return;
}
else
{
unsigned nLeft = nRemoved - cigarString[length(cigarString)-1].count;
appendValue(removed, cigarString[length(cigarString)-1]);
erase(cigarString, length(cigarString)-1);
resetCigarStringEnd(record, nLeft, removed);
}
}
}
bool qualityClipEnd(BamAlignmentRecord& record, int windowSize, map<CharString, Pair<MateEditInfo> >& mateEditMap)
{
bool sameOrientation = hasFlagRC(record) == hasFlagNextRC(record);
int qualSum = 0;
for (unsigned i = length(record.qual)-1; i>=length(record.qual)-windowSize; --i)
qualSum += (record.qual[i]-33);
int averageQual = round((double)qualSum/(double)windowSize);
if (averageQual>=25)
return true;
int index = 0;
while (averageQual<25 && index < length(record.qual))
{
++index;
qualSum -= (record.qual[length(record.qual)-index]-33);
qualSum += (record.qual[length(record.qual)-windowSize-index]-33);
averageQual = round((double)qualSum/(double)windowSize);
}
delStats.nQualityClippedBp += index;
erase(record.seq, length(record.seq)-index, length(record.seq));
erase(record.qual,length(record.qual)-index, length(record.qual));
if (hasFlagRC(record) && !sameOrientation)
{
if (record.tLen > 0)
record.tLen -= index;
else
record.tLen += index;
}
String<CigarElement<> > removedCigar;
resetCigarStringEnd(record, index, removedCigar);
if (length(record.seq)>=50)
{
if (!sameOrientation)
{
if (hasFlagRC(record))
mateEditMap[record.qName].i2.fragLenChange = record.beginPos + getAlignmentLengthInRef(record);
}
return true;
}
else
{
if (!sameOrientation)
{
if (hasFlagRC(record))
mateEditMap[record.qName].i2.mateRemoved = true;
else
mateEditMap[record.qName].i1.mateRemoved = true;
}
return false;
}
}
void resetCigarStringBegin(BamAlignmentRecord& record, unsigned nRemoved, String<CigarElement<> >& removed)
{
String<CigarElement<> >& cigarString = record.cigar;
CharString cigarOperation = cigarString[0].operation;
string cigarOperationStr = toCString(cigarOperation);
if (cigarOperationStr.compare("D")==0)
{
appendValue(removed, cigarString[0]);
erase(cigarString, 0);
}
if (cigarString[0].count > nRemoved)
{
CigarElement<> t = cigarString[0];
t.count = nRemoved;
appendValue(removed, t);
cigarString[0].count -= nRemoved;
return;
}
else
{
if (cigarString[0].count == nRemoved)
{
appendValue(removed, cigarString[0]);
erase(cigarString, 0);
cigarOperation = cigarString[0].operation;
cigarOperationStr = toCString(cigarOperation);
if (cigarOperationStr.compare("D")==0)
{
appendValue(removed, cigarString[0]);
erase(cigarString, 0);
}
return;
}
else
{
unsigned nLeft = nRemoved - cigarString[0].count;
appendValue(removed, cigarString[0]);
erase(cigarString, 0);
resetCigarStringBegin(record, nLeft, removed);
}
}
}
bool qualityClipBegin(BamAlignmentRecord& record, int windowSize, map<CharString, Pair<MateEditInfo> >& mateEditMap)
{
bool sameOrientation = hasFlagRC(record) == hasFlagNextRC(record);
int qualSum = 0;
for (unsigned i = 0; i<windowSize; ++i)
qualSum += (record.qual[i]-33);
int averageQual = round((double)qualSum/(double)windowSize);
if (averageQual>=25)
return true;
unsigned index = 0;
while (averageQual<25 && index+windowSize < length(record.qual))
{
qualSum -= (record.qual[index]-33);
qualSum += (record.qual[index+windowSize]-33);
averageQual = round((double)qualSum/(double)windowSize);
++index;
}
delStats.nQualityClippedBp += index;
erase(record.seq, 0, index);
erase(record.qual, 0, index);
CharString cigarOperation = record.cigar[0].operation;
string cigarOperationStr = toCString(cigarOperation);
if (cigarOperationStr.compare("I")==0)
{
int shift = index - record.cigar[0].count;
if (shift > 0)
record.beginPos += shift;
}
else
record.beginPos += index;
if (!hasFlagRC(record) && !sameOrientation)
{
if (record.tLen > 0)
record.tLen -= index;
else
record.tLen += index;
}
String<CigarElement<> > removedCigar;
resetCigarStringBegin(record, index, removedCigar);
if (length(record.seq)>=50)
{
if (!sameOrientation)
{
if (hasFlagRC(record))
{
mateEditMap[record.qName].i2.beginPosShift += index;
}
else
{
mateEditMap[record.qName].i1.beginPosShift += index;
mateEditMap[record.qName].i1.fragLenChange = record.beginPos;
}
}
return true;
}
else
{
if (!sameOrientation)
{
if (hasFlagRC(record))
mateEditMap[record.qName].i2.mateRemoved = true;
else
mateEditMap[record.qName].i1.mateRemoved = true;
}
return false;
}
}
bool removeSoftClipped(BamAlignmentRecord& record, map<CharString, Pair<MateEditInfo> >& mateEditMap, unsigned minMatchingBases)
{
bool sameOrientation = hasFlagRC(record) == hasFlagNextRC(record);
String<CigarElement<> > cigarString = record.cigar;
CharString readName = record.qName;
CharString cigarOperation = cigarString[0].operation;
string cigarOperationStr = toCString(cigarOperation);
if (cigarOperationStr.compare("S")==0)
{
delStats.nSoftClippedBp += cigarString[0].count;
erase(record.seq, 0, cigarString[0].count);
erase(record.qual, 0, cigarString[0].count);
erase(record.cigar, 0);
}
cigarOperation = cigarString[length(cigarString)-1].operation;
cigarOperationStr = toCString(cigarOperation);
if (cigarOperationStr.compare("S")==0)
{
delStats.nSoftClippedBp += cigarString[length(cigarString)-1].count;
for (unsigned i = 0; i<cigarString[length(cigarString)-1].count; ++i)
{
eraseBack(record.seq);
eraseBack(record.qual);
}
eraseBack(record.cigar);
}
if (length(record.seq)>=minMatchingBases)
return true;
else
{
if (!sameOrientation)
{
if (hasFlagRC(record))
mateEditMap[readName].i2.mateRemoved = true;
else
mateEditMap[readName].i1.mateRemoved = true;
}
return false;
}
}
bool removeNsAtEnds(BamAlignmentRecord& record, map<CharString, Pair<MateEditInfo> >& mateEditMap, unsigned minMatchingBases)
{
CharString firstBaseC = record.seq[0];
string firstBase = toCString(firstBaseC);
CharString lastBaseC = record.seq[length(record.seq)-1];
string lastBase = toCString(lastBaseC);
int nOfNs = 0;
if (firstBase.compare("N")==0)
{
++nOfNs;
int idx =1;
CharString base2checkC = record.seq[idx];
string base2check = toCString(base2checkC);
while (base2check.compare("N")==0 && idx < length(record.seq)-1)
{
++nOfNs;
++idx;
base2checkC = record.seq[idx];
base2check = toCString(base2checkC);
}
//Remove ns from beginning of sequence and qual fields:
erase(record.seq, 0, nOfNs);
erase(record.qual, 0, nOfNs);
if (!hasFlagUnmapped(record)) //Only have to fix CIGAR, beginPos and fragLen if the read is mapped.
{
String<CigarElement<> > removedCigar;
resetCigarStringBegin(record, nOfNs, removedCigar);
int shift = 0;
for (unsigned i=0; i<length(removedCigar);++i)
{
CharString cigarOperation = removedCigar[i].operation;
string cigarOperationStr = toCString(cigarOperation);
if (cigarOperationStr.compare("M")==0 || cigarOperationStr.compare("D")==0)
shift += removedCigar[i].count;
}
record.beginPos += shift;
if (!hasFlagRC(record))
{
mateEditMap[record.qName].i1.beginPosShift += shift;
mateEditMap[record.qName].i1.fragLenChange = record.beginPos;
}
else
mateEditMap[record.qName].i2.beginPosShift += shift;
//cout << "Removed: " << nOfNs << " Ns from beginning of " << record.qName << endl;
}
}
if (length(record.seq) < minMatchingBases)
return false;
nOfNs = 0;
if (lastBase.compare("N")==0)
{
++nOfNs;
int idx =length(record.seq)-2;
CharString base2checkC = record.seq[idx];
string base2check = toCString(base2checkC);
while (base2check.compare("N")==0 && idx > 0)
{
++nOfNs;
--idx;
base2checkC = record.seq[idx];
base2check = toCString(base2checkC);
}
//Remove ns from end of sequence and qual fields:
erase(record.seq, length(record.seq)-nOfNs, length(record.seq));
erase(record.qual,length(record.qual)-nOfNs, length(record.qual));
if (!hasFlagUnmapped(record)) //Only have to fix CIGAR, beginPos and fragLen if the read is mapped.
{
String<CigarElement<> > removedCigar;
resetCigarStringEnd(record, nOfNs, removedCigar);
if (hasFlagRC(record))
mateEditMap[record.qName].i2.fragLenChange = record.beginPos + getAlignmentLengthInRef(record);
//cout << "Removed: " << nOfNs << " Ns from end of " << record.qName << endl;
}
}
if (length(record.seq) < minMatchingBases)
return false;
return true;
}
bool qualityFilterLevel2(BamAlignmentRecord& record, map<CharString, Pair<MateEditInfo> >& mateEditMap, unsigned minMatchingBases)
{
/*if (!removeSoftClipped(record, mateEditMap))
return false;
if (!qualityClipBegin(record, 5, mateEditMap))
return false;
if (!qualityClipEnd(record, 5, mateEditMap))
return false;*/
if (!removeNsAtEnds(record, mateEditMap, minMatchingBases))
{
++delStats.nMatchRemovedReads;
mateEditMap[record.qName].i2.mateRemoved = true;
mateEditMap[record.qName].i1.mateRemoved = true;
return false;
}
if (!hasFlagUnmapped(record))
{
if (!cigarAndSeqMatch(record))
cout << "THE CIGAR STRING AND READ-LENGTH DON'T MATCH: " << record.qName << endl;
unsigned matchingBases = countMatchingBases(record.cigar);
if (matchingBases < minMatchingBases)
{
++delStats.nMatchRemovedReads;
mateEditMap[record.qName].i2.mateRemoved = true;
mateEditMap[record.qName].i1.mateRemoved = true;
return false;
}
}
return true;
}
Pair<int> findNum2Clip(BamAlignmentRecord& recordReverse, int forwardStartPos)
{
int num2clip = 0, num2shift = 0;
unsigned cigarIndex = 0, reverseStartPos = recordReverse.beginPos, n;
CharString cigarOperation = recordReverse.cigar[cigarIndex].operation;
string cigarOperationStr = toCString(cigarOperation);
if (cigarOperationStr.compare("S")==0)
{
num2clip = recordReverse.cigar[cigarIndex].count;
++cigarIndex;
}
for (unsigned i=cigarIndex; i<length(recordReverse.cigar); ++i)
{
cigarOperation = recordReverse.cigar[cigarIndex].operation;
cigarOperationStr = toCString(cigarOperation);
n=1;
while (reverseStartPos < forwardStartPos && n <= recordReverse.cigar[cigarIndex].count)
{
if (cigarOperationStr.compare("D")!=0)
++num2clip;
if (cigarOperationStr.compare("I")!=0)
++reverseStartPos;
++n;
}
if (reverseStartPos == forwardStartPos)
break;
if (reverseStartPos>forwardStartPos)
cout << recordReverse.qName << " Reverse startpos has become bigger than forward, reverse beginPos: " << reverseStartPos << " forward beginPos is: " << forwardStartPos << endl;
++cigarIndex;
}
if (cigarOperationStr.compare("D")==0)
{
num2shift = recordReverse.cigar[cigarIndex].count - n + 1;
if (num2shift > 0)
cout << "Will shift reverse read because it starts with a deletion: " << recordReverse.qName << endl;
}
return Pair<int>(num2clip,num2shift);
}
bool removeAdapters(BamAlignmentRecord& recordForward, BamAlignmentRecord& recordReverse, map<CharString, Pair<MateEditInfo> >& mateEditMap, unsigned minMatchingBases)
{
//Check for soft clipped bases at beginning of forward record.
CharString cigarOperation = recordForward.cigar[0].operation;
string cigarOperationStr = toCString(cigarOperation);
if (cigarOperationStr.compare("S")==0)
{
if (recordForward.beginPos - recordForward.cigar[0].count <= recordReverse.beginPos)
{
return true;
}
}
if (!removeSoftClipped(recordForward, mateEditMap, minMatchingBases) || !removeSoftClipped(recordReverse, mateEditMap, minMatchingBases))
return false;
delStats.nAdapterReads += 2;
int startPosDiff = recordForward.beginPos - recordReverse.beginPos;
if (startPosDiff<0)
return true;
Pair<int> clipAndShift = findNum2Clip(recordReverse, recordForward.beginPos);
int index = clipAndShift.i1;
int shift = clipAndShift.i2;
//erase from reverse read bases 0 to index
erase(recordReverse.seq, 0, index);
erase(recordReverse.qual, 0, index);
String<CigarElement<> > removedCigar;
resetCigarStringBegin(recordReverse, index, removedCigar);
//erase from forward read bases from length(reverse.seq) to end
delStats.nAdapterClippedBp += index;
int forwardClip = index;
if (length(recordForward.seq)>length(recordReverse.seq) && index>0)
{
forwardClip = length(recordForward.seq) - length(recordReverse.seq);
erase(recordForward.seq, length(recordReverse.seq), length(recordForward.seq));
erase(recordForward.qual, length(recordReverse.qual), length(recordForward.qual));
delStats.nAdapterClippedBp += forwardClip;
String<CigarElement<> > removedCigar;
resetCigarStringEnd(recordForward, forwardClip, removedCigar);
}
// if (forwardClip != index)
// {
// cout << "Did not clip the same number of bases from both reads! " << recordReverse.qName << endl;
// cout << "Clipped " << index << "bp from beginning of reverse read and " << forwardClip << " from the end of the forward read." << endl;
// }
recordReverse.beginPos = recordForward.beginPos;
if (shift > 0)
recordReverse.beginPos += shift;
recordForward.pNext = recordReverse.beginPos;
mateEditMap[recordReverse.qName].i2.fragLenChange = recordReverse.beginPos + getAlignmentLengthInRef(recordReverse);
mateEditMap[recordForward.qName].i1.fragLenChange = recordForward.beginPos;
if (!cigarAndSeqMatch(recordForward))
cout << "The cigar string and sequence length don't match for the forward read!!" << endl;
if (!cigarAndSeqMatch(recordReverse))
cout << "The cigar string and sequence length don't match for the reverse read!!" << endl;
if (length(recordForward.seq)>=minMatchingBases)
return true;
else
return false;
}
bool qualityFilter(BamAlignmentRecord& record, map<CharString, Pair<MateEditInfo> >& mateEditMap, int maxFragmentLength, map<CharString, BamAlignmentRecord>& adapterMap, unsigned minMatchingBases, bool keepMapQual, map<unsigned, String<BamAlignmentRecord> >& beginPosToReads)
{
MateEditInfo editInfo;
BamAlignmentRecord reverseRecord;
bool sameOrientation = hasFlagRC(record) == hasFlagNextRC(record);
if (hasFlagNextUnmapped(record) && sameOrientation)
{
//If mate is unmapped and both have same orientation, need to flip mate orientation
if (hasFlagRC(record))
record.flag |= ~BAM_FLAG_NEXT_RC;
else
record.flag |= BAM_FLAG_NEXT_RC;
sameOrientation = false;
}
if (hasFlagUnmapped(record) && sameOrientation)
{
//If read is unmapped and both have same orientation, need to flip read orientation
if (hasFlagRC(record))
record.flag |= ~BAM_FLAG_RC;
else
record.flag |= BAM_FLAG_RC;
sameOrientation = false;
}
if (hasFlagRC(record))
{
if (mateEditMap.count(record.qName)==0)
mateEditMap[record.qName].i2 = editInfo;
mateEditMap[record.qName].i2.fragLenChange = record.beginPos + getAlignmentLengthInRef(record);
}
else
{
if (mateEditMap.count(record.qName)==0)
mateEditMap[record.qName].i1 = editInfo;
mateEditMap[record.qName].i1.fragLenChange = record.beginPos;
}
if (sameOrientation)
{
makeUnpaired(record, keepMapQual);
mateEditMap[record.qName].i2.mateRemoved = true;
mateEditMap[record.qName].i1.mateRemoved = true;
}
//if (hasFlagDuplicate(record) || hasFlagUnmapped(record))
if (hasFlagDuplicate(record))
{
if (hasFlagRC(record))
mateEditMap[record.qName].i2.mateRemoved = true;
else
mateEditMap[record.qName].i1.mateRemoved = true;
return false;
}
//if (hasFlagNextUnmapped(record) || abs(record.tLen) > maxFragmentLength || record.rID != record.rNextId)
if (abs(record.tLen) > maxFragmentLength || record.rID != record.rNextId)
{
makeUnpaired(record, keepMapQual);
mateEditMap[record.qName].i2.mateRemoved = true;
mateEditMap[record.qName].i1.mateRemoved = true;
}
if (abs(record.tLen)<length(record.seq) && record.rID == record.rNextId && adapterMap.count(record.qName)==0 && !hasFlagNextUnmapped(record) && !hasFlagUnmapped(record))
{
adapterMap[record.qName] = record;
return false;
}
if (abs(record.tLen)<length(record.seq) && record.rID == record.rNextId && adapterMap.count(record.qName)!=0)
{
if (!sameOrientation)
{
if (!hasFlagRC(record))
reverseRecord = adapterMap[record.qName];
else
{
reverseRecord = record;
record = adapterMap[record.qName];
}
adapterMap.erase(record.qName);
if (!removeAdapters(record,reverseRecord, mateEditMap, minMatchingBases))
return false;
}
else
reverseRecord = adapterMap[record.qName];
if (qualityFilterLevel2(reverseRecord, mateEditMap, minMatchingBases))
{
CharString binary_qual = "";
for (unsigned x=0; x<length(reverseRecord.qual); ++x)
{
if (reverseRecord.qual[x]-33 >= 25)
append(binary_qual,"I");
else
append(binary_qual,"!");
}
reverseRecord.qual = binary_qual;
appendValue(beginPosToReads[reverseRecord.beginPos], reverseRecord);
}
else
mateEditMap[record.qName].i2.mateRemoved = true;
}
if (!qualityFilterLevel2(record, mateEditMap, minMatchingBases))
return false;
return true;
}
void removeHardClipped(BamAlignmentRecord& record)
{
//cout << "Working on record: " << record.qName << endl;
if (hasFlagUnmapped(record))
return;
CharString cigarOperation = record.cigar[0].operation;
string cigarOperationStr = toCString(cigarOperation);
if (cigarOperationStr.compare("H")==0)
erase(record.cigar, 0);
cigarOperation = record.cigar[length(record.cigar)-1].operation;
cigarOperationStr = toCString(cigarOperation);
if (cigarOperationStr.compare("H")==0)
erase(record.cigar, length(record.cigar)-1);
}
int qualityFilterSlice(Triple<CharString, int, int >& chr_start_end, CharString baiPathIn, BamFileIn& bamFileIn, BamFileOut& bamFileOut, map<CharString, Pair<MateEditInfo> >& mateEditMap, bool keepMapQual, int maxFragLen, map<unsigned, String<BamAlignmentRecord> >& beginPosToReads, map<CharString, BamAlignmentRecord>& adapterMap, unsigned minMatchingBases, double avgCovByReadLen, map<CharString, unsigned>& readNameToNum, unsigned& currIdx)
{
double maxQueSum = avgCovByReadLen*(double)50.0*(double)3.0;
unsigned currBeginPos = 0;
deque<unsigned> myQue;
BamIndex<Bai> baiIndex;
if (!open(baiIndex, toCString(baiPathIn)))
{
std::cerr << "ERROR: Could not read BAI index file " << baiPathIn << "\n";
return 1;
}
int rID = 0;
if (!getIdByName(rID, contigNamesCache(context(bamFileIn)), chr_start_end.i1))
{
std::cerr << "ERROR: Reference sequence named " << chr_start_end.i1 << " not known.\n";
return 1;
}
bool hasAlignments = false;
if (!jumpToRegion(bamFileIn, hasAlignments, rID, std::max((int)0,(int)chr_start_end.i2-maxFragLen), chr_start_end.i3+maxFragLen, baiIndex))
{
std::cerr << "ERROR: Could not jump to " << chr_start_end.i2 << ":" << chr_start_end.i3 << "\n";
return 1;
}
if (!hasAlignments)
{
cout << "No alignments found in the interval: " << std::max((int)0,(int)chr_start_end.i2-maxFragLen) << " to " << chr_start_end.i3+maxFragLen << "\n";
return 0;
}
BamAlignmentRecord record;
CharString binary_qual;
while (!atEnd(bamFileIn) )
{
readRecord(record, bamFileIn);
//cout << "Processing read: " << record.qName << " at:" << record.beginPos << endl;
if (record.rID == -1 || record.rID > rID || record.beginPos > chr_start_end.i3+maxFragLen)
break;
if (record.beginPos < std::max((int)0,chr_start_end.i2-maxFragLen))
continue;
removeHardClipped(record);
++delStats.nTotalReads;
if (record.beginPos != currBeginPos)
{
if (record.beginPos-currBeginPos > 1)
{
for (unsigned i=1; i<record.beginPos-currBeginPos; ++i)
{
myQue.push_front(0);
while (myQue.size()>50)
myQue.pop_back();
}
}
myQue.push_front(1);
while (myQue.size()>50)
myQue.pop_back();
currBeginPos = record.beginPos;
}
else
++myQue.front();
if (std::accumulate(myQue.begin(),myQue.end(),0) > maxQueSum)
{
++delStats.nCoverageFiltered;
--myQue.front();
if (hasFlagRC(record))
mateEditMap[record.qName].i2.mateRemoved = true;
else
mateEditMap[record.qName].i1.mateRemoved = true;
continue;
}
if (qualityFilter(record, mateEditMap, maxFragLen, adapterMap, minMatchingBases, keepMapQual, beginPosToReads))
{
binary_qual = "";
for (unsigned x=0; x<length(record.qual); ++x)
{
if (record.qual[x]-33 >= 25)
append(binary_qual,"I");
else
append(binary_qual,"!");
}
record.qual = binary_qual;
appendValue(beginPosToReads[record.beginPos], record);
if (record.beginPos-maxFragLen >=0)
{
//If I am printing reads infront of the interval I need to remove ones that don't have a mate in the interval first.
if (beginPosToReads.begin()->first < chr_start_end.i2)
removeBeginReads(beginPosToReads, mateEditMap, record.beginPos-maxFragLen, chr_start_end.i2, chr_start_end.i3);
printReadyReads(mateEditMap, beginPosToReads, record.beginPos-maxFragLen, bamFileOut, readNameToNum, currIdx, keepMapQual, Pair<unsigned>(chr_start_end.i2, chr_start_end.i3));
}
}
else
--myQue.front();
}
printReadyReads(mateEditMap, beginPosToReads, chr_start_end.i3, bamFileOut, readNameToNum, currIdx, keepMapQual, Pair<unsigned>(chr_start_end.i2, chr_start_end.i3));
removeUnPairReads(beginPosToReads, mateEditMap);
printReadyReads(mateEditMap, beginPosToReads, record.beginPos, bamFileOut, readNameToNum, currIdx, keepMapQual, Pair<unsigned>(chr_start_end.i2, chr_start_end.i3));
if (beginPosToReads.size()>0)
{
for (map<unsigned, String<BamAlignmentRecord> >::const_iterator it = beginPosToReads.begin(); it != beginPosToReads.end(); ++it)
{
if (length(beginPosToReads[it->first])>0)
cout << "There are " << length(beginPosToReads[it->first]) << " reads left at: " << it->first << endl;
}
}
return 0;
}
String<Triple<CharString, int, int > > readIntervals(CharString& intervalFile, int maxFragLen)
{
//String of intervals to return
String<Triple<CharString, int, int > > intervalString;
string chrStr;
Triple<CharString, int, int > chr_start_end;
ifstream intFile(toCString(intervalFile));
if(intFile.fail())
{
cout << "Unable to locate interval file at: " << intervalFile << endl;
return intervalString;
}
//Read first interval and add to string
intFile >> chrStr;
chr_start_end.i1 = chrStr;
intFile >> chr_start_end.i2;
--chr_start_end.i2;
intFile >> chr_start_end.i3;
--chr_start_end.i3;
//cout << "Added: " << chr_start_end.i1 << ":" << chr_start_end.i2 << "-" << chr_start_end.i3 << " to interval string.\n";
append(intervalString, chr_start_end);
//If file only contains one interval, return.
if (intFile.eof())
return intervalString;
while (!intFile.eof())
{
intFile >> chrStr;
chr_start_end.i1 = chrStr;
intFile >> chr_start_end.i2;
--chr_start_end.i2;
intFile >> chr_start_end.i3;
--chr_start_end.i3;
if (intFile.eof())
break;
//If beginning of interval is closer than 2*maxFragLen bases to the previous interval we merge them. Otherwise we cannot ensure sorting of reads.
if (chr_start_end.i2-intervalString[length(intervalString)-1].i3 <= 2*maxFragLen && chr_start_end.i1 == intervalString[length(intervalString)-1].i1)
{
//cout << "Merging: " << intervalString[length(intervalString)-1].i2 << "-" << intervalString[length(intervalString)-1].i3 << " and " << chr_start_end.i2 << "-" << chr_start_end.i3 << " on " << chr_start_end.i1 << "\n";
intervalString[length(intervalString)-1].i3 = chr_start_end.i3;
}
else
{
//cout << "Added: " << chr_start_end.i1 << ":" << chr_start_end.i2 << "-" << chr_start_end.i3 << " to interval string.\n";
append(intervalString, chr_start_end);
}
}
intFile.close();
return intervalString;
}
int main(int argc, char const ** argv)
{
if (argc != 7 && argc != 9)
{
cerr << "USAGE: " << argv[0] << " IN.bam OUT.bam maxFragmentLength keepMapQuality(Y/N) minNumMatches avgCovByReadLen.sh [baiFile intervalFile]\n";
return 1;
}
cout<< "File to filter: " << argv[1] << endl;
double avgCovByReadLen = lexicalCast<double>(argv[6]);
CharString bamPathIn = argv[1], baiPathIn, intervalFile;
int maxFragLen = lexicalCast<unsigned>(argv[3]), minMatchingBases = lexicalCast<unsigned>(argv[5]);
bool keepMapQual = false, readBamSlice = false;
string keepMapQualStr = argv[4];
String<Triple<CharString, int, int > > intervalString;
if (keepMapQualStr.compare("Y")==0)
keepMapQual = true;
if (argc == 9)
{
baiPathIn = argv[7];
intervalFile = argv[8];
intervalString = readIntervals(intervalFile, maxFragLen);
// cout << "Listing intervals: " << endl;