LocARNA-1.9.2
|
00001 #ifndef LOCARNA_AUX_HH 00002 #define LOCARNA_AUX_HH 00003 00004 #ifdef HAVE_CONFIG_H 00005 #include <config.h> 00006 #endif 00007 00008 #include <iosfwd> 00009 #include <exception> 00010 #include <string> 00011 #include <vector> 00012 #include <cassert> 00013 00014 #if __cplusplus < 201100L 00015 # define nullptr NULL 00016 #endif 00017 00018 // import and define types for unordered_map/set 00019 // in a way that is compatible with stdc++ and libc++ 00020 #ifdef _LIBCPP_VERSION 00021 #include <unordered_map> 00022 #include <unordered_set> 00023 namespace LocARNA { 00024 template <class Key, // unordered_map::key_type 00025 class T, // unordered_map::mapped_type 00026 class Hash = std::hash<Key>, // unordered_map::hasher 00027 class Pred = std::equal_to<Key>, // unordered_map::key_equal 00028 class Alloc = std::allocator< 00029 std::pair<const Key, T> > // unordered_map::allocator_type 00030 > 00031 struct unordered_map { 00032 typedef std::unordered_map<Key, T, Hash, Pred, Alloc> type; 00033 }; 00034 00035 template <class Key, // unordered_set::key_type/value_type 00036 class Hash = std::hash<Key>, // unordered_set::hasher 00037 class Pred = std::equal_to<Key>, // unordered_set::key_equal 00038 class Alloc = std::allocator<Key> // unordered_set::allocator_type 00039 > 00040 struct unordered_set { 00041 typedef std::unordered_set<Key, Hash, Pred, Alloc> type; 00042 }; 00043 } 00044 // typedef std::unordered_set LocARNA::unordered_set; 00045 #else 00046 #include <tr1/unordered_map> 00047 #include <tr1/unordered_set> 00048 namespace LocARNA { 00049 template <class Key, // unordered_map::key_type 00050 class T, // unordered_map::mapped_type 00051 class Hash = std::tr1::hash<Key>, // unordered_map::hasher 00052 class Pred = std::equal_to<Key>, // unordered_map::key_equal 00053 class Alloc = std::allocator< 00054 std::pair<const Key, T> > // unordered_map::allocator_type 00055 > 00056 struct unordered_map { 00057 typedef std::tr1::unordered_map<Key, T, Hash, Pred, Alloc> type; 00058 }; 00059 00060 template <class Key, // unordered_set::key_type/value_type 00061 class Hash = std::tr1::hash<Key>, // unordered_set::hasher 00062 class Pred = std::equal_to<Key>, // unordered_set::key_equal 00063 class Alloc = std::allocator<Key> // unordered_set::allocator_type 00064 > 00065 struct unordered_set { 00066 typedef std::tr1::unordered_set<Key, Hash, Pred, Alloc> type; 00067 }; 00068 } 00069 #endif 00070 00074 00075 namespace LocARNA { 00076 00077 class string1; 00078 00082 struct pair_of_size_t_hash { 00088 size_t 00089 operator()(std::pair<size_t, size_t> p) const { 00090 return p.first << (sizeof(size_t) / 2) | p.second; 00091 } 00092 }; 00093 00095 typedef size_t size_type; 00096 00098 typedef size_type pos_type; 00099 00100 // ------------------------------------------------------------ 00101 // define gap codes and symbols 00102 00104 class Gap { 00105 private: 00106 size_t idx_; 00107 public: 00108 static size_t size; 00109 00111 static const Gap regular; 00113 static const Gap loop; 00116 static const Gap locality; 00118 static const Gap other; 00119 00122 explicit Gap(size_t idx) : idx_(idx) {} 00123 00125 size_t 00126 idx() const { 00127 return (size_t)idx_; 00128 } 00129 00136 bool 00137 operator==(const Gap &x) const { 00138 return this->idx_ == x.idx_; 00139 } 00140 00147 bool 00148 operator!=(const Gap &x) const { 00149 return this->idx_ != x.idx_; 00150 } 00151 }; 00152 00157 bool 00158 is_gap_symbol(char c); 00159 00161 char 00162 gap_symbol(Gap gap); 00163 00165 char 00166 special_gap_symbol(Gap gap); 00167 00169 Gap 00170 gap_code(char symbol); 00171 // ------------------------------------------------------------ 00172 00174 class failure : public std::exception { 00176 std::string msg_; 00177 00178 public: 00184 explicit failure(const std::string &msg) 00185 : std::exception(), msg_(msg){}; 00186 00190 explicit failure() : std::exception(), msg_(){}; 00191 00193 virtual ~failure() throw(); 00194 00198 virtual const char * 00199 what() const throw(); 00200 }; 00201 00205 struct wrong_format_failure : public failure { 00206 wrong_format_failure() : failure("Wrong format") {} 00207 }; 00208 00212 struct syntax_error_failure : public failure { 00214 syntax_error_failure() : failure("Syntax error") {} 00215 00221 explicit syntax_error_failure(const std::string &msg) 00222 : failure("Syntax error: " + msg) {} 00223 }; 00224 00230 inline double 00231 prob_exp_f(int seqlen) { 00232 return 1.0 / (2.0 * seqlen); 00233 } 00234 00235 // ------------------------------------------------------------ 00236 // transformation of strings 00237 00244 void 00245 transform_toupper(std::string &s); 00246 00254 void 00255 normalize_rna_sequence(std::string &seq); 00256 00267 void 00268 split_at_separator(const std::string &s, 00269 char sep, 00270 std::vector<std::string> &v); 00271 00282 std::vector<std::string> 00283 split_at_separator(const std::string &s, char sep); 00284 00295 std::string 00296 concat_with_separator(const std::vector<std::string> &v, char sep); 00297 00307 typedef double FLT_OR_DBL; 00308 00318 inline bool 00319 frag_len_geq(size_t i, size_t j, size_t minlen) { 00320 return i + minlen <= j + 1; 00321 } 00322 00331 inline size_t 00332 frag_len(size_t i, size_t j) { 00333 return j + 1 - i; 00334 } 00335 00345 inline size_t 00346 bp_span(size_t i, size_t j) { 00347 return frag_len(i, j); 00348 } 00349 00360 bool 00361 has_prefix(const std::string &s, const std::string &p, size_t start = 0); 00362 00380 bool 00381 get_nonempty_line(std::istream &in, std::string &line); 00382 00383 double 00384 sequence_identity(const string1 &seqA, const string1 &seqB); 00385 } 00386 00387 #endif