| 1 | /* |
|---|
| 2 | * |
|---|
| 3 | * Beta.hpp |
|---|
| 4 | * |
|---|
| 5 | */ |
|---|
| 6 | #ifndef BETA_HPP |
|---|
| 7 | #define BETA_HPP |
|---|
| 8 | using namespace std; |
|---|
| 9 | |
|---|
| 10 | struct Beta { |
|---|
| 11 | static const int NBASE = 4;// A, C, G, U |
|---|
| 12 | static const int NBASEG = (NBASE + 1); // A, C, G, U, Gap |
|---|
| 13 | static const int NBASENG = (NBASE + 2); // A, C, G, U, N, Gap |
|---|
| 14 | static const int NPBASE = (NBASE * NBASE); |
|---|
| 15 | static const int NPBASEG = (NBASEG * NBASEG); |
|---|
| 16 | static const int NPBASENG = (NBASENG * NBASENG); |
|---|
| 17 | static const int NCODE = 7; |
|---|
| 18 | static const int BASEBIT = 2; |
|---|
| 19 | // static const int BASEMSK = ~(~0 << BASEBIT); |
|---|
| 20 | static const int BASEMSK = ~(INT_MAX >> BASEBIT << BASEBIT); |
|---|
| 21 | enum BaseCode { |
|---|
| 22 | A_CODE = 0, |
|---|
| 23 | C_CODE = 1, |
|---|
| 24 | G_CODE = 2, |
|---|
| 25 | U_CODE = 3, |
|---|
| 26 | N_CODE = 4, |
|---|
| 27 | GAP_CODE = 5, |
|---|
| 28 | INVALID_CODE = 16 |
|---|
| 29 | }; |
|---|
| 30 | enum PairCode { |
|---|
| 31 | AA_CODE = 0, AC_CODE = 1, AG_CODE = 2, AU_CODE = 3, |
|---|
| 32 | CA_CODE = 4, CC_CODE = 5, CG_CODE = 6, CU_CODE = 7, |
|---|
| 33 | GA_CODE = 8, GC_CODE = 9, GG_CODE = 10, GU_CODE = 11, |
|---|
| 34 | UA_CODE = 12, UC_CODE = 13, UG_CODE = 14, UU_CODE = 15, |
|---|
| 35 | INVALID_PAIR_CODE = 16 |
|---|
| 36 | }; |
|---|
| 37 | enum ReducedPairCode { |
|---|
| 38 | REDUCED_NPBASE = 7, |
|---|
| 39 | REDUCED_AU_CODE = 0, |
|---|
| 40 | REDUCED_CG_CODE = 1, |
|---|
| 41 | REDUCED_GC_CODE = 2, |
|---|
| 42 | REDUCED_GU_CODE = 3, |
|---|
| 43 | REDUCED_UA_CODE = 4, |
|---|
| 44 | REDUCED_UG_CODE = 5, |
|---|
| 45 | REDUCED_MM_CODE = 6, |
|---|
| 46 | REDUCED_INVALID_PAIR_CODE = 16 |
|---|
| 47 | }; |
|---|
| 48 | |
|---|
| 49 | static const int N_CANONICAL = 6; |
|---|
| 50 | static const int canonicalPairs[N_CANONICAL]; |
|---|
| 51 | static const int N_NON_CANONICAL = 10; |
|---|
| 52 | static const int nonCanonicalPairs[N_NON_CANONICAL]; |
|---|
| 53 | static const int i2nt[NCODE]; |
|---|
| 54 | static const int nt2i[256]; |
|---|
| 55 | static const bool isCanonicalBasePair[NPBASE]; |
|---|
| 56 | |
|---|
| 57 | static bool IsValidCode(const int& c) {return A_CODE <= c && c <= GAP_CODE;} |
|---|
| 58 | static bool IsBaseCode(const int& c) {return (A_CODE <= c && c <= U_CODE);} |
|---|
| 59 | static bool IsBasePairCode(const int& c){return (AA_CODE <= c && c <= UU_CODE);} |
|---|
| 60 | static bool IsReducedBasePairCode(const int& c) { |
|---|
| 61 | return (REDUCED_AU_CODE <= c && c <= REDUCED_MM_CODE); |
|---|
| 62 | } |
|---|
| 63 | static bool IsAmbiguousCode(const int& c) {return c == N_CODE;} |
|---|
| 64 | static bool IsGapCode(const int& c) {return c == GAP_CODE;} |
|---|
| 65 | static bool IsValidChar(const unsigned char& c) { |
|---|
| 66 | return IsValidCode(nt2i[c]); |
|---|
| 67 | } |
|---|
| 68 | static bool IsBaseChar(const int& c) {return IsBaseCode(nt2i[c]);} |
|---|
| 69 | static bool IsAmbiguousChar(const int& c) {return IsAmbiguousCode(nt2i[c]);} |
|---|
| 70 | static bool IsGapChar(const int& c) {return IsGapCode(nt2i[c]);} |
|---|
| 71 | static int nt2code(const int& nt) { |
|---|
| 72 | if (0 <= nt && nt < 256) { |
|---|
| 73 | return nt2i[nt]; |
|---|
| 74 | } else { |
|---|
| 75 | return INVALID_CODE; |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | static int getPairCode(const int& c, const int& c1) { |
|---|
| 79 | return (IsBaseCode(c) && IsBaseCode(c1) |
|---|
| 80 | ? ((c << BASEBIT) | c1) |
|---|
| 81 | : INVALID_PAIR_CODE); |
|---|
| 82 | } |
|---|
| 83 | static bool isValidPairCode(const int& pairCode) { |
|---|
| 84 | return (0 <= pairCode && pairCode < NPBASE); |
|---|
| 85 | } |
|---|
| 86 | static void pair2Bases(const int& pairCode, int& c, int& c1) { |
|---|
| 87 | //Assert(IsBasePairCode(pairCode)); |
|---|
| 88 | c1 = pairCode & BASEMSK; |
|---|
| 89 | c = (pairCode >> BASEBIT) & BASEMSK; |
|---|
| 90 | } |
|---|
| 91 | static int getReducedPairCode(const int& c, const int& c1) { |
|---|
| 92 | return reducePairCode(getPairCode(c, c1)); |
|---|
| 93 | } |
|---|
| 94 | static int reducePairCode(const int& pairCode) { |
|---|
| 95 | static const int table[NPBASE] = { |
|---|
| 96 | REDUCED_MM_CODE, REDUCED_MM_CODE, REDUCED_MM_CODE, REDUCED_AU_CODE, |
|---|
| 97 | REDUCED_MM_CODE, REDUCED_MM_CODE, REDUCED_CG_CODE, REDUCED_MM_CODE, |
|---|
| 98 | REDUCED_MM_CODE, REDUCED_GC_CODE, REDUCED_MM_CODE, REDUCED_GU_CODE, |
|---|
| 99 | REDUCED_UA_CODE, REDUCED_MM_CODE, REDUCED_UG_CODE, REDUCED_MM_CODE, |
|---|
| 100 | }; |
|---|
| 101 | return (IsBasePairCode(pairCode) |
|---|
| 102 | ? table[pairCode] |
|---|
| 103 | : REDUCED_INVALID_PAIR_CODE); |
|---|
| 104 | } |
|---|
| 105 | static bool isValidReducedPairCode(const int& pairCode) { |
|---|
| 106 | return (0 <= pairCode && pairCode < REDUCED_NPBASE); |
|---|
| 107 | } |
|---|
| 108 | static bool isCanonicalReducedPairCode(const int& pairCode) { |
|---|
| 109 | return (REDUCED_AU_CODE <= pairCode |
|---|
| 110 | && pairCode <= REDUCED_UG_CODE); |
|---|
| 111 | } |
|---|
| 112 | static int flipReducedPairCode(const int& reducedPairCode) { |
|---|
| 113 | static const int table[REDUCED_NPBASE + 1] = { |
|---|
| 114 | REDUCED_UA_CODE, |
|---|
| 115 | REDUCED_GC_CODE, |
|---|
| 116 | REDUCED_CG_CODE, |
|---|
| 117 | REDUCED_UG_CODE, |
|---|
| 118 | REDUCED_AU_CODE, |
|---|
| 119 | REDUCED_GU_CODE, |
|---|
| 120 | REDUCED_MM_CODE, |
|---|
| 121 | REDUCED_INVALID_PAIR_CODE |
|---|
| 122 | }; |
|---|
| 123 | return (IsReducedBasePairCode(reducedPairCode) |
|---|
| 124 | ? table[reducedPairCode] : table[REDUCED_NPBASE]); |
|---|
| 125 | } |
|---|
| 126 | static void seq2i(char* s, const char* t, int len) { |
|---|
| 127 | const char* const s_end = s + len; |
|---|
| 128 | while (s < s_end) *s++ = nt2i[(unsigned) (*t++)]; |
|---|
| 129 | } |
|---|
| 130 | static void i2seq(char* s, const char* t, int len) { |
|---|
| 131 | const char* const s_end = s + len; |
|---|
| 132 | while (s < s_end) *s++ = i2nt[(unsigned) (*t++)]; |
|---|
| 133 | } |
|---|
| 134 | static void i2seq(ostream& fo, const char* t, int len) { |
|---|
| 135 | const char* const t_end = t + len; |
|---|
| 136 | while (t < t_end) fo << (char) i2nt[(unsigned) (*t++)]; |
|---|
| 137 | } |
|---|
| 138 | static char* wd2str(unsigned int wdSize, unsigned int wd) { |
|---|
| 139 | const unsigned int MAX_WD_SIZE = (sizeof(unsigned int) * 8 / BASEBIT); |
|---|
| 140 | static char buf[MAX_WD_SIZE + 1] = {}; |
|---|
| 141 | //Assert(wdSize <= MAX_WD_SIZE); |
|---|
| 142 | |
|---|
| 143 | char* s = buf + wdSize; |
|---|
| 144 | *s = '\0'; |
|---|
| 145 | do { |
|---|
| 146 | *(--s) = Beta::i2nt[wd & BASEMSK]; |
|---|
| 147 | wd >>= BASEBIT; |
|---|
| 148 | } while (s > buf); |
|---|
| 149 | return buf; |
|---|
| 150 | } |
|---|
| 151 | static void printWd(ostream& fo, unsigned int wdSize, unsigned int wd) { |
|---|
| 152 | fo << wd2str(wdSize, wd); |
|---|
| 153 | } |
|---|
| 154 | static const char* code2str(const int& code) { |
|---|
| 155 | static const char table[NBASENG+1][2] = { |
|---|
| 156 | "A", "C", "G", "U", "N", ".", "?" |
|---|
| 157 | }; |
|---|
| 158 | return ((A_CODE <= code && code <= GAP_CODE) |
|---|
| 159 | ? table[code] : table[NBASENG]); |
|---|
| 160 | } |
|---|
| 161 | static const char* pairCode2str(const int& pairCode) { |
|---|
| 162 | static const char table[NPBASE+1][3] = { |
|---|
| 163 | "AA", "AC", "AG", "AU", |
|---|
| 164 | "CA", "CC", "CG", "CU", |
|---|
| 165 | "GA", "GC", "GG", "GU", |
|---|
| 166 | "UA", "UC", "UG", "UU", |
|---|
| 167 | "??" |
|---|
| 168 | }; |
|---|
| 169 | return (IsBasePairCode(pairCode) ? table[pairCode] : table[NPBASE]); |
|---|
| 170 | } |
|---|
| 171 | static const char* reducedPairCode2str(const int& reducedPairCode) { |
|---|
| 172 | static const char table[REDUCED_NPBASE+1][3] = { |
|---|
| 173 | "AU", "CG", "GC", "GU", "UA", "UG", "MM", "??" |
|---|
| 174 | }; |
|---|
| 175 | return (IsReducedBasePairCode(reducedPairCode) |
|---|
| 176 | ? table[reducedPairCode] : table[REDUCED_NPBASE]); |
|---|
| 177 | } |
|---|
| 178 | static char nt2Code(const char& c){ |
|---|
| 179 | if (!IsValidChar(c)) { cerr << "character " << c << " is not valid"; } |
|---|
| 180 | |
|---|
| 181 | return (char) nt2i[(int) c]; |
|---|
| 182 | } |
|---|
| 183 | static char code2char(const int& c) { |
|---|
| 184 | static const char table[NBASENG+1] = { |
|---|
| 185 | 'A', 'C', 'G', 'U', 'N', '.', '?' |
|---|
| 186 | }; |
|---|
| 187 | return ((A_CODE <= c && c <= GAP_CODE) |
|---|
| 188 | ? table[(int) c] : table[NBASENG]); |
|---|
| 189 | } |
|---|
| 190 | /* |
|---|
| 191 | static string generateRandomRNA(const int& len) { |
|---|
| 192 | static const char nt[5] = "ACGU"; |
|---|
| 193 | string rna(len, '\0'); |
|---|
| 194 | for (int i = 0; i < len; i++) { |
|---|
| 195 | rna[i] = nt[Rand(4)]; |
|---|
| 196 | } |
|---|
| 197 | return rna; |
|---|
| 198 | } |
|---|
| 199 | */ |
|---|
| 200 | }; |
|---|
| 201 | |
|---|
| 202 | #endif |
|---|