about summary refs log tree commit diff
path: root/src/mathfunc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mathfunc.cpp')
-rw-r--r--src/mathfunc.cpp23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/mathfunc.cpp b/src/mathfunc.cpp
index c82872e..ad29982 100644
--- a/src/mathfunc.cpp
+++ b/src/mathfunc.cpp
@@ -667,14 +667,27 @@ std::tuple<size_t, size_t, size_t> compute_ratio(size_t ni_total, const gsl_vect
 }
 
 double compute_maf(size_t ni_total, size_t ni_test, size_t n_miss, const double *gs, const vector<int> &indicator) {
-  double maf = 0.0;
-
+  // Missing genotypes arrive here as NaN (Gf stores) or as the 255 sentinel that
+  // the Gb decoder turns into NaN, so they must be left out of the sum; the
+  // denominator is the number of *observed* genotypes, which is what GEMMA uses:
+  //   maf = sum(geno) / (2 * (ni_test - n_miss))
+  // It used to add NaNs into the sum, so af came out NaN for any marker with a
+  // missing call, and it trusted the caller's n_miss, which the LMDB path passed
+  // as 0 (see mdb_calc_gwa).
+  double total = 0.0;
+  size_t observed = 0;
   for (size_t i = 0; i < ni_total; ++i) { // read genotypes
     if (indicator[i]) {
       double geno = gs[i];
-      maf += geno; // 0..2 range
+      if (!std::isfinite(geno))
+        continue;
+      total += geno; // 0..2 range
+      observed++;
     }
   }
-  maf /= 2.0 * (double)(ni_test - n_miss); // Assumption is that geno value is between 0 and 2. FIXME
-  return maf;
+  (void)ni_test;
+  (void)n_miss; // kept for the signature: `observed' is the same number, counted here
+  if (observed == 0)
+    return 0.0;
+  return total / (2.0 * (double)observed);
 }