-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpmd.cpp
More file actions
1442 lines (1297 loc) · 52 KB
/
pmd.cpp
File metadata and controls
1442 lines (1297 loc) · 52 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
/* Copyright (C) 2018-2025 Andrew D Smith and Benjamin Decato
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*/
[[maybe_unused]] static constexpr auto description = R"(
Identify PMDs in methylomes. Methylation must be provided in the methcounts
file format (chrom, position, strand, context, methylation, reads). See the
methcounts documentation for details. This program assumes only data at CpG
sites and that strands are collapsed so only the positive site appears in the
file, but reads counts are from both strands.
)";
#include "Interval.hpp"
#include "Interval6.hpp"
#include "MSite.hpp"
#include "TwoStateHMM_PMD.hpp"
#include "bsutils.hpp"
#include "counts_header.hpp"
#include "OptionParser.hpp"
#include "smithlab_utils.hpp"
#include <bamxx.hpp>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <new>
#include <numeric>
#include <random>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
// NOLINTBEGIN(*-narrowing-conversions)
struct pmd_summary {
explicit pmd_summary(const std::vector<Interval6> &pmds) :
pmd_count{std::size(pmds)},
pmd_total_size{std::accumulate(
std::cbegin(pmds), std::cend(pmds), 0ul,
[](const std::uint64_t t, const Interval6 &p) { return t + size(p); })} {
pmd_mean_size = static_cast<double>(pmd_total_size) /
std::max(pmd_count, static_cast<std::uint64_t>(1));
}
// pmd_count is the number of identified PMDs.
std::uint64_t pmd_count{};
// total_pmd_size is the sum of the sizes of the identified PMDs
std::uint64_t pmd_total_size{};
// mean_pmd_size is the mean size of the identified PMDs
double pmd_mean_size{};
};
[[nodiscard]] static inline auto
to_string(const pmd_summary &s) -> std::string {
std::ostringstream oss;
oss << "pmd_count: " << s.pmd_count << '\n'
<< "pmd_total_size: " << s.pmd_total_size << '\n'
<< "pmd_mean_size: " << std::fixed << s.pmd_mean_size;
return oss.str();
}
static void
get_adjacent_distances(const std::vector<Interval6> &pmds,
std::vector<std::size_t> &dists) {
for (std::size_t i = 1; i < std::size(pmds); ++i)
if (pmds[i].chrom == pmds[i - 1].chrom)
dists.push_back(pmds[i].start - pmds[i - 1].stop);
}
static bool
precedes(const std::string &chrom, const std::size_t position,
const Interval6 &r) {
return chrom < r.chrom || (chrom == r.chrom && position < r.start);
}
static bool
succeeds(const std::string &chrom, const std::size_t position,
const Interval6 &r) {
return r.chrom < chrom || (chrom == r.chrom && r.stop <= position);
}
static void
merge_nearby_pmd(const std::size_t max_merge_dist,
std::vector<Interval6> &pmds) {
std::size_t j = 0;
for (std::size_t i = 1; i < std::size(pmds); ++i) {
if (pmds[j].chrom == pmds[i].chrom &&
pmds[i].start - pmds[j].stop <= max_merge_dist) {
pmds[j].stop = pmds[i].stop;
const std::string combined_name(pmds[j].name + pmds[i].name);
pmds[j].name = combined_name;
}
else
pmds[++j] = pmds[i];
}
pmds.resize(j);
}
[[nodiscard]] static inline double
lnbeta(const double a, const double b) {
return std::lgamma(a) + std::lgamma(b) - std::lgamma(a + b);
}
[[nodiscard]] static inline double
beta_log_likelihood(const double alpha, const double beta, const double p) {
return (alpha - 1.0) * std::log(p) + (beta - 1.0) * std::log(1.0 - p) -
lnbeta(alpha, beta);
}
[[nodiscard]] static inline double
beta_max_likelihood(const double fg_alpha, const double fg_beta,
const double bg_alpha, const double bg_beta,
const double p_low, const double p_hi) {
return beta_log_likelihood(fg_alpha, fg_beta, p_low) +
beta_log_likelihood(bg_alpha, bg_beta, p_hi);
}
static std::size_t
find_best_bound(const bool IS_RIGHT_BOUNDARY,
const std::map<std::size_t, std::pair<std::size_t, std::size_t>>
&pos_meth_tot,
const std::vector<double> &fg_alpha,
const std::vector<double> &fg_beta,
const std::vector<double> &bg_alpha,
const std::vector<double> &bg_beta) {
std::vector<std::pair<std::size_t, std::size_t>> meth_tot;
std::vector<std::size_t> positions;
for (const auto &it : pos_meth_tot) {
positions.push_back(it.first);
meth_tot.push_back(it.second);
}
std::vector<std::size_t> cumu_left_meth(std::size(meth_tot), 0);
std::vector<std::size_t> cumu_left_tot(std::size(meth_tot), 0);
std::vector<std::size_t> cumu_right_meth(std::size(meth_tot), 0);
std::vector<std::size_t> cumu_right_tot(std::size(meth_tot), 0);
if (std::size(meth_tot) > 0)
for (std::size_t i = 1; i + 1 < std::size(meth_tot); ++i) {
const std::size_t j = std::size(meth_tot) - 1 - i;
cumu_left_meth[i] = cumu_left_meth[i - 1] + meth_tot[i - 1].first;
cumu_left_tot[i] = cumu_left_tot[i - 1] + meth_tot[i - 1].second;
cumu_right_meth[j] = cumu_right_meth[j + 1] + meth_tot[j + 1].first;
cumu_right_tot[j] = cumu_right_tot[j + 1] + meth_tot[j + 1].second;
}
std::size_t best_idx = 0;
double best_score = -std::numeric_limits<double>::max();
if (std::size(meth_tot) > 0)
for (std::size_t i = 1; i + 1 < std::size(meth_tot); ++i) {
std::size_t N_low{};
std::size_t k_low{};
std::size_t N_hi{};
std::size_t k_hi{};
if (!IS_RIGHT_BOUNDARY) {
N_low = cumu_right_tot[i] + meth_tot[i].second;
k_low = cumu_right_meth[i] + meth_tot[i].first;
N_hi = cumu_left_tot[i];
k_hi = cumu_left_meth[i];
}
else {
N_low = cumu_left_tot[i] + meth_tot[i].second;
k_low = cumu_left_meth[i] + meth_tot[i].first;
N_hi = cumu_right_tot[i];
k_hi = cumu_right_meth[i];
}
if (N_hi > 0 && N_low > 0) {
double score = 0;
const double p_hi = static_cast<double>(k_hi) / N_hi;
const double p_low = static_cast<double>(k_low) / N_low;
for (std::size_t j = 0; j < std::size(fg_alpha); ++j) {
score += beta_max_likelihood(fg_alpha[j], fg_beta[j], bg_alpha[j],
bg_beta[j], p_low, p_hi);
} // beta max likelihood using learned emissions
score /= std::size(fg_alpha);
if (p_hi > p_low && score > best_score) {
best_idx = i;
best_score = score;
}
}
}
return (best_score > -std::numeric_limits<double>::max())
? positions[best_idx]
: std::numeric_limits<std::size_t>::max();
}
static void
get_boundary_positions(std::vector<Interval6> &bounds,
const std::vector<Interval6> &pmds,
const std::size_t &bin_size) {
for (std::size_t i = 0; i < std::size(pmds); ++i) {
bounds.push_back(pmds[i]);
bounds.back().start =
pmds[i].start > bin_size ? pmds[i].start - bin_size : 0;
bounds.back().stop = pmds[i].start + bin_size;
bounds.push_back(pmds[i]);
bounds.back().start = pmds[i].stop > bin_size ? pmds[i].stop - bin_size : 0;
bounds.back().stop = pmds[i].stop + bin_size;
}
}
static void
get_optimized_boundary_likelihoods(
const std::vector<std::string> &cpgs_file,
const std::vector<Interval6> &bounds, const std::vector<bool> &array_status,
const std::vector<double> &fg_alpha, const std::vector<double> &fg_beta,
const std::vector<double> &bg_alpha, const std::vector<double> &bg_beta,
std::vector<double> &boundary_scores,
std::vector<std::size_t> &boundary_certainties) {
// For weighting array contribution to boundary observations
static constexpr double array_coverage_constant = 10;
std::vector<bamxx::bgzf_file> in;
for (std::size_t i = 0; i < std::size(cpgs_file); ++i) {
in.emplace_back(cpgs_file[i], "r");
if (get_has_counts_header(cpgs_file[i]))
skip_counts_header(in.back());
}
std::map<std::size_t, std::pair<std::size_t, std::size_t>> pos_meth_tot;
std::size_t n_meth{};
std::size_t n_reads{};
std::size_t bound_idx{};
for (; bound_idx < std::size(bounds); ++bound_idx) { // for each boundary
for (std::size_t i = 0; i < std::size(in); ++i) {
// get totals for all CpGs overlapping that boundary
MSite site;
while (read_site(in[i], site) &&
!succeeds(site.chrom, site.pos, bounds[bound_idx])) {
if (array_status[i])
site.n_reads = array_coverage_constant;
// check if CpG is inside boundary
if (!precedes(site.chrom, site.pos, bounds[bound_idx])) {
if (array_status[i]) {
if (site.meth != -1) {
n_meth = site.n_meth();
n_reads = site.n_reads;
}
else {
n_meth = 0;
n_reads = 0;
}
}
else {
n_meth = site.n_meth();
n_reads = site.n_reads;
}
if (pos_meth_tot.find(site.pos) == std::cend(pos_meth_tot))
pos_meth_tot[site.pos] = std::make_pair(n_meth, n_reads);
else { // add this file's contribution to the site's methylation
pos_meth_tot[site.pos].first += n_meth;
pos_meth_tot[site.pos].second += n_reads;
}
}
}
}
// Get the boundary position
std::size_t boundary_position =
(bounds[bound_idx].start + bounds[bound_idx].stop) / 2;
std::size_t N_low = 0, k_low = 0, N_hi = 0, k_hi = 0;
for (const auto &p : pos_meth_tot) {
if (p.first < boundary_position) {
N_low += p.second.second;
k_low += p.second.first;
}
else {
N_hi += p.second.second;
k_hi += p.second.first;
}
}
double score = 0;
const double p_hi = static_cast<double>(k_hi) / N_hi;
const double p_low = static_cast<double>(k_low) / N_low;
if (bound_idx % 2) { // its a right boundary, p_low should go with fg
for (std::size_t j = 0; j < std::size(fg_alpha); ++j)
score += beta_max_likelihood(fg_alpha[j], fg_beta[j], bg_alpha[j],
bg_beta[j], p_low, p_hi);
}
else { // its a left boundary, p_low should go with bg
for (std::size_t j = 0; j < std::size(fg_alpha); ++j)
score += beta_max_likelihood(bg_alpha[j], bg_beta[j], fg_alpha[j],
fg_beta[j], p_low, p_hi);
}
boundary_certainties.push_back(std::min(N_low, N_hi));
score /= std::size(fg_alpha);
boundary_scores.push_back(exp(score));
pos_meth_tot.clear();
}
}
static void
find_exact_boundaries(const std::vector<std::string> &cpgs_file,
const std::vector<Interval6> &bounds,
const std::vector<bool> &array_status,
const std::vector<double> &fg_alpha,
const std::vector<double> &fg_beta,
const std::vector<double> &bg_alpha,
const std::vector<double> &bg_beta,
std::vector<std::size_t> &bound_site) {
// For weighting array contribution to boundary observations
static constexpr double array_coverage_constant = 10;
std::vector<bamxx::bgzf_file> in;
for (std::size_t i = 0; i < std::size(cpgs_file); ++i) {
in.emplace_back(cpgs_file[i], "r");
if (get_has_counts_header(cpgs_file[i]))
skip_counts_header(in[i]);
}
std::map<std::size_t, std::pair<std::size_t, std::size_t>> pos_meth_tot;
std::size_t n_meth{};
std::size_t n_reads{};
std::size_t bound_idx{};
for (; bound_idx < std::size(bounds); ++bound_idx) { // for each boundary
for (std::size_t i = 0; i < std::size(in); ++i) {
// get totals for all CpGs overlapping that boundary
MSite site;
while (read_site(in[i], site) &&
!succeeds(site.chrom, site.pos, bounds[bound_idx])) {
if (array_status[i])
site.pos = array_coverage_constant;
// check if CpG is inside boundary
if (!precedes(site.chrom, site.pos, bounds[bound_idx])) {
if (array_status[i]) {
if (site.meth != -1) {
n_meth = site.n_meth();
n_reads = site.n_reads;
}
else {
n_meth = 0;
n_reads = 0;
}
}
else {
n_meth = site.n_meth();
n_reads = site.n_reads;
}
auto it = pos_meth_tot.find(site.pos);
if (it == end(pos_meth_tot)) { // does not exist in map
pos_meth_tot.emplace(site.pos, std::make_pair(n_meth, n_reads));
}
else { // add this file's contribution to the CpG's methylation
pos_meth_tot[site.pos].first += site.n_meth();
pos_meth_tot[site.pos].second += site.n_reads;
}
}
}
}
bound_site.push_back(find_best_bound(bound_idx % 2, pos_meth_tot, fg_alpha,
fg_beta, bg_alpha, bg_beta));
pos_meth_tot.clear();
}
}
static void
optimize_boundaries(
const std::size_t bin_size, const std::vector<std::string> &cpgs_file,
std::vector<Interval6> &pmds, const std::vector<bool> &array_status,
const std::vector<double> &fg_alpha, const std::vector<double> &fg_beta,
const std::vector<double> &bg_alpha, const std::vector<double> &bg_beta) {
std::vector<Interval6> bounds;
get_boundary_positions(bounds, pmds, bin_size);
std::vector<std::size_t> bound_site;
find_exact_boundaries(cpgs_file, bounds, array_status, fg_alpha, fg_beta,
bg_alpha, bg_beta, bound_site);
// Now reset the starts and ends of PMDs
for (std::size_t i = 0; i < std::size(pmds); ++i) {
const std::size_t start_site = bound_site[2 * i];
if (start_site != std::numeric_limits<std::size_t>::max())
pmds[i].start = start_site;
const std::size_t end_site = bound_site[2 * i + 1];
if (end_site != std::numeric_limits<std::size_t>::max())
pmds[i].stop = end_site + 1;
}
// Now merge the pmds that are too close
std::vector<std::size_t> dists;
get_adjacent_distances(pmds, dists);
std::sort(std::begin(dists), std::end(dists));
// Need to use some randomization method here to figure out the
// merging distance
std::vector<std::pair<std::size_t, std::size_t>> dist_hist;
std::size_t first = 0;
for (std::size_t i = 1; i < std::size(dists); ++i)
if (dists[i] != dists[i - 1]) {
dist_hist.push_back(std::make_pair(dists[i - 1], i - first));
first = i;
}
merge_nearby_pmd(2 * bin_size, pmds);
// Last, get the cpg sites within 1 bin of each boundary and compute
// the likelihood to get a "score" on the boundary
bounds.clear(); // need updated boundaries after merging nearby PMDs
get_boundary_positions(bounds, pmds, bin_size);
std::vector<double> boundary_scores;
std::vector<std::size_t> boundary_certainties;
get_optimized_boundary_likelihoods(cpgs_file, bounds, array_status, fg_alpha,
fg_beta, bg_alpha, bg_beta,
boundary_scores, boundary_certainties);
// Add the boundary scores to the PMD names
// clang-format off
for (std::size_t i = 0; i < std::size(pmds); ++i)
pmds[i].name = pmds[i].name + ":" +
std::to_string(boundary_scores[2 * i]) + ":" +
std::to_string(boundary_certainties[2 * i]) + ":" +
std::to_string(boundary_scores[2 * i + 1]) + ":" +
std::to_string(boundary_certainties[2 * i + 1]);
// clang-format on
}
double
get_score_cutoff_for_fdr(const std::vector<double> &scores, const double fdr) {
if (fdr <= 0)
return std::numeric_limits<double>::max();
if (fdr > 1)
return std::numeric_limits<double>::min();
std::vector<double> local(scores);
std::sort(std::begin(local), std::end(local));
std::size_t i = 0;
for (; i + 1 < std::size(local) &&
local[i + 1] < fdr * static_cast<double>(i + 1) / std::size(local);
++i)
;
return local[i] + 1.0 / std::size(scores);
}
static inline double
score_contribution(const std::pair<double, double> &m) {
const double denom = m.first + m.second;
return denom > 0 ? 1.0 - m.first / denom : 0.0;
}
static void
get_domain_scores(
const std::vector<bool> &classes,
const std::vector<std::vector<std::pair<double, double>>> &meth,
const std::vector<std::size_t> &reset_points, std::vector<double> &scores) {
const std::size_t n_replicates = std::size(meth);
std::size_t reset_idx = 1;
bool in_domain = false;
double score = 0;
for (std::size_t i = 0; i < std::size(classes); ++i) {
if (reset_points[reset_idx] == i) {
if (in_domain) {
scores.push_back(score);
score = 0;
in_domain = false;
}
++reset_idx;
}
if (classes[i]) {
for (std::size_t r = 0; r < n_replicates; ++r)
score += score_contribution(meth[r][i]);
in_domain = true;
}
else if (in_domain) {
scores.push_back(score);
score = 0;
in_domain = false;
}
}
if (in_domain)
scores.push_back(score);
}
static void
build_domains(const std::vector<Interval> &bins,
const std::vector<std::size_t> &reset_points,
const std::vector<bool> &classes,
std::vector<Interval6> &domains) {
std::size_t n_bins = 0, reset_idx = 1, prev_end = 0;
bool in_domain = false;
for (std::size_t i = 0; i < std::size(classes); ++i) {
if (reset_points[reset_idx] == i) {
if (in_domain) {
domains.back().stop = prev_end;
domains.back().score = n_bins;
n_bins = 0;
in_domain = false;
}
++reset_idx;
}
if (classes[i]) {
if (!in_domain) {
domains.emplace_back(bins[i].chrom, bins[i].start, bins[i].stop,
std::string{}, 0, '+');
in_domain = true;
}
++n_bins;
}
else if (in_domain) {
domains.back().stop = prev_end;
domains.back().score = n_bins;
n_bins = 0;
in_domain = false;
}
prev_end = bins[i].stop;
}
}
// Modified to take multiple replicates
template <class T, class U>
static void
separate_regions(const std::size_t desert_size,
std::vector<std::vector<Interval>> &bins,
std::vector<std::vector<T>> &meth,
std::vector<std::vector<U>> &reads,
std::vector<std::size_t> &reset_points,
std::vector<std::size_t> &dists_btwn_bins) {
const std::size_t n_replicates = std::size(bins);
// eliminate the zero-read cpg sites if no coverage in any rep
std::size_t end_coord_of_prev = 0;
std::size_t j = 0;
for (std::size_t i = 0; i < std::size(bins[0]); ++i) {
bool all_empty = true;
std::size_t rep_idx = 0;
while (all_empty && rep_idx < n_replicates) {
if (reads[rep_idx][i] == 0)
++rep_idx;
else
all_empty = false;
}
if (!all_empty) {
dists_btwn_bins.push_back(bins[0][i].start - end_coord_of_prev);
end_coord_of_prev = bins[0][i].stop;
for (std::size_t r = 0; r < n_replicates; ++r) {
bins[r][j] = bins[r][i];
meth[r][j] = meth[r][i];
reads[r][j] = reads[r][i];
}
++j;
}
}
for (std::size_t r = 0; r < n_replicates; ++r) {
bins[r].resize(j);
meth[r].resize(j);
reads[r].resize(j);
}
// segregate bins
std::size_t prev_cpg = 0;
for (std::size_t i = 0; i < std::size(bins[0]); ++i) {
const std::size_t dist = (i > 0 && bins[0][i].chrom == bins[0][i - 1].chrom)
? bins[0][i].start - prev_cpg
: std::numeric_limits<std::size_t>::max();
if (dist > desert_size)
reset_points.push_back(i);
prev_cpg = bins[0][i].start;
}
assert(std::size(reset_points) > 0);
reset_points.push_back(std::size(bins[0]));
}
static void
shuffle_bins(const std::size_t rng_seed, const TwoStateHMM &hmm,
std::vector<std::vector<std::pair<double, double>>>
meth, // cppcheck-suppress[passedByValue]
const std::vector<std::size_t> &reset_points,
const std::vector<double> &start_trans,
const std::vector<std::vector<double>> &trans,
const std::vector<double> &end_trans,
const std::vector<double> &fg_alpha,
const std::vector<double> &fg_beta,
const std::vector<double> &bg_alpha,
const std::vector<double> &bg_beta,
std::vector<double> &domain_scores,
const std::vector<bool> &array_status) {
const auto n_replicates = std::size(meth);
auto eng = std::default_random_engine(rng_seed);
for (std::size_t r = 0; r < n_replicates; ++r)
std::shuffle(std::begin(meth[r]), std::end(meth[r]), eng);
std::vector<bool> classes;
std::vector<double> scores;
hmm.PosteriorDecoding_rep(meth, reset_points, start_trans, trans, end_trans,
fg_alpha, fg_beta, bg_alpha, bg_beta, classes,
scores, array_status);
get_domain_scores(classes, meth, reset_points, domain_scores);
std::sort(std::begin(domain_scores), std::end(domain_scores));
}
static void
assign_p_values(const std::vector<double> &random_scores,
const std::vector<double> &observed_scores,
std::vector<double> &p_values) {
const double n_randoms = std::max(std::size(random_scores), 1ul);
for (auto scr : observed_scores) {
const auto scr_itr =
upper_bound(std::cbegin(random_scores), std::cend(random_scores), scr);
p_values.push_back(std::distance(scr_itr, std::cend(random_scores)) /
n_randoms);
}
}
static void
read_params_file(const bool verbose, const std::string ¶ms_file,
double &fg_alpha, double &fg_beta, double &bg_alpha,
double &bg_beta, std::vector<double> &start_trans,
std::vector<std::vector<double>> &trans,
std::vector<double> &end_trans, double &fdr_cutoff) {
std::string jnk;
std::ifstream in(params_file.c_str());
in >> jnk >> fg_alpha >> jnk >> fg_beta >> jnk >> bg_alpha >> jnk >>
bg_beta >> jnk >> start_trans[0] >> jnk >> start_trans[1] >> jnk >>
trans[0][0] >> jnk >> trans[0][1] >> jnk >> trans[1][0] >> jnk >>
trans[1][1] >> jnk >> end_trans[0] >> jnk >> end_trans[1] >> jnk >>
fdr_cutoff;
if (verbose)
std::cerr << "Read in params from " << params_file << '\n'
<< "FG_ALPHA\t" << fg_alpha << '\n'
<< "FG_BETA\t" << fg_beta << '\n'
<< "BG_ALPHA\t" << bg_alpha << '\n'
<< "BG_BETA\t" << bg_beta << '\n'
<< "S_F\t" << start_trans[0] << '\n'
<< "S_B\t" << start_trans[1] << '\n'
<< "F_F\t" << trans[0][0] << '\n'
<< "F_B\t" << trans[0][1] << '\n'
<< "B_F\t" << trans[1][0] << '\n'
<< "B_B\t" << trans[1][1] << '\n'
<< "F_E\t" << end_trans[0] << '\n'
<< "B_E\t" << end_trans[1] << '\n'
<< "FDR_CUTOFF\t" << fdr_cutoff << '\n';
}
static void
write_posteriors_file(const std::string &posteriors_file,
const std::vector<std::vector<Interval>> &bins,
const std::vector<double> &scores) {
static constexpr auto decimal_precision = 10;
std::ofstream out(posteriors_file);
if (!out)
throw std::runtime_error("failed to open: " + posteriors_file);
out.precision(decimal_precision);
for (std::size_t r = 0; r < std::size(scores); ++r)
out << bins[0][r] << '\t' << scores[r] << '\n';
}
static void
write_params_file(const std::string &outfile,
const std::vector<double> &fg_alpha,
const std::vector<double> &fg_beta,
const std::vector<double> &bg_alpha,
const std::vector<double> &bg_beta,
const std::vector<double> &start_trans,
const std::vector<std::vector<double>> &trans,
const std::vector<double> &end_trans) {
static constexpr auto decimal_precision = 30;
std::ofstream out(outfile);
if (!out)
throw std::runtime_error("failed to open: " + outfile);
out.precision(decimal_precision);
// NOLINTBEGIN(*-avoid-magic-numbers)
for (std::size_t r = 0; r < std::size(fg_alpha); ++r)
out << "FG_ALPHA_" << r + 1 << "\t" << std::setw(14) << fg_alpha[r] << "\t"
<< "FG_BETA_" << r + 1 << "\t" << std::setw(14) << fg_beta[r] << "\t"
<< "BG_ALPHA_" << r + 1 << "\t" << std::setw(14) << bg_alpha[r] << "\t"
<< "BG_BETA_" << r + 1 << "\t" << std::setw(14) << bg_beta[r] << '\n';
// NOLINTEND(*-avoid-magic-numbers)
out << "S_F\t" << start_trans[0] << '\n'
<< "S_B\t" << start_trans[1] << '\n'
<< "F_F\t" << trans[0][0] << '\n'
<< "F_B\t" << trans[0][1] << '\n'
<< "B_F\t" << trans[1][0] << '\n'
<< "B_B\t" << trans[1][1] << '\n'
<< "F_E\t" << end_trans[0] << '\n'
<< "B_E\t" << end_trans[1] << '\n';
}
static bool
check_if_array_data(const std::string &infile) {
bamxx::bgzf_file in(infile, "r");
if (!in)
throw std::runtime_error("bad file: " + infile);
if (get_has_counts_header(infile))
skip_counts_header(in);
std::string line;
if (!getline(in, line))
throw std::runtime_error("failed to read line from file: " + infile);
std::istringstream iss(line);
std::string chrom, pos, strand, seq, meth, cov;
iss >> chrom >> pos >> strand >> seq >> meth;
return !(iss >> cov);
}
static void
load_array_data(const std::size_t bin_size, const std::string &cpgs_file,
std::vector<Interval> &bins,
std::vector<std::pair<double, double>> &meth,
std::vector<std::size_t> &reads) {
// MAGIC. GS: minimum value for array?
static constexpr double meth_min = 1.0e-2;
bamxx::bgzf_file in(cpgs_file, "r");
if (!in)
throw std::runtime_error("bad sites file: " + cpgs_file);
if (get_has_counts_header(cpgs_file))
skip_counts_header(in);
std::string curr_chrom;
std::size_t prev_pos = 0ul, curr_pos = 0ul;
double array_meth_bin = 0.0;
double num_probes_in_bin = 0.0;
MSite site;
while (read_site(in, site)) {
// TODO(MN): I think that the block below should be placed later in this
// scope. At this location, the methylation level of the first site in a
// new chrom is contributed to the last bin of the previous chrom.
if (site.n_reads > 0) { // its covered by a probe
++num_probes_in_bin;
if (site.meth < meth_min)
array_meth_bin += meth_min;
else if (site.meth > 1.0 - meth_min)
array_meth_bin += (1.0 - meth_min);
else
array_meth_bin += site.meth;
}
if (curr_chrom != site.chrom) {
if (!curr_chrom.empty()) {
if (site.chrom < curr_chrom)
throw std::runtime_error("CpGs not sorted in file \"" + cpgs_file +
"\"");
bins.push_back(Interval(curr_chrom, curr_pos, prev_pos + 1));
meth.push_back(std::make_pair(array_meth_bin, num_probes_in_bin));
if (num_probes_in_bin > 0)
reads.push_back(1);
else
reads.push_back(0);
}
curr_chrom = site.chrom;
curr_pos = site.pos;
array_meth_bin = 0.0;
num_probes_in_bin = 0.0;
}
else if (curr_pos > site.pos) {
throw std::runtime_error("CpGs not sorted in file \"" + cpgs_file + "\"");
}
else if (site.pos > curr_pos + bin_size) {
bins.push_back(Interval(curr_chrom, curr_pos, curr_pos + bin_size));
meth.push_back(std::make_pair(array_meth_bin, num_probes_in_bin));
(num_probes_in_bin > 0) ? reads.push_back(1) : reads.push_back(0);
array_meth_bin = 0.0;
num_probes_in_bin = 0.0;
curr_pos += bin_size;
while (curr_pos + bin_size < site.pos) {
bins.push_back(Interval(curr_chrom, curr_pos, curr_pos + bin_size));
reads.push_back(0);
meth.push_back(std::make_pair(0.0, 0.0));
curr_pos += bin_size;
}
}
}
if (site.meth != -1) { // its covered by a probe
++num_probes_in_bin;
if (site.meth < meth_min)
array_meth_bin += meth_min;
else if (site.meth > 1.0 - meth_min)
array_meth_bin += (1.0 - meth_min);
else
array_meth_bin += site.meth;
}
prev_pos = site.pos;
if (!curr_chrom.empty()) {
bins.push_back(Interval(curr_chrom, curr_pos, prev_pos + 1));
meth.push_back(std::make_pair(array_meth_bin, num_probes_in_bin));
if (num_probes_in_bin > 0)
reads.push_back(1);
else
reads.push_back(0);
}
}
static void
load_wgbs_data(const std::size_t bin_size, const std::string &cpgs_file,
std::vector<Interval> &bins,
std::vector<std::pair<double, double>> &meth,
std::vector<std::size_t> &reads) {
reads.clear(); // for safety
meth.clear();
bins.clear();
// ADS: loading data each iteration should be put outside loop
bamxx::bgzf_file in(cpgs_file, "r");
if (!in)
throw std::runtime_error("bad sites file: " + cpgs_file);
if (get_has_counts_header(cpgs_file))
skip_counts_header(in);
// keep track of the chroms we've seen
std::string curr_chrom;
std::unordered_set<std::string> chroms_seen;
MSite site;
std::size_t prev_pos = 0ul;
std::size_t sites_in_bin = 0ul;
while (read_site(in, site)) {
if (curr_chrom != site.chrom) { // handle change of chrom
if (sites_in_bin > 0)
bins.back().stop = prev_pos;
if (chroms_seen.find(site.chrom) != std::cend(chroms_seen))
throw std::runtime_error("sites not sorted");
chroms_seen.insert(site.chrom);
curr_chrom = site.chrom;
reads.push_back(0);
meth.push_back(std::make_pair(0.0, 0.0));
bins.push_back(Interval(site.chrom, 0, bin_size));
sites_in_bin = 0;
}
prev_pos = site.pos;
if (site.pos < bins.back().start)
throw std::runtime_error("sites not sorted");
while (bins.back().stop < site.pos) {
sites_in_bin = 0;
reads.push_back(0);
meth.push_back(std::make_pair(0.0, 0.0));
bins.push_back(
Interval(site.chrom, bins.back().stop, bins.back().stop + bin_size));
}
reads.back() += site.n_reads;
meth.back().first += site.n_meth();
meth.back().second += site.n_unmeth();
sites_in_bin++;
}
if (sites_in_bin > 0)
bins.back().stop = prev_pos;
}
static void
remove_empty_bins_at_chrom_start(std::vector<Interval> &bins,
std::vector<std::pair<double, double>> &meth,
std::vector<std::size_t> &reads) {
bool chrom_start = true;
std::size_t j = 0;
std::string prev_chrom = "";
for (std::size_t i = 0; i < std::size(bins); ++i) {
if (bins[i].chrom != prev_chrom) {
chrom_start = true;
prev_chrom = bins[i].chrom;
}
if (reads[i] > 0)
chrom_start = false;
if (!chrom_start) {
reads[j] = reads[i];
meth[j] = meth[i];
bins[j] = bins[i];
++j;
}
}
bins.erase(std::begin(bins) + j, std::end(bins));
meth.erase(std::begin(meth) + j, std::end(meth));
reads.erase(std::begin(reads) + j, std::end(reads));
}
static void
load_read_counts(const std::string &cpgs_file, const std::size_t bin_size,
std::vector<std::size_t> &reads) {
reads.clear(); // for safety
// ADS: loading data each iteration should be put outside loop
bamxx::bgzf_file in(cpgs_file, "r");
if (!in)
throw std::runtime_error("bad methcounts file: " + cpgs_file);
if (get_has_counts_header(cpgs_file))
skip_counts_header(in);
// keep track of where we are and what we've seen
std::size_t bin_start = 0ul;
std::string curr_chrom;
std::unordered_set<std::string> chroms_seen;
MSite site;
while (read_site(in, site)) {
if (curr_chrom != site.chrom) { // handle change of chrom
if (chroms_seen.find(site.chrom) != std::cend(chroms_seen))
throw std::runtime_error("sites not sorted");
chroms_seen.insert(site.chrom);
bin_start = 0;
curr_chrom = site.chrom;
reads.push_back(0);
}
if (site.pos < bin_start)
throw std::runtime_error("sites not sorted");
for (; bin_start + bin_size < site.pos; bin_start += bin_size)
reads.push_back(0);
reads.back() += site.n_reads;
}
}
static double
good_bins_frac(const std::vector<std::size_t> &cumulative,
const std::size_t min_bin_size, const std::size_t bin_size,
const std::size_t min_cov_to_pass) {
// make sure the target bin size is a multiple of the minimum so we
// have the resolution to construct the new bins
assert(bin_size % min_bin_size == 0);
// the step size corresponds to the number of minium sized bins that
// would make up a new bin of the target size
const std::size_t step_size = bin_size / min_bin_size;
std::size_t passing_bins = 0, covered_bins = 0;
std::size_t prev_total = 0;
for (std::size_t i = 0; i + step_size < std::size(cumulative);
i += step_size) {
const std::size_t curr_cumulative = cumulative[i + step_size];
const std::size_t bin_count = curr_cumulative - prev_total;
covered_bins += (bin_count > 0);
passing_bins += (bin_count >= min_cov_to_pass);
prev_total = curr_cumulative;
}
// check if there is a leftover bin at the end
if (std::size(cumulative) % step_size != 0) {
const std::size_t bin_count = cumulative.back() - prev_total;
covered_bins += (bin_count > 0);
passing_bins += (bin_count >= min_cov_to_pass);
}
return static_cast<double>(passing_bins) / std::max(1ul, covered_bins);
}
static std::size_t
get_min_reads_for_confidence(const double conf_level) {
// ADS: value of 0.5 below important; this is where the CI is widest
static const double fixed_phat = 0.5;
std::size_t n_reads = 0;
double lower = 0.0, upper = 1.0;
// ADS: should be doubling first, followed by bisection
while (1.0 - conf_level < upper - lower) {
++n_reads;
wilson_ci_for_binomial(1.0 - conf_level, n_reads, fixed_phat, lower, upper);
}
return n_reads;
}
// ADS: this function will return std::numeric_limits<std::size_t>::max() if the
// fraction of "good" bins is zero for all attempted bin sizes.
static std::size_t
binsize_selection(const std::size_t resolution, const std::size_t min_bin_sz,
const std::size_t max_bin_sz, const double conf_level,
const double min_frac_passed, const std::string &cpgs_file) {
const std::size_t min_cov_to_pass = get_min_reads_for_confidence(conf_level);
std::vector<std::size_t> reads;
load_read_counts(cpgs_file, resolution, reads);
std::partial_sum(std::cbegin(reads), std::cend(reads), std::begin(reads));
double frac_passed = 0.0;
std::size_t bin_size = min_bin_sz;
while (bin_size < max_bin_sz && frac_passed < min_frac_passed) {
frac_passed = good_bins_frac(reads, resolution, bin_size, min_cov_to_pass);