-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathShiftAnd_unittest.cpp
357 lines (272 loc) · 9.7 KB
/
ShiftAnd_unittest.cpp
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
353
354
355
356
357
// Metal - A fast methylation alignment and calling tool for WGBS data.
// Copyright (C) 2017 Jonas Fischer
//
// This program 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.
//
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
//
// Jonas Fischer [email protected]
#include "gtest/gtest.h"
#include "ShiftAnd.h"
// test fixture
class ShiftAnd_test : public testing::Test {
protected:
virtual void SetUp()
{
lmap['A'] = 0;
lmap['C'] = 1;
lmap['G'] = 2;
lmap['T'] = 3;
}
std::array<uint8_t, 256> lmap;
};
// tests the reset function for resetting the set of active states
// which are represented by bitvectors internally
TEST_F(ShiftAnd_test, reset)
{
std::string seq = "ACCATGTGACTGCATG";
ShiftAnd<0> sa0(seq, lmap);
sa0.active[0].B_0 = 15;
sa0.active[0].B_1 = 1;
sa0.reset();
ASSERT_EQ(1, sa0.active[0].B_0);
ASSERT_EQ(0, sa0.active[0].B_1);
ShiftAnd<2> sa2(seq, lmap);
sa2.active[0].B_0 = 15;
sa2.active[0].B_1 = 1;
sa2.active[1].B_0 = 1;
sa2.active[1].B_1 = 1;
sa2.active[2].B_0 = 0;
sa2.active[2].B_1 = 1;
sa2.reset();
ASSERT_EQ(1, sa2.active[0].B_0);
ASSERT_EQ(0, sa2.active[0].B_1);
ASSERT_EQ(3, sa2.active[1].B_0);
ASSERT_EQ(0, sa2.active[1].B_1);
ASSERT_EQ(7, sa2.active[2].B_0);
ASSERT_EQ(0, sa2.active[2].B_1);
}
// tests if the bitmasks are set correctly for the simple sequence
// ACACACCCC
TEST_F(ShiftAnd_test, simpleBitmasks)
{
std::string seq = "ACACACCCC";
ShiftAnd<1> sa1(seq, lmap);
const uint64_t maskA_0 = sa1.masks[lmap['A']].B_0;
const uint64_t maskA_1 = sa1.masks[lmap['A']].B_1;
const uint64_t maskC_0 = sa1.masks[lmap['C']].B_0;
const uint64_t maskC_1 = sa1.masks[lmap['C']].B_1;
const uint64_t maskG_0 = sa1.masks[lmap['G']].B_0;
const uint64_t maskG_1 = sa1.masks[lmap['G']].B_1;
const uint64_t maskT_0 = sa1.masks[lmap['T']].B_0;
const uint64_t maskT_1 = sa1.masks[lmap['T']].B_1;
const uint64_t full = 0xffffffffffffffffULL;
ASSERT_EQ(0xfffffffffffffc2bULL, maskA_0);
ASSERT_EQ(0xffffffffffffffd5ULL, maskC_0);
ASSERT_EQ(0xfffffffffffffc01ULL, maskG_0);
ASSERT_EQ(0xfffffffffffffc01ULL, maskT_0);
ASSERT_EQ(full, maskA_1);
ASSERT_EQ(full, maskC_1);
ASSERT_EQ(full, maskG_1);
ASSERT_EQ(full, maskT_1);
ASSERT_EQ(0x0000000000000200ULL, sa1.accepted.B_0);
ASSERT_EQ(0, sa1.accepted.B_1);
}
// tests if the bitmasks are set correctly for the simple sequence
// AAAAA.......ACG
// where the last 3 letters are at position 63 to 65 in the sequence
// to test for correct splitting between the two uint64_t
TEST_F(ShiftAnd_test, simpleBitmasksOverflow)
{
std::string seq = "AAAAAAAAAA";
seq = seq + seq + seq + seq + seq + seq + "AAAACG";
ShiftAnd<1> sa1(seq, lmap);
const uint64_t maskA_0 = sa1.masks[lmap['A']].B_0;
const uint64_t maskA_1 = sa1.masks[lmap['A']].B_1;
const uint64_t maskC_0 = sa1.masks[lmap['C']].B_0;
const uint64_t maskC_1 = sa1.masks[lmap['C']].B_1;
const uint64_t maskG_0 = sa1.masks[lmap['G']].B_0;
const uint64_t maskG_1 = sa1.masks[lmap['G']].B_1;
const uint64_t maskT_0 = sa1.masks[lmap['T']].B_0;
const uint64_t maskT_1 = sa1.masks[lmap['T']].B_1;
const uint64_t full = 0xffffffffffffffffULL;
ASSERT_EQ(full, maskA_0);
ASSERT_EQ(1, maskC_0);
ASSERT_EQ(1, maskG_0);
ASSERT_EQ(1, maskT_0);
ASSERT_EQ(0xfffffffffffffff9ULL, maskA_1);
ASSERT_EQ(0xfffffffffffffffaULL, maskC_1);
ASSERT_EQ(0xfffffffffffffffcULL, maskG_1);
ASSERT_EQ(0xfffffffffffffff8ULL, maskT_1);
ASSERT_EQ(0, sa1.accepted.B_0);
ASSERT_EQ(0x0000000000000004ULL, sa1.accepted.B_1);
}
// tests if too long patterns are still correctly processed
TEST_F(ShiftAnd_test, bitmaskLongPattern)
{
std::string seq = "GGGGGGGGGGGGGGGG";
seq += seq;
seq += seq;
std::string seq2 = "CCCCCCCCCCCCCCCC";
seq2 += seq2;
seq2 += seq2;
// after the following line, we reached 128 letters
seq += seq2;
seq += "AAA";
ShiftAnd<2> sa2(seq, lmap);
const uint64_t maskA_0 = sa2.masks[lmap['A']].B_0;
const uint64_t maskA_1 = sa2.masks[lmap['A']].B_1;
const uint64_t maskC_0 = sa2.masks[lmap['C']].B_0;
const uint64_t maskC_1 = sa2.masks[lmap['C']].B_1;
const uint64_t maskG_0 = sa2.masks[lmap['G']].B_0;
const uint64_t maskG_1 = sa2.masks[lmap['G']].B_1;
const uint64_t maskT_0 = sa2.masks[lmap['T']].B_0;
const uint64_t maskT_1 = sa2.masks[lmap['T']].B_1;
const uint64_t full = 0xffffffffffffffffULL;
ASSERT_EQ(1, maskA_0);
ASSERT_EQ(1, maskC_0);
ASSERT_EQ(full, maskG_0);
ASSERT_EQ(1, maskT_0);
ASSERT_EQ(0, maskA_1);
ASSERT_EQ(0xfffffffffffffffeULL, maskC_1);
ASSERT_EQ(1, maskG_1);
ASSERT_EQ(0, maskT_1);
ASSERT_EQ(0, sa2.accepted.B_0);
ASSERT_EQ(0x8000000000000000ULL, sa2.accepted.B_1);
}
// tests if the bitmasks are set correctly for the sequence
// ATTATTTCCC
// to look if WGBS specific behaviour is achieved
TEST_F(ShiftAnd_test, bitmaskBisulfite)
{
std::string seq = "ATTATTTCCC";
ShiftAnd<1> sa1(seq, lmap);
const uint64_t maskA_0 = sa1.masks[lmap['A']].B_0;
const uint64_t maskC_0 = sa1.masks[lmap['C']].B_0;
const uint64_t maskG_0 = sa1.masks[lmap['G']].B_0;
const uint64_t maskT_0 = sa1.masks[lmap['T']].B_0;
ASSERT_EQ(0xfffffffffffff813ULL, maskA_0);
ASSERT_EQ(0xffffffffffffffedULL, maskC_0);
ASSERT_EQ(0xfffffffffffff801ULL, maskG_0);
ASSERT_EQ(0xfffffffffffff8edULL, maskT_0);
}
// simple matching test with
// same sequence for text as for pattern
// and with one substitution
TEST_F(ShiftAnd_test, matching_same)
{
// string of size 20
std::string seq = "AAAAAAAAAAAAAAAAAAAA";
seq = seq + seq + seq;
seq += seq;
seq += "TTTTTTT";
std::vector<char> t (seq.begin(), seq.end());
// don't allow errors
ShiftAnd<0> sa0(seq, lmap);
// allow single error
ShiftAnd<1> sa1(seq, lmap);
// query sequence
std::vector<uint64_t> matchings0;
std::vector<uint8_t> errors0;
sa0.querySeq(t.begin(), t.end(), matchings0, errors0);
std::vector<uint64_t> matchings1;
std::vector<uint8_t> errors1;
sa1.querySeq(t.begin(), t.end(), matchings1, errors1);
// check the accepting masks
ASSERT_EQ(0, sa0.accepted.B_0);
ASSERT_EQ(0, sa1.accepted.B_0);
ASSERT_EQ(0x8000000000000000ULL, sa0.accepted.B_1);
ASSERT_EQ(0x8000000000000000ULL, sa1.accepted.B_1);
// check size of matchings
ASSERT_EQ(1, matchings0.size());
ASSERT_EQ(1, matchings1.size());
ASSERT_EQ(126, matchings0[0]);
// ASSERT_EQ(125, matchings1[0]);
ASSERT_EQ(126, matchings1[0]);
// check the mismatches
ASSERT_EQ(1, errors0.size());
ASSERT_EQ(1, errors1.size());
ASSERT_EQ(0, errors0[0]);
// ASSERT_EQ(1, errors1[0]);
ASSERT_EQ(0, errors1[0]);
// Exchange a letter to produce mismatch
t[5] = 'C';
matchings0.clear();
matchings1.clear();
errors0.clear();
errors1.clear();
sa0.querySeq(t.begin(), t.end(), matchings0, errors0);
sa1.querySeq(t.begin(), t.end(), matchings1, errors1);
ASSERT_EQ(0, matchings0.size());
ASSERT_EQ(1, matchings1.size());
ASSERT_EQ(126, matchings1[0]);
ASSERT_EQ(1, errors1[0]);
}
// simple matching test of small pattern in larger sequence
// p = AGGCGAGGC
// t = AGGCGAGGCGAAGCGAGGC
TEST_F(ShiftAnd_test, matching_smaller)
{
// init pattern and text
std::string p = "AGGCGAGGC";
std::string tSeq = "AGGCGAGGCGAAGCGAGGC";
std::vector<char> t (tSeq.begin(), tSeq.end());
// allow single error
ShiftAnd<1> sa1(p, lmap);
// query the text to automata
std::vector<uint64_t> matchings;
std::vector<uint8_t> errors;
sa1.querySeq(t.begin(), t.end(), matchings, errors);
// we should have one full match at pos 8, one with deletion at pos 7,
// one with insertion at pos 9,
// one with substitution 13,
// another one with deletion at 18
ASSERT_EQ(3, matchings.size());
ASSERT_EQ(3, errors.size());
ASSERT_EQ(8, matchings[0]);
ASSERT_EQ(0, errors[0]);
ASSERT_EQ(13, matchings[1]);
ASSERT_EQ(1, errors[1]);
ASSERT_EQ(18, matchings[2]);
ASSERT_EQ(1, errors[2]);
// query only last part
matchings.clear();
errors.clear();
sa1.querySeq(t.begin() + 11, t.end(), matchings, errors);
ASSERT_EQ(7, matchings[0]);
ASSERT_EQ(1, errors[0]);
}
// simple matching test for pattern with Ts against text with Cs
TEST_F(ShiftAnd_test, matching_bisulfite)
{
std::string p = "AGGTTATTC";
std::string tSeq = "AGGTCACCCAAT";
std::vector<char> t (tSeq.begin(), tSeq.end());
ShiftAnd<1> sa1(p, lmap);
std::vector<uint64_t> matchings;
std::vector<uint8_t> errors;
sa1.querySeq(t.begin(), t.end(), matchings, errors);
ASSERT_EQ(1, matchings.size());
ASSERT_EQ(1, errors.size());
ASSERT_EQ(8, matchings[0]);
ASSERT_EQ(0, errors[0]);
// introduce a substitution error
t[4]= 'G';
matchings.clear();
errors.clear();
sa1.querySeq(t.begin(), t.end(), matchings, errors);
ASSERT_EQ(1, matchings.size());
ASSERT_EQ(1, errors.size());
ASSERT_EQ(8, matchings[0]);
ASSERT_EQ(1, errors[0]);
}
// TODO long test?