libStatGen Software  1
TestValidate.cpp
00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include "TestValidate.h"
00019 #include "BaseUtilities.h"
00020 
00021 const std::string TestValidate::READ1_CIGAR = "5M2D";
00022 const std::string TestValidate::READ1_SEQ = "CCGAA";
00023 const std::string TestValidate::READ1_QUAL = "6>6+4";
00024 
00025 const std::string TestValidate::READ6_CIGAR = "3S2H5M";
00026 const std::string TestValidate::READ6_SEQ = "TGCACGTN";
00027 const std::string TestValidate::READ6_QUAL = "453;>>>>";
00028 
00029 const std::string TestValidate::READ7_CIGAR = "3S5M1S3H";
00030 const std::string TestValidate::READ7_SEQ = "TGCACGTNG";
00031 const std::string TestValidate::READ7_QUAL = "453;>>>>5";
00032 
00033 void validateRead1(SamRecord& samRecord)
00034 {
00035     //////////////////////////////////////////
00036     // Validate Record 1
00037     // Create record structure for validating.
00038     int expectedBlockSize = 89;
00039     const char* expectedReferenceName = "1";
00040     const char* expectedMateReferenceName = "1";
00041     const char* expectedMateReferenceNameOrEqual = "=";
00042 
00043     bamRecordStruct* expectedRecordPtr =
00044         (bamRecordStruct *) malloc(expectedBlockSize + sizeof(int));
00045 
00046     char tag[3];
00047     char type;
00048     void* value;
00049     const bamRecordStruct* bufferPtr;
00050     unsigned char* varPtr;
00051 
00052     expectedRecordPtr->myBlockSize = expectedBlockSize;
00053     expectedRecordPtr->myReferenceID = 0;
00054     expectedRecordPtr->myPosition = TestValidate::READ1_POS;
00055     expectedRecordPtr->myReadNameLength = 23;
00056     expectedRecordPtr->myMapQuality = 0;
00057     expectedRecordPtr->myBin = 4681;
00058     expectedRecordPtr->myCigarLength = 2;
00059     expectedRecordPtr->myFlag = 73;
00060     expectedRecordPtr->myReadLength = 5;
00061     expectedRecordPtr->myMateReferenceID = 0;
00062     expectedRecordPtr->myMatePosition = 1010;
00063     expectedRecordPtr->myInsertSize = 0;
00064    
00065     assert(samRecord.getString("MD") == "37");
00066     assert(samRecord.getString("YZ") == "");
00067     assert(samRecord.getInteger("YZ") == -1);
00068     float tmpFloat = -1;
00069     assert(samRecord.getFloatTag("YZ", tmpFloat) == false);
00070     // Check the alignment end
00071     assert(samRecord.get0BasedAlignmentEnd() == TestValidate::READ1_ALIGN_END);
00072     assert(samRecord.get1BasedAlignmentEnd() == (TestValidate::READ1_ALIGN_END + 1));
00073     assert(samRecord.getAlignmentLength() == TestValidate::READ1_ALIGN_LEN);
00074     assert(samRecord.get1BasedUnclippedStart() == (TestValidate::READ1_UNCLIP_START + 1));
00075     assert(samRecord.get0BasedUnclippedStart() == TestValidate::READ1_UNCLIP_START);
00076     assert(samRecord.get1BasedUnclippedEnd() == (TestValidate::READ1_UNCLIP_END + 1));
00077     assert(samRecord.get0BasedUnclippedEnd() == TestValidate::READ1_UNCLIP_END);
00078 
00079     // Check the accessors.
00080     assert(samRecord.getBlockSize() == expectedRecordPtr->myBlockSize);
00081     assert(samRecord.getReferenceID() == expectedRecordPtr->myReferenceID);
00082     assert(strcmp(samRecord.getReferenceName(), expectedReferenceName) == 0);
00083     assert(samRecord.get1BasedPosition() == expectedRecordPtr->myPosition + 1);
00084     assert(samRecord.get0BasedPosition() == expectedRecordPtr->myPosition);
00085     assert(samRecord.getReadNameLength() == 
00086            expectedRecordPtr->myReadNameLength);
00087     assert(samRecord.getMapQuality() == expectedRecordPtr->myMapQuality);
00088     assert(samRecord.getBin() == expectedRecordPtr->myBin);
00089     assert(samRecord.getCigarLength() == expectedRecordPtr->myCigarLength);
00090     assert(samRecord.getFlag() == expectedRecordPtr->myFlag);
00091     assert(samRecord.getReadLength() == expectedRecordPtr->myReadLength);
00092     assert(samRecord.getMateReferenceID() == 
00093            expectedRecordPtr->myMateReferenceID);
00094     assert(strcmp(samRecord.getMateReferenceName(),
00095                   expectedMateReferenceName) == 0);
00096     assert(strcmp(samRecord.getMateReferenceNameOrEqual(),
00097                   expectedMateReferenceNameOrEqual) == 0);
00098     assert(samRecord.get1BasedMatePosition() ==
00099            expectedRecordPtr->myMatePosition + 1);
00100     assert(samRecord.get0BasedMatePosition() == 
00101            expectedRecordPtr->myMatePosition);
00102     assert(samRecord.getInsertSize() == expectedRecordPtr->myInsertSize);
00103     assert(strcmp(samRecord.getReadName(), "1:1011:F:255+17M15D20M") == 0);
00104     assert(samRecord.getCigar() == TestValidate::READ1_CIGAR);
00105     assert(samRecord.getSequence() == TestValidate::READ1_SEQ);
00106     assert(samRecord.getQuality() == TestValidate::READ1_QUAL);
00107 
00108     assert(samRecord.getSequence(0) == 'C');
00109     assert(samRecord.getQuality(0) == '6');
00110     assert(samRecord.getSequence(1) == 'C');
00111     assert(samRecord.getQuality(1) == '>');
00112     assert(samRecord.getSequence(2) == 'G');
00113     assert(samRecord.getQuality(2) == '6');
00114     assert(samRecord.getSequence(3) == 'A');
00115     assert(samRecord.getQuality(3) == '+');
00116     assert(samRecord.getSequence(4) == 'A');
00117     assert(samRecord.getQuality(4) == '4');
00118 
00119     bool caught = false;
00120     try
00121     {
00122         samRecord.getSequence(-1);
00123     }
00124     catch (std::exception& e) 
00125     {
00126         caught = true;
00127         assert(strcmp(e.what(), "SamRecord::getSequence(-1) is out of range. Index must be between 0 and 4") == 0);
00128     }
00129     assert(caught == true);
00130     caught = false;
00131     try
00132     {
00133         samRecord.getQuality(-1);
00134     }
00135     catch (std::exception& e) 
00136     {
00137         caught = true;
00138         assert(strcmp(e.what(), "SamRecord::getQuality(-1) is out of range. Index must be between 0 and 4") == 0);
00139     }
00140     assert(caught == true);
00141     
00142     caught = false;
00143     try
00144     {
00145         samRecord.getSequence(5);
00146     }
00147     catch (std::exception& e) 
00148     {
00149         caught = true;
00150         assert(strcmp(e.what(), "SamRecord::getSequence(5) is out of range. Index must be between 0 and 4") == 0);
00151     }
00152     assert(caught == true);
00153     caught = false;
00154     try
00155     {
00156         samRecord.getQuality(5);
00157     }
00158     catch (std::exception& e) 
00159     {
00160         caught = true;
00161         assert(strcmp(e.what(), "SamRecord::getQuality(5) is out of range. Index must be between 0 and 4") == 0);
00162     }
00163     assert(caught == true);
00164     
00165     assert(samRecord.getNumOverlaps(1010, 1017) == 5);
00166     assert(samRecord.getNumOverlaps(1010, 1016) == 5);
00167     assert(samRecord.getNumOverlaps(1012, 1017) == 3);
00168     assert(samRecord.getNumOverlaps(1015, 1017) == 0);
00169     assert(samRecord.getNumOverlaps(1017, 1010) == 0);
00170     assert(samRecord.getNumOverlaps(1013, 1011) == 0);
00171     assert(samRecord.getNumOverlaps(-1, 1017) == 5);
00172     assert(samRecord.getNumOverlaps(1010, -1) == 5);
00173 
00174     // Check the tags.
00175     assert(samRecord.getNextSamTag(tag, type, &value) == true);
00176     assert(tag[0] == 'A');
00177     assert(tag[1] == 'M');
00178     assert(type == 'i');
00179     assert(*(char*)value == 0);
00180     assert(samRecord.getNextSamTag(tag, type, &value) == true);
00181     assert(tag[0] == 'M');
00182     assert(tag[1] == 'D');
00183     assert(type == 'Z');
00184     assert(*(String*)value == "37");
00185     assert(samRecord.getNextSamTag(tag, type, &value) == true);
00186     assert(tag[0] == 'N');
00187     assert(tag[1] == 'M');
00188     assert(type == 'i');
00189     assert(*(char*)value == 0);
00190     assert(samRecord.getNextSamTag(tag, type, &value) == true);
00191     assert(tag[0] == 'X');
00192     assert(tag[1] == 'T');
00193     assert(type == 'A');
00194     assert(*(char*)value == 'R');
00195     // No more tags, should return false.
00196     assert(samRecord.getNextSamTag(tag, type, &value) == false);
00197     assert(samRecord.getNextSamTag(tag, type, &value) == false);
00198 
00199     // Get the record ptr.   
00200     bufferPtr = (const bamRecordStruct*)samRecord.getRecordBuffer();
00201     // Validate the buffers match.
00202     assert(bufferPtr->myBlockSize == expectedRecordPtr->myBlockSize);
00203     assert(bufferPtr->myReferenceID == expectedRecordPtr->myReferenceID);
00204     assert(bufferPtr->myPosition == expectedRecordPtr->myPosition);
00205     assert(bufferPtr->myReadNameLength == expectedRecordPtr->myReadNameLength);
00206     assert(bufferPtr->myMapQuality == expectedRecordPtr->myMapQuality);
00207     assert(bufferPtr->myBin == expectedRecordPtr->myBin);
00208     assert(bufferPtr->myCigarLength == expectedRecordPtr->myCigarLength);
00209     assert(bufferPtr->myFlag == expectedRecordPtr->myFlag);
00210     assert(bufferPtr->myReadLength == expectedRecordPtr->myReadLength);
00211     assert(bufferPtr->myMateReferenceID == 
00212            expectedRecordPtr->myMateReferenceID);
00213     assert(bufferPtr->myMatePosition == expectedRecordPtr->myMatePosition);
00214     assert(bufferPtr->myInsertSize == expectedRecordPtr->myInsertSize);
00215 
00216     // Validate the variable length fields in the buffer.
00217     // Set the pointer to the start of the variable fields.
00218     varPtr = (unsigned char*)(&(bufferPtr->myData[0]));
00219 
00220     // Validate the readname.
00221     for(int i = 0; i < expectedRecordPtr->myReadNameLength; i++)
00222     {
00223         assert(*varPtr == samRecord.getReadName()[i]);
00224         varPtr++;
00225     }
00226 
00227     // Validate the cigar.
00228     // The First cigar is 5M which is 5 << 4 | 0 = 80
00229     assert(*(unsigned int*)varPtr == 80);
00230     // Increment the varptr the size of an int.
00231     varPtr += 4;
00232     // The 2nd cigar is 2D which is 2 << 4 | 2 = 34
00233     assert(*(unsigned int*)varPtr == 34);
00234     // Increment the varptr the size of an int.
00235     varPtr += 4;
00236    
00237     // Validate the sequence.
00238     // CC = 0x22
00239     assert(*varPtr == 0x22);
00240     varPtr++;
00241     // GA = 0x41
00242     assert(*varPtr == 0x41);
00243     varPtr++;
00244     // A  = 0x10
00245     assert(*varPtr == 0x10);
00246     varPtr++;
00247   
00248     // Validate the Quality
00249     for(int i = 0; i < expectedRecordPtr->myReadLength; i++)
00250     {
00251         assert(*varPtr == samRecord.getQuality()[i] - 33);
00252         varPtr++;
00253     }
00254 
00255     // Validate the tags.  
00256     assert(*varPtr == 'A');
00257     varPtr++;
00258     assert(*varPtr == 'M');
00259     varPtr++;
00260     assert(*varPtr == 'C');
00261     varPtr++;
00262     assert(*varPtr == 0);
00263     varPtr++;
00264     assert(*varPtr == 'M');
00265     varPtr++;
00266     assert(*varPtr == 'D');
00267     varPtr++;
00268     assert(*varPtr == 'Z');
00269     varPtr++;
00270     assert(*varPtr == '3');
00271     varPtr++;
00272     assert(*varPtr == '7');
00273     varPtr++;
00274     assert(*varPtr == 0);
00275     varPtr++;
00276     assert(*varPtr == 'N');
00277     varPtr++;
00278     assert(*varPtr == 'M');
00279     varPtr++;
00280     assert(*varPtr == 'C');
00281     varPtr++;
00282     assert(*varPtr == 0);
00283     varPtr++;
00284     assert(*varPtr == 'X');
00285     varPtr++;
00286     assert(*varPtr == 'T');
00287     varPtr++;
00288     assert(*varPtr == 'A');
00289     varPtr++;
00290     assert(*varPtr == 'R');
00291     varPtr++;
00292 }
00293 
00294 
00295 void validateRead2(SamRecord& samRecord)
00296 {
00297     //////////////////////////////////////////
00298     // Validate Record 2
00299     // Create record structure for validating.
00300     int expectedBlockSize = 61;
00301     const char* expectedReferenceName = "1";
00302     const char* expectedMateReferenceName = "1";
00303     const char* expectedMateReferenceNameOrEqual = "=";
00304 
00305     bamRecordStruct* expectedRecordPtr =
00306         (bamRecordStruct *) malloc(expectedBlockSize + sizeof(int));
00307 
00308     char tag[3];
00309     char type;
00310     void* value;
00311     bamRecordStruct* bufferPtr;
00312     unsigned char* varPtr;
00313 
00314     expectedRecordPtr->myBlockSize = expectedBlockSize;
00315     expectedRecordPtr->myReferenceID = 0;
00316     expectedRecordPtr->myPosition = TestValidate::READ2_POS;
00317     expectedRecordPtr->myReadNameLength = 23;
00318     expectedRecordPtr->myMapQuality = 0;
00319     expectedRecordPtr->myBin = 4681;
00320     expectedRecordPtr->myCigarLength = 0;
00321     expectedRecordPtr->myFlag = 133;
00322     expectedRecordPtr->myReadLength = 4;
00323     expectedRecordPtr->myMateReferenceID = 0;
00324     expectedRecordPtr->myMatePosition = 1010;
00325     expectedRecordPtr->myInsertSize = 0;
00326    
00327     // Check the fields.
00328     bamRecordStruct retrieveRecord;
00329     String retrieveReadName;
00330     String retrieveCigar;
00331     String retrieveSequence;
00332     String retrieveQuality;
00333 
00334     assert(samRecord.getFields(retrieveRecord, retrieveReadName, 
00335                                retrieveCigar, retrieveSequence,
00336                                retrieveQuality) == true);
00337     assert(retrieveRecord.myBlockSize == expectedRecordPtr->myBlockSize);
00338     assert(retrieveRecord.myReferenceID == expectedRecordPtr->myReferenceID);
00339     assert(retrieveRecord.myPosition == expectedRecordPtr->myPosition);
00340     assert(retrieveRecord.myReadNameLength == 
00341            expectedRecordPtr->myReadNameLength);
00342     assert(retrieveRecord.myMapQuality == expectedRecordPtr->myMapQuality);
00343     assert(retrieveRecord.myBin == expectedRecordPtr->myBin);
00344     assert(retrieveRecord.myCigarLength == expectedRecordPtr->myCigarLength);
00345     assert(retrieveRecord.myFlag == expectedRecordPtr->myFlag);
00346     assert(retrieveRecord.myReadLength == expectedRecordPtr->myReadLength);
00347     assert(retrieveRecord.myMateReferenceID == 
00348            expectedRecordPtr->myMateReferenceID);
00349     assert(retrieveRecord.myMatePosition == expectedRecordPtr->myMatePosition);
00350     assert(retrieveRecord.myInsertSize == expectedRecordPtr->myInsertSize);
00351 
00352     // Check the alignment end
00353     assert(samRecord.getAlignmentLength() == 0);
00354     assert(samRecord.get0BasedAlignmentEnd() == 1011);
00355     assert(samRecord.get1BasedAlignmentEnd() == 1012);
00356     assert(samRecord.get0BasedUnclippedStart() == 1011);
00357     assert(samRecord.get1BasedUnclippedStart() == 1012);
00358     assert(samRecord.get0BasedUnclippedEnd() == 1011);
00359     assert(samRecord.get1BasedUnclippedEnd() == 1012);
00360 
00361     // Check the accessors.
00362     assert(samRecord.getBlockSize() == expectedRecordPtr->myBlockSize);
00363     assert(samRecord.getReferenceID() == expectedRecordPtr->myReferenceID);
00364     assert(strcmp(samRecord.getReferenceName(), expectedReferenceName) == 0);
00365     assert(samRecord.get1BasedPosition() == expectedRecordPtr->myPosition + 1);
00366     assert(samRecord.get0BasedPosition() == expectedRecordPtr->myPosition);
00367     assert(samRecord.getReadNameLength() == 
00368            expectedRecordPtr->myReadNameLength);
00369     assert(samRecord.getMapQuality() == expectedRecordPtr->myMapQuality);
00370     assert(samRecord.getBin() == expectedRecordPtr->myBin);
00371     assert(samRecord.getCigarLength() == expectedRecordPtr->myCigarLength);
00372     assert(samRecord.getFlag() == expectedRecordPtr->myFlag);
00373     assert(samRecord.getReadLength() == expectedRecordPtr->myReadLength);
00374     assert(samRecord.getMateReferenceID() == 
00375            expectedRecordPtr->myMateReferenceID);
00376     assert(strcmp(samRecord.getMateReferenceName(), 
00377                   expectedMateReferenceName) == 0);
00378     assert(strcmp(samRecord.getMateReferenceNameOrEqual(), 
00379                   expectedMateReferenceNameOrEqual) == 0);
00380     assert(samRecord.get1BasedMatePosition() ==
00381            expectedRecordPtr->myMatePosition + 1);
00382     assert(samRecord.get0BasedMatePosition() == 
00383            expectedRecordPtr->myMatePosition);
00384     assert(samRecord.getInsertSize() == expectedRecordPtr->myInsertSize);
00385     assert(strcmp(samRecord.getReadName(), "1:1011:F:255+17M15D20M") == 0);
00386     assert(strcmp(samRecord.getCigar(), "*") == 0);
00387     assert(strcmp(samRecord.getSequence(), "CTGT") == 0);
00388     assert(strcmp(samRecord.getQuality(), ">>9>") == 0);
00389 
00390     assert(samRecord.getSequence(0) == 'C');
00391     assert(samRecord.getQuality(0) == '>');
00392     assert(samRecord.getSequence(1) == 'T');
00393     assert(samRecord.getQuality(1) == '>');
00394     assert(samRecord.getSequence(2) == 'G');
00395     assert(samRecord.getQuality(2) == '9');
00396     assert(samRecord.getSequence(3) == 'T');
00397     assert(samRecord.getQuality(3) == '>');
00398     bool caught = false;
00399     try
00400     {
00401         samRecord.getSequence(-1);
00402     }
00403     catch (std::exception& e) 
00404     {
00405         caught = true;
00406         assert(strcmp(e.what(), "SamRecord::getSequence(-1) is out of range. Index must be between 0 and 3") == 0);
00407     }
00408     assert(caught == true);
00409     caught = false;
00410     try
00411     {
00412         samRecord.getQuality(-1);
00413     }
00414     catch (std::exception& e) 
00415     {
00416         caught = true;
00417         assert(strcmp(e.what(), "SamRecord::getQuality(-1) is out of range. Index must be between 0 and 3") == 0);
00418     }
00419     assert(caught == true);
00420     
00421     caught = false;
00422     try
00423     {
00424         samRecord.getSequence(4);
00425     }
00426     catch (std::exception& e) 
00427     {
00428         caught = true;
00429         assert(strcmp(e.what(), "SamRecord::getSequence(4) is out of range. Index must be between 0 and 3") == 0);
00430     }
00431     assert(caught == true);
00432     caught = false;
00433     try
00434     {
00435         samRecord.getQuality(4);
00436     }
00437     catch (std::exception& e) 
00438     {
00439         caught = true;
00440         assert(strcmp(e.what(), "SamRecord::getQuality(4) is out of range. Index must be between 0 and 3") == 0);
00441     }
00442     assert(caught == true);
00443     
00444     assert(samRecord.getNumOverlaps(1011, 1017) == 0);
00445     assert(samRecord.getNumOverlaps(0, 1116) == 0);
00446 
00447     // No Tags to check, should return false.
00448     assert(samRecord.getNextSamTag(tag, type, &value) == false);
00449     assert(samRecord.getNextSamTag(tag, type, &value) == false);
00450 
00451     // Get the record ptr.   
00452     bufferPtr = (bamRecordStruct*)samRecord.getRecordBuffer();
00453     // Validate the buffers match.
00454     assert(bufferPtr->myBlockSize == expectedRecordPtr->myBlockSize);
00455     assert(bufferPtr->myReferenceID == expectedRecordPtr->myReferenceID);
00456     assert(bufferPtr->myPosition == expectedRecordPtr->myPosition);
00457     assert(bufferPtr->myReadNameLength == expectedRecordPtr->myReadNameLength);
00458     assert(bufferPtr->myMapQuality == expectedRecordPtr->myMapQuality);
00459     assert(bufferPtr->myBin == expectedRecordPtr->myBin);
00460     assert(bufferPtr->myCigarLength == expectedRecordPtr->myCigarLength);
00461     assert(bufferPtr->myFlag == expectedRecordPtr->myFlag);
00462     assert(bufferPtr->myReadLength == expectedRecordPtr->myReadLength);
00463     assert(bufferPtr->myMateReferenceID == 
00464            expectedRecordPtr->myMateReferenceID);
00465     assert(bufferPtr->myMatePosition == expectedRecordPtr->myMatePosition);
00466     assert(bufferPtr->myInsertSize == expectedRecordPtr->myInsertSize);
00467 
00468     // Validate the variable length fields in the buffer.
00469     // Set the pointer to the start of the variable fields.
00470     varPtr = (unsigned char*)(&(bufferPtr->myData[0]));
00471 
00472     // Validate the readname.
00473     for(int i = 0; i < expectedRecordPtr->myReadNameLength; i++)
00474     {
00475         assert(*varPtr == samRecord.getReadName()[i]);
00476         varPtr++;
00477     }
00478 
00479     // No cigar to validate. 
00480     // Validate the sequence.
00481     // CT = 0x28
00482     assert(*varPtr == 0x28);
00483     varPtr++;
00484     // GT = 0x48
00485     assert(*varPtr == 0x48);
00486     varPtr++;
00487 
00488     // Validate the Quality
00489     for(int i = 0; i < expectedRecordPtr->myReadLength; i++)
00490     {
00491         assert(*varPtr == samRecord.getQuality()[i] - 33);
00492         varPtr++;
00493     }
00494 
00495     // No tags.  
00496 }
00497 
00498 
00499 void validateRead3(SamRecord& samRecord)
00500 {
00501     //////////////////////////////////////////
00502     // Validate Record 3
00503     // Create record structure for validating.
00504     int expectedBlockSize = 87;
00505     const char* expectedReferenceName = "1";
00506     const char* expectedMateReferenceName = "18";
00507     const char* expectedMateReferenceNameOrEqual = "18";
00508 
00509     bamRecordStruct* expectedRecordPtr =
00510         (bamRecordStruct *) malloc(expectedBlockSize + sizeof(int));
00511 
00512     char tag[3];
00513     char type;
00514     void* value;
00515     bamRecordStruct* bufferPtr;
00516     unsigned char* varPtr;
00517 
00518     expectedRecordPtr->myBlockSize = expectedBlockSize;
00519     expectedRecordPtr->myReferenceID = 0;
00520     expectedRecordPtr->myPosition = 74;
00521     expectedRecordPtr->myReadNameLength = 21;
00522     expectedRecordPtr->myMapQuality = 0;
00523     expectedRecordPtr->myBin = 4681;
00524     expectedRecordPtr->myCigarLength = 1;
00525     expectedRecordPtr->myFlag = 97;
00526     expectedRecordPtr->myReadLength = 5;
00527     expectedRecordPtr->myMateReferenceID = 17;
00528     expectedRecordPtr->myMatePosition = 756;
00529     expectedRecordPtr->myInsertSize = 0;
00530    
00531     // Check the accessors.
00532     assert(samRecord.getBlockSize() == expectedRecordPtr->myBlockSize);
00533     assert(samRecord.getReferenceID() == expectedRecordPtr->myReferenceID);
00534     assert(strcmp(samRecord.getReferenceName(), expectedReferenceName) == 0);
00535     assert(samRecord.get1BasedPosition() == expectedRecordPtr->myPosition + 1);
00536     assert(samRecord.get0BasedPosition() == expectedRecordPtr->myPosition);
00537     assert(samRecord.getReadNameLength() == 
00538            expectedRecordPtr->myReadNameLength);
00539     assert(samRecord.getMapQuality() == expectedRecordPtr->myMapQuality);
00540     assert(samRecord.getBin() == expectedRecordPtr->myBin);
00541     assert(samRecord.getCigarLength() == expectedRecordPtr->myCigarLength);
00542     assert(samRecord.getFlag() == expectedRecordPtr->myFlag);
00543     assert(samRecord.getReadLength() == expectedRecordPtr->myReadLength);
00544     assert(samRecord.getMateReferenceID() == 
00545            expectedRecordPtr->myMateReferenceID);
00546     assert(strcmp(samRecord.getMateReferenceName(), 
00547                   expectedMateReferenceName) == 0);
00548     assert(strcmp(samRecord.getMateReferenceNameOrEqual(),
00549                   expectedMateReferenceNameOrEqual) == 0);
00550     assert(samRecord.get1BasedMatePosition() == 
00551            expectedRecordPtr->myMatePosition + 1);
00552     assert(samRecord.get0BasedMatePosition() ==
00553            expectedRecordPtr->myMatePosition);
00554     assert(samRecord.getInsertSize() == expectedRecordPtr->myInsertSize);
00555     assert(strcmp(samRecord.getReadName(), "18:462+29M5I3M:F:295") == 0);
00556     assert(strcmp(samRecord.getCigar(), "5M") == 0);
00557     assert(strcmp(samRecord.getSequence(), "ACGTN") == 0);
00558     assert(strcmp(samRecord.getQuality(), ";>>>>") == 0);
00559     assert(samRecord.getNumOverlaps(74, 79) == 5);
00560     assert(samRecord.getNumOverlaps(74, 78) == 4);
00561     assert(samRecord.getNumOverlaps(73, 79) == 5);
00562     assert(samRecord.getNumOverlaps(75, 79) == 4);
00563     assert(samRecord.getNumOverlaps(0, 179) == 5);
00564     assert(samRecord.getNumOverlaps(0, 19) == 0);
00565 
00566     // Check the alignment end
00567     assert(samRecord.get0BasedAlignmentEnd() == 78);
00568     assert(samRecord.get1BasedAlignmentEnd() == 79);
00569     assert(samRecord.getAlignmentLength() == 5);
00570     assert(samRecord.get0BasedUnclippedStart() == 74);
00571     assert(samRecord.get1BasedUnclippedStart() == 75);
00572     assert(samRecord.get0BasedUnclippedEnd() == 78);
00573     assert(samRecord.get1BasedUnclippedEnd() == 79);
00574 
00575 
00576     // Check the tags.
00577     assert(samRecord.getNextSamTag(tag, type, &value) == true);
00578     assert(tag[0] == 'A');
00579     assert(tag[1] == 'M');
00580     assert(type == 'i');
00581     assert(*(char*)value == 0);
00582     assert(samRecord.getNextSamTag(tag, type, &value) == true);
00583     assert(tag[0] == 'M');
00584     assert(tag[1] == 'D');
00585     assert(type == 'Z');
00586     assert(*(String*)value == "30A0C5");
00587     assert(samRecord.getNextSamTag(tag, type, &value) == true);
00588     assert(tag[0] == 'N');
00589     assert(tag[1] == 'M');
00590     assert(type == 'i');
00591     assert(*(char*)value == 2);
00592     assert(samRecord.getNextSamTag(tag, type, &value) == true);
00593     assert(tag[0] == 'X');
00594     assert(tag[1] == 'T');
00595     assert(type == 'A');
00596     assert(*(char*)value == 'R');
00597     // No more tags, should return false.
00598     assert(samRecord.getNextSamTag(tag, type, &value) == false);
00599     assert(samRecord.getNextSamTag(tag, type, &value) == false);
00600 
00601     // Get the record ptr.   
00602     bufferPtr = (bamRecordStruct*)samRecord.getRecordBuffer();
00603     // Validate the buffers match.
00604     assert(bufferPtr->myBlockSize == expectedRecordPtr->myBlockSize);
00605     assert(bufferPtr->myReferenceID == expectedRecordPtr->myReferenceID);
00606     assert(bufferPtr->myPosition == expectedRecordPtr->myPosition);
00607     assert(bufferPtr->myReadNameLength == expectedRecordPtr->myReadNameLength);
00608     assert(bufferPtr->myMapQuality == expectedRecordPtr->myMapQuality);
00609     assert(bufferPtr->myBin == expectedRecordPtr->myBin);
00610     assert(bufferPtr->myCigarLength == expectedRecordPtr->myCigarLength);
00611     assert(bufferPtr->myFlag == expectedRecordPtr->myFlag);
00612     assert(bufferPtr->myReadLength == expectedRecordPtr->myReadLength);
00613     assert(bufferPtr->myMateReferenceID == 
00614            expectedRecordPtr->myMateReferenceID);
00615     assert(bufferPtr->myMatePosition == expectedRecordPtr->myMatePosition);
00616     assert(bufferPtr->myInsertSize == expectedRecordPtr->myInsertSize);
00617 
00618     // Validate the variable length fields in the buffer.
00619     // Set the pointer to the start of the variable fields.
00620     varPtr = (unsigned char*)(&(bufferPtr->myData[0]));
00621 
00622     // Validate the readname.
00623     for(int i = 0; i < expectedRecordPtr->myReadNameLength; i++)
00624     {
00625         assert(*varPtr == samRecord.getReadName()[i]);
00626         varPtr++;
00627     }
00628 
00629     // Validate the cigar.
00630     // The cigar is 5M which is 5 << 4 | 0 = 80
00631     assert(*(unsigned int*)varPtr == 80);
00632     // Increment the varptr the size of an int.
00633     varPtr += 4;
00634    
00635     // Validate the sequence.
00636     // AC = 0x12
00637     assert(*varPtr == 0x12);
00638     varPtr++;
00639     // GT = 0x48
00640     assert(*varPtr == 0x48);
00641     varPtr++;
00642     // N  = 0xF0
00643     assert(*varPtr == 0xF0);
00644     varPtr++;
00645 
00646     // Validate the Quality
00647     for(int i = 0; i < expectedRecordPtr->myReadLength; i++)
00648     {
00649         assert(*varPtr == samRecord.getQuality()[i] - 33);
00650         varPtr++;
00651     }
00652 
00653     // Validate the tags.  
00654     assert(*varPtr == 'A');
00655     varPtr++;
00656     assert(*varPtr == 'M');
00657     varPtr++;
00658     assert(*varPtr == 'C');
00659     varPtr++;
00660     assert(*varPtr == 0);
00661     varPtr++;
00662     assert(*varPtr == 'M');
00663     varPtr++;
00664     assert(*varPtr == 'D');
00665     varPtr++;
00666     assert(*varPtr == 'Z');
00667     varPtr++;
00668     assert(*varPtr == '3');
00669     varPtr++;
00670     assert(*varPtr == '0');
00671     varPtr++;
00672     assert(*varPtr == 'A');
00673     varPtr++;
00674     assert(*varPtr == '0');
00675     varPtr++;
00676     assert(*varPtr == 'C');
00677     varPtr++;
00678     assert(*varPtr == '5');
00679     varPtr++;
00680     assert(*varPtr == 0);
00681     varPtr++;
00682     assert(*varPtr == 'N');
00683     varPtr++;
00684     assert(*varPtr == 'M');
00685     varPtr++;
00686     assert(*varPtr == 'C');
00687     varPtr++;
00688     assert(*varPtr == 2);
00689     varPtr++;
00690     assert(*varPtr == 'X');
00691     varPtr++;
00692     assert(*varPtr == 'T');
00693     varPtr++;
00694     assert(*varPtr == 'A');
00695     varPtr++;
00696     assert(*varPtr == 'R');
00697     varPtr++;
00698 }
00699 
00700 
00701 void validateRead4(SamRecord& samRecord)
00702 {
00703     //////////////////////////////////////////
00704     // Validate Record 4
00705     // Create record structure for validating.
00706     int expectedBlockSize = 57;
00707     const char* expectedReferenceName = "1";
00708     const char* expectedMateReferenceName = "18";
00709     const char* expectedMateReferenceNameOrEqual = "18";
00710 
00711     bamRecordStruct* expectedRecordPtr =
00712         (bamRecordStruct *) malloc(expectedBlockSize + sizeof(int));
00713 
00714     char tag[3];
00715     char type;
00716     void* value;
00717     bamRecordStruct* bufferPtr;
00718     unsigned char* varPtr;
00719 
00720     expectedRecordPtr->myBlockSize = expectedBlockSize;
00721     expectedRecordPtr->myReferenceID = 0;
00722     expectedRecordPtr->myPosition = 74;
00723     expectedRecordPtr->myReadNameLength = 21;
00724     expectedRecordPtr->myMapQuality = 0;
00725     expectedRecordPtr->myBin = 4681;
00726     expectedRecordPtr->myCigarLength = 0;
00727     expectedRecordPtr->myFlag = 97;
00728     expectedRecordPtr->myReadLength = 0;
00729     expectedRecordPtr->myMateReferenceID = 17;
00730     expectedRecordPtr->myMatePosition = 756;
00731     expectedRecordPtr->myInsertSize = 0;
00732    
00733     // Check the alignment end
00734     assert(samRecord.get1BasedUnclippedEnd() == 75);
00735     assert(samRecord.get0BasedUnclippedEnd() == 74);
00736     assert(samRecord.get0BasedUnclippedStart() == 74);
00737     assert(samRecord.get1BasedUnclippedStart() == 75);
00738     assert(samRecord.get1BasedAlignmentEnd() == 75);
00739     assert(samRecord.get0BasedAlignmentEnd() == 74);
00740     assert(samRecord.getAlignmentLength() == 0);
00741 
00742     // Check the accessors.
00743     assert(samRecord.getBlockSize() == expectedRecordPtr->myBlockSize);
00744     assert(samRecord.getReferenceID() == expectedRecordPtr->myReferenceID);
00745     assert(strcmp(samRecord.getReferenceName(), expectedReferenceName) == 0);
00746     assert(samRecord.get1BasedPosition() == expectedRecordPtr->myPosition + 1);
00747     assert(samRecord.get0BasedPosition() == expectedRecordPtr->myPosition);
00748     assert(samRecord.getReadNameLength() ==
00749            expectedRecordPtr->myReadNameLength);
00750     assert(samRecord.getMapQuality() == expectedRecordPtr->myMapQuality);
00751     assert(samRecord.getBin() == expectedRecordPtr->myBin);
00752     assert(samRecord.getCigarLength() == expectedRecordPtr->myCigarLength);
00753     assert(samRecord.getFlag() == expectedRecordPtr->myFlag);
00754     assert(samRecord.getReadLength() == expectedRecordPtr->myReadLength);
00755     assert(samRecord.getMateReferenceID() ==
00756            expectedRecordPtr->myMateReferenceID);
00757     assert(strcmp(samRecord.getMateReferenceName(),
00758                   expectedMateReferenceName) == 0);
00759     assert(strcmp(samRecord.getMateReferenceNameOrEqual(),
00760                   expectedMateReferenceNameOrEqual) == 0);
00761     assert(samRecord.get1BasedMatePosition() ==
00762            expectedRecordPtr->myMatePosition + 1);
00763     assert(samRecord.get0BasedMatePosition() ==
00764            expectedRecordPtr->myMatePosition);
00765     assert(samRecord.getInsertSize() == expectedRecordPtr->myInsertSize);
00766     assert(strcmp(samRecord.getReadName(), "18:462+29M5I3M:F:295") == 0);
00767     assert(strcmp(samRecord.getCigar(), "*") == 0);
00768     assert(strcmp(samRecord.getSequence(), "*") == 0);
00769     assert(strcmp(samRecord.getQuality(), "*") == 0);
00770 
00771     bool caught = false;
00772     try
00773     {
00774         samRecord.getSequence(0);
00775     }
00776     catch (std::exception& e) 
00777     {
00778         caught = true;
00779         assert(strcmp(e.what(), "SamRecord::getSequence(0) is not allowed since sequence = '*'") == 0);
00780     }
00781     assert(caught == true);
00782     caught = false;
00783     try
00784     {
00785         assert(samRecord.getQuality(0) == BaseUtilities::UNKNOWN_QUALITY_CHAR);
00786     }
00787     catch (std::exception& e) 
00788     {
00789         caught = true;
00790     }
00791     assert(caught == false);
00792     try
00793     {
00794         samRecord.getSequence(-1);
00795     }
00796     catch (std::exception& e) 
00797     {
00798         caught = true;
00799         assert(strcmp(e.what(), "SamRecord::getSequence(-1) is not allowed since sequence = '*'") == 0);
00800     }
00801     assert(caught == true);
00802     caught = false;
00803     try
00804     {
00805         assert(samRecord.getQuality(-1) == BaseUtilities::UNKNOWN_QUALITY_CHAR);
00806     }
00807     catch (std::exception& e) 
00808     {
00809         caught = true;
00810     }
00811     assert(caught == false);
00812     
00813     caught = false;
00814     try
00815     {
00816         samRecord.getSequence(5);
00817     }
00818     catch (std::exception& e) 
00819     {
00820         caught = true;
00821         assert(strcmp(e.what(), "SamRecord::getSequence(5) is not allowed since sequence = '*'") == 0);
00822     }
00823     assert(caught == true);
00824     caught = false;
00825     try
00826     {
00827         assert(samRecord.getQuality(5) == BaseUtilities::UNKNOWN_QUALITY_CHAR);
00828     }
00829     catch (std::exception& e) 
00830     {
00831         caught = true;
00832     }
00833     assert(caught == false);
00834 
00835     assert(samRecord.getNumOverlaps(74, 79) == 0);
00836     assert(samRecord.getNumOverlaps(74, 78) == 0);
00837     assert(samRecord.getNumOverlaps(73, 79) == 0);
00838     assert(samRecord.getNumOverlaps(75, 79) == 0);
00839     assert(samRecord.getNumOverlaps(0, 179) == 0);
00840     assert(samRecord.getNumOverlaps(0, 19) == 0);
00841 
00842     // Check the tag.
00843     assert(samRecord.getNextSamTag(tag, type, &value) == true);
00844     assert(tag[0] == 'A');
00845     assert(tag[1] == 'M');
00846     assert(type == 'i');
00847     assert(*(char*)value == 0);
00848     // No more Tags to check, should return false.
00849     assert(samRecord.getNextSamTag(tag, type, &value) == false);
00850     assert(samRecord.getNextSamTag(tag, type, &value) == false);
00851 
00852     // Get the record ptr.   
00853     bufferPtr = (bamRecordStruct*)samRecord.getRecordBuffer();
00854     // Validate the buffers match.
00855     assert(bufferPtr->myBlockSize == expectedRecordPtr->myBlockSize);
00856     assert(bufferPtr->myReferenceID == expectedRecordPtr->myReferenceID);
00857     assert(bufferPtr->myPosition == expectedRecordPtr->myPosition);
00858     assert(bufferPtr->myReadNameLength == expectedRecordPtr->myReadNameLength);
00859     assert(bufferPtr->myMapQuality == expectedRecordPtr->myMapQuality);
00860     assert(bufferPtr->myBin == expectedRecordPtr->myBin);
00861     assert(bufferPtr->myCigarLength == expectedRecordPtr->myCigarLength);
00862     assert(bufferPtr->myFlag == expectedRecordPtr->myFlag);
00863     assert(bufferPtr->myReadLength == expectedRecordPtr->myReadLength);
00864     assert(bufferPtr->myMateReferenceID ==
00865            expectedRecordPtr->myMateReferenceID);
00866     assert(bufferPtr->myMatePosition == expectedRecordPtr->myMatePosition);
00867     assert(bufferPtr->myInsertSize == expectedRecordPtr->myInsertSize);
00868 
00869     // Validate the variable length fields in the buffer.
00870     // Set the pointer to the start of the variable fields.
00871     varPtr = (unsigned char*)(&(bufferPtr->myData[0]));
00872 
00873     // Validate the readname.
00874     for(int i = 0; i < expectedRecordPtr->myReadNameLength; i++)
00875     {
00876         assert(*varPtr == samRecord.getReadName()[i]);
00877         varPtr++;
00878     }
00879 
00880     // No cigar to validate. 
00881     // Validate the sequence.
00882     // No sequence.
00883     // No Quality.
00884 
00885     // Validate the tags.  
00886     assert(*varPtr == 'A');
00887     varPtr++;
00888     assert(*varPtr == 'M');
00889     varPtr++;
00890     assert(*varPtr == 'C');
00891     varPtr++;
00892     assert(*varPtr == 0);
00893     varPtr++;
00894 }
00895 
00896 
00897 void validateRead5(SamRecord& samRecord)
00898 {
00899     //////////////////////////////////////////
00900     // Validate Record 5
00901     int expectedBlockSize = 87;
00902     const char* expectedReferenceName = "2";
00903     const char* expectedMateReferenceName = "18";
00904     const char* expectedMateReferenceNameOrEqual = "18";
00905 
00906     bamRecordStruct* expectedRecordPtr =
00907         (bamRecordStruct *) malloc(expectedBlockSize + sizeof(int));
00908 
00909     char tag[3];
00910     char type;
00911     void* value;
00912     bamRecordStruct* bufferPtr;
00913     unsigned char* varPtr;
00914 
00915     expectedRecordPtr->myBlockSize = expectedBlockSize;
00916     expectedRecordPtr->myReferenceID = 1;
00917     expectedRecordPtr->myPosition = 74;
00918     expectedRecordPtr->myReadNameLength = 21;
00919     expectedRecordPtr->myMapQuality = 0;
00920     expectedRecordPtr->myBin = 4681;
00921     expectedRecordPtr->myCigarLength = 1;
00922     expectedRecordPtr->myFlag = 97;
00923     expectedRecordPtr->myReadLength = 5;
00924     expectedRecordPtr->myMateReferenceID = 17;
00925     expectedRecordPtr->myMatePosition = 756;
00926     expectedRecordPtr->myInsertSize = 0;
00927    
00928     // Check the accessors.
00929     assert(samRecord.getBlockSize() == expectedRecordPtr->myBlockSize);
00930     assert(samRecord.getReferenceID() == expectedRecordPtr->myReferenceID);
00931     assert(strcmp(samRecord.getReferenceName(), expectedReferenceName) == 0);
00932     assert(samRecord.get1BasedPosition() == expectedRecordPtr->myPosition + 1);
00933     assert(samRecord.get0BasedPosition() == expectedRecordPtr->myPosition);
00934     assert(samRecord.getReadNameLength() ==
00935            expectedRecordPtr->myReadNameLength);
00936     assert(samRecord.getMapQuality() == expectedRecordPtr->myMapQuality);
00937     assert(samRecord.getBin() == expectedRecordPtr->myBin);
00938     assert(samRecord.getCigarLength() == expectedRecordPtr->myCigarLength);
00939     assert(samRecord.getFlag() == expectedRecordPtr->myFlag);
00940     assert(samRecord.getReadLength() == expectedRecordPtr->myReadLength);
00941     assert(samRecord.getMateReferenceID() ==
00942            expectedRecordPtr->myMateReferenceID);
00943     assert(strcmp(samRecord.getMateReferenceName(),
00944                   expectedMateReferenceName) == 0);
00945     assert(strcmp(samRecord.getMateReferenceNameOrEqual(),
00946                   expectedMateReferenceNameOrEqual) == 0);
00947     assert(samRecord.get1BasedMatePosition() == 
00948            expectedRecordPtr->myMatePosition + 1);
00949     assert(samRecord.get0BasedMatePosition() ==
00950            expectedRecordPtr->myMatePosition);
00951     assert(samRecord.getInsertSize() == expectedRecordPtr->myInsertSize);
00952     assert(strcmp(samRecord.getReadName(), "18:462+29M5I3M:F:295") == 0);
00953     assert(strcmp(samRecord.getCigar(), "5M") == 0);
00954     assert(strcmp(samRecord.getSequence(), "ACGTN") == 0);
00955     assert(strcmp(samRecord.getQuality(), "*") == 0);
00956     assert(samRecord.getNumOverlaps(74, 79) == 5);
00957     assert(samRecord.getNumOverlaps(74, 78) == 4);
00958     assert(samRecord.getNumOverlaps(73, 79) == 5);
00959     assert(samRecord.getNumOverlaps(75, 79) == 4);
00960     assert(samRecord.getNumOverlaps(0, 179) == 5);
00961     assert(samRecord.getNumOverlaps(0, 19) == 0);
00962 
00963     assert(samRecord.getSequence(0) == 'A');
00964     char expChar = BaseUtilities::UNKNOWN_QUALITY_CHAR;
00965     assert(samRecord.getQuality(0) == expChar);
00966     assert(samRecord.getSequence(1) == 'C');
00967     assert(samRecord.getQuality(1) == expChar);
00968     assert(samRecord.getSequence(2) == 'G');
00969     assert(samRecord.getQuality(2) == expChar);
00970     assert(samRecord.getSequence(3) == 'T');
00971     assert(samRecord.getQuality(3) == expChar);
00972     assert(samRecord.getSequence(4) == 'N');
00973     assert(samRecord.getQuality(4) == expChar);
00974 
00975     bool caught = false;
00976     try
00977     {
00978         samRecord.getSequence(-1);
00979     }
00980     catch (std::exception& e) 
00981     {
00982         caught = true;
00983         assert(strcmp(e.what(), "SamRecord::getSequence(-1) is out of range. Index must be between 0 and 4") == 0);
00984     }
00985     assert(caught == true);
00986     caught = false;
00987     try
00988     {
00989         samRecord.getQuality(-1);
00990     }
00991     catch (std::exception& e) 
00992     {
00993         caught = true;
00994         assert(strcmp(e.what(), "SamRecord::getQuality(-1) is out of range. Index must be between 0 and 4") == 0);
00995     }
00996     assert(caught == true);
00997     
00998     caught = false;
00999     try
01000     {
01001         samRecord.getSequence(5);
01002     }
01003     catch (std::exception& e) 
01004     {
01005         caught = true;
01006         assert(strcmp(e.what(), "SamRecord::getSequence(5) is out of range. Index must be between 0 and 4") == 0);
01007     }
01008     assert(caught == true);
01009     caught = false;
01010     try
01011     {
01012         samRecord.getQuality(5);
01013     }
01014     catch (std::exception& e) 
01015     {
01016         caught = true;
01017         assert(strcmp(e.what(), "SamRecord::getQuality(5) is out of range. Index must be between 0 and 4") == 0);
01018     }
01019     assert(caught == true);
01020 
01021     // Check the tags.
01022     assert(samRecord.getNextSamTag(tag, type, &value) == true);
01023     assert(tag[0] == 'A');
01024     assert(tag[1] == 'M');
01025     assert(type == 'i');
01026     assert(*(char*)value == 0);
01027     assert(samRecord.getNextSamTag(tag, type, &value) == true);
01028     assert(tag[0] == 'M');
01029     assert(tag[1] == 'D');
01030     assert(type == 'Z');
01031     assert(*(String*)value == "30A0C5");
01032     assert(samRecord.getNextSamTag(tag, type, &value) == true);
01033     assert(tag[0] == 'N');
01034     assert(tag[1] == 'M');
01035     assert(type == 'i');
01036     assert(*(char*)value == 2);
01037     assert(samRecord.getNextSamTag(tag, type, &value) == true);
01038     assert(tag[0] == 'X');
01039     assert(tag[1] == 'T');
01040     assert(type == 'A');
01041     assert(*(char*)value == 'R');
01042     // No more tags, should return false.
01043     assert(samRecord.getNextSamTag(tag, type, &value) == false);
01044     assert(samRecord.getNextSamTag(tag, type, &value) == false);
01045 
01046     // Get the record ptr.   
01047     bufferPtr = (bamRecordStruct*)samRecord.getRecordBuffer();
01048     // Validate the buffers match.
01049     assert(bufferPtr->myBlockSize == expectedRecordPtr->myBlockSize);
01050     assert(bufferPtr->myReferenceID == expectedRecordPtr->myReferenceID);
01051     assert(bufferPtr->myPosition == expectedRecordPtr->myPosition);
01052     assert(bufferPtr->myReadNameLength == expectedRecordPtr->myReadNameLength);
01053     assert(bufferPtr->myMapQuality == expectedRecordPtr->myMapQuality);
01054     assert(bufferPtr->myBin == expectedRecordPtr->myBin);
01055     assert(bufferPtr->myCigarLength == expectedRecordPtr->myCigarLength);
01056     assert(bufferPtr->myFlag == expectedRecordPtr->myFlag);
01057     assert(bufferPtr->myReadLength == expectedRecordPtr->myReadLength);
01058     assert(bufferPtr->myMateReferenceID ==
01059            expectedRecordPtr->myMateReferenceID);
01060     assert(bufferPtr->myMatePosition == expectedRecordPtr->myMatePosition);
01061     assert(bufferPtr->myInsertSize == expectedRecordPtr->myInsertSize);
01062 
01063     // Validate the variable length fields in the buffer.
01064     // Set the pointer to the start of the variable fields.
01065     varPtr = (unsigned char*)(&(bufferPtr->myData[0]));
01066 
01067     // Validate the readname.
01068     for(int i = 0; i < expectedRecordPtr->myReadNameLength; i++)
01069     {
01070         assert(*varPtr == samRecord.getReadName()[i]);
01071         varPtr++;
01072     }
01073 
01074     // Validate the cigar.
01075     // The cigar is 5M which is 5 << 4 | 0 = 80
01076     assert(*(unsigned int*)varPtr == 80);
01077     // Increment the varptr the size of an int.
01078     varPtr += 4;
01079    
01080     // Validate the sequence.
01081     // AC = 0x12
01082     assert(*varPtr == 0x12);
01083     varPtr++;
01084     // GT = 0x48
01085     assert(*varPtr == 0x48);
01086     varPtr++;
01087     // N  = 0xF0
01088     assert(*varPtr == 0xF0);
01089     varPtr++;
01090 
01091     // Validate the Quality
01092     for(int i = 0; i < expectedRecordPtr->myReadLength; i++)
01093     {
01094         assert(*varPtr == 0xFF);
01095         varPtr++;
01096     }
01097 
01098     // Validate the tags.  
01099     assert(*varPtr == 'A');
01100     varPtr++;
01101     assert(*varPtr == 'M');
01102     varPtr++;
01103     assert(*varPtr == 'C');
01104     varPtr++;
01105     assert(*varPtr == 0);
01106     varPtr++;
01107     assert(*varPtr == 'M');
01108     varPtr++;
01109     assert(*varPtr == 'D');
01110     varPtr++;
01111     assert(*varPtr == 'Z');
01112     varPtr++;
01113     assert(*varPtr == '3');
01114     varPtr++;
01115     assert(*varPtr == '0');
01116     varPtr++;
01117     assert(*varPtr == 'A');
01118     varPtr++;
01119     assert(*varPtr == '0');
01120     varPtr++;
01121     assert(*varPtr == 'C');
01122     varPtr++;
01123     assert(*varPtr == '5');
01124     varPtr++;
01125     assert(*varPtr == 0);
01126     varPtr++;
01127     assert(*varPtr == 'N');
01128     varPtr++;
01129     assert(*varPtr == 'M');
01130     varPtr++;
01131     assert(*varPtr == 'C');
01132     varPtr++;
01133     assert(*varPtr == 2);
01134     varPtr++;
01135     assert(*varPtr == 'X');
01136     varPtr++;
01137     assert(*varPtr == 'T');
01138     varPtr++;
01139     assert(*varPtr == 'A');
01140     varPtr++;
01141     assert(*varPtr == 'R');
01142     varPtr++;
01143 }
01144 
01145 
01146 void validateRead6(SamRecord& samRecord)
01147 {
01148     //////////////////////////////////////////
01149     // Validate Record 6
01150     // Create record structure for validating.
01151     int expectedBlockSize = 77;
01152     const char* expectedReferenceName = "1";
01153     const char* expectedMateReferenceName = "18";
01154     const char* expectedMateReferenceNameOrEqual = "18";
01155 
01156     bamRecordStruct* expectedRecordPtr =
01157         (bamRecordStruct *) malloc(expectedBlockSize + sizeof(int));
01158 
01159     char tag[3];
01160     char type;
01161     void* value;
01162     bamRecordStruct* bufferPtr;
01163     unsigned char* varPtr;
01164 
01165     expectedRecordPtr->myBlockSize = expectedBlockSize;
01166     expectedRecordPtr->myReferenceID = 0;
01167     expectedRecordPtr->myPosition = TestValidate::READ6_POS;
01168     expectedRecordPtr->myReadNameLength = 21;
01169     expectedRecordPtr->myMapQuality = 0;
01170     expectedRecordPtr->myBin = 4681;
01171     expectedRecordPtr->myCigarLength = 3;
01172     expectedRecordPtr->myFlag = 97;
01173     expectedRecordPtr->myReadLength = 8;
01174     expectedRecordPtr->myMateReferenceID = 17;
01175     expectedRecordPtr->myMatePosition = 756;
01176     expectedRecordPtr->myInsertSize = 0;
01177    
01178     // Check the accessors.
01179     assert(samRecord.getBlockSize() == expectedRecordPtr->myBlockSize);
01180     assert(samRecord.getReferenceID() == expectedRecordPtr->myReferenceID);
01181     assert(strcmp(samRecord.getReferenceName(), expectedReferenceName) == 0);
01182     assert(samRecord.get1BasedPosition() == expectedRecordPtr->myPosition + 1);
01183     assert(samRecord.get0BasedPosition() == expectedRecordPtr->myPosition);
01184     assert(samRecord.getReadNameLength() == 
01185            expectedRecordPtr->myReadNameLength);
01186     assert(samRecord.getMapQuality() == expectedRecordPtr->myMapQuality);
01187     assert(samRecord.getBin() == expectedRecordPtr->myBin);
01188     assert(samRecord.getCigarLength() == expectedRecordPtr->myCigarLength);
01189     assert(samRecord.getFlag() == expectedRecordPtr->myFlag);
01190     assert(samRecord.getReadLength() == expectedRecordPtr->myReadLength);
01191     assert(samRecord.getMateReferenceID() ==
01192            expectedRecordPtr->myMateReferenceID);
01193     assert(strcmp(samRecord.getMateReferenceName(),
01194                   expectedMateReferenceName) == 0);
01195     assert(strcmp(samRecord.getMateReferenceNameOrEqual(),
01196                   expectedMateReferenceNameOrEqual) == 0);
01197     assert(samRecord.get1BasedMatePosition() ==
01198            expectedRecordPtr->myMatePosition + 1);
01199     assert(samRecord.get0BasedMatePosition() ==
01200            expectedRecordPtr->myMatePosition);
01201     assert(samRecord.getInsertSize() == expectedRecordPtr->myInsertSize);
01202     assert(strcmp(samRecord.getReadName(), "18:462+29M5I3M:F:296") == 0);
01203     assert(samRecord.getCigar() == TestValidate::READ6_CIGAR);
01204     assert(samRecord.getSequence() == TestValidate::READ6_SEQ);
01205     assert(samRecord.getQuality() == TestValidate::READ6_QUAL);
01206     assert(samRecord.getNumOverlaps(1750, 1755) == 5);
01207     assert(samRecord.getNumOverlaps(1750, 1754) == 4);
01208     assert(samRecord.getNumOverlaps(0, 2000) == 5);
01209     assert(samRecord.getNumOverlaps(1749, 1755) == 5);
01210     assert(samRecord.getNumOverlaps(1751, 1755) == 4);
01211     assert(samRecord.getNumOverlaps(0, 1752) == 2);
01212     assert(samRecord.getNumOverlaps(0, 19) == 0);
01213 
01214     // Check the alignment end
01215     assert(samRecord.get0BasedAlignmentEnd() == TestValidate::READ6_ALIGN_END);
01216     assert(samRecord.get1BasedAlignmentEnd() == (TestValidate::READ6_ALIGN_END + 1));
01217     assert(samRecord.getAlignmentLength() == TestValidate::READ6_ALIGN_LEN);
01218     assert(samRecord.get0BasedUnclippedStart() == TestValidate::READ6_UNCLIP_START);
01219     assert(samRecord.get1BasedUnclippedStart() == (TestValidate::READ6_UNCLIP_START + 1));
01220     assert(samRecord.get0BasedUnclippedEnd() == TestValidate::READ6_UNCLIP_END);
01221     assert(samRecord.get1BasedUnclippedEnd() == (TestValidate::READ6_UNCLIP_END + 1));
01222 
01223     // No tags.
01224     assert(samRecord.getNextSamTag(tag, type, &value) == false);
01225 
01226     // Get the record ptr.   
01227     bufferPtr = (bamRecordStruct*)samRecord.getRecordBuffer();
01228     // Validate the buffers match.
01229     assert(bufferPtr->myBlockSize == expectedRecordPtr->myBlockSize);
01230     assert(bufferPtr->myReferenceID == expectedRecordPtr->myReferenceID);
01231     assert(bufferPtr->myPosition == expectedRecordPtr->myPosition);
01232     assert(bufferPtr->myReadNameLength == expectedRecordPtr->myReadNameLength);
01233     assert(bufferPtr->myMapQuality == expectedRecordPtr->myMapQuality);
01234     assert(bufferPtr->myBin == expectedRecordPtr->myBin);
01235     assert(bufferPtr->myCigarLength == expectedRecordPtr->myCigarLength);
01236     assert(bufferPtr->myFlag == expectedRecordPtr->myFlag);
01237     assert(bufferPtr->myReadLength == expectedRecordPtr->myReadLength);
01238     assert(bufferPtr->myMateReferenceID ==
01239            expectedRecordPtr->myMateReferenceID);
01240     assert(bufferPtr->myMatePosition == expectedRecordPtr->myMatePosition);
01241     assert(bufferPtr->myInsertSize == expectedRecordPtr->myInsertSize);
01242 
01243     // Validate the variable length fields in the buffer.
01244     // Set the pointer to the start of the variable fields.
01245     varPtr = (unsigned char*)(&(bufferPtr->myData[0]));
01246 
01247     // Validate the readname.
01248     for(int i = 0; i < expectedRecordPtr->myReadNameLength; i++)
01249     {
01250         assert(*varPtr == samRecord.getReadName()[i]);
01251         varPtr++;
01252     }
01253 
01254     // Validate the cigar.
01255     // The cigar is 3S2H5M which is:
01256     // 3S: 3 << 4 | 4 = 0x34
01257     assert(*(unsigned int*)varPtr == 0x34);
01258     // Increment the varptr the size of an int.
01259     varPtr += 4;
01260     // 2H: 2 << 4 | 5 = 0x25
01261     assert(*(unsigned int*)varPtr == 0x25);
01262     // Increment the varptr the size of an int.
01263     varPtr += 4;
01264     // 5M: 5 << 4 | 0 = 0x50
01265     assert(*(unsigned int*)varPtr == 0x50);
01266     // Increment the varptr the size of an int.
01267     varPtr += 4;
01268    
01269     // Validate the sequence.
01270     // TG = 0x84
01271     assert(*varPtr == 0x84);
01272     varPtr++;
01273     // CA = 0x21
01274     assert(*varPtr == 0x21);
01275     varPtr++;
01276     // CG = 0x24
01277     assert(*varPtr == 0x24);
01278     varPtr++;
01279     // TN = 0x8F
01280     assert(*varPtr == 0x8F);
01281     varPtr++;
01282 
01283     // Validate the Quality
01284     for(int i = 0; i < expectedRecordPtr->myReadLength; i++)
01285     {
01286         assert(*varPtr == samRecord.getQuality()[i] - 33);
01287         varPtr++;
01288     }
01289 }
01290 
01291 
01292 void validateRead7(SamRecord& samRecord)
01293 {
01294     //////////////////////////////////////////
01295     // Validate Record 7
01296     // Create record structure for validating.
01297     int expectedBlockSize = 83;
01298     const char* expectedReferenceName = "2";
01299     const char* expectedMateReferenceName = "18";
01300     const char* expectedMateReferenceNameOrEqual = "18";
01301 
01302     bamRecordStruct* expectedRecordPtr =
01303         (bamRecordStruct *) malloc(expectedBlockSize + sizeof(int));
01304 
01305     char tag[3];
01306     char type;
01307     void* value;
01308     bamRecordStruct* bufferPtr;
01309     unsigned char* varPtr;
01310 
01311     expectedRecordPtr->myBlockSize = expectedBlockSize;
01312     expectedRecordPtr->myReferenceID = 1;
01313     expectedRecordPtr->myPosition = TestValidate::READ7_POS;
01314     expectedRecordPtr->myReadNameLength = 21;
01315     expectedRecordPtr->myMapQuality = 0;
01316     expectedRecordPtr->myBin = 4681;
01317     expectedRecordPtr->myCigarLength = 4;
01318     expectedRecordPtr->myFlag = 97;
01319     expectedRecordPtr->myReadLength = 9;
01320     expectedRecordPtr->myMateReferenceID = 17;
01321     expectedRecordPtr->myMatePosition = 756;
01322     expectedRecordPtr->myInsertSize = 0;
01323    
01324     // Check the accessors.
01325     assert(samRecord.getBlockSize() == expectedRecordPtr->myBlockSize);
01326     assert(samRecord.getReferenceID() == expectedRecordPtr->myReferenceID);
01327     assert(strcmp(samRecord.getReferenceName(), expectedReferenceName) == 0);
01328     assert(samRecord.get1BasedPosition() == expectedRecordPtr->myPosition + 1);
01329     assert(samRecord.get0BasedPosition() == expectedRecordPtr->myPosition);
01330     assert(samRecord.getReadNameLength() ==
01331            expectedRecordPtr->myReadNameLength);
01332     assert(samRecord.getMapQuality() == expectedRecordPtr->myMapQuality);
01333     assert(samRecord.getBin() == expectedRecordPtr->myBin);
01334     assert(samRecord.getCigarLength() == expectedRecordPtr->myCigarLength);
01335     assert(samRecord.getFlag() == expectedRecordPtr->myFlag);
01336     assert(samRecord.getReadLength() == expectedRecordPtr->myReadLength);
01337     assert(samRecord.getMateReferenceID() ==
01338            expectedRecordPtr->myMateReferenceID);
01339     assert(strcmp(samRecord.getMateReferenceName(),
01340                   expectedMateReferenceName) == 0);
01341     assert(strcmp(samRecord.getMateReferenceNameOrEqual(),
01342                   expectedMateReferenceNameOrEqual) == 0);
01343     assert(samRecord.get1BasedMatePosition() ==
01344            expectedRecordPtr->myMatePosition + 1);
01345     assert(samRecord.get0BasedMatePosition() ==
01346            expectedRecordPtr->myMatePosition);
01347     assert(samRecord.getInsertSize() == expectedRecordPtr->myInsertSize);
01348     assert(strcmp(samRecord.getReadName(), "18:462+29M5I3M:F:297") == 0);
01349     assert(samRecord.getCigar() == TestValidate::READ7_CIGAR);
01350     assert(samRecord.getSequence() == TestValidate::READ7_SEQ);
01351     assert(samRecord.getQuality() == TestValidate::READ7_QUAL);
01352     assert(samRecord.getNumOverlaps(1750, 1755) == 5);
01353     assert(samRecord.getNumOverlaps(1750, 1754) == 4);
01354     assert(samRecord.getNumOverlaps(0, 2000) == 5);
01355     assert(samRecord.getNumOverlaps(1749, 1755) == 5);
01356     assert(samRecord.getNumOverlaps(1751, 1755) == 4);
01357     assert(samRecord.getNumOverlaps(0, 1752) == 2);
01358     assert(samRecord.getNumOverlaps(0, 19) == 0);
01359 
01360     // Check the alignment end
01361     assert(samRecord.get0BasedAlignmentEnd() == TestValidate::READ7_ALIGN_END);
01362     assert(samRecord.get1BasedAlignmentEnd() == (TestValidate::READ7_ALIGN_END + 1));
01363     assert(samRecord.getAlignmentLength() == TestValidate::READ7_ALIGN_LEN);
01364     assert(samRecord.get0BasedUnclippedStart() == TestValidate::READ7_UNCLIP_START);
01365     assert(samRecord.get1BasedUnclippedStart() == (TestValidate::READ7_UNCLIP_START + 1));
01366     assert(samRecord.get0BasedUnclippedEnd() == TestValidate::READ7_UNCLIP_END);
01367     assert(samRecord.get1BasedUnclippedEnd() == (TestValidate::READ7_UNCLIP_END + 1));
01368 
01369     // No tags.
01370     assert(samRecord.getNextSamTag(tag, type, &value) == false);
01371 
01372     // Get the record ptr.   
01373     bufferPtr = (bamRecordStruct*)samRecord.getRecordBuffer();
01374     // Validate the buffers match.
01375     assert(bufferPtr->myBlockSize == expectedRecordPtr->myBlockSize);
01376     assert(bufferPtr->myReferenceID == expectedRecordPtr->myReferenceID);
01377     assert(bufferPtr->myPosition == expectedRecordPtr->myPosition);
01378     assert(bufferPtr->myReadNameLength == expectedRecordPtr->myReadNameLength);
01379     assert(bufferPtr->myMapQuality == expectedRecordPtr->myMapQuality);
01380     assert(bufferPtr->myBin == expectedRecordPtr->myBin);
01381     assert(bufferPtr->myCigarLength == expectedRecordPtr->myCigarLength);
01382     assert(bufferPtr->myFlag == expectedRecordPtr->myFlag);
01383     assert(bufferPtr->myReadLength == expectedRecordPtr->myReadLength);
01384     assert(bufferPtr->myMateReferenceID ==
01385            expectedRecordPtr->myMateReferenceID);
01386     assert(bufferPtr->myMatePosition == expectedRecordPtr->myMatePosition);
01387     assert(bufferPtr->myInsertSize == expectedRecordPtr->myInsertSize);
01388 
01389     // Validate the variable length fields in the buffer.
01390     // Set the pointer to the start of the variable fields.
01391     varPtr = (unsigned char*)(&(bufferPtr->myData[0]));
01392 
01393     // Validate the readname.
01394     for(int i = 0; i < expectedRecordPtr->myReadNameLength; i++)
01395     {
01396         assert(*varPtr == samRecord.getReadName()[i]);
01397         varPtr++;
01398     }
01399 
01400     // Validate the cigar.
01401     // The cigar is 3S5M1S3H which is:
01402     // 3S: 3 << 4 | 4 = 0x34
01403     assert(*(unsigned int*)varPtr == 0x34);
01404     // Increment the varptr the size of an int.
01405     varPtr += 4;
01406     // 5M: 5 << 4 | 0 = 0x50
01407     assert(*(unsigned int*)varPtr == 0x50);
01408     // Increment the varptr the size of an int.
01409     varPtr += 4;
01410     // 1S: 1 << 4 | 4 = 0x14
01411     assert(*(unsigned int*)varPtr == 0x14);
01412     // Increment the varptr the size of an int.
01413     varPtr += 4;
01414     // 3H: 3 << 4 | 5 = 0x35
01415     assert(*(unsigned int*)varPtr == 0x35);
01416     // Increment the varptr the size of an int.
01417     varPtr += 4;
01418    
01419     // Validate the sequence.
01420     // TG = 0x84
01421     assert(*varPtr == 0x84);
01422     varPtr++;
01423     // CA = 0x21
01424     assert(*varPtr == 0x21);
01425     varPtr++;
01426     // CG = 0x24
01427     assert(*varPtr == 0x24);
01428     varPtr++;
01429     // TN = 0x8F
01430     assert(*varPtr == 0x8F);
01431     varPtr++;
01432     // G  = 0x40
01433     assert(*varPtr == 0x40);
01434     varPtr++;
01435 
01436     // Validate the Quality
01437     for(int i = 0; i < expectedRecordPtr->myReadLength; i++)
01438     {
01439         assert(*varPtr == samRecord.getQuality()[i] - 33);
01440         varPtr++;
01441     }
01442 }
01443 
01444 
01445 void validateRead8(SamRecord& samRecord)
01446 {
01447     //////////////////////////////////////////
01448     // Validate Record 8
01449     // Create record structure for validating.
01450     int expectedBlockSize = 65;
01451     const char* expectedReferenceName = "*";
01452     const char* expectedMateReferenceName = "*";
01453     const char* expectedMateReferenceNameOrEqual = "*";
01454 
01455     bamRecordStruct* expectedRecordPtr =
01456         (bamRecordStruct *) malloc(expectedBlockSize + sizeof(int));
01457 
01458     char tag[3];
01459     char type;
01460     void* value;
01461     bamRecordStruct* bufferPtr;
01462     unsigned char* varPtr;
01463 
01464     expectedRecordPtr->myBlockSize = expectedBlockSize;
01465     expectedRecordPtr->myReferenceID = -1;
01466     expectedRecordPtr->myPosition = -1;
01467     expectedRecordPtr->myReadNameLength = 27;
01468     expectedRecordPtr->myMapQuality = 0;
01469     expectedRecordPtr->myBin = 4680;
01470     expectedRecordPtr->myCigarLength = 0;
01471     expectedRecordPtr->myFlag = 141;
01472     expectedRecordPtr->myReadLength = 4;
01473     expectedRecordPtr->myMateReferenceID = -1;
01474     expectedRecordPtr->myMatePosition = -1;
01475     expectedRecordPtr->myInsertSize = 0;
01476    
01477     // Check the alignment end
01478     assert(samRecord.get0BasedAlignmentEnd() == -1);
01479     assert(samRecord.get1BasedAlignmentEnd() == 0);
01480     assert(samRecord.getAlignmentLength() == 0);
01481     assert(samRecord.get0BasedUnclippedStart() == -1);
01482     assert(samRecord.get1BasedUnclippedStart() == 0);
01483     assert(samRecord.get0BasedUnclippedEnd() == -1);
01484     assert(samRecord.get1BasedUnclippedEnd() == 0);
01485 
01486     // Check the accessors.
01487     assert(samRecord.getBlockSize() == expectedRecordPtr->myBlockSize);
01488     assert(samRecord.getReferenceID() == expectedRecordPtr->myReferenceID);
01489     assert(strcmp(samRecord.getReferenceName(), expectedReferenceName) == 0);
01490     assert(samRecord.get1BasedPosition() == expectedRecordPtr->myPosition + 1);
01491     assert(samRecord.get0BasedPosition() == expectedRecordPtr->myPosition);
01492     assert(samRecord.getReadNameLength() ==
01493            expectedRecordPtr->myReadNameLength);
01494     assert(samRecord.getMapQuality() == expectedRecordPtr->myMapQuality);
01495     assert(samRecord.getBin() == expectedRecordPtr->myBin);
01496     assert(samRecord.getCigarLength() == expectedRecordPtr->myCigarLength);
01497     assert(samRecord.getFlag() == expectedRecordPtr->myFlag);
01498     assert(samRecord.getReadLength() == expectedRecordPtr->myReadLength);
01499     assert(samRecord.getMateReferenceID() ==
01500            expectedRecordPtr->myMateReferenceID);
01501     assert(strcmp(samRecord.getMateReferenceName(),
01502                   expectedMateReferenceName) == 0);
01503     assert(strcmp(samRecord.getMateReferenceNameOrEqual(),
01504                   expectedMateReferenceNameOrEqual) == 0);
01505     assert(samRecord.get1BasedMatePosition() == 
01506            expectedRecordPtr->myMatePosition + 1);
01507     assert(samRecord.get0BasedMatePosition() ==
01508            expectedRecordPtr->myMatePosition);
01509     assert(samRecord.getInsertSize() == expectedRecordPtr->myInsertSize);
01510     assert(strcmp(samRecord.getReadName(), "Y:16597235+13M13I11M:F:181") == 0);
01511     assert(strcmp(samRecord.getCigar(), "*") == 0);
01512     assert(strcmp(samRecord.getSequence(), "AACT") == 0);
01513     assert(strcmp(samRecord.getQuality(), "==;;") == 0);
01514     assert(samRecord.getNumOverlaps(1750, 1755) == 0);
01515     assert(samRecord.getNumOverlaps(1750, 1754) == 0);
01516     assert(samRecord.getNumOverlaps(0, 2000) == 0);
01517     assert(samRecord.getNumOverlaps(1749, 1755) == 0);
01518     assert(samRecord.getNumOverlaps(1751, 1755) == 0);
01519     assert(samRecord.getNumOverlaps(0, 1752) == 0);
01520     assert(samRecord.getNumOverlaps(0, 19) == 0);
01521     assert(samRecord.getNumOverlaps(-1, 4) == 0);
01522 
01523     // No Tags to check, should return false.
01524     assert(samRecord.getNextSamTag(tag, type, &value) == false);
01525     assert(samRecord.getNextSamTag(tag, type, &value) == false);
01526 
01527     // Get the record ptr.   
01528     bufferPtr = (bamRecordStruct*)samRecord.getRecordBuffer();
01529     // Validate the buffers match.
01530     assert(bufferPtr->myBlockSize == expectedRecordPtr->myBlockSize);
01531     assert(bufferPtr->myReferenceID == expectedRecordPtr->myReferenceID);
01532     assert(bufferPtr->myPosition == expectedRecordPtr->myPosition);
01533     assert(bufferPtr->myReadNameLength == expectedRecordPtr->myReadNameLength);
01534     assert(bufferPtr->myMapQuality == expectedRecordPtr->myMapQuality);
01535     assert(bufferPtr->myBin == expectedRecordPtr->myBin);
01536     assert(bufferPtr->myCigarLength == expectedRecordPtr->myCigarLength);
01537     assert(bufferPtr->myFlag == expectedRecordPtr->myFlag);
01538     assert(bufferPtr->myReadLength == expectedRecordPtr->myReadLength);
01539     assert(bufferPtr->myMateReferenceID ==
01540            expectedRecordPtr->myMateReferenceID);
01541     assert(bufferPtr->myMatePosition == expectedRecordPtr->myMatePosition);
01542     assert(bufferPtr->myInsertSize == expectedRecordPtr->myInsertSize);
01543 
01544     // Validate the variable length fields in the buffer.
01545     // Set the pointer to the start of the variable fields.
01546     varPtr = (unsigned char*)(&(bufferPtr->myData[0]));
01547 
01548     // Validate the readname.
01549     for(int i = 0; i < expectedRecordPtr->myReadNameLength; i++)
01550     {
01551         assert(*varPtr == samRecord.getReadName()[i]);
01552         varPtr++;
01553     }
01554 
01555     // No cigar to validate. 
01556     // Validate the sequence.
01557     // AA = 0x11
01558     assert(*varPtr == 0x11);
01559     varPtr++;
01560     // CT = 0x28
01561     assert(*varPtr == 0x28);
01562     varPtr++;
01563 
01564     // Validate the Quality
01565     for(int i = 0; i < expectedRecordPtr->myReadLength; i++)
01566     {
01567         assert(*varPtr == samRecord.getQuality()[i] - 33);
01568         varPtr++;
01569     }
01570 
01571     // No tags.  
01572 }
01573 
01574 
01575 void validateRead9(SamRecord& samRecord)
01576 {
01577     //////////////////////////////////////////
01578     // Validate Record 9
01579     // Create record structure for validating.
01580     int expectedBlockSize = 77;
01581     const char* expectedReferenceName = "3";
01582     const char* expectedMateReferenceName = "18";
01583     const char* expectedMateReferenceNameOrEqual = "18";
01584 
01585     bamRecordStruct* expectedRecordPtr =
01586         (bamRecordStruct *) malloc(expectedBlockSize + sizeof(int));
01587 
01588     char tag[3];
01589     char type;
01590     void* value;
01591     bamRecordStruct* bufferPtr;
01592     unsigned char* varPtr;
01593 
01594     expectedRecordPtr->myBlockSize = expectedBlockSize;
01595     expectedRecordPtr->myReferenceID = 2;
01596     expectedRecordPtr->myPosition = 74;
01597     expectedRecordPtr->myReadNameLength = 21;
01598     expectedRecordPtr->myMapQuality = 0;
01599     expectedRecordPtr->myBin = 4681;
01600     expectedRecordPtr->myCigarLength = 3;
01601     expectedRecordPtr->myFlag = 97;
01602     expectedRecordPtr->myReadLength = 8;
01603     expectedRecordPtr->myMateReferenceID = 17;
01604     expectedRecordPtr->myMatePosition = 756;
01605     expectedRecordPtr->myInsertSize = 0;
01606    
01607     // Check the accessors.
01608     assert(samRecord.getBlockSize() == expectedRecordPtr->myBlockSize);
01609     assert(samRecord.getStatus() == SamStatus::SUCCESS);
01610     assert(samRecord.getReferenceID() == expectedRecordPtr->myReferenceID);
01611     assert(strcmp(samRecord.getReferenceName(), expectedReferenceName) == 0);
01612     assert(samRecord.get1BasedPosition() == expectedRecordPtr->myPosition + 1);
01613     assert(samRecord.get0BasedPosition() == expectedRecordPtr->myPosition);
01614     assert(samRecord.getReadNameLength() ==
01615            expectedRecordPtr->myReadNameLength);
01616     assert(samRecord.getMapQuality() == expectedRecordPtr->myMapQuality);
01617     assert(samRecord.getBin() == expectedRecordPtr->myBin);
01618     assert(samRecord.getCigarLength() == expectedRecordPtr->myCigarLength);
01619     assert(samRecord.getFlag() == expectedRecordPtr->myFlag);
01620     assert(samRecord.getReadLength() == expectedRecordPtr->myReadLength);
01621     assert(samRecord.getMateReferenceID() ==
01622            expectedRecordPtr->myMateReferenceID);
01623     assert(strcmp(samRecord.getMateReferenceName(),
01624                   expectedMateReferenceName) == 0);
01625     assert(strcmp(samRecord.getMateReferenceNameOrEqual(),
01626                   expectedMateReferenceNameOrEqual) == 0);
01627     assert(samRecord.get1BasedMatePosition() ==
01628            expectedRecordPtr->myMatePosition + 1);
01629     assert(samRecord.get0BasedMatePosition() ==
01630            expectedRecordPtr->myMatePosition);
01631     assert(samRecord.getInsertSize() == expectedRecordPtr->myInsertSize);
01632     assert(strcmp(samRecord.getReadName(), "18:462+29M5I3M:F:298") == 0);
01633     assert(strcmp(samRecord.getCigar(), "3S5M4H") == 0);
01634     assert((strcmp(samRecord.getSequence(), "TGCACGTN") == 0) || 
01635            (strcmp(samRecord.getSequence(), "tgcacgtn") == 0));
01636     assert(strcmp(samRecord.getQuality(), "453;>>>>") == 0);
01637     assert(samRecord.getNumOverlaps(74, 79) == 5);
01638     assert(samRecord.getNumOverlaps(73, 79) == 5);
01639     assert(samRecord.getNumOverlaps(75, 78) == 3);
01640     assert(samRecord.getNumOverlaps(0, 1017) == 5);
01641     assert(samRecord.getNumOverlaps(79, 85) == 0);
01642     assert(samRecord.getNumOverlaps(78, 85) == 1);
01643     assert(samRecord.getNumOverlaps(-1, 1017) == 5);
01644 
01645     // Check the alignment end
01646     assert(samRecord.get0BasedAlignmentEnd() == 78);
01647     assert(samRecord.get1BasedAlignmentEnd() == 79);
01648     assert(samRecord.getAlignmentLength() == 5);
01649     assert(samRecord.get0BasedUnclippedStart() == 71);
01650     assert(samRecord.get1BasedUnclippedStart() == 72);
01651     assert(samRecord.get0BasedUnclippedEnd() == 82);
01652     assert(samRecord.get1BasedUnclippedEnd() == 83);
01653 
01654     // No tags.
01655     assert(samRecord.getNextSamTag(tag, type, &value) == false);
01656 
01657     // Get the record ptr.   
01658     bufferPtr = (bamRecordStruct*)samRecord.getRecordBuffer();
01659     assert(bufferPtr != NULL);
01660     // Validate the buffers match.
01661     assert(bufferPtr->myBlockSize == expectedRecordPtr->myBlockSize);
01662     assert(bufferPtr->myReferenceID == expectedRecordPtr->myReferenceID);
01663     assert(bufferPtr->myPosition == expectedRecordPtr->myPosition);
01664     assert(bufferPtr->myReadNameLength == expectedRecordPtr->myReadNameLength);
01665     assert(bufferPtr->myMapQuality == expectedRecordPtr->myMapQuality);
01666     assert(bufferPtr->myBin == expectedRecordPtr->myBin);
01667     assert(bufferPtr->myCigarLength == expectedRecordPtr->myCigarLength);
01668     assert(bufferPtr->myFlag == expectedRecordPtr->myFlag);
01669     assert(bufferPtr->myReadLength == expectedRecordPtr->myReadLength);
01670     assert(bufferPtr->myMateReferenceID == 
01671            expectedRecordPtr->myMateReferenceID);
01672     assert(bufferPtr->myMatePosition == expectedRecordPtr->myMatePosition);
01673     assert(bufferPtr->myInsertSize == expectedRecordPtr->myInsertSize);
01674 
01675     // Validate the variable length fields in the buffer.
01676     // Set the pointer to the start of the variable fields.
01677     varPtr = (unsigned char*)(&(bufferPtr->myData[0]));
01678 
01679     // Validate the readname.
01680     for(int i = 0; i < expectedRecordPtr->myReadNameLength; i++)
01681     {
01682         assert(*varPtr == samRecord.getReadName()[i]);
01683         varPtr++;
01684     }
01685 
01686     // Validate the cigar.
01687     // The cigar is 3S5M1S3H which is:
01688     // 3S: 3 << 4 | 4 = 0x34
01689     assert(*(unsigned int*)varPtr == 0x34);
01690     // Increment the varptr the size of an int.
01691     varPtr += 4;
01692     // 5M: 5 << 4 | 0 = 0x50
01693     assert(*(unsigned int*)varPtr == 0x50);
01694     // Increment the varptr the size of an int.
01695     varPtr += 4;
01696     // 4H: 4 << 4 | 5 = 0x45
01697     assert(*(unsigned int*)varPtr == 0x45);
01698     // Increment the varptr the size of an int.
01699     varPtr += 4;
01700    
01701     // Validate the sequence.
01702     // TG = 0x84
01703     assert(*varPtr == 0x84);
01704     varPtr++;
01705     // CA = 0x21
01706     assert(*varPtr == 0x21);
01707     varPtr++;
01708     // CG = 0x24
01709     assert(*varPtr == 0x24);
01710     varPtr++;
01711     // TN = 0x8F
01712     assert(*varPtr == 0x8F);
01713     varPtr++;
01714 
01715     // Validate the Quality
01716     for(int i = 0; i < expectedRecordPtr->myReadLength; i++)
01717     {
01718         assert(*varPtr == samRecord.getQuality()[i] - 33);
01719         varPtr++;
01720     }
01721 }
01722 
01723 
01724 void validateRead10(SamRecord& samRecord)
01725 {
01726     //////////////////////////////////////////
01727     // Validate Record 10
01728     // Create record structure for validating.
01729     int expectedBlockSize = 59;
01730     const char* expectedReferenceName = "*";
01731     const char* expectedMateReferenceName = "*";
01732     const char* expectedMateReferenceNameOrEqual = "*";
01733 
01734     bamRecordStruct* expectedRecordPtr =
01735         (bamRecordStruct *) malloc(expectedBlockSize + sizeof(int));
01736 
01737     char tag[3];
01738     char type;
01739     void* value;
01740     bamRecordStruct* bufferPtr;
01741     unsigned char* varPtr;
01742 
01743     expectedRecordPtr->myBlockSize = expectedBlockSize;
01744     expectedRecordPtr->myReferenceID = -1;
01745     expectedRecordPtr->myPosition = -1;
01746     expectedRecordPtr->myReadNameLength = 27;
01747     expectedRecordPtr->myMapQuality = 0;
01748     expectedRecordPtr->myBin = 4680;
01749     expectedRecordPtr->myCigarLength = 0;
01750     expectedRecordPtr->myFlag = 141;
01751     expectedRecordPtr->myReadLength = 0;
01752     expectedRecordPtr->myMateReferenceID = -1;
01753     expectedRecordPtr->myMatePosition = -1;
01754     expectedRecordPtr->myInsertSize = 0;
01755    
01756     // Check the alignment end
01757     assert(samRecord.get0BasedUnclippedStart() == -1);
01758     assert(samRecord.get1BasedUnclippedStart() == 0);
01759     assert(samRecord.get0BasedUnclippedEnd() == -1);
01760     assert(samRecord.get1BasedUnclippedEnd() == 0);
01761     assert(samRecord.get1BasedAlignmentEnd() == 0);
01762     assert(samRecord.get0BasedAlignmentEnd() == -1);
01763     assert(samRecord.getAlignmentLength() == 0);
01764 
01765     // Check the accessors.
01766     assert(samRecord.getBlockSize() == expectedRecordPtr->myBlockSize);
01767     assert(samRecord.getReferenceID() == expectedRecordPtr->myReferenceID);
01768     assert(strcmp(samRecord.getReferenceName(), expectedReferenceName) == 0);
01769     assert(samRecord.get1BasedPosition() == expectedRecordPtr->myPosition + 1);
01770     assert(samRecord.get0BasedPosition() == expectedRecordPtr->myPosition);
01771     assert(samRecord.getReadNameLength() == 
01772            expectedRecordPtr->myReadNameLength);
01773     assert(samRecord.getMapQuality() == expectedRecordPtr->myMapQuality);
01774     assert(samRecord.getBin() == expectedRecordPtr->myBin);
01775     assert(samRecord.getCigarLength() == expectedRecordPtr->myCigarLength);
01776     assert(samRecord.getFlag() == expectedRecordPtr->myFlag);
01777     assert(samRecord.getReadLength() == expectedRecordPtr->myReadLength);
01778     assert(samRecord.getMateReferenceID() == 
01779            expectedRecordPtr->myMateReferenceID);
01780     assert(strcmp(samRecord.getMateReferenceName(), 
01781                   expectedMateReferenceName) == 0);
01782     assert(strcmp(samRecord.getMateReferenceNameOrEqual(),
01783                   expectedMateReferenceNameOrEqual) == 0);
01784     assert(samRecord.get1BasedMatePosition() == 
01785            expectedRecordPtr->myMatePosition + 1);
01786     assert(samRecord.get0BasedMatePosition() == 
01787            expectedRecordPtr->myMatePosition);
01788     assert(samRecord.getInsertSize() == expectedRecordPtr->myInsertSize);
01789     assert(strcmp(samRecord.getReadName(), "Y:16597235+13M13I11M:F:181") == 0);
01790     assert(strcmp(samRecord.getCigar(), "*") == 0);
01791     assert(strcmp(samRecord.getSequence(), "*") == 0);
01792     assert(strcmp(samRecord.getQuality(), "*") == 0);
01793     assert(samRecord.getNumOverlaps(74, 79) == 0);
01794     assert(samRecord.getNumOverlaps(73, 79) == 0);
01795     assert(samRecord.getNumOverlaps(75, 78) == 0);
01796     assert(samRecord.getNumOverlaps(0, 1017) == 0);
01797     assert(samRecord.getNumOverlaps(79, 85) == 0);
01798     assert(samRecord.getNumOverlaps(78, 85) == 0);
01799     assert(samRecord.getNumOverlaps(-1, 1017) == 0);
01800 
01801     // No Tags to check, should return false.
01802     assert(samRecord.getNextSamTag(tag, type, &value) == false);
01803     assert(samRecord.getNextSamTag(tag, type, &value) == false);
01804 
01805     // Get the record ptr.   
01806     bufferPtr = (bamRecordStruct*)samRecord.getRecordBuffer();
01807     // Validate the buffers match.
01808     assert(bufferPtr->myBlockSize == expectedRecordPtr->myBlockSize);
01809     assert(bufferPtr->myReferenceID == expectedRecordPtr->myReferenceID);
01810     assert(bufferPtr->myPosition == expectedRecordPtr->myPosition);
01811     assert(bufferPtr->myReadNameLength == expectedRecordPtr->myReadNameLength);
01812     assert(bufferPtr->myMapQuality == expectedRecordPtr->myMapQuality);
01813     assert(bufferPtr->myBin == expectedRecordPtr->myBin);
01814     assert(bufferPtr->myCigarLength == expectedRecordPtr->myCigarLength);
01815     assert(bufferPtr->myFlag == expectedRecordPtr->myFlag);
01816     assert(bufferPtr->myReadLength == expectedRecordPtr->myReadLength);
01817     assert(bufferPtr->myMateReferenceID == 
01818            expectedRecordPtr->myMateReferenceID);
01819     assert(bufferPtr->myMatePosition == expectedRecordPtr->myMatePosition);
01820     assert(bufferPtr->myInsertSize == expectedRecordPtr->myInsertSize);
01821 
01822     // Validate the variable length fields in the buffer.
01823     // Set the pointer to the start of the variable fields.
01824     varPtr = (unsigned char*)(&(bufferPtr->myData[0]));
01825 
01826     // Validate the readname.
01827     for(int i = 0; i < expectedRecordPtr->myReadNameLength; i++)
01828     {
01829         assert(*varPtr == samRecord.getReadName()[i]);
01830         varPtr++;
01831     }
01832 
01833     // No cigar to validate. 
01834     // No sequence.
01835     // No Quality.
01836     // No Tags.
01837 }
01838 
01839 
01840 void validateHeader(SamFileHeader& samHeader)
01841 {
01842     validateHeaderFields(samHeader);
01843     validateHeaderString(samHeader);
01844 }
01845 
01846 
01847 void validateHeaderFields(SamFileHeader& samHeader)
01848 {
01849     const char* value;
01850 
01851     ////////////////////////////////////////////////////////
01852     // Test getting a specific HD Tag value from the header
01853     // that does not exist.
01854     value = samHeader.getHDTagValue("GO");
01855     assert(strcmp(value, "") == 0);
01856 
01857     ////////////////////////////////////////////////////////
01858     // Test getting a specific PG Tag value from the header
01859     // that does not exist.
01860     value = samHeader.getPGTagValue("CL", "1");
01861     assert(strcmp(value, "") == 0);
01862 
01863     ////////////////////////////////////////////////////////
01864     // Test getting a specific SQ Tag value from the header
01865     value = samHeader.getSQTagValue("LN", "1");
01866     assert(value != NULL);
01867     assert(strcmp(value, "247249719") == 0);
01868     value = samHeader.getSQTagValue("LN", "22");
01869     assert(value != NULL);
01870     assert(strcmp(value, "49691432") == 0);
01871 
01872     ////////////////////////////////////////////////////////
01873     // Test getting a specific SQ Tag value from the header
01874     // that does not exist.
01875     value = samHeader.getSQTagValue("LN", "1000");
01876     assert(strcmp(value, "") == 0);
01877 
01878     ////////////////////////////////////////////////////////
01879     // Test getting a specific SQ Tag value from the header
01880     // that does not exist - sq exists, but not with that tag.
01881     value = samHeader.getSQTagValue("AS", "1");
01882     assert(strcmp(value, "") == 0);
01883 
01884     ////////////////////////////////////////////////////////
01885     // Test getting a specific RG Tag value from the header
01886     value = samHeader.getRGTagValue("LB", "myID2");
01887     assert(value != NULL);
01888     assert(strcmp(value, "library2") == 0);
01889     value = samHeader.getRGTagValue("LB", "myID");
01890     assert(value != NULL);
01891     assert(strcmp(value, "library") == 0);
01892 
01893     ////////////////////////////////////////////////////////
01894     // Test getting a specific SQ from the header
01895     // Then pulling the tags out of it.
01896     SamHeaderSQ* sq = samHeader.getSQ("10");
01897     assert(strcmp(sq->getTagValue("SN"), "10") == 0);
01898     assert(strcmp(sq->getTagValue("LN"), "135374737") == 0);
01899    
01900     // Test pulling a tag that does not exist.
01901     assert(strcmp(sq->getTagValue("DD"), "") == 0);
01902    
01903 
01904     ////////////////////////////////////////////////////////
01905     // Test getting a specific RG from the header
01906     // Then pulling the tags out of it.
01907     const SamHeaderRG* rg = samHeader.getRG("myID");
01908     assert(strcmp(rg->getTagValue("ID"), "myID") == 0);
01909     assert(strcmp(rg->getTagValue("SM"), "sample") == 0);
01910     assert(strcmp(rg->getTagValue("LB"), "library") == 0);
01911    
01912     // Test pulling a tag that does not exist.
01913     assert(strcmp(rg->getTagValue("DD"), "") == 0);
01914    
01915     ////////////////////////////////////////////////////////
01916     // Test getting a specific RG from the header that does not exist.
01917     rg = samHeader.getRG("noExist");
01918     assert(rg == NULL);
01919 
01920     ////////////////////////////////////////////////////////
01921     // Test getting a specific SQ from the header that does not exist.
01922     sq = samHeader.getSQ("noExist");
01923     assert(sq == NULL);
01924 
01925     ////////////////////////////////////////////////////////
01926     // Test getting the reference ID.
01927     assert(samHeader.getReferenceID("2") == 1);
01928     std::string refIDStdString = "X";
01929     assert(samHeader.getReferenceID(refIDStdString.c_str()) == 22);
01930     String refIDString = "22";
01931     assert(samHeader.getReferenceID(refIDString) == 21);
01932     assert(samHeader.getReferenceID(refIDString.c_str()) == 21);
01933     assert(samHeader.getReferenceID("Z") == SamReferenceInfo::NO_REF_ID);
01934     assert(samHeader.getReferenceID("Z", true) == 23);
01935     assert(samHeader.getReferenceID("*") == -1);
01936     refIDString = "*";
01937     assert(samHeader.getReferenceID(refIDString) == -1);
01938     assert(samHeader.getReferenceID(refIDString.c_str()) == -1);
01939 }
01940 
01941 void validateHeaderString(SamFileHeader& samHeader)
01942 {
01943     // Check the header line.
01944     std::string headerString = "";
01945     assert(samHeader.getHeaderString(headerString) == true);
01946     assert(headerString == "@SQ\tSN:1\tLN:247249719\n@SQ\tSN:2\tLN:242951149\n@SQ\tSN:3\tLN:199501827\n@SQ\tSN:4\tLN:191273063\n@SQ\tSN:5\tLN:180857866\n@SQ\tSN:6\tLN:170899992\n@SQ\tSN:7\tLN:158821424\n@SQ\tSN:8\tLN:146274826\n@SQ\tSN:9\tLN:140273252\n@SQ\tSN:10\tLN:135374737\n@SQ\tSN:11\tLN:134452384\n@SQ\tSN:12\tLN:132349534\n@SQ\tSN:13\tLN:114142980\n@SQ\tSN:14\tLN:106368585\n@SQ\tSN:15\tLN:100338915\n@SQ\tSN:16\tLN:88827254\n@SQ\tSN:17\tLN:78774742\n@SQ\tSN:18\tLN:76117153\n@SQ\tSN:19\tLN:63811651\n@SQ\tSN:20\tLN:62435964\n@SQ\tSN:21\tLN:46944323\n@SQ\tSN:22\tLN:49691432\n@SQ\tSN:X\tLN:154913754\n@RG\tID:myID\tLB:library\tSM:sample\n@RG\tID:myID2\tSM:sample2\tLB:library2\n@CO\tComment 1\n@CO\tComment 2\n");
01947 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends