LocARNA-2.0.0
aux.hh
1 #ifndef LOCARNA_AUX_HH
2 #define LOCARNA_AUX_HH
3 
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7 
8 #include <iostream>
9 #include <exception>
10 #include <string>
11 #include <vector>
12 #include <cassert>
13 
17 
18 #if __cplusplus < 201100L
19 # define nullptr NULL
20 #endif
21 
22 namespace std
23 {
25  template <class T1, class T2>
26  struct hash<std::pair<T1, T2>>
27  {
28  size_t
29  operator()(const std::pair<T1, T2> &p) const
30  noexcept(noexcept(hash<T1>()(p.first)) &&
31  noexcept(hash<T2>()(p.second))) {
32  return hash<T1>()(p.first) ^ (hash<T2>()(p.second) << 1);
33  }
34  };
35 }
36 
37 #include "quadmath.hh"
38 
39 namespace LocARNA {
40 
41  using standard_pf_score_t = double;
42  using extended_pf_score_t = long double;
43 
44 #if defined(_GLIBCXX_USE_FLOAT128) && ! defined(__clang__)
45  using quad_pf_score_t = __float128;
46 #else
47  using quad_pf_score_t = long double;
48 #endif
49 
50  template <typename T>
51  struct check_score_t {
52  template<class CLP>
53  check_score_t(const CLP &clp) {}
54  };
55 
56  template <>
57  struct check_score_t<extended_pf_score_t> {
58  template <class CLP>
59  check_score_t(const CLP &clp) {
60  if (clp.verbose) {
61  std::cout << "Use extended precision for partition functions ("
62  << sizeof(extended_pf_score_t) << " bytes; usually 80bit precision)."
63  <<std::endl;
64  }
65  if (!(sizeof(extended_pf_score_t) > sizeof(standard_pf_score_t))) {
66  std::cerr << "WARNING: the extended precision type (long double) "
67  << "is not larger than the standard precision "
68  << "( double, "<<sizeof(standard_pf_score_t)<<" bytes )."
69  <<std::endl
70  << "This issue is system and compiler dependent."
71  <<std::endl;
72  }
73  }
74  };
75 
76 
77  // template <typename T, class CLP>
78  // void
79  // check_score_t(const CLP &clp) {}
80 
81  // template <class CLP>
82  // void
83  // check_score_t<extended_pf_score_t,CLP>(const CLP &clp) {
84  // if (clp.verbose) {
85  // std::cout << "Use extended precision for partition functions ("
86  // << sizeof(extended_pf_score_t) << " bytes; usually 80bit precision)."
87  // <<std::endl;
88  // }
89  // if (!(sizeof(extended_pf_score_t) > sizeof(standard_pf_score_t))) {
90  // std::cerr << "WARNING: the extended precision type (long double) "
91  // << "is not larger than the standard precision "
92  // << "( double, "<<sizeof(standard_pf_score_t)<<" bytes )."
93  // <<std::endl
94  // << "This issue is system and compiler dependent."
95  // <<std::endl;
96  // }
97  // }
98 
99 #if defined( _GLIBCXX_USE_FLOAT128 ) && ! defined( __clang__ )
100  template <>
101  struct check_score_t<quad_pf_score_t> {
102  template <class CLP>
103  check_score_t(const CLP &clp) {
104  if (clp.verbose) {
105  std::cout << "Use quad precision for partition functions ("
106  << sizeof(quad_pf_score_t) << " bytes; 128bit precision)."
107  <<std::endl;
108  }
109  if (!(sizeof(quad_pf_score_t) > sizeof(standard_pf_score_t))) {
110  std::cerr << "WARNING: the quad precision type (__float128) "
111  << "is not larger than the standard precision "
112  << "( double, "<<sizeof(standard_pf_score_t)<<" bytes )."
113  <<std::endl;
114  }
115  }
116  };
117 #endif
118 
119 
120  class string1;
121 
123  typedef size_t size_type;
124 
127 
128  // ------------------------------------------------------------
129  // define gap codes and symbols
130 
132  enum class Gap {
133  regular,
134  loop,
135  locality,
136  other
137  };
138 
143  bool
144  is_gap_symbol(char c);
145 
147  char
148  gap_symbol(Gap gap);
149 
151  char
152  special_gap_symbol(Gap gap);
153 
155  Gap
156  gap_code(char symbol);
157  // ------------------------------------------------------------
158 
160  class failure : public std::exception {
162  std::string msg_;
163 
164  public:
170  explicit failure(const std::string &msg)
171  : std::exception(), msg_(msg){};
172 
176  explicit failure() : std::exception(), msg_(){};
177 
179  virtual ~failure();
180 
184  virtual const char *
185  what() const noexcept;
186  };
187 
191  struct wrong_format_failure : public failure {
192  wrong_format_failure() : failure("Wrong format") {}
193  };
194 
198  struct syntax_error_failure : public failure {
200  syntax_error_failure() : failure("Syntax error") {}
201 
207  explicit syntax_error_failure(const std::string &msg)
208  : failure("Syntax error: " + msg) {}
209  };
210 
216  constexpr
217  inline double
218  prob_exp_f(int seqlen) {
219  return 1.0 / (2.0 * seqlen);
220  }
221 
222  // ------------------------------------------------------------
223  // transformation of strings
224 
231  void
232  transform_toupper(std::string &s);
233 
241  void
242  normalize_rna_sequence(std::string &seq);
243 
254  void
255  split_at_separator(const std::string &s,
256  char sep,
257  std::vector<std::string> &v);
258 
269  std::vector<std::string>
270  split_at_separator(const std::string &s, char sep);
271 
279  std::string
280  concat_with_separator(const std::vector<std::string> &v, char sep);
281 
291  typedef double FLT_OR_DBL;
292 
302  constexpr
303  inline bool
304  frag_len_geq(size_t i, size_t j, size_t minlen) {
305  return i + minlen <= j + 1;
306  }
307 
316  constexpr
317  inline size_t
318  frag_len(size_t i, size_t j) {
319  return j + 1 - i;
320  }
321 
331  constexpr
332  inline size_t
333  bp_span(size_t i, size_t j) {
334  return frag_len(i, j);
335  }
336 
347  bool
348  has_prefix(const std::string &s, const std::string &p, size_t start = 0);
349 
367  bool
368  get_nonempty_line(std::istream &in, std::string &line);
369 
370  double
371  sequence_identity(const string1 &seqA, const string1 &seqB);
372 
379  template <class Iterable, typename KeyFun>
380  auto
381  maximum(const Iterable&x, const KeyFun &key) {
382  auto maxelem_it = max_element(x.begin(), x.end(),
383  [&key](const auto &x, const auto &y) {
384  return key(x) < key(y);
385  });
386  return key(*maxelem_it);
387  }
388 
395  template <class Iterable, typename KeyFun>
396  auto
397  minimum(const Iterable&x, const KeyFun &key) {
398  auto minelem_it = min_element(x.begin(), x.end(),
399  [&key](const auto &x, const auto &y) {
400  return key(x) < key(y);
401  });
402  return key(*minelem_it);
403  }
404 
405  // @brief delay the evaluation of a function
406  template <class Value, class Function>
407  class Delay {
408  public:
409  Delay(Function fun)
410  : fun_(fun), evaluated_(false)
411  {};
412 
413  const Value &
414  get() {
415  if (!evaluated_) {
416  val_ = fun_();
417  }
418  evaluated_ = true;
419  return val_;
420  }
421 
422  private:
423  Value val_;
424  Function fun_;
425  bool evaluated_;
426  };
427 
428  // @brief make object of Delay
429  // @note work around partial specialization of functions
430  template <class Value>
431  class make_delay {
432  public:
433  template <class Function>
434  static auto
435  fun(Function fun_) {
436  return Delay<Value, Function>(fun_);
437  }
438  };
439 
440 
441 }
442 
443 #endif
Definition: aux.hh:407
Simple exception class that supports a text message.
Definition: aux.hh:160
virtual const char * what() const noexcept
Provide message string.
Definition: aux.cc:14
failure()
Construct empty.
Definition: aux.hh:176
virtual ~failure()
Destruct.
Definition: aux.cc:11
failure(const std::string &msg)
Construct with message.
Definition: aux.hh:170
Definition: aux.hh:431
command_line_parameters clp
holds command line parameters of locarna
Definition: locarna.cc:80
Definition: aligner.cc:15
bool has_prefix(const std::string &s, const std::string &p, size_t start)
Test string prefix.
Definition: aux.cc:73
char gap_symbol(Gap gap)
simplified symbols of gaps
Definition: aux.cc:36
size_type pos_type
type of a sequence position
Definition: aux.hh:126
bool get_nonempty_line(std::istream &in, std::string &line)
Get next non-empty/non-comment line.
Definition: aux.cc:122
void normalize_rna_sequence(std::string &seq)
Transform an RNA sequence string.
Definition: aux.cc:65
char special_gap_symbol(Gap gap)
special symbols of gaps
Definition: aux.cc:41
std::string concat_with_separator(const std::vector< std::string > &v, char sep)
Concatenate strings, inserting separators.
Definition: aux.cc:111
void split_at_separator(const std::string &s, char sep, std::vector< std::string > &v)
Tokenize string at separator symbol.
Definition: aux.cc:81
constexpr size_t bp_span(size_t i, size_t j)
Definition: aux.hh:333
auto minimum(const Iterable &x, const KeyFun &key)
generic minimum value of iterable
Definition: aux.hh:397
Gap
different types of gaps
Definition: aux.hh:132
constexpr double prob_exp_f(int seqlen)
expected probability of a base pair (null-model)
Definition: aux.hh:218
constexpr size_t frag_len(size_t i, size_t j)
Definition: aux.hh:318
Gap gap_code(char symbol)
code of a gap symbol
Definition: aux.cc:46
auto maximum(const Iterable &x, const KeyFun &key)
generic maximum value of iterable
Definition: aux.hh:381
bool is_gap_symbol(char c)
Test for gap symbol.
Definition: aux.cc:31
void transform_toupper(std::string &s)
Converts char to upper case.
Definition: aux.cc:60
size_t size_type
general size type
Definition: aux.hh:120
constexpr bool frag_len_geq(size_t i, size_t j, size_t minlen)
Definition: aux.hh:304
double FLT_OR_DBL
select FLT_OR_DBL
Definition: aux.hh:291
Definition: aux.hh:51
thrown, when the format is recognized but syntax is incorrect
Definition: aux.hh:198
syntax_error_failure()
empty constructor
Definition: aux.hh:200
syntax_error_failure(const std::string &msg)
Construct with message string.
Definition: aux.hh:207
thrown, when reading data that is not in the supposed format
Definition: aux.hh:191