libStatGen Software  1
SamCoordOutput.h
00001 /*
00002  *  Copyright (C) 2011  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef __SAM_COORD_OUTPUT_H__
00019 #define __SAM_COORD_OUTPUT_H__
00020 
00021 #include "SamFile.h"
00022 #include "SamRecordPool.h"
00023 
00024 /// Class for buffering up output reads to ensure that it is sorted. 
00025 /// They are added in almost sorted order.
00026 /// Flush writes any records that start at/before the specified position.
00027 class SamCoordOutput
00028 {
00029 public:
00030     /// Create an output buffer returning any written records to the specified pool.
00031     /// \param pool pool that any written records should be returned to, a pointer
00032     /// to this pool is stored, so it should not go out of scope until the output buffer
00033     /// has written all the records.
00034     SamCoordOutput(SamRecordPool& pool);
00035     ~SamCoordOutput();
00036 
00037     /// Set the already opened output file to write to when flushed.
00038     /// The user should not close/delete the SamFile until this class is done
00039     /// with it.  This class does NOT close/delete the SamFile.
00040     /// \param outFile pointer to an already opened (and header written) 
00041     /// SAM/BAM output file.
00042     /// \param header pointer to an already written header that should be
00043     /// used for writing the records.
00044     void setOutputFile(SamFile* outFile, SamFileHeader* header);
00045 
00046     /// Add the specified record to this read buffer.
00047     bool add(SamRecord* record);
00048 
00049     /// Flush the entire buffer, writing all records.
00050     /// If no output buffer is set, the files cannot be written, but the
00051     /// flushed records are removed/freed.
00052     bool flushAll();
00053 
00054     /// Flush the buffer based on the specified chromosome id/position, writing
00055     /// any records that start at/before the specified chromosome id/position.
00056     /// If no output buffer is set, the files cannot be written, but the
00057     /// flushed records are removed/freed.
00058     /// A chromID of -1 will flush everything regardless of pos0Based.
00059     bool flush(int32_t chromID, int32_t pos0Based);
00060 
00061 protected:
00062     
00063 
00064 private:
00065     // Require a sam record pool, so make the constructor with
00066     // no parameters private.
00067     SamCoordOutput();
00068 
00069     SamFile* myOutputFile;
00070     SamFileHeader* myHeader;
00071     std::multimap<uint64_t, SamRecord*> myReadBuffer;
00072     SamRecordPool* myPool;
00073 };
00074 
00075 
00076 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends