forked from BenLangmead/bowtie2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mask.h
79 lines (71 loc) · 2.12 KB
/
mask.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
/*
* Copyright 2011, Ben Langmead <[email protected]>
*
* This file is part of Bowtie 2.
*
* Bowtie 2 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.
*
* Bowtie 2 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 Bowtie 2. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MASK_H_
#define MASK_H_
#include <iostream>
#include "random_source.h"
// 5-bit pop count
extern int alts5[32];
// Index of lowest set bit
extern int firsts5[32];
/**
* Return 1 if a 2-bit-encoded base ('i') matches any bit in the mask ('j') and
* the mask < 16. Returns -1 if either the reference or the read character was
* ambiguous. Returns 0 if the characters unambiguously mismatch.
*/
static inline int matchesEx(int i, int j) {
if(j >= 16 || i > 3) {
// read and/or ref was ambiguous
return -1;
}
return (((1 << i) & j) != 0) ? 1 : 0;
}
/**
* Return 1 if a 2-bit-encoded base ('i') matches any bit in the mask ('j').
*/
static inline bool matches(int i, int j) {
return ((1 << i) & j) != 0;
}
/**
* Given a mask with up to 5 bits, return an index corresponding to a
* set bit in the mask, randomly chosen from among all set bits.
*/
static inline int randFromMask(RandomSource& rnd, int mask) {
assert_gt(mask, 0);
if(alts5[mask] == 1) {
// only one to pick from, pick it via lookup table
return firsts5[mask];
}
assert_gt(mask, 0);
assert_lt(mask, 32);
int r = rnd.nextU32() % alts5[mask];
assert_geq(r, 0);
assert_lt(r, alts5[mask]);
// could do the following via lookup table too
for(int i = 0; i < 5; i++) {
if((mask & (1 << i)) != 0) {
if(r == 0) return i;
r--;
}
}
std::cerr << "Shouldn't get here" << std::endl;
throw 1;
return -1;
}
#endif /*ndef MASK_H_*/