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             INVALID_TAG
00043         };
00044 
00045     static const char* getTypeString(Type type);
00046 
00047     SamValidationError(Type type, Severity severity, std::string Message);
00048    
00049     Type getType() const;
00050     Severity getSeverity() const;
00051     const char* getMessage() const;
00052 
00053     const char* getTypeString() const;
00054     const char* getSeverityString() const;
00055 
00056     void getErrorString(std::string& errorString) const;
00057 
00058     void printError() const;
00059 
00060 private:
00061     SamValidationError();
00062 
00063     static const char* enumTypeString[];
00064     static const char* enumSeverityString[];
00065 
00066     Type myType;
00067     Severity mySeverity;
00068     std::string myMessage;
00069 
00070 };
00071 
00072 
00073 //
00074 // stream output for validation failure information
00075 //
00076 inline std::ostream &operator << (std::ostream &stream, 
00077                                   const SamValidationError &error)
00078 {
00079     std::string errorMessage;
00080     error.getErrorString(errorMessage);
00081     stream << errorMessage;
00082     return stream;
00083 }
00084 
00085 
00086 class SamValidationErrors
00087 {
00088 public:
00089     // Constructor.
00090     SamValidationErrors();
00091     // Destructor
00092     ~SamValidationErrors();
00093 
00094     // Remove all the errors from the list.
00095     void clear();
00096 
00097     // Adds the specified error to the set of errors.
00098     void addError(SamValidationError::Type newType, 
00099                   SamValidationError::Severity newSeverity,
00100                   const char* newMessage);
00101 
00102     // Return the number of validation errors that are contained in this object.
00103     unsigned int numErrors();
00104 
00105     // Return a pointer to the next error.  It does not remove it from the list.
00106     // Returns null once all errors have been retrieved until resetErrorIter
00107     // is called.
00108     const SamValidationError* getNextError();
00109    
00110     // Resets the iterator to the begining of the errors.
00111     void resetErrorIter();
00112 
00113     // Appends the error messages to the passed in string.
00114     void getErrorString(std::string& errorString) const;
00115 
00116 private:
00117     std::list<const SamValidationError*> myValidationErrors;
00118     std::list<const SamValidationError*>::const_iterator myErrorIter;
00119 };
00120 
00121 
00122 //
00123 // stream output for all validation failures information
00124 //
00125 inline std::ostream& operator << (std::ostream& stream,
00126                                   const SamValidationErrors& errors)
00127 {
00128     std::string errorString = "";
00129     errors.getErrorString(errorString);
00130     stream << errorString;
00131     return stream;
00132 }
00133 
00134 
00135 class SamValidator
00136 {
00137 public:
00138 
00139     static bool isValid(SamFileHeader& samHeader, SamRecord& samRecord, 
00140                         SamValidationErrors& validationErrors);
00141 
00142     static bool isValidQname(const char* qname, uint8_t qnameLen, 
00143                              SamValidationErrors& validationErrors);
00144     static bool isValidFlag(uint16_t flag,
00145                             SamValidationErrors& validationErrors);
00146     // Validate the rname including validating against the header.
00147     static bool isValidRname(SamFileHeader& samHeader, 
00148                              const char* rname,
00149                              SamValidationErrors& validationErrors);
00150     // Validate the rname without validating against the header.
00151     static bool isValidRname(const char* rname,
00152                              SamValidationErrors& validationErrors);
00153     static bool isValidRefID(int32_t refID, const SamReferenceInfo& refInfo, 
00154                              SamValidationErrors& validationErrors);
00155     static bool isValid1BasedPos(int32_t pos, 
00156                                  SamValidationErrors& validationErrors);
00157     static bool isValidMapQuality(uint8_t mapQuality,
00158                                   SamValidationErrors& validationErrors);
00159 
00160     /// Validate the sequence, but not against the cigar or quality string.
00161     /// Validation against cigar is done in isValidCigar.
00162     /// Validation against the quality string is done in isValidQuality.
00163     static bool isValidSequence(SamRecord& samRecord,
00164                                 SamValidationErrors& validationErrors);
00165 
00166     /// Cigar validation depends on sequence.
00167     static bool isValidCigar(SamRecord& samRecord,
00168                              SamValidationErrors& validationErrors);
00169     static bool isValidCigar(const char* cigar, const char* sequence,
00170                              SamValidationErrors& validationErrors);
00171     static bool isValidCigar(const char* cigar,
00172                              int seqLen,
00173                              SamValidationErrors& validationErrors);
00174     static bool isValidMrnm();
00175     static bool isValidMpos();
00176     static bool isValidIsize();
00177     static bool isValidSeq();
00178     // Quality validation depends on sequence.
00179     static bool isValidQuality(SamRecord& samRecord,
00180                         SamValidationErrors& validationErrors);
00181     static bool isValidQuality(const char* quality, const char* sequence,
00182                                   SamValidationErrors& validationErrors);
00183     bool static isValidQuality(const char* quality,
00184                                int seqLength,
00185                                SamValidationErrors& validationErrors);
00186     static bool isValidTags(SamRecord& samRecord,
00187                             SamValidationErrors& validationErrors);
00188     static bool isValidVtype();
00189     static bool isValidValue();
00190 };
00191 
00192 
00193 #endif
Generated on Tue Aug 23 18:19:04 2011 for libStatGen Software by  doxygen 1.6.3