-
Notifications
You must be signed in to change notification settings - Fork 4
/
sequence_model.h
352 lines (247 loc) · 8.3 KB
/
sequence_model.h
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* Copyright (C) 2009-2012 Simon A. Berger
*
* This file is part of papara.
*
* papara is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* papara is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with papara. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SEQUENCE_MODEL_H_
#define SEQUENCE_MODEL_H_
#include <stdint.h>
#include <cctype>
#include <cstddef>
#include <cassert>
#include <iostream>
#include <stdexcept>
#include <vector>
#include "ivymike/algorithm.h"
#ifndef _MSC_VER
template<typename T>
size_t popcount( T v ) {
return __builtin_popcount(v);
}
#else
#include <intrin.h>
inline size_t popcount( unsigned short v ) {
return __popcnt16(v);
}
inline size_t popcount( unsigned int v ) {
return __popcnt(v);
}
//inline size_t popcount( unsigned __int64 v ) {
// return __popcnt64(v);
//}
#endif
namespace sequence_model {
class illegal_character : public std::runtime_error {
public:
illegal_character( const char *msg, int c ) : runtime_error(msg), c_(c) {}
int c_;
};
class tag_dna;
class tag_dna4;
class tag_aa;
template<typename TAG>
class model {
//static uint8_t normalize( uint8_t c );
};
template<>
class model<tag_dna> {
public:
typedef uint8_t pars_state_t;
const static std::vector<char> inverse_meaning;
// const static std::vector<pars_state_t> bit_vector;
static uint8_t normalize( size_t xc ) {
assert( xc <= 255 );
int c = int(xc);
c = std::toupper(c);
switch( c ) {
case 'U':
return 'T';
case 'N':
case '?':
case '.':
return '-';
default:
return c;
}
}
static bool is_known_sstate( size_t c ) {
return std::find(inverse_meaning.begin(), inverse_meaning.end(), c ) != inverse_meaning.end();
}
static pars_state_t s2p( size_t c ) {
c = normalize(c);
ptrdiff_t idx = std::distance(inverse_meaning.begin(),
std::find(inverse_meaning.begin(), inverse_meaning.end(), c ) );
assert( idx >= 0 );
if( size_t(idx) >= inverse_meaning.size() ) {
//std::cerr << "illegal character: " << int(c) << "\n";
throw illegal_character( "illegal character in DNA/RNA sequence", int(c));
}
return pars_state_t(idx);
}
static uint8_t s2c( size_t c ) {
c = normalize(c);
ptrdiff_t idx = std::distance(inverse_meaning.begin(),
std::find(inverse_meaning.begin(), inverse_meaning.end(), c ) );
assert( idx >= 0 );
if( size_t(idx) >= inverse_meaning.size() ) {
throw illegal_character( "illegal character in DNA/RNA sequence", int(c));
}
return uint8_t(idx); // safe because of the check above
}
static pars_state_t c2p( size_t c ) {
return pars_state_t(c);
}
static uint8_t p2s( pars_state_t c ) {
return inverse_meaning.at(c);
}
static inline bool pstate_is_single(pars_state_t ps) {
return !pstate_is_gap(ps) && ps != 0;
}
static inline bool pstate_is_gap(pars_state_t ps) {
return ps == gap_pstate();
}
static inline bool cstate_is_gap( uint8_t cs) {
return pstate_is_gap(c2p(cs));
}
static inline bool cstate_is_single( size_t cs) {
return pstate_is_single(c2p(cs));
}
static inline pars_state_t gap_pstate() {
return pars_state_t(inverse_meaning.size() - 1);
}
static inline size_t num_cstates() {
return inverse_meaning.size();
}
};
template<>
class model<tag_dna4> {
public:
typedef uint8_t pars_state_t;
const static std::vector<char> inverse_meaning;
// const static std::vector<pars_state_t> bit_vector;
static uint8_t normalize( size_t xc ) {
assert( xc <= 255 );
int c = int(xc);
c = std::toupper(c);
switch( c ) {
case 'U':
return 'T';
case 'N':
case '?':
case '.':
return '-';
default:
return c;
}
}
static bool sstate_is_character( uint8_t c ) {
c = normalize(c);
ptrdiff_t idx = std::distance(inverse_meaning.begin(),
std::find(inverse_meaning.begin(), inverse_meaning.end(), c ) );
return idx >= 0 && idx <= gap_cstate();
}
static uint8_t s2c( size_t c ) {
c = normalize(c);
ptrdiff_t idx = std::distance(inverse_meaning.begin(),
std::find(inverse_meaning.begin(), inverse_meaning.end(), c ) );
assert( idx >= 0 );
if( size_t(idx) >= inverse_meaning.size() ) {
throw illegal_character( "illegal character in DNA/RNA sequence", int(c));
}
return uint8_t(idx); // safe because of the check above
}
static uint8_t c2s( size_t c ) {
return inverse_meaning.at(c);
}
static inline uint8_t gap_cstate() {
return uint8_t(inverse_meaning.size() - 1);
}
static inline bool cstate_is_gap( uint8_t cs) {
return cs == gap_cstate();
}
static inline size_t num_cstates() {
return inverse_meaning.size();
}
};
// FIXME: fake aa model
template<>
class model<tag_aa> {
public:
typedef uint32_t pars_state_t;
// const static char inverseMeaningPROT[23];
// const static unsigned int bitVectorAA[23];
const static std::vector<char> inverse_meaning;
const static std::vector<unsigned int> bit_vector;
static inline uint8_t normalize( size_t xc ) {
assert( xc <= 255 );
int c = int(xc);
return std::toupper(c);
}
static bool is_known_sstate( size_t c ) {
return std::find(inverse_meaning.begin(), inverse_meaning.end(), c ) != inverse_meaning.end();
}
static pars_state_t s2p( size_t c ) {
c = normalize(c);
ptrdiff_t idx = std::distance(inverse_meaning.begin(),
std::find(inverse_meaning.begin(), inverse_meaning.end(), c ) );
// std::cout << idx << "\n";
// TODO: is there any reason to use more verbose range checking than '.at'?
return bit_vector.at(idx);
}
static uint8_t s2c( size_t c ) {
c = normalize(c);
ptrdiff_t idx = std::distance(inverse_meaning.begin(),
std::find(inverse_meaning.begin(), inverse_meaning.end(), c ) );
assert( idx >= 0 );
if( size_t(idx) >= inverse_meaning.size() ) {
throw illegal_character( "illegal character in DNA/RNA sequence", int(c));
}
return uint8_t(idx);
}
static pars_state_t c2p( size_t c ) {
return bit_vector.at(c);
}
static uint8_t p2s( pars_state_t c ) {
ptrdiff_t idx = std::distance(bit_vector.begin(),
std::find(bit_vector.begin(), bit_vector.end(), c ) );
assert( idx >= 0 );
if( idx >= ptrdiff_t(inverse_meaning.size()) ) {
return 'X'; // parsimony state not representable as sequence character.
}
return inverse_meaning.at(idx);
}
static inline bool pstate_is_single(pars_state_t ps) {
return popcount(ps) == 1;
}
static inline bool pstate_is_gap(pars_state_t ps) {
return ps == gap_pstate();
}
static inline bool cstate_is_gap( size_t cs) {
return pstate_is_gap(c2p(cs));
// return cs == inverse_meaning.size() - 1;
}
static inline bool cstate_is_single( size_t cs) {
return pstate_is_single(c2p(cs));
}
static inline pars_state_t gap_pstate() {
return bit_vector.back(); // by convention the last element of bit_vector is the gap state
}
static inline size_t num_cstates() {
return inverse_meaning.size();
}
};
}
#endif /* SEQUENCE_MODEL_H_ */