SamFile.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_FILE_H__
00019 #define __SAM_FILE_H__
00020 
00021 #include "SamStatus.h"
00022 #include "InputFile.h"
00023 #include "SamFileHeader.h"
00024 #include "SamRecord.h"
00025 #include "GenericSamInterface.h"
00026 #include "BamIndex.h"
00027 #include "SamStatistics.h"
00028 
00029 class SamFile
00030 {
00031 public:
00032     enum OpenType {READ, WRITE};
00033 
00035     enum SortedType {
00036         UNSORTED = 0, 
00037         FLAG,         
00038         COORDINATE,   
00039         QUERY_NAME    
00040     };
00041     
00043     SamFile();
00044 
00047     SamFile(ErrorHandler::HandlingType errorHandlingType);
00048 
00053     SamFile(const char* filename, OpenType mode);
00054 
00060     SamFile(const char* filename, OpenType mode,
00061             ErrorHandler::HandlingType errorHandlingType);
00062 
00063     virtual ~SamFile();
00064    
00068     bool OpenForRead(const char * filename);
00069 
00072     bool OpenForWrite(const char * filename);
00073 
00077     bool ReadBamIndex(const char * filename);
00078 
00080     void Close();
00081 
00085     bool IsEOF();
00086    
00090     bool ReadHeader(SamFileHeader& header);
00091    
00094     bool WriteHeader(SamFileHeader& header);
00095 
00099     bool ReadRecord(SamFileHeader& header, SamRecord& record);
00100    
00103     bool WriteRecord(SamFileHeader& header, SamRecord& record);
00104    
00107     void setSortedValidation(SortedType sortType);
00108 
00110     uint32_t GetCurrentRecordCount();
00111 
00114     inline SamStatus::Status GetFailure()
00115     {
00116         return(GetStatus());
00117     }
00118 
00120     inline SamStatus::Status GetStatus()
00121     {
00122         return(myStatus.getStatus());
00123     }
00124 
00126     inline const char* GetStatusMessage()
00127     {
00128         return(myStatus.getStatusMessage());
00129     }
00130 
00139     bool SetReadSection(int32_t refID);
00140 
00149     bool SetReadSection(const char* refName);
00150 
00161     bool SetReadSection(int32_t refID, int32_t start, int32_t end);
00162 
00173     bool SetReadSection(const char* refName, int32_t start, int32_t end);
00174 
00179     uint32_t GetNumOverlaps(SamRecord& samRecord);
00180 
00185     void GenerateStatistics(bool genStats);
00186 
00187     inline void PrintStatistics() {if(myStatistics != NULL) myStatistics->print();}
00188 
00189 protected:
00190     void resetFile();
00191 
00195     bool validateSortOrder(SamRecord& record, SamFileHeader& header);
00196    
00197     // Return the sort order as defined by the header.  If it is undefined
00198     // or set to an unknown value, UNSORTED is returned.
00199     SortedType getSortOrderFromHeader(SamFileHeader& header);
00200 
00202     bool readIndexedRecord(SamFileHeader& header, SamRecord& record);
00203 
00204     bool processNewSection(SamFileHeader &header);
00205 
00206     IFILE  myFilePtr;
00207     GenericSamInterface* myInterfacePtr;
00208 
00210     bool myIsOpenForRead;
00212     bool myIsOpenForWrite;
00215     bool myHasHeader;
00216 
00217     SortedType mySortedType;
00218 
00220     int32_t myPrevCoord;
00221     int32_t myPrevRefID;
00222     std::string myPrevReadName;
00223 
00225     uint32_t myRecordCount;
00226 
00228     SamStatistics* myStatistics;
00229    
00231     SamStatus myStatus;
00232 
00234     bool myIsBamOpenForRead;
00235     bool myNewSection;
00236     int32_t myRefID;
00237     int32_t myStartPos;
00238     int32_t myEndPos;
00239     uint64_t myCurrentChunkEnd;
00240     SortedChunkList myChunksToRead;
00241     BamIndex* myBamIndex;
00242 
00243     std::string myRefName;
00244 };
00245 
00246 
00247 class SamFileReader : public SamFile
00248 {
00249 public:
00250 
00252     SamFileReader();
00253 
00255     SamFileReader(const char* filename);
00256 
00257     virtual ~SamFileReader();
00258 };
00259 
00260 
00261 class SamFileWriter : public SamFile
00262 {
00263 public:
00265     SamFileWriter();
00266 
00268     SamFileWriter(const char* filename);
00269 
00270     virtual ~SamFileWriter();
00271 };
00272 
00273 #endif
Generated on Tue Nov 9 16:11:48 2010 for StatGen Software by  doxygen 1.6.3