2424
2525#include < bamxx.hpp>
2626
27- #include < gsl/gsl_sf_gamma.h>
28-
2927#include < algorithm>
3028#include < cassert>
3129#include < cmath>
4745#include < utility>
4846#include < vector>
4947
50- // NOLINTBEGIN(*-avoid-c-arrays,*-avoid- magic-numbers,*-init-variables,*- narrowing-conversions,*-owning-memory,*-pointer-arithmetic )
48+ // NOLINTBEGIN(*-avoid-magic-numbers,*-narrowing-conversions)
5149
5250using std::cerr;
5351using std::cout;
@@ -71,18 +69,19 @@ struct pmd_summary {
7169 explicit pmd_summary (const vector<GenomicRegion> &pmds) :
7270 pmd_count{std::size (pmds)} {
7371 // NOLINTBEGIN(*-prefer-member-initializer)
74- pmd_total_size = accumulate (cbegin (pmds), cend (pmds), 0ul ,
75- [](const uint64_t t, const GenomicRegion &p) {
76- return t + p.get_width ();
77- });
72+ pmd_total_size =
73+ accumulate (cbegin (pmds), cend (pmds), 0ul ,
74+ [](const std::uint64_t t, const GenomicRegion &p) {
75+ return t + p.get_width ();
76+ });
7877 pmd_mean_size = static_cast <double >(pmd_total_size) /
79- std::max (pmd_count, static_cast <uint64_t >(1 ));
78+ std::max (pmd_count, static_cast <std:: uint64_t >(1 ));
8079 // NOLINTEND(*-prefer-member-initializer)
8180 }
8281 // pmd_count is the number of identified PMDs.
83- uint64_t pmd_count{};
82+ std:: uint64_t pmd_count{};
8483 // total_pmd_size is the sum of the sizes of the identified PMDs
85- uint64_t pmd_total_size{};
84+ std:: uint64_t pmd_total_size{};
8685 // mean_pmd_size is the mean size of the identified PMDs
8786 double pmd_mean_size{};
8887
@@ -134,13 +133,23 @@ merge_nearby_pmd(const size_t max_merge_dist, vector<GenomicRegion> &pmds) {
134133 pmds.resize (j);
135134}
136135
137- inline double
136+ [[nodiscard]] static inline double
137+ lnbeta (const double a, const double b) {
138+ return std::lgamma (a) + std::lgamma (b) - std::lgamma (a + b);
139+ }
140+
141+ [[nodiscard]] static inline double
142+ beta_log_likelihood (const double alpha, const double beta, const double p) {
143+ return (alpha - 1.0 ) * std::log (p) + (beta - 1.0 ) * std::log (1.0 - p) -
144+ lnbeta (alpha, beta);
145+ }
146+
147+ [[nodiscard]] static inline double
138148beta_max_likelihood (const double fg_alpha, const double fg_beta,
139149 const double bg_alpha, const double bg_beta,
140150 const double p_low, const double p_hi) {
141- return (fg_alpha - 1.0 ) * log (p_low) + (fg_beta - 1.0 ) * log (1.0 - p_low) -
142- gsl_sf_lnbeta (fg_alpha, fg_beta) + (bg_alpha - 1.0 ) * log (p_hi) +
143- (bg_beta - 1.0 ) * log (1.0 - p_hi) - gsl_sf_lnbeta (bg_alpha, bg_beta);
151+ return beta_log_likelihood (fg_alpha, fg_beta, p_low) +
152+ beta_log_likelihood (bg_alpha, bg_beta, p_hi);
144153}
145154
146155static size_t
@@ -172,7 +181,10 @@ find_best_bound(const bool IS_RIGHT_BOUNDARY,
172181 double best_score = -num_lim<double >::max ();
173182 if (meth_tot.size () > 0 )
174183 for (size_t i = 1 ; i < meth_tot.size () - 1 ; ++i) {
175- size_t N_low, k_low, N_hi, k_hi;
184+ size_t N_low{};
185+ size_t k_low{};
186+ size_t N_hi{};
187+ size_t k_hi{};
176188 if (!IS_RIGHT_BOUNDARY) {
177189 N_low = cumu_right_tot[i] + meth_tot[i].second ;
178190 k_low = cumu_right_meth[i] + meth_tot[i].first ;
@@ -235,11 +247,11 @@ get_optimized_boundary_likelihoods(
235247 // CONTRIBUTION TO BOUNDARY OBSERVATIONS
236248 static const double array_coverage_constant = 10 ;
237249
238- vector<bgzf_file * > in (cpgs_file. size ()) ;
250+ vector<bgzf_file> in;
239251 for (size_t i = 0 ; i < cpgs_file.size (); ++i) {
240- in[i] = new bgzf_file (cpgs_file[i], " r" );
252+ in. emplace_back (cpgs_file[i], " r" );
241253 if (get_has_counts_header (cpgs_file[i]))
242- skip_counts_header (*in[i]); // cppcheck-suppress shadowVariable
254+ skip_counts_header (in. back ());
243255 }
244256
245257 std::map<size_t , std::pair<size_t , size_t >> pos_meth_tot;
@@ -251,7 +263,7 @@ get_optimized_boundary_likelihoods(
251263 // get totals for all CpGs overlapping that boundary
252264
253265 MSite site;
254- while (read_site (* in[i], site) &&
266+ while (read_site (in[i], site) &&
255267 !succeeds (site.chrom , site.pos , bounds[bound_idx])) {
256268 if (array_status[i])
257269 site.n_reads = array_coverage_constant;
@@ -319,9 +331,6 @@ get_optimized_boundary_likelihoods(
319331 boundary_scores.push_back (exp (score));
320332 pos_meth_tot.clear ();
321333 }
322-
323- for (auto &&fp : in)
324- delete fp;
325334}
326335
327336static void
@@ -334,11 +343,11 @@ find_exact_boundaries(
334343 // CONTRIBUTION TO BOUNDARY OBSERVATIONS
335344 static const double array_coverage_constant = 10 ;
336345
337- vector<bgzf_file * > in (cpgs_file. size ()) ;
338- for (size_t i = 0 ; i < cpgs_file. size (); ++i) {
339- in[i] = new bgzf_file (cpgs_file[i], " r" );
346+ vector<bgzf_file> in;
347+ for (size_t i = 0 ; i < std:: size (cpgs_file ); ++i) {
348+ in. emplace_back (cpgs_file[i], " r" );
340349 if (get_has_counts_header (cpgs_file[i]))
341- skip_counts_header (* in[i]); // cppcheck-suppress shadowVariable
350+ skip_counts_header (in[i]);
342351 }
343352
344353 std::map<size_t , std::pair<size_t , size_t >> pos_meth_tot;
@@ -350,7 +359,7 @@ find_exact_boundaries(
350359 // get totals for all CpGs overlapping that boundary
351360
352361 MSite site;
353- while (read_site (* in[i], site) &&
362+ while (read_site (in[i], site) &&
354363 !succeeds (site.chrom , site.pos , bounds[bound_idx])) {
355364 if (array_status[i])
356365 site.pos = array_coverage_constant;
@@ -386,9 +395,6 @@ find_exact_boundaries(
386395 fg_beta, bg_alpha, bg_beta));
387396 pos_meth_tot.clear ();
388397 }
389-
390- for (size_t i = 0 ; i < std::size (in); ++i)
391- delete in[i];
392398}
393399
394400static void
@@ -1426,4 +1432,4 @@ main_pmd(int argc, char *argv[]) { // NOLINT(*-avoid-c-arrays)
14261432 return EXIT_SUCCESS;
14271433}
14281434
1429- // NOLINTEND(*-avoid-c-arrays,*-avoid- magic-numbers,*-init-variables,*- narrowing-conversions,*-owning-memory,*-pointer-arithmetic )
1435+ // NOLINTEND(*-avoid-magic-numbers,*-narrowing-conversions)
0 commit comments