forked from pkrusche/vt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discover.cpp
1041 lines (924 loc) · 36.7 KB
/
discover.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
/* The MIT License
Copyright (c) 2013 Adrian Tan <atks@umich.edu>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "discover.h"
namespace
{
typedef struct
{
int32_t start1, end1;
} interval_t;
KHASH_MAP_INIT_STR(rdict, interval_t)
#define SNP 1
#define MNP 2
#define INDEL 4
/**
* Class for mining candidate variants.
*
* Processes an align read and records the variants observed against the reference
*/
class VariantHunter
{
public:
/**
* Constructor
* baseq_cutoff - q value cutoff to select candidate SNPs
*/
VariantHunter(uint32_t vtype,
uint32_t evidence_allele_count_cutoff,
double fractional_evidence_allele_count_cutoff,
uint32_t baseq_cutoff,
faidx_t *fai,
BCFOrderedWriter *odw)
: vtype(vtype),
evidence_allele_count_cutoff(evidence_allele_count_cutoff),
fractional_evidence_allele_count_cutoff(fractional_evidence_allele_count_cutoff),
baseq_cutoff(baseq_cutoff),
fai(fai),
odw(odw),
buffer_size(800),
X(buffer_size),
Y(buffer_size),
I(buffer_size),
D(buffer_size),
N(buffer_size,0),
REF(buffer_size),
ANCHOR(buffer_size),
chrom(0),
start(0), end(0),
empty_buffer_space(0),
min_empty_buffer_size(400),
start_genome_pos0(0),
max_used_buffer_size_threshold(buffer_size-min_empty_buffer_size),
max_indel_length(50),
debug(false)
{
alleles = {0,0,0};
read_seq = {0,0,0};
qual = {0,0,0};
cigar = {0,0,0};
};
/**
* Transfer read into a buffer for processing later
*/
void process_read(bam_hdr_t *h, bam1_t *s)
{
//extract relevant information from sam record
const char* chrom = bam_get_chrom(h, s);
int32_t pos0 = bam_get_pos0(s);
bam_get_seq_string(s, &read_seq);
bam_get_cigar_expanded_string(s, &cigar);
bam_get_qual_string(s, &qual);
int32_t ref_len;
uint32_t q;
//reset buffer if necessary
if (this->chrom)
{
//change in chromosome
if (strcmp(this->chrom, chrom))
{
extract_candidate_variants(this->chrom, UINT_MAX);
free(this->chrom);
this->chrom = strdup(chrom);
}
else
{
extract_candidate_variants(this->chrom, pos0);
}
}
else //first read
{
this->chrom = strdup(chrom);
}
//basically equivalent to emptying the buffer
extract_candidate_variants(chrom, pos0);
char* genome_seq = faidx_fetch_seq(fai, chrom, pos0-1, pos0+cigar.l, &ref_len);
uint32_t genome_seq_pos0 = 1;
uint32_t read_seq_pos0 = 0;
uint32_t cur_pos0 = get_cur_pos0(pos0); //current buffer index
bool last_position_had_snp = false;
bool mnp_allele_construction_in_progress = false;
bool ins_init = true;
bool del_init = true;
uint32_t last_snp_pos = 0;
uint32_t mnp_init_pos = 0;
char mnp_init_base = 'N';
uint32_t ins_init_pos = 0;
uint32_t del_init_pos = 0;
if (0)
{
std::cerr << "===============\n";
std::cerr << "ADD READ\n";
std::cerr << "pos1 : " << (pos0+1) << "\n";
std::cerr << "start_genome_pos0 : " << start_genome_pos0 << "\n";
std::cerr << "end_genome_pos0 : " << start_genome_pos0 + diff(end,start) << "\n";
std::cerr << "start : " << start << "\n";
std::cerr << "end : " << end << "\n";
std::cerr << "cur_pos0 : " << cur_pos0 << "\n";
std::cerr << "chrom : " << chrom << "\n";
std::cerr << "genome sequence : " << genome_seq << "\n";
std::cerr << "read sequence : " << read_seq.s << "\n";
std::cerr << "qual : " << qual.s << "\n";
std::cerr << "cigar : " << cigar.s << "\n";
std::cerr << "cigar length : " << cigar.l << "\n";
}
if (is_empty())
start_genome_pos0 = pos0;
//cycle through the cigar string, this cigar string has essentially been expanded
for (uint32_t cigar_pos0=0; cigar_pos0<cigar.l; ++cigar_pos0)
{
char a = cigar.s[cigar_pos0];
if (a=='M')
{
REF[cur_pos0] = genome_seq[genome_seq_pos0];
q = qual.s[read_seq_pos0]-33;
if (genome_seq[genome_seq_pos0]!=read_seq.s[read_seq_pos0] && q>=baseq_cutoff)
{
X[cur_pos0].push_back(read_seq.s[read_seq_pos0]);
//initialize mnp
if (last_position_had_snp && !mnp_allele_construction_in_progress)
{
mnp_allele_construction_in_progress = true;
mnp_init_pos = last_snp_pos;
last_position_had_snp = false;
Y[mnp_init_pos].push_back("");
Y[mnp_init_pos].back().append(1, mnp_init_base);
}
if (mnp_allele_construction_in_progress)
{
Y[mnp_init_pos].back().append(1, read_seq.s[read_seq_pos0]);
}
last_position_had_snp = true;
last_snp_pos = cur_pos0;
mnp_init_base = read_seq.s[read_seq_pos0];
}
else
{
last_position_had_snp = false;
mnp_allele_construction_in_progress = false;
}
ins_init = true;
del_init = true;
++N[cur_pos0];
add(cur_pos0);
++genome_seq_pos0;
++read_seq_pos0;
}
else if (a=='I')
{
if (ins_init)
{
ins_init_pos = cur_pos0;
I[ins_init_pos].push_back("");
I[ins_init_pos].back().append(1, (read_seq_pos0!=0?read_seq.s[read_seq_pos0-1]:genome_seq[genome_seq_pos0-1]));
ANCHOR[ins_init_pos] = genome_seq[genome_seq_pos0-1];
}
last_position_had_snp =false;
mnp_allele_construction_in_progress = false;
I[ins_init_pos].back().append(1, read_seq.s[read_seq_pos0]);
ins_init = false;
del_init = true;
//helps maintain count as I's can be 3' hanging
if (cigar_pos0==cigar.l-1 || cigar.s[cigar_pos0+1]=='S')
{
++N[ins_init_pos];
}
++read_seq_pos0;
}
else if (a=='D')
{
REF[cur_pos0] = genome_seq[genome_seq_pos0];
if (del_init)
{
del_init_pos = cur_pos0;
D[del_init_pos].push_back("");
D[del_init_pos].back().append(1, (read_seq_pos0!=0?read_seq.s[read_seq_pos0-1]:genome_seq[genome_seq_pos0-1]));
++N[del_init_pos];
ANCHOR[del_init_pos] = genome_seq[genome_seq_pos0-1];
}
D[del_init_pos].back().append(1, genome_seq[genome_seq_pos0]);
last_position_had_snp =false;
mnp_allele_construction_in_progress = false;
del_init = false;
ins_init = true;
add(cur_pos0);
++genome_seq_pos0;
}
else //S, H and others
{
++read_seq_pos0;
}
}
if (ref_len>0) free(genome_seq);
if (0)
{
std::cout << "final cur_pos0 : " << cur_pos0 << "\n";
std::cout << "final start_genome_pos: " << start_genome_pos0 << "\n";
std::cout << "final endGenomePos : " << start_genome_pos0 + diff(end,start) << "\n";
std::cout << "final start : " << start << "\n";
std::cout << "final end : " << end << "\n";
std::cout << "====================\n";
printBuffer();
}
};
/**
* Processes buffer to pick up variants
*/
void extract_candidate_variants()
{
extract_candidate_variants(chrom, 0, true);
};
private:
uint32_t buffer_size;
std::vector<std::vector<char> > X; // contains read bases that differ from the genome
std::vector<std::vector<std::string> > Y; // contains multiple consecutive read bases that differ from the genome
std::vector<std::vector<std::string> > I; //contains inserted bases
std::vector<std::vector<std::string> > D; //contains reference bases that are deleted
std::vector<int32_t> N; // number of evidences observed here - combination of X, I and D
std::vector<char> REF;
std::vector<char> ANCHOR;
std::vector<std::string> ALT;
char* chrom;
//key control variables for circular buffer
uint32_t start, end;
uint32_t empty_buffer_space;
uint32_t min_empty_buffer_size;
uint32_t start_genome_pos0;
uint32_t max_used_buffer_size_threshold;
uint32_t max_indel_length;
uint32_t baseq_cutoff;
uint32_t evidence_allele_count_cutoff;
double fractional_evidence_allele_count_cutoff;
faidx_t *fai;
uint32_t vtype;
kstring_t s;
kstring_t alleles;
kstring_t read_seq;
kstring_t qual;
kstring_t cigar;
bcf1_t *v;
BCFOrderedWriter *odw;
bool debug;
/**
* Processes buffer to pick up variants
* Empty buffer to recover space.
* @chrom - remove variants on chrom
* @pos1 - remove variants up to pos1
* @flush - remove all variants
*/
void extract_candidate_variants(const char* chrom, uint32_t pos1, bool flush=false)
{
//variable to tell when to stop flushing
uint32_t stop = 0;
if (flush)
{
stop = end;
}
else if (is_empty())
{
return;
}
//extract when separated
else if (start_genome_pos0+(diff(end,start))<pos1)
{
if (debug)
{
std::cout << "***********************\n";
std::cout << "flush buffer segregated\n";
}
//flush buffer completely
stop = end;
}
//extract when overlapping
else
{
if (pos1-start_genome_pos0>max_used_buffer_size_threshold)
{
stop = add(start, pos1-start_genome_pos0);
if (debug)
{
std::cout << "************************\n";
std::cout << "flush buffer overlapping\n";
}
}
else
{
return;
}
}
// if (debug)
// {
// std::cout << "pos1 : " << pos1 << "\n";
// std::cout << "stop : " << stop << "\n";
// std::cout << "usedBufferSize : " << diff(end,start) << "\n";
// std::cout << "buffer_size : " << buffer_size << "\n";
// std::cout << "min_empty_buffer_size: " << min_empty_buffer_size << "\n";
// std::cout << "start_genome_pos0 : " << start_genome_pos0 << "\n";
// std::cout << "endGenomePos : " << start_genome_pos0 + diff(end,start) << "\n";
// std::cout << "start : " << start << "\n";
// std::cout << "end : " << end << "\n";
//
// std::cout << pos1 << "," << diff(end,start) << "," << buffer_size-min_empty_buffer_size << "\n";
// }
std::map<char, int32_t> snp_alts;
std::map<std::string, int32_t> mnp_alts;
std::map<std::string, int32_t> indel_alts;
char anchor, ref;
int32_t ref_len;
//print out candidate variants
while (start!=stop)
{
//assayed position
if (N[start]>=1)
{
if (vtype&INDEL)
{
//handling insertions
indel_alts.clear();
anchor = ANCHOR[start];
if (I[start].size()!=0)
{
for (uint32_t i=0; i<I[start].size(); ++i)
{
if (indel_alts.find(I[start][i])==indel_alts.end())
{
indel_alts[I[start][i]] = 1;
}
else
{
++indel_alts[I[start][i]];
}
}
for (std::map<std::string, int32_t>::iterator i =indel_alts.begin(); i!=indel_alts.end(); ++i)
{
//make sure that we do not output alleles with N bases.
if (i->second>= evidence_allele_count_cutoff &&
((double)i->second/(double) N[start]) >= fractional_evidence_allele_count_cutoff &&
anchor!='N' && (i->first).find_first_of('N')==std::string::npos)
{
v = odw->get_bcf1_from_pool();
bcf_set_chrom(odw->hdr, v, chrom);
bcf_set_pos1(v, start_genome_pos0);
alleles.l = 0;
kputc(anchor, &alleles);
kputc(',', &alleles);
kputs(i->first.c_str(), &alleles);
bcf_update_alleles_str(odw->hdr, v, alleles.s);
bcf_update_format_int32(odw->hdr, v, "E", &i->second, 1);
bcf_update_format_int32(odw->hdr, v, "N", &N[start], 1);
odw->write(v);
}
}
}
//handling deletions
indel_alts.clear();
if (D[start].size()!=0)
{
for (uint32_t i=0; i<D[start].size(); ++i)
{
if (indel_alts.find(D[start][i])==indel_alts.end())
{
indel_alts[D[start][i]] = 1;
}
else
{
++indel_alts[D[start][i]];
}
}
for (std::map<std::string, int32_t>::iterator i = indel_alts.begin(); i!= indel_alts.end(); ++i)
{
//make sure that we do not output alleles with N bases.
if (i->second>= evidence_allele_count_cutoff &&
((double)i->second/(double) N[start]) >= fractional_evidence_allele_count_cutoff &&
anchor!='N' && (i->first).find_first_of('N')==std::string::npos)
{
v = odw->get_bcf1_from_pool();
bcf_set_chrom(odw->hdr, v, chrom);
bcf_set_pos1(v, start_genome_pos0);
alleles.l = 0;
const char* deletedAllele = i->first.c_str();
char replacement_anchor = deletedAllele[0];
kputc(anchor, &alleles);
++deletedAllele;
kputs(deletedAllele, &alleles);
kputc(',', &alleles);
kputc(replacement_anchor, &alleles);
bcf_update_alleles_str(odw->hdr, v, alleles.s);
bcf_update_format_int32(odw->hdr, v, "E", &i->second, 1);
bcf_update_format_int32(odw->hdr, v, "N", &N[start], 1);
odw->write(v);
}
}
}
}
if (vtype&SNP)
{
//handling SNPs
snp_alts.clear();
ref = REF[start];
if (X[start].size()!=0)
{
for (uint32_t i=0; i<X[start].size(); ++i)
{
if (snp_alts.find(X[start][i])==snp_alts.end())
{
snp_alts[X[start][i]] = 1;
}
else
{
++snp_alts[X[start][i]];
}
}
for (std::map<char, int32_t>::iterator i =snp_alts.begin(); i!=snp_alts.end(); ++i)
{
//make sure that we do not output alleles with N bases.
if (i->second>= evidence_allele_count_cutoff &&
((double)i->second/(double) N[start]) >= fractional_evidence_allele_count_cutoff &&
ref!='N' && (i->first)!='N')
{
v = odw->get_bcf1_from_pool();
bcf_set_chrom(odw->hdr, v, chrom);
bcf_set_pos1(v, start_genome_pos0+1);
alleles.l = 0;
kputc(ref, &alleles);
kputc(',', &alleles);
kputc(i->first, &alleles);
bcf_update_alleles_str(odw->hdr, v, alleles.s);
bcf_update_format_int32(odw->hdr, v, "E", &i->second, 1);
bcf_update_format_int32(odw->hdr, v, "N", &N[start], 1);
odw->write(v);
}
}
}
}
if (vtype&MNP)
{
//handling MNPs
mnp_alts.clear();
if (Y[start].size()!=0)
{
for (uint32_t i=0; i<Y[start].size(); ++i)
{
if (mnp_alts.find(Y[start][i])==mnp_alts.end())
{
mnp_alts[Y[start][i]] = 1;
}
else
{
++mnp_alts[Y[start][i]];
}
}
for (std::map<std::string, int32_t>::iterator i =mnp_alts.begin(); i!=mnp_alts.end(); ++i)
{
char* seq = faidx_fetch_seq(fai, const_cast<char*>(chrom), start_genome_pos0, start_genome_pos0+i->first.size()-1, &ref_len);
//make sure that we do not output alleles with N bases.
if (i->second>= evidence_allele_count_cutoff &&
((double)i->second/(double) N[start]) >= fractional_evidence_allele_count_cutoff &&
!strchr(seq, 'N') && (i->first).find_first_of('N')==std::string::npos)
{
v = odw->get_bcf1_from_pool();
bcf_set_chrom(odw->hdr, v, chrom);
bcf_set_pos1(v, start_genome_pos0+1);
alleles.l = 0;
kputc(anchor, &alleles);
kputc(',', &alleles);
kputs(i->first.c_str(), &alleles);
bcf_update_alleles_str(odw->hdr, v, alleles.s);
bcf_update_format_int32(odw->hdr, v, "E", &i->second, 1);
bcf_update_format_int32(odw->hdr, v, "N", &N[start], 1);
odw->write(v);
}
if (ref_len>0) free(seq);
}
}
}
}
Y[start].clear();
X[start].clear();
I[start].clear();
D[start].clear();
N[start] = 0;
add(start);
++start_genome_pos0;
}
// //clean up final position too
// if (is_empty())
// {
// X[start].clear();
// I[start].clear();
// D[start].clear();
// N[start] = 0;
// }
// if (debug)
// {
// std::cout << "final start : " << start << "\n";
// std::cout << "final end : " << end << "\n";
// std::cout << "*************\n";
// printBuffer();
// }
};
/**
* Checks if buffer is empty
*/
bool is_empty()
{
return start==end;
};
/**
*Increments buffer index i by 1.
*/
void add(uint32_t& i)
{
if (i>=buffer_size)
{
std::cerr << "Unaccepted buffer index: " << i << " (" << buffer_size << ")\n";
exit(1);
}
uint32_t temp = (i+1)%buffer_size;
i = end==i ? (end=temp) : temp;
};
/**
* Increments buffer index i by j.
*/
uint32_t add(uint32_t i, uint32_t j)
{
if (i>=buffer_size)
{
std::cerr << "Unaccepted buffer index: " << i << " (" << buffer_size << ")\n";
exit(1);
}
return (i+j)%buffer_size;
};
/**
* Decrements buffer index i by j.
*/
uint32_t minus(uint32_t& i, uint32_t j)
{
if (i>=buffer_size)
{
std::cerr << "Unaccepted buffer index: " << i << " (" << buffer_size << ")\n";
exit(1);
}
return (i>=j ? i-j : buffer_size-(j-i));
};
/**
* Decrements buffer index i by 1.
*/
void minus(uint32_t& i)
{
if (i>=buffer_size)
{
std::cerr << "Unaccepted buffer index: " << i << " (" << buffer_size << ")\n";
exit(1);
}
i = (i>=1 ? i-1 : buffer_size-1);
};
/**
* Returns the difference between 2 buffer positions
*/
uint32_t diff(uint32_t i, uint32_t j)
{
return (i>=j ? i-j : buffer_size-(j-i));
};
/**
* Gets the position in the buffer that corresponds to
* the genome position indicated by pos.
*/
uint32_t get_cur_pos0(uint32_t genome_pos0)
{
//when buffer is empty
if (is_empty())
{
start_genome_pos0 = genome_pos0;
return start;
}
else
{
if (genome_pos0-start_genome_pos0>buffer_size)
{
std::cerr << "overflow buffer\n" ;
//should allow for unbuffering here
}
return (start + (genome_pos0-start_genome_pos0))%buffer_size;
}
};
/**
* Print buffer contents for debugging purpose
*/
void printBuffer()
{
std::cout << "PRINT BUFFER" << "\n";
std::cout << "usedBufferSize: " << diff(end,start) << "\n";
uint32_t cur_pos0 = start;
uint32_t genome_pos = start_genome_pos0;
while (cur_pos0!=end)
{
std::cout << genome_pos << "\t" << cur_pos0 << "\t" << REF[cur_pos0] << "\t";
for (uint32_t j=0; j<I[cur_pos0].size(); ++j)
{
std::cout << I[cur_pos0][j] << ",";
}
for (uint32_t j=0; j<D[cur_pos0].size(); ++j)
{
std::cout << D[cur_pos0][j] << ",";
}
std::cout << "\t" << N[cur_pos0] << "\n";
add(cur_pos0);
++genome_pos;
}
};
};
class Igor : Program
{
public:
///////////
//options//
///////////
std::vector<GenomeInterval> intervals;
std::string output_vcf_file;
std::string input_bam_file;
std::string ref_fasta_file;
std::string sample_id;
uint32_t mapq_cutoff;
uint32_t baseq_cutoff;
//takes on snps, mnps, indels
std::string variant_type;
uint32_t evidence_allele_count_cutoff;
double fractional_evidence_allele_count_cutoff;
uint16_t exclude_flag;
///////
//i/o//
///////
BAMOrderedReader *odr;
bam1_t *s;
BCFOrderedWriter *odw;
bcf1_t *v;
/////////
//stats//
/////////
uint32_t no_reads;
uint32_t no_overlapping_reads;
uint32_t no_passed_reads;
uint32_t no_exclude_flag_reads;
uint32_t no_low_mapq_reads;
/////////
//tools//
/////////
VariantHunter *variantHunter;
Igor(int argc, char **argv)
{
version = "0.5";
//////////////////////////
//options initialization//
//////////////////////////
try
{
std::string desc = "Discovers variants from reads in a BAM file.";
TCLAP::CmdLine cmd(desc, ' ', version);
VTOutput my; cmd.setOutput(&my);
TCLAP::ValueArg<std::string> arg_intervals("i", "i", "intervals []", false, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_interval_list("I", "I", "file containing list of intervals []", false, "", "file", cmd);
TCLAP::ValueArg<std::string> arg_output_vcf_file("o", "o", "output VCF file [-]", false, "-", "str", cmd);
TCLAP::ValueArg<std::string> arg_ref_fasta_file("r", "r", "reference sequence fasta file []", true, "", "str", cmd);
TCLAP::ValueArg<std::string> arg_sample_id("s", "s", "sample ID", true, "", "str", cmd);
TCLAP::ValueArg<uint32_t> arg_mapq_cutoff("m", "m", "MAPQ cutoff for alignments [20]", false, 20, "int", cmd);
TCLAP::ValueArg<uint32_t> arg_baseq_cutoff("q", "q", "base quality cutoff for bases [13]", false, 13, "int", cmd);
TCLAP::ValueArg<uint32_t> arg_evidence_allele_count_cutoff("e", "e", "evidence count cutoff for candidate allele [2]", false, 2, "int", cmd);
TCLAP::ValueArg<double> arg_fractional_evidence_allele_count_cutoff("f", "f", "fractional evidence cutoff for candidate allele [0.1]", false, 0.1, "float", cmd);
TCLAP::ValueArg<std::string> arg_variant_type("v", "v", "variant types [snps,mnps,indels]", false, "snps,mnps,indels", "str", cmd);
TCLAP::ValueArg<std::string> arg_input_bam_file("b", "b", "input BAM file", true, "", "string", cmd);
cmd.parse(argc, argv);
input_bam_file = arg_input_bam_file.getValue();
parse_intervals(intervals, arg_interval_list.getValue(), arg_intervals.getValue());
output_vcf_file = arg_output_vcf_file.getValue();
sample_id = arg_sample_id.getValue();
ref_fasta_file = arg_ref_fasta_file.getValue();
mapq_cutoff = arg_mapq_cutoff.getValue();
baseq_cutoff = arg_baseq_cutoff.getValue();
variant_type = arg_variant_type.getValue();
evidence_allele_count_cutoff = arg_evidence_allele_count_cutoff.getValue();
fractional_evidence_allele_count_cutoff = arg_fractional_evidence_allele_count_cutoff.getValue();
}
catch (TCLAP::ArgException &e)
{
std::cerr << "error: " << e.error() << " for arg " << e.argId() << "\n";
abort();
}
};
void initialize()
{
//////////////////////
//i/o initialization//
//////////////////////
exclude_flag = 0x0704;
odr = new BAMOrderedReader(input_bam_file, intervals);
s = bam_init1();
odw = new BCFOrderedWriter(output_vcf_file, 0);
bam_hdr_transfer_contigs_to_bcf_hdr(odr->hdr, odw->hdr);
bcf_hdr_append(odw->hdr, "##FORMAT=<ID=E,Number=1,Type=Integer,Description=\"Number of reads containing evidence of the alternate allele\">");
bcf_hdr_append(odw->hdr, "##FORMAT=<ID=N,Number=1,Type=Integer,Description=\"Total number of reads at a candidate locus with reads that contain evidence of the alternate allele\">");
bcf_hdr_add_sample(odw->hdr, sample_id.c_str());
bcf_hdr_add_sample(odw->hdr, NULL);
//bcf_hdr_set_n_sample(odw->hdr, 1);
v = NULL;
std::vector<std::string> variant_types;
split(variant_types, ",", variant_type);
uint32_t vtype = 0;
for (uint32_t i = 0; i<variant_types.size(); ++i)
{
if (variant_types[i] == "snps")
{
vtype |= SNP;
}
else if (variant_types[i] == "mnps")
{
vtype |= MNP;
}
else if (variant_types[i] == "indels")
{
vtype |= INDEL;
}
else if (variant_types[i] == "all")
{
vtype = SNP|MNP|INDEL;
}
}
////////////////////////
//stats initialization//
////////////////////////
no_reads = 0;
no_overlapping_reads = 0;
no_passed_reads = 0;
no_exclude_flag_reads = 0;
no_low_mapq_reads = 0;
////////////////////////
//tools initialization//
////////////////////////
faidx_t *fai = fai_load(ref_fasta_file.c_str());
if (fai==NULL)
{
fprintf(stderr, "[%s:%d %s] Cannot load genome index: %s\n", __FILE__, __LINE__, __FUNCTION__, ref_fasta_file.c_str());
exit(1);
}
variantHunter = new VariantHunter(vtype,
evidence_allele_count_cutoff,
fractional_evidence_allele_count_cutoff,
baseq_cutoff,
fai,
odw);
}
void discover()
{
odw->write_hdr();
//for tracking overlapping reads
khash_t(rdict) *reads = kh_init(rdict);
khiter_t k;
int32_t ret;
while (odr->read(s))
{
++no_reads;
//this read is the first of the pair
if (bam_get_mpos1(s) && (bam_get_tid(s)==bam_get_mtid(s)))
{
//first mate
if (bam_get_mpos1(s)>bam_get_pos1(s))
{
//overlapping
if (bam_get_mpos1(s)<=(bam_get_pos1(s) + bam_get_l_qseq(s) - 1))
{
//add read that has overlapping
//duplicate the record and perform the stitching later
char* qname = strdup(bam_get_qname(s));
k = kh_put(rdict, reads, qname, &ret);
if (!ret)
{
//already present
free(qname);
}
kh_val(reads, k) = {bam_get_pos1(s), bam_get_pos1(s)+bam_get_l_qseq(s)-1};
}
}
else
{
//check overlap
//todo: perform stitching in future
if((k = kh_get(rdict, reads, bam_get_qname(s)))!=kh_end(reads))
{
if (kh_exist(reads, k))
{
free((char*)kh_key(reads, k));
kh_del(rdict, reads, k);
++no_overlapping_reads;
}
//continue;
}
}
}
if(bam_get_flag(s) & exclude_flag)
{
//1. unmapped
//2. secondary alignment
//3. not passing QC
//4. PCR or optical duplicate
++no_exclude_flag_reads;
continue;
}
if (bam_get_mapq(s) < mapq_cutoff)
{
//filter short aligments and those with too many indels (?)
++no_low_mapq_reads;
continue;
}
if (0)
{
bam_print(s);
}
variantHunter->process_read(odr->hdr, s);
// if (no_reads%100000==0) std::cerr << no_reads << "\n";
++no_passed_reads;
}
odw->close();
};
void bam_print(bam1_t *s)
{
const char* chrom = bam_get_chrom(odr->hdr, s);
uint32_t pos1 = bam_get_pos1(s);
kstring_t seq = {0,0,0};
bam_get_seq_string(s, &seq);
uint32_t len = bam_get_l_qseq(s);
kstring_t qual = {0,0,0};
bam_get_qual_string(s, &qual);
kstring_t cigar_string = {0,0,0};
bam_get_cigar_string(s, &cigar_string);
kstring_t cigar_expanded_string = {0,0,0};
bam_get_cigar_expanded_string(s, &cigar_expanded_string);
uint16_t flag = bam_get_flag(s);
uint32_t mapq = bam_get_mapq(s);
std::cerr << "##################" << "\n";
std::cerr << "read no : " << no_reads << "\n";
std::cerr << "chrom-pos: " << chrom << "-" << pos1 << "\n";
std::cerr << "read : " << seq.s << "\n";
std::cerr << "qual : " << qual.s << "\n";
std::cerr << "cigar_str: " << cigar_string.s << "\n";
std::cerr << "cigar : " << cigar_expanded_string.s << "\n";
std::cerr << "len : " << len << "\n";
std::cerr << "mapq : " << mapq << "\n";
std::cerr << "mpos1 : " << bam_get_mpos1(s) << "\n";
std::cerr << "mtid : " << bam_get_mtid(s) << "\n";
if (seq.m) free(seq.s);
if (qual.m) free(qual.s);
if (cigar_string.m) free(cigar_string.s);
if (cigar_expanded_string.m) free(cigar_expanded_string.s);
}
void print_options()
{
std::clog << "discover v" << version << "\n\n";