]> git.donarmstrong.com Git - bamtools.git/commitdiff
Cleanup in SortTool
authorderek <derekwbarnett@gmail.com>
Tue, 11 Oct 2011 20:52:51 +0000 (16:52 -0400)
committerderek <derekwbarnett@gmail.com>
Tue, 11 Oct 2011 20:52:51 +0000 (16:52 -0400)
 * Now using the new BamTools::Algorithms::Sort function objects
 * Special handling of unmapped alignments should no longer be necessary
as the sorting function objects (also used by multi-reader merging
strategy) handles those cases.

src/toolkit/bamtools_sort.cpp

index f4950588d95f72431906be62f96f4c682812962d..03143e18bee6b94c1cc7e3719046f40333b16372 100644 (file)
@@ -2,7 +2,7 @@
 // bamtools_sort.cpp (c) 2010 Derek Barnett, Erik Garrison
 // Marth Lab, Department of Biology, Boston College
 // ---------------------------------------------------------------------------
-// Last modified: 3 October 2011 (DB)
+// Last modified: 11 October 2011 (DB)
 // ---------------------------------------------------------------------------
 // Sorts an input BAM file
 // ***************************************************************************
 #include <api/SamConstants.h>
 #include <api/BamMultiReader.h>
 #include <api/BamWriter.h>
+#include <api/algorithms/Sort.h>
 #include <utils/bamtools_options.h>
 using namespace BamTools;
+using namespace BamTools::Algorithms;
 
 #include <cstdio>
 #include <algorithm>
@@ -34,30 +36,6 @@ namespace BamTools {
 //    compromise that should perform well on average.
 const unsigned int SORT_DEFAULT_MAX_BUFFER_COUNT  = 500000;  // max numberOfAlignments for buffer
 const unsigned int SORT_DEFAULT_MAX_BUFFER_MEMORY = 1024;    // Mb
-
-// -----------------------------------
-// comparison objects (for sorting)
-
-struct SortLessThanPosition {
-    bool operator() (const BamAlignment& lhs, const BamAlignment& rhs) {
-
-        // force unmapped alignments to end
-        if ( lhs.RefID == -1 ) return false;
-        if ( rhs.RefID == -1 ) return true;
-
-        // sort first on RefID, then by Position
-        if ( lhs.RefID != rhs.RefID )
-            return lhs.RefID < rhs.RefID;
-        else
-            return lhs.Position < rhs.Position;
-    }
-};
-
-struct SortLessThanName {
-    bool operator() (const BamAlignment& lhs, const BamAlignment& rhs) {
-        return lhs.Name < rhs.Name;
-    }
-};
     
 } // namespace BamTools
 
@@ -136,7 +114,7 @@ SortTool::SortToolPrivate::SortToolPrivate(SortTool::SortSettings* settings)
     // that way multiple sort runs don't trip on each other's temp files
     if ( m_settings) {
         size_t extensionFound = m_settings->InputBamFilename.find(".bam");
-        if (extensionFound != string::npos )
+        if ( extensionFound != string::npos )
             m_tempFilenameStub = m_settings->InputBamFilename.substr(0,extensionFound);
         m_tempFilenameStub.append(".sort.temp.");
     }
@@ -183,18 +161,10 @@ bool SortTool::SortToolPrivate::GenerateSortedRuns(void) {
 
             // if buffer is "full"
             else {
-
-                // push any unmapped reads into buffer,
-                // don't want to split these into a separate temp file
-                if ( !al.IsMapped() )
-                    buffer.push_back(al);
-
-                // "al" is mapped, so create a sorted temp file with current buffer contents
+                // so create a sorted temp file with current buffer contents
                 // then push "al" into fresh buffer
-                else {
-                    CreateSortedTempFile(buffer);
-                    buffer.push_back(al);
-                }
+                CreateSortedTempFile(buffer);
+                buffer.push_back(al);
             }
         }
     }
@@ -214,18 +184,10 @@ bool SortTool::SortToolPrivate::GenerateSortedRuns(void) {
 
             // if buffer is "full"
             else {
-
-                // push any unmapped reads into buffer,
-                // don't want to split these into a separate temp file
-                if ( !al.IsMapped() )
-                    buffer.push_back(al);
-
-                // "al" is mapped, so create a sorted temp file with current buffer contents
+                // create a sorted temp file with current buffer contents
                 // then push "al" into fresh buffer
-                else {
-                    CreateSortedTempFile(buffer);
-                    buffer.push_back(al);
-                }
+                CreateSortedTempFile(buffer);
+                buffer.push_back(al);
             }
         }
     }
@@ -320,9 +282,9 @@ void SortTool::SortToolPrivate::SortBuffer(vector<BamAlignment>& buffer) {
     
     // sort buffer by desired method
     if ( m_settings->IsSortingByName )
-        std::stable_sort ( buffer.begin(), buffer.end(), SortLessThanName() );
-    else 
-        std::stable_sort ( buffer.begin(), buffer.end(), SortLessThanPosition() );
+        std::stable_sort( buffer.begin(), buffer.end(), Sort::ByName() );
+    else
+        std::stable_sort( buffer.begin(), buffer.end(), Sort::ByPosition() );
 }
     
 bool SortTool::SortToolPrivate::WriteTempFile(const vector<BamAlignment>& buffer,