/
aln_util.cpp
187 lines (187 loc) · 4.31 KB
/
aln_util.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
#include <string>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include "aln_idx.h"
void Ebwt::sanityCheckUpToSide(TIndexOff upToSide) const
{
assert(isInMemory());
TIndexOffU occ[] = {0, 0, 0, 0};
ASSERT_ONLY(TIndexOffU occ_save[] = {0, 0, 0, 0});
TIndexOffU cur = 0;
const EbwtParams &eh = this->_eh;
bool fw = false;
while (cur < (TIndexOffU)(upToSide * eh._sideSz))
{
assert_leq(cur + eh._sideSz, eh._ebwtTotLen);
for (uint32_t i = 0; i < eh._sideBwtSz; i++)
{
uint8_t by = this->ebwt()[cur + (fw ? i : eh._sideBwtSz - i - 1)];
for (int j = 0; j < 4; j++)
{
int twoBit = unpack_2b_from_8b(by, fw ? j : 3 - j);
occ[twoBit]++;
}
assert_eq(0, (occ[0] + occ[1] + occ[2] + occ[3]) % 4);
}
assert_eq(0, (occ[0] + occ[1] + occ[2] + occ[3]) % eh._sideBwtLen);
ASSERT_ONLY(const TIndexOffU *u_ebwt = reinterpret_cast<const TIndexOffU *>(&ebwt()[cur + eh._sideBwtSz]));
ASSERT_ONLY(TIndexOffU as = u_ebwt[0]);
ASSERT_ONLY(TIndexOffU cs = u_ebwt[1]);
ASSERT_ONLY(TIndexOffU gs = u_ebwt[2]);
ASSERT_ONLY(TIndexOffU ts = u_ebwt[3]);
assert(as == occ_save[0] || as == occ_save[0] - 1);
assert_eq(cs, occ_save[1]);
assert_eq(gs, occ_save[2]);
assert_eq(ts, occ_save[3]);
#ifndef NDEBUG
occ_save[0] = occ[0];
occ_save[1] = occ[1];
occ_save[2] = occ[2];
occ_save[3] = occ[3];
#endif
cur += eh._sideSz;
}
}
void Ebwt::sanityCheckAll(int reverse) const
{
const EbwtParams &eh = this->_eh;
assert(isInMemory());
for (TIndexOffU i = 1; i < eh._ftabLen; i++)
{
assert_geq(this->ftabHi(i), this->ftabLo(i - 1));
assert_geq(this->ftabLo(i), this->ftabHi(i - 1));
assert_leq(this->ftabHi(i), eh._bwtLen + 1);
}
assert_eq(this->ftabHi(eh._ftabLen - 1), eh._bwtLen);
TIndexOff seenLen = (eh._bwtLen + 31) >> ((TIndexOffU)5);
TIndexOff *seen;
try
{
seen = new TIndexOff[seenLen];
}
catch (bad_alloc &e)
{
cerr << "Out of memory allocating seen[] at " << __FILE__ << ":" << __LINE__ << endl;
throw e;
}
memset(seen, 0, OFF_SIZE * seenLen);
TIndexOffU offsLen = eh._offsLen;
for (TIndexOffU i = 0; i < offsLen; i++)
{
assert_lt(this->offs()[i], eh._bwtLen);
TIndexOff w = this->offs()[i] >> 5;
TIndexOff r = this->offs()[i] & 31;
assert_eq(0, (seen[w] >> r) & 1);
seen[w] |= (1 << r);
}
delete[] seen;
assert_gt(this->_nPat, 0);
for (TIndexOffU i = 0; i < this->_nPat; i++)
{
assert_geq(this->plen()[i], 0);
}
if (this->rstarts() != NULL)
{
for (TIndexOffU i = 0; i < this->_nFrag - 1; i++)
{
assert_gt(this->rstarts()[(i + 1) * 3], this->rstarts()[i * 3]);
if (reverse == REF_READ_REVERSE)
{
assert(this->rstarts()[(i * 3) + 1] >= this->rstarts()[((i + 1) * 3) + 1]);
}
else
{
assert(this->rstarts()[(i * 3) + 1] <= this->rstarts()[((i + 1) * 3) + 1]);
}
}
}
sanityCheckUpToSide(eh._numSides);
VMSG_NL("Ebwt::sanityCheck passed");
}
void Ebwt::restore(SString<char> &s) const
{
assert(isInMemory());
s.resize(this->_eh._len);
TIndexOffU jumps = 0;
TIndexOffU i = this->_eh._len;
SideLocus l(i, this->_eh, this->ebwt());
while (i != _zOff)
{
assert_lt(jumps, this->_eh._len);
TIndexOffU newi = mapLF(l ASSERT_ONLY(, false));
assert_neq(newi, i);
s[this->_eh._len - jumps - 1] = rowL(l);
i = newi;
l.initFromRow(i, this->_eh, this->ebwt());
jumps++;
}
assert_eq(jumps, this->_eh._len);
}
void Ebwt::checkOrigs(
const EList<SString<char>> &os,
bool color,
bool mirror) const
{
SString<char> rest;
restore(rest);
TIndexOffU restOff = 0;
size_t i = 0, j = 0;
if (mirror)
{
return;
}
while (i < os.size())
{
size_t olen = os[i].length();
int lastorig = -1;
for (; j < olen; j++)
{
size_t joff = j;
if (mirror)
joff = olen - j - 1;
if ((int)os[i][joff] == 4)
{
lastorig = -1;
if (!mirror)
{
while (j < olen && (int)os[i][j] == 4)
j++;
}
else
{
while (j < olen && (int)os[i][olen - j - 1] == 4)
j++;
}
j--;
continue;
}
if (lastorig == -1 && color)
{
lastorig = os[i][joff];
continue;
}
if (color)
{
assert_neq(-1, lastorig);
assert_eq(dinuc2color[(int)os[i][joff]][lastorig], rest[restOff]);
}
else
{
assert_eq(os[i][joff], rest[restOff]);
}
lastorig = (int)os[i][joff];
restOff++;
}
if (j == os[i].length())
{
i++;
j = 0;
}
else
{
}
}
}