|
ʻOhana
Population structure, admixture history, and selection using learning methods.
|
7 #ifndef JADE_DISCRETE_GENOTYPE_MATRIX_HPP__
8 #define JADE_DISCRETE_GENOTYPE_MATRIX_HPP__
10 #include "jade.genotype.hpp"
11 #include "jade.verification.hpp"
19 template <
typename TValue>
54 char const *
const path)
64 const std::string & path)
147 auto qfa_ij_ptr = qfa.
get_data() + j;
148 auto qfb_ij_ptr = qfb.
get_data() + j;
149 const auto g_ij_end = g_ij_ptr + _g.
get_length();
150 const auto g_step = J;
151 const auto q_step = K;
152 const auto qf_step = J;
153 while (g_ij_ptr != g_ij_end)
155 auto is_evaluated =
true;
159 case genotype_major_major: g_ij = 0;
break;
160 case genotype_major_minor: g_ij = 1;
break;
161 case genotype_minor_minor: g_ij = 2;
break;
162 default: is_evaluated =
false;
break;
167 const auto qfa_ij = *qfa_ij_ptr;
168 const auto qfb_ij = *qfb_ij_ptr;
169 const auto term1 = g_ij / qfa_ij;
170 const auto term2 = (2 - g_ij) / qfb_ij;
171 const auto term3 = term1 - term2;
172 const auto term4 = term1 / qfa_ij + term2 / qfb_ij;
182 auto q_ik1_ptr = q_i0_ptr;
183 const auto h_end = h_ptr + h_mat.
get_length();
184 while (h_ptr != h_end)
186 const auto q_ik1 = *q_ik1_ptr;
188 *d_ptr += term3 * q_ik1;
194 auto q_ik2_ptr = q_i0_ptr;
195 const auto q_ik2_end = q_ik2_ptr + q.
get_width();
196 while (q_ik2_ptr != q_ik2_end)
198 *h_ptr -= term4 * q_ik1 * *q_ik2_ptr;
210 qfa_ij_ptr += qf_step;
211 qfb_ij_ptr += qf_step;
257 const auto g_ij_end = g_ij_ptr + J;
258 auto qfa_ij_ptr = qfa.
get_data(i, 0);
259 auto qfb_ij_ptr = qfb.
get_data(i, 0);
262 const auto f_step = J;
263 while (g_ij_ptr != g_ij_end)
265 bool is_evaluated =
true;
269 case genotype_major_major: g_ij = 0;
break;
270 case genotype_major_minor: g_ij = 1;
break;
271 case genotype_minor_minor: g_ij = 2;
break;
272 default: is_evaluated =
false;
break;
277 const auto qfa_ij = *qfa_ij_ptr;
278 const auto qfb_ij = *qfb_ij_ptr;
279 const auto term1 = g_ij / qfa_ij;
280 const auto term2 = (2 - g_ij) / qfb_ij;
281 const auto term3 = term1 / qfa_ij;
282 const auto term4 = term2 / qfb_ij;
291 auto fa_k1j_ptr = fa_0j_ptr;
292 auto fb_k1j_ptr = fb_0j_ptr;
295 const auto h_end = h_ptr + h_mat.
get_length();
296 while (h_ptr != h_end)
298 const auto fa_k1j = *fa_k1j_ptr;
299 const auto fb_k1j = *fb_k1j_ptr;
301 *d_ptr += term1 * fa_k1j + term2 * fb_k1j;
308 auto fa_k2j_ptr = fa_0j_ptr;
309 auto fb_k2j_ptr = fb_0j_ptr;
310 const auto fa_k2j_end = fa_k2j_ptr + fa.
get_length();
311 while (fa_k2j_ptr != fa_k2j_end)
313 const auto fa_k2j = *fa_k2j_ptr;
314 const auto fb_k2j = *fb_k2j_ptr;
316 *h_ptr -= (term3 * fa_k1j * fa_k2j)
317 + (term4 * fb_k1j * fb_k2j);
319 fa_k2j_ptr += f_step;
320 fb_k2j_ptr += f_step;
324 fa_k1j_ptr += f_step;
325 fb_k1j_ptr += f_step;
354 const auto J = g.get_width();
356 auto g_ij_ptr = g.get_data();
357 const auto g_ij_end = g.get_data() + g.get_length();
361 while (g_ij_ptr != g_ij_end)
368 while (fa_0j_ptr != fa_0j_end)
372 case genotype_major_major:
374 auto q_ik_ptr = q_i0_ptr;
375 const auto q_ik_end = q_i0_ptr + K;
376 auto fb_kj_ptr = fb_0j_ptr;
379 while (q_ik_ptr != q_ik_end)
381 const auto q_ik = *q_ik_ptr;
382 const auto fb_kj = *fb_kj_ptr;
384 sum_rhs += q_ik * fb_kj;
394 case genotype_major_minor:
396 auto q_ik_ptr = q_i0_ptr;
397 const auto q_ik_end = q_i0_ptr + K;
398 auto fa_kj_ptr = fa_0j_ptr;
399 auto fb_kj_ptr = fb_0j_ptr;
403 while (q_ik_ptr != q_ik_end)
405 const auto q_ik = *q_ik_ptr;
406 const auto fa_kj = *fa_kj_ptr;
407 const auto fb_kj = *fb_kj_ptr;
409 sum_lhs += q_ik * fa_kj;
410 sum_rhs += q_ik * fb_kj;
417 sum_j += std::log(sum_lhs * sum_rhs);
421 case genotype_minor_minor:
423 auto q_ik_ptr = q_i0_ptr;
424 const auto q_ik_end = q_i0_ptr + K;
425 auto fa_kj_ptr = fa_0j_ptr;
428 while (q_ik_ptr != q_ik_end)
430 const auto q_ik = *q_ik_ptr;
431 const auto fa_kj = *fa_kj_ptr;
433 sum_lhs += q_ik * fa_kj;
463 const auto f_min =
value_type(0.0) + f_epsilon;
464 const auto f_max =
value_type(1.0) - f_epsilon;
473 for (
size_t j = 0; j < J; j++)
477 for (
size_t i = 0; i < I; i++)
481 case jade::genotype_major_major:
485 case jade::genotype_major_minor:
494 mu[j] = std::min(std::max(
538 inline virtual std::string
str()
const override
548 #endif // JADE_DISCRETE_GENOTYPE_MATRIX_HPP__
basic_discrete_genotype_matrix(const initializer_list_type &values)
Initializes a new instance of the class based on the specified values.
A template for a class that performs validation on various types of matrices.
const value_type * get_data() const
size_t get_length() const
genotype_matrix_type::initializer_list_type initializer_list_type
The initializer list for the genotype matrix.
basic_discrete_genotype_matrix * as_dgm() override
basic_matrix< value_type > matrix_type
The matrix type.
A template for an abstract class implementing operations for a genotype matrix.
basic_discrete_genotype_matrix(std::istream &in)
Initializes a new instance of the class based on values from the specified input stream.
virtual size_t get_height() const override
std::string get_size_str() const
basic_discrete_genotype_matrix(char const *const path)
Initializes a new instance of the class based on values from the specified file.
bool is_size(const basic_matrix &other) const
virtual std::string get_size_str() const override
virtual std::string str() const override
basic_discrete_genotype_matrix()
Initializes a new instance of the class.
basic_matrix< genotype > genotype_matrix_type
The genotype matrix type.
size_t get_height() const
virtual size_t get_width() const override
const genotype_matrix_type & get_matrix() const
basic_verification< value_type > verification_type
The verification type.
static bool validate_gqf_sizes(const genotype_matrix_type &g, const matrix_type &q, const matrix_type &f)
Validates the sizes of the G, Q, and F matrices and throws an exception if validation fails.
virtual void compute_derivatives_q(const matrix_type &, const matrix_type &fa, const matrix_type &fb, const matrix_type &qfa, const matrix_type &qfb, const size_t i, matrix_type &d_vec, matrix_type &h_mat) const override
Computes the derivative vector and hessian matrix for a specified individual of the Q matrix.
A template for a class implementing operations for a discrete genotype matrix.
std::initializer_list< std::initializer_list< value_type > > initializer_list_type
The initializer list type.
const basic_discrete_genotype_matrix * as_dgm() const override
basic_discrete_genotype_matrix(const std::string &path)
Initializes a new instance of the class based on values from the specified file.
virtual matrix_type create_mu(const value_type f_epsilon) const override
virtual void compute_derivatives_f(const matrix_type &q, const matrix_type &, const matrix_type &, const matrix_type &qfa, const matrix_type &qfb, const size_t j, matrix_type &d_vec, matrix_type &h_mat) const override
Computes the derivative vector and hessian matrix for a specified marker of the F matrix.
void set_values(const value_type value)
Sets all values of the matrix to the specified value.
virtual value_type compute_lle(const matrix_type &q, const matrix_type &fa, const matrix_type &fb, const matrix_type &, const matrix_type &) const override
TValue value_type
The value type.