-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsummary.cpp
More file actions
executable file
·768 lines (716 loc) · 24 KB
/
Copy pathsummary.cpp
File metadata and controls
executable file
·768 lines (716 loc) · 24 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
//
// Created by Yuhao Dan on 2020/8/2.
//
#include <getopt.h>
#include <fstream>
#include "./include/summary.h"
#include "./include/mhap.h"
#include "./include/utils.h"
#include "./htslib-1.10.2/htslib/regidx.h"
#include "./htslib-1.10.2/htslib/kseq.h"
namespace std {
ContextSummary::~ContextSummary() {
if (!genome_wide) {
if (fp_bed) {
fclose(fp_bed);
}
if (hap_idx) {
mhap_idx_destroy(hap_idx);
}
if (fp_cpg) {
hts_close(fp_cpg);
}
} else {
if (fp_cpg) {
hts_close(fp_cpg);
}
}
}
int summary_opt_check(ContextSummary &ctx_sum) {
if (ctx_sum.fn_hap == NULL) {
hts_log_error("Please specify input file");
return 1;
}
if (ctx_sum.genome_wide) {
if (ctx_sum.fn_cpg == NULL) {
hts_log_error("If -g is specified, -c is also required");
return 1;
}
} else {
if (ctx_sum.fn_bed == NULL && ctx_sum.region == NULL) {
hts_log_error("Please specify -b or -r");
return 1;
}
}
return 0;
}
int get_region(char* region_string, region_t ®_t) {
if (region_string == NULL) {
return 1;
}
char *p, *q;
p = region_string;
q = region_string;
while(*q != '\0' && *q != ':') {
q++;
}
if (*q != '\0') {
*q = '\0';
} else {
return 1;
}
reg_t.chr = string(p);
*q = ':';
p = q + 1;
if (*p != '\0') {
while(*q != '\0' && *q != '-') {
q++;
}
if (*q != '\0') {
*q = '\0';
}
reg_t.beg = atoll(p);
*q = '-';
p = q + 1;
if (*p != '\0') {
reg_t.end = atoll(p);
} else {
return 1;
}
} else {
return 1;
}
return 0;
}
int get_summary_within_region(ContextSummary &ctx_sum, region_t ®_t, summary_t &sum_t) {
//get summary result within reg_t and put to sum_t
ctx_sum.fp_hap_gz = bgzf_open(ctx_sum.fn_hap, "r");
if (ctx_sum.fp_hap_gz == NULL) {
hts_log_error("Fail to open the mhap file.");
return 1;
}
mhap_t hap_line_t = mhap_t {MHAP_NULL_STRING, 0, 0, MHAP_NULL_STRING, 0, MHAP_DEFAULT_DIRECTION};
int hap_tid = mhap_name2id(ctx_sum.hap_idx, reg_t.chr.c_str());
hts_pos_t start = reg_t.beg;
hts_pos_t end = reg_t.end;
if (start <= 500) {
hts_pos_t start = 0;
} else {
start -= 500;
}
end += 500;
hts_itr_t *hap_itr = mhap_itr_queryi(ctx_sum.hap_idx, hap_tid, start, end);
kstring_t ksbuf = {0, 0, NULL};
while(hts_itr_next(ctx_sum.fp_hap_gz, hap_itr, &ksbuf, ctx_sum.hap_idx) >= 0) {
parse_mhap_line(ksbuf.s, ksbuf.l, &hap_line_t);
ctx_sum.region_chr_match = true;
mhap_pos_t cur_m_base = 0;
mhap_pos_t cur_t_base = 0;
if ((hap_line_t.chr_end >= reg_t.beg && hap_line_t.chr_end <= reg_t.end) ||
(hap_line_t.chr_beg >= reg_t.beg && hap_line_t.chr_beg <= reg_t.end) ||
(hap_line_t.chr_beg <= reg_t.beg && hap_line_t.chr_end >= reg_t.end)) {
ctx_sum.region_beg_end_match = true;
if (ctx_sum.stranded) {
if (hap_line_t.mhap_direction == '+') {
sum_t.n_reads += hap_line_t.mhap_count;
sum_t.t_base += hap_line_t.mhap_str.size() * hap_line_t.mhap_count;
cur_t_base = hap_line_t.mhap_str.size();
bool nmr = false;
for (auto b : hap_line_t.mhap_str) {
if (b == '1') {
nmr = true;
sum_t.m_base += hap_line_t.mhap_count;
++cur_m_base;
}
}
if (nmr) { // 先计算碱基的总数量,之后从里面- m_base
sum_t.c_base += hap_line_t.mhap_str.size() * hap_line_t.mhap_count;
}
if (cur_t_base >= 4) {
if (nmr) {
sum_t.n_mr += hap_line_t.mhap_count;
}
sum_t.n_reads_k4 += hap_line_t.mhap_count;
if (cur_t_base != cur_m_base && cur_m_base != 0) {
sum_t.n_dr += hap_line_t.mhap_count;
}
}
} else if (hap_line_t.mhap_direction == '-') {
sum_t.n_reads_r += hap_line_t.mhap_count;
sum_t.t_base_r += hap_line_t.mhap_str.size() * hap_line_t.mhap_count;
cur_t_base = hap_line_t.mhap_str.size();
bool nmr = false;
for (auto b : hap_line_t.mhap_str) {
if (b == '1') {
nmr = true;
sum_t.m_base_r += hap_line_t.mhap_count;
++cur_m_base;
}
}
if (nmr) {
sum_t.c_base_r += hap_line_t.mhap_str.size() * hap_line_t.mhap_count;
}
if (cur_t_base >= 4) {
if (nmr) {
sum_t.n_mr_r += hap_line_t.mhap_count;
}
sum_t.n_reads_k4_r += hap_line_t.mhap_count;
if (cur_t_base != cur_m_base && cur_m_base != 0) {
sum_t.n_dr_r += hap_line_t.mhap_count;
}
}
} else {
hts_log_error("Contain unknown direction info.");
return 1;
}
} else {
sum_t.n_reads += hap_line_t.mhap_count;
sum_t.t_base += hap_line_t.mhap_str.size() * hap_line_t.mhap_count;
cur_t_base = hap_line_t.mhap_str.size();
bool nmr = false;
for (auto b : hap_line_t.mhap_str) {
if (b == '1') {
nmr = true;
sum_t.m_base += hap_line_t.mhap_count;
++cur_m_base;
}
}
if (nmr) {
sum_t.c_base += hap_line_t.mhap_str.size() * hap_line_t.mhap_count;
}
if (cur_t_base >= 4) {
if (nmr) {
sum_t.n_mr += hap_line_t.mhap_count;
}
sum_t.n_reads_k4 += hap_line_t.mhap_count;
if (cur_t_base != cur_m_base && cur_m_base != 0) {
sum_t.n_dr += hap_line_t.mhap_count;
}
}
}
}
}
if (sum_t.c_base != 0) {
sum_t.c_base = sum_t.c_base - sum_t.m_base;
}
if (ctx_sum.stranded) {
if (sum_t.c_base_r != 0) {
sum_t.c_base_r = sum_t.c_base_r - sum_t.m_base_r;
}
}
if (hap_itr) {
hts_itr_destroy(hap_itr);
hap_itr = NULL;
}
if (ctx_sum.fp_hap_gz) {
bgzf_close(ctx_sum.fp_hap_gz);
ctx_sum.fp_hap_gz = NULL;
}
return 0;
}
void get_summary_str(ContextSummary &ctx_sum, region_t ®_t, summary_t &sum_t) {
if (ctx_sum.stranded == true) {
ctx_sum.summary_result.push_back(reg_t.chr + '\t' + to_string(reg_t.beg) +
'\t' + to_string(reg_t.end) + '\t' + '+' + '\t' + to_string(sum_t.n_reads)
+ '\t' + to_string(sum_t.m_base) + '\t' + to_string(sum_t.t_base) + '\t' +
to_string(sum_t.c_base) + '\t' +
to_string(sum_t.n_reads_k4) + '\t' + to_string(sum_t.n_dr) + '\t' + to_string(sum_t.n_mr));
ctx_sum.summary_result.push_back(reg_t.chr + '\t' + to_string(reg_t.beg) +
'\t' + to_string(reg_t.end) + '\t' + '-' + '\t' + to_string(sum_t.n_reads_r)
+ '\t' + to_string(sum_t.m_base_r) + '\t' + to_string(sum_t.t_base_r) + '\t' +
to_string(sum_t.c_base_r) + '\t' +
to_string(sum_t.n_reads_k4_r) + '\t' + to_string(sum_t.n_dr_r) + '\t' + to_string(sum_t.n_mr_r));
} else {
ctx_sum.summary_result.push_back(reg_t.chr + '\t' + to_string(reg_t.beg) +
'\t' + to_string(reg_t.end) + '\t' + '*' + '\t' + to_string(sum_t.n_reads)
+ '\t' + to_string(sum_t.m_base) + '\t' + to_string(sum_t.t_base) + '\t' +
to_string(sum_t.c_base) + '\t' +
to_string(sum_t.n_reads_k4) + '\t' + to_string(sum_t.n_dr) + '\t' + to_string(sum_t.n_mr));
}
}
int get_summary(ContextSummary &ctx_sum) {
int ret = 0;
string filename_hap_idx = string(ctx_sum.fn_hap) + ".tbi";
ctx_sum.hap_idx = mhap_index_load(filename_hap_idx.c_str());
if (ctx_sum.hap_idx == NULL) {
hts_log_error("Use the following command to generate .tbi index file for the intput file.");
string tbx_command = "tabix -b 2 -e 3 -p bed " + string(ctx_sum.fn_hap);
hts_log_error("%s", tbx_command.c_str());
return 1;
}
if (ctx_sum.region != NULL) {
summary_t sum_t = summary_t{0,0,0,0,0,0,0,0,0,0,0,0,0,0};
region_t reg_t = region_t {"", 0,0};
ret = get_region(ctx_sum.region, reg_t);
if (ret == 1) {
return 1;
}
ret = get_summary_within_region(ctx_sum, reg_t, sum_t);
if (ret == 1) {
return 1;
}
get_summary_str(ctx_sum, reg_t, sum_t);
}
if (ctx_sum.fn_bed != NULL) {
regidx_t *idx = regidx_init(ctx_sum.fn_bed,NULL,NULL,0,NULL);
regitr_t *itr = regitr_init(idx);
while (regitr_loop(itr)) {
summary_t sum_t = summary_t{0,0,0,0,0,0,0,0,0,0,0,0,0,0};
region_t reg_t = region_t {"", 0,0};
reg_t.chr = itr->seq;
reg_t.beg = itr->beg + 1;
reg_t.end = itr->end + 1;
ret = get_summary_within_region(ctx_sum, reg_t, sum_t);
if (ret == 1) {
return 1;
}
get_summary_str(ctx_sum, reg_t, sum_t);
}
if (idx) {
regidx_destroy(idx);
idx = NULL;
}
if (itr) {
regitr_destroy(itr);
itr = NULL;
}
}
return 0;
}
void saving_summary(ContextSummary &ctx_sum) {
if (!ctx_sum.region_chr_match || !ctx_sum.region_beg_end_match) {
if (!ctx_sum.region_chr_match) {
hts_log_warning("Warning: Check the region you specified (especially the chr name), no reads in the mhap file match the region.");
}
if (!ctx_sum.region_beg_end_match) {
hts_log_warning("Warning: Check the region you specified, no reads in the mhap file match the region.");
}
}
string out_stream_name;
if (ctx_sum.fn_out != NULL) {
out_stream_name = ctx_sum.fn_out;
} else {
out_stream_name = "summary.txt";
}
ofstream out_stream(out_stream_name);
if (ctx_sum.summary_result.size() > 0) {
out_stream << "Chr" << '\t' << "Start" << '\t' << "End" << '\t' << "Strand" << '\t' << "nReads" << '\t' << "mBase" << '\t' << "tBase" << '\t' << "cBase" << '\t' << "K4plus" << '\t' << "nDR" << '\t' << "nMR" << endl;
}
for (auto s : ctx_sum.summary_result) {
out_stream << s << endl;
}
out_stream.close();
}
inline int parse_cpg_line(char *cpg_line, string *chr, hts_pos_t *beg, hts_pos_t *end) {
if (cpg_line == NULL || chr == NULL || beg == NULL || end == NULL) {
return 1;
}
char *p ,*q;
p = q = cpg_line;
while(*q && *q != '\t') {
q++;
}
*q = '\0';
*chr = string(p);
*q = '\t';
p = q + 1;
q = p;
while(*q && *q != '\t') {
q++;
}
*q = '\0';
*beg = atoll(p);
*q = '\t';
p = q + 1;
*end = atoll(p);
return 0;
}
int load_cpg_init_map(ContextSummary &ctx_sum) {
int ret = 0;
map<string, map<mhap_pos_t, summary_t> >::iterator chr_itor;
map<mhap_pos_t, summary_t>::iterator cpg_itor;
ctx_sum.fp_cpg = hts_open(ctx_sum.fn_cpg, "r");
if (ctx_sum.fp_cpg == NULL) {
hts_log_error("Fail to open CpG file.");
return 1;
}
kstring_t cpg_line = {0,0,NULL};
while (hts_getline(ctx_sum.fp_cpg, KS_SEP_LINE, &cpg_line) > 0) {
string chr;
hts_pos_t beg;
hts_pos_t end;
ret = parse_cpg_line(cpg_line.s, &chr, &beg, &end);
if (ret == 1) {
hts_log_error("Error: parse_cpg_line()");
return 1;
}
//load genome_summary_map
chr_itor = ctx_sum.genome_wide_map.find(chr);
if (chr_itor == ctx_sum.genome_wide_map.end()) {
map<mhap_pos_t, summary_t> summary_t_map;
summary_t sum_t = summary_t{0,0,0,0,0,0,0,0,0,0,0,0,0,0};
summary_t_map[beg] = sum_t;
ctx_sum.genome_wide_map[chr] = summary_t_map;
} else {
cpg_itor = ctx_sum.genome_wide_map[chr].find(beg);
if (cpg_itor == ctx_sum.genome_wide_map[chr].end()) {
summary_t sum_t = summary_t{0,0,0,0,0,0,0,0,0,0,0,0,0,0};
ctx_sum.genome_wide_map[chr][beg] = sum_t;
}
}
}
return 0;
}
int saving_genome_wide(ContextSummary &ctx_sum) {
string out_stream_name;
if (ctx_sum.fn_out != NULL) {
out_stream_name = ctx_sum.fn_out;
} else {
out_stream_name = "summary_genome_wide.txt";
}
ofstream out_stream(out_stream_name);
map<string, map<mhap_pos_t, summary_t> >::iterator chr_itor;
map<mhap_pos_t, summary_t>::iterator cpg_itor;
out_stream << "Chr" << '\t' << "Start" << '\t' << "End" << '\t' << "Strand" << '\t' << "nReads" << '\t' << "mBase" << '\t' << "tBase" << '\t' << "cBase" << '\t' << "K4plus" << '\t' << "nDR" << '\t' << "nMR" <<endl;
for (chr_itor = ctx_sum.genome_wide_map.begin(); chr_itor != ctx_sum.genome_wide_map.end(); chr_itor++) {
for (cpg_itor = chr_itor->second.begin(); cpg_itor != chr_itor->second.end(); cpg_itor++) {
if (!cpg_itor->second.is_empty()) {
if (ctx_sum.stranded) {
if (!cpg_itor->second.is_direction_plus_empty()) {
out_stream << chr_itor->first + '\t' + to_string(cpg_itor->first) +
'\t' + to_string(cpg_itor->first + 1) + '\t' + '+' + '\t' + to_string(cpg_itor->second.n_reads)
+ '\t' + to_string(cpg_itor->second.m_base) + '\t' + to_string(cpg_itor->second.t_base) + '\t' +
to_string(cpg_itor->second.c_base) + '\t' +
to_string(cpg_itor->second.n_reads_k4) + '\t' + to_string(cpg_itor->second.n_dr) + '\t' + to_string(cpg_itor->second.n_mr) << endl;
}
if (!cpg_itor->second.is_direction_minus_empty()) {
out_stream << chr_itor->first + '\t' + to_string(cpg_itor->first) +
'\t' + to_string(cpg_itor->first + 1) + '\t' + '-' + '\t' + to_string(cpg_itor->second.n_reads_r)
+ '\t' + to_string(cpg_itor->second.m_base_r) + '\t' + to_string(cpg_itor->second.t_base_r) + '\t' +
to_string(cpg_itor->second.c_base_r) + '\t' +
to_string(cpg_itor->second.n_reads_k4_r) + '\t' + to_string(cpg_itor->second.n_dr_r) + '\t' + to_string(cpg_itor->second.n_mr_r) << endl;
}
} else {
out_stream << chr_itor->first + '\t' + to_string(cpg_itor->first) +
'\t' + to_string(cpg_itor->first + 1) + '\t' + '*' + '\t' + to_string(cpg_itor->second.n_reads)
+ '\t' + to_string(cpg_itor->second.m_base) + '\t' + to_string(cpg_itor->second.t_base) + '\t' +
to_string(cpg_itor->second.c_base) + '\t' +
to_string(cpg_itor->second.n_reads_k4) + '\t' + to_string(cpg_itor->second.n_dr) + '\t' + to_string(cpg_itor->second.n_mr)<< endl;
}
}
}
}
out_stream.close();
return 0;
}
int process_genome_wide(ContextSummary &ctx_sum) {
ctx_sum.fp_hap_gz = bgzf_open(ctx_sum.fn_hap, "r");
if (ctx_sum.fp_hap_gz == NULL) {
hts_log_error("Fail to open mhap file");
return 1;
}
mhap_t hap_line_t = mhap_t {MHAP_NULL_STRING, 0, 0, MHAP_NULL_STRING, 0, MHAP_DEFAULT_DIRECTION};
kstring_t str = {0, 0, NULL};
while(bgzf_getline(ctx_sum.fp_hap_gz, '\n', &str) >= 0) {
mhap_pos_t cur_m_base = 0;
mhap_pos_t cur_t_base = 0;
map<string, map<mhap_pos_t, summary_t> >::iterator chr_itor;
map<mhap_pos_t, summary_t>::iterator cpg_itor;
parse_mhap_line(str.s, str.l, &hap_line_t);
chr_itor = ctx_sum.genome_wide_map.find(hap_line_t.chr);
if (chr_itor == ctx_sum.genome_wide_map.end()) {
hts_log_error("Can not find chr: %s, CpG file and mhap file are not match.", hap_line_t.chr.c_str());
return 1;
}
cpg_itor = ctx_sum.genome_wide_map[hap_line_t.chr].find(hap_line_t.chr_beg);
if (cpg_itor == ctx_sum.genome_wide_map[hap_line_t.chr].end()) {
hts_log_error("Can not find CpG begin point %lld, CpG file and mhap file are not match.", hap_line_t.chr_beg);
return 1;
}
summary_t cur_sum_t = summary_t{0,0,0,0,0,0,0,0,0,0,0,0,0,0};
if (ctx_sum.stranded) {
if (hap_line_t.mhap_direction == '+') {
cur_sum_t.n_reads += hap_line_t.mhap_count;
cur_sum_t.t_base += hap_line_t.mhap_str.size() * hap_line_t.mhap_count;
cur_t_base = hap_line_t.mhap_str.size();
bool nmr = false;
for (auto b : hap_line_t.mhap_str) {
if (b == '1') {
nmr = true;
cur_sum_t.m_base += hap_line_t.mhap_count;
++cur_m_base;
}
}
if (nmr) {
cur_sum_t.c_base += hap_line_t.mhap_str.size() * hap_line_t.mhap_count;
}
if (cur_t_base >= 4) {
if (nmr) {
cur_sum_t.n_mr += hap_line_t.mhap_count;
}
cur_sum_t.n_reads_k4 += hap_line_t.mhap_count;
if (cur_t_base != cur_m_base && cur_m_base != 0) {
cur_sum_t.n_dr += hap_line_t.mhap_count;
}
}
} else if (hap_line_t.mhap_direction == '-') {
cur_sum_t.n_reads_r += hap_line_t.mhap_count;
cur_sum_t.t_base_r += hap_line_t.mhap_str.size() * hap_line_t.mhap_count;
cur_t_base = hap_line_t.mhap_str.size();
bool nmr = false;
for (auto b : hap_line_t.mhap_str) {
if (b == '1') {
nmr = true;
cur_sum_t.m_base_r += hap_line_t.mhap_count;
++cur_m_base;
}
}
if (nmr) {
cur_sum_t.c_base_r += hap_line_t.mhap_str.size() * hap_line_t.mhap_count;
}
if (cur_t_base >= 4) {
if (nmr) {
cur_sum_t.n_mr_r += hap_line_t.mhap_count;
}
cur_sum_t.n_reads_k4_r += hap_line_t.mhap_count;
if (cur_t_base != cur_m_base && cur_m_base != 0) {
cur_sum_t.n_dr_r += hap_line_t.mhap_count;
}
}
} else {
hts_log_error("Contain unknown direction info.");
return 1;
}
} else {
cur_sum_t.n_reads += hap_line_t.mhap_count;
cur_sum_t.t_base += hap_line_t.mhap_str.size() * hap_line_t.mhap_count;
cur_t_base = hap_line_t.mhap_str.size();
bool nmr = false;
for (auto b : hap_line_t.mhap_str) {
if (b == '1') {
nmr = true;
cur_sum_t.m_base += hap_line_t.mhap_count;
++cur_m_base;
}
}
if (nmr) {
cur_sum_t.c_base += hap_line_t.mhap_str.size() * hap_line_t.mhap_count;
}
if (cur_t_base >= 4) {
if (nmr) {
cur_sum_t.n_mr += hap_line_t.mhap_count;
}
cur_sum_t.n_reads_k4 += hap_line_t.mhap_count;
if (cur_t_base != cur_m_base && cur_m_base != 0) {
cur_sum_t.n_dr += hap_line_t.mhap_count;
}
}
}
if (cur_sum_t.c_base != 0) {
cur_sum_t.c_base = cur_sum_t.c_base - cur_sum_t.m_base;
}
if (ctx_sum.stranded) {
if (cur_sum_t.c_base_r != 0) {
cur_sum_t.c_base_r = cur_sum_t.c_base_r - cur_sum_t.m_base_r;
}
}
for (int i = 0;
cpg_itor != ctx_sum.genome_wide_map[hap_line_t.chr].end() &&
i < hap_line_t.mhap_str.size(); cpg_itor++, i++) {
cpg_itor->second.n_reads += cur_sum_t.n_reads;
cpg_itor->second.m_base += cur_sum_t.m_base;
cpg_itor->second.t_base += cur_sum_t.t_base;
cpg_itor->second.c_base += cur_sum_t.c_base;
cpg_itor->second.n_reads_k4 += cur_sum_t.n_reads_k4;
cpg_itor->second.n_dr += cur_sum_t.n_dr;
cpg_itor->second.n_mr += cur_sum_t.n_mr;
if (ctx_sum.stranded) {
cpg_itor->second.n_reads_r += cur_sum_t.n_reads_r;
cpg_itor->second.m_base_r += cur_sum_t.m_base_r;
cpg_itor->second.t_base_r += cur_sum_t.t_base_r;
cpg_itor->second.c_base_r += cur_sum_t.c_base_r;
cpg_itor->second.n_reads_k4_r += cur_sum_t.n_reads_k4_r;
cpg_itor->second.n_dr_r += cur_sum_t.n_dr_r;
cpg_itor->second.n_mr_r += cur_sum_t.n_mr_r;
}
//sanity check
if (i == hap_line_t.mhap_str.size() - 1) {
if (cpg_itor->first != hap_line_t.chr_end) {
hts_log_error("CpG file and mhap file do not match.");
return 1;
}
}
}
}
bgzf_close(ctx_sum.fp_hap_gz);
ctx_sum.fp_hap_gz = NULL;
return 0;
}
int get_genome_wide(ContextSummary &ctx_sum) {
int ret = 0;
cout << "Loading CpG positions..." << endl;
ret = load_cpg_init_map(ctx_sum);
if (ret == 1) {
return 1;
}
cout << "Processing..." << endl;
ret = process_genome_wide(ctx_sum);
if (ret == 1) {
return 1;
}
return 0;
}
static void help() {
cout << "Usage: mhaptools summary -i <in.mhap.gz> -c <CpG.gz> [-r chr:beg-end | -b bed_file.bed ] | [-g] [-s] [-o name.txt]" << endl;
cout << "Options:" << endl;
cout << " -i str input file, .mhap.gz format" << endl;
cout << " -c str CpG file, gz format" << endl;
cout << " -r str region, e.g. chr1:2000-200000" << endl;
cout << " -b str bed file, one query region per line" << endl;
cout << " -g flag get genome-wide result" << endl;
cout << " -s flag group results by the direction of mHap reads" << endl;
cout << " -o str output filename [genome_wide.txt | summary.txt]" << endl;
cout << "Long options:" << endl;
cout << " -i --input" << endl;
cout << " -c --cpg" << endl;
cout << " -r --region" << endl;
cout << " -b --bed" << endl;
cout << " -g --genome-wide" << endl;
cout << " -s --stranded" << endl;
cout << " -o --output" << endl;
cout << "Examples:" << endl;
cout << "- Get summary within a region:" << endl;
cout << " mhaptools summary -i in.mhap.gz -c CpG.gz -r chr1:2000-200000" << endl << endl;
cout << "- Get summary within several regions:" << endl;
cout << " mhaptools summary -i in.mhap.gz -c CpG.gz -b bed_file.bed" << endl << endl;
cout << "- Get genome-wide summary:" << endl;
cout << " mhaptools summary -i in.mhap.gz -c CpG.gz -g" << endl << endl;
}
int sum_fn_suffix_check(ContextSummary &ctx_sum) {
string gz_suffix = ".gz";
string mhap_suffix = ".mhap";
string mhap_gz_suffix = ".mhap.gz";
string bed_suffix = ".bed";
string txt_suffix = ".txt";
if (ctx_sum.fn_hap) {
if (!is_suffix(ctx_sum.fn_hap, mhap_gz_suffix)) {
hts_log_error("-i opt should be followed by a .mhap.gz file.");
return 1;
}
}
if (ctx_sum.fn_bed) {
if (!is_suffix(ctx_sum.fn_bed, bed_suffix)) {
hts_log_error("-b opt should be followed by a .bed file.");
return 1;
}
}
if (ctx_sum.fn_cpg) {
if (!is_suffix(ctx_sum.fn_cpg, gz_suffix)) {
hts_log_error("-c opt should be followed by a .gz file.");
return 1;
}
}
if (ctx_sum.fn_out) {
if (!is_suffix(ctx_sum.fn_out, txt_suffix)) {
hts_log_error("-o opt should be followed by a .txt file.");
return 1;
}
}
return 0;
}
int main_summary(int argc, char *argv[]) {
if (argc == optind) {
help();
return 0;
}
ContextSummary ctx_sum = ContextSummary();
int long_index;
static const char *opt_string = "i:o:b:sr:gc:h";
static const struct option long_opts[] = {
{ "input", required_argument, NULL, 'i' },
{ "output", optional_argument, NULL, 'o' },
{ "cpg", optional_argument, NULL, 'c' },
{ "bed", optional_argument, NULL, 'b' },
{ "stranded", optional_argument, NULL, 's' },
{ "region", optional_argument, NULL, 'r' },
{ "genome-wide", optional_argument, NULL, 'g' },
{ "help", optional_argument, NULL, 'h' },
{ NULL, no_argument, NULL, 0 }
};
int opt = getopt_long(argc, argv, opt_string, long_opts, &long_index);
while (opt != -1) {
switch (opt) {
case 'i': {
ctx_sum.fn_hap = optarg;
break;
}
case 'o': {
ctx_sum.fn_out = optarg;
break;
}
case 'b': {
ctx_sum.fn_bed = optarg;
break;
}
case 'c': {
ctx_sum.fn_cpg = optarg;
break;
}
case 's': {
ctx_sum.stranded = true;
break;
}
case 'r': {
ctx_sum.region = optarg;
break;
}
case 'g': {
ctx_sum.genome_wide = true;
break;
}
case 'h': {
help();
return 0;
break;
}
default: {
break;
}
}
opt = getopt_long(argc, argv, opt_string, long_opts, &long_index);
}
int ret = 0;
ret = summary_opt_check(ctx_sum);
if (ret == 1){
hts_log_error("opt error");
return 1;
}
if (sum_fn_suffix_check(ctx_sum) == 1) {
hts_log_error("filename suffix error.");
return 1;
}
if (ctx_sum.genome_wide == false) {
cout << "Processing..." << endl;
ret = get_summary(ctx_sum);
if (ret == 1) {
hts_log_error("Get summary error.");
return 0;
}
cout << "Saving..." << endl;
saving_summary(ctx_sum);
} else {
ret = get_genome_wide(ctx_sum);
if (ret == 1) {
hts_log_error("Error: get_genome_wide()");
return 1;
}
cout << "Saving..." << endl;
ret = saving_genome_wide(ctx_sum);
if (ret == 1) {
hts_log_error("Error: saving_genome_wide()");
return 1;
}
}
return 0;
}
}//namespace std