Skip to content

Commit bf19669

Browse files
committed
possible fix
1 parent 16b318c commit bf19669

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/utilities/snp_hwe.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
#include <cassert>
2+
#include <cmath>
3+
#include <cstdint>
4+
#include <limits>
5+
16
#include <graphtyper/utilities/logging.hpp>
27
#include <graphtyper/utilities/snp_hwe.hpp>
38

@@ -28,6 +33,9 @@ double p_hwe_excess_het(int obs_hets, int obs_hom1, int obs_hom2)
2833
exit(EXIT_FAILURE);
2934
}
3035

36+
if (obs_hets == 0 && (obs_hom1 == 0 || obs_hom2 == 0))
37+
return 1.0;
38+
3139
int obs_homc = obs_hom1 < obs_hom2 ? obs_hom2 : obs_hom1;
3240
int obs_homr = obs_hom1 < obs_hom2 ? obs_hom1 : obs_hom2;
3341

@@ -37,7 +45,11 @@ double p_hwe_excess_het(int obs_hets, int obs_hom1, int obs_hom2)
3745
std::vector<double> het_probs(rare_copies + 1, 0.0);
3846

3947
// start at midpoint
40-
int mid = rare_copies * (2 * genotypes - rare_copies) / (2 * genotypes);
48+
double mid_double = static_cast<double>(rare_copies) * static_cast<double>(2 * genotypes - rare_copies) /
49+
static_cast<double>(2 * genotypes);
50+
51+
assert(mid_double < static_cast<double>(std::numeric_limits<int>::max()));
52+
int mid = std::lround(mid_double);
4153

4254
// check to ensure that midpoint and rare alleles have same parity
4355
if ((rare_copies & 1) ^ (mid & 1))
@@ -47,11 +59,15 @@ double p_hwe_excess_het(int obs_hets, int obs_hom1, int obs_hom2)
4759
int curr_homr = (rare_copies - mid) / 2;
4860
int curr_homc = genotypes - curr_hets - curr_homr;
4961

62+
assert(mid < static_cast<int>(het_probs.size()));
5063
het_probs[mid] = 1.0;
5164
double sum = het_probs[mid];
5265

5366
for (/*curr_hets = mid*/; curr_hets > 1; curr_hets -= 2)
5467
{
68+
assert(curr_hets < static_cast<int>(het_probs.size()));
69+
assert(curr_hets > 1);
70+
5571
het_probs[curr_hets - 2] =
5672
het_probs[curr_hets] * curr_hets * (curr_hets - 1.0) / (4.0 * (curr_homr + 1.0) * (curr_homc + 1.0));
5773

@@ -68,8 +84,12 @@ double p_hwe_excess_het(int obs_hets, int obs_hom1, int obs_hom2)
6884

6985
for (curr_hets = mid; curr_hets <= rare_copies - 2; curr_hets += 2)
7086
{
87+
assert(curr_hets < static_cast<int>(het_probs.size()));
88+
assert((curr_hets + 2) < static_cast<int>(het_probs.size()));
89+
7190
het_probs[curr_hets + 2] =
7291
het_probs[curr_hets] * 4.0 * curr_homr * curr_homc / ((curr_hets + 2.0) * (curr_hets + 1.0));
92+
7393
sum += het_probs[curr_hets + 2];
7494

7595
// add 2 heterozygotes for next iteration -> subtract one rare, one common homozygote
@@ -81,9 +101,10 @@ double p_hwe_excess_het(int obs_hets, int obs_hom1, int obs_hom2)
81101
het_probs[i] /= sum;
82102

83103
// alternate p-value calculation for p_hi
84-
double p_hi = het_probs[obs_hets];
104+
assert(obs_hets < static_cast<int>(het_probs.size()));
105+
double p_hi = 0.0;
85106

86-
for (int i = obs_hets + 1; i <= rare_copies; i++)
107+
for (int i = obs_hets; i <= rare_copies; i++)
87108
p_hi += het_probs[i];
88109

89110
return p_hi > 1.0 ? 1.0 : p_hi;

0 commit comments

Comments
 (0)