]> git.donarmstrong.com Git - bamtools.git/commitdiff
Gracefully handle empty files with the BamMultiReader
authorErik Garrison <erik.garrison@bc.edu>
Mon, 21 Jun 2010 15:56:39 +0000 (11:56 -0400)
committerErik Garrison <erik.garrison@bc.edu>
Mon, 21 Jun 2010 15:56:39 +0000 (11:56 -0400)
This commit handles the case where an empty BAM file is passed in the
list of filenames given to BamMultiReader::Open(...).  Now a warning is
emitted if the file contains no alignments (or cannot be opened) and the
file is ignored.

BamMultiReader.cpp

index 733087e3f089f9be74cc6e5e2160763eb43dbec1..6d80323e60f5401349abfd5fe1431e10d5b65a7c 100644 (file)
@@ -217,20 +217,29 @@ void BamMultiReader::Open(const vector<string> filenames, bool openIndexes, bool
     for (vector<string>::const_iterator it = filenames.begin(); it != filenames.end(); ++it) {
         string filename = *it;
         BamReader* reader = new BamReader;
+
+        // TODO; change Open to return success/failure so we can report errors here
         if (openIndexes) {
             reader->Open(filename, filename + ".bai");
         } else {
             reader->Open(filename); // for merging, jumping is disallowed
         }
+
+        bool fileOK = true;
         BamAlignment* alignment = new BamAlignment;
         if (coreMode) {
-            reader->GetNextAlignmentCore(*alignment);
+            fileOK &= reader->GetNextAlignmentCore(*alignment);
         } else {
-            reader->GetNextAlignment(*alignment);
+            fileOK &= reader->GetNextAlignment(*alignment);
         }
-        readers.push_back(make_pair(reader, alignment)); // store pointers to our readers for cleanup
-        alignments.insert(make_pair(make_pair(alignment->RefID, alignment->Position),
-                                    make_pair(reader, alignment)));
+        if (fileOK) {
+            readers.push_back(make_pair(reader, alignment)); // store pointers to our readers for cleanup
+            alignments.insert(make_pair(make_pair(alignment->RefID, alignment->Position),
+                                        make_pair(reader, alignment)));
+        } else {
+            cerr << "WARNING: could not read first alignment in " << filename << ", ignoring file" << endl;
+        }
+
     }
     ValidateReaders();
 }