| 1 |
|
| 2 |
#include <pbcopper/align/cssw/ssw.h> |
| 3 |
#include <pbcopper/align/cssw/ssw_cpp.h> |
| 4 |
|
| 5 |
#include <sstream> |
| 6 |
|
| 7 |
namespace { |
| 8 |
|
| 9 |
static const int8_t kBaseTranslation[128] = { |
| 10 |
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, |
| 11 |
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, |
| 12 |
// A C G |
| 13 |
4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, |
| 14 |
// T |
| 15 |
4, 4, 4, 4, 3, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 16 |
// a c g |
| 17 |
4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, |
| 18 |
// t |
| 19 |
4, 4, 4, 4, 3, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}; |
| 20 |
|
| 21 |
void BuildSwScoreMatrix(const uint8_t& match_score, const uint8_t& mismatch_penalty, int8_t* matrix) |
| 22 |
{ |
| 23 |
|
| 24 |
// The score matrix looks like |
| 25 |
// // A, C, G, T, N |
| 26 |
// score_matrix_ = { 2, -2, -2, -2, -2, // A |
| 27 |
// -2, 2, -2, -2, -2, // C |
| 28 |
// -2, -2, 2, -2, -2, // G |
| 29 |
// -2, -2, -2, 2, -2, // T |
| 30 |
// -2, -2, -2, -2, -2};// N |
| 31 |
|
| 32 |
int id = 0; |
| 33 |
for (int i = 0; i < 4; ++i) { |
| 34 |
for (int j = 0; j < 4; ++j) { |
| 35 |
matrix[id] = ((i == j) ? match_score : static_cast<int8_t>(-mismatch_penalty)); |
| 36 |
++id; |
| 37 |
} |
| 38 |
matrix[id] = static_cast<int8_t>(-mismatch_penalty); // For N |
| 39 |
++id; |
| 40 |
} |
| 41 |
|
| 42 |
for (int i = 0; i < 5; ++i) |
| 43 |
matrix[id++] = static_cast<int8_t>(-mismatch_penalty); // For N |
| 44 |
} |
| 45 |
|
| 46 |
void ConvertAlignment(const s_align& s_al, const int& query_len, |
| 47 |
StripedSmithWaterman::Alignment* al) |
| 48 |
{ |
| 49 |
al->sw_score = s_al.score1; |
| 50 |
al->sw_score_next_best = s_al.score2; |
| 51 |
al->ref_begin = s_al.ref_begin1; |
| 52 |
al->ref_end = s_al.ref_end1; |
| 53 |
al->query_begin = s_al.read_begin1; |
| 54 |
al->query_end = s_al.read_end1; |
| 55 |
al->ref_end_next_best = s_al.ref_end2; |
| 56 |
|
| 57 |
al->cigar.clear(); |
| 58 |
al->cigar_string.clear(); |
| 59 |
|
| 60 |
if (s_al.cigarLen > 0) { |
| 61 |
std::ostringstream cigar_string; |
| 62 |
if (al->query_begin > 0) { |
| 63 |
uint32_t cigar = to_cigar_int(al->query_begin, 'S'); |
| 64 |
al->cigar.push_back(cigar); |
| 65 |
cigar_string << al->query_begin << 'S'; |
| 66 |
} |
| 67 |
|
| 68 |
for (int i = 0; i < s_al.cigarLen; ++i) { |
| 69 |
al->cigar.push_back(s_al.cigar[i]); |
| 70 |
cigar_string << cigar_int_to_len(s_al.cigar[i]) << cigar_int_to_op(s_al.cigar[i]); |
| 71 |
} |
| 72 |
|
| 73 |
int end = query_len - al->query_end - 1; |
| 74 |
if (end > 0) { |
| 75 |
uint32_t cigar = to_cigar_int(end, 'S'); |
| 76 |
al->cigar.push_back(cigar); |
| 77 |
cigar_string << end << 'S'; |
| 78 |
} |
| 79 |
|
| 80 |
al->cigar_string = cigar_string.str(); |
| 81 |
} // end if |
| 82 |
} |
| 83 |
|
| 84 |
// @Function: |
| 85 |
// Calculate the length of the previous cigar operator |
| 86 |
// and store it in new_cigar and new_cigar_string. |
| 87 |
// Clean up in_M (false), in_X (false), length_M (0), and length_X(0). |
| 88 |
void CleanPreviousMOperator(bool* in_M, bool* in_X, uint32_t* length_M, uint32_t* length_X, |
| 89 |
std::vector<uint32_t>* new_cigar, std::ostringstream* new_cigar_string) |
| 90 |
{ |
| 91 |
if (*in_M) { |
| 92 |
uint32_t match = to_cigar_int(*length_M, '='); |
| 93 |
new_cigar->push_back(match); |
| 94 |
(*new_cigar_string) << *length_M << '='; |
| 95 |
} else if (*in_X) { //in_X |
| 96 |
uint32_t match = to_cigar_int(*length_X, 'X'); |
| 97 |
new_cigar->push_back(match); |
| 98 |
(*new_cigar_string) << *length_X << 'X'; |
| 99 |
} |
| 100 |
|
| 101 |
// Clean up |
| 102 |
*in_M = false; |
| 103 |
*in_X = false; |
| 104 |
*length_M = 0; |
| 105 |
*length_X = 0; |
| 106 |
} |
| 107 |
|
| 108 |
// @Function: |
| 109 |
// 1. Calculate the number of mismatches. |
| 110 |
// 2. Modify the cigar string: |
| 111 |
// differentiate matches (M) and mismatches(X). |
| 112 |
// @Return: |
| 113 |
// The number of mismatches. |
| 114 |
int CalculateNumberMismatch(StripedSmithWaterman::Alignment* al, int8_t const* ref, |
| 115 |
int8_t const* query, const int& query_len) |
| 116 |
{ |
| 117 |
|
| 118 |
ref += al->ref_begin; |
| 119 |
query += al->query_begin; |
| 120 |
int mismatch_length = 0; |
| 121 |
|
| 122 |
std::vector<uint32_t> new_cigar; |
| 123 |
std::ostringstream new_cigar_string; |
| 124 |
|
| 125 |
if (al->query_begin > 0) { |
| 126 |
uint32_t cigar = to_cigar_int(al->query_begin, 'S'); |
| 127 |
new_cigar.push_back(cigar); |
| 128 |
new_cigar_string << al->query_begin << 'S'; |
| 129 |
} |
| 130 |
|
| 131 |
bool in_M = false; // the previous is match |
| 132 |
bool in_X = false; // the previous is mismatch |
| 133 |
uint32_t length_M = 0; |
| 134 |
uint32_t length_X = 0; |
| 135 |
|
| 136 |
for (unsigned int i = 0; i < al->cigar.size(); ++i) { |
| 137 |
char op = cigar_int_to_op(al->cigar[i]); |
| 138 |
uint32_t length = cigar_int_to_len(al->cigar[i]); |
| 139 |
if (op == 'M') { |
| 140 |
for (uint32_t j = 0; j < length; ++j) { |
| 141 |
if (*ref != *query) { |
| 142 |
++mismatch_length; |
| 143 |
if (in_M) { // the previous is match; however the current one is mismatche |
| 144 |
uint32_t match = to_cigar_int(length_M, '='); |
| 145 |
new_cigar.push_back(match); |
| 146 |
new_cigar_string << length_M << '='; |
| 147 |
} |
| 148 |
length_M = 0; |
| 149 |
++length_X; |
| 150 |
in_M = false; |
| 151 |
in_X = true; |
| 152 |
} else { // *ref == *query |
| 153 |
if (in_X) { // the previous is mismatch; however the current one is matche |
| 154 |
uint32_t match = to_cigar_int(length_X, 'X'); |
| 155 |
new_cigar.push_back(match); |
| 156 |
new_cigar_string << length_X << 'X'; |
| 157 |
} |
| 158 |
++length_M; |
| 159 |
length_X = 0; |
| 160 |
in_M = true; |
| 161 |
in_X = false; |
| 162 |
} // end of if (*ref != *query) |
| 163 |
++ref; |
| 164 |
++query; |
| 165 |
} |
| 166 |
} else if (op == 'I') { |
| 167 |
query += length; |
| 168 |
mismatch_length += length; |
| 169 |
CleanPreviousMOperator(&in_M, &in_X, &length_M, &length_X, &new_cigar, |
| 170 |
&new_cigar_string); |
| 171 |
new_cigar.push_back(al->cigar[i]); |
| 172 |
new_cigar_string << length << 'I'; |
| 173 |
} else if (op == 'D') { |
| 174 |
ref += length; |
| 175 |
mismatch_length += length; |
| 176 |
CleanPreviousMOperator(&in_M, &in_X, &length_M, &length_X, &new_cigar, |
| 177 |
&new_cigar_string); |
| 178 |
new_cigar.push_back(al->cigar[i]); |
| 179 |
new_cigar_string << length << 'D'; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
CleanPreviousMOperator(&in_M, &in_X, &length_M, &length_X, &new_cigar, &new_cigar_string); |
| 184 |
|
| 185 |
int end = query_len - al->query_end - 1; |
| 186 |
if (end > 0) { |
| 187 |
uint32_t cigar = to_cigar_int(end, 'S'); |
| 188 |
new_cigar.push_back(cigar); |
| 189 |
new_cigar_string << end << 'S'; |
| 190 |
} |
| 191 |
|
| 192 |
al->cigar_string.clear(); |
| 193 |
al->cigar.clear(); |
| 194 |
al->cigar_string = new_cigar_string.str(); |
| 195 |
al->cigar = new_cigar; |
| 196 |
|
| 197 |
return mismatch_length; |
| 198 |
} |
| 199 |
|
| 200 |
void SetFlag(const StripedSmithWaterman::Filter& filter, uint8_t* flag) |
| 201 |
{ |
| 202 |
if (filter.report_begin_position) *flag |= 0x08; |
| 203 |
if (filter.report_cigar) *flag |= 0x0f; |
| 204 |
} |
| 205 |
|
| 206 |
// http://www.cplusplus.com/faq/sequences/arrays/sizeof-array/#cpp |
| 207 |
template <typename T, size_t N> |
| 208 |
inline size_t SizeOfArray(const T (&)[N]) |
| 209 |
{ |
| 210 |
return N; |
| 211 |
} |
| 212 |
|
| 213 |
} // namespace |
| 214 |
|
| 215 |
namespace StripedSmithWaterman { |
| 216 |
|
| 217 |
Aligner::Aligner(void) |
| 218 |
: score_matrix_(NULL) |
| 219 |
, score_matrix_size_(5) |
| 220 |
, translation_matrix_(NULL) |
| 221 |
, match_score_(2) |
| 222 |
, mismatch_penalty_(2) |
| 223 |
, gap_opening_penalty_(3) |
| 224 |
, gap_extending_penalty_(1) |
| 225 |
, translated_reference_(NULL) |
| 226 |
, reference_length_(0) |
| 227 |
{ |
| 228 |
BuildDefaultMatrix(); |
| 229 |
} |
| 230 |
|
| 231 |
Aligner::Aligner(const uint8_t& match_score, const uint8_t& mismatch_penalty, |
| 232 |
const uint8_t& gap_opening_penalty, const uint8_t& gap_extending_penalty) |
| 233 |
|
| 234 |
: score_matrix_(NULL) |
| 235 |
, score_matrix_size_(5) |
| 236 |
, translation_matrix_(NULL) |
| 237 |
, match_score_(match_score) |
| 238 |
, mismatch_penalty_(mismatch_penalty) |
| 239 |
, gap_opening_penalty_(gap_opening_penalty) |
| 240 |
, gap_extending_penalty_(gap_extending_penalty) |
| 241 |
, translated_reference_(NULL) |
| 242 |
, reference_length_(0) |
| 243 |
{ |
| 244 |
BuildDefaultMatrix(); |
| 245 |
} |
| 246 |
|
| 247 |
Aligner::Aligner(const int8_t* score_matrix, const int& score_matrix_size, |
| 248 |
const int8_t* translation_matrix, const int& translation_matrix_size) |
| 249 |
|
| 250 |
: score_matrix_(NULL) |
| 251 |
, score_matrix_size_(score_matrix_size) |
| 252 |
, translation_matrix_(NULL) |
| 253 |
, match_score_(2) |
| 254 |
, mismatch_penalty_(2) |
| 255 |
, gap_opening_penalty_(3) |
| 256 |
, gap_extending_penalty_(1) |
| 257 |
, translated_reference_(NULL) |
| 258 |
, reference_length_(0) |
| 259 |
{ |
| 260 |
score_matrix_ = new int8_t[score_matrix_size_ * score_matrix_size_]; |
| 261 |
memcpy(score_matrix_, score_matrix, sizeof(int8_t) * score_matrix_size_ * score_matrix_size_); |
| 262 |
translation_matrix_ = new int8_t[translation_matrix_size]; |
| 263 |
memcpy(translation_matrix_, translation_matrix, sizeof(int8_t) * translation_matrix_size); |
| 264 |
} |
| 265 |
|
| 266 |
Aligner::~Aligner(void) { Clear(); } |
| 267 |
|
| 268 |
int Aligner::SetReferenceSequence(const char* seq, const int& length) |
| 269 |
{ |
| 270 |
|
| 271 |
int len = 0; |
| 272 |
if (translation_matrix_) { |
| 273 |
// calculate the valid length |
| 274 |
//int calculated_ref_length = static_cast<int>(strlen(seq)); |
| 275 |
//int valid_length = (calculated_ref_length > length) |
| 276 |
// ? length : calculated_ref_length; |
| 277 |
int valid_length = length; |
| 278 |
// delete the current buffer |
| 279 |
CleanReferenceSequence(); |
| 280 |
// allocate a new buffer |
| 281 |
translated_reference_ = new int8_t[valid_length]; |
| 282 |
|
| 283 |
len = TranslateBase(seq, valid_length, translated_reference_); |
| 284 |
} else { |
| 285 |
// nothing |
| 286 |
} |
| 287 |
|
| 288 |
reference_length_ = len; |
| 289 |
return len; |
| 290 |
} |
| 291 |
|
| 292 |
int Aligner::TranslateBase(const char* bases, const int& length, int8_t* translated) const |
| 293 |
{ |
| 294 |
|
| 295 |
const char* ptr = bases; |
| 296 |
int len = 0; |
| 297 |
for (int i = 0; i < length; ++i) { |
| 298 |
translated[i] = translation_matrix_[static_cast<int>(*ptr)]; |
| 299 |
++ptr; |
| 300 |
++len; |
| 301 |
} |
| 302 |
|
| 303 |
return len; |
| 304 |
} |
| 305 |
|
| 306 |
bool Aligner::Align(const char* query, const Filter& filter, Alignment* alignment) const |
| 307 |
{ |
| 308 |
if (!translation_matrix_) return false; |
| 309 |
if (reference_length_ == 0) return false; |
| 310 |
|
| 311 |
int query_len = strlen(query); |
| 312 |
if (query_len == 0) return false; |
| 313 |
int8_t* translated_query = new int8_t[query_len]; |
| 314 |
TranslateBase(query, query_len, translated_query); |
| 315 |
|
| 316 |
const int8_t score_size = 2; |
| 317 |
s_profile* profile = |
| 318 |
ssw_init(translated_query, query_len, score_matrix_, score_matrix_size_, score_size); |
| 319 |
|
| 320 |
uint8_t flag = 0; |
| 321 |
SetFlag(filter, &flag); |
| 322 |
s_align* s_al = |
| 323 |
ssw_align(profile, translated_reference_, reference_length_, |
| 324 |
static_cast<int>(gap_opening_penalty_), static_cast<int>(gap_extending_penalty_), |
| 325 |
flag, filter.score_filter, filter.distance_filter, query_len); |
| 326 |
|
| 327 |
alignment->Clear(); |
| 328 |
ConvertAlignment(*s_al, query_len, alignment); |
| 329 |
alignment->mismatches = |
| 330 |
CalculateNumberMismatch(&*alignment, translated_reference_, translated_query, query_len); |
| 331 |
|
| 332 |
// Free memory |
| 333 |
delete[] translated_query; |
| 334 |
align_destroy(s_al); |
| 335 |
init_destroy(profile); |
| 336 |
|
| 337 |
return true; |
| 338 |
} |
| 339 |
|
| 340 |
bool Aligner::Align(const char* query, const char* ref, const int& ref_len, const Filter& filter, |
| 341 |
Alignment* alignment) const |
| 342 |
{ |
| 343 |
if (!translation_matrix_) return false; |
| 344 |
|
| 345 |
int query_len = strlen(query); |
| 346 |
if (query_len == 0) return false; |
| 347 |
int8_t* translated_query = new int8_t[query_len]; |
| 348 |
TranslateBase(query, query_len, translated_query); |
| 349 |
|
| 350 |
// calculate the valid length |
| 351 |
//int calculated_ref_length = static_cast<int>(strlen(ref)); |
| 352 |
//int valid_ref_len = (calculated_ref_length > ref_len) |
| 353 |
// ? ref_len : calculated_ref_length; |
| 354 |
int valid_ref_len = ref_len; |
| 355 |
int8_t* translated_ref = new int8_t[valid_ref_len]; |
| 356 |
TranslateBase(ref, valid_ref_len, translated_ref); |
| 357 |
|
| 358 |
const int8_t score_size = 2; |
| 359 |
s_profile* profile = |
| 360 |
ssw_init(translated_query, query_len, score_matrix_, score_matrix_size_, score_size); |
| 361 |
|
| 362 |
uint8_t flag = 0; |
| 363 |
SetFlag(filter, &flag); |
| 364 |
s_align* s_al = |
| 365 |
ssw_align(profile, translated_ref, valid_ref_len, static_cast<int>(gap_opening_penalty_), |
| 366 |
static_cast<int>(gap_extending_penalty_), flag, filter.score_filter, |
| 367 |
filter.distance_filter, query_len); |
| 368 |
|
| 369 |
alignment->Clear(); |
| 370 |
ConvertAlignment(*s_al, query_len, alignment); |
| 371 |
alignment->mismatches = |
| 372 |
CalculateNumberMismatch(&*alignment, translated_ref, translated_query, query_len); |
| 373 |
|
| 374 |
// Free memory |
| 375 |
delete[] translated_query; |
| 376 |
delete[] translated_ref; |
| 377 |
align_destroy(s_al); |
| 378 |
init_destroy(profile); |
| 379 |
|
| 380 |
return true; |
| 381 |
} |
| 382 |
|
| 383 |
void Aligner::Clear(void) |
| 384 |
{ |
| 385 |
ClearMatrices(); |
| 386 |
CleanReferenceSequence(); |
| 387 |
} |
| 388 |
|
| 389 |
void Aligner::SetAllDefault(void) |
| 390 |
{ |
| 391 |
score_matrix_size_ = 5; |
| 392 |
match_score_ = 2; |
| 393 |
mismatch_penalty_ = 2; |
| 394 |
gap_opening_penalty_ = 3; |
| 395 |
gap_extending_penalty_ = 1; |
| 396 |
reference_length_ = 0; |
| 397 |
} |
| 398 |
|
| 399 |
bool Aligner::ReBuild(void) |
| 400 |
{ |
| 401 |
if (translation_matrix_) return false; |
| 402 |
|
| 403 |
SetAllDefault(); |
| 404 |
BuildDefaultMatrix(); |
| 405 |
|
| 406 |
return true; |
| 407 |
} |
| 408 |
|
| 409 |
bool Aligner::ReBuild(const uint8_t& match_score, const uint8_t& mismatch_penalty, |
| 410 |
const uint8_t& gap_opening_penalty, const uint8_t& gap_extending_penalty) |
| 411 |
{ |
| 412 |
if (translation_matrix_) return false; |
| 413 |
|
| 414 |
SetAllDefault(); |
| 415 |
|
| 416 |
match_score_ = match_score; |
| 417 |
mismatch_penalty_ = mismatch_penalty; |
| 418 |
gap_opening_penalty_ = gap_opening_penalty; |
| 419 |
gap_extending_penalty_ = gap_extending_penalty; |
| 420 |
|
| 421 |
BuildDefaultMatrix(); |
| 422 |
|
| 423 |
return true; |
| 424 |
} |
| 425 |
|
| 426 |
bool Aligner::ReBuild(const int8_t* score_matrix, const int& /*unused*/, |
| 427 |
const int8_t* translation_matrix, const int& translation_matrix_size) |
| 428 |
{ |
| 429 |
|
| 430 |
ClearMatrices(); |
| 431 |
score_matrix_ = new int8_t[score_matrix_size_ * score_matrix_size_]; |
| 432 |
memcpy(score_matrix_, score_matrix, sizeof(int8_t) * score_matrix_size_ * score_matrix_size_); |
| 433 |
translation_matrix_ = new int8_t[translation_matrix_size]; |
| 434 |
memcpy(translation_matrix_, translation_matrix, sizeof(int8_t) * translation_matrix_size); |
| 435 |
|
| 436 |
return true; |
| 437 |
} |
| 438 |
|
| 439 |
void Aligner::BuildDefaultMatrix(void) |
| 440 |
{ |
| 441 |
ClearMatrices(); |
| 442 |
score_matrix_ = new int8_t[score_matrix_size_ * score_matrix_size_]; |
| 443 |
BuildSwScoreMatrix(match_score_, mismatch_penalty_, score_matrix_); |
| 444 |
translation_matrix_ = new int8_t[SizeOfArray(kBaseTranslation)]; |
| 445 |
memcpy(translation_matrix_, kBaseTranslation, sizeof(int8_t) * SizeOfArray(kBaseTranslation)); |
| 446 |
} |
| 447 |
|
| 448 |
void Aligner::ClearMatrices(void) |
| 449 |
{ |
| 450 |
delete[] score_matrix_; |
| 451 |
score_matrix_ = NULL; |
| 452 |
|
| 453 |
delete[] translation_matrix_; |
| 454 |
translation_matrix_ = NULL; |
| 455 |
} |
| 456 |
} // namespace StripedSmithWaterman |