| 1 | /* |
|---|
| 2 | Copyright (c) 2006-2018 Elmar Pruesse <elmar.pruesse@ucdenver.edu> |
|---|
| 3 | |
|---|
| 4 | This file is part of SINA. |
|---|
| 5 | SINA is free software: you can redistribute it and/or modify it under |
|---|
| 6 | the terms of the GNU General Public License as published by the Free |
|---|
| 7 | Software Foundation, either version 3 of the License, or (at your |
|---|
| 8 | option) any later version. |
|---|
| 9 | |
|---|
| 10 | SINA is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|---|
| 12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|---|
| 13 | for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with SINA. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 17 | |
|---|
| 18 | Additional permission under GNU GPL version 3 section 7 |
|---|
| 19 | |
|---|
| 20 | If you modify SINA, or any covered work, by linking or combining it |
|---|
| 21 | with components of ARB (or a modified version of that software), |
|---|
| 22 | containing parts covered by the terms of the |
|---|
| 23 | ARB-public-library-license, the licensors of SINA grant you additional |
|---|
| 24 | permission to convey the resulting work. Corresponding Source for a |
|---|
| 25 | non-source form of such a combination shall include the source code |
|---|
| 26 | for the parts of ARB used as well as that of the covered work. |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #ifndef _ALIGNED_BASE_H_ |
|---|
| 30 | #define _ALIGNED_BASE_H_ |
|---|
| 31 | |
|---|
| 32 | #include <limits> |
|---|
| 33 | #include <iostream> |
|---|
| 34 | #include <exception> |
|---|
| 35 | #include <string> |
|---|
| 36 | |
|---|
| 37 | namespace sina { |
|---|
| 38 | |
|---|
| 39 | enum base_types { |
|---|
| 40 | BASE_A=0, |
|---|
| 41 | BASE_G=1, |
|---|
| 42 | BASE_C=2, |
|---|
| 43 | BASE_TU=3, |
|---|
| 44 | BASE_MAX=4, |
|---|
| 45 | BASE_LC=4 |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | enum base_types_bitmask { |
|---|
| 49 | BASEM_A=1<<BASE_A, |
|---|
| 50 | BASEM_G=1<<BASE_G, |
|---|
| 51 | BASEM_C=1<<BASE_C, |
|---|
| 52 | BASEM_TU=1<<BASE_TU, |
|---|
| 53 | BASEM_LC=1<<BASE_LC |
|---|
| 54 | }; |
|---|
| 55 | |
|---|
| 56 | class base_iupac { |
|---|
| 57 | public: |
|---|
| 58 | using value_type = unsigned char; |
|---|
| 59 | static const value_type iupac_char_to_bmask[256]; |
|---|
| 60 | static const unsigned char bmask_to_iupac_rna_char[32]; |
|---|
| 61 | static const unsigned char bmask_to_iupac_dna_char[32]; |
|---|
| 62 | static const float base_pairings[256]; |
|---|
| 63 | |
|---|
| 64 | class bad_character_exception : public std::exception { |
|---|
| 65 | public: |
|---|
| 66 | explicit bad_character_exception(value_type c) noexcept |
|---|
| 67 | : character(c) |
|---|
| 68 | { |
|---|
| 69 | } |
|---|
| 70 | const char* what() const noexcept override { |
|---|
| 71 | static std::string msg; |
|---|
| 72 | |
|---|
| 73 | msg = "Character '"; |
|---|
| 74 | msg += character; |
|---|
| 75 | msg += "' not IUPAC encoded base or gap"; |
|---|
| 76 | |
|---|
| 77 | return msg.c_str(); |
|---|
| 78 | } |
|---|
| 79 | value_type character; |
|---|
| 80 | }; |
|---|
| 81 | |
|---|
| 82 | /* construct from char */ |
|---|
| 83 | base_iupac(unsigned char c) { |
|---|
| 84 | value_type data = iupac_char_to_bmask[c]; |
|---|
| 85 | if (data == 0 && c != '-' && c != '.') { |
|---|
| 86 | throw bad_character_exception(c); |
|---|
| 87 | } |
|---|
| 88 | _data = data; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /* assign from char */ |
|---|
| 92 | base_iupac& operator=(unsigned char c) { |
|---|
| 93 | value_type data = iupac_char_to_bmask[c]; |
|---|
| 94 | if (data == 0 && c != '-' && c != '.') { |
|---|
| 95 | throw bad_character_exception(c); |
|---|
| 96 | } |
|---|
| 97 | _data = data; |
|---|
| 98 | return *this; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /* implicit cast to char */ |
|---|
| 102 | operator unsigned char() const { |
|---|
| 103 | return iupac_rna(); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | unsigned char iupac_dna() const { |
|---|
| 107 | return bmask_to_iupac_dna_char[_data]; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | unsigned char iupac_rna() const { |
|---|
| 111 | return bmask_to_iupac_rna_char[_data]; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | /* construct from base_type */ |
|---|
| 115 | base_iupac(base_types b) { |
|---|
| 116 | _data = 1 << b; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /* explicit cast to base_type */ |
|---|
| 120 | base_types getBaseType() const { |
|---|
| 121 | return static_cast<base_types>(__builtin_ctz(_data & 0xf)); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | base_iupac& complement() { |
|---|
| 125 | _data = ((_data & BASEM_G) << (BASE_C - BASE_G)) | |
|---|
| 126 | ((_data & BASEM_C) >> (BASE_C - BASE_G)) | |
|---|
| 127 | ((_data & BASEM_A) << (BASE_TU - BASE_A)) | |
|---|
| 128 | ((_data & BASEM_TU) >> (BASE_TU - BASE_A)) | |
|---|
| 129 | (_data & BASEM_LC); |
|---|
| 130 | return *this; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | base_iupac& setLowerCase() { |
|---|
| 134 | _data |= BASEM_LC; |
|---|
| 135 | return *this; |
|---|
| 136 | } |
|---|
| 137 | base_iupac& setUpperCase() { |
|---|
| 138 | _data &= ~BASEM_LC; |
|---|
| 139 | return *this; |
|---|
| 140 | } |
|---|
| 141 | bool isLowerCase() const { |
|---|
| 142 | return (_data & BASEM_LC) != 0; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | int ambig_order() const { |
|---|
| 146 | return count_bits(_data & 0xf); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | bool is_ambig() const { |
|---|
| 150 | return ambig_order() > 1; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | bool has_A() const { return (_data & BASEM_A) != 0; } |
|---|
| 155 | bool has_G() const { return (_data & BASEM_G) != 0; } |
|---|
| 156 | bool has_C() const { return (_data & BASEM_C) != 0; } |
|---|
| 157 | bool has_TU() const { return (_data & BASEM_TU) != 0; } |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | bool comp(const base_iupac& rhs) const{ |
|---|
| 161 | //optimistic, match if IUPAC suggests match possible |
|---|
| 162 | return (0xf & _data & rhs._data) != 0; |
|---|
| 163 | |
|---|
| 164 | //this would compute average |
|---|
| 165 | //return 1.f - (2.f/count_bits(_data | rhs._data)) * |
|---|
| 166 | // count_bits(_data & rhs._data) ; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | bool comp_pessimistic(const base_iupac& rhs) const { |
|---|
| 171 | return !is_ambig() && (0xf & _data) == (0xf & rhs._data); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | bool comp_exact(const base_iupac& rhs) const { |
|---|
| 175 | return (0xf & _data) == (0xf & rhs._data); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | struct matrix_type { |
|---|
| 179 | float v[BASE_MAX*BASE_MAX]; |
|---|
| 180 | }; |
|---|
| 181 | |
|---|
| 182 | // this does an IUPAC aware comparison using the given scoring matrix |
|---|
| 183 | float comp(const base_iupac& rhs, const matrix_type& m) const { |
|---|
| 184 | float rval = 0; |
|---|
| 185 | int c = 0; |
|---|
| 186 | |
|---|
| 187 | // use some mean bit magic to do a real fast |
|---|
| 188 | // log2(x) with x in 0,1,2,4 |
|---|
| 189 | const unsigned int t = 0x30002010; |
|---|
| 190 | // given this array we can compute log2(x) as follows: |
|---|
| 191 | // log2(x) = (t >> (x*4)) & 0xF |
|---|
| 192 | // (shift x nibbles to right, mask everything but leftmost nibble) |
|---|
| 193 | |
|---|
| 194 | // "a &= a-1" unsets least significant bit |
|---|
| 195 | // "a & -a" unsets all but least significant bit |
|---|
| 196 | |
|---|
| 197 | for(value_type lm = _data & 0xf; lm != 0u; lm &= lm-1) { |
|---|
| 198 | unsigned char l = (t >> (((lm & -lm)-1)*4)) & 0xF; |
|---|
| 199 | for (value_type rm = rhs._data & 0xf; rm != 0u; rm &= rm-1) { |
|---|
| 200 | unsigned char r = (t >> (((rm &-rm)-1)*4)) & 0xF; |
|---|
| 201 | rval += m.v[l*BASE_MAX+r]; |
|---|
| 202 | c++; |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | #if 0 // enable to verify above code |
|---|
| 207 | float tval = 0; |
|---|
| 208 | for (int l=0; l<BASE_MAX; l++) { |
|---|
| 209 | if (_data & 1<<l) { |
|---|
| 210 | for(int r=0; r<BASE_MAX; r++) { |
|---|
| 211 | if (rhs._data & 1<<r) { |
|---|
| 212 | tval += m.v[l*BASE_MAX+r]; |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | if (rval != tval) { |
|---|
| 218 | std::cerr << "x12: " << rval << " " <<tval<<std::endl; |
|---|
| 219 | } |
|---|
| 220 | #endif |
|---|
| 221 | |
|---|
| 222 | return rval/c; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | float pair(const base_iupac& rhs) const { |
|---|
| 226 | return pair(rhs, base_pairings); |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | protected: |
|---|
| 230 | value_type getData() const { return _data; } |
|---|
| 231 | |
|---|
| 232 | static int count_bits(unsigned char c) { |
|---|
| 233 | #define HAVE_BUILTIN_POPCOUNT |
|---|
| 234 | #ifdef HAVE_BUILTIN_POPCOUNT |
|---|
| 235 | return __builtin_popcount(c); |
|---|
| 236 | #else |
|---|
| 237 | int rval = 0; |
|---|
| 238 | while (c) { |
|---|
| 239 | c &= c-1; // this will unset the least significant bit |
|---|
| 240 | rval++; |
|---|
| 241 | } |
|---|
| 242 | return rval; |
|---|
| 243 | #endif |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | float pair(const base_iupac& rhs, const float *bp) const { |
|---|
| 247 | return bp[((_data&0xf)<<4)+(rhs._data&0xf)]; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | private: |
|---|
| 251 | value_type _data{0}; |
|---|
| 252 | }; |
|---|
| 253 | |
|---|
| 254 | template<typename T> |
|---|
| 255 | class aligned : public T |
|---|
| 256 | { |
|---|
| 257 | public: |
|---|
| 258 | using idx_type = unsigned int; |
|---|
| 259 | using base_type = T; |
|---|
| 260 | |
|---|
| 261 | aligned(const idx_type& pos=0, const base_type& base='-') |
|---|
| 262 | : T(base), _idx(pos) {} |
|---|
| 263 | |
|---|
| 264 | base_type getBase() const { return *this;} |
|---|
| 265 | void setBase(const T& b) { T::operator=(b); } |
|---|
| 266 | |
|---|
| 267 | idx_type getPosition() const { return _idx;} |
|---|
| 268 | void setPosition(const idx_type& i) { _idx=i; } |
|---|
| 269 | |
|---|
| 270 | bool operator<(const aligned<T> &rhs) const { |
|---|
| 271 | return _idx < rhs._idx; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | float getWeight() const { return 1; } |
|---|
| 275 | |
|---|
| 276 | private: |
|---|
| 277 | idx_type _idx; |
|---|
| 278 | friend struct aligned_base_reverse_position; |
|---|
| 279 | }; |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | /** |
|---|
| 283 | * This implementation of "aligned" is more compact (4 vs 8 bytes), |
|---|
| 284 | * but slower (needs shifts to access position) and definitely |
|---|
| 285 | * hacky. It casts around happily assuming that the byte order is |
|---|
| 286 | * little endian that and sizeof(T)==1 |
|---|
| 287 | */ |
|---|
| 288 | |
|---|
| 289 | class position { |
|---|
| 290 | protected: |
|---|
| 291 | unsigned char data[3]{0,0,0}; |
|---|
| 292 | }; |
|---|
| 293 | |
|---|
| 294 | template<typename T> |
|---|
| 295 | class aligned_compact : public position, public T |
|---|
| 296 | { |
|---|
| 297 | public: |
|---|
| 298 | using idx_type = uint32_t; |
|---|
| 299 | using base_type = T; |
|---|
| 300 | |
|---|
| 301 | aligned_compact(idx_type pos=0, unsigned char value='-') |
|---|
| 302 | : T(value) |
|---|
| 303 | { |
|---|
| 304 | setPosition(pos); |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | base_type getBase() const { return *this;} |
|---|
| 308 | void setBase(const T& b) { T::operator=(b); } |
|---|
| 309 | |
|---|
| 310 | idx_type getPosition() const { |
|---|
| 311 | return (*(uint32_t*)this & 0xFFFFFF); |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | void setPosition(idx_type pos) { |
|---|
| 315 | *(uint32_t*)&data = (pos & 0xFFFFFF) | (T::getData() << 24) ; |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | bool operator<(const aligned_compact<T> &rhs) const { |
|---|
| 319 | return getPosition() < rhs.getPosition(); |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | float getWeight() const { return 1; } |
|---|
| 323 | |
|---|
| 324 | private: |
|---|
| 325 | friend struct aligned_base_reverse_position; |
|---|
| 326 | }; |
|---|
| 327 | |
|---|
| 328 | #define COMPACT_ALIGNED_BASE |
|---|
| 329 | #ifndef COMPACT_ALIGNED_BASE |
|---|
| 330 | using aligned_base = aligned<base_iupac>; |
|---|
| 331 | #else |
|---|
| 332 | using aligned_base = aligned_compact<base_iupac>; |
|---|
| 333 | #endif |
|---|
| 334 | |
|---|
| 335 | }// namespace sina |
|---|
| 336 | |
|---|
| 337 | namespace std { |
|---|
| 338 | template<> |
|---|
| 339 | struct numeric_limits<sina::base_iupac> : numeric_limits<sina::base_iupac::value_type> {}; |
|---|
| 340 | } // namespace std |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | std::ostream& operator<<(std::ostream& out, sina::aligned_base ab); |
|---|
| 344 | |
|---|
| 345 | #endif // _ALIGNED_BASE_H_ |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | /* |
|---|
| 349 | Local Variables: |
|---|
| 350 | mode:c++ |
|---|
| 351 | c-file-style:"stroustrup" |
|---|
| 352 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) |
|---|
| 353 | indent-tabs-mode:nil |
|---|
| 354 | fill-column:99 |
|---|
| 355 | End: |
|---|
| 356 | */ |
|---|
| 357 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : |
|---|