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 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
|
/*
* Copyright (C) 2010-2015 Regents of the University of Michigan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "SamValidation.h"
#include "CigarRoller.h"
#include "SamTags.h"
const char* SamValidationError::enumSeverityString[] = {
"WARNING", "ERROR"};
const char* SamValidationError::enumTypeString[] = {
"INVALID_QNAME",
"INVALID_REF_ID",
"INVALID_RNAME",
"INVALID_POS",
"INVALID_MAPQ",
"INVALID_CIGAR",
"INVALID_MRNM",
"INVALID_QUAL",
"INVALID_TAG"
};
const char* SamValidationError::getTypeString(Type type)
{
return(enumTypeString[type]);
}
SamValidationError::SamValidationError(Type type, Severity severity,
std::string message)
{
myType = type;
mySeverity = severity;
myMessage = message;
}
SamValidationError::Type SamValidationError::getType() const
{
return(myType);
}
SamValidationError::Severity SamValidationError::getSeverity() const
{
return(mySeverity);
}
const char* SamValidationError::getMessage() const
{
return(myMessage.c_str());
}
const char* SamValidationError::getTypeString() const
{
return(enumTypeString[myType]);
}
const char* SamValidationError::getSeverityString() const
{
return(enumSeverityString[mySeverity]);
}
void SamValidationError::getErrorString(std::string& errorString) const
{
errorString = getTypeString();
errorString += " (";
errorString += getSeverityString();
errorString += ") : ";
errorString += getMessage();
errorString += "\n";
}
void SamValidationError::printError() const
{
std::cerr << this;
}
// Constructor.
SamValidationErrors::SamValidationErrors()
: myValidationErrors()
{
myErrorIter = myValidationErrors.begin();
}
// Destructor
SamValidationErrors::~SamValidationErrors()
{
clear();
}
void SamValidationErrors::clear()
{
// Clear the errors.
std::list<const SamValidationError*>::iterator errorIter;
for(errorIter = myValidationErrors.begin();
errorIter != myValidationErrors.end(); ++errorIter)
{
delete *errorIter;
*errorIter = NULL;
}
myValidationErrors.clear();
myErrorIter = myValidationErrors.end();
}
void SamValidationErrors::addError(SamValidationError::Type newType,
SamValidationError::Severity newSeverity,
const char* newMessage)
{
myValidationErrors.push_back(new SamValidationError(newType,
newSeverity,
newMessage));
// If this is the first element in the list, set the iterator.
if(myValidationErrors.size() == 1)
{
// set the iterator to the first element.
myErrorIter = myValidationErrors.begin();
}
}
// Return the number of validation errors that are contained in this object.
unsigned int SamValidationErrors::numErrors()
{
return(myValidationErrors.size());
}
// Return a pointer to the next error. It does not remove it from the list.
// Returns null once all errors have been retrieved until resetErrorIter
// is called.
const SamValidationError* SamValidationErrors::getNextError()
{
if(myErrorIter == myValidationErrors.end())
{
// at the end of the list, return null.
return(NULL);
}
// Not at the end of the list, return the last element and increment.
return(*myErrorIter++);
}
// Resets the iterator to the begining of the errors.
void SamValidationErrors::resetErrorIter()
{
myErrorIter = myValidationErrors.begin();
}
// Appends the error messages to the passed in string.
void SamValidationErrors::getErrorString(std::string& errorString) const
{
for(std::list<const SamValidationError*>::
const_iterator validationErrorIter =
myValidationErrors.begin();
validationErrorIter != myValidationErrors.end();
validationErrorIter++)
{
std::string error = "";
(*validationErrorIter)->getErrorString(error);
errorString += error;
}
}
bool SamValidator::isValid(SamFileHeader& samHeader, SamRecord& samRecord,
SamValidationErrors& validationErrors)
{
bool status = true;
status &= isValidQname(samRecord.getReadName(),
samRecord.getReadNameLength(),
validationErrors);
status &= isValidFlag(samRecord.getFlag(),
validationErrors);
// Validate the RName including validating it against the header.
status &= isValidRname(samHeader,
samRecord.getReferenceName(),
validationErrors);
status &= isValidRefID(samRecord.getReferenceID(),
samHeader.getReferenceInfo(),
validationErrors);
status &= isValid1BasedPos(samRecord.get1BasedPosition(),
validationErrors);
status &= isValidMapQuality(samRecord.getMapQuality(), validationErrors);
status &= isValidSequence(samRecord, validationErrors);
status &= isValidCigar(samRecord, validationErrors);
status &= isValidQuality(samRecord, validationErrors);
status &= isValidTags(samRecord, validationErrors);
return(status);
}
// qname is the query (read) name - result of SamRecord::getReadName().
// readNameLen is the length of the read name including the null (the result
// of SamRecord::getReadNameLength()).
// For some invalid records, the getReadNameLength may be different than the
// length of qname.
// NOTE: Query Name and Read Name both refer to the same field.
bool SamValidator::isValidQname(const char* qname, uint8_t readNameLen,
SamValidationErrors& validationErrors)
{
// Validation for QNAME is:
// a) length of the qname string is the same as the read name length
// b) length is between 1 and 254.
// c) [ \t\n\r] are not allowed in the name.
bool status = true;
// Get the length of the qname string.
int32_t qnameLenNull = strlen(qname) + 1;
////////////////////////////////////
// a) length of the qname string is the same as the read name length
if(qnameLenNull != readNameLen)
{
// This results from a poorly formatted bam file, where the null
// terminated read_name field is not the same length as specified by
// read_name_len.
String message = "Invalid Query Name - the string length (";
message += qnameLenNull;
message += ") does not match the specified query name length (";
message += readNameLen;
message += ").";
validationErrors.addError(SamValidationError::INVALID_QNAME,
SamValidationError::ERROR,
message.c_str());
status = false;
}
////////////////////////////////////
// b) length is between 1 and 254
// The length with the terminating null must be between 2 & 255,
if((qnameLenNull < 2) || (qnameLenNull > 255))
{
String message = "Invalid Query Name (QNAME) length: ";
message += qnameLenNull;
message += ". Length with the terminating null must be between 2 & 255.";
validationErrors.addError(SamValidationError::INVALID_QNAME,
SamValidationError::WARNING,
message.c_str());
status = false;
}
////////////////////////////////////
// Loop through and validate they all characters are valid.
// c) [ \t\n\r] are not allowed in the name.
String message;
for(int i = 0; i < qnameLenNull; ++i)
{
switch(qname[i])
{
case ' ':
// Invalid character.
message = "Invalid character in the Query Name (QNAME): ' ' at position ";
message += i;
message += ".";
validationErrors.addError(SamValidationError::INVALID_QNAME,
SamValidationError::WARNING,
message.c_str());
status = false;
break;
case '\t':
// Invalid character.
message = "Invalid character in the Query Name (QNAME): '\t' at position ";
message += i;
message += ".";
validationErrors.addError(SamValidationError::INVALID_QNAME,
SamValidationError::WARNING,
message.c_str());
status = false;
break;
case '\n':
// Invalid character.
message = "Invalid character in the Query Name (QNAME): '\n' at position ";
message += i;
message += ".";
validationErrors.addError(SamValidationError::INVALID_QNAME,
SamValidationError::WARNING,
message.c_str());
status = false;
break;
case '\r':
// Invalid character.
message = "Invalid character in the Query Name (QNAME): '\r' at position ";
message += i;
message += ".";
validationErrors.addError(SamValidationError::INVALID_QNAME,
SamValidationError::WARNING,
message.c_str());
status = false;
break;
}
}
return(status);
}
bool SamValidator::isValidFlag(uint16_t flag,
SamValidationErrors& validationErrors)
{
// All values in a uint16_t are valid, so return true.
return(true);
}
bool SamValidator::isValidRname(SamFileHeader& samHeader,
const char* rname,
SamValidationErrors& validationErrors)
{
bool status = true;
// Cross validate the rname and the header.
// If the rname is not '*'
// AND there are any SQ records in the header,
// Then the rname must be in one of them.
if((strcmp(rname, "*") != 0) &&
(samHeader.getNumSQs() != 0) &&
(samHeader.getSQ(rname) == NULL))
{
// There are SQ fields, but the ref name is not in it.
status = false;
std::string message = "RNAME, ";
message += rname;
message += ", was not found in a SAM Header SQ record";
validationErrors.addError(SamValidationError::INVALID_RNAME,
SamValidationError::WARNING,
message.c_str());
}
status &= isValidRname(rname, validationErrors);
return(status);
}
bool SamValidator::isValidRname(const char* rname,
SamValidationErrors& validationErrors)
{
// Validation for RNAME is:
// a) cannot be 0 length.
// b) [ \t\n\r@=] are not allowed in the name.
bool status = true;
// Get the length of the rname string.
int32_t rnameLen = strlen(rname);
String message;
if(rnameLen == 0)
{
validationErrors.addError(SamValidationError::INVALID_RNAME,
SamValidationError::WARNING,
"Reference Sequence Name (RNAME) cannot have 0 length.");
status = false;
}
////////////////////////////////////
////////////////////////////////////
// Loop through and validate they all characters are valid.
// b) [ \t\n\r] are not allowed in the name.
for(int i = 0; i < rnameLen; ++i)
{
switch(rname[i])
{
case ' ':
// Invalid character.
message = "Invalid character in the Reference Sequence Name (RNAME): ' ' at position ";
message += i;
message += ".";
validationErrors.addError(SamValidationError::INVALID_RNAME,
SamValidationError::WARNING,
message.c_str());
status = false;
break;
case '\t':
// Invalid character.
message = "Invalid character in the Reference Sequence Name (RNAME): '\t' at position ";
message += i;
message += ".";
validationErrors.addError(SamValidationError::INVALID_RNAME,
SamValidationError::WARNING,
message.c_str());
status = false;
break;
case '\n':
// Invalid character.
message = "Invalid character in the Reference Sequence Name (RNAME): '\n' at position ";
message += i;
message += ".";
validationErrors.addError(SamValidationError::INVALID_RNAME,
SamValidationError::WARNING,
message.c_str());
status = false;
break;
case '\r':
// Invalid character.
message = "Invalid character in the Reference Sequence Name (RNAME): '\r' at position ";
message += i;
message += ".";
validationErrors.addError(SamValidationError::INVALID_RNAME,
SamValidationError::WARNING,
message.c_str());
status = false;
break;
case '@':
// Invalid character.
message = "Invalid character in the Reference Sequence Name (RNAME): '@' at position ";
message += i;
message += ".";
validationErrors.addError(SamValidationError::INVALID_RNAME,
SamValidationError::WARNING,
message.c_str());
status = false;
break;
case '=':
// Invalid character.
message = "Invalid character in the Reference Sequence Name (RNAME): '=' at position ";
message += i;
message += ".";
validationErrors.addError(SamValidationError::INVALID_RNAME,
SamValidationError::WARNING,
message.c_str());
status = false;
break;
default:
// Allowed character.
break;
}
}
return(status);
}
bool SamValidator::isValidRefID(int32_t refID,
const SamReferenceInfo& refInfo,
SamValidationErrors& validationErrors)
{
// Validation for rID is:
// a) must be between -1 and the number of refInfo.
// -1 is allowed, and otherwise it must properly index into the array.
bool status = true;
if((refID < -1) || (refID >= refInfo.getNumEntries()))
{
// Reference ID is too large or too small.
String message = "Invalid Reference ID, out of range (";
message += refID;
message += ") must be between -1 and ";
message += refInfo.getNumEntries() - 1;
message += ".";
validationErrors.addError(SamValidationError::INVALID_REF_ID,
SamValidationError::WARNING,
message.c_str());
status = false;
}
return(status);
}
bool SamValidator::isValid1BasedPos(int32_t pos,
SamValidationErrors& validationErrors)
{
// Validation for pos is:
// a) must be between 0 and (2^29)-1.
bool status = true;
if((pos < 0) || (pos > 536870911))
{
String message = "POS out of range (";
message += pos;
message += ") must be between 0 and (2^29)-1.";
validationErrors.addError(SamValidationError::INVALID_POS,
SamValidationError::WARNING,
message.c_str());
status = false;
}
return(status);
}
bool SamValidator::isValidMapQuality(uint8_t mapQuality,
SamValidationErrors& validationErrors)
{
// All values in a uint8_t are valid, so return true.
return(true);
}
bool SamValidator::isValidSequence(SamRecord& samRecord,
SamValidationErrors& validationErrors)
{
return(true);
}
bool SamValidator::isValidCigar(SamRecord& samRecord,
SamValidationErrors& validationErrors)
{
return(isValidCigar(samRecord.getCigar(),
samRecord.getReadLength(),
validationErrors));
}
bool SamValidator::isValidCigar(const char* cigar,
const char* sequence,
SamValidationErrors& validationErrors)
{
if(strcmp(sequence, "*") != 0)
{
return(isValidCigar(cigar, strlen(sequence), validationErrors));
}
// Sequence is '*', so the length is 0.
return(isValidCigar(cigar, 0, validationErrors));
}
bool SamValidator::isValidCigar(const char* cigar,
int seqLen,
SamValidationErrors& validationErrors)
{
// Validation for CIGAR is:
// a) cannot be 0 length.
// if not "*", validate the following:
// b) must have an integer length for each operator (if not "*"). TODO
// c) all operators must be valid (if not "*"). TODO
// d) evaluates to the same read length as the sequence string if not '*'.
bool status = true;
String message;
int32_t cigarLen = strlen(cigar);
// a) cannot be 0 length.
if(cigarLen == 0)
{
validationErrors.addError(SamValidationError::INVALID_CIGAR,
SamValidationError::WARNING,
"Cigar must not be blank.");
status = false;
}
if(strcmp(cigar, "*") != 0)
{
// The cigar is not "*", so validate it.
CigarRoller cigarRoller(cigar);
// b) must have an integer length for each operator.
// TODO
// c) all operators must be valid.
// TODO
// d) is the same length as the sequence string.
int cigarSeqLen = cigarRoller.getExpectedQueryBaseCount();
if((cigarSeqLen != seqLen) && (seqLen != 0))
{
message = "CIGAR does not evaluate to the same length as SEQ, (";
message += cigarSeqLen;
message += " != ";
message += seqLen;
message += ").";
validationErrors.addError(SamValidationError::INVALID_CIGAR,
SamValidationError::WARNING,
message.c_str());
status = false;
}
}
return(status);
}
bool SamValidator::isValidQuality(SamRecord& samRecord,
SamValidationErrors& validationErrors)
{
return(isValidQuality(samRecord.getQuality(),
samRecord.getReadLength(),
validationErrors));
}
bool SamValidator::isValidQuality(const char* quality,
const char* sequence,
SamValidationErrors& validationErrors)
{
// Determine the length of the sequence.
int seqLen = strlen(sequence);
// Check if the sequence is '*' since then the seqLength is 0.
if(strcmp(sequence, "*") == 0)
{
seqLen = 0;
}
return(isValidQuality(quality, seqLen, validationErrors));
}
bool SamValidator::isValidQuality(const char* quality,
int seqLength,
SamValidationErrors& validationErrors)
{
bool status = true;
// If the quality or the sequence are non-"*", validate that the quality
// and sequence have the same length.
if((seqLength != 0) && (strcmp(quality, "*") != 0))
{
int qualLen = strlen(quality);
// Both the sequence and the quality are not "*", so validate
// that they are the same length.
if(seqLength != qualLen)
{
// Both fields are specified but are different lengths.
String message = "QUAL is not the same length as SEQ, (";
message += qualLen;
message += " != ";
message += seqLength;
message += ").";
validationErrors.addError(SamValidationError::INVALID_QUAL,
SamValidationError::WARNING,
message.c_str());
status = false;
}
}
return(status);
}
bool SamValidator::isValidTags(SamRecord& samRecord,
SamValidationErrors& validationErrors)
{
bool status = true;
GenomeSequence* reference = samRecord.getReference();
// If the reference is not null, check the MD tag.
if(reference != NULL)
{
const String* recordMD = samRecord.getStringTag(SamTags::MD_TAG);
if(recordMD != NULL)
{
// The record has an MD tag so check to see if it is correct.
if(!SamTags::isMDTagCorrect(samRecord, *reference))
{
// Invalid MD tags.
String correctMD;
if(!SamTags::createMDTag(correctMD, samRecord, *reference))
{
// Failed to get the MD tag, so indicate that it is unknown.
correctMD = "UNKNOWN";
}
String message = "Incorrect MD Tag, ";
message += *recordMD;
message += ", should be ";
message += correctMD;
message += ".";
validationErrors.addError(SamValidationError::INVALID_TAG,
SamValidationError::WARNING,
message.c_str());
status = false;
}
}
}
return(status);
}
|