libStatGen Software 1
Loading...
Searching...
No Matches
SamCoordOutput.cpp
1/*
2 * Copyright (C) 2011 Regents of the University of Michigan
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "SamCoordOutput.h"
19#include "SamHelper.h"
20
21
23 : myOutputFile(NULL),
24 myHeader(NULL),
25 myPool(&pool)
26{
27}
28
29SamCoordOutput::~SamCoordOutput()
30{
31 // Flush the rest of the records.
32 flush(-1, -1);
33
34 myOutputFile = NULL;
35 myHeader = NULL;
36}
37
39{
40 myOutputFile = outFile;
41 myHeader = header;
42}
43
44
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}
57
58
60{
61 return(flush(-1,-1));
62}
63
64bool SamCoordOutput::flush(int32_t chromID, int32_t pos0Based)
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 flush(int32_t chromID, int32_t pos0Based)
Flush the buffer based on the specified chromosome id/position, writing any records that start at/bef...
bool add(SamRecord *record)
Add the specified record to this read buffer.
bool flushAll()
Flush the entire buffer, writing all records.
void setOutputFile(SamFile *outFile, SamFileHeader *header)
Set the already opened output file to write to when flushed.
SamCoordOutput(SamRecordPool &pool)
Create an output buffer returning any written records to the specified pool.
This class allows a user to get/set the fields in a SAM/BAM Header.
Allows the user to easily read/write a SAM/BAM file.
Definition SamFile.h:36
bool WriteRecord(SamFileHeader &header, SamRecord &record)
Writes the specified record into the file.
Definition SamFile.cpp:632
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
void releaseRecord(SamRecord *record)
If record is not NULL, adds it back to the free list.
Class providing an easy to use interface to get/set/operate on the fields in a SAM/BAM record.
Definition SamRecord.h:52
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.