From: derek Date: Tue, 11 Oct 2011 20:52:51 +0000 (-0400) Subject: Cleanup in SortTool X-Git-Url: https://git.donarmstrong.com/?p=bamtools.git;a=commitdiff_plain;h=15a173d229110e7fbac63774890853d78539fa1d Cleanup in SortTool * 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. --- diff --git a/src/toolkit/bamtools_sort.cpp b/src/toolkit/bamtools_sort.cpp index f495058..03143e1 100644 --- a/src/toolkit/bamtools_sort.cpp +++ b/src/toolkit/bamtools_sort.cpp @@ -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 // *************************************************************************** @@ -12,8 +12,10 @@ #include #include #include +#include #include using namespace BamTools; +using namespace BamTools::Algorithms; #include #include @@ -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& 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& buffer,