1 // ***************************************************************************
2 // BamMultiReader.cpp (c) 2010 Erik Garrison, Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 19 November 2010 (DB)
7 // ---------------------------------------------------------------------------
8 // Uses BGZF routines were adapted from the bgzf.c code developed at the Broad
10 // ---------------------------------------------------------------------------
11 // Functionality for simultaneously reading multiple BAM files.
13 // This functionality allows applications to work on very large sets of files
14 // without requiring intermediate merge, sort, and index steps for each file
15 // subset. It also improves the performance of our merge system as it
16 // precludes the need to sort merged files.
17 // ***************************************************************************
19 #include <api/BamMultiReader.h>
21 using namespace BamTools;
32 // -----------------------------------------------------
33 // BamMultiReader implementation
34 // -----------------------------------------------------
37 BamMultiReader::BamMultiReader(void)
43 BamMultiReader::~BamMultiReader(void) {
47 // close the BAM files
48 void BamMultiReader::Close(void) {
50 // close all BAM readers and clean up pointers
51 vector<pair<BamReader*, BamAlignment*> >::iterator readerIter = readers.begin();
52 vector<pair<BamReader*, BamAlignment*> >::iterator readerEnd = readers.end();
53 for ( ; readerIter != readerEnd; ++readerIter) {
55 BamReader* reader = (*readerIter).first;
56 BamAlignment* alignment = (*readerIter).second;
59 if ( reader) reader->Close();
61 // delete reader pointer
65 // delete alignment pointer
70 // clear out the container
74 // saves index data to BAM index files (".bai"/".bti") where necessary, returns success/fail
75 bool BamMultiReader::CreateIndexes(bool useStandardIndex) {
77 for (vector<pair<BamReader*, BamAlignment*> >::iterator it = readers.begin(); it != readers.end(); ++it) {
78 BamReader* reader = it->first;
79 result &= reader->CreateIndex(useStandardIndex);
84 // sets the index caching mode on the readers
85 void BamMultiReader::SetIndexCacheMode(const BamIndex::BamIndexCacheMode mode) {
86 for (vector<pair<BamReader*, BamAlignment*> >::iterator it = readers.begin(); it != readers.end(); ++it) {
87 BamReader* reader = it->first;
88 reader->SetIndexCacheMode(mode);
93 void BamMultiReader::DumpAlignmentIndex(void) {
94 for (AlignmentIndex::const_iterator it = alignments.begin(); it != alignments.end(); ++it) {
95 cerr << it->first.first << ":" << it->first.second << " " << it->second.first->GetFilename() << endl;
99 // makes a virtual, unified header for all the bam files in the multireader
100 const string BamMultiReader::GetHeaderText(void) const {
102 string mergedHeader = "";
103 map<string, bool> readGroups;
105 // foreach extraction entry (each BAM file)
106 for (vector<pair<BamReader*, BamAlignment*> >::const_iterator rs = readers.begin(); rs != readers.end(); ++rs) {
108 BamReader* reader = rs->first;
109 string headerText = reader->GetHeaderText();
110 if ( headerText.empty() ) continue;
112 map<string, bool> currentFileReadGroups;
113 stringstream header(headerText);
114 vector<string> lines;
116 while (getline(header, item))
117 lines.push_back(item);
119 for (vector<string>::const_iterator it = lines.begin(); it != lines.end(); ++it) {
121 // get next line from header, skip if empty
122 string headerLine = *it;
123 if ( headerLine.empty() ) { continue; }
125 // if first file, save HD & SQ entries
126 if ( rs == readers.begin() ) {
127 if ( headerLine.find("@HD") == 0 || headerLine.find("@SQ") == 0) {
128 mergedHeader.append(headerLine.c_str());
129 mergedHeader.append(1, '\n');
133 // (for all files) append RG entries if they are unique
134 if ( headerLine.find("@RG") == 0 ) {
135 stringstream headerLineSs(headerLine);
136 string part, readGroupPart, readGroup;
137 while(std::getline(headerLineSs, part, '\t')) {
138 stringstream partSs(part);
140 std::getline(partSs, subtag, ':');
141 if (subtag == "ID") {
142 std::getline(partSs, readGroup, ':');
146 if (readGroups.find(readGroup) == readGroups.end()) { // prevents duplicate @RG entries
147 mergedHeader.append(headerLine.c_str() );
148 mergedHeader.append(1, '\n');
149 readGroups[readGroup] = true;
150 currentFileReadGroups[readGroup] = true;
152 // warn iff we are reading one file and discover duplicated @RG tags in the header
153 // otherwise, we emit no warning, as we might be merging multiple BAM files with identical @RG tags
154 if (currentFileReadGroups.find(readGroup) != currentFileReadGroups.end()) {
155 cerr << "WARNING: duplicate @RG tag " << readGroup
156 << " entry in header of " << reader->GetFilename() << endl;
163 // return merged header text
167 // get next alignment among all files
168 bool BamMultiReader::GetNextAlignment(BamAlignment& nextAlignment) {
170 // bail out if we are at EOF in all files, means no more alignments to process
171 if (!HasOpenReaders())
174 // when all alignments have stepped into a new target sequence, update our
175 // current reference sequence id
178 // our lowest alignment and reader will be at the front of our alignment index
179 BamAlignment* alignment = alignments.begin()->second.second;
180 BamReader* reader = alignments.begin()->second.first;
182 // now that we have the lowest alignment in the set, save it by copy to our argument
183 nextAlignment = BamAlignment(*alignment);
185 // remove this alignment index entry from our alignment index
186 alignments.erase(alignments.begin());
188 // and add another entry if we can get another alignment from the reader
189 if (reader->GetNextAlignment(*alignment)) {
190 alignments.insert(make_pair(make_pair(alignment->RefID, alignment->Position),
191 make_pair(reader, alignment)));
192 } else { // do nothing
193 //cerr << "reached end of file " << lowestReader->GetFilename() << endl;
200 // get next alignment among all files without parsing character data from alignments
201 bool BamMultiReader::GetNextAlignmentCore(BamAlignment& nextAlignment) {
203 // bail out if we are at EOF in all files, means no more alignments to process
204 if (!HasOpenReaders())
207 // when all alignments have stepped into a new target sequence, update our
208 // current reference sequence id
211 // our lowest alignment and reader will be at the front of our alignment index
212 BamAlignment* alignment = alignments.begin()->second.second;
213 BamReader* reader = alignments.begin()->second.first;
215 // now that we have the lowest alignment in the set, save it by copy to our argument
216 nextAlignment = BamAlignment(*alignment);
217 //memcpy(&nextAlignment, alignment, sizeof(BamAlignment));
219 // remove this alignment index entry from our alignment index
220 alignments.erase(alignments.begin());
222 // and add another entry if we can get another alignment from the reader
223 if (reader->GetNextAlignmentCore(*alignment)) {
224 alignments.insert(make_pair(make_pair(alignment->RefID, alignment->Position),
225 make_pair(reader, alignment)));
226 } else { // do nothing
227 //cerr << "reached end of file " << lowestReader->GetFilename() << endl;
234 // ---------------------------------------------------------------------------------------
236 // NB: The following GetReferenceX() functions assume that we have identical
237 // references for all BAM files. We enforce this by invoking the above
238 // validation function (ValidateReaders) to verify that our reference data
239 // is the same across all files on Open, so we will not encounter a situation
240 // in which there is a mismatch and we are still live.
242 // ---------------------------------------------------------------------------------------
244 // returns the number of reference sequences
245 const int BamMultiReader::GetReferenceCount(void) const {
246 return readers.front().first->GetReferenceCount();
249 // returns vector of reference objects
250 const BamTools::RefVector BamMultiReader::GetReferenceData(void) const {
251 return readers.front().first->GetReferenceData();
254 // returns refID from reference name
255 const int BamMultiReader::GetReferenceID(const string& refName) const {
256 return readers.front().first->GetReferenceID(refName);
259 // ---------------------------------------------------------------------------------------
261 // checks if any readers still have alignments
262 bool BamMultiReader::HasOpenReaders() {
263 return alignments.size() > 0;
266 // returns whether underlying BAM readers ALL have an index loaded
267 // this is useful to indicate whether Jump() or SetRegion() are possible
268 bool BamMultiReader::IsIndexLoaded(void) const {
270 vector<pair<BamReader*, BamAlignment*> >::const_iterator readerIter = readers.begin();
271 vector<pair<BamReader*, BamAlignment*> >::const_iterator readerEnd = readers.end();
272 for ( ; readerIter != readerEnd; ++readerIter ) {
273 const BamReader* reader = (*readerIter).first;
274 if ( reader ) ok &= reader->IsIndexLoaded();
279 // jumps to specified region(refID, leftBound) in BAM files, returns success/fail
280 bool BamMultiReader::Jump(int refID, int position) {
282 //if ( References.at(refID).RefHasAlignments && (position <= References.at(refID).RefLength) ) {
283 CurrentRefID = refID;
284 CurrentLeft = position;
287 for (vector<pair<BamReader*, BamAlignment*> >::iterator it = readers.begin(); it != readers.end(); ++it) {
288 BamReader* reader = it->first;
289 result &= reader->Jump(refID, position);
291 cerr << "ERROR: could not jump " << reader->GetFilename() << " to " << refID << ":" << position << endl;
295 if (result) UpdateAlignments();
300 bool BamMultiReader::Open(const vector<string>& filenames, bool openIndexes, bool coreMode, bool preferStandardIndex) {
302 // for filename in filenames
303 fileNames = filenames; // save filenames in our multireader
304 for (vector<string>::const_iterator it = filenames.begin(); it != filenames.end(); ++it) {
306 const string filename = *it;
307 BamReader* reader = new BamReader;
309 bool openedOK = true;
310 openedOK = reader->Open(filename, "", openIndexes, preferStandardIndex);
312 // if file opened ok, check that it can be read
316 BamAlignment* alignment = new BamAlignment;
317 fileOK &= ( coreMode ? reader->GetNextAlignmentCore(*alignment) : reader->GetNextAlignment(*alignment) );
320 readers.push_back(make_pair(reader, alignment)); // store pointers to our readers for cleanup
321 alignments.insert(make_pair(make_pair(alignment->RefID, alignment->Position),
322 make_pair(reader, alignment)));
324 cerr << "WARNING: could not read first alignment in " << filename << ", ignoring file" << endl;
325 // if only file available & could not be read, return failure
326 if ( filenames.size() == 1 ) return false;
330 // TODO; any further error handling when openedOK is false ??
335 // files opened ok, at least one alignment could be read,
336 // now need to check that all files use same reference data
341 void BamMultiReader::PrintFilenames(void) {
342 for (vector<pair<BamReader*, BamAlignment*> >::iterator it = readers.begin(); it != readers.end(); ++it) {
343 BamReader* reader = it->first;
344 cout << reader->GetFilename() << endl;
348 // returns BAM file pointers to beginning of alignment data
349 bool BamMultiReader::Rewind(void) {
351 for (vector<pair<BamReader*, BamAlignment*> >::iterator it = readers.begin(); it != readers.end(); ++it) {
352 BamReader* reader = it->first;
353 result &= reader->Rewind();
358 bool BamMultiReader::SetRegion(const int& leftRefID, const int& leftPosition, const int& rightRefID, const int& rightPosition) {
359 BamRegion region(leftRefID, leftPosition, rightRefID, rightPosition);
360 return SetRegion(region);
363 bool BamMultiReader::SetRegion(const BamRegion& region) {
367 // NB: While it may make sense to track readers in which we can
368 // successfully SetRegion, In practice a failure of SetRegion means "no
369 // alignments here." It makes sense to simply accept the failure,
370 // UpdateAlignments(), and continue.
372 for (vector<pair<BamReader*, BamAlignment*> >::iterator it = readers.begin(); it != readers.end(); ++it) {
373 if (!it->first->SetRegion(region)) {
374 cerr << "ERROR: could not jump " << it->first->GetFilename() << " to "
375 << region.LeftRefID << ":" << region.LeftPosition
376 << ".." << region.RightRefID << ":" << region.RightPosition << endl;
384 void BamMultiReader::UpdateAlignments(void) {
387 for (vector<pair<BamReader*, BamAlignment*> >::iterator it = readers.begin(); it != readers.end(); ++it) {
388 BamReader* br = it->first;
389 BamAlignment* ba = it->second;
390 if (br->GetNextAlignment(*ba)) {
391 alignments.insert(make_pair(make_pair(ba->RefID, ba->Position),
394 // assume BamReader end of region / EOF
399 // updates the reference id stored in the BamMultiReader
400 // to reflect the current state of the readers
401 void BamMultiReader::UpdateReferenceID(void) {
402 // the alignments are sorted by position, so the first alignment will always have the lowest reference ID
403 if (alignments.begin()->second.second->RefID != CurrentRefID) {
404 // get the next reference id
405 // while there aren't any readers at the next ref id
406 // increment the ref id
407 int nextRefID = CurrentRefID;
408 while (alignments.begin()->second.second->RefID != nextRefID) {
411 //cerr << "updating reference id from " << CurrentRefID << " to " << nextRefID << endl;
412 CurrentRefID = nextRefID;
416 // ValidateReaders checks that all the readers point to BAM files representing
417 // alignments against the same set of reference sequences, and that the
418 // sequences are identically ordered. If these checks fail the operation of
419 // the multireader is undefined, so we force program exit.
420 void BamMultiReader::ValidateReaders(void) const {
421 int firstRefCount = readers.front().first->GetReferenceCount();
422 BamTools::RefVector firstRefData = readers.front().first->GetReferenceData();
423 for (vector<pair<BamReader*, BamAlignment*> >::const_iterator it = readers.begin(); it != readers.end(); ++it) {
424 BamReader* reader = it->first;
425 BamTools::RefVector currentRefData = reader->GetReferenceData();
426 BamTools::RefVector::const_iterator f = firstRefData.begin();
427 BamTools::RefVector::const_iterator c = currentRefData.begin();
428 if (reader->GetReferenceCount() != firstRefCount || firstRefData.size() != currentRefData.size()) {
429 cerr << "ERROR: mismatched number of references in " << reader->GetFilename()
430 << " expected " << firstRefCount
431 << " reference sequences but only found " << reader->GetReferenceCount() << endl;
434 // this will be ok; we just checked above that we have identically-sized sets of references
435 // here we simply check if they are all, in fact, equal in content
436 while (f != firstRefData.end()) {
437 if (f->RefName != c->RefName || f->RefLength != c->RefLength) {
438 cerr << "ERROR: mismatched references found in " << reader->GetFilename()
439 << " expected: " << endl;
440 for (BamTools::RefVector::const_iterator a = firstRefData.begin(); a != firstRefData.end(); ++a)
441 cerr << a->RefName << " " << a->RefLength << endl;
442 cerr << "but found: " << endl;
443 for (BamTools::RefVector::const_iterator a = currentRefData.begin(); a != currentRefData.end(); ++a)
444 cerr << a->RefName << " " << a->RefLength << endl;