libStatGen Software  1
ValidationTest.cpp
1 /*
2  * Copyright (C) 2010 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "SamRecord.h"
19 #include "SamValidation.h"
20 #include "ValidationTest.h"
21 #include <assert.h>
22 
23 void testSamQNAME()
24 {
25  // This method tests:
26  // QNAME.Length() > 0 and <= 254
27  // QNAME does not contain [ \t\n\r]
28 
29  char qname[256];
30  SamFileHeader samHeader;
31  SamRecord testRecord(ErrorHandler::RETURN);
32  // Error list
33  SamValidationErrors errorList;
34 
35  // Test Length == 0 by setting qname[0] to 0 (end of char*)
36  qname[0] = 0;
37  // It fails, because it is a required field.
38  assert(testRecord.setReadName(qname) == false);
39  assert(strcmp(testRecord.getReadName(), "UNKNOWN") == 0);
40  // It was reset to the default which is valid.
41  assert(SamValidator::isValid(samHeader, testRecord, errorList) == true);
42  assert(errorList.numErrors() == 0);
43  assert(errorList.getNextError() == NULL);
44 
45  // Test too long of a read name.
46  memset(qname, '.', 255);
47  qname[255] = 0;
48  assert(testRecord.setReadName(qname) == true);
49  assert(strcmp(testRecord.getReadName(), qname) == 0);
50  assert(SamValidator::isValid(samHeader, testRecord, errorList) == false);
51  // 2 errors - 1 since the qname is longer than 254 (it is 255).
52  // and the qname length including the null is 256, but the
53  // read name length is only 8 bits, so that is a 1.
54  assert(errorList.numErrors() == 2);
55  assert(errorList.getNextError()->getType() ==
57  assert(errorList.getNextError()->getType() ==
59  assert(errorList.getNextError() == NULL);
60 
61  // Clear the error list
62  errorList.clear();
63 
64  // Setup a buffer to set the record to.
65  int bufferBlockSize = 32;
66 
67  bamRecordStruct* bufferRecordPtr =
68  (bamRecordStruct *) malloc(bufferBlockSize + sizeof(int));
69 
70  bufferRecordPtr->myBlockSize = bufferBlockSize;
71  bufferRecordPtr->myReferenceID = -1;
72  bufferRecordPtr->myPosition = 1010;
73  // Set the read name length to 0.
74  bufferRecordPtr->myReadNameLength = 0;
75  bufferRecordPtr->myMapQuality = 0;
76  bufferRecordPtr->myBin = 4681;
77  bufferRecordPtr->myCigarLength = 0;
78  bufferRecordPtr->myFlag = 73;
79  bufferRecordPtr->myReadLength = 0;
80  bufferRecordPtr->myMateReferenceID = -1;
81  bufferRecordPtr->myMatePosition = 1010;
82  bufferRecordPtr->myInsertSize = 0;
83 
84  assert(testRecord.setBuffer((const char*)bufferRecordPtr,
85  bufferBlockSize + sizeof(int),
86  samHeader) == SamStatus::SUCCESS);
87  // 1 error - the read name length is 0.
88  assert(SamValidator::isValid(samHeader, testRecord, errorList) == false);
89  assert(errorList.numErrors() == 1);
90  assert(errorList.getNextError()->getType() ==
92  assert(errorList.getNextError() == NULL);
93 
94  // Clear the error list
95  errorList.clear();
96 
97  // Test a buffer that has a read name, but the length specified is
98  // longer than the first null.
99  bufferBlockSize = 40;
100  bufferRecordPtr->myBlockSize = bufferBlockSize;
101  // Set the read name length to 8 - longer than 3 - "HI\0".
102  bufferRecordPtr->myReadNameLength = 8;
103  bufferRecordPtr->myData[0] = 'H';
104  bufferRecordPtr->myData[1] = 'I';
105  bufferRecordPtr->myData[2] = 0;
106 
107  assert(testRecord.setBuffer((const char*)bufferRecordPtr,
108  bufferBlockSize + sizeof(int),
109  samHeader) == SamStatus::SUCCESS);
110  // 1 error - the read name length in the buffer does not match the
111  // length of the read name to the first null.
112  assert(SamValidator::isValid(samHeader, testRecord, errorList) == false);
113  assert(errorList.numErrors() == 1);
114  assert(errorList.getNextError()->getType() ==
116  assert(errorList.getNextError() == NULL);
117 
118  // Clear the error list
119  errorList.clear();
120 
121  // Test a buffer that has a read name, but the length specified is
122  // shorter than the first null.
123  bufferBlockSize = 34;
124  bufferRecordPtr->myBlockSize = bufferBlockSize;
125  // Set the read name length to 2 - longer than 3 - "HI\0"..
126  bufferRecordPtr->myReadNameLength = 2;
127  bufferRecordPtr->myData[0] = 'H';
128  bufferRecordPtr->myData[1] = 'I';
129  bufferRecordPtr->myData[2] = 0;
130 
131  assert(testRecord.setBuffer((const char*)bufferRecordPtr,
132  bufferBlockSize + sizeof(int),
133  samHeader) == SamStatus::SUCCESS);
134  // 1 error - the read name length in the buffer does not match
135  // the length of the read name to the first null.
136  assert(SamValidator::isValid(samHeader, testRecord, errorList) == false);
137  assert(errorList.numErrors() == 1);
138  assert(errorList.getNextError()->getType() ==
140  assert(errorList.getNextError() == NULL);
141 
142  // Clear the error list
143  errorList.clear();
144 }
145 
146 
147 void testBamRID()
148 {
149  // BAM
150  SamRecord testRecord(ErrorHandler::RETURN);
151  // Error list
152  SamValidationErrors errorList;
153  SamFileHeader samHeader;
154 
155  // Clear the error list
156  errorList.clear();
157 
158  // Setup a buffer to set the record to.
159  int bufferBlockSize = 35;
160 
161  bamRecordStruct* bufferRecordPtr =
162  (bamRecordStruct *) malloc(bufferBlockSize + sizeof(int));
163 
164  bufferRecordPtr->myBlockSize = bufferBlockSize;
165  bufferRecordPtr->myPosition = 1010;
166  bufferRecordPtr->myReferenceID = -1;
167  // Set the read name length to 0.
168  bufferRecordPtr->myReadNameLength = 3;
169  bufferRecordPtr->myMapQuality = 0;
170  bufferRecordPtr->myBin = 4681;
171  bufferRecordPtr->myCigarLength = 0;
172  bufferRecordPtr->myFlag = 73;
173  bufferRecordPtr->myReadLength = 0;
174  bufferRecordPtr->myMateReferenceID = -1;
175  bufferRecordPtr->myMatePosition = 1010;
176  bufferRecordPtr->myInsertSize = 0;
177  bufferRecordPtr->myData[0] = 'H';
178  bufferRecordPtr->myData[1] = 'I';
179  bufferRecordPtr->myData[2] = 0;
180 
181  ////////////////////////////////////////////
182  // Test out of range reference sequence id.
183  bufferRecordPtr->myReferenceID = 100;
184 
185  assert(testRecord.setBuffer((const char*)bufferRecordPtr,
186  bufferBlockSize + sizeof(int),
187  samHeader) == SamStatus::SUCCESS);
188  // 1 error - the read name length is 0.
189  assert(SamValidator::isValid(samHeader, testRecord, errorList) == false);
190  assert(errorList.numErrors() == 1);
191  assert(errorList.getNextError()->getType() ==
193  assert(errorList.getNextError() == NULL);
194 
195  // Clear the error list
196  errorList.clear();
197 
198  ////////////////////////////////////////////
199  // Test out of range reference sequence id.
200  bufferRecordPtr->myReferenceID = -100;
201 
202  assert(testRecord.setBuffer((const char*)bufferRecordPtr,
203  bufferBlockSize + sizeof(int),
204  samHeader) == SamStatus::SUCCESS);
205  // 1 error - the read name length is 0.
206  assert(SamValidator::isValid(samHeader, testRecord, errorList) == false);
207  assert(errorList.numErrors() == 1);
208  assert(errorList.getNextError()->getType() ==
210  assert(errorList.getNextError() == NULL);
211 
212  // Clear the error list
213  errorList.clear();
214 }
215 
216 
217 void testEmptyQual()
218 {
219 
220 }
221 
just return failure on the error
Definition: ErrorHandler.h:31
unsigned int numErrors()
Return the number of validation errors contained in this object.
void clear()
Remove all the errors from the container.
method completed successfully.
Definition: StatGenStatus.h:32
Structure of a BAM record.
Definition: SamRecord.h:33
Type getType() const
Return the type enum of this validation error object.
const SamValidationError * getNextError()
Return a pointer to the next error without removing it from the container, and returning null once al...
This class allows a user to get/set the fields in a SAM/BAM Header.
Definition: SamFileHeader.h:34
static bool isValid(SamFileHeader &samHeader, SamRecord &samRecord, SamValidationErrors &validationErrors)
Validates whether or not the specified SamRecord is valid, calling all of the other validations...
The SamValidationErrors class is a container class that holds SamValidationError Objects, allowing a validation method to return all of the invalid errors rather than just one.
Invalid read/query name.
Definition: SamValidation.h:49
Class providing an easy to use interface to get/set/operate on the fields in a SAM/BAM record...
Definition: SamRecord.h:51