-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconvert.cpp
More file actions
executable file
·1366 lines (1149 loc) · 37.4 KB
/
Copy pathconvert.cpp
File metadata and controls
executable file
·1366 lines (1149 loc) · 37.4 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
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
//
// Created by Yuhao Dan on 2020/4/13.
//
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <vector>
#include <iostream>
#include <chrono>
#include <algorithm>
#include <fcntl.h>
#include <stdio.h>
#include <cstdlib>
#include <random>
#include <sstream>
#include <ctime>
#include "./htslib-1.10.2/htslib/kseq.h"
#include "./htslib-1.10.2/htslib/bgzf.h"
#include "./htslib-1.10.2/htslib/hfile.h"
#include "./htslib-1.10.2/htslib/regidx.h"
#include "./include/convert.h"
#include "./include/utils.h"
namespace std {
bool ContextConvert::parse_region() {
//parse the -r chr:start-end
const char *reg = region;
int flags = 0;
while(*reg) {
reg = sam_parse_region(hdr_bam, reg, &i_tid, &i_beg, &i_end, flags);
if (!reg) {
return false;
}
}
i_chr = hdr_bam->target_name[i_tid];
return true;
}
bool load_get_cpg_with_idx(ContextConvert &ctx, char *chr, hts_pos_t beg, hts_pos_t end, hts_pos_t shift = 500) {
//concat name of the tbi file
ctx.cpg_pos.clear();
hts_pos_t i_beg;
i_beg = beg - shift;
if (i_beg < 0) {
i_beg = 0;
}
hts_pos_t i_end = end + shift;
int tbx_tid = tbx_name2id(ctx.idx_cpg, chr);
ctx.cpg_itr = tbx_itr_queryi(ctx.idx_cpg, tbx_tid, i_beg, i_end);
kstring_t ksbuf = {0, 0, NULL};
string cpg_line_sv;
hts_pos_t cpg_start = 0;
hts_pos_t cpg_end = 0;
while(tbx_itr_next(ctx.fp_cpg, ctx.idx_cpg, ctx.cpg_itr, &ksbuf) >= 0) {
cpg_line_sv = string(ksbuf.s);
int i = 0;
for (; i < cpg_line_sv.size(); i++) {
if (cpg_line_sv[i] == '\t') {
cpg_line_sv = cpg_line_sv.substr(i + 1);
//curser清零
i = 0;
//找到原line中的第2个tab位置
for (; i < cpg_line_sv.size(); i++) {
if (cpg_line_sv[i] == '\t') {
cpg_start = atoll(string(cpg_line_sv.substr(0, i)).c_str());
cpg_end = atoll(string(cpg_line_sv.substr(i, cpg_line_sv.size() - i)).c_str());
//确保cpg位点在用户指定的范围内。
ctx.cpg_pos.push_back(cpg_start);
}
}
}
}
}
return true;
}
inline SamRead::~SamRead() {
}
bool SamRead::init(ContextConvert &ctx) {
bool ret;
this->ctx = &ctx;
read_len = ctx.aln->core.l_qseq; //length of the read.
read_chr = ctx.hdr_bam->target_name[ctx.aln->core.tid] ; //checked
read_start = ctx.aln->core.pos +1; //left most position of alignment in zero based coordianate (+1)
read_end = ctx.aln->core.pos + read_len;
read_qual = bam_get_qual(ctx.aln); //quality string
read_name = bam_get_qname(ctx.aln);
flag = ctx.aln->core.flag; //
read_map_quality = int(ctx.aln->core.qual); //mapping quality
read_cigar = bam_get_cigar(ctx.aln);
//get the seq pointer and put it into a vector
uint8_t *seq_p = bam_get_seq(ctx.aln);
if (read_len == 0) {
return false;
}
//quality check
if (_get_XM(ctx)) {
ret = _get_bismark_QC(ctx);
if (!ret) {
return false;
}
}
//get direction
if (ctx.non_directional) {
read_WC = DIRECTION_UNKNOWN;
} else {
if (ctx.aln->core.flag & BAM_FPAIRED && ctx.aln->core.flag & BAM_FPROPER_PAIR) {
if (ctx.aln->core.flag & BAM_FREAD1) {
if (ctx.aln->core.flag & BAM_FREVERSE) {
read_WC = DIRECTION_MINUS;
} else if (ctx.aln->core.flag & BAM_FMREVERSE) {
read_WC = DIRECTION_PLUS;
}
} else if (ctx.aln->core.flag & BAM_FREAD2 ) {
if (ctx.aln->core.flag & BAM_FREVERSE) {
read_WC = DIRECTION_PLUS;
} else if (ctx.aln->core.flag & BAM_FMREVERSE) {
read_WC = DIRECTION_MINUS;
}
}
} else if (!(ctx.aln->core.flag & BAM_FPAIRED) && !(ctx.aln->core.flag & BAM_FPROPER_PAIR)){
if (ctx.aln->core.flag & BAM_FREVERSE) {
read_WC = DIRECTION_MINUS;
} else {
read_WC = DIRECTION_PLUS;
}
} else {
return false;
}
}
seq = new char[read_len];
for(int i = 0; i < read_len; i++) {
*(seq + i) = kbase[bam_seqi(seq_p, i)];
}
return true;
}
bool SamRead::haplo_type() {
hts_pos_t r_pos;
vector<hts_pos_t> cpg;
vector<int8_t> hap_qual;
_hap_seq = "";
hts_pos_t pos;
for (int i = 0; i < ctx->cpg_pos.size(); i++) {
pos = ctx->cpg_pos[i];
if (read_WC == DIRECTION_PLUS || read_WC == DIRECTION_UNKNOWN) {
if (pos < read_start) {
continue;
}
if (pos > read_end) {
break;
}
r_pos = pos - read_start;
} else {
if (pos < read_start - 1) {
continue;
}
if (pos > read_end - 1) {
break;
}
r_pos = pos - read_start + 1;
}
if (r_pos >= read_len || r_pos < 0) {
continue;
}
cpg.push_back(pos);
_hap_seq += seq[r_pos];
hap_qual.push_back(read_qual[r_pos]);
}
if (seq != NULL) {
delete [] seq;
seq = NULL;
}
if (cpg.size() == 0) {
hts_log_trace("remove this read due to the size of cpg is 0.");
QC = false;
}
_hap_qual = hap_qual;
_cpg = cpg;
_hap_met = "";
for (int i = 0; i < _hap_seq.size(); i++) {
char nucleobases = _hap_seq[i];
if (read_WC == DIRECTION_PLUS || read_WC == DIRECTION_UNKNOWN) {
if (nucleobases == 'C') {
if (ctx->mode == "BS") {
_hap_met += '1';
} else if (ctx->mode == "TAPS") {
_hap_met += '0';
}
} else if (nucleobases == 'T') {
if (ctx->mode == "BS") {
_hap_met += '0';
} else if (ctx->mode == "TAPS") {
_hap_met += '1';
}
} else {
_hap_met += nucleobases;
hts_log_trace("Direction + or * : beg: %lld, end: %lld, nucleobases error: %s", read_start,read_end, _hap_met.c_str());
QC = false;
}
} else if (read_WC == DIRECTION_MINUS) {
if (nucleobases == 'G') {
if (ctx->mode == "BS") {
_hap_met += '1';
} else if (ctx->mode == "TAPS") {
_hap_met += '0';
}
} else if (nucleobases == 'A') {
if (ctx->mode == "BS") {
_hap_met += '0';
} else if (ctx->mode == "TAPS") {
_hap_met += '1';
}
} else {
_hap_met += nucleobases;
hts_log_trace("Direction - : beg: %lld, end: %lld, nucleobases error: %s", read_start,read_end, _hap_met.c_str());
QC = false;
}
} else {
hts_log_trace("Strand undefined");
QC = false;
}
}
if (_cpg.size() > 0) {
HT = HT_s(read_chr, _cpg[0], _cpg[_cpg.size() - 1], _hap_met, 1, read_WC);
}
return true;
}
bool convert_opt_check(ContextConvert &ctx) {
if (ctx.fn_bam == NULL || ctx.fn_cpg == NULL) {
hts_log_error("Please specify -i and -c options.");
return false;
}
if (ctx.region != NULL && ctx.fn_bed != NULL) {
hts_log_error("You can not specify both -r and -b options.");
return false;
}
return true;
}
bool SamRead::_get_XM(ContextConvert &ctx) {
ctx.bam_aux_p = bam_aux_get(ctx.aln, "XM");
if (!ctx.bam_aux_p) {
return false;
}
return true;
}
bool SamRead::_get_bismark_QC(ContextConvert &ctx) {
XM_string = bam_aux2Z(ctx.bam_aux_p);
if (!XM_string) {
hts_log_error("has no XM string");
return false;
}
string XM_str = string(XM_string); //todo:string_view
for (auto c : XM_str) {
if (c == 'X' || c == 'H' || c == 'U') {
return false;
}
}
return true;
}
bool paired_end_check(SamRead &samF, SamRead &samR) {
if (strcmp(samF.read_name, samR.read_name) != 0) {
return false;
}
if (strcmp(samF.read_chr, samR.read_chr) != 0) {
return false;
}
bool checkF = samF._cpg[samF._cpg.size() - 1] >= samR._cpg[0];
bool checkR = samR._cpg[samR._cpg.size() - 1] >= samF._cpg[0];
return (checkF && checkR) || (!checkF && !checkR);
}
HT_s paired_end_merge(SamRead &samF, SamRead &samR) {
map<hts_pos_t, char> merged_seq;
map<hts_pos_t, int8_t> merged_qual;
map<hts_pos_t, char> merged_met;
map<hts_pos_t, char>::iterator merged_met_itor;
for (int i = 0; i < samF._cpg.size(); i++) {
hts_pos_t pos = samF._cpg[i];
merged_seq[pos] = samF._hap_seq[i];
merged_qual[pos] = samF._hap_qual[i];
merged_met[pos] = samF._hap_met[i];
}
for (int i = 0; i < samR._cpg.size(); i++) {
hts_pos_t pos = samR._cpg[i];
merged_met_itor = merged_met.find(pos);
if (merged_met_itor == merged_met.end() || samR._hap_qual[i] > merged_qual[pos]) {
merged_seq[pos] = samR._hap_seq[i];
merged_qual[pos] = samR._hap_qual[i];
merged_met[pos] = samR._hap_met[i];
}
}
//map创建后,其key直接就是降序排列的
string hap_seq = "";
string hap_met = "";
merged_met_itor = merged_met.begin();
vector<hts_pos_t> cpg;
while(merged_met_itor != merged_met.end()) {
cpg.push_back(merged_met_itor->first);
hap_seq += merged_seq[merged_met_itor->first];
hap_met += merged_met[merged_met_itor->first];
merged_met_itor++;
}
HT_s merged_HT = HT_s(samF.read_chr, cpg[0], cpg[cpg.size() - 1], hap_met, 1, samF.read_WC);
return merged_HT;
}
bool region_to_parse(ContextConvert &ctx) {
/* Recognize the way the user specifies the region
* 1.use command line -r option to specify single region --> SINGLE_REGION
* 2.use bed file --> MULTI_REGION
* 3.not specify, process whole file --> WHOLE_FILE
* */
if (ctx.region) {
ctx.region_to_parse = SINGLE_REGION;
hts_log_trace("SINGLE_REGION");
} else if (ctx.fn_bed) {
ctx.region_to_parse = MULTI_REGION;
hts_log_trace("MULTI_REGION");
} else if (ctx.fn_cpg && ctx.fn_bam) {
ctx.region_to_parse = WHOLE_FILE;
hts_log_trace("WHOLE_FILE");
} else {
return false;
}
return true;
}
bool comp_HT_vec(const HT_s &a, const HT_s &b)
{
if (strcmp(a.h_chr, b.h_chr) == 0) {
if (a.h_start != b.h_start) {
return a.h_start < b.h_start;
} else if (a.h_end != b.h_end) {
return a.h_end < b.h_end;
} else if (a.hap_met != b.hap_met) {
return a.hap_met < b.hap_met;
} else if (a.ht_count != b.ht_count){
return a.ht_count < b.ht_count;
} else {
return a.WC < b.WC;
}
} else {
return strcmp(a.h_chr, b.h_chr) < 0;
}
}
bool load_cpg_no_idx(ContextConvert &ctx) {
kstring_t cpg_line = {0,0,NULL};
unordered_map<int, vector<hts_pos_t> >::iterator cpg_pos_map_itor;
while (hts_getline(ctx.fp_cpg, KS_SEP_LINE, &cpg_line) > 0) {
char *p ,*q;
int tid;
hts_pos_t cpg_start;
p = q = cpg_line.s;
while(*q && *q != '\t') {
q++;
}
*q = '\0';
tid = bam_name2id(ctx.hdr_bam, p);
*q = '\t';
p = q + 1;
q = p;
while(*q && *q != '\t') {
q++;
}
*q = '\0';
cpg_start = atoll(p);
cpg_pos_map_itor = ctx.cpg_pos_map.find(tid);
if (cpg_pos_map_itor == ctx.cpg_pos_map.end()) {
vector<hts_pos_t> v;
v.push_back(cpg_start);
ctx.cpg_pos_map[tid] = v;
} else {
ctx.cpg_pos_map[tid].push_back(cpg_start);
}
}
return true;
}
int _lower_bound(vector<hts_pos_t> &v, hts_pos_t &cpg_pos)//二分查找求下界
//找到cpg_pos这个数字在v中的下标
{
int low = 0, high = v.size() - 1;
while(low < high)
{
int mid = low + (high - low)/2;
if(v[mid] >= cpg_pos) high = mid;
else low = mid + 1;
}
return low;
}
void get_cpg_no_idx(ContextConvert &ctx, char *chr, hts_pos_t &beg, hts_pos_t &end, hts_pos_t shift = 500) {
ctx.cpg_pos.clear();
hts_pos_t i_beg;
i_beg = beg - shift;
if (i_beg < 0) {
i_beg = 0;
}
hts_pos_t i_end = end + shift;
int tid = bam_name2id(ctx.hdr_bam, chr);
int pos = _lower_bound(ctx.cpg_pos_map[tid], i_beg);
unordered_map<int, vector<hts_pos_t> >::iterator cpg_pos_map_itor;
cpg_pos_map_itor = ctx.cpg_pos_map.find(tid);
if (cpg_pos_map_itor != ctx.cpg_pos_map.end()) {
while (pos < ctx.cpg_pos_map[tid].size()) {
if (ctx.cpg_pos_map[tid][pos] >= i_beg && ctx.cpg_pos_map[tid][pos] <= i_end) {
ctx.cpg_pos.push_back(ctx.cpg_pos_map[tid][pos]);
pos++;
} else {
break;
}
}
}
}
void save_cache(ContextConvert &ctx) {
}
bool open_bam_file(ContextConvert &ctx) {
ctx.fp_bam = hts_open(ctx.fn_bam, "r");
ctx.hdr_bam = sam_hdr_read(ctx.fp_bam); //read header
if (ctx.hdr_bam == NULL) {
return false;
}
ctx.aln = bam_init1();
if (ctx.aln == NULL) {
return false;
}
return true;
}
vector<HT_s> itor_sam(ContextConvert &ctx) {
unordered_map<string, SamRead > sam_map;
uint64_t sam_map_cache_size = 50000;
uint64_t read_id_cnt = 0;
unordered_map<string, SamRead >::iterator iter_find; // iter used to find a specific sam read
unordered_map<string, SamRead >::iterator iter;
unordered_map<string, SamRead >::iterator iter_pre;
vector<HT_s> HT_vec;
if (ctx.region_to_parse == SINGLE_REGION) {
//load tbi index outside the load_get_cpg_with_idx()
string cpg_idx_fn = string(ctx.fn_cpg) + string(".tbi");
ctx.idx_cpg = tbx_index_load(cpg_idx_fn.c_str());
ctx.has_idx_cpg = true;
if (ctx.idx_cpg == NULL) {
string error_message = "Please run the command to generate the index file (.tbi):\ntabix -b 2 -e 3 -p bed " +
string(ctx.fn_cpg) + "\nand place the index file with the CpG file in the same folder.";
hts_log_error("%s", error_message.c_str());
vector<HT_s> null_HT_s;
return null_HT_s;
}
}
uint64_t cnt = 0;
char* cur_chr = NULL;
char* pre_chr = NULL;
if (ctx.region_to_parse == SINGLE_REGION) {
auto single_start = std::chrono::high_resolution_clock::now(); //stop_watch
//load bai index
static string bam_idx_fn = string(ctx.fn_bam) + ".bai";
ctx.idx_bam = sam_index_load(ctx.fp_bam, bam_idx_fn.c_str());
if (ctx.idx_bam == NULL) {
string error_message = "Please run the command to generate the index file (.bai):\nsamtools index " +
string(ctx.fn_bam) + "\nand place the index file with the input file in the same folder.";
hts_log_error("%s", error_message.c_str());
vector<HT_s> null_HT_s;
return null_HT_s;
}
hts_itr_t *sam_itr = sam_itr_queryi(ctx.idx_bam, ctx.i_tid, ctx.i_beg, ctx.i_end);
while(sam_itr_next(ctx.fp_bam, sam_itr, ctx.aln) >= 0) {
++cnt;
if (cnt % 1000000 == 0) {
cout << cnt << " reads processed." << endl;
}
if (ctx.aln->core.flag & BAM_FQCFAIL || ctx.aln->core.flag & BAM_FUNMAP || ctx.aln->core.flag & BAM_FDUP
|| ctx.aln->core.flag & BAM_FSECONDARY || ctx.aln->core.flag & BAM_FSUPPLEMENTARY) {
continue;
}
SamRead sam_r = SamRead();
int ret = sam_r.init(ctx);
if (!ret) {
hts_log_trace("san_r init error.");
if (sam_r.seq != NULL) {
delete [] sam_r.seq;
}
continue;
}
sam_r.read_id = read_id_cnt;
if (read_id_cnt + 1 == 0) {
hts_log_error("read_id_cnt overflow.");
vector<HT_s> null_HT_s;
return null_HT_s;
}
++read_id_cnt;
if ((read_id_cnt % sam_map_cache_size) == 0) {
iter = sam_map.begin();
while(iter != sam_map.end()){
iter_pre = iter;
++iter;
if ((read_id_cnt - iter_pre->second.read_id) > 5000) {
HT_vec.push_back(iter_pre->second.HT);
sam_map.erase(iter_pre);
}
}
}
string qname = string(sam_r.read_name);
load_get_cpg_with_idx(ctx, sam_r.read_chr, sam_r.read_start, sam_r.read_end);
sam_r.haplo_type();
if (!sam_r.QC) {
continue;
}
iter_find = sam_map.find(qname);
if (iter_find == sam_map.end()) {
sam_map[qname] = sam_r;
} else {
SamRead samF = sam_map[qname];
SamRead samR = sam_r;
if (paired_end_check(samF, samR)) {
HT_s ht = paired_end_merge(samF, samR);
HT_vec.push_back(ht);
} else {
HT_vec.push_back(samF.HT);
HT_vec.push_back(samR.HT);
}
sam_map.erase(iter_find);
}
}
} else if (ctx.region_to_parse == WHOLE_FILE) {
// load the index file to ensure the bam file is sorted
static string bam_idx_fn = string(ctx.fn_bam) + ".bai";
ctx.idx_bam = sam_index_load(ctx.fp_bam, bam_idx_fn.c_str());
if (ctx.idx_bam == NULL) {
string error_message = "Please run the command to generate the index file (.bai):\nsamtools index " +
string(ctx.fn_bam) + "\nand place the index file with the input file in the same folder.";
hts_log_error("%s", error_message.c_str());
vector<HT_s> null_HT_s;
return null_HT_s;
}
load_cpg_no_idx(ctx);
while(sam_read1(ctx.fp_bam, ctx.hdr_bam, ctx.aln) >= 0) {
++cnt;
if (cnt % 1000000 == 0) {
cout << cnt << " reads processed." << endl;
}
if (ctx.aln->core.flag & BAM_FQCFAIL || ctx.aln->core.flag & BAM_FUNMAP || ctx.aln->core.flag & BAM_FDUP
|| ctx.aln->core.flag & BAM_FSECONDARY || ctx.aln->core.flag & BAM_FSUPPLEMENTARY) {
continue;
}
SamRead sam_r = SamRead();
int ret = sam_r.init(ctx);
if (!ret) {
hts_log_trace("san_r init error.");
if (sam_r.seq != NULL) {
delete [] sam_r.seq;
}
continue;
}
cur_chr = sam_r.read_chr;
if (pre_chr == NULL) {
pre_chr = sam_r.read_chr;
}
if (strcmp(cur_chr, pre_chr) != 0) {
pre_chr = cur_chr;
// push all the unpaied HT to HT_vec
for (auto _sam : sam_map) {
HT_vec.push_back(_sam.second.HT);
}
if (HT_vec.size() != 0) {
ctx.has_output = true;
// count similar HT
vector<HT_s>::iterator ht_itor;
ofstream out_stream("./" + ctx.cache_rand_id + ".mhap.tmp" + to_string(ctx.cache_cnt));
ctx.cache_cnt += 1;
//sort
sort(HT_vec.begin(), HT_vec.end(), comp_HT_vec);
for (ht_itor = HT_vec.begin(); ht_itor != HT_vec.end(); ht_itor++) {//auto _ht: HT_vec
string ht_id = (*ht_itor).to_str();
map<string, int>::iterator res_map_itor;
res_map_itor = ctx.res_map.find(ht_id);
if (res_map_itor == ctx.res_map.end()) {
ctx.res_map[ht_id] = 1;
ht_itor->ht_count = 1;
} else {
ctx.res_map[ht_id] += 1;
ht_itor->ht_count = ctx.res_map[ht_id];
}
}
for (ht_itor = HT_vec.begin(); ht_itor != HT_vec.end(); ht_itor++) {//auto _ht: HT_vec
string ht_id = (*ht_itor).to_str();
map<string, int>::iterator res_map_itor;
res_map_itor = ctx.res_map.find(ht_id);
if (ht_itor->ht_count == res_map_itor->second) {
//the last ht in duplicated hts
(*ht_itor).get_WC_symbol();
string line = string((*ht_itor).h_chr) + '\t' + to_string((*ht_itor).h_start) + '\t' +
to_string((*ht_itor).h_end) + '\t' + (*ht_itor).hap_met + '\t' +
to_string((*ht_itor).ht_count) + '\t' + (*ht_itor).WC_symbol;
out_stream << line << '\n';
}
//out_stream << ht_id << "\n";
}
out_stream.close();
}
// clear memory
vector<HT_s>().swap(HT_vec);
unordered_map<string, SamRead > empty_sam_map;
sam_map.swap(empty_sam_map);
sam_map.clear();
map<string, int> empty_res_map;
ctx.res_map.swap(empty_res_map);
ctx.res_map.clear();
}
sam_r.read_id = read_id_cnt;
if (read_id_cnt + 1 == 0) {
hts_log_error("read_id_cnt overflow.");
vector<HT_s> null_HT_s;
return null_HT_s;
}
++read_id_cnt;
if ((read_id_cnt % sam_map_cache_size) == 0) {
iter = sam_map.begin();
while(iter != sam_map.end()){
iter_pre = iter;
++iter;
if ((read_id_cnt - iter_pre->second.read_id) > 5000) {
HT_vec.push_back(iter_pre->second.HT);
sam_map.erase(iter_pre);
}
}
}
string qname = string(sam_r.read_name);
get_cpg_no_idx(ctx, sam_r.read_chr, sam_r.read_start, sam_r.read_end);
sam_r.haplo_type();
if (!sam_r.QC) {
hts_log_trace("--> QC check fail, remove the read.");
continue;
}
iter_find = sam_map.find(qname);
if (iter_find == sam_map.end()) {
sam_map[qname] = sam_r;
} else {
SamRead samF = sam_map[qname];
SamRead samR = sam_r;
if (paired_end_check(samF, samR)) {
HT_s ht = paired_end_merge(samF, samR);
HT_vec.push_back(ht);
} else {
HT_vec.push_back(samF.HT);
HT_vec.push_back(samR.HT);
}
sam_map.erase(iter_find);
}
}
} else if (ctx.region_to_parse == MULTI_REGION) {
load_cpg_no_idx(ctx);
regidx_t *idx = regidx_init(ctx.fn_bed,NULL,NULL,0,NULL);
regitr_t *itr = regitr_init(idx);
//load bai index
static string bam_idx_fn = string(ctx.fn_bam) + ".bai";
ctx.idx_bam = sam_index_load(ctx.fp_bam, bam_idx_fn.c_str());
if (ctx.idx_bam == NULL) {
string error_message = "Please run the command to generate the index file (.bai):\nsamtools index " +
string(ctx.fn_bam) + "\nand place the index file with the input file in the same folder.";
hts_log_error("%s", error_message.c_str());
vector<HT_s> null_HT_s;
return null_HT_s;
}
while ( regitr_loop(itr) ) {
int tid = bam_name2id(ctx.hdr_bam, itr->seq);
if (tid == -1 || tid == -2) {
hts_log_trace("tid not found");
continue;
}
hts_itr_t *sam_itr = sam_itr_queryi(ctx.idx_bam, tid, itr->beg+1, itr->end+1);
if (sam_itr == NULL) {
hts_log_debug("%d:%lld-%lld", tid, itr->beg+1, itr->end+1);
continue;
}
while(sam_itr_next(ctx.fp_bam, sam_itr, ctx.aln) >= 0) {
++cnt;
if (cnt % 1000000 == 0) {
cout << cnt << " reads processed." << endl;
}
if (ctx.aln->core.flag & BAM_FQCFAIL || ctx.aln->core.flag & BAM_FUNMAP || ctx.aln->core.flag & BAM_FDUP
|| ctx.aln->core.flag & BAM_FSECONDARY || ctx.aln->core.flag & BAM_FSUPPLEMENTARY) {
continue;
}
SamRead sam_r = SamRead();
int ret = sam_r.init(ctx);
if (!ret) {
hts_log_trace("san_r init error.");
if (sam_r.seq != NULL) {
delete [] sam_r.seq;
}
continue;
}
cur_chr = sam_r.read_chr;
if (pre_chr == NULL) {
pre_chr = sam_r.read_chr;
}
if (strcmp(cur_chr, pre_chr) != 0) {
pre_chr = cur_chr;
// push all the unpaied HT to HT_vec
for (auto _sam : sam_map) {
HT_vec.push_back(_sam.second.HT);
}
if (HT_vec.size() != 0) {
ctx.has_output = true;
// count similar HT
vector<HT_s>::iterator ht_itor;
ofstream out_stream("./" + ctx.cache_rand_id + ".mhap.tmp" + to_string(ctx.cache_cnt));
ctx.cache_cnt += 1;
//sort
sort(HT_vec.begin(), HT_vec.end(), comp_HT_vec);
for (ht_itor = HT_vec.begin(); ht_itor != HT_vec.end(); ht_itor++) {//auto _ht: HT_vec
string ht_id = (*ht_itor).to_str();
map<string, int>::iterator res_map_itor;
res_map_itor = ctx.res_map.find(ht_id);
if (res_map_itor == ctx.res_map.end()) {
ctx.res_map[ht_id] = 1;
ht_itor->ht_count = 1;
} else {
ctx.res_map[ht_id] += 1;
ht_itor->ht_count = ctx.res_map[ht_id];
}
}
for (ht_itor = HT_vec.begin(); ht_itor != HT_vec.end(); ht_itor++) {//auto _ht: HT_vec
string ht_id = (*ht_itor).to_str();
map<string, int>::iterator res_map_itor;
res_map_itor = ctx.res_map.find(ht_id);
if (ht_itor->ht_count == res_map_itor->second) {
//the last ht in duplicated hts
(*ht_itor).get_WC_symbol();
string line = string((*ht_itor).h_chr) + '\t' + to_string((*ht_itor).h_start) + '\t' +
to_string((*ht_itor).h_end) + '\t' + (*ht_itor).hap_met + '\t' +
to_string((*ht_itor).ht_count) + '\t' + (*ht_itor).WC_symbol;
out_stream << line << '\n';
}
//out_stream << ht_id << "\n";
}
out_stream.close();
}
// clear memory
vector<HT_s>().swap(HT_vec);
unordered_map<string, SamRead > empty_sam_map;
sam_map.swap(empty_sam_map);
sam_map.clear();
map<string, int> empty_res_map;
ctx.res_map.swap(empty_res_map);
ctx.res_map.clear();
}
sam_r.read_id = read_id_cnt;
if (read_id_cnt + 1 == 0) {
hts_log_error("read_id_cnt overflow.");
vector<HT_s> null_HT_s;
return null_HT_s;
}
++read_id_cnt;
if ((read_id_cnt % sam_map_cache_size) == 0) {
iter = sam_map.begin();
while(iter != sam_map.end()){
iter_pre = iter;
++iter;
if ((read_id_cnt - iter_pre->second.read_id) > 5000) {
HT_vec.push_back(iter_pre->second.HT);
sam_map.erase(iter_pre);
}
}
}
string qname = string(sam_r.read_name);
get_cpg_no_idx(ctx, sam_r.read_chr, sam_r.read_start, sam_r.read_end);
sam_r.haplo_type();
if (!sam_r.QC) {
continue;
}
iter_find = sam_map.find(qname);
if (iter_find == sam_map.end()) {
sam_map[qname] = sam_r;
} else {
SamRead samF = sam_map[qname];
SamRead samR = sam_r;
if (paired_end_check(samF, samR)) {
HT_s ht = paired_end_merge(samF, samR);
HT_vec.push_back(ht);
} else {
HT_vec.push_back(samF.HT);
HT_vec.push_back(samR.HT);
}
sam_map.erase(iter_find);
}
}
}
regidx_destroy(idx);
idx = NULL;
regitr_destroy(itr);
itr = NULL;
} else {
hts_log_error("region_error");
exit(1);
}
// push all the unpaied HT to HT_vec
for (auto _sam : sam_map) {
HT_vec.push_back(_sam.second.HT);
}
if (HT_vec.size() != 0) {
ctx.has_output = true;
// count similar HT
vector<HT_s>::iterator ht_itor;
ofstream out_stream("./" + ctx.cache_rand_id + ".mhap.tmp" + to_string(ctx.cache_cnt));
ctx.cache_cnt += 1;
//sort
sort(HT_vec.begin(), HT_vec.end(), comp_HT_vec);
for (ht_itor = HT_vec.begin(); ht_itor != HT_vec.end(); ht_itor++) {//auto _ht: HT_vec
string ht_id = (*ht_itor).to_str();
map<string, int>::iterator res_map_itor;
res_map_itor = ctx.res_map.find(ht_id);
if (res_map_itor == ctx.res_map.end()) {
ctx.res_map[ht_id] = 1;
ht_itor->ht_count = 1;
} else {
ctx.res_map[ht_id] += 1;
ht_itor->ht_count = ctx.res_map[ht_id];
}
}
for (ht_itor = HT_vec.begin(); ht_itor != HT_vec.end(); ht_itor++) {//auto _ht: HT_vec
string ht_id = (*ht_itor).to_str();
map<string, int>::iterator res_map_itor;
res_map_itor = ctx.res_map.find(ht_id);
if (ht_itor->ht_count == res_map_itor->second) {
//the last ht in duplicated hts
(*ht_itor).get_WC_symbol();
string line = string((*ht_itor).h_chr) + '\t' + to_string((*ht_itor).h_start) + '\t' +
to_string((*ht_itor).h_end) + '\t' + (*ht_itor).hap_met + '\t' +