SamValidation.h

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 #ifndef __SAM_VALIDATION_H__
00019 #define __SAM_VALIDATION_H__
00020 
00021 #include "SamFile.h"
00022 #include <list>
00023 
00024 class SamValidationError
00025 {
00026 public:
00027     // Warning is used if it is just an invalid value.
00028     // Error is used if parsing could not succeed.
00029     enum Severity {WARNING, ERROR};
00030 
00031     enum Type
00032         {
00033             // 
00034             INVALID_QNAME,
00035             INVALID_REF_ID,
00036             INVALID_RNAME,
00037             INVALID_POS,
00038             INVALID_MAPQ,
00039             INVALID_CIGAR,
00040             INVALID_MRNM,
00041             INVALID_QUAL
00042         };
00043 
00044     static const char* getTypeString(Type type);
00045 
00046     SamValidationError(Type type, Severity severity, std::string Message);
00047    
00048     Type getType() const;
00049     Severity getSeverity() const;
00050     const char* getMessage() const;
00051 
00052     const char* getTypeString() const;
00053     const char* getSeverityString() const;
00054 
00055     void getErrorString(std::string& errorString) const;
00056 
00057     void printError() const;
00058 
00059 private:
00060     SamValidationError();
00061 
00062     static const char* enumTypeString[];
00063     static const char* enumSeverityString[];
00064 
00065     Type myType;
00066     Severity mySeverity;
00067     std::string myMessage;
00068 
00069 };
00070 
00071 
00072 //
00073 // stream output for validation failure information
00074 //
00075 inline std::ostream &operator << (std::ostream &stream, 
00076                                   const SamValidationError &error)
00077 {
00078     std::string errorMessage;
00079     error.getErrorString(errorMessage);
00080     stream << errorMessage;
00081     return stream;
00082 }
00083 
00084 
00085 class SamValidationErrors
00086 {
00087 public:
00088     // Constructor.
00089     SamValidationErrors();
00090     // Destructor
00091     ~SamValidationErrors();
00092 
00093     // Remove all the errors from the list.
00094     void clear();
00095 
00096     // Adds the specified error to the set of errors.
00097     void addError(SamValidationError::Type newType, 
00098                   SamValidationError::Severity newSeverity,
00099                   const char* newMessage);
00100 
00101     // Return the number of validation errors that are contained in this object.
00102     unsigned int numErrors();
00103 
00104     // Return a pointer to the next error.  It does not remove it from the list.
00105     // Returns null once all errors have been retrieved until resetErrorIter
00106     // is called.
00107     const SamValidationError* getNextError();
00108    
00109     // Resets the iterator to the begining of the errors.
00110     void resetErrorIter();
00111 
00112     // Appends the error messages to the passed in string.
00113     void getErrorString(std::string& errorString) const;
00114 
00115 private:
00116     std::list<const SamValidationError*> myValidationErrors;
00117     std::list<const SamValidationError*>::const_iterator myErrorIter;
00118 };
00119 
00120 
00121 //
00122 // stream output for all validation failures information
00123 //
00124 inline std::ostream& operator << (std::ostream& stream,
00125                                   const SamValidationErrors& errors)
00126 {
00127     std::string errorString = "";
00128     errors.getErrorString(errorString);
00129     stream << errorString;
00130     return stream;
00131 }
00132 
00133 
00134 class SamValidator
00135 {
00136 public:
00137 
00138     static bool isValid(SamFileHeader& samHeader, SamRecord& samRecord, 
00139                         SamValidationErrors& validationErrors);
00140 
00141     static bool isValidQname(const char* qname, uint8_t qnameLen, 
00142                              SamValidationErrors& validationErrors);
00143     static bool isValidFlag(uint16_t flag,
00144                             SamValidationErrors& validationErrors);
00145     // Validate the rname including validating against the header.
00146     static bool isValidRname(SamFileHeader& samHeader, 
00147                              const char* rname,
00148                              SamValidationErrors& validationErrors);
00149     // Validate the rname without validating against the header.
00150     static bool isValidRname(const char* rname,
00151                              SamValidationErrors& validationErrors);
00152     static bool isValidRefID(int32_t refID, const SamReferenceInfo& refInfo, 
00153                              SamValidationErrors& validationErrors);
00154     static bool isValid1BasedPos(int32_t pos, 
00155                                  SamValidationErrors& validationErrors);
00156     static bool isValidMapQuality(uint8_t mapQuality,
00157                                   SamValidationErrors& validationErrors);
00158     // Cigar validation depends on sequence.
00159     static bool isValidCigar(const char* cigar, const char* sequence,
00160                              SamValidationErrors& validationErrors);
00161     static bool isValidMrnm();
00162     static bool isValidMpos();
00163     static bool isValidIsize();
00164     static bool isValidSeq();
00165     // Quality validation depends on sequence.
00166     static bool isValidQuality(const char* quality, const char* sequence,
00167                                   SamValidationErrors& validationErrors);
00168     static bool isValidTag();
00169     static bool isValidVtype();
00170     static bool isValidValue();
00171 };
00172 
00173 
00174 #endif
Generated on Wed Nov 17 15:38:27 2010 for StatGen Software by  doxygen 1.6.3