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