ʻOhana
Population structure, admixture history, and selection using learning methods.
nemeco/jade.settings.hpp
1 /* -------------------------------------------------------------------------
2  Ohana
3  Copyright (c) 2015-2020 Jade Cheng (\___/)
4  Jade Cheng <info@jade-cheng.com> (='.'=)
5  ------------------------------------------------------------------------- */
6 
7 #ifndef JADE_SETTINGS_HPP__
8 #define JADE_SETTINGS_HPP__
9 
10 #include "jade.agi_reader.hpp"
11 #include "jade.genotype_matrix_factory.hpp"
12 #include "jade.options.hpp"
13 
14 namespace jade
15 {
16  ///
17  /// A template for a class that prepares settings for the algorithm.
18  ///
19  template <typename TValue>
20  class basic_settings
21  {
22  public:
23  /// The value type.
24  typedef TValue value_type;
25 
26  /// The admixture graph input reader.
28 
29  /// A pointer to an admixture graph input reader.
30  typedef std::unique_ptr<agi_reader_type> agi_ptr_type;
31 
32  /// The genotype matrix factory type.
35 
36  /// The genotype matrix type.
38 
39  /// The matrix type.
41 
42  /// The options type.
44 
45  /// The verification type.
47 
48  ///
49  /// Initializes a new instance of the class based on the specified
50  /// command-line arguments.
51  ///
52  explicit basic_settings(
53  args & a) ///< The command-line arguments.
54  : _opts (a)
55  , _agi ()
56  , _f ()
57  , _rf ()
58  , _c ()
59  , _mu ()
60  {
61  if (_opts.is_ain_specified())
62  _agi.reset(new agi_reader_type(_opts.get_ain()));
63 
64  const std::unique_ptr<genotype_matrix_type> g_ptr (
65  genotype_matrix_factory_type::create(a.pop<std::string>()));
66  const auto & g = *g_ptr;
68 
69  _f.read(a.pop<std::string>());
71 
72  a.validate_empty();
73 
74  _rf = create_rf(_f);
75  _mu = g.create_mu(_opts.get_f_epsilon());
76 
77  //
78  // If the user specifies the C matrix, read it from the file; if
79  // the user specifies a tree or the AGI file, do not initialize its
80  // values because they will be initialized by the optimizer based
81  // on the tree or AGI definitions. If the C matrix, the tree, and
82  // the AGI file are unspecified, then create it as the sample
83  // covariance matrix of the rooted F matrix.
84  //
85  if (_opts.is_cin_specified())
86  {
87  _c.read(_opts.get_cin());
90  }
91  else if (_opts.is_tin_specified())
92  {
93  const auto rk = _rf.get_height();
94  _c.resize(rk, rk);
95  }
96  else if (_opts.is_ain_specified())
97  {
98  const auto agi_k = _agi->get_k();
99  const auto f_k = _f.get_height();
100 
101  if (agi_k != f_k)
102  throw jade::error()
103  << "inconsistent component sizes " << agi_k << " and "
104  << f_k << " in AGI file and F matrix.";
105 
106  const auto rk = _rf.get_height();
107  _c.resize(rk, rk);
108  }
109  else
110  {
111  _c = create_c(_rf);
112  }
113 
115  }
116 
117  ///
118  /// \return The admixture graph input reader.
119  ///
120  inline const agi_reader_type & get_agi() const
121  {
122  assert(nullptr != _agi.get());
123  return *_agi;
124  }
125 
126  ///
127  /// \return The C matrix.
128  ///
129  inline const matrix_type & get_c() const
130  {
131  return _c;
132  }
133 
134  ///
135  /// \return The mu vector.
136  ///
137  inline const matrix_type & get_mu() const
138  {
139  return _mu;
140  }
141 
142  ///
143  /// \return The options.
144  ///
145  inline const options_type & get_options() const
146  {
147  return _opts;
148  }
149 
150  ///
151  /// \return The rooted F matrix.
152  ///
153  inline const matrix_type & get_rf() const
154  {
155  return _rf;
156  }
157 
158  ///
159  /// Creates the covariance matrix.
160  /// \return The covariance matrix.
161  ///
163  const matrix_type & rf) ///< The [RK x 1] rooted F matrix.
164  {
165  const auto RK = rf.get_height();
166  const auto J = rf.get_width();
167 
168  assert(J > 1);
169 
170  static const auto n1 = value_type(1.0);
171  static const auto n0_25 = value_type(0.25);
172  const auto nj = static_cast<value_type>(J);
173  const auto nj1 = static_cast<value_type>(J - 1);
174  const auto s = n1 / nj1 / n0_25;
175 
176  //
177  // Create a vector of average row values for the RF matrix.
178  //
179  matrix_type rf_avg (RK, 1);
180  for (size_t rk = 0; rk < RK; rk++)
181  rf_avg[rk] = rf.get_row_sum(rk) / nj;
182 
183  //
184  // The covariance matrix is [K-1 x K-1].
185  //
186  matrix_type c (RK, RK);
187 
188  //
189  // Calculate the lower triangle of sample covariance of the RF
190  // matrix and divide it by the mux.
191  //
192  for (size_t j = 0; j < J; j++)
193  {
194  for (size_t row = 0; row < RK; row++)
195  {
196  const auto s_row = s * (rf(row, j) - rf_avg[row]);
197  for (size_t col = 0; col <= row; col++)
198  c(row, col) += s_row * (rf(col, j) - rf_avg[col]);
199  }
200  }
201 
202  //
203  // Copy the lower triangle to the upper triangle and return the
204  // covariance matrix.
205  //
207  return c;
208  }
209 
210  ///
211  /// Creates the rooted F matrix.
212  /// \return The rooted F matrix.
213  ///
215  const matrix_type & f) ///< The F matrix for major alleles.
216  {
217  const auto K = f.get_height();
218  const auto J = f.get_width();
219 
220  assert(K > 1);
221 
222  const auto RK = K - 1;
223 
224  matrix_type rf (RK, J);
225 
226  for (size_t rk = 0; rk < RK; rk++)
227  for (size_t j = 0; j < J; j++)
228  rf(rk, j) = f(rk + 1, j) - f(0, j);
229 
230  return rf;
231  }
232 
233  private:
234  options_type _opts;
235  agi_ptr_type _agi;
236  matrix_type _f;
237  matrix_type _rf;
238  matrix_type _c;
239  matrix_type _mu;
240  };
241 }
242 
243 #endif // JADE_SETTINGS_HPP__
jade::basic_options< value_type >
jade::basic_verification
A template for a class that performs validation on various types of matrices.
Definition: jade.verification.hpp:20
jade::basic_args::validate_empty
void validate_empty() const
Throws an exception if there are more arguments to be processed.
Definition: jade.args.hpp:161
jade::basic_verification::validate_c
static bool validate_c(const matrix_type &c)
Validates the C matrix and throws an exception if validation fails.
Definition: jade.verification.hpp:38
jade::basic_genotype_matrix_factory::create
static genotype_matrix_type * create(const std::string &path)
Creates a genotype matrix based on values from a file. This function determines what kind of genotype...
Definition: jade.genotype_matrix_factory.hpp:48
jade::basic_settings::get_options
const options_type & get_options() const
Definition: nemeco/jade.settings.hpp:145
jade::basic_settings::g_ptr
std::unique_ptr< genotype_matrix_type > g_ptr
The genotype matrix pointer type.
Definition: cpax/jade.settings.hpp:52
jade::basic_verification::validate_gf_sizes
static bool validate_gf_sizes(const genotype_matrix_type &g, const matrix_type &f)
Validates the sizes of the G and F matrices and throws an exception if validation fails.
Definition: jade.verification.hpp:177
jade::basic_matrix::resize
void resize(const size_t height, const size_t width)
Resizes the matrix to the specified dimensions. The values of the matrix are not reset.
Definition: jade.matrix.hpp:1135
jade::basic_matrix::get_width
size_t get_width() const
Definition: jade.matrix.hpp:757
jade::basic_settings::create_c
static matrix_type create_c(const matrix_type &rf)
Creates the covariance matrix.
Definition: nemeco/jade.settings.hpp:162
jade::basic_settings::create_rf
static matrix_type create_rf(const matrix_type &f)
Creates the rooted F matrix.
Definition: nemeco/jade.settings.hpp:214
jade::basic_genotype_matrix
A template for an abstract class implementing operations for a genotype matrix.
Definition: jade.genotype_matrix.hpp:26
jade::basic_settings::value_type
TValue value_type
The value type.
Definition: nemeco/jade.settings.hpp:24
jade::basic_agi_reader
A template for a class that reads an AGI (Admixture Graph Input) file. The file provides the followin...
Definition: jade.agi_reader.hpp:43
jade::basic_settings::get_mu
const matrix_type & get_mu() const
Definition: nemeco/jade.settings.hpp:137
jade::basic_settings::verification_type
basic_verification< value_type > verification_type
The verification type.
Definition: nemeco/jade.settings.hpp:46
jade::basic_settings::options_type
basic_options< value_type > options_type
The options type.
Definition: nemeco/jade.settings.hpp:43
jade::basic_verification::validate_fc_sizes
static bool validate_fc_sizes(const matrix_type &f, const matrix_type &c)
Validates the sizes of the F and C matrices and throws an exception if validation fails.
Definition: jade.verification.hpp:114
jade::basic_verification::validate_f
static bool validate_f(const matrix_type &f)
Validates the F matrix and throws an exception if validation fails.
Definition: jade.verification.hpp:73
jade::basic_matrix::read
void read(const std::string &path)
Reads the matrix values from the specified file.
Definition: jade.matrix.hpp:1059
jade::basic_options::is_tin_specified
bool is_tin_specified() const
Definition: nemeco/jade.options.hpp:214
jade::basic_options::is_ain_specified
bool is_ain_specified() const
Definition: nemeco/jade.options.hpp:166
jade::basic_args::pop
TValue pop()
Definition: jade.args.hpp:96
jade::basic_matrix::get_height
size_t get_height() const
Definition: jade.matrix.hpp:603
jade::basic_settings::agi_reader_type
basic_agi_reader< value_type > agi_reader_type
The admixture graph input reader.
Definition: nemeco/jade.settings.hpp:27
jade::basic_settings::get_agi
const agi_reader_type & get_agi() const
Definition: nemeco/jade.settings.hpp:120
jade::basic_options::get_ain
const std::string & get_ain() const
Definition: nemeco/jade.options.hpp:86
jade::basic_args
A template for a class that helps process command-line arguments.
Definition: jade.args.hpp:19
jade::basic_settings::basic_settings
basic_settings(args &a)
Initializes a new instance of the class based on the specified command-line arguments.
Definition: nemeco/jade.settings.hpp:52
jade::basic_genotype_matrix_factory
A template for a class that creates genotype matrices based on files and their file extensions.
Definition: jade.genotype_matrix_factory.hpp:21
jade::basic_options::is_cin_specified
bool is_cin_specified() const
Definition: nemeco/jade.options.hpp:174
jade::basic_settings::genotype_matrix_factory_type
basic_genotype_matrix_factory< value_type > genotype_matrix_factory_type
The genotype matrix factory type.
Definition: nemeco/jade.settings.hpp:34
jade::basic_settings::get_rf
const matrix_type & get_rf() const
Definition: nemeco/jade.settings.hpp:153
jade::basic_options::get_cin
const std::string & get_cin() const
Definition: nemeco/jade.options.hpp:95
jade::basic_matrix::get_row_sum
value_type get_row_sum(const size_t row) const
Definition: jade.matrix.hpp:716
jade::basic_verification::validate_g
static bool validate_g(const genotype_matrix_type &g)
Validates the G matrix and throws an exception if validation fails.
Definition: jade.verification.hpp:158
jade::basic_matrix::copy_lower_to_upper
void copy_lower_to_upper()
Copies the elements in the lower triangle of the square matrix to the corresponding upper triangle el...
Definition: jade.matrix.hpp:266
jade::basic_error
A template for a class representing an exception thrown from this namespace.
Definition: jade.error.hpp:20
jade::basic_settings::genotype_matrix_type
basic_genotype_matrix< value_type > genotype_matrix_type
The genotype matrix type.
Definition: nemeco/jade.settings.hpp:37
jade::basic_settings::agi_ptr_type
std::unique_ptr< agi_reader_type > agi_ptr_type
A pointer to an admixture graph input reader.
Definition: nemeco/jade.settings.hpp:30
jade::basic_options::get_f_epsilon
value_type get_f_epsilon() const
Definition: cpax/jade.options.hpp:141
jade::basic_matrix< value_type >
jade::basic_settings::get_c
const matrix_type & get_c() const
Definition: nemeco/jade.settings.hpp:129
jade::basic_settings::matrix_type
basic_matrix< value_type > matrix_type
The matrix type.
Definition: nemeco/jade.settings.hpp:40