ʻOhana
Population structure, admixture history, and selection using learning methods.
jade.genotype_matrix.hpp
1 /* -------------------------------------------------------------------------
2  Ohana
3  Copyright (c) 2015-2020 Jade Cheng (\___/)
4  Jade Cheng <info@jade-cheng.com> (='.'=)
5  ------------------------------------------------------------------------- */
6 
7 #ifndef JADE_GENOTYPE_MATRIX_HPP__
8 #define JADE_GENOTYPE_MATRIX_HPP__
9 
10 #include "jade.matrix.hpp"
11 
12 namespace jade
13 {
14  template <typename TValue>
15  class basic_discrete_genotype_matrix;
16 
17  template <typename TValue>
18  class basic_likelihood_genotype_matrix;
19 
20  ///
21  /// A template for an abstract class implementing operations for a genotype
22  /// matrix.
23  ///
24  template <typename TValue>
26  {
27  public:
28  /// The value type.
29  typedef TValue value_type;
30 
31  /// The matrix type.
33 
34  /// The discrete genotype matrix type.
36 
37  /// The likelihood genotype matrix type.
39 
40  ///
41  /// Reclaims resources used by the class and derived classes.
42  ///
43  inline virtual ~basic_genotype_matrix()
44  {
45  }
46 
47  #define JADE_CREATE_CASTS_TO_TYPE(TYPE, AS_TYPE, IS_TYPE, TO_TYPE) \
48  inline virtual const TYPE * AS_TYPE() const { \
49  return nullptr; \
50  } \
51  inline virtual TYPE * AS_TYPE() { \
52  return nullptr; \
53  } \
54  inline bool IS_TYPE() const { \
55  return nullptr != AS_TYPE(); \
56  } \
57  inline const TYPE & TO_TYPE() const { \
58  const auto out = AS_TYPE(); \
59  assert(nullptr != out); \
60  return *out; \
61  } \
62  inline TYPE & TO_TYPE() { \
63  const auto out = AS_TYPE(); \
64  assert(nullptr != out); \
65  return *out; \
66  }
67 
68  JADE_CREATE_CASTS_TO_TYPE(dgm_type, as_dgm, is_dgm, to_dgm)
69  JADE_CREATE_CASTS_TO_TYPE(lgm_type, as_lgm, is_lgm, to_lgm)
70 
71  #undef JADE_CREATE_CASTS_TO_TYPE
72 
73  ///
74  /// Computes the derivative vector and hessian matrix for a specified
75  /// marker of the F matrix.
76  ///
77  virtual void compute_derivatives_f(
78  const matrix_type & q, ///< The Q matrix.
79  const matrix_type & fa, ///< The F matrix.
80  const matrix_type & fb, ///< The 1-F matrix.
81  const matrix_type & qfa, ///< The Q*F product.
82  const matrix_type & qfb, ///< The Q*(1-F) product.
83  const size_t j, ///< The marker.
84  matrix_type & d_vec, ///< The derivative vector.
85  matrix_type & h_mat) ///< The hessian matrix.
86  const = 0;
87 
88  ///
89  /// Computes the derivative vector and hessian matrix for a specified
90  /// individual of the Q matrix.
91  ///
92  virtual void compute_derivatives_q(
93  const matrix_type & q, ///< The Q matrix.
94  const matrix_type & fa, ///< The F matrix.
95  const matrix_type & fb, ///< The 1-F matrix.
96  const matrix_type & qfa, ///< The Q*F product.
97  const matrix_type & qfb, ///< The Q*(1-F) product.
98  const size_t i, ///< The individual.
99  matrix_type & d_vec, ///< The derivative vector.
100  matrix_type & h_mat) ///< The hessian matrix.
101  const = 0;
102 
103  ///
104  /// \return The log of the likelihood function.
105  ///
107  const matrix_type & q, ///< The Q matrix.
108  const matrix_type & fa, ///< The F matrix for major alleles.
109  const matrix_type & fb, ///< The F matrix for minor alleles.
110  const matrix_type & qfa, ///< The Q*Fa product.
111  const matrix_type & qfb) ///< The Q*Fb product.
112  const = 0;
113 
114  ///
115  /// \return A new mu matrix.
116  ///
118  const value_type f_epsilon) ///< The F matrix boundary epsilon.
119  const = 0;
120 
121  ///
122  /// \return The height of the matrix.
123  ///
124  virtual size_t get_height() const = 0;
125 
126  ///
127  /// \return The string representation of the size of the matrix.
128  ///
129  virtual std::string get_size_str() const = 0;
130 
131  ///
132  /// \return The width of the matrix.
133  ///
134  virtual size_t get_width() const = 0;
135 
136  ///
137  /// \return The string representation of the matrix.
138  ///
139  virtual std::string str() const = 0;
140 
141  protected:
142  ///
143  /// Initializes a new instance of the class.
144  ///
146  {
147  }
148  };
149 }
150 
151 #endif // JADE_GENOTYPE_MATRIX_HPP__
jade::basic_genotype_matrix::matrix_type
basic_matrix< TValue > matrix_type
The matrix type.
Definition: jade.genotype_matrix.hpp:32
jade::basic_likelihood_genotype_matrix
A template class implementing operations for a likelihood genotype matrix type.
Definition: jade.likelihood_genotype_matrix.hpp:21
jade::basic_genotype_matrix::get_size_str
virtual std::string get_size_str() const =0
jade::basic_genotype_matrix
A template for an abstract class implementing operations for a genotype matrix.
Definition: jade.genotype_matrix.hpp:26
jade::basic_genotype_matrix::basic_genotype_matrix
basic_genotype_matrix()
Initializes a new instance of the class.
Definition: jade.genotype_matrix.hpp:145
jade::basic_genotype_matrix::get_width
virtual size_t get_width() const =0
jade::basic_genotype_matrix::create_mu
virtual matrix_type create_mu(const value_type f_epsilon) const =0
jade::basic_genotype_matrix::dgm_type
basic_discrete_genotype_matrix< value_type > dgm_type
The discrete genotype matrix type.
Definition: jade.genotype_matrix.hpp:35
jade::basic_genotype_matrix::compute_lle
virtual value_type compute_lle(const matrix_type &q, const matrix_type &fa, const matrix_type &fb, const matrix_type &qfa, const matrix_type &qfb) const =0
jade::basic_discrete_genotype_matrix
A template for a class implementing operations for a discrete genotype matrix.
Definition: jade.discrete_genotype_matrix.hpp:22
jade::basic_genotype_matrix::~basic_genotype_matrix
virtual ~basic_genotype_matrix()
Reclaims resources used by the class and derived classes.
Definition: jade.genotype_matrix.hpp:43
jade::basic_genotype_matrix::lgm_type
basic_likelihood_genotype_matrix< value_type > lgm_type
The likelihood genotype matrix type.
Definition: jade.genotype_matrix.hpp:38
jade::basic_genotype_matrix::get_height
virtual size_t get_height() const =0
jade::basic_genotype_matrix::str
virtual std::string str() const =0
jade::basic_genotype_matrix::value_type
TValue value_type
The value type.
Definition: jade.genotype_matrix.hpp:29
jade::basic_genotype_matrix::compute_derivatives_q
virtual void compute_derivatives_q(const matrix_type &q, 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 =0
Computes the derivative vector and hessian matrix for a specified individual of the Q matrix.
jade::basic_matrix
A template class for a matrix.
Definition: jade.matrix.hpp:21
jade::basic_genotype_matrix::compute_derivatives_f
virtual void compute_derivatives_f(const matrix_type &q, const matrix_type &fa, const matrix_type &fb, const matrix_type &qfa, const matrix_type &qfb, const size_t j, matrix_type &d_vec, matrix_type &h_mat) const =0
Computes the derivative vector and hessian matrix for a specified marker of the F matrix.