19 #include "ModifyVar.h"
20 #include "SamValidation.h"
22 void modifyFirstBase()
48 samValidationErrors));
52 assert(strcmp(sequence,
"") != 0);
53 std::string upSeq = sequence;
66 void modifyFirstBaseLong()
92 samValidationErrors));
96 assert(strcmp(sequence,
"") != 0);
97 std::string upSeq = sequence;
112 #ifdef __ZLIB_AVAILABLE__
116 modTest.testModifyVar(
"testFiles/testSam.sam",
true);
117 modTest.testModifyVar(
"testFiles/testSam.sam",
false);
118 #ifdef __ZLIB_AVAILABLE__
119 modTest.testModifyVar(
"testFiles/testBam.bam",
true);
120 modTest.testModifyVar(
"testFiles/testBam.bam",
false);
125 void modifyVar::testModifyVar(
const char* filename,
bool valBufFirst)
127 myFilename = filename;
128 myValBufFirst = valBufFirst;
130 testModifyReadNameOnlySameLength();
131 testModifyCigarOnlySameLength();
132 testModifySequenceOnlySameLength();
133 testModifyQualityOnlySameLength();
135 testShortenQuality();
136 testLengthenQuality();
138 testShortenReadName();
140 testShortenSequence();
142 testLengthenReadName();
144 testLengthenSequence();
147 testRemoveSequence();
148 testLengthenSequenceAndQuality();
152 void modifyVar::testModifyReadNameOnlySameLength()
158 expectedReadNameString =
"1:1011:G:255+17M15D20M";
159 samRecord.
setReadName(expectedReadNameString.c_str());
165 void modifyVar::testModifyCigarOnlySameLength()
171 expectedCigarString =
"3M2I";
172 samRecord.
setCigar(expectedCigarString.c_str());
177 expectedCigarBufLen = 2;
178 expectedCigarBuffer[0] = 0x30;
179 expectedCigarBuffer[1] = 0x21;
185 void modifyVar::testModifySequenceOnlySameLength()
191 expectedSequenceString =
"NCGAN";
192 samRecord.
setSequence(expectedSequenceString.c_str());
195 expectedSequenceBuffer[0] = 0xF2;
196 expectedSequenceBuffer[1] = 0x41;
197 expectedSequenceBuffer[2] = 0xF0;
203 void modifyVar::testModifyQualityOnlySameLength()
209 expectedQualityString =
"!>6+!";
210 samRecord.
setQuality(expectedQualityString.c_str());
216 void modifyVar::testRemoveQuality()
223 expectedQualityString =
"*";
224 samRecord.
setQuality(expectedQualityString.c_str());
230 void modifyVar::testShortenQuality()
237 expectedQualityString =
"!!";
238 samRecord.
setQuality(expectedQualityString.c_str());
244 void modifyVar::testLengthenQuality()
251 expectedQualityString =
"!!@@##";
252 samRecord.
setQuality(expectedQualityString.c_str());
258 void modifyVar::testShortenReadName()
264 expectedReadNameString =
"1:1011:G:255";
265 samRecord.
setReadName(expectedReadNameString.c_str());
271 void modifyVar::testShortenCigar()
277 expectedCigarString =
"5M";
278 samRecord.
setCigar(expectedCigarString.c_str());
282 expectedCigarBufLen = 1;
283 expectedCigarBuffer[0] = 0x50;
289 void modifyVar::testShortenSequence()
295 expectedSequenceString =
"CCGA";
296 samRecord.
setSequence(expectedSequenceString.c_str());
299 expectedSequenceBuffer[0] = 0x22;
300 expectedSequenceBuffer[1] = 0x41;
306 void modifyVar::testLengthenReadName()
312 expectedReadNameString =
"1:1011:G:255+17M15D20M:1111111";
313 samRecord.
setReadName(expectedReadNameString.c_str());
319 void modifyVar::testLengthenCigar()
325 expectedCigarString =
"3M2D2I";
326 samRecord.
setCigar(expectedCigarString.c_str());
332 expectedCigarBufLen = 3;
333 expectedCigarBuffer[0] = 0x30;
334 expectedCigarBuffer[1] = 0x22;
335 expectedCigarBuffer[2] = 0x21;
341 void modifyVar::testLengthenSequence()
347 expectedSequenceString =
"CCGAATT";
348 samRecord.
setSequence(expectedSequenceString.c_str());
351 expectedSequenceBuffer[0] = 0x22;
352 expectedSequenceBuffer[1] = 0x41;
353 expectedSequenceBuffer[2] = 0x18;
354 expectedSequenceBuffer[3] = 0x80;
360 void modifyVar::testRemoveCigar()
366 expectedCigarString =
"*";
367 expectedCigarBufLen = 0;
368 samRecord.
setCigar(expectedCigarString.c_str());
374 void modifyVar::testRemoveSequence()
380 expectedSequenceString =
"*";
381 samRecord.
setSequence(expectedSequenceString.c_str());
387 void modifyVar::testLengthenSequenceAndQuality()
393 expectedSequenceString =
"CCGAATT";
394 expectedQualityString =
"!@#$%^&";
395 samRecord.
setSequence(expectedSequenceString.c_str());
396 samRecord.
setQuality(expectedQualityString.c_str());
399 expectedSequenceBuffer[0] = 0x22;
400 expectedSequenceBuffer[1] = 0x41;
401 expectedSequenceBuffer[2] = 0x18;
402 expectedSequenceBuffer[3] = 0x80;
408 void modifyVar::validate()
417 validateReadName(recordBuffer);
418 validateCigar(recordBuffer);
419 validateSequence(recordBuffer);
420 validateQuality(recordBuffer);
421 validateTags(recordBuffer);
424 validateReadNameString();
425 validateCigarString();
426 validateSequenceString();
427 validateQualityString();
428 validateTagsString();
437 validateReadName(recordBuffer);
438 validateCigar(recordBuffer);
439 validateSequence(recordBuffer);
440 validateQuality(recordBuffer);
441 validateTags(recordBuffer);
444 validateReadNameString();
445 validateCigarString();
446 validateSequenceString();
447 validateQualityString();
448 validateTagsString();
454 const char* varPtr = (
const char*)&(recordBuffer->myData);
456 unsigned int len = expectedReadNameString.length();
457 for(
unsigned int i = 0; i < len; i++)
459 assert(varPtr[i] == expectedReadNameString[i]);
463 assert(varPtr[len] == 0);
466 assert(recordBuffer->myReadNameLength ==
467 expectedReadNameString.length() + 1);
473 const unsigned char* cigarStart =
474 (
const unsigned char*)&(recordBuffer->myData)
475 + recordBuffer->myReadNameLength;
477 unsigned int* varPtr = (
unsigned int*)cigarStart;
479 for(
int i = 0; i < expectedCigarBufLen; i++)
481 assert(varPtr[i] == expectedCigarBuffer[i]);
484 assert(recordBuffer->myCigarLength == expectedCigarBufLen);
491 int expectedReadLen = expectedSequenceString.length();
492 int seqLen = (expectedReadLen + 1)/2;
493 if(expectedSequenceString ==
"*")
499 const unsigned char* sequencePtr =
500 (
const unsigned char*)&(recordBuffer->myData)
501 + recordBuffer->myReadNameLength + (recordBuffer->myCigarLength * 4);
503 for(
int i = 0; i < seqLen; i++)
505 assert(sequencePtr[i] == expectedSequenceBuffer[i]);
508 assert(recordBuffer->myReadLength == expectedReadLen);
514 int expectedReadLen = expectedSequenceString.length();
515 int seqLen = (expectedReadLen + 1)/2;
516 if(expectedSequenceString ==
"*")
522 const uint8_t* qualityPtr =
523 (
const unsigned char*)&(recordBuffer->myData)
524 + recordBuffer->myReadNameLength + (recordBuffer->myCigarLength * 4)
527 int qualityLen = expectedQualityString.length();
529 for(
int i = 0; i < expectedReadLen; i++)
531 if(expectedQualityString ==
"*")
534 assert(qualityPtr[i] == 0xFF);
536 else if(i >= qualityLen)
540 assert(qualityPtr[i] == 0xFF);
544 assert(qualityPtr[i] == (expectedQualityString[i] - 33));
548 assert(recordBuffer->myReadLength == expectedReadLen);
554 const unsigned char* tagsPtr =
555 (
const unsigned char*)&(recordBuffer->myData)
556 + recordBuffer->myReadNameLength + (recordBuffer->myCigarLength * 4)
557 + (recordBuffer->myReadLength + 1)/2 + recordBuffer->myReadLength;
559 for(
int i = 0; i < expectedTagsLen; i++)
561 assert(tagsPtr[i] == expectedTagsBuffer[i]);
567 int32_t expectedBlockSize = tagsPtr - (
const unsigned char*)(recordBuffer)
568 + expectedTagsLen - 4;
569 assert(recordBuffer->myBlockSize == expectedBlockSize);
573 void modifyVar::validateTagsString()
579 assert(tag[0] ==
'A');
580 assert(tag[1] ==
'M');
582 assert(*(
char*)value == 0);
584 assert(tag[0] ==
'M');
585 assert(tag[1] ==
'D');
587 assert(*(
String*)value ==
"37");
589 assert(tag[0] ==
'N');
590 assert(tag[1] ==
'M');
592 assert(*(
char*)value == 0);
594 assert(tag[0] ==
'X');
595 assert(tag[1] ==
'T');
597 assert(*(
char*)value ==
'R');
603 void modifyVar::validateReadNameString()
605 assert(samRecord.
getReadName() == expectedReadNameString);
608 void modifyVar::validateCigarString()
610 assert(samRecord.
getCigar() == expectedCigarString);
613 void modifyVar::validateSequenceString()
615 assert(samRecord.
getSequence() == expectedSequenceString);
618 void modifyVar::validateQualityString()
620 assert(samRecord.
getQuality() == expectedQualityString);
624 void modifyVar::resetExpected()
626 expectedReadNameString =
"1:1011:F:255+17M15D20M";
627 expectedCigarString =
"5M2D";
628 expectedSequenceString =
"CCGAA";
629 expectedQualityString =
"6>6+4";
634 expectedCigarBufLen = 2;
635 expectedCigarBuffer[0] = 0x50;
636 expectedCigarBuffer[1] = 0x22;
639 expectedSequenceBuffer[0] = 0x22;
640 expectedSequenceBuffer[1] = 0x41;
641 expectedSequenceBuffer[2] = 0x10;
643 expectedTagsLen = 18;
644 expectedTagsBuffer[0] =
'A';
645 expectedTagsBuffer[1] =
'M';
646 expectedTagsBuffer[2] =
'C';
647 expectedTagsBuffer[3] = 0;
648 expectedTagsBuffer[4] =
'M';
649 expectedTagsBuffer[5] =
'D';
650 expectedTagsBuffer[6] =
'Z';
651 expectedTagsBuffer[7] =
'3';
652 expectedTagsBuffer[8] =
'7';
653 expectedTagsBuffer[9] = 0;
654 expectedTagsBuffer[10] =
'N';
655 expectedTagsBuffer[11] =
'M';
656 expectedTagsBuffer[12] =
'C';
657 expectedTagsBuffer[13] = 0;
658 expectedTagsBuffer[14] =
'X';
659 expectedTagsBuffer[15] =
'T';
660 expectedTagsBuffer[16] =
'A';
661 expectedTagsBuffer[17] =
'R';
665 void modifyVar::openAndRead1Rec()
674 assert(samIn.
ReadRecord(samHeader, samRecord));
Allows the user to easily read/write a SAM/BAM file.
bool ReadHeader(SamFileHeader &header)
Reads the header section from the file and stores it in the passed in header.
bool ReadRecord(SamFileHeader &header, SamRecord &record)
Reads the next record from the file & stores it in the passed in record.
bool OpenForRead(const char *filename, SamFileHeader *header=NULL)
Open a sam/bam file for reading with the specified filename, determing the type of file and SAM/BAM b...
SamStatus::Status GetStatus()
Get the Status of the last call that sets status.
bool OpenForWrite(const char *filename, SamFileHeader *header=NULL)
Open a sam/bam file for writing with the specified filename, determining SAM/BAM from the extension (...
bool WriteHeader(SamFileHeader &header)
Writes the specified header into the file.
bool WriteRecord(SamFileHeader &header, SamRecord &record)
Writes the specified record into the file.
Class providing an easy to use interface to get/set/operate on the fields in a SAM/BAM record.
bool setReadName(const char *readName)
Set QNAME to the passed in name.
const void * getRecordBuffer()
Get a const pointer to the buffer that contains the BAM representation of the record.
bool setCigar(const char *cigar)
Set the CIGAR to the specified SAM formatted cigar string.
bool getNextSamTag(char *tag, char &vtype, void **value)
Get the next tag from the record.
bool setSequence(const char *seq)
Sets the sequence (SEQ) to the specified SAM formatted sequence string.
const char * getCigar()
Returns the SAM formatted CIGAR string.
const char * getReadName()
Returns the SAM formatted Read Name (QNAME).
bool setQuality(const char *quality)
Sets the quality (QUAL) to the specified SAM formatted quality string.
const char * getQuality()
Returns the SAM formatted quality string (QUAL).
const char * getSequence()
Returns the SAM formatted sequence string (SEQ), translating the base as specified by setSequenceTran...
The SamValidationErrors class is a container class that holds SamValidationError Objects,...
static bool isValid(SamFileHeader &samHeader, SamRecord &samRecord, SamValidationErrors &validationErrors)
Validates whether or not the specified SamRecord is valid, calling all of the other validations.
@ NO_MORE_RECS
NO_MORE_RECS: failed to read a record since there are no more to read either in the file or section i...
Structure of a BAM record.