LocARNA-1.8.11
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 <iosfwd>
9 #include <exception>
10 #include <string>
11 #include <vector>
12 #include <cassert>
13 
14 
15 // import and define types for unordered_map/set
16 // in a way that is compatible with stdc++ and libc++
17 #ifdef _LIBCPP_VERSION
18 # include <unordered_map>
19 # include <unordered_set>
20 namespace LocARNA {
21  template < class Key, // unordered_map::key_type
22  class T, // unordered_map::mapped_type
23  class Hash = std::hash<Key>, // unordered_map::hasher
24  class Pred = std::equal_to<Key>, // unordered_map::key_equal
25  class Alloc = std::allocator< std::pair<const Key,T> > // unordered_map::allocator_type
26  >
27  struct unordered_map {
28  typedef std::unordered_map<Key,T,Hash,Pred,Alloc> type;
29  };
30 
31  template < class Key, // unordered_set::key_type/value_type
32  class Hash = std::hash<Key>, // unordered_set::hasher
33  class Pred = std::equal_to<Key>, // unordered_set::key_equal
34  class Alloc = std::allocator<Key> // unordered_set::allocator_type
35  >
36  struct unordered_set {
37  typedef std::unordered_set<Key,Hash,Pred,Alloc> type;
38  };
39 }
40 //typedef std::unordered_set LocARNA::unordered_set;
41 #else
42 # include <tr1/unordered_map>
43 # include <tr1/unordered_set>
44 namespace LocARNA {
45  template < class Key, // unordered_map::key_type
46  class T, // unordered_map::mapped_type
47  class Hash = std::tr1::hash<Key>, // unordered_map::hasher
48  class Pred = std::equal_to<Key>, // unordered_map::key_equal
49  class Alloc = std::allocator< std::pair<const Key,T> > // unordered_map::allocator_type
50  >
51  struct unordered_map {
52  typedef std::tr1::unordered_map<Key,T,Hash,Pred,Alloc> type;
53  };
54 
55  template < class Key, // unordered_set::key_type/value_type
56  class Hash = std::tr1::hash<Key>, // unordered_set::hasher
57  class Pred = std::equal_to<Key>, // unordered_set::key_equal
58  class Alloc = std::allocator<Key> // unordered_set::allocator_type
59  >
60  struct unordered_set {
61  typedef std::tr1::unordered_set<Key,Hash,Pred,Alloc> type;
62  };
63 }
64 #endif
65 
66 
70 
71 
72 namespace LocARNA {
73 
74  class string1;
75 
80  {
86  size_t
87  operator()(std::pair<size_t,size_t> p) const {
88  return p.first<<(sizeof(size_t)/2) | p.second;
89  }
90  };
91 
92 
94  typedef size_t size_type;
95 
97  typedef size_type pos_type;
98 
99 
100  // ------------------------------------------------------------
101  // define gap codes and symbols
102 
104  class Gap
105  {
106  private:
107  size_t idx_;
108  public:
109  static size_t size;
110 
112  static const Gap regular;
114  static const Gap loop;
116  static const Gap locality;
118  static const Gap other;
119 
122  explicit
123  Gap(size_t idx) : idx_(idx) {}
124 
126  size_t
127  idx() const { return (size_t)idx_; }
128 
135  bool
136  operator == (const Gap & x) const { return this->idx_ == x.idx_; }
137 
144  bool
145  operator != (const Gap & x) const { return this->idx_ != x.idx_; }
146  };
147 
148 
153  bool is_gap_symbol(char c);
154 
156  char
157  gap_symbol(Gap gap);
158 
160  char
161  special_gap_symbol(Gap gap);
162 
164  Gap gap_code(char symbol);
165  // ------------------------------------------------------------
166 
168  class failure : public std::exception {
170  std::string msg_;
171  public:
177  explicit
178  failure (const std::string& msg): std::exception(), msg_(msg) {};
179 
183  explicit
184  failure (): std::exception(), msg_() {};
185 
187  virtual
188  ~failure() throw();
189 
193  virtual
194  const char* what() const throw();
195  };
196 
200  struct wrong_format_failure: public failure {
201  wrong_format_failure():failure("Wrong format") {}
202  };
203 
207  struct syntax_error_failure: public failure {
208 
210  syntax_error_failure():failure("Syntax error") {}
211 
217  explicit
218  syntax_error_failure(const std::string &msg):failure("Syntax error: "+msg) {}
219  };
220 
221 
226  inline
227  double
228  prob_exp_f(int seqlen) {return 1.0/(2.0*seqlen);}
229 
230 
231  // ------------------------------------------------------------
232  // transformation of strings
233 
240  void transform_toupper(std::string &s);
241 
242 
250  void
251  normalize_rna_sequence(std::string &seq);
252 
253 
264  void
265  split_at_separator(const std::string &s, char sep, std::vector<std::string> &v);
266 
277  std::vector<std::string>
278  split_at_separator(const std::string &s, char sep);
279 
290  std::string
291  concat_with_separator(const std::vector<std::string> &v, char sep);
292 
302  typedef double FLT_OR_DBL;
303 
304 
314  inline
315  bool
316  frag_len_geq(size_t i, size_t j, size_t minlen) {
317  return i+minlen <= j+1;
318  }
319 
328  inline
329  size_t
330  frag_len(size_t i, size_t j) {
331  return j+1-i;
332  }
333 
344  bool
345  has_prefix(const std::string &s, const std::string &p, size_t start=0);
346 
364  bool
365  get_nonempty_line(std::istream &in,
366  std::string &line);
367 
368 
369  double
370  sequence_identity(const string1 &seqA, const string1 &seqB);
371 
372 
373 }
374 
375 
376 #endif
syntax_error_failure()
empty constructor
Definition: aux.hh:210
double FLT_OR_DBL
select FLT_OR_DBL
Definition: aux.hh:302
Function class definining hash function for pairs of size_t.
Definition: aux.hh:79
bool operator==(const TaintedInftyInt &x, const TaintedInftyInt &y)
Definition: infty_int.hh:598
size_type pos_type
type of a sequence position
Definition: aux.hh:97
thrown, when the format is recognized but syntax is incorrect
Definition: aux.hh:207
failure(const std::string &msg)
Construct with message.
Definition: aux.hh:178
bool is_gap_symbol(char c)
Test for gap symbol.
Definition: aux.cc:35
bool has_prefix(const std::string &s, const std::string &p, size_t start)
Test string prefix.
Definition: aux.cc:80
STL namespace.
size_t size_type
general size type
Definition: aux.hh:94
failure()
Construct empty.
Definition: aux.hh:184
void normalize_rna_sequence(std::string &seq)
Transform an RNA sequence string.
Definition: aux.cc:72
bool frag_len_geq(size_t i, size_t j, size_t minlen)
Definition: aux.hh:316
Definition: aligner.cc:17
void split_at_separator(const std::string &s, char sep, std::vector< std::string > &v)
Tokenize string at separator symbol.
Definition: aux.cc:89
size_t idx() const
0-based index
Definition: aux.hh:127
A simple 1-based string.
Definition: string1.hh:22
"enum class" of gaps in alignment edges
Definition: aux.hh:104
thrown, when reading data that is not in the supposed format
Definition: aux.hh:200
static const Gap loop
gap from inserting/deleting a loop (in sparse)
Definition: aux.hh:114
Gap(size_t idx)
init from 0-based index
Definition: aux.hh:123
static size_t size
< index of enumeration value
Definition: aux.hh:109
std::string concat_with_separator(const std::vector< std::string > &v, char sep)
Tokenize string at separator symbol.
Definition: aux.cc:113
static const Gap locality
gap outside of the locally aligned region (sequence and structure local alignment) ...
Definition: aux.hh:116
syntax_error_failure(const std::string &msg)
Construct with message string.
Definition: aux.hh:218
static const Gap regular
regular gap
Definition: aux.hh:112
char special_gap_symbol(Gap gap)
special symbols of gaps
Definition: aux.cc:45
bool get_nonempty_line(std::istream &in, std::string &line)
Get next non-empty/non-comment line.
Definition: aux.cc:123
size_t frag_len(size_t i, size_t j)
Definition: aux.hh:330
double prob_exp_f(int seqlen)
expected probability of a base pair (null-model)
Definition: aux.hh:228
static const Gap other
other gaps
Definition: aux.hh:118
Simple exception class that supports a text message.
Definition: aux.hh:168
Definition: aux.hh:60
Definition: aux.hh:51
Gap gap_code(char symbol)
code of a gap symbol
Definition: aux.cc:49
size_t operator()(std::pair< size_t, size_t > p) const
Hash function for pairs of size_t.
Definition: aux.hh:87
void transform_toupper(std::string &s)
Definition: aux.cc:67
char gap_symbol(Gap gap)
simplified symbols of gaps
Definition: aux.cc:40