SamCoordOutput.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "SamCoordOutput.h"
00019 #include "SamHelper.h"
00020
00021
00022 SamCoordOutput::SamCoordOutput(SamRecordPool& pool)
00023 : myOutputFile(NULL),
00024 myHeader(NULL),
00025 myPool(&pool)
00026 {
00027 }
00028
00029 SamCoordOutput::~SamCoordOutput()
00030 {
00031
00032 flush(-1, -1);
00033
00034 myOutputFile = NULL;
00035 myHeader = NULL;
00036 }
00037
00038 void SamCoordOutput::setOutputFile(SamFile* outFile, SamFileHeader* header)
00039 {
00040 myOutputFile = outFile;
00041 myHeader = header;
00042 }
00043
00044
00045 bool SamCoordOutput::add(SamRecord* record)
00046 {
00047 if(record != NULL)
00048 {
00049 uint64_t chrom = record->getReferenceID();
00050 uint64_t chromPos =
00051 SamHelper::combineChromPos(chrom, record->get0BasedPosition());
00052 myReadBuffer.insert(std::pair<uint64_t, SamRecord*>(chromPos, record));
00053 return(true);
00054 }
00055 return(false);
00056 }
00057
00058
00059 bool SamCoordOutput::flushAll()
00060 {
00061 return(flush(-1,-1));
00062 }
00063
00064 bool SamCoordOutput::flush(int32_t chromID, int32_t pos0Based)
00065 {
00066 static std::multimap<uint64_t, SamRecord*>::iterator iter;
00067
00068 uint64_t chromPos = SamHelper::combineChromPos(chromID, pos0Based);
00069
00070 bool returnVal = true;
00071 iter = myReadBuffer.begin();
00072
00073 if((myOutputFile == NULL) || (myHeader == NULL))
00074 {
00075 std::cerr <<
00076 "SamCoordOutput::flush, no output file/header is set, so records removed without being written\n";
00077 returnVal = false;
00078 }
00079
00080 while((iter != myReadBuffer.end()) &&
00081 (((*iter).first <= chromPos) || (chromID == -1)))
00082 {
00083 if((myOutputFile != NULL) && (myHeader != NULL))
00084 {
00085 returnVal &=
00086 myOutputFile->WriteRecord(*myHeader, *((*iter).second));
00087 }
00088 if(myPool != NULL)
00089 {
00090 myPool->releaseRecord((*iter).second);
00091 }
00092 else
00093 {
00094 delete((*iter).second);
00095 }
00096 ++iter;
00097 }
00098
00099
00100 myReadBuffer.erase(myReadBuffer.begin(), iter);
00101
00102 return(returnVal);
00103 }