]> git.donarmstrong.com Git - bamtools.git/commitdiff
fixed bug with @RG handling
authorErik Garrison <erik.garrison@bc.edu>
Wed, 9 Jun 2010 13:31:01 +0000 (09:31 -0400)
committerErik Garrison <erik.garrison@bc.edu>
Wed, 9 Jun 2010 13:31:01 +0000 (09:31 -0400)
Prior to this commit files merged with bamtools merge would have one @RG
tag for each file.  This is undesirable behavior.  This commit fixes the
issue by tracking unique @RG tags in our unified header
(BamMultiReader::GetHeaderText) and prevents the MultiReader from
observing more than one @RG tag in the header.  Future merges will have
the correct header.

BamMultiReader.cpp
BamMultiReader.h

index 2d8580d1d96158d0afe133e87d57770503b0a22d..58d82a2d016b49de532d1ec85be39650a982d915 100644 (file)
@@ -204,6 +204,7 @@ bool BamMultiReader::CreateIndexes(void) {
 const string BamMultiReader::GetHeaderText(void) const {
 
     string mergedHeader = "";
+    map<string, bool> readGroups;
 
     // foreach extraction entry (each BAM file)
     bool isFirstTime = true;
@@ -231,10 +232,24 @@ const string BamMultiReader::GetHeaderText(void) const {
                 }
             }
 
-            // (for all files) append RG entries
+            // (for all files) append RG entries if they are unique
             if ( headerLine.find("@RG") == 0 ) {
-                mergedHeader.append(headerLine.c_str() );
-                mergedHeader.append(1, '\n');
+                stringstream headerLineSs(headerLine);
+                string part, readGroupPart, readGroup;
+                while(std::getline(headerLineSs, part, '\t')) {
+                    if (part == "@RG") {
+                       std::getline(headerLineSs, readGroupPart, '\t');
+                       stringstream readGroupPartSs(readGroupPart);
+                       std::getline(readGroupPartSs, readGroup, ':');
+                       std::getline(readGroupPartSs, readGroup, ':');
+                       break;
+                    }
+                }
+                if (readGroups.find(readGroup) == readGroups.end()) { // prevents duplicate @RG entries
+                    mergedHeader.append(headerLine.c_str() );
+                    mergedHeader.append(1, '\n');
+                    readGroups[readGroup] = true;
+                }
             }
 
         }
index f1a2bb242e450785c5cacf32517d393a93fc9e1a..6d5a805f2739c9306ac6c9d2f23bc37b63ab388e 100644 (file)
@@ -15,6 +15,7 @@
 #include <string>\r
 #include <map>\r
 #include <utility> // for pair\r
+#include <sstream>\r
 \r
 using namespace std;\r
 \r
@@ -27,6 +28,7 @@ namespace BamTools {
 // 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
     // constructor / destructor\r