-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnovelSnvFilter_ACGT.cpp
More file actions
executable file
·823 lines (696 loc) · 28.6 KB
/
novelSnvFilter_ACGT.cpp
File metadata and controls
executable file
·823 lines (696 loc) · 28.6 KB
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
/*****************************************************************************
(c) 2020 - Sun Ruping
ruping@umn.edu
g++ novelSnvFilter_ACGT.cpp -I/home/ruping/ruping/tools/bamtools/include/bamtools/ -I/home/ruping/ruping/tools/zlib/current/include/ -I/home/ruping/ruping/tools/boost/current/include/ -L/home/ruping/ruping/tools/bamtools/lib64/ -L/home/ruping/ruping/tools/zlib/current/lib/ -L/home/ruping/ruping/tools/boost/current/lib/ -lbamtools -lz -Wl,-rpath,/home/ruping/ruping/tools/bamtools/lib64/:/home/ruping/ruping/tools/boost/current/lib/ -lboost_regex -pthread -static -o novelSnvFilter_ACGT
g++ novelSnvFilter_ACGT.cpp
-I/home/regularhand/tools/bamtools/include/ -I/home/regularhand/tools/zlib/current/include/ -I/home/regularhand/tools/boost/current/include/
-L/home/regularhand/tools/bamtools/lib/ -L/home/regularhand/tools/zlib/current/lib/ -L/home/regularhand/tools/boost/current/lib/
-lbamtools -lz -Wl,-rpath,/home/regularhand/tools/bamtools/lib/:/home/regularhand/tools/boost/current/lib/ -lboost_regex -o novelSnvFilter_ACGT
******************************************************************************/
#include <api/BamReader.h>
#include <api/BamMultiReader.h>
using namespace BamTools;
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <deque>
#include <set>
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>
#include "boost/regex.hpp"
#include "novelSnvFilter_ACGT.h"
using namespace std;
using namespace boost;
struct var { // a bed file containing gene annotations
string chr;
string chro; //original chr name from the input list
string snpID;
string ref;
string alt;
unsigned int start;
unsigned int end;
// results storing here
unsigned int countAlt;
unsigned int countAll;
unsigned int countA;
unsigned int countAn;
unsigned int countC;
unsigned int countCn;
unsigned int countG;
unsigned int countGn;
unsigned int countT;
unsigned int countTn;
unsigned int countMappingGood;
unsigned int countMappingBad;
unsigned int countPositive;
unsigned int countNegative;
unsigned int inends;
unsigned int countJump;
unsigned int F1R2_alt;
unsigned int F1R2_all;
unsigned int F2R1_alt;
unsigned int F2R1_all;
unsigned int readlen;
vector <unsigned int> lenVarReads;
vector <unsigned int> surrounding;
vector <unsigned int> surroundingIndels;
map <unsigned int, unsigned int> conMis;
string qualities;
};
//unsigned int read_length = 0;
inline void ParseCigar(const vector<CigarOp> &cigar, vector<int> &blockStarts, vector<int> &blockEnds, unsigned int &alignmentEnd, map<unsigned int, unsigned int> &insertions, unsigned int &softClip);
inline void splitstring(const string &str, vector<string> &elements, const string &delimiter);
inline bool eatline(const string &str, deque <struct var> &var_ref, string &withChr);
inline string int2str(unsigned int &i);
inline string float2str(float &f);
inline void var_processing(struct var &variant);
inline float CalcMedian (vector<unsigned int> &scores);
int main ( int argc, char *argv[] ) {
struct parameters *param = 0;
param = interface(param, argc, argv);
//region file input (the region file should be sorted as the same way as the bam file)
ifstream var_f;
var_f.open(param->var_f, ios_base::in); // the region file is opened
//bam input and generate index if not yet
//-------------------------------------------------------------------------------------------------------+
// BAM input (file or filenames?) |
//-------------------------------------------------------------------------------------------------------+
char *fof = param->mapping_f;
FILE *IN=NULL;
char linefof[5000];
int filecount=0;
vector <string> fnames;
if (strchr(fof,' ')!=NULL) {
char *ptr;
ptr=strtok(fof," ");
while (ptr!=NULL) {
fnames.push_back(ptr);
filecount++;
ptr=strtok(NULL," ");
}
} else {
IN=fopen(fof,"rt");
if (IN!=NULL) {
long linecount=0;
while (fgets(linefof,5000-1,IN)!=NULL) {
linecount++;
if (linefof[0]!='#' && linefof[0]!='\n') {
char *ptr=strchr(linefof,'\n');
if (ptr!=NULL && ptr[0]=='\n') {
ptr[0]='\0';
}
FILE *dummy=NULL;
dummy=fopen(linefof,"rt");
if (dummy!=NULL) { // seems to be a file of filenames...
fclose(dummy);
fnames.push_back(linefof);
filecount++;
} else if (filecount==0 || linecount>=1000-1) { // seems to be a single file
fnames.push_back(fof);
filecount++;
break;
}
}
}
fclose(IN);
}
} //file or file name decided and stored in vector "fnames"
cerr << "the input mapping files are:" << endl;
vector <string>::iterator fit = fnames.begin();
for(; fit != fnames.end(); fit++) {
cerr << *fit << endl;
}
//-------------------------------------------------------------------------------------------------------+
// end of file or filenames |
//-------------------------------------------------------------------------------------------------------+
// open the BAM file(s)
BamMultiReader reader;
reader.Open(fnames);
// get header & reference information
string header = reader.GetHeaderText();
RefVector refs = reader.GetReferenceData();
if ( ! reader.LocateIndexes() ) // opens any existing index files that match our BAM files
reader.CreateIndexes(); // creates index files for BAM files that still lack one
//should decide which chromosome
string line;
string old_chr = "SRP";
string type = param->type;
string startwithChr = param->chr;
if (startwithChr == "") {
startwithChr = "none";
}
cerr << "chr prefix is: " << startwithChr << endl;
//regions for the input of region file
deque <struct var> variants;
bool isComment;
getline(var_f, line); //get the first line
isComment = eatline(line, variants, startwithChr);
while (isComment == true){
//print comment
//cout << line << endl;
getline(var_f, line); //get a new line
isComment = eatline(line, variants, startwithChr); //eat a new line
}
deque <struct var>::iterator it = variants.begin();
while ( it->chr != old_chr ) {
old_chr = it->chr; // set the current chr as old chr
int chr_id = reader.GetReferenceID(it->chr);
if ( chr_id == -1 ) { //reference not found
for (; it != variants.end() && it->chr == old_chr; ) {
var_processing(*it); // print the old region info
it = variants.erase(it); // erase the current region
}
while ( variants.empty() ) {
getline(var_f, line);
if ( var_f.eof() ){
cerr << "finished: end of region file, zone 0" << endl;
break;
}
eatline(line, variants, startwithChr);
it = variants.begin();
if (it->chr == old_chr){
var_processing(*it);
variants.clear();
continue;
}
}
continue;
}
int chr_len = refs.at(chr_id).RefLength;
if ( !reader.SetRegion(chr_id, 1, chr_id, chr_len) ) // here set region
{
cerr << "bamtools count ERROR: Jump region failed " << it->chr << endl;
reader.Close();
exit(1);
}
////pile-up pos stats
//map <string, unsigned int> PileUp;
//unsigned int old_PileUp_start = 0;
BamAlignment bam;
while (reader.GetNextAlignment(bam)) {
if ( bam.IsMapped() == false ) continue; // skip unaligned reads
if ( bam.IsDuplicate() == true && param->skipPileup == 1) continue; // skip PCR duplicates
unsigned int unique = 0;
if ( bam.HasTag("NH") ) {
bam.GetTag("NH", unique); // uniqueness
} else {
if (bam.MapQuality > 10) { // other aligner
unique = 1;
}
}
if (param->unique == 1) {
if (unique != 1) { // skipe uniquelly mapped reads
continue;
}
}
//if (bam.Length > read_length) { // get the read length
// read_length = bam.Length;
//}
string chrom = refs.at(bam.RefID).RefName;
string strand = "+";
if (bam.IsReverseStrand()) strand = "-";
string FxRx = "FxRx";
if (bam.IsProperPair()) {
if (bam.IsFirstMate()) { //first mate
if (strand == "+") {
FxRx = "F1R2";
} else {
FxRx = "F2R1";
}
} else { //second mate
if (strand == "+") {
FxRx = "F2R1";
} else {
FxRx = "F1R2";
}
}
}
unsigned int mappingQuality = bam.MapQuality;
unsigned int alignmentStart = bam.Position+1;
unsigned int alignmentEnd = bam.GetEndPosition();
unsigned int cigarEnd;
vector <int> blockLengths;
vector <int> blockStarts;
map<unsigned int, unsigned int> insertions; // for insertions
unsigned int softClip = 0; // for soft clipping
blockStarts.push_back(0);
ParseCigar(bam.CigarData, blockStarts, blockLengths, cigarEnd, insertions, softClip);
//// do pileup check for duplicates
//string alignSum = int2str(alignmentStart) + "\t" + bam.QueryBases;
//if ( alignmentStart != old_PileUp_start ) {
// PileUp.clear(); //clear PileUp set
// PileUp.insert( pair <string, unsigned int> (alignSum, 1) ); //insert the new read
//} else if ( alignmentStart == old_PileUp_start ) { // same starts
// if ( PileUp.count(alignSum) > 0 ) { // PileUp
// PileUp[alignSum]++;
// if ( bam.IsDuplicate() == true ) { // skip PCR duplicates
// continue;
// } //PCR duplicates
// } else {
// PileUp.insert( pair <string, unsigned int> (alignSum, 1) );
// }
//} //same starts
//old_PileUp_start = alignmentStart;
////pile up check
deque <struct var>::iterator iter = variants.begin();
if ( iter->start > alignmentEnd ) continue; // skip reads not overlapping with the first region
while ( iter->chr == old_chr && iter->start <= alignmentEnd && iter != variants.end() ) {
if (iter->end < alignmentStart) { // the region end is beyond the alignmentStart
var_processing(*iter); // processing
iter = variants.erase(iter); // this region should be removed
if ( variants.empty() ) {
getline(var_f, line); // get a line of region file
if ( ! var_f.eof() ) {
eatline(line, variants, startwithChr); // eat a line and put it into the duque
iter = variants.begin();
}
else { // it's reaching the end of the region file
cerr << "finished: end of region file, zone 3" << endl;
break;
}
}
continue;
}
if ( iter->end >= alignmentStart && iter->start <= alignmentEnd ) { //overlapping, should take action
if (bam.Length > iter->readlen) { // should we re-define the read length?
iter->readlen = bam.Length;
}
unsigned int mismatches = 0; // how many mismatches (including indels) does this read have?
unsigned int indels = 0; // how many indels does this read have?
bool varInRead = false; // is the var in the read?
bool posInRead = false;
vector <int>::iterator bliter = blockLengths.begin();
vector <int>::iterator bSiter = blockStarts.begin();
while (bliter != blockLengths.end() && bSiter != blockStarts.end()) {
unsigned int blockstart = *bSiter + alignmentStart;
unsigned int blockend = *bliter + blockstart;
if (iter->start >= blockstart && iter->end <= blockend) {
posInRead = true;
break;
} //overlap
bliter++;
bSiter++;
}
if (posInRead == true) { //need to get strand information for all reads !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
iter->countAll += 1;
if (strand == "+") {
iter->countPositive += 1;
} else {
iter->countNegative += 1;
}
if (FxRx == "F1R2") {
iter->F1R2_all += 1;
} else if (FxRx == "F2R1") {
iter->F2R1_all += 1;
}
}
else
iter->countJump += 1;
//processing MD string, calculate mismatch coordinates and compare with the variants
string MD;
bam.GetTag("MD", MD);
//cerr << MD << endl;
unsigned int cuPos = alignmentStart;
unsigned int cuPosRead = softClip + 1;
//cerr << iter->start << "\t" << bam.Name << "\t" << cuPosRead << endl; //deBUG!!!!!!!!
regex rgx( "([0-9]+)([ACGTacgt]|\\^[ACGTacgt]+)" );
int subs[] = {1,2};
sregex_token_iterator rit ( MD.begin(), MD.end(), rgx, subs );
sregex_token_iterator rend;
map<unsigned int, unsigned int>::iterator inserit_index = insertions.begin();
while ( inserit_index != insertions.end() ) { // check insertions
mismatches += 1; // count as mismatches
indels += 1; // count for indels
inserit_index++;
}
inserit_index = insertions.begin(); //reset it for the begin of insertions
while ( rit != rend ) {
unsigned int incre = atoi((*rit).str().c_str()); //number 1
cuPos += incre; //number 1
cuPosRead += incre;
if (blockStarts.size() > 1) { //judge which block the mutation locate
vector <int>::iterator bliter2 = blockLengths.begin();
vector <int>::iterator bSiter2 = blockStarts.begin();
unsigned int culength = 0;
while (bliter2 != blockLengths.end() && bSiter2 != blockStarts.end()) {
if (cuPosRead <= (culength + *bliter2)) {
cuPos += (*bSiter2 - culength);
break;
}
culength += *bliter2;
bliter2++;
bSiter2++;
}
} //multi blocks especially useful for RNA-seq junction reads
map<unsigned int, unsigned int>::iterator inserit = inserit_index;
while ( inserit != insertions.end() ) {
if ( inserit->first < cuPosRead ) {
cuPosRead += inserit->second;
inserit++;
inserit_index = inserit;
} else {
inserit_index = inserit;
break;
}
}
++rit; //round 1 addition
if (((*rit).str())[0] == '^') { //variant 2
incre = (*rit).length() - 1; //variant 2
cuPos += incre; //variant 2
mismatches += 1;
indels += 1;
} else if ((*rit).length() == 1) { // single base nucleotide change
//check whether it is "N" or not
string baseInReadPre = (bam.QueryBases).substr( cuPosRead-1, 1 );
if (baseInReadPre != "N") {
mismatches += 1;
map<unsigned int, unsigned int>::iterator cmi = (iter->conMis).find(cuPos);
if ( cmi == (iter->conMis).end() ) { // not found need to record mismatch in a map
(iter->conMis).insert( pair <unsigned int, unsigned int> (cuPos, 1) ); // not found need to record mismatch in a map
} else {
cmi->second += 1; // found increase it
}
}
if ( cuPos == iter->start ) { // it is right here with some variant base!!!
varInRead = true;
if ((alignmentEnd - cuPos) <= 10 || (cuPos - alignmentStart) <= 10) { // inends
iter->inends += 1;
}
if ( mappingQuality >= 30 ) { //good mapping qual
iter->countMappingGood += 1;
} else if (mappingQuality <= 29) { // bad mapping qual
iter->countMappingBad += 1;
}
//cout << cuPosRead << endl; // deBUG!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
string baseInRead = (bam.QueryBases).substr( cuPosRead-1, 1 );
if (baseInRead == iter->alt) { // it is exactly the same alt base
iter->qualities += (bam.Qualities).substr( cuPosRead-1, 1 ); //base quality
if (FxRx == "F1R2") {
iter->F1R2_alt += 1;
} else if (FxRx == "F2R1") {
iter->F2R1_alt += 1;
}
}
iter->countAlt += 1;
if (strand == "+") { //positive strand
if (baseInRead == "A") {
iter->countA += 1;
} else if (baseInRead == "C") {
iter->countC += 1;
} else if (baseInRead == "G") {
iter->countG += 1;
} else if (baseInRead == "T") {
iter->countT += 1;
}
} else { //negative strand
if (baseInRead == "A") {
iter->countAn += 1;
} else if (baseInRead == "C") {
iter->countCn += 1;
} else if (baseInRead == "G") {
iter->countGn += 1;
} else if (baseInRead == "T") {
iter->countTn += 1;
}
}
}
cuPos += 1;
cuPosRead += 1;
} else {
cerr << "wired thing happened in the MD string of " << bam.Name << endl;
exit(1);
}
++rit; //round 2 addition
} //loop for all MD characters
if (varInRead == true) {
(iter->surrounding).push_back(mismatches);
(iter->surroundingIndels).push_back(indels);
(iter->lenVarReads).push_back(bam.Length);
}
} // overlapping take action!
if ( (iter+1) != variants.end() )
iter++; // if this region is not the last element in the deque
else { // the last element
getline(var_f, line); // get a line of region file
if ( ! var_f.eof() ) {
eatline(line, variants, startwithChr); // eat a line and put it into the duque
iter = variants.end();
iter--;
}
else { //it's reaching the end of the region file
cerr << "finished: end of region file, zone 4" << endl;
break;
}
}
} //while
} // read a bam
//somehow to loop back
it = variants.begin(); //reset to begin
for (; it != variants.end() && it->chr == old_chr; ) {
var_processing(*it); // print the old region info
it = variants.erase(it); // erase the current region
}
while ( variants.empty() ) {
getline(var_f, line);
if ( var_f.eof() ){
cerr << "finished: end of region file, zone 5" << endl;
exit(0);
}
eatline(line, variants, startwithChr);
it = variants.begin();
if (it->chr == old_chr){
var_processing(*it);
variants.clear();
continue;
}
}
} // region chr != old chr
variants.clear();
reader.Close();
var_f.close();
return 0;
} //main
inline string int2str(unsigned int &i){
string s;
stringstream ss(s);
ss << i;
return ss.str();
}
inline string float2str(float &f){
string s;
stringstream ss(s);
ss << f;
return ss.str();
}
inline void splitstring(const string &str, vector<string> &elements, const string &delimiter) {
string::size_type lastPos = str.find_first_not_of(delimiter, 0);
string::size_type pos = str.find_first_of(delimiter, lastPos);
while (string::npos != pos || string::npos != lastPos) {
elements.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(delimiter, pos);
pos = str.find_first_of(delimiter, lastPos);
}
}
inline bool eatline(const string &str, deque <struct var> &var_ref, string &withChr) {
bool isComment = false;
if (str[0] == '#' || str[0] == '@') {
isComment = true;
return isComment;
}
vector <string> line_content;
//split line and then put it into a deque
splitstring(str, line_content, "\t");
vector <string>::iterator iter = line_content.begin();
unsigned int i;
struct var tmp;
tmp.countAlt = 0;
tmp.countAll = 0;
tmp.countA = 0;
tmp.countAn = 0;
tmp.countC = 0;
tmp.countCn = 0;
tmp.countG = 0;
tmp.countGn = 0;
tmp.countT = 0;
tmp.countTn = 0;
tmp.countMappingGood = 0;
tmp.countMappingBad = 0;
tmp.countPositive = 0;
tmp.countNegative = 0;
tmp.inends = 0;
tmp.countJump = 0;
tmp.F1R2_alt = 0;
tmp.F1R2_all = 0;
tmp.F2R1_alt = 0;
tmp.F2R1_all = 0;
tmp.readlen = 0;
for(i = 1; iter != line_content.end(); iter++, i++) {
switch (i) {
case 1: // chr
tmp.chr = *iter;
tmp.chro = *iter;
if (withChr != "none") {
if ((tmp.chr).substr(0,1) != "c" && (tmp.chr).substr(0,1) != "C" && (tmp.chr).length() < 3) { //mostlikely not starting with chr
tmp.chr = withChr + tmp.chr;
}
//if(tmp.chr == "chrMT") {
// tmp.chr = "chrM";
//}
} else { //'chr' is not required
if ( ( (tmp.chr).substr(0,1) == "c" || (tmp.chr).substr(0,1) == "C" ) && (tmp.chr).length() > 3) { //mostlikely starting with chr
tmp.chr = (tmp.chr).substr(3);
}
}
continue;
case 2: // pos
tmp.start = atoi((*iter).c_str());
tmp.end = atoi((*iter).c_str());
continue;
case 3: // id
tmp.snpID = *iter;
continue;
case 4: // ref
tmp.ref = *iter;
continue;
case 5: // alt
tmp.alt = *iter;
continue;
default:
break;
}
}
var_ref.push_back(tmp);
return isComment;
}
inline void ParseCigar(const vector<CigarOp> &cigar, vector<int> &blockStarts, vector<int> &blockLengths, unsigned int &alignmentEnd, map<unsigned int, unsigned int> &insertions, unsigned int &softClip) {
int currPosition = 0;
int blockLength = 0;
unsigned int insertSize;
unsigned int insertPos;
// Rip through the CIGAR ops and figure out if there is more
// than one block for this alignment
vector<CigarOp>::const_iterator cigItr = cigar.begin();
vector<CigarOp>::const_iterator cigEnd = cigar.end();
for (; cigItr != cigEnd; ++cigItr) {
switch (cigItr->Type) {
case ('M') : // matching
blockLength += cigItr->Length;
currPosition += cigItr->Length;
break;
case ('I') : // insertion
insertSize = cigItr->Length;
insertPos = currPosition + 1;
insertions.insert( pair <unsigned int, unsigned int> (insertPos, insertSize) );
break;
case ('S') : // soft-clipping
if (currPosition == 0){ //only take action for the beginning clipping
softClip = cigItr->Length;
}
break;
case ('D') : // deletion
blockLength += cigItr->Length;
currPosition += cigItr->Length;
break;
case ('P') : break; // padding
case ('N') : // skipped region
blockStarts.push_back(currPosition + cigItr->Length);
blockLengths.push_back(blockLength);
currPosition += cigItr->Length;
blockLength = 0; // a new block
break;
case ('H') : break; // for 'H' - do nothing, move to next op
default :
printf("ERROR: Invalid Cigar op type\n"); // shouldn't get here
exit(1);
}
}
// add the kast block and set the
// alignment end (i.e., relative to the start)
blockLengths.push_back(blockLength);
alignmentEnd = currPosition;
}
inline void var_processing(struct var &variant) {
unsigned int ssum = 0;
vector <unsigned int>::iterator sit = (variant.surrounding).begin();
for(; sit != (variant.surrounding).end(); sit++) {
ssum += *sit;
}
float meanMis;
float medianMis;
unsigned int surrSize = variant.surrounding.size();
if (surrSize == 0) {
meanMis = 0.0;
medianMis = 0.0;
} else {
meanMis = ((float)ssum)/((float)surrSize);
medianMis = CalcMedian(variant.surrounding);
}
unsigned int indelsum = 0;
vector <unsigned int>::iterator indit = (variant.surroundingIndels).begin();
for(; indit != (variant.surroundingIndels).end(); indit++) {
indelsum += *indit;
}
float meanIndel;
float medianIndel;
unsigned int surrIndelSize = variant.surroundingIndels.size();
if (surrIndelSize == 0) {
meanIndel = 0.0;
medianIndel = 0.0;
} else {
meanIndel = ((float)indelsum)/((float)surrIndelSize);
medianIndel = CalcMedian(variant.surroundingIndels);
}
//unsigned int lenSum;
//vector <unsigned int>::iterator lenit = (variant.lenVarReads).begin();
//for(; lenit != (variant.lenVarReads).end(); lenit++) {
// lenSum += *lenit;
//}
//float meanVRLength; // get Length of reads with variants
float medianVRLength; // get Length of reads with variants
unsigned int lenVRSize = variant.lenVarReads.size();
if (surrIndelSize == 0) {
//meanVRLength = 0.0;
medianVRLength = 0.0;
} else {
//meanVRLength = ((float)lenSum)/((float)lenVRSize);
medianVRLength = CalcMedian(variant.lenVarReads);
}
float fracBadMappingQual = 0;
if ((variant.countMappingGood + variant.countMappingBad) > 0) {
fracBadMappingQual = ((float)(variant.countMappingBad))/((float)(variant.countMappingGood + variant.countMappingBad));
}
// get local error rate estimate
float totalBases = (float)variant.countAll * (float)variant.readlen;
map<unsigned int, unsigned int>::iterator cmi = (variant.conMis).begin();
unsigned int numncMis = 0;
for (; cmi != (variant.conMis).end(); cmi++) {
if (cmi->second == 1) {
numncMis += 1;
}
}
float localEr;
if (totalBases == 0) {
localEr = 0;
} else {
localEr = ((float)numncMis)/totalBases;
}
cout << variant.chro << "\t" << variant.start << "\t" << variant.countAll << "\t" << variant.countPositive << "\t" << variant.countNegative << "\t" << variant.F1R2_all << "\t" << variant.F2R1_all << "\t" << variant.F1R2_alt << "\t" << variant.F2R1_alt << "\t" << variant.countAlt << "\t" << variant.countA << "\t" << variant.countAn << "\t" << variant.countC << "\t" << variant.countCn << "\t" << variant.countG << "\t" << variant.countGn << "\t" << variant.countT << "\t" << variant.countTn << "\t" << variant.inends << "\t" << variant.countJump << "\t" << setprecision(4) << fracBadMappingQual << "\t" << setprecision(2) << meanMis << "\t" << setprecision(2) << medianMis << "\t" << setprecision(3) << meanIndel << "\t" << setprecision(3) << medianIndel << "\t" << setprecision(3) << medianVRLength << "\t" << setprecision(2) << localEr << "\t" << variant.qualities << endl;
}
inline float CalcMedian (vector<unsigned int> &scores)
{
float median;
size_t size = scores.size();
sort(scores.begin(), scores.end());
if (size % 2 == 0)
{
median = ((float)scores[size / 2 - 1] + (float)scores[size / 2]) / 2;
}
else
{
median = (float)scores[size / 2];
}
return median;
}