diff options
Diffstat (limited to 'src/gemma_io.cpp')
| -rw-r--r-- | src/gemma_io.cpp | 68 |
1 files changed, 61 insertions, 7 deletions
diff --git a/src/gemma_io.cpp b/src/gemma_io.cpp index e630f64..11e2746 100644 --- a/src/gemma_io.cpp +++ b/src/gemma_io.cpp @@ -1478,7 +1478,9 @@ void ReadFile_eigenD(const string &file_kd, bool &error, gsl_vector *eval) { // Read bimbam mean genotype file and calculate kinship matrix. -gsl_matrix *mdb_calc_kin(const string file_geno, const int k_mode, const string loco) { +gsl_matrix *mdb_calc_kin(const string file_geno, const int k_mode, const string loco, + size_t &n_markers_used, const double maf_level, + const double miss_level) { checkpoint("mdb-kin",file_geno); bool is_loco = !loco.empty(); @@ -1589,7 +1591,11 @@ gsl_matrix *mdb_calc_kin(const string file_geno, const int k_mode, const string const uint8_t* gs_bbuf = reinterpret_cast<const uint8_t*>(value.data()); gs.reserve(size); for (size_t i = 0; i < size; ++i) { - double g = static_cast<double>(gs_bbuf[i])/127.0; + // 255 is the Gb missing sentinel; read as 255/127.0 it would be 2.0079, + // a real genotype, and every missing call would silently enter the GRM. + double g = (gs_bbuf[i] == 255) + ? std::numeric_limits<double>::quiet_NaN() + : static_cast<double>(gs_bbuf[i])/127.0; gs.push_back(g); } } else { @@ -1603,27 +1609,74 @@ gsl_matrix *mdb_calc_kin(const string file_geno, const int k_mode, const string } } + // Missing genotypes: the mask used to be set to 1 for every individual (so + // the imputation below never fired) and n_miss was never counted, which is + // why a store with missing calls produced a GRM that looked fine and was not + // the one GEMMA computes. Now: NaN is missing, the mean and variance are + // taken over the observed calls, and the missing ones are plugged with the + // mean, exactly as gemma_io.cpp's ReadFile_geno does for K. + // The same marker filters the scan applies (see mdb_calc_gwa): GEMMA's K + // reader skips every marker its genotype reader rejected + // (indicator_snp[i] == 0, gemma_io.cpp:1775), so K and GWA must see one + // marker set. Without this, HLC's K was built from 330514 markers where + // GEMMA uses 273349, and every p-value in the scan moved. + { + size_t n_miss0 = 0, observed0 = 0; + double total0 = 0.0, first0 = 0.0; + bool first_seen = false, poly = false; + for (size_t i = 0; i < ni_total; ++i) { + double g = gs[i]; + if (std::isnan(g)) { + n_miss0++; + continue; + } + if (!first_seen) { + first0 = g; + first_seen = true; + } else if (g != first0) { + poly = true; + } + total0 += g; + observed0++; + } + double maf0 = observed0 ? total0 / (2.0 * (double)observed0) : 0.0; + if ((double)n_miss0 / (double)ni_total > miss_level) + continue; + if ((maf0 < maf_level || maf0 > (1.0 - maf_level)) && maf_level != -1) + continue; + if (!poly) + continue; + } + size_t n_miss = 0; - double geno_mean = 0.0; + double geno_sum = 0.0; double geno_var = 0.0; gsl_vector_set_all(geno_miss, 0); for (size_t i = 0; i < ni_total; ++i) { double d = gs[i]; + if (std::isnan(d)) { + gsl_vector_set(geno, i, 0.0); // placeholder, patched below + gsl_vector_set(geno_miss, i, 1); + n_miss++; + continue; + } gsl_vector_set(geno, i, d); - gsl_vector_set(geno_miss, i, 1); - geno_mean += d; + geno_sum += d; geno_var += d * d; } - geno_mean /= (double)(ni_total - n_miss); + const size_t observed = ni_total - n_miss; + const double geno_mean = observed ? geno_sum / (double)observed : 0.0; + // Variance over all individuals with the missing ones at the mean, as GEMMA + // does: sum(d^2) over observed + n_miss*mean^2, divided by ni_total. geno_var += geno_mean * geno_mean * (double)n_miss; geno_var /= (double)ni_total; geno_var -= geno_mean * geno_mean; // impute missing values by plugging in the mean for (size_t i = 0; i < ni_total; ++i) { - if (gsl_vector_get(geno_miss, i) == 0) { + if (gsl_vector_get(geno_miss, i) == 1) { gsl_vector_set(geno, i, geno_mean); } } @@ -1664,6 +1717,7 @@ gsl_matrix *mdb_calc_kin(const string file_geno, const int k_mode, const string gsl_vector_free(geno_miss); gsl_matrix_free(Xlarge); + n_markers_used = ns_test; return matrix_kin; } |
