-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssw_cpp.cpp
477 lines (393 loc) · 13.4 KB
/
ssw_cpp.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// ssw_cpp.cpp
// Created by Wan-Ping Lee
// Last revision by Mengyao Zhao on 2017-05-30
#include "ssw_cpp.h"
#include "ssw.h"
#include <sstream>
namespace {
static const int8_t kBaseTranslation[128] = {
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
// A C G
4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
// T
4, 4, 4, 4, 3, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
// a c g
4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
// t
4, 4, 4, 4, 3, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
};
void BuildSwScoreMatrix(const uint8_t& match_score,
const uint8_t& mismatch_penalty,
int8_t* matrix) {
// The score matrix looks like
// // A, C, G, T, N
// score_matrix_ = { 2, -2, -2, -2, -2, // A
// -2, 2, -2, -2, -2, // C
// -2, -2, 2, -2, -2, // G
// -2, -2, -2, 2, -2, // T
// -2, -2, -2, -2, -2};// N
int id = 0;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
matrix[id] = ((i == j) ? match_score : static_cast<int8_t>(-mismatch_penalty));
++id;
}
matrix[id] = static_cast<int8_t>(-mismatch_penalty); // For N
++id;
}
for (int i = 0; i < 5; ++i)
matrix[id++] = static_cast<int8_t>(-mismatch_penalty); // For N
}
void ConvertAlignment(const s_align& s_al,
const int& query_len,
StripedSmithWaterman::Alignment* al) {
al->sw_score = s_al.score1;
al->sw_score_next_best = s_al.score2;
al->ref_begin = s_al.ref_begin1;
al->ref_end = s_al.ref_end1;
al->query_begin = s_al.read_begin1;
al->query_end = s_al.read_end1;
al->ref_end_next_best = s_al.ref_end2;
al->cigar.clear();
al->cigar_string.clear();
if (s_al.cigarLen > 0) {
std::ostringstream cigar_string;
if (al->query_begin > 0) {
uint32_t cigar = to_cigar_int(al->query_begin, 'S');
al->cigar.push_back(cigar);
cigar_string << al->query_begin << 'S';
}
for (int i = 0; i < s_al.cigarLen; ++i) {
al->cigar.push_back(s_al.cigar[i]);
cigar_string << cigar_int_to_len(s_al.cigar[i]) << cigar_int_to_op(s_al.cigar[i]);
}
int end = query_len - al->query_end - 1;
if (end > 0) {
uint32_t cigar = to_cigar_int(end, 'S');
al->cigar.push_back(cigar);
cigar_string << end << 'S';
}
al->cigar_string = cigar_string.str();
} // end if
}
// @Function:
// Calculate the length of the previous cigar operator
// and store it in new_cigar and new_cigar_string.
// Clean up in_M (false), in_X (false), length_M (0), and length_X(0).
void CleanPreviousMOperator(
bool* in_M,
bool* in_X,
uint32_t* length_M,
uint32_t* length_X,
std::vector<uint32_t>* new_cigar,
std::ostringstream* new_cigar_string) {
if (*in_M) {
uint32_t match = to_cigar_int(*length_M, '=');
new_cigar->push_back(match);
(*new_cigar_string) << *length_M << '=';
} else if (*in_X){ //in_X
uint32_t match = to_cigar_int(*length_X, 'X');
new_cigar->push_back(match);
(*new_cigar_string) << *length_X << 'X';
}
// Clean up
*in_M = false;
*in_X = false;
*length_M = 0;
*length_X = 0;
}
// @Function:
// 1. Calculate the number of mismatches.
// 2. Modify the cigar string:
// differentiate matches (M) and mismatches(X).
// @Return:
// The number of mismatches.
int CalculateNumberMismatch(
StripedSmithWaterman::Alignment* al,
int8_t const *ref,
int8_t const *query,
const int& query_len) {
ref += al->ref_begin;
query += al->query_begin;
int mismatch_length = 0;
std::vector<uint32_t> new_cigar;
std::ostringstream new_cigar_string;
if (al->query_begin > 0) {
uint32_t cigar = to_cigar_int(al->query_begin, 'S');
new_cigar.push_back(cigar);
new_cigar_string << al->query_begin << 'S';
}
bool in_M = false; // the previous is match
bool in_X = false; // the previous is mismatch
uint32_t length_M = 0;
uint32_t length_X = 0;
for (unsigned int i = 0; i < al->cigar.size(); ++i) {
char op = cigar_int_to_op(al->cigar[i]);
uint32_t length = cigar_int_to_len(al->cigar[i]);
if (op == 'M') {
for (uint32_t j = 0; j < length; ++j) {
if (*ref != *query) {
++mismatch_length;
if (in_M) { // the previous is match; however the current one is mismatche
uint32_t match = to_cigar_int(length_M, '=');
new_cigar.push_back(match);
new_cigar_string << length_M << '=';
}
length_M = 0;
++length_X;
in_M = false;
in_X = true;
} else { // *ref == *query
if (in_X) { // the previous is mismatch; however the current one is matche
uint32_t match = to_cigar_int(length_X, 'X');
new_cigar.push_back(match);
new_cigar_string << length_X << 'X';
}
++length_M;
length_X = 0;
in_M = true;
in_X = false;
} // end of if (*ref != *query)
++ref;
++query;
}
} else if (op == 'I') {
query += length;
mismatch_length += length;
CleanPreviousMOperator(&in_M, &in_X, &length_M, &length_X, &new_cigar, &new_cigar_string);
new_cigar.push_back(al->cigar[i]);
new_cigar_string << length << 'I';
} else if (op == 'D') {
ref += length;
mismatch_length += length;
CleanPreviousMOperator(&in_M, &in_X, &length_M, &length_X, &new_cigar, &new_cigar_string);
new_cigar.push_back(al->cigar[i]);
new_cigar_string << length << 'D';
}
}
CleanPreviousMOperator(&in_M, &in_X, &length_M, &length_X, &new_cigar, &new_cigar_string);
int end = query_len - al->query_end - 1;
if (end > 0) {
uint32_t cigar = to_cigar_int(end, 'S');
new_cigar.push_back(cigar);
new_cigar_string << end << 'S';
}
al->cigar_string.clear();
al->cigar.clear();
al->cigar_string = new_cigar_string.str();
al->cigar = new_cigar;
return mismatch_length;
}
void SetFlag(const StripedSmithWaterman::Filter& filter, uint8_t* flag) {
if (filter.report_begin_position) *flag |= 0x08;
if (filter.report_cigar) *flag |= 0x0f;
}
// http://www.cplusplus.com/faq/sequences/arrays/sizeof-array/#cpp
template <typename T, size_t N>
inline size_t SizeOfArray( const T(&)[ N ] )
{
return N;
}
} // namespace
namespace StripedSmithWaterman {
Aligner::Aligner(void)
: score_matrix_(NULL)
, score_matrix_size_(5)
, translation_matrix_(NULL)
, match_score_(2)
, mismatch_penalty_(2)
, gap_opening_penalty_(3)
, gap_extending_penalty_(1)
, translated_reference_(NULL)
, reference_length_(0)
{
BuildDefaultMatrix();
}
Aligner::Aligner(
const uint8_t& match_score,
const uint8_t& mismatch_penalty,
const uint8_t& gap_opening_penalty,
const uint8_t& gap_extending_penalty)
: score_matrix_(NULL)
, score_matrix_size_(5)
, translation_matrix_(NULL)
, match_score_(match_score)
, mismatch_penalty_(mismatch_penalty)
, gap_opening_penalty_(gap_opening_penalty)
, gap_extending_penalty_(gap_extending_penalty)
, translated_reference_(NULL)
, reference_length_(0)
{
BuildDefaultMatrix();
}
Aligner::Aligner(const int8_t* score_matrix,
const int& score_matrix_size,
const int8_t* translation_matrix,
const int& translation_matrix_size)
: score_matrix_(NULL)
, score_matrix_size_(score_matrix_size)
, translation_matrix_(NULL)
, match_score_(2)
, mismatch_penalty_(2)
, gap_opening_penalty_(3)
, gap_extending_penalty_(1)
, translated_reference_(NULL)
, reference_length_(0)
{
score_matrix_ = new int8_t[score_matrix_size_ * score_matrix_size_];
memcpy(score_matrix_, score_matrix, sizeof(int8_t) * score_matrix_size_ * score_matrix_size_);
translation_matrix_ = new int8_t[translation_matrix_size];
memcpy(translation_matrix_, translation_matrix, sizeof(int8_t) * translation_matrix_size);
}
Aligner::~Aligner(void){
Clear();
}
int Aligner::SetReferenceSequence(const char* seq, const int& length) {
int len = 0;
if (translation_matrix_) {
// calculate the valid length
//int calculated_ref_length = static_cast<int>(strlen(seq));
//int valid_length = (calculated_ref_length > length)
// ? length : calculated_ref_length;
int valid_length = length;
// delete the current buffer
CleanReferenceSequence();
// allocate a new buffer
translated_reference_ = new int8_t[valid_length];
len = TranslateBase(seq, valid_length, translated_reference_);
} else {
// nothing
}
reference_length_ = len;
return len;
}
int Aligner::TranslateBase(const char* bases, const int& length,
int8_t* translated) const {
const char* ptr = bases;
int len = 0;
for (int i = 0; i < length; ++i) {
translated[i] = translation_matrix_[(int) *ptr];
++ptr;
++len;
}
return len;
}
bool Aligner::Align(const char* query, const Filter& filter,
Alignment* alignment, const int32_t maskLen) const
{
if (!translation_matrix_) return false;
if (reference_length_ == 0) return false;
int query_len = strlen(query);
if (query_len == 0) return false;
int8_t* translated_query = new int8_t[query_len];
TranslateBase(query, query_len, translated_query);
const int8_t score_size = 2;
s_profile* profile = ssw_init(translated_query, query_len, score_matrix_,
score_matrix_size_, score_size);
uint8_t flag = 0;
SetFlag(filter, &flag);
s_align* s_al = ssw_align(profile, translated_reference_, reference_length_,
static_cast<int>(gap_opening_penalty_),
static_cast<int>(gap_extending_penalty_),
flag, filter.score_filter, filter.distance_filter, maskLen);
alignment->Clear();
ConvertAlignment(*s_al, query_len, alignment);
alignment->mismatches = CalculateNumberMismatch(&*alignment, translated_reference_, translated_query, query_len);
// Free memory
delete [] translated_query;
align_destroy(s_al);
init_destroy(profile);
return true;
}
bool Aligner::Align(const char* query, const char* ref, const int& ref_len,
const Filter& filter, Alignment* alignment, const int32_t maskLen) const
{
if (!translation_matrix_) return false;
int query_len = strlen(query);
if (query_len == 0) return false;
int8_t* translated_query = new int8_t[query_len];
TranslateBase(query, query_len, translated_query);
// calculate the valid length
int valid_ref_len = ref_len;
int8_t* translated_ref = new int8_t[valid_ref_len];
TranslateBase(ref, valid_ref_len, translated_ref);
const int8_t score_size = 2;
s_profile* profile = ssw_init(translated_query, query_len, score_matrix_,
score_matrix_size_, score_size);
uint8_t flag = 0;
SetFlag(filter, &flag);
s_align* s_al = ssw_align(profile, translated_ref, valid_ref_len,
static_cast<int>(gap_opening_penalty_),
static_cast<int>(gap_extending_penalty_),
flag, filter.score_filter, filter.distance_filter, maskLen);
alignment->Clear();
ConvertAlignment(*s_al, query_len, alignment);
alignment->mismatches = CalculateNumberMismatch(&*alignment, translated_ref, translated_query, query_len);
// Free memory
delete [] translated_query;
delete [] translated_ref;
align_destroy(s_al);
init_destroy(profile);
return true;
}
void Aligner::Clear(void) {
ClearMatrices();
CleanReferenceSequence();
}
void Aligner::SetAllDefault(void) {
score_matrix_size_ = 5;
match_score_ = 2;
mismatch_penalty_ = 2;
gap_opening_penalty_ = 3;
gap_extending_penalty_ = 1;
reference_length_ = 0;
}
bool Aligner::ReBuild(void) {
if (translation_matrix_) return false;
SetAllDefault();
BuildDefaultMatrix();
return true;
}
bool Aligner::ReBuild(
const uint8_t& match_score,
const uint8_t& mismatch_penalty,
const uint8_t& gap_opening_penalty,
const uint8_t& gap_extending_penalty) {
if (translation_matrix_) return false;
SetAllDefault();
match_score_ = match_score;
mismatch_penalty_ = mismatch_penalty;
gap_opening_penalty_ = gap_opening_penalty;
gap_extending_penalty_ = gap_extending_penalty;
BuildDefaultMatrix();
return true;
}
bool Aligner::ReBuild(
const int8_t* score_matrix,
const int& score_matrix_size,
const int8_t* translation_matrix,
const int& translation_matrix_size) {
ClearMatrices();
score_matrix_ = new int8_t[score_matrix_size_ * score_matrix_size_];
memcpy(score_matrix_, score_matrix, sizeof(int8_t) * score_matrix_size_ * score_matrix_size_);
translation_matrix_ = new int8_t[translation_matrix_size];
memcpy(translation_matrix_, translation_matrix, sizeof(int8_t) * translation_matrix_size);
return true;
}
void Aligner::BuildDefaultMatrix(void) {
ClearMatrices();
score_matrix_ = new int8_t[score_matrix_size_ * score_matrix_size_];
BuildSwScoreMatrix(match_score_, mismatch_penalty_, score_matrix_);
translation_matrix_ = new int8_t[SizeOfArray(kBaseTranslation)];
memcpy(translation_matrix_, kBaseTranslation, sizeof(int8_t) * SizeOfArray(kBaseTranslation));
}
void Aligner::ClearMatrices(void) {
delete [] score_matrix_;
score_matrix_ = NULL;
delete [] translation_matrix_;
translation_matrix_ = NULL;
}
} // namespace StripedSmithWaterman