libStatGen Software  1
SamCoordOutput Class Reference

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

#include <SamCoordOutput.h>

List of all members.

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

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.

References SamCoordOutput().

Referenced by SamCoordOutput().

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

Member Function Documentation

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.

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

Referenced by flushAll().

{
    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);
}

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.

References flush().

{
    return(flush(-1,-1));
}
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.

{
    myOutputFile = outFile;
    myHeader = header;
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends