X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2Ftoolkit%2Fbamtools_sort.cpp;h=e268bee748b7d8c726bec2a71ade943a871f7f5d;hb=76bb08a359f3974eee3e96327b965ccf7958ed1a;hp=f4950588d95f72431906be62f96f4c682812962d;hpb=8a90b7aefffaf186053ef4da96c8663bf528274a;p=bamtools.git diff --git a/src/toolkit/bamtools_sort.cpp b/src/toolkit/bamtools_sort.cpp index f495058..e268bee 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: 27 March 2012 (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."); } @@ -155,6 +133,8 @@ bool SortTool::SortToolPrivate::GenerateSortedRuns(void) { // get basic data that will be shared by all temp/output files SamHeader header = reader.GetHeader(); + if ( !header.HasVersion() ) + header.Version = Constants::SAM_CURRENT_VERSION; header.SortOrder = ( m_settings->IsSortingByName ? Constants::SAM_HD_SORTORDER_QUERYNAME : Constants::SAM_HD_SORTORDER_COORDINATE ); @@ -183,18 +163,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 +186,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 +284,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,