libStatGen Software  1
SamCoordOutput.cpp
00001 /*
00002  *  Copyright (C) 2011  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 #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     // Flush the rest of the records.
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         int32_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     // Remove the elements from the begining up to,
00099     // but not including the current iterator position.
00100     myReadBuffer.erase(myReadBuffer.begin(), iter);
00101 
00102     return(returnVal);
00103 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends