]> git.donarmstrong.com Git - bamtools.git/blobdiff - BamMultiReader.h
change merger to use GetNextAlignmentCore
[bamtools.git] / BamMultiReader.h
index 319d3270819c8d0b0d5ed5c288b97193c6f1107a..3d9024c40ce9927e13e3588acb82d4d09b7711a7 100644 (file)
@@ -13,6 +13,9 @@
 \r
 // C++ includes\r
 #include <string>\r
+#include <map>\r
+#include <utility> // for pair\r
+#include <sstream>\r
 \r
 using namespace std;\r
 \r
@@ -22,7 +25,9 @@ using namespace std;
 \r
 namespace BamTools {\r
 \r
-enum BamReaderState { START, END, CLOSED };\r
+// index mapping reference/position pairings to bamreaders and their alignments\r
+typedef multimap<pair<int, int>, pair<BamReader*, BamAlignment*> > AlignmentIndex;\r
+\r
 \r
 class BamMultiReader {\r
 \r
@@ -44,11 +49,18 @@ class BamMultiReader {
 \r
         // close BAM files\r
         void Close(void);\r
+\r
+        // opens BAM files (and optional BAM index files, if provided)\r
+        // @openIndexes - triggers index opening, useful for suppressing\r
+        // error messages during merging of files in which we may not have\r
+        // indexes.\r
+        // @coreMode - setup our first alignments using GetNextAlignmentCore();\r
+        // also useful for merging\r
+        void Open(const vector<string> filenames, bool openIndexes = true, bool coreMode = false);\r
+\r
         // performs random-access jump to reference, position\r
         bool Jump(int refID, int position = 0);\r
-        // opens BAM files (and optional BAM index files, if provided)\r
-        //void Open(const vector<std::string&> filenames, const vector<std::string&> indexFilenames);\r
-        void Open(const vector<string> filenames);\r
+\r
         // returns file pointers to beginning of alignments\r
         bool Rewind(void);\r
 \r
@@ -60,6 +72,10 @@ class BamMultiReader {
 \r
         // retrieves next available alignment (returns success/fail) from all files\r
         bool GetNextAlignment(BamAlignment&);\r
+        // retrieves next available alignment (returns success/fail) from all files\r
+        // and populates the support data with information about the alignment\r
+        // *** BUT DOES NOT PARSE CHARACTER DATA FROM THE ALIGNMENT\r
+        bool GetNextAlignmentCore(BamAlignment&);\r
         // ... should this be private?\r
         bool HasOpenReaders(void);\r
 \r
@@ -68,13 +84,15 @@ class BamMultiReader {
         // ----------------------\r
 \r
         // returns unified SAM header text for all files\r
-        const string GetUnifiedHeaderText(void) const;\r
+        const string GetHeaderText(void) const;\r
         // returns number of reference sequences\r
         const int GetReferenceCount(void) const;\r
         // returns vector of reference objects\r
         const BamTools::RefVector GetReferenceData(void) const;\r
         // returns reference id (used for BamMultiReader::Jump()) for the given reference name\r
-        //const int GetReferenceID(const std::string& refName) const;\r
+        const int GetReferenceID(const std::string& refName) const;\r
+        // validates that we have a congruent set of BAM files that are aligned against the same reference sequences\r
+        void ValidateReaders() const;\r
 \r
         // ----------------------\r
         // BAM index operations\r
@@ -87,21 +105,18 @@ class BamMultiReader {
 \r
         // utility\r
         void PrintFilenames(void);\r
-        void UpdateAlignments(void);\r
-\r
+        void DumpAlignmentIndex(void);\r
 \r
     // private implementation\r
     private:\r
-        // TODO perhaps, for legibility, I should use a struct to wrap them all up\r
-        //      But this may actually make things more confusing, as I'm only\r
-        //      operating on them all simultaneously during GetNextAlignment\r
-        //      calls.\r
-        // all these vectors are ordered the same\r
-        // readers.at(N) refers to the same reader as alignments.at(N) and readerStates.at(N)\r
-        vector<BamReader*> readers; // the set of readers which we operate on\r
-        vector<BamAlignment*> alignments; // the equivalent set of alignments we use to step through the files\r
-        vector<BamReaderState> readerStates; // states of the various readers\r
-        // alignment position?\r
+\r
+        // the set of readers and alignments which we operate on, maintained throughout the life of this class\r
+        vector<pair<BamReader*, BamAlignment*> > readers;\r
+\r
+        // readers and alignments sorted by reference id and position, to keep track of the lowest (next) alignment\r
+        // when a reader reaches EOF, its entry is removed from this index\r
+        AlignmentIndex alignments;\r
+\r
         vector<string> fileNames;\r
 };\r
 \r