SamCoordOutput.cpp
/*
* 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);
}
SamRecord::get0BasedPosition
int32_t get0BasedPosition()
Get the 0-based(BAM) leftmost position of the record.
Definition: SamRecord.cpp:1307
SamFile::WriteRecord
bool WriteRecord(SamFileHeader &header, SamRecord &record)
Writes the specified record into the file.
Definition: SamFile.cpp:619
SamRecord::getReferenceID
int32_t getReferenceID()
Get the reference sequence id of the record (BAM format rid).
Definition: SamRecord.cpp:1293
SamHelper::combineChromPos
static uint64_t combineChromPos(int32_t chromID, int32_t position)
Helper method that combines the chromosome ID and position into a 64bit number by shifting the chromo...
Definition: SamHelper.h:34
SamCoordOutput::add
bool add(SamRecord *record)
Add the specified record to this read buffer.
Definition: SamCoordOutput.cpp:45
SamCoordOutput::setOutputFile
void setOutputFile(SamFile *outFile, SamFileHeader *header)
Set the already opened output file to write to when flushed.
Definition: SamCoordOutput.cpp:38
SamRecordPool::releaseRecord
void releaseRecord(SamRecord *record)
If record is not NULL, adds it back to the free list.
Definition: SamRecordPool.cpp:81
SamFileHeader
This class allows a user to get/set the fields in a SAM/BAM Header.
Definition: SamFileHeader.h:34
SamCoordOutput::flushAll
bool flushAll()
Flush the entire buffer, writing all records.
Definition: SamCoordOutput.cpp:59
SamRecord
Class providing an easy to use interface to get/set/operate on the fields in a SAM/BAM record.
Definition: SamRecord.h:51
SamCoordOutput::flush
bool flush(int32_t chromID, int32_t pos0Based)
Flush the buffer based on the specified chromosome id/position, writing any records that start at/bef...
Definition: SamCoordOutput.cpp:64
SamFile
Allows the user to easily read/write a SAM/BAM file.
Definition: SamFile.h:35
SamCoordOutput::SamCoordOutput
SamCoordOutput(SamRecordPool &pool)
Create an output buffer returning any written records to the specified pool.
Definition: SamCoordOutput.cpp:22
SamRecordPool
Definition: SamRecordPool.h:25