libStatGen Software  1
ValidationTest.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 "SamRecord.h"
00019 #include "SamValidation.h"
00020 #include "ValidationTest.h"
00021 #include <assert.h>
00022 
00023 void testSamQNAME()
00024 {
00025     // This method tests:
00026     //   QNAME.Length() > 0 and <= 254
00027     //   QNAME does not contain [ \t\n\r]
00028 
00029     char qname[256];
00030     SamFileHeader samHeader;
00031     SamRecord testRecord(ErrorHandler::RETURN);
00032     // Error list
00033     SamValidationErrors errorList;
00034    
00035     // Test Length == 0 by setting qname[0] to 0 (end of char*)
00036     qname[0] = 0;
00037     // It fails, because it is a required field.
00038     assert(testRecord.setReadName(qname) == false);
00039     assert(strcmp(testRecord.getReadName(), "UNKNOWN") == 0);
00040     // It was reset to the default which is valid.
00041     assert(SamValidator::isValid(samHeader, testRecord, errorList) == true);
00042     assert(errorList.numErrors() == 0);
00043     assert(errorList.getNextError() == NULL);
00044 
00045     // Test too long of a read name.
00046     memset(qname, '.', 255);
00047     qname[255] = 0;
00048     assert(testRecord.setReadName(qname) == true);
00049     assert(strcmp(testRecord.getReadName(), qname) == 0);
00050     assert(SamValidator::isValid(samHeader, testRecord, errorList) == false);
00051     // 2 errors - 1 since the qname is longer than 254 (it is 255).
00052     // and the qname length including the null is 256, but the 
00053     // read name length is only 8 bits, so that is a 1.
00054     assert(errorList.numErrors() == 2);
00055     assert(errorList.getNextError()->getType() == 
00056            SamValidationError::INVALID_QNAME);
00057     assert(errorList.getNextError()->getType() == 
00058            SamValidationError::INVALID_QNAME);
00059     assert(errorList.getNextError() == NULL);
00060 
00061     // Clear the error list
00062     errorList.clear();
00063 
00064     // Setup a buffer to set the record to.
00065     int bufferBlockSize = 32;
00066 
00067     bamRecordStruct* bufferRecordPtr =
00068         (bamRecordStruct *) malloc(bufferBlockSize + sizeof(int));
00069 
00070     bufferRecordPtr->myBlockSize = bufferBlockSize;
00071     bufferRecordPtr->myReferenceID = -1;
00072     bufferRecordPtr->myPosition = 1010;
00073     // Set the read name length to 0.
00074     bufferRecordPtr->myReadNameLength = 0;
00075     bufferRecordPtr->myMapQuality = 0;
00076     bufferRecordPtr->myBin = 4681;
00077     bufferRecordPtr->myCigarLength = 0;
00078     bufferRecordPtr->myFlag = 73;
00079     bufferRecordPtr->myReadLength = 0;
00080     bufferRecordPtr->myMateReferenceID = -1;
00081     bufferRecordPtr->myMatePosition = 1010;
00082     bufferRecordPtr->myInsertSize = 0;
00083 
00084     assert(testRecord.setBuffer((const char*)bufferRecordPtr, 
00085                                 bufferBlockSize + sizeof(int), 
00086                                 samHeader) == SamStatus::SUCCESS);
00087     // 1 error - the read name length is 0.
00088     assert(SamValidator::isValid(samHeader, testRecord, errorList) == false);
00089     assert(errorList.numErrors() == 1);
00090     assert(errorList.getNextError()->getType() == 
00091            SamValidationError::INVALID_QNAME);
00092     assert(errorList.getNextError() == NULL);
00093 
00094     // Clear the error list
00095     errorList.clear();
00096 
00097     // Test a buffer that has a read name, but the length specified is
00098     // longer than the first null.
00099     bufferBlockSize = 40;
00100     bufferRecordPtr->myBlockSize = bufferBlockSize;
00101     // Set the read name length to 8 - longer than 3 - "HI\0".
00102     bufferRecordPtr->myReadNameLength = 8;
00103     bufferRecordPtr->myData[0] = 'H';
00104     bufferRecordPtr->myData[1] = 'I';
00105     bufferRecordPtr->myData[2] = 0;
00106 
00107     assert(testRecord.setBuffer((const char*)bufferRecordPtr,
00108                                 bufferBlockSize + sizeof(int), 
00109                                 samHeader) == SamStatus::SUCCESS);
00110     // 1 error - the read name length in the buffer does not match the
00111     // length of the read name to the first null.
00112     assert(SamValidator::isValid(samHeader, testRecord, errorList) == false);
00113     assert(errorList.numErrors() == 1);
00114     assert(errorList.getNextError()->getType() ==
00115            SamValidationError::INVALID_QNAME);
00116     assert(errorList.getNextError() == NULL);
00117 
00118     // Clear the error list
00119     errorList.clear();
00120 
00121     // Test a buffer that has a read name, but the length specified is
00122     // shorter than the first null.
00123     bufferBlockSize = 34;
00124     bufferRecordPtr->myBlockSize = bufferBlockSize;
00125     // Set the read name length to 2 - longer than 3 - "HI\0"..
00126     bufferRecordPtr->myReadNameLength = 2;
00127     bufferRecordPtr->myData[0] = 'H';
00128     bufferRecordPtr->myData[1] = 'I';
00129     bufferRecordPtr->myData[2] = 0;
00130 
00131     assert(testRecord.setBuffer((const char*)bufferRecordPtr, 
00132                                 bufferBlockSize + sizeof(int), 
00133                                 samHeader) == SamStatus::SUCCESS);
00134     // 1 error - the read name length in the buffer does not match
00135     // the length of the read name to the first null.
00136     assert(SamValidator::isValid(samHeader, testRecord, errorList) == false);
00137     assert(errorList.numErrors() == 1);
00138     assert(errorList.getNextError()->getType() ==
00139            SamValidationError::INVALID_QNAME);
00140     assert(errorList.getNextError() == NULL);
00141 
00142     // Clear the error list
00143     errorList.clear();
00144 }
00145 
00146 
00147 void testBamRID()
00148 {
00149     // BAM
00150     SamRecord testRecord(ErrorHandler::RETURN);
00151     // Error list
00152     SamValidationErrors errorList;
00153     SamFileHeader samHeader;
00154 
00155     // Clear the error list
00156     errorList.clear();
00157 
00158     // Setup a buffer to set the record to.
00159     int bufferBlockSize = 35;
00160 
00161     bamRecordStruct* bufferRecordPtr =
00162         (bamRecordStruct *) malloc(bufferBlockSize + sizeof(int));
00163 
00164     bufferRecordPtr->myBlockSize = bufferBlockSize;
00165     bufferRecordPtr->myPosition = 1010;
00166     bufferRecordPtr->myReferenceID = -1;
00167     // Set the read name length to 0.
00168     bufferRecordPtr->myReadNameLength = 3;
00169     bufferRecordPtr->myMapQuality = 0;
00170     bufferRecordPtr->myBin = 4681;
00171     bufferRecordPtr->myCigarLength = 0;
00172     bufferRecordPtr->myFlag = 73;
00173     bufferRecordPtr->myReadLength = 0;
00174     bufferRecordPtr->myMateReferenceID = -1;
00175     bufferRecordPtr->myMatePosition = 1010;
00176     bufferRecordPtr->myInsertSize = 0;
00177     bufferRecordPtr->myData[0] = 'H';
00178     bufferRecordPtr->myData[1] = 'I';
00179     bufferRecordPtr->myData[2] = 0;
00180 
00181     ////////////////////////////////////////////
00182     // Test out of range reference sequence id.
00183     bufferRecordPtr->myReferenceID = 100;
00184 
00185     assert(testRecord.setBuffer((const char*)bufferRecordPtr, 
00186                                 bufferBlockSize + sizeof(int),
00187                                 samHeader) == SamStatus::SUCCESS);
00188     // 1 error - the read name length is 0.
00189     assert(SamValidator::isValid(samHeader, testRecord, errorList) == false);
00190     assert(errorList.numErrors() == 1);
00191     assert(errorList.getNextError()->getType() == 
00192            SamValidationError::INVALID_REF_ID);
00193     assert(errorList.getNextError() == NULL);
00194 
00195     // Clear the error list
00196     errorList.clear();
00197 
00198     ////////////////////////////////////////////
00199     // Test out of range reference sequence id.
00200     bufferRecordPtr->myReferenceID = -100;
00201 
00202     assert(testRecord.setBuffer((const char*)bufferRecordPtr,
00203                                 bufferBlockSize + sizeof(int),
00204                                 samHeader) == SamStatus::SUCCESS);
00205     // 1 error - the read name length is 0.
00206     assert(SamValidator::isValid(samHeader, testRecord, errorList) == false);
00207     assert(errorList.numErrors() == 1);
00208     assert(errorList.getNextError()->getType() == 
00209            SamValidationError::INVALID_REF_ID);
00210     assert(errorList.getNextError() == NULL);
00211 
00212     // Clear the error list
00213     errorList.clear();
00214 }
00215 
00216 
00217 void testEmptyQual()
00218 {
00219    
00220 }
00221 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends