1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
#ifndef UTILITY_FUNCTIONS_HPP
#define UTILITY_FUNCTIONS_HPP
#include <limits>
#include "SalmonUtils.hpp"
// from http://stackoverflow.com/questions/17719674/c11-fast-constexpr-integer-powers
constexpr int64_t constExprPow(int64_t base, unsigned int exp, int64_t result = 1) {
return exp < 1 ? result : constExprPow(base*base, exp/2, (exp % 2) ? result*base : result);
}
inline std::string kmerForIndex(uint32_t idx, uint32_t K) {
std::string kmer(K, 'X');
// The number of bits we need to shift the
// current mask to the left.
uint32_t pos{0};
for (int32_t i = K - 1; i >= 0; --i) {
uint8_t c = (idx >> pos) & 0x3;
switch (c) {
case 0:
kmer[i] = 'A';
break;
case 1:
kmer[i] = 'C';
break;
case 2:
kmer[i] = 'G';
break;
case 3:
kmer[i] = 'T';
break;
default:
break;
}
pos += 2;
}
return kmer;
}
inline uint32_t nextKmerIndex(uint32_t idx, char n, uint32_t K,
salmon::utils::Direction dir) {
using salmon::utils::Direction;
if(dir == Direction::REVERSE or dir == Direction::REVERSE_COMPLEMENT) {
// drop the leftmost character, and replace it with the complement of the
// new one.
idx = idx >> 2;
switch(n) {
case 'A':
case 'a':
// n='T';
// complement is 'T';
idx = idx | (3 << 2*(K-1));
break;
case 'C':
case 'c':
// n='G';
// complement is 'G';
idx = idx | (2 << 2*(K-1));
break;
case 'g':
case 'G':
// n='C';
// complement is 'C';
idx = idx | (1 << 2*(K-1));
break;
case 'T':
case 't':
case 'U':
case 'u':
// n='A';
// complement is 'A';
break;
}
return idx;
} else {
// drop the rightmost character and replace it with the new one.
idx = idx << 2;
switch(n) {
case 'A':
case 'a': break;
case 'C':
case 'c': idx = idx + 1;
break;
case 'G':
case 'g': idx = idx + 2;
break;
case 'T':
case 't':
case 'U':
case 'u':
idx = idx + 3;
break;
}
// Clear the top 32 - 2*K bits.
uint32_t clearShift = (32 - 2*K);
return idx & (0xFFFFFFFF >> clearShift);
}
}
inline uint32_t indexForKmer(const char* s,
uint32_t K,
salmon::utils::Direction dir) {
using salmon::utils::Direction;
// The index we'll return
uint32_t idx{0};
// The number of bits we need to shift the
// current mask to the left.
if(dir == Direction::FORWARD) {
for (int32_t i = 0; i < K; ++i) {
switch (s[i]) {
case 'A':
case 'a':
break;
case 'C':
case 'c':
idx += 1;
break;
case 'G':
case 'g':
idx += 2;
break;
case 'T':
case 't':
case 'U':
case 'u':
idx += 3;
break;
default:
return std::numeric_limits<uint32_t>::max();
}
if (i < K - 1) {idx = idx << 2;}
}
} else {
for(int32_t i=K-1 ; i>=0 ; i--) {
switch(s[i]) {
case 'T':
case 't':
case 'u':
case 'U': break;
case 'C':
case 'c': idx += 2;
break;
case 'G':
case 'g': idx += 1;
break;
case 'A':
case 'a': idx += 3;
break;
default:
return std::numeric_limits<uint32_t>::max();
}
if (i > 0) {idx = idx << 2;}
}
}
return idx;
}
#endif //UTILITY_FUNCTIONS_HPP
|