ʻOhana
Population structure, admixture history, and selection using learning methods.
nemeco/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 that reads the command-line arguments.
16  ///
17  template <typename TValue>
18  class basic_options
19  {
20  public:
21  /// The value type.
22  typedef TValue value_type;
23 
24  /// The value assigned with no --epsilon option.
25  static constexpr auto no_epsilon =
26  std::numeric_limits<value_type>::quiet_NaN();
27 
28  /// The value assigned with no --max-iterations option.
29  static constexpr auto no_iterations =
30  std::numeric_limits<size_t>::max();
31 
32  /// The value assigned with no --max-time option.
33  static constexpr auto no_time =
34  std::numeric_limits<double>::quiet_NaN();
35 
36  ///
37  /// Initializes a new instance of the class.
38  ///
39  explicit basic_options(
40  args & a) ///< The command-line arguments.
41  : _ain (a.read<std::string>("--ain", "-ai"))
42  , _cin (a.read<std::string>("--cin", "-ci"))
43  , _cout (a.read<std::string>("--cout", "-co"))
44  , _epsilon (a.read("--epsilon", "-e", no_epsilon))
45  , _f_epsilon (a.read("--f-epsilon", "-fe", value_type(1.0e-6)))
46  , _max_iterations (a.read("--max-iterations", "-mi", no_iterations))
47  , _max_time (a.read("--max-time", "-mt", no_time))
48  , _tin (a.read<std::string>("--tin", "-ti"))
49  , _tout (a.read<std::string>("--tout", "-to"))
50  {
51  if (is_epsilon_specified() && _epsilon < value_type(0))
52  throw error()
53  << "invalid value for --epsilon option: " << _epsilon;
54 
55  if (!(_f_epsilon > value_type(0.0) &&
56  _f_epsilon < value_type(0.1)))
57  {
58  throw error()
59  << "invalid value for --f-epsilon option: "
60  << _f_epsilon;
61  }
62 
63  if (is_max_time_specified() && _max_time < 0.0)
64  throw error()
65  << "invalid value for --max-time option: " << _max_time;
66 
67  if (is_max_iterations_specified() && _max_iterations == 0)
68  throw error()
69  << "invalid number of iterations for --max-iterations "
70  << "option: " << _max_iterations;
71 
73  throw error("invalid specification of --tout option "
74  "without --tin option");
75 
76  if ((is_ain_specified() ? 1 : 0) +
77  (is_cin_specified() ? 1 : 0) +
78  (is_tin_specified() ? 1 : 0) > 1)
79  throw error("only one of --ain, --cin, and --tin options may "
80  "be specified");
81  }
82 
83  ///
84  /// \return The admixture graph input path, if specified.
85  ///
86  inline const std::string & get_ain() const
87  {
88  assert(is_ain_specified());
89  return _ain;
90  }
91 
92  ///
93  /// \return The C input matrix path, if specified.
94  ///
95  inline const std::string & get_cin() const
96  {
97  assert(is_cin_specified());
98  return _cin;
99  }
100 
101  ///
102  /// \return The C output matrix path, if specified.
103  ///
104  inline const std::string & get_cout() const
105  {
106  assert(is_cout_specified());
107  return _cout;
108  }
109 
110  ///
111  /// \return The epsilon value.
112  ///
113  inline value_type get_epsilon() const
114  {
115  assert(is_epsilon_specified());
116  return _epsilon;
117  }
118 
119  ///
120  /// \return The F epsilon value.
121  ///
122  inline value_type get_f_epsilon() const
123  {
124  return _f_epsilon;
125  }
126 
127  ///
128  /// \return The maximum number of iterations.
129  ///
130  inline size_t get_max_iterations() const
131  {
132  assert(is_max_iterations_specified());
133  return _max_iterations;
134  }
135 
136  ///
137  /// \return The maximum amount of time to execute.
138  ///
139  inline double get_max_time() const
140  {
141  assert(is_max_time_specified());
142  return _max_time;
143  }
144 
145  ///
146  /// \return The T input tree path, if specified.
147  ///
148  inline const std::string & get_tin() const
149  {
150  assert(is_tin_specified());
151  return _tin;
152  }
153 
154  ///
155  /// \return The T output tree path, if specified.
156  ///
157  inline const std::string & get_tout() const
158  {
159  assert(is_tout_specified());
160  return _tout;
161  }
162 
163  ///
164  /// \return True if the admixture graph input file was specified.
165  ///
166  inline bool is_ain_specified() const
167  {
168  return !_ain.empty();
169  }
170 
171  ///
172  /// \return True if the C input matrix was specified.
173  ///
174  inline bool is_cin_specified() const
175  {
176  return !_cin.empty();
177  }
178 
179  ///
180  /// \return True if the C output matrix was specified.
181  ///
182  inline bool is_cout_specified() const
183  {
184  return !_cout.empty();
185  }
186 
187  ///
188  /// \return True if the epsilon option was specified.
189  ///
190  inline bool is_epsilon_specified() const
191  {
192  return !std::isnan(_epsilon);
193  }
194 
195  ///
196  /// \return True if the maximum iterations option was specified.
197  ///
198  inline bool is_max_iterations_specified() const
199  {
200  return _max_iterations != no_iterations;
201  }
202 
203  ///
204  /// \return True if the maximum time option was specified.
205  ///
206  inline bool is_max_time_specified() const
207  {
208  return !std::isnan(_max_time);
209  }
210 
211  ///
212  /// \return True if the T input tree was specified.
213  ///
214  inline bool is_tin_specified() const
215  {
216  return !_tin.empty();
217  }
218 
219  ///
220  /// \return True if the T output tree was specified.
221  ///
222  inline bool is_tout_specified() const
223  {
224  return !_tout.empty();
225  }
226 
227  private:
228  std::string _ain;
229  std::string _cin;
230  std::string _cout;
231  value_type _epsilon;
232  value_type _f_epsilon;
233  size_t _max_iterations;
234  double _max_time;
235  std::string _tin;
236  std::string _tout;
237  };
238 }
239 
240 #endif // JADE_OPTIONS_HPP__
jade::basic_options::basic_options
basic_options(args &a)
Initializes a new instance of the class.
Definition: nemeco/jade.options.hpp:39
jade::basic_options::is_max_iterations_specified
bool is_max_iterations_specified() const
Definition: cpax/jade.options.hpp:310
jade::basic_options::is_cout_specified
bool is_cout_specified() const
Definition: nemeco/jade.options.hpp:182
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: nemeco/jade.options.hpp:139
jade::basic_options::get_tout
const std::string & get_tout() const
Definition: nemeco/jade.options.hpp:157
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: nemeco/jade.options.hpp:130
jade::basic_options::is_tin_specified
bool is_tin_specified() const
Definition: nemeco/jade.options.hpp:214
jade::basic_options::is_tout_specified
bool is_tout_specified() const
Definition: nemeco/jade.options.hpp:222
jade::basic_options::is_ain_specified
bool is_ain_specified() const
Definition: nemeco/jade.options.hpp:166
jade::basic_options::get_epsilon
value_type get_epsilon() const
Definition: nemeco/jade.options.hpp:113
jade::basic_options::no_time
static constexpr auto no_time
The value assigned with no –max-time option.
Definition: nemeco/jade.options.hpp:33
jade::basic_options::no_iterations
static constexpr auto no_iterations
The value assigned with no –max-iterations option.
Definition: nemeco/jade.options.hpp:29
jade::basic_options::get_tin
const std::string & get_tin() const
Definition: nemeco/jade.options.hpp:148
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_options::is_cin_specified
bool is_cin_specified() const
Definition: nemeco/jade.options.hpp:174
jade::basic_options::get_cin
const std::string & get_cin() const
Definition: nemeco/jade.options.hpp:95
jade::basic_error
A template for a class representing an exception thrown from this namespace.
Definition: jade.error.hpp:20
jade::basic_options::get_f_epsilon
value_type get_f_epsilon() const
Definition: nemeco/jade.options.hpp:122
jade::basic_options::value_type
TValue value_type
The value type.
Definition: nemeco/jade.options.hpp:22
jade::basic_options::get_cout
const std::string & get_cout() const
Definition: nemeco/jade.options.hpp:104