| 1 |
#include <pbcopper/data/Cigar.h> |
| 2 |
|
| 3 |
#include <pbcopper/utility/SequenceUtils.h> |
| 4 |
#include <pbcopper/utility/Ssize.h> |
| 5 |
|
| 6 |
#include <iterator> |
| 7 |
#include <ostream> |
| 8 |
#include <sstream> |
| 9 |
#include <stack> |
| 10 |
#include <stdexcept> |
| 11 |
#include <type_traits> |
| 12 |
|
| 13 |
#include <cassert> |
| 14 |
#include <cstddef> |
| 15 |
#include <cstdint> |
| 16 |
|
| 17 |
namespace PacBio { |
| 18 |
namespace Data { |
| 19 |
|
| 20 |
Cigar::Cigar(const char* str) : std::vector<CigarOperation>{} |
| 21 |
{ |
| 22 |
size_t numberStart = 0; |
| 23 |
size_t i = 0; |
| 24 |
for (const char* c = str; *c; ++i, ++c) { |
| 25 |
if (!std::isdigit(*c)) { |
| 26 |
const size_t distance = i - numberStart; |
| 27 |
const uint32_t length = std::stoul(std::string{str, numberStart, distance}); |
| 28 |
push_back(CigarOperation(*c, length)); |
| 29 |
numberStart = i + 1; |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
Cigar::Cigar(const std::string& cigarString) : Cigar{cigarString.c_str()} {} |
| 35 |
|
| 36 |
Cigar::Cigar(std::vector<CigarOperation> cigar) : std::vector<CigarOperation>{std::move(cigar)} {} |
| 37 |
|
| 38 |
Cigar Cigar::FromStdString(const std::string& stdString) { return Cigar(stdString.c_str()); } |
| 39 |
|
| 40 |
Cigar Cigar::FromCStr(const char* str) { return Cigar(str); } |
| 41 |
|
| 42 |
std::string Cigar::ToStdString() const |
| 43 |
{ |
| 44 |
std::ostringstream s; |
| 45 |
const auto endIt = this->cend(); |
| 46 |
for (auto iter = this->cbegin(); iter != endIt; ++iter) { |
| 47 |
const CigarOperation& cigar = (*iter); |
| 48 |
s << cigar.Length() << cigar.Char(); |
| 49 |
} |
| 50 |
return s.str(); |
| 51 |
} |
| 52 |
|
| 53 |
size_t ReferenceLength(const Cigar& cigar) |
| 54 |
{ |
| 55 |
size_t length = 0; |
| 56 |
for (const auto& op : cigar) { |
| 57 |
if (ConsumesReference(op.Type())) { |
| 58 |
length += op.Length(); |
| 59 |
} |
| 60 |
} |
| 61 |
return length; |
| 62 |
} |
| 63 |
|
| 64 |
std::ostream& operator<<(std::ostream& os, const Cigar& cigar) |
| 65 |
{ |
| 66 |
return os << "Cigar(" << cigar.ToStdString() << ')'; |
| 67 |
} |
| 68 |
|
| 69 |
/// http://bitbucket.pacificbiosciences.com:7990/projects/SAT/repos/pbmm2/browse/src/MM2Helper.cpp#980 |
| 70 |
CigarBaseCounts CigarOpsCalculator(const Cigar& cigar) |
| 71 |
{ |
| 72 |
|
| 73 |
CigarBaseCounts results = {}; |
| 74 |
|
| 75 |
for (const auto& c : cigar) { |
| 76 |
int32_t len = c.Length(); |
| 77 |
switch (c.Type()) { |
| 78 |
case Data::CigarOperationType::INSERTION: |
| 79 |
results.InsertionBases += len; |
| 80 |
++results.InsertionEvents; |
| 81 |
break; |
| 82 |
case Data::CigarOperationType::DELETION: |
| 83 |
results.DeletionBases += len; |
| 84 |
++results.DeletionEvents; |
| 85 |
break; |
| 86 |
case Data::CigarOperationType::SEQUENCE_MISMATCH: |
| 87 |
results.MismatchBases += len; |
| 88 |
++results.MismatchEvents; |
| 89 |
break; |
| 90 |
case Data::CigarOperationType::REFERENCE_SKIP: |
| 91 |
break; |
| 92 |
case Data::CigarOperationType::SEQUENCE_MATCH: |
| 93 |
case Data::CigarOperationType::ALIGNMENT_MATCH: |
| 94 |
results.MatchBases += len; |
| 95 |
++results.MatchEvents; |
| 96 |
break; |
| 97 |
case Data::CigarOperationType::PADDING: |
| 98 |
case Data::CigarOperationType::SOFT_CLIP: |
| 99 |
case Data::CigarOperationType::HARD_CLIP: |
| 100 |
break; |
| 101 |
case Data::CigarOperationType::UNKNOWN_OP: |
| 102 |
default: |
| 103 |
throw std::runtime_error("UNKNOWN CIGAR OPERATION"); |
| 104 |
break; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
results.NumAlignedBases = results.MatchBases + results.InsertionBases + results.MismatchBases; |
| 109 |
results.Identity = 100.0 * results.MatchBases / |
| 110 |
(results.MatchBases + results.MismatchBases + results.DeletionBases + |
| 111 |
results.InsertionBases); |
| 112 |
results.GapCompressedIdentity = 100.0 * results.MatchBases / |
| 113 |
(results.MatchBases + results.MismatchBases + |
| 114 |
results.DeletionEvents + results.InsertionEvents); |
| 115 |
|
| 116 |
return results; |
| 117 |
} |
| 118 |
|
| 119 |
bool ConvertCigarToM5(const std::string& ref, const std::string& query, bool qReversed, |
| 120 |
const Data::Cigar& cigar, std::string& retRefAln, std::string& retQueryAln) |
| 121 |
{ |
| 122 |
return ConvertCigarToM5(ref, query, 0, ref.size(), 0, query.size(), qReversed, cigar, retRefAln, |
| 123 |
retQueryAln); |
| 124 |
} |
| 125 |
|
| 126 |
bool ConvertCigarToM5(const std::string& ref, const std::string& query, const int32_t rStart, |
| 127 |
const int32_t rEnd, const int32_t qStart, const int32_t qEnd, |
| 128 |
const bool qReversed, const Data::Cigar& cigar, std::string& retRefAln, |
| 129 |
std::string& retQueryAln) |
| 130 |
{ |
| 131 |
std::ostringstream rss; |
| 132 |
std::ostringstream qss; |
| 133 |
|
| 134 |
retRefAln.clear(); |
| 135 |
retQueryAln.clear(); |
| 136 |
|
| 137 |
// Calculate the query and reference length from the CIGAR |
| 138 |
// to check for sanity. |
| 139 |
int32_t calcRefLen = 0; |
| 140 |
int32_t calcQueryLen = 0; |
| 141 |
for (const auto& cigarOp : cigar) { |
| 142 |
const auto op = cigarOp.Type(); |
| 143 |
switch (op) { |
| 144 |
case CigarOperationType::ALIGNMENT_MATCH: |
| 145 |
case CigarOperationType::SEQUENCE_MATCH: |
| 146 |
case CigarOperationType::SEQUENCE_MISMATCH: { |
| 147 |
calcRefLen += cigarOp.Length(); |
| 148 |
calcQueryLen += cigarOp.Length(); |
| 149 |
break; |
| 150 |
} |
| 151 |
case Data::CigarOperationType::INSERTION: |
| 152 |
case CigarOperationType::SOFT_CLIP: { |
| 153 |
calcQueryLen += cigarOp.Length(); |
| 154 |
break; |
| 155 |
} |
| 156 |
case CigarOperationType::DELETION: |
| 157 |
case CigarOperationType::REFERENCE_SKIP: { |
| 158 |
calcRefLen += cigarOp.Length(); |
| 159 |
break; |
| 160 |
} |
| 161 |
default: { |
| 162 |
std::string msg{"[pbcopper] CIGAR conversion ERROR: unsupported op: '"}; |
| 163 |
msg += cigarOp.Char(); |
| 164 |
msg += "'"; |
| 165 |
throw std::runtime_error{msg}; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
if (cigar.empty() || calcQueryLen != (qEnd - qStart) || calcRefLen != (rEnd - rStart)) { |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
// Prepare the query for simpler usage. |
| 174 |
std::string querySub = query.substr(qStart, qEnd - qStart); |
| 175 |
if (qReversed) { |
| 176 |
querySub = Utility::ReverseComplemented(querySub); |
| 177 |
} |
| 178 |
|
| 179 |
int32_t qPos = 0; // We extracted the entire subsequence. |
| 180 |
int32_t rPos = rStart; |
| 181 |
|
| 182 |
for (auto& cigarOp : cigar) { |
| 183 |
auto op = cigarOp.Type(); |
| 184 |
auto count = cigarOp.Length(); |
| 185 |
switch (op) { |
| 186 |
case CigarOperationType::ALIGNMENT_MATCH: |
| 187 |
case CigarOperationType::SEQUENCE_MATCH: |
| 188 |
case CigarOperationType::SEQUENCE_MISMATCH: { |
| 189 |
qss << querySub.substr(qPos, count); |
| 190 |
rss << ref.substr(rPos, count); |
| 191 |
qPos += count; |
| 192 |
rPos += count; |
| 193 |
break; |
| 194 |
} |
| 195 |
case CigarOperationType::INSERTION: |
| 196 |
case CigarOperationType::SOFT_CLIP: { |
| 197 |
qss << querySub.substr(qPos, count); |
| 198 |
rss << std::string(count, '-'); |
| 199 |
qPos += count; |
| 200 |
break; |
| 201 |
} |
| 202 |
case CigarOperationType::DELETION: |
| 203 |
case CigarOperationType::REFERENCE_SKIP: { |
| 204 |
qss << std::string(count, '-'); |
| 205 |
rss << ref.substr(rPos, count); |
| 206 |
rPos += count; |
| 207 |
break; |
| 208 |
} |
| 209 |
default: { |
| 210 |
std::string msg{"[pbcopper] CIGAR conversion ERROR: unsupported op: '"}; |
| 211 |
msg += cigarOp.Char(); |
| 212 |
msg += "'"; |
| 213 |
throw std::runtime_error{msg}; |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
retRefAln = rss.str(); |
| 219 |
retQueryAln = qss.str(); |
| 220 |
return true; |
| 221 |
} |
| 222 |
|
| 223 |
bool ValidateCigar(const Data::Cigar& cigar, const std::string_view ref, |
| 224 |
const std::string_view query) |
| 225 |
{ |
| 226 |
const auto cigarBegin = std::cbegin(cigar); |
| 227 |
const auto cigarEnd = std::cend(cigar); |
| 228 |
|
| 229 |
const int32_t refLen = Utility::Ssize(ref); |
| 230 |
const int32_t queryLen = Utility::Ssize(query); |
| 231 |
|
| 232 |
int32_t refPos = 0; |
| 233 |
int32_t queryPos = 0; |
| 234 |
|
| 235 |
for (auto it = cigarBegin; it != cigarEnd; ++it) { |
| 236 |
const Data::CigarOperationType type = it->Type(); |
| 237 |
const int32_t length = it->Length(); |
| 238 |
|
| 239 |
if ((it != cigarBegin) && (type == std::prev(it)->Type())) { |
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
if (type == Data::CigarOperationType::INSERTION) { |
| 244 |
queryPos += length; |
| 245 |
|
| 246 |
} else if ((type == Data::CigarOperationType::DELETION) || |
| 247 |
(type == Data::CigarOperationType::REFERENCE_SKIP)) { |
| 248 |
refPos += length; |
| 249 |
|
| 250 |
} else if (type == Data::CigarOperationType::SOFT_CLIP) { |
| 251 |
if ((it != cigarBegin) && (it != std::prev(cigarEnd)) && |
| 252 |
(std::next(it)->Type() != Data::CigarOperationType::HARD_CLIP) && |
| 253 |
(std::prev(it)->Type() != Data::CigarOperationType::HARD_CLIP)) { |
| 254 |
return false; |
| 255 |
} |
| 256 |
queryPos += length; |
| 257 |
|
| 258 |
} else if (type == Data::CigarOperationType::HARD_CLIP) { |
| 259 |
if ((it != cigarBegin) && (it != std::prev(cigarEnd))) { |
| 260 |
return false; |
| 261 |
} |
| 262 |
|
| 263 |
} else if (type == Data::CigarOperationType::PADDING) { |
| 264 |
continue; |
| 265 |
|
| 266 |
} else if (type == Data::CigarOperationType::SEQUENCE_MATCH) { |
| 267 |
for (int32_t i = 0; i < length; ++i, ++refPos, ++queryPos) { |
| 268 |
if ((refPos >= refLen) || (queryPos >= queryLen) || |
| 269 |
(ref[refPos] != query[queryPos])) { |
| 270 |
return false; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
} else if (type == Data::CigarOperationType::SEQUENCE_MISMATCH) { |
| 275 |
for (int32_t i = 0; i < length; ++i, ++refPos, ++queryPos) { |
| 276 |
if ((refPos >= refLen) || (queryPos >= queryLen) || |
| 277 |
(ref[refPos] == query[queryPos])) { |
| 278 |
return false; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
} else { // ALIGNMENT_MATCH | UNKNOWN_OP |
| 283 |
return false; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
return (refPos == refLen) && (queryPos == queryLen); |
| 288 |
} |
| 289 |
|
| 290 |
Data::Cigar LeftAlignCigar(const Data::Cigar& cigar, const std::string_view ref, |
| 291 |
const std::string_view query, const bool replaceMismatches) |
| 292 |
{ |
| 293 |
assert(ValidateCigar(cigar, ref, query)); |
| 294 |
|
| 295 |
std::stack<Data::CigarOperation> cigarStack; |
| 296 |
|
| 297 |
int32_t refBegin = 0; |
| 298 |
int32_t refEnd = Utility::Ssize(ref); |
| 299 |
int32_t refLoan = 0; |
| 300 |
|
| 301 |
int32_t queryBegin = 0; |
| 302 |
int32_t queryEnd = Utility::Ssize(query); |
| 303 |
int32_t queryLoan = 0; |
| 304 |
|
| 305 |
Data::Cigar result; |
| 306 |
result.reserve(std::size(cigar) * 1.5); |
| 307 |
|
| 308 |
// helper lambdas |
| 309 |
const auto updateCigarStack = [&]() { |
| 310 |
while (!cigarStack.empty()) { |
| 311 |
Data::CigarOperation op = cigarStack.top(); |
| 312 |
const Data::CigarOperationType type = op.Type(); |
| 313 |
|
| 314 |
assert(type != Data::CigarOperationType::PADDING); |
| 315 |
assert(type != Data::CigarOperationType::HARD_CLIP); |
| 316 |
assert(type != Data::CigarOperationType::SOFT_CLIP); |
| 317 |
|
| 318 |
if (((queryLoan == 0) && (refLoan == 0)) || |
| 319 |
((type == Data::CigarOperationType::INSERTION) && (queryLoan == 0)) || |
| 320 |
((type == Data::CigarOperationType::DELETION) && (refLoan == 0))) { |
| 321 |
break; |
| 322 |
} |
| 323 |
|
| 324 |
cigarStack.pop(); |
| 325 |
int32_t length = op.Length(); |
| 326 |
|
| 327 |
if ((type == Data::CigarOperationType::SEQUENCE_MATCH) || |
| 328 |
(type == Data::CigarOperationType::SEQUENCE_MISMATCH)) { |
| 329 |
const int32_t loan = std::min(queryLoan, refLoan); |
| 330 |
if (loan >= length) { |
| 331 |
queryLoan -= length; |
| 332 |
refLoan -= length; |
| 333 |
} else { |
| 334 |
queryLoan -= loan; |
| 335 |
refLoan -= loan; |
| 336 |
length -= loan; |
| 337 |
if ((queryLoan > 0) && (refLoan == 0)) { |
| 338 |
if (queryLoan >= length) { |
| 339 |
cigarStack.emplace(Data::CigarOperationType::DELETION, length); |
| 340 |
queryLoan -= length; |
| 341 |
} else { |
| 342 |
cigarStack.emplace(type, length - queryLoan); |
| 343 |
cigarStack.emplace(Data::CigarOperationType::DELETION, queryLoan); |
| 344 |
queryLoan = 0; |
| 345 |
} |
| 346 |
} else if ((queryLoan == 0) && (refLoan > 0)) { |
| 347 |
if (refLoan >= length) { |
| 348 |
cigarStack.emplace(Data::CigarOperationType::INSERTION, length); |
| 349 |
refLoan -= length; |
| 350 |
} else { |
| 351 |
cigarStack.emplace(type, length - refLoan); |
| 352 |
cigarStack.emplace(Data::CigarOperationType::INSERTION, refLoan); |
| 353 |
refLoan = 0; |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
} else if (type == Data::CigarOperationType::INSERTION) { |
| 359 |
if (queryLoan >= length) { |
| 360 |
queryLoan -= length; |
| 361 |
} else { |
| 362 |
length -= queryLoan; |
| 363 |
queryLoan = 0; |
| 364 |
cigarStack.emplace(Data::CigarOperationType::INSERTION, length); |
| 365 |
} |
| 366 |
|
| 367 |
} else if ((type == Data::CigarOperationType::DELETION) || |
| 368 |
(type == Data::CigarOperationType::REFERENCE_SKIP)) { |
| 369 |
if (refLoan >= length) { |
| 370 |
refLoan -= length; |
| 371 |
} else { |
| 372 |
length -= refLoan; |
| 373 |
refLoan = 0; |
| 374 |
cigarStack.emplace(Data::CigarOperationType::DELETION, length); |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
}; |
| 379 |
|
| 380 |
const auto updateOrAppendToResult = [&result](Data::CigarOperation op) { |
| 381 |
if (!result.empty() && (result.back().Type() == op.Type())) { |
| 382 |
result.back().Length(result.back().Length() + op.Length()); |
| 383 |
} else { |
| 384 |
result.emplace_back(op); |
| 385 |
} |
| 386 |
}; |
| 387 |
|
| 388 |
// preprocess query |
| 389 |
if (!cigar.empty() && (cigar.front().Type() == Data::CigarOperationType::SOFT_CLIP)) { |
| 390 |
queryBegin += cigar.front().Length(); |
| 391 |
} |
| 392 |
if ((Utility::Ssize(cigar) > 1) && |
| 393 |
(cigar.back().Type() == Data::CigarOperationType::SOFT_CLIP)) { |
| 394 |
queryEnd -= cigar.back().Length(); |
| 395 |
} |
| 396 |
|
| 397 |
// unroll |
| 398 |
for (const auto& it : cigar) { |
| 399 |
Data::CigarOperationType type = it.Type(); |
| 400 |
|
| 401 |
if (type == Data::CigarOperationType::PADDING) { |
| 402 |
continue; |
| 403 |
} |
| 404 |
if ((type == Data::CigarOperationType::HARD_CLIP) || |
| 405 |
(type == Data::CigarOperationType::SOFT_CLIP)) { |
| 406 |
// SOFT_CLIP does not consume query due to preprocess |
| 407 |
result.emplace_back(it); |
| 408 |
continue; |
| 409 |
} |
| 410 |
|
| 411 |
if (replaceMismatches && (type == Data::CigarOperationType::SEQUENCE_MISMATCH)) { |
| 412 |
const int32_t length = it.Length(); |
| 413 |
for (int32_t i = 0; i < length; ++i) { |
| 414 |
cigarStack.emplace(Data::CigarOperationType::INSERTION, 1); |
| 415 |
cigarStack.emplace(Data::CigarOperationType::DELETION, 1); |
| 416 |
} |
| 417 |
} else { |
| 418 |
cigarStack.emplace(it); |
| 419 |
} |
| 420 |
|
| 421 |
updateCigarStack(); |
| 422 |
while (!cigarStack.empty()) { |
| 423 |
Data::CigarOperation op = cigarStack.top(); |
| 424 |
cigarStack.pop(); |
| 425 |
|
| 426 |
type = op.Type(); |
| 427 |
const int32_t length = op.Length(); |
| 428 |
|
| 429 |
if ((type == Data::CigarOperationType::SEQUENCE_MATCH) || |
| 430 |
(type == Data::CigarOperationType::SEQUENCE_MISMATCH)) { |
| 431 |
updateOrAppendToResult(op); |
| 432 |
refBegin += length; |
| 433 |
queryBegin += length; |
| 434 |
|
| 435 |
} else if (type == Data::CigarOperationType::INSERTION) { |
| 436 |
for (int32_t i = 0; i < length; ++i, ++queryBegin) { |
| 437 |
if ((refBegin < refEnd) && (ref[refBegin] == query[queryBegin])) { |
| 438 |
// left align |
| 439 |
updateOrAppendToResult( |
| 440 |
Data::CigarOperation{Data::CigarOperationType::SEQUENCE_MATCH, 1}); |
| 441 |
++refBegin; |
| 442 |
++refLoan; |
| 443 |
} else { |
| 444 |
updateOrAppendToResult( |
| 445 |
Data::CigarOperation{Data::CigarOperationType::INSERTION, 1}); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
} else if ((type == Data::CigarOperationType::DELETION) || |
| 450 |
(type == Data::CigarOperationType::REFERENCE_SKIP)) { |
| 451 |
for (int32_t i = 0; i < length; ++i, ++refBegin) { |
| 452 |
if ((queryBegin < queryEnd) && (ref[refBegin] == query[queryBegin])) { |
| 453 |
// left align |
| 454 |
updateOrAppendToResult( |
| 455 |
Data::CigarOperation{Data::CigarOperationType::SEQUENCE_MATCH, 1}); |
| 456 |
++queryBegin; |
| 457 |
++queryLoan; |
| 458 |
} else { |
| 459 |
updateOrAppendToResult( |
| 460 |
Data::CigarOperation{Data::CigarOperationType::DELETION, 1}); |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
updateCigarStack(); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
assert(ValidateCigar(result, ref, query)); |
| 470 |
|
| 471 |
return result; |
| 472 |
} |
| 473 |
|
| 474 |
} // namespace Data |
| 475 |
} // namespace PacBio |