ʻOhana
Population structure, admixture history, and selection using learning methods.
qpas/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.forced_grouping.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 encapsulating the settings provided to the
18  /// optimizer.
19  ///
20  template <typename TValue>
21  class basic_settings
22  {
23  public:
24  /// The value type.
25  typedef TValue value_type;
26 
27  /// The matrix type.
29 
30  /// The forced grouping type.
32 
33  /// The genotype matrix.
35 
36  /// The options type.
38 
39  /// The randomizer type.
41 
42  /// The verification type.
44 
45  /// The forced grouping pointer type.
46  typedef std::unique_ptr<forced_grouping_type> fg_ptr;
47 
48  /// The Fin-force pointer type.
49  typedef std::unique_ptr<matrix_type> fif_ptr;
50 
51  /// The genotype matrix pointer type.
52  typedef std::unique_ptr<genotype_matrix_type> g_ptr;
53 
54  /// The genotype matrix factory type.
57 
58  ///
59  /// Initializes a new instance of the class based on the specified
60  /// arguments.
61  ///
62  explicit basic_settings(
63  args & a) ///< The command-line arguments.
64  : _opts (a)
65  , _g (g_matrix_factory_type::create(a.pop<std::string>()))
66  , _q ()
67  , _f ()
68  , _fif ()
69  , _fg ()
70  , _rnd ()
71  {
73 
74  a.validate_empty();
75 
76  if (_opts.is_qin_specified())
77  {
78  _q.read(_opts.get_qin());
80  }
81 
82  if (_opts.is_fin_specified())
83  {
84  _f.read(_opts.get_fin());
86  }
87 
88  if (_opts.is_fin_force_specified())
89  {
90  _fif.reset(new matrix_type(_opts.get_fin_force()));
92  }
93 
94  if (_opts.is_force_specified())
95  _fg.reset(new forced_grouping_type(_opts.get_force()));
96 
97  const auto n = _g->get_height();
98  const auto k = _opts.is_qin_specified() ? _q.get_width() :
99  _opts.is_fin_specified() ? _f.get_height() :
100  _opts.is_force_specified() ? _fg->get_k() :
101  _opts.get_ksize();
102 
103  if (_fif)
105  *_fif, k, _g->get_width());
106 
107  _rnd.get_engine().seed(_opts.get_seed());
108 
109  if (!_opts.is_qin_specified())
110  _q = _opts.is_force_specified()
111  ? _fg->randomize_q(_rnd)
112  : _rnd.randomize_q(n ,k);
113 
114  if (_opts.is_fin_force_specified())
115  _f = _rnd.randomize_f(
116  k,
117  _g->create_mu(_opts.get_f_epsilon()),
118  *_fif);
119  else if (!_opts.is_fin_specified())
120  _f = _rnd.randomize_f(
121  k,
122  _g->create_mu(_opts.get_f_epsilon()));
123 
125 
126  if (_opts.is_force_specified())
127  _fg->validate_q(_q);
128  }
129 
130  ///
131  /// \return The F matrix.
132  ///
133  inline const matrix_type & get_f() const
134  {
135  return _f;
136  }
137 
138  ///
139  /// \return The F matrix.
140  ///
141  inline matrix_type & get_f()
142  {
143  return _f;
144  }
145 
146  ///
147  /// \return The Fin-force matrix.
148  ///
149  inline const matrix_type * get_fif() const
150  {
151  return _fif.get();
152  }
153 
154  ///
155  /// \return A pointer to the forced grouping file or nullptr if the
156  /// information is not available.
157  ///
158  inline const forced_grouping_type * get_fg() const
159  {
160  return _fg.get();
161  }
162 
163  ///
164  /// \return The genotype matrix.
165  ///
166  inline const genotype_matrix_type & get_g() const
167  {
168  return *_g;
169  }
170 
171  ///
172  /// \return The options.
173  ///
174  inline const options_type & get_options() const
175  {
176  return _opts;
177  }
178 
179  ///
180  /// \return The Q matrix.
181  ///
182  inline const matrix_type & get_q() const
183  {
184  return _q;
185  }
186 
187  ///
188  /// \return The Q matrix.
189  ///
190  inline matrix_type & get_q()
191  {
192  return _q;
193  }
194 
195  ///
196  /// \return The randomizer.
197  ///
198  inline const randomizer_type & get_randomizer() const
199  {
200  return _rnd;
201  }
202 
203  private:
204  options_type _opts;
205  g_ptr _g;
206  matrix_type _q;
207  matrix_type _f;
208  fif_ptr _fif;
209  fg_ptr _fg;
210  randomizer_type _rnd;
211  };
212 }
213 
214 #endif // JADE_SETTINGS_HPP__
jade::basic_options< value_type >
jade::basic_settings::get_f
const matrix_type & get_f() const
Definition: qpas/jade.settings.hpp:133
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_settings::forced_grouping_type
basic_forced_grouping< value_type > forced_grouping_type
The forced grouping type.
Definition: qpas/jade.settings.hpp:31
jade::basic_settings::get_options
const options_type & get_options() const
Definition: qpas/jade.settings.hpp:174
jade::basic_randomizer< value_type >
jade::basic_randomizer::get_engine
std::default_random_engine & get_engine()
Definition: jade.randomizer.hpp:42
jade::basic_settings::g_matrix_factory_type
basic_genotype_matrix_factory< value_type > g_matrix_factory_type
The genotype matrix factory type.
Definition: qpas/jade.settings.hpp:56
jade::basic_options::get_ksize
size_t get_ksize() const
Definition: cpax/jade.options.hpp:185
jade::basic_settings::g_ptr
std::unique_ptr< genotype_matrix_type > g_ptr
The genotype matrix pointer type.
Definition: qpas/jade.settings.hpp:52
jade::basic_matrix::get_width
size_t get_width() const
Definition: jade.matrix.hpp:757
jade::basic_settings::get_q
matrix_type & get_q()
Definition: qpas/jade.settings.hpp:190
jade::basic_settings::get_fif
const matrix_type * get_fif() const
Definition: qpas/jade.settings.hpp:149
jade::basic_genotype_matrix
A template for an abstract class implementing operations for a genotype matrix.
Definition: jade.genotype_matrix.hpp:26
jade::basic_randomizer::randomize_q
matrix_type randomize_q(const size_t I, const size_t K)
Definition: jade.randomizer.hpp:123
jade::basic_options::is_fin_force_specified
bool is_fin_force_specified() const
Definition: cpax/jade.options.hpp:262
jade::basic_settings::value_type
TValue value_type
The value type.
Definition: qpas/jade.settings.hpp:25
jade::basic_settings::fif_ptr
std::unique_ptr< matrix_type > fif_ptr
The Fin-force pointer type.
Definition: qpas/jade.settings.hpp:49
jade::basic_settings::get_g
const genotype_matrix_type & get_g() const
Definition: qpas/jade.settings.hpp:166
jade::basic_settings::verification_type
basic_verification< value_type > verification_type
The verification type.
Definition: qpas/jade.settings.hpp:43
jade::basic_settings::options_type
basic_options< value_type > options_type
The options type.
Definition: qpas/jade.settings.hpp:37
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_randomizer::randomize_f
matrix_type randomize_f(const size_t K, const matrix_type &mu)
Definition: jade.randomizer.hpp:50
jade::basic_options::get_fin_force
const std::string & get_fin_force() const
Definition: cpax/jade.options.hpp:158
jade::basic_settings::get_f
matrix_type & get_f()
Definition: qpas/jade.settings.hpp:141
jade::basic_settings::randomizer_type
basic_randomizer< value_type > randomizer_type
The randomizer type.
Definition: qpas/jade.settings.hpp:40
jade::basic_matrix::get_height
size_t get_height() const
Definition: jade.matrix.hpp:603
jade::basic_options::get_fin
const std::string & get_fin() const
Definition: cpax/jade.options.hpp:149
jade::basic_verification::validate_gqf_sizes
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.
Definition: jade.verification.hpp:211
jade::basic_options::is_fin_specified
bool is_fin_specified() const
Definition: cpax/jade.options.hpp:254
jade::basic_settings::get_q
const matrix_type & get_q() const
Definition: qpas/jade.settings.hpp:182
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 arguments.
Definition: qpas/jade.settings.hpp:62
jade::basic_settings::get_fg
const forced_grouping_type * get_fg() const
Definition: qpas/jade.settings.hpp:158
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_verification::validate_q
static bool validate_q(const matrix_type &q)
Validates the Q matrix and throws an exception if validation fails.
Definition: jade.verification.hpp:225
jade::basic_options::is_force_specified
bool is_force_specified() const
Definition: cpax/jade.options.hpp:286
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_options::is_qin_specified
bool is_qin_specified() const
Definition: cpax/jade.options.hpp:326
jade::basic_options::get_force
const std::string & get_force() const
Definition: cpax/jade.options.hpp:167
jade::basic_options::get_qin
const std::string & get_qin() const
Definition: cpax/jade.options.hpp:212
jade::basic_verification::validate_fif_size
static bool validate_fif_size(const matrix_type &fif, const size_t k, const size_t j)
Validates the size of the Fin-force matrix and throws an exception if validation fails.
Definition: jade.verification.hpp:131
jade::basic_settings::genotype_matrix_type
basic_genotype_matrix< value_type > genotype_matrix_type
The genotype matrix.
Definition: qpas/jade.settings.hpp:34
jade::basic_forced_grouping
A template for a class that implements the forced grouping feature.
Definition: cpax/jade.forced_grouping.hpp:19
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_randomizer
const randomizer_type & get_randomizer() const
Definition: qpas/jade.settings.hpp:198
jade::basic_options::get_seed
seed_type get_seed() const
Definition: cpax/jade.options.hpp:230
jade::basic_settings::matrix_type
basic_matrix< value_type > matrix_type
The matrix type.
Definition: qpas/jade.settings.hpp:28
jade::basic_settings::fg_ptr
std::unique_ptr< forced_grouping_type > fg_ptr
The forced grouping pointer type.
Definition: qpas/jade.settings.hpp:46