-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathCompressedA3M.cpp
More file actions
361 lines (305 loc) · 9.9 KB
/
CompressedA3M.cpp
File metadata and controls
361 lines (305 loc) · 9.9 KB
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
358
359
360
361
/*
* extractA3M adapted from HH-suite 3.0: a3m_compress.h
* Original Author: meiermark
* Relicensed with permission from jsoeding to MIT
*/
#include "CompressedA3M.h"
#include "DBConcat.h"
#include <sstream>
void readU16(const char **ptr, uint16_t &result) {
unsigned char array[2];
array[0] = (unsigned char) (**ptr);
(*ptr)++;
array[1] = (unsigned char) (**ptr);
(*ptr)++;
result = array[0] | (array[1] << 8);
}
void readU32(const char **ptr, uint32_t &result) {
unsigned char array[4];
array[0] = (unsigned char) (**ptr);
(*ptr)++;
array[1] = (unsigned char) (**ptr);
(*ptr)++;
array[2] = (unsigned char) (**ptr);
(*ptr)++;
array[3] = (unsigned char) (**ptr);
(*ptr)++;
result = array[0] | (array[1] << 8) | (array[2] << 16) | (array[3] << 24);
}
std::string CompressedA3M::extractA3M(const char *data, size_t data_size,
DBReader<KeyType>& sequenceReader,
DBReader<KeyType>& headerReader, int thread_idx) {
std::ostringstream output;
//read stuff till compressed part
char last_char = '\0';
size_t index = 0;
size_t consensus_length = 0;
char inConsensus = 0;
//handle commentary line
if ((*data) == '#') {
while ((*data) != '\n') {
output.put((*data));
last_char = (*data);
data++;
index++;
}
output.put('\n');
last_char = '\n';
data++;
index++;
}
while (!(last_char == '\n' && (*data) == ';') && index < data_size) {
if ((*data) == '\n') {
inConsensus++;
}
else if (inConsensus == 1) {
consensus_length++;
}
output.put((*data));
last_char = (*data);
data++;
index++;
}
//get past ';'
data++;
index++;
while (index < data_size - 1) {
uint32_t entry_index;
uint16_t nr_blocks;
uint16_t start_pos;
readU32(&data, entry_index);
index += 4;
std::string sequence = sequenceReader.getData(entry_index, thread_idx);
std::string header = headerReader.getData(entry_index, thread_idx);
// make sure we always have a valid fasta prefix
if (header[0] != '>') {
output.put('>');
}
output.write(header.c_str(), header.length() - 1);
output.put('\n');
readU16(&data, start_pos);
index += 2;
readU16(&data, nr_blocks);
index += 2;
size_t actual_pos = start_pos;
size_t alignment_length = 0;
for (unsigned short int block_index = 0; block_index < nr_blocks; block_index++) {
unsigned char nr_matches = (unsigned char) (*data);
data++;
index++;
for (int i = 0; i < nr_matches; i++) {
output.put(sequence[actual_pos - 1]);
actual_pos++;
alignment_length++;
}
char nr_insertions_deletions = (*data);
data++;
index++;
if (nr_insertions_deletions > 0) {
for (int i = 0; i < nr_insertions_deletions; i++) {
output.put(tolower(sequence[actual_pos - 1]));
actual_pos++;
}
}
else {
for (int i = 0; i < -nr_insertions_deletions; i++) {
output.put('-');
alignment_length++;
}
}
}
while (alignment_length < consensus_length) {
output.put('-');
alignment_length++;
}
output.put('\n');
}
return output.str();
}
void CompressedA3M::extractMatcherResults(KeyType &key, std::vector<Matcher::result_t> &results,
const char *data, size_t dataSize, DBReader<KeyType> &sequenceReader, bool skipFirst) {
//read stuff till compressed part
char lastChar = '\0';
size_t index = 0;
//handle commentary line
if ((*data) == '#') {
while ((*data) != '\n') {
data++;
index++;
}
lastChar = '\n';
data++;
index++;
}
char inConsensus = 0;
size_t consensusLength = 0;
while (!(lastChar == '\n' && (*data) == ';') && index < dataSize) {
if ((*data) == '\n') {
inConsensus++;
}
else if (inConsensus == 1) {
consensusLength++;
}
lastChar = (*data);
data++;
index++;
}
std::string backtrace;
backtrace.reserve(consensusLength);
//get past ';'
data++;
index++;
size_t qLen = 0;
bool isFirst = true;
while (index < dataSize - 1) {
Matcher::result_t match;
match.seqId = 0;
match.score = 0;
match.eval = 0;
uint32_t entryIndex;
readU32(&data, entryIndex);
index += 4;
match.dbKey = sequenceReader.getDbKey(entryIndex);
if (isFirst) {
key = match.dbKey;
qLen = sequenceReader.getSeqLen(entryIndex);
match.qLen = match.dbLen = qLen;
} else {
match.qLen = qLen;
match.dbLen = sequenceReader.getSeqLen(entryIndex);
}
unsigned short int startPos;
readU16(&data, startPos);
index += 2;
match.dbStartPos = startPos - 1;
unsigned short int nrBlocks;
readU16(&data, nrBlocks);
index += 2;
if (skipFirst && isFirst) {
data += 2 * nrBlocks;
index += 2 * nrBlocks;
isFirst = false;
continue;
}
match.qStartPos = 0;
unsigned int qAlnLength = 0;
unsigned int dbAlnLength = 0;
bool firstBlockM = false;
for (unsigned short int blockIdx = 0; blockIdx < nrBlocks; blockIdx++) {
unsigned char matchCount = (unsigned char) (*data);
data++;
index++;
qAlnLength += matchCount;
dbAlnLength += matchCount;
backtrace.append(matchCount, 'M');
if (matchCount != 0) {
firstBlockM = true;
}
signed char inDelCount = (*data);
data++;
index++;
if (firstBlockM == false) {
match.qStartPos -= inDelCount;
} else {
if (inDelCount > 0) {
backtrace.append(inDelCount, 'D');
qAlnLength += inDelCount;
} else if (inDelCount < 0) {
backtrace.append(-inDelCount, 'I');
dbAlnLength -= inDelCount;
}
}
}
match.backtrace = backtrace;
match.qEndPos = match.qStartPos + dbAlnLength - 1;
match.dbEndPos = match.dbStartPos + qAlnLength - 1;
results.emplace_back(match);
backtrace.clear();
}
}
//#define CA3M_DEBUG
void CompressedA3M::hitToBuffer(unsigned int targetId, const Matcher::result_t& hit, std::string& buffer) {
#ifndef CA3M_DEBUG
buffer.append(reinterpret_cast<const char *>(&targetId), sizeof(unsigned int));
#else
buffer.append(SSTR((int)targetId));
buffer.append(1, '\t');
#endif
// Starts at 1 in ca3m format
unsigned short int startPos = hit.dbStartPos + 1;
#ifndef CA3M_DEBUG
buffer.append(reinterpret_cast<const char *>(&startPos), sizeof(unsigned short));
#else
buffer.append(SSTR((int)startPos));
buffer.append(1, '\t');
#endif
unsigned int nbOfBlocks = 0;
size_t countPos = buffer.size();
#ifndef CA3M_DEBUG
buffer.append(sizeof(unsigned short), '\0');
#endif
// add the deletion at the begining of the sequence (local alignment)
unsigned int firstGap = hit.qStartPos;
// need to make as many 127-long deletion blocks as needed
while (firstGap) {
unsigned int gapToWrite = -std::min((unsigned int) (127), firstGap);
#ifndef CA3M_DEBUG
// no match in this block
buffer.append(sizeof(char), '\0');
// only gaps
buffer.append(reinterpret_cast<const char *>(&gapToWrite), sizeof(char));
#else
buffer.append(SSTR((int)0));
buffer.append(1, '\t');
buffer.append(SSTR((int)gapToWrite));
buffer.append(1, '\t');
#endif
firstGap -= -gapToWrite;
nbOfBlocks++;
}
// detect the blocks
for (size_t btIndex = 0; btIndex < hit.backtrace.size();) {
// seek the next insertion or deletion
unsigned char matchLen = 0;
while (btIndex < hit.backtrace.size() && hit.backtrace[btIndex] == 'M' && matchLen < 255) {
btIndex++;
matchLen++;
}
#ifndef CA3M_DEBUG
buffer.append(reinterpret_cast<const char *>(&matchLen), sizeof(unsigned char));
#else
buffer.append(SSTR((int)matchLen));
buffer.append(1, '\t');
#endif
// end of block because an I or D was found
char inOrDel = 0;
if (btIndex < hit.backtrace.size() && hit.backtrace[btIndex] != 'M') {
// store whether it is I or D
inOrDel = hit.backtrace[btIndex];
}
// seek the next match
unsigned char indelLen = 0;
while (btIndex < hit.backtrace.size() && hit.backtrace[btIndex] == inOrDel && indelLen < 127) {
btIndex++;
indelLen++;
}
// deletions must be counted backwards
// deletion states are insertion states from the perspective of the aligned sequence
if (indelLen && inOrDel == 'I') {
indelLen *= -1;
}
#ifndef CA3M_DEBUG
buffer.append(sizeof(char), indelLen);
#else
buffer.append(SSTR((int)indelLen));
buffer.append(1, '\t');
#endif
nbOfBlocks++;
}
#ifndef CA3M_DEBUG
buffer.replace(countPos, sizeof(unsigned short), reinterpret_cast<const char *>(&nbOfBlocks), sizeof(unsigned short));
#else
buffer.append(SSTR((int)nbOfBlocks));
buffer.append(1, '\n');
#endif
}