SamCoordOutput Class Reference

Class for buffering up output reads to ensure that it is sorted. More...

`#include <SamCoordOutput.h>`

## Public Member Functions

| SamCoordOutput (SamRecordPool &pool)
| Create an output buffer returning any written records to the specified pool. More...
void | setOutputFile (SamFile *outFile, SamFileHeader *header)
| Set the already opened output file to write to when flushed. More...
bool | add (SamRecord *record)
| Add the specified record to this read buffer. More...
bool | flushAll ()
| Flush the entire buffer, writing all records. More...
bool | flush (int32_t chromID, int32_t pos0Based)
| Flush the buffer based on the specified chromosome id/position, writing any records that start at/before the specified chromosome id/position. More...

## Detailed Description

Class for buffering up output reads to ensure that it is sorted.

They are added in almost sorted order. Flush writes any records that start at/before the specified position.

Definition at line 27 of file SamCoordOutput.h.

## Constructor & Destructor Documentation

## SamCoordOutput()

SamCoordOutput::SamCoordOutput | ( | SamRecordPool & | pool | ) |

Create an output buffer returning any written records to the specified pool.

Parameters

  pool | pool that any written records should be returned to, a pointer to this pool is stored, so it should not go out of scope until the output buffer has written all the records.

Definition at line 22 of file SamCoordOutput.cpp.

: myOutputFile(NULL),
myHeader(NULL),
myPool(&pool)
{
}

References SamCoordOutput().

Referenced by SamCoordOutput().

## ~SamCoordOutput()

SamCoordOutput::~SamCoordOutput | ( |  | ) |

Definition at line 29 of file SamCoordOutput.cpp.

{
// Flush the rest of the records.
flush(-1, -1);
myOutputFile = NULL;
myHeader = NULL;
}
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

## Member Function Documentation

## add()

bool SamCoordOutput::add | ( | SamRecord * | record | ) |

Add the specified record to this read buffer.

Definition at line 45 of file SamCoordOutput.cpp.

{
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);
}
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
SamRecord::getReferenceID
int32_t getReferenceID()
Get the reference sequence id of the record (BAM format rid).
Definition: SamRecord.cpp:1305
SamRecord::get0BasedPosition
int32_t get0BasedPosition()
Get the 0-based(BAM) leftmost position of the record.
Definition: SamRecord.cpp:1319

References SamHelper::combineChromPos(), SamRecord::get0BasedPosition(), and SamRecord::getReferenceID().

## flush()

bool SamCoordOutput::flush | ( | int32_t | chromID,
|  | int32_t | pos0Based
| ) |  |

Flush the buffer based on the specified chromosome id/position, writing any records that start at/before the specified chromosome id/position.

If no output buffer is set, the files cannot be written, but the flushed records are removed/freed. A chromID of -1 will flush everything regardless of pos0Based.

Definition at line 64 of file SamCoordOutput.cpp.

{
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);
}
SamFile::WriteRecord
bool WriteRecord(SamFileHeader &header, SamRecord &record)
Writes the specified record into the file.
Definition: SamFile.cpp:632
SamRecordPool::releaseRecord
void releaseRecord(SamRecord *record)
If record is not NULL, adds it back to the free list.
Definition: SamRecordPool.cpp:81

References SamHelper::combineChromPos(), SamRecordPool::releaseRecord(), and SamFile::WriteRecord().

Referenced by flushAll().

## flushAll()

bool SamCoordOutput::flushAll | ( |  | ) |

Flush the entire buffer, writing all records.

If no output buffer is set, the files cannot be written, but the flushed records are removed/freed.

Definition at line 59 of file SamCoordOutput.cpp.

{
return(flush(-1,-1));
}

References flush().

## setOutputFile()

void SamCoordOutput::setOutputFile | ( | SamFile * | outFile,
|  | SamFileHeader * | header
| ) |  |

Set the already opened output file to write to when flushed.

The user should not close/delete the SamFile until this class is done with it. This class does NOT close/delete the SamFile.

Parameters

  outFile | pointer to an already opened (and header written) SAM/BAM output file.
  header | pointer to an already written header that should be used for writing the records.

Definition at line 38 of file SamCoordOutput.cpp.

{
myOutputFile = outFile;
myHeader = header;
}

The documentation for this class was generated from the following files:

- bam/SamCoordOutput.h
- bam/SamCoordOutput.cpp