]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/api/internal/BamWriter_p.cpp
Merge branches 'master' and 'iodevice' into iodevice
[bamtools.git] / src / api / internal / BamWriter_p.cpp
index fbe64dbdc4807741298a34efb59f709d99a82e92..c9199b9a7b7859dfbfd4b0ef48f0de8aa58dc0f3 100644 (file)
@@ -1,15 +1,15 @@
 // ***************************************************************************
 // BamWriter_p.cpp (c) 2010 Derek Barnett
 // Marth Lab, Department of Biology, Boston College
-// All rights reserved.
 // ---------------------------------------------------------------------------
-// Last modified: 10 May 2011 (DB)
+// Last modified: 16 June 2011 (DB)
 // ---------------------------------------------------------------------------
 // Provides the basic functionality for producing BAM files
 // ***************************************************************************
 
 #include <api/BamAlignment.h>
 #include <api/BamConstants.h>
+#include <api/IBamIODevice.h>
 #include <api/internal/BamWriter_p.h>
 using namespace BamTools;
 using namespace BamTools::Internal;
@@ -125,7 +125,7 @@ void BamWriterPrivate::EncodeQuerySequence(const string& query, string& encodedQ
 
 // returns whether BAM file is open for writing or not
 bool BamWriterPrivate::IsOpen(void) const {
-    return m_stream.IsOpen;
+    return m_stream.IsOpen();
 }
 
 // opens the alignment archive
@@ -134,7 +134,7 @@ bool BamWriterPrivate::Open(const string& filename,
                             const RefVector& referenceSequences)
 {
     // open the BGZF file for writing, return failure if error
-    if ( !m_stream.Open(filename, "wb") )
+    if ( !m_stream.Open(filename, IBamIODevice::WriteOnly) )
         return false;
 
     // write BAM file 'metadata' components
@@ -156,11 +156,14 @@ void BamWriterPrivate::SaveAlignment(const BamAlignment& al) {
         if ( m_isBigEndian ) BamTools::SwapEndian_32(blockSize);
         m_stream.Write((char*)&blockSize, Constants::BAM_SIZEOF_INT);
 
+        // re-calculate bin (in case BamAlignment's position has been previously modified)
+        const uint32_t alignmentBin = CalculateMinimumBin(al.Position, al.GetEndPosition());
+
         // assign the BAM core data
         uint32_t buffer[Constants::BAM_CORE_BUFFER_SIZE];
         buffer[0] = al.RefID;
         buffer[1] = al.Position;
-        buffer[2] = (al.Bin << 16) | (al.MapQuality << 8) | al.SupportData.QueryNameLength;
+        buffer[2] = (alignmentBin << 16) | (al.MapQuality << 8) | al.SupportData.QueryNameLength;
         buffer[3] = (al.AlignmentFlag << 16) | al.SupportData.NumCigarOperations;
         buffer[4] = al.SupportData.QuerySequenceLength;
         buffer[5] = al.MateRefID;