1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
/*
* Copyright (C) 2011 Regents of the University of Michigan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SamCoordOutput.h"
#include "SamHelper.h"
SamCoordOutput::SamCoordOutput(SamRecordPool& pool)
: myOutputFile(NULL),
myHeader(NULL),
myPool(&pool)
{
}
SamCoordOutput::~SamCoordOutput()
{
// Flush the rest of the records.
flush(-1, -1);
myOutputFile = NULL;
myHeader = NULL;
}
void SamCoordOutput::setOutputFile(SamFile* outFile, SamFileHeader* header)
{
myOutputFile = outFile;
myHeader = header;
}
bool SamCoordOutput::add(SamRecord* record)
{
if(record != NULL)
{
int32_t chrom = record->getReferenceID();
uint64_t chromPos =
SamHelper::combineChromPos(chrom, record->get0BasedPosition());
myReadBuffer.insert(std::pair<uint64_t, SamRecord*>(chromPos, record));
return(true);
}
return(false);
}
bool SamCoordOutput::flushAll()
{
return(flush(-1,-1));
}
bool SamCoordOutput::flush(int32_t chromID, int32_t pos0Based)
{
static std::multimap<uint64_t, SamRecord*>::iterator iter;
uint64_t chromPos = SamHelper::combineChromPos(chromID, pos0Based);
bool returnVal = true;
iter = myReadBuffer.begin();
if((myOutputFile == NULL) || (myHeader == NULL))
{
std::cerr <<
"SamCoordOutput::flush, no output file/header is set, so records removed without being written\n";
returnVal = false;
}
while((iter != myReadBuffer.end()) &&
(((*iter).first <= chromPos) || (chromID == -1)))
{
if((myOutputFile != NULL) && (myHeader != NULL))
{
returnVal &=
myOutputFile->WriteRecord(*myHeader, *((*iter).second));
}
if(myPool != NULL)
{
myPool->releaseRecord((*iter).second);
}
else
{
delete((*iter).second);
}
++iter;
}
// Remove the elements from the begining up to,
// but not including the current iterator position.
myReadBuffer.erase(myReadBuffer.begin(), iter);
return(returnVal);
}
|