ʻOhana
Population structure, admixture history, and selection using learning methods.
qpas/jade.options.hpp
1 /* -------------------------------------------------------------------------
2  Ohana
3  Copyright (c) 2015-2020 Jade Cheng (\___/)
4  Jade Cheng <info@jade-cheng.com> (='.'=)
5  ------------------------------------------------------------------------- */
6 
7 #ifndef JADE_OPTIONS_HPP__
8 #define JADE_OPTIONS_HPP__
9 
10 #include "jade.args.hpp"
11 
12 namespace jade
13 {
14  ///
15  /// A template for a class encapsulating the program options.
16  ///
17  template <typename TValue>
18  class basic_options
19  {
20  public:
21  /// The value type.
22  typedef TValue value_type;
23 
24  /// The random number generator seed type.
25  typedef std::random_device::result_type seed_type;
26 
27  /// The value assigned for no --epsilon option.
28  static constexpr auto no_epsilon =
29  std::numeric_limits<value_type>::quiet_NaN();
30 
31  /// The value assigned for no --ksize option.
32  static constexpr auto no_ksize =
33  std::numeric_limits<size_t>::max();
34 
35  /// The value assigned for no --max-iterations option.
36  static constexpr auto no_max_iterations =
37  std::numeric_limits<size_t>::max();
38 
39  /// The value assigned for no --max-time option.
40  static constexpr auto no_max_time =
41  std::numeric_limits<double>::quiet_NaN();
42 
43  ///
44  /// Initializes a new instance of the class.
45  ///
46  explicit basic_options(
47  args & a) ///< The command-line arguments.
48  : _epsilon (a.read("--epsilon", "-e", no_epsilon))
49  , _f_epsilon (a.read("--f-epsilon", "-fe", value_type(1.0e-6)))
50  , _fin (a.read<std::string>("--fin", "-fi"))
51  , _fin_force (a.read<std::string>("--fin-force", "-fif"))
52  , _force (a.read<std::string>("--force", "-fg"))
53  , _fout (a.read<std::string>("--fout", "-fo"))
54  , _ksize (a.read("--ksize", "-k", no_ksize))
55  , _max_iterations (a.read("--max-iterations", "-mi", no_max_iterations))
56  , _max_time (a.read("--max-time", "-mt", no_max_time))
57  , _qin (a.read<std::string>("--qin", "-qi"))
58  , _qout (a.read<std::string>("--qout", "-qo"))
59  , _seed (a.read("--seed", "-s", std::random_device()()))
60  , _frb (a.read_flag("--frequency-bounds", "-frb"))
61  , _fixed_f (a.read_flag("--fixed-f", "-ff"))
62  , _fixed_q (a.read_flag("--fixed-q", "-fq"))
63  , _quiet (a.read_flag("--quiet", "-q"))
64  {
65  if (is_epsilon_specified() && _epsilon < value_type(0))
66  throw error()
67  << "invalid value for --epsilon option: "
68  << _epsilon;
69 
70  if (!(_f_epsilon > value_type(0.0) &&
71  _f_epsilon < value_type(0.1)))
72  {
73  throw error()
74  << "invalid value for --f-epsilon option: "
75  << _f_epsilon;
76  }
77 
78  if (is_ksize_specified() && _ksize < 2)
79  throw error()
80  << "invalid value for --ksize option: "
81  << _ksize;
82 
83  if (is_max_time_specified() && _max_time < 0.0)
84  throw error()
85  << "invalid value for --max-time option: "
86  << _max_time;
87 
88  if (!is_ksize_specified() &&
89  !is_qin_specified() &&
90  !is_fin_specified() &&
92  {
93  throw error()
94  << "at least one of --ksize, --qin, --fin, or --force must "
95  << "be specified in order to determine the number of "
96  << "components";
97  }
98 
100  throw error() << "invalid specification of --fin option "
101  << "with --fin-force option";
102 
103  if (!is_fin_specified() && _fixed_f)
104  throw error() << "invalid specification of --fixed-f option "
105  << "without --fin option";
106 
107  if (!is_qin_specified() && _fixed_q)
108  throw error() << "invalid specification of --fixed-q option "
109  << "without --qin option";
110 
111  if (is_force_specified() && _fixed_q)
112  throw error() << "invalid specification of --fixed-q option "
113  << "and --force option";
114 
115  if (is_frb())
116  {
118  throw error()
119  << "invalid specification of --fin-force "
120  << "and --frequency-bounds options";
121 
122  if (is_fixed_f() && is_fin_specified())
123  throw error()
124  << "invalid specification of --fixed-f, --fin, "
125  << "and --frequency-bounds options";
126  }
127  }
128 
129  ///
130  /// \return The epsilon value.
131  ///
132  inline value_type get_epsilon() const
133  {
134  assert(is_epsilon_specified());
135  return _epsilon;
136  }
137 
138  ///
139  /// \return The F epsilon value.
140  ///
141  inline value_type get_f_epsilon() const
142  {
143  return _f_epsilon;
144  }
145 
146  ///
147  /// \return The fin value.
148  ///
149  inline const std::string & get_fin() const
150  {
151  assert(is_fin_specified());
152  return _fin;
153  }
154 
155  ///
156  /// \return The fin-force value.
157  ////
158  inline const std::string & get_fin_force() const
159  {
160  assert(is_fin_force_specified());
161  return _fin_force;
162  }
163 
164  ///
165  /// \return The force value.
166  ///
167  inline const std::string & get_force() const
168  {
169  assert(is_force_specified());
170  return _force;
171  }
172 
173  ///
174  /// \return The fout value.
175  ///
176  inline const std::string & get_fout() const
177  {
178  assert(is_fout_specified());
179  return _fout;
180  }
181 
182  ///
183  /// \return The ksize value.
184  ///
185  inline size_t get_ksize() const
186  {
187  assert(is_ksize_specified());
188  return _ksize;
189  }
190 
191  ///
192  /// \return The maximum iterations value.
193  ///
194  inline size_t get_max_iterations() const
195  {
196  assert(is_max_iterations_specified());
197  return _max_iterations;
198  }
199 
200  ///
201  /// \return The maximum time value.
202  ///
203  inline double get_max_time() const
204  {
205  assert(is_max_time_specified());
206  return _max_time;
207  }
208 
209  ///
210  /// \return The qin value.
211  ///
212  inline const std::string & get_qin() const
213  {
214  assert(is_qin_specified());
215  return _qin;
216  }
217 
218  ///
219  /// \return The qout value.
220  ///
221  inline const std::string & get_qout() const
222  {
223  assert(is_qout_specified());
224  return _qout;
225  }
226 
227  ///
228  /// \return The seed value.
229  ///
230  inline seed_type get_seed() const
231  {
232  return _seed;
233  }
234 
235  ///
236  /// \return True if the epsilon option is specified.
237  ///
238  inline bool is_epsilon_specified() const
239  {
240  return !std::isnan(_epsilon);
241  }
242 
243  ///
244  /// \return True if the frequency-bounds option is specified.
245  ///
246  inline bool is_frb() const
247  {
248  return _frb;
249  }
250 
251  ///
252  /// \return True if the fin option is specified.
253  ///
254  inline bool is_fin_specified() const
255  {
256  return !_fin.empty();
257  }
258 
259  ///
260  /// \return True if the fin-force option is specified.
261  ///
262  inline bool is_fin_force_specified() const
263  {
264  return !_fin_force.empty();
265  }
266 
267  ///
268  /// \return The fixed F value.
269  ///
270  inline bool is_fixed_f() const
271  {
272  return _fixed_f;
273  }
274 
275  ///
276  /// \return The fixed Q value.
277  ///
278  inline bool is_fixed_q() const
279  {
280  return _fixed_q;
281  }
282 
283  ///
284  /// \return True if the force option is specified.
285  ///
286  inline bool is_force_specified() const
287  {
288  return !_force.empty();
289  }
290 
291  ///
292  /// \return True if the fout option is specified.
293  ///
294  inline bool is_fout_specified() const
295  {
296  return !_fout.empty();
297  }
298 
299  ///
300  /// \return True if the ksize option is specified.
301  ///
302  inline bool is_ksize_specified() const
303  {
304  return _ksize != no_ksize;
305  }
306 
307  ///
308  /// \return True if the maximum iterations option is specified.
309  ///
310  inline bool is_max_iterations_specified() const
311  {
312  return _max_iterations != no_max_iterations;
313  }
314 
315  ///
316  /// \return True if the maximum time option is specified.
317  ///
318  inline bool is_max_time_specified() const
319  {
320  return !std::isnan(_max_time);
321  }
322 
323  ///
324  /// \return True if the qin option is specified.
325  ///
326  inline bool is_qin_specified() const
327  {
328  return !_qin.empty();
329  }
330 
331  ///
332  /// \return True if the qout option is specified.
333  ///
334  inline bool is_qout_specified() const
335  {
336  return !_qout.empty();
337  }
338 
339  ///
340  /// \return The quiet value.
341  ///
342  inline bool is_quiet() const
343  {
344  return _quiet;
345  }
346 
347  private:
348  // options with arguments
349  const value_type _epsilon;
350  const value_type _f_epsilon;
351  const std::string _fin;
352  const std::string _fin_force;
353  const std::string _force;
354  const std::string _fout;
355  const size_t _ksize;
356  const size_t _max_iterations;
357  const double _max_time;
358  const std::string _qin;
359  const std::string _qout;
360  const seed_type _seed;
361 
362  // options without arguments
363  const bool _frb;
364  const bool _fixed_f;
365  const bool _fixed_q;
366  const bool _quiet;
367  };
368 }
369 
370 #endif // JADE_OPTIONS_HPP__
jade::basic_options::is_ksize_specified
bool is_ksize_specified() const
Definition: cpax/jade.options.hpp:302
jade::basic_options::basic_options
basic_options(args &a)
Initializes a new instance of the class.
Definition: qpas/jade.options.hpp:46
jade::basic_options::is_max_iterations_specified
bool is_max_iterations_specified() const
Definition: cpax/jade.options.hpp:310
jade::basic_options::is_quiet
bool is_quiet() const
Definition: qpas/jade.options.hpp:342
jade::basic_options::get_fout
const std::string & get_fout() const
Definition: qpas/jade.options.hpp:176
jade::basic_options::seed_type
std::random_device::result_type seed_type
The random number generator seed type.
Definition: qpas/jade.options.hpp:25
jade::basic_options::get_ksize
size_t get_ksize() const
Definition: qpas/jade.options.hpp:185
jade::basic_options::get_qout
const std::string & get_qout() const
Definition: qpas/jade.options.hpp:221
jade::basic_options::is_fin_force_specified
bool is_fin_force_specified() const
Definition: cpax/jade.options.hpp:262
jade::basic_options::is_epsilon_specified
bool is_epsilon_specified() const
Definition: cpax/jade.options.hpp:238
jade::basic_options::get_max_time
double get_max_time() const
Definition: qpas/jade.options.hpp:203
jade::basic_options::no_epsilon
static constexpr auto no_epsilon
The value assigned for no –epsilon option.
Definition: cpax/jade.options.hpp:28
jade::basic_options::is_max_time_specified
bool is_max_time_specified() const
Definition: cpax/jade.options.hpp:318
jade::basic_options::get_max_iterations
size_t get_max_iterations() const
Definition: qpas/jade.options.hpp:194
jade::basic_options::is_fixed_q
bool is_fixed_q() const
Definition: qpas/jade.options.hpp:278
jade::basic_options::get_fin_force
const std::string & get_fin_force() const
Definition: qpas/jade.options.hpp:158
jade::basic_options::no_max_iterations
static constexpr auto no_max_iterations
The value assigned for no –max-iterations option.
Definition: cpax/jade.options.hpp:36
jade::basic_options::get_epsilon
value_type get_epsilon() const
Definition: qpas/jade.options.hpp:132
jade::basic_options::no_ksize
static constexpr auto no_ksize
The value assigned for no –ksize option.
Definition: cpax/jade.options.hpp:32
jade::basic_options::is_fout_specified
bool is_fout_specified() const
Definition: cpax/jade.options.hpp:294
jade::basic_options::get_fin
const std::string & get_fin() const
Definition: qpas/jade.options.hpp:149
jade::basic_options::is_fixed_f
bool is_fixed_f() const
Definition: cpax/jade.options.hpp:270
jade::basic_options::no_max_time
static constexpr auto no_max_time
The value assigned for no –max-time option.
Definition: cpax/jade.options.hpp:40
jade::basic_options::is_fin_specified
bool is_fin_specified() const
Definition: cpax/jade.options.hpp:254
jade::basic_args
A template for a class that helps process command-line arguments.
Definition: jade.args.hpp:19
jade::basic_options::is_force_specified
bool is_force_specified() const
Definition: cpax/jade.options.hpp:286
jade::basic_options::is_qin_specified
bool is_qin_specified() const
Definition: cpax/jade.options.hpp:326
jade::basic_error
A template for a class representing an exception thrown from this namespace.
Definition: jade.error.hpp:20
jade::basic_options::get_force
const std::string & get_force() const
Definition: qpas/jade.options.hpp:167
jade::basic_options::get_qin
const std::string & get_qin() const
Definition: qpas/jade.options.hpp:212
jade::basic_options::get_f_epsilon
value_type get_f_epsilon() const
Definition: qpas/jade.options.hpp:141
jade::basic_options::is_qout_specified
bool is_qout_specified() const
Definition: cpax/jade.options.hpp:334
jade::basic_options::value_type
TValue value_type
The value type.
Definition: qpas/jade.options.hpp:22
jade::basic_options::get_seed
seed_type get_seed() const
Definition: qpas/jade.options.hpp:230
jade::basic_options::is_frb
bool is_frb() const
Definition: cpax/jade.options.hpp:246