bpp-core3  3.0.0
ApplicationTools.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_APP_APPLICATIONTOOLS_H
6 #define BPP_APP_APPLICATIONTOOLS_H
7 
8 
9 #include "../Io/FileTools.h"
10 #include "../Io/OutputStream.h"
11 #include "../Numeric/Matrix/Matrix.h"
12 #include "../Text/NestedStringTokenizer.h"
13 #include "../Text/StringTokenizer.h"
14 #include "../Text/TextTools.h"
15 
16 // From the STL:
17 #include <map>
18 #include <vector>
19 #include <iostream>
20 #include <ctime>
21 
22 namespace bpp
23 {
56 {
57 public:
61  static std::shared_ptr<OutputStream> error;
65  static std::shared_ptr<OutputStream> message;
69  static std::shared_ptr<OutputStream> warning;
70 
74  static time_t startTime;
75 
79  static size_t terminalWidth;
80 
84  static float terminalSplit;
85 
89  static bool interactive;
90 
94  static int warningLevel;
95 
96 public:
98  virtual ~ApplicationTools() {}
99 
100 public:
108  static bool parameterExists(const std::string& parameterName, const std::map<std::string, std::string>& params)
109  {
110  std::map<std::string, std::string>::const_iterator it = params.find(parameterName);
111 
112  return it != params.end() && !TextTools::isEmpty(it->second);
113  }
114 
115 
116  static bool parameterExists(const std::string& parameterName, std::vector<std::string>& params)
117  {
118  for (size_t i = 0; i < params.size(); ++i)
119  {
120  if (params[i] == parameterName)
121  return true;
122  }
123 
124  return false;
125  }
126 
136  static std::vector<std::string> matchingParameters(const std::string& pattern, const std::map<std::string, std::string>& params);
137 
138  static std::vector<std::string> matchingParameters(const std::string& pattern, std::vector<std::string>& params);
139 
151  static double getDoubleParameter(
152  const std::string& parameterName,
153  const std::map<std::string, std::string>& params,
154  double defaultValue,
155  const std::string& suffix = "",
156  bool suffixIsOptional = true,
157  int warn = 0);
158 
170  static int getIntParameter(
171  const std::string& parameterName,
172  const std::map<std::string, std::string>& params,
173  int defaultValue,
174  const std::string& suffix = "",
175  bool suffixIsOptional = true,
176  int warn = 0);
177 
189  static std::string getStringParameter(
190  const std::string& parameterName,
191  const std::map<std::string, std::string>& params,
192  const std::string& defaultValue,
193  const std::string& suffix = "",
194  bool suffixIsOptional = true,
195  int warn = 0)
196  {
197  std::string sParam = defaultValue;
198  std::map<std::string, std::string>::const_iterator it1 = params.find(parameterName + suffix);
199  if (it1 != params.end() && !TextTools::isEmpty(it1->second))
200  sParam = it1->second;
201  else
202  {
203  std::map<std::string, std::string>::const_iterator it2 = params.find(parameterName);
204  if (suffixIsOptional && it2 != params.end() && !TextTools::isEmpty(it2->second))
205  sParam = it2->second;
206  else if (warn <= warningLevel)
207  {
208  displayWarning("Parameter " + parameterName + " not specified. Default used instead: " + defaultValue);
209  }
210  }
211 
212  return sParam;
213  }
214 
215 
227  static bool getBooleanParameter(
228  const std::string& parameterName,
229  const std::map<std::string, std::string>& params,
230  bool defaultValue,
231  const std::string& suffix = "",
232  bool suffixIsOptional = true,
233  int warn = 0);
234 
246  template<class T> static T getParameter(
247  const std::string& parameterName,
248  const std::map<std::string, std::string>& params,
249  T defaultValue,
250  const std::string& suffix = "",
251  bool suffixIsOptional = true,
252  int warn = 0)
253  {
254  T tParam = defaultValue;
255  if (parameterExists(parameterName + suffix, params))
256  {
257  tParam = TextTools::to<T>(params.at(parameterName + suffix));
258  }
259  else if (suffixIsOptional && parameterExists(parameterName, params))
260  {
261  tParam = TextTools::to<T>(params.at(parameterName));
262  }
263  else if (warn <= warningLevel)
264  {
265  displayWarning("Parameter " + parameterName + suffix + " not specified. Default used instead: " + TextTools::toString(defaultValue));
266  }
267  return tParam;
268  }
269 
270 
288  static std::string getAFilePath(
289  const std::string& parameter,
290  const std::map<std::string, std::string>& params,
291  bool isRequired = true,
292  bool mustExist = true,
293  const std::string& suffix = "",
294  bool suffixIsOptional = false,
295  const std::string& defaultPath = "none",
296  int warn = 0);
297 
311  template<class T> static std::vector<T> getVectorParameter(
312  const std::string& parameterName,
313  const std::map<std::string, std::string>& params,
314  char separator,
315  const std::string& defaultValue,
316  const std::string& suffix = "",
317  bool suffixIsOptional = true,
318  int warn = 0)
319  {
320  if (separator == ' ') throw Exception("ApplicationTools::getVectorParameter(). Separator cannot be a space character.");
321  std::string s = getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
322  if (TextTools::isEmpty(s)) return std::vector<T>(0);
323  if (s[0] == '(' && s[s.size() - 1] == ')')
324  {
325  // This is a delimited vector:
326  s = s.substr(1, s.size() - 2);
327  if (TextTools::isEmpty(s)) return std::vector<T>(0);
328  }
329  NestedStringTokenizer st(s, "(", ")", TextTools::toString(separator));
330  size_t n = st.numberOfRemainingTokens();
331  std::vector<T> v(n);
332  for (size_t i = 0; i < n; ++i)
333  {
334  v[i] = TextTools::fromString<T>(st.nextToken());
335  }
336  return v;
337  }
338 
352  template<class T> static std::vector< std::vector<T>> getVectorOfVectorsParameter(
353  const std::string& parameterName,
354  const std::map<std::string, std::string>& params,
355  char separator,
356  const std::string& defaultValue,
357  const std::string& suffix = "",
358  bool suffixIsOptional = true,
359  int warn = 0)
360  {
361  if (separator == ' ') throw Exception("ApplicationTools::getVectorOfVectorsParameter(). Separator cannot be a space character.");
362  std::string s = getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
363  if (TextTools::isEmpty(s)) return std::vector< std::vector<T>>(0);
364  if (s[0] == '(' && s[s.size() - 1] == ')')
365  {
366  // This is a delimited vector:
367  s = s.substr(1, s.size() - 2);
368  if (TextTools::isEmpty(s)) return std::vector< std::vector<T>>(0);
369  }
370  NestedStringTokenizer st(s, "(", ")", TextTools::toString(separator));
371  size_t n = st.numberOfRemainingTokens();
372  std::string s2;
373  std::vector< std::vector<T>> v(n);
374  for (size_t i = 0; i < n; ++i)
375  {
376  s2 = st.nextToken();
377  if (s2[0] == '(' && s2[s2.size() - 1] == ')')
378  {
379  // This is a delimited vector:
380  s2 = s2.substr(1, s2.size() - 2);
381  }
382  if (!TextTools::isEmpty(s2))
383  {
384  NestedStringTokenizer st2(s2, "(", ")", TextTools::toString(separator));
385  size_t n2 = st2.numberOfRemainingTokens();
386  v[i].resize(n2);
387  for (size_t j = 0; j < n2; ++j)
388  {
389  v[i][j] = TextTools::fromString<T>(st2.nextToken());
390  }
391  }
392  }
393  return v;
394  }
395 
412  template<class T> static std::vector<T> getVectorParameter(
413  const std::string& parameterName,
414  const std::map<std::string, std::string>& params,
415  char separator,
416  char rangeOperator,
417  const std::string& defaultValue,
418  const std::string& suffix = "",
419  bool suffixIsOptional = true,
420  bool warn = true)
421  {
422  std::string s = getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
423  if (s[0] == '(' && s[s.size() - 1] == ')')
424  {
425  // This is a delimited vector:
426  s = s.substr(1, s.size() - 2);
427  if (TextTools::isEmpty(s)) return std::vector<T>(0);
428  }
429  StringTokenizer st(s, TextTools::toString(separator));
430  size_t n = st.numberOfRemainingTokens();
431  std::vector<T> v;
432  for (size_t i = 0; i < n; ++i)
433  {
434  std::string token = st.nextToken();
435  std::string::size_type pos = token.find(rangeOperator);
436  if (pos == std::string::npos)
437  v.push_back(TextTools::fromString<T>(token));
438  else
439  {
440  T d1 = TextTools::fromString<T>(token.substr(0, pos));
441  T d2 = TextTools::fromString<T>(token.substr(pos + 1));
442  for (T j = d1; j < d2; j++)
443  {
444  v.push_back(j);
445  }
446  v.push_back(d2);
447  }
448  }
449  return v;
450  }
451 
472  template<class T> static RowMatrix<T> getMatrixParameter(
473  const std::string& parameterName,
474  const std::map<std::string, std::string>& params,
475  char separator,
476  const std::string& defaultValue,
477  const std::string& suffix = "",
478  bool suffixIsOptional = true,
479  bool warn = true)
480  {
481  RowMatrix<T> mat;
482 
483  std::string s = getStringParameter(parameterName, params, defaultValue, suffix, suffixIsOptional, warn);
484  if (TextTools::isEmpty(s)) return RowMatrix<T>(0, 0);
485  if (s[0] == '(' && s[s.size() - 1] == ')')
486  {
487  // This is a delimited vector:
488  s = s.substr(1, s.size() - 2);
489  if (TextTools::isEmpty(s)) return RowMatrix<T>(0, 0);
490  }
491 
492  StringTokenizer st1(s, "()");
493 
494  while (st1.hasMoreToken())
495  {
496  std::string si = st1.nextToken();
497  StringTokenizer st2(si, TextTools::toString(separator));
498  size_t n = st2.numberOfRemainingTokens();
499 
500  std::vector<T> v(n);
501  for (size_t i = 0; i < n; i++)
502  {
503  v[i] = TextTools::fromString<T>(st2.nextToken());
504  }
505 
506  if (v.size() != 0)
507  mat.addRow(v);
508  }
509  return mat;
510  }
511 
512 
524  static void displayMessage(const std::string& text);
525 
531  static void displayError(const std::string& text);
532 
538  static void displayWarning(const std::string& text);
539 
548  static void displayTask(const std::string& text, bool eof = false);
549 
555  static void displayTaskDone();
556 
567  template<class T>
568  static void displayResult(const std::string& text, const T& result)
569  {
570  displayMessage(TextTools::resizeRight(text, static_cast<size_t>(static_cast<float>(terminalWidth) * terminalSplit - 1), '.') + ": " + TextTools::toString<T>(result));
571  }
572 
580  static void displayBooleanResult(const std::string& text, bool result)
581  {
582  displayResult(text, result ? std::string("yes") : std::string("no"));
583  }
584 
606  static void displayGauge(size_t iter, size_t total, char symbol = '>', const std::string& mes = "");
607 
632  static void displayUnlimitedGauge(size_t iter, const std::string& mes = "");
633 
634 
640  static void startTimer()
641  {
642  time(&startTime);
643  }
644 
650  static void displayTime(const std::string& msg);
651 
657  static double getTime();
658 };
659 } // end of namespace bpp.
660 #endif // BPP_APP_APPLICATIONTOOLS_H
std::string resizeRight(const std::string &s, std::size_t newSize, char fill)
Definition: TextTools.cpp:226
static bool parameterExists(const std::string &parameterName, const std::map< std::string, std::string > &params)
Tells if a parameter have been specified.
static void displayTaskDone()
Print a task ended message.
static void displayWarning(const std::string &text)
Print a warning message.
static std::vector< std::vector< T > > getVectorOfVectorsParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, char separator, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a vector of vectors.
A tokenizer for strings.
static std::vector< std::string > matchingParameters(const std::string &pattern, const std::map< std::string, std::string > &params)
Returns a vector of parameter names that match a given pattern.
static std::vector< T > getVectorParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, char separator, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a vector.
const std::string & nextToken()
Get the next available token. If no token is availbale, throw an Exception.
static int warningLevel
Specify the amount of warning to display.
static void displayTime(const std::string &msg)
Display the current timer value to the &#39;message&#39; stream.
bool hasMoreToken() const
Tell if some tokens are still available.
static void displayResult(const std::string &text, const T &result)
Print a result message.
static void displayTask(const std::string &text, bool eof=false)
Print a task message.
static T getParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, T defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a parameter.
static size_t terminalWidth
The width of the output terminal (in character).
size_t numberOfRemainingTokens() const
Tell how many tokens are available.
static double getDoubleParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, double defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a double parameter.
This class provides some common tools for developping applications.
Matrix storage by row.
Definition: Matrix.h:92
static std::shared_ptr< OutputStream > warning
The output stream where warnings have to be displayed.
static RowMatrix< T > getMatrixParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, char separator, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, bool warn=true)
Get a RowMatrix. The input is made of embedded parenthesis, such as ((1,2),(3,4)), where the matrix is filled by lines. Here, the matrix would be: .
static void startTimer()
Starts the timer.
static time_t startTime
Timer variable.
static std::shared_ptr< OutputStream > message
The output stream where messages have to be displayed.
static bool interactive
Tell if the program is interactive (typically run in foreground). Default to yes. ...
static int getIntParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, int defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get an integer parameter.
static std::shared_ptr< OutputStream > error
The output stream where errors have to be displayed.
static void displayMessage(const std::string &text)
Print a message.
static std::string getAFilePath(const std::string &parameter, const std::map< std::string, std::string > &params, bool isRequired=true, bool mustExist=true, const std::string &suffix="", bool suffixIsOptional=false, const std::string &defaultPath="none", int warn=0)
Get a file path.
static bool getBooleanParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, bool defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a boolean parameter.
static bool parameterExists(const std::string &parameterName, std::vector< std::string > &params)
static float terminalSplit
The fraction of terminal width dedicated to messages.
const std::string & nextToken()
Get the next available token. If no token is availbale, throw an Exception.
static void displayBooleanResult(const std::string &text, bool result)
Print a boolean result message ("yes" or "no").
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
static void displayGauge(size_t iter, size_t total, char symbol='>', const std::string &mes="")
Display a gauge.
static std::string getStringParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a string parameter.
static std::vector< T > getVectorParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, char separator, char rangeOperator, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, bool warn=true)
Get a vector.
static void displayUnlimitedGauge(size_t iter, const std::string &mes="")
Display a gauge for unefined amount of iterations.
void addRow(const std::vector< Scalar > &newRow)
Definition: Matrix.h:185
An improved tokenizer for strings.
bool isEmpty(const std::string &s)
Tell if a string is empty. A string is considered to be &#39;empty&#39; if it is only made of white spaces...
Definition: TextTools.cpp:20
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:115
static void displayError(const std::string &text)
Print an error message.
static double getTime()
Get the current timer value.