libStatGen Software 1
Loading...
Searching...
No Matches
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.
 
void setOutputFile (SamFile *outFile, SamFileHeader *header)
 Set the already opened output file to write to when flushed.
 
bool add (SamRecord *record)
 Add the specified record to this read buffer.
 
bool flushAll ()
 Flush the entire buffer, writing all records.
 
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.
 

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
poolpool 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.

23 : myOutputFile(NULL),
24 myHeader(NULL),
25 myPool(&pool)
26{
27}

References SamCoordOutput().

Referenced by SamCoordOutput().

◆ ~SamCoordOutput()

SamCoordOutput::~SamCoordOutput ( )

Definition at line 29 of file SamCoordOutput.cpp.

30{
31 // Flush the rest of the records.
32 flush(-1, -1);
33
34 myOutputFile = NULL;
35 myHeader = NULL;
36}
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...

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.

46{
47 if(record != NULL)
48 {
49 int32_t chrom = record->getReferenceID();
50 uint64_t chromPos =
52 myReadBuffer.insert(std::pair<uint64_t, SamRecord*>(chromPos, record));
53 return(true);
54 }
55 return(false);
56}
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
int32_t getReferenceID()
Get the reference sequence id of the record (BAM format rid).
int32_t get0BasedPosition()
Get the 0-based(BAM) leftmost position of the record.

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.

65{
66 static std::multimap<uint64_t, SamRecord*>::iterator iter;
67
68 uint64_t chromPos = SamHelper::combineChromPos(chromID, pos0Based);
69
70 bool returnVal = true;
71 iter = myReadBuffer.begin();
72
73 if((myOutputFile == NULL) || (myHeader == NULL))
74 {
75 std::cerr <<
76 "SamCoordOutput::flush, no output file/header is set, so records removed without being written\n";
77 returnVal = false;
78 }
79
80 while((iter != myReadBuffer.end()) &&
81 (((*iter).first <= chromPos) || (chromID == -1)))
82 {
83 if((myOutputFile != NULL) && (myHeader != NULL))
84 {
85 returnVal &=
86 myOutputFile->WriteRecord(*myHeader, *((*iter).second));
87 }
88 if(myPool != NULL)
89 {
90 myPool->releaseRecord((*iter).second);
91 }
92 else
93 {
94 delete((*iter).second);
95 }
96 ++iter;
97 }
98 // Remove the elements from the begining up to,
99 // but not including the current iterator position.
100 myReadBuffer.erase(myReadBuffer.begin(), iter);
101
102 return(returnVal);
103}
bool WriteRecord(SamFileHeader &header, SamRecord &record)
Writes the specified record into the file.
Definition SamFile.cpp:632
void releaseRecord(SamRecord *record)
If record is not NULL, adds it back to the free list.

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.

60{
61 return(flush(-1,-1));
62}

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
outFilepointer to an already opened (and header written) SAM/BAM output file.
headerpointer to an already written header that should be used for writing the records.

Definition at line 38 of file SamCoordOutput.cpp.

39{
40 myOutputFile = outFile;
41 myHeader = header;
42}

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