]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/api/BamAlignment.cpp
Cleaned up intra-API includes & moved version numbers to 2.0.0
[bamtools.git] / src / api / BamAlignment.cpp
index 980303ae6050f116b4e9197de9d10d5e259af9ec..78d7d6b22b8801e374d168a4684ce06cca83d86b 100644 (file)
@@ -2,23 +2,14 @@
 // BamAlignment.cpp (c) 2009 Derek Barnett
 // Marth Lab, Department of Biology, Boston College
 // ---------------------------------------------------------------------------
-// Last modified: 4 October 2011 (DB)
+// Last modified: 10 October 2011 (DB)
 // ---------------------------------------------------------------------------
 // Provides the BamAlignment data structure
 // ***************************************************************************
 
-#include <api/BamAlignment.h>
-#include <api/BamConstants.h>
+#include "api/BamAlignment.h"
+#include "api/BamConstants.h"
 using namespace BamTools;
-
-#include <cctype>
-#include <cstdio>
-#include <cstdlib>
-#include <cstring>
-#include <exception>
-#include <iostream>
-#include <map>
-#include <utility>
 using namespace std;
 
 /*! \class BamTools::BamAlignment
@@ -114,31 +105,6 @@ BamAlignment::BamAlignment(const BamAlignment& other)
 */
 BamAlignment::~BamAlignment(void) { }
 
-///*! \fn bool BamAlignment::AddTag(const std::string& tag, const std::string& type, const std::string& value)
-//    \brief Adds a field with string data to the BAM tags.
-
-//    Does NOT modify an existing tag - use \link BamAlignment::EditTag() \endlink instead.
-
-//    \param[in] tag   2-character tag name
-//    \param[in] type  1-character tag type (must be "Z" or "H")
-//    \param[in] value string data to store
-//    \return \c true if the \b new tag was added successfully
-//    \sa \samSpecURL for more details on reserved tag names, supported tag types, etc.
-//*/
-
-
-///*! \fn bool AddTag(const std::string& tag, const std::vector<uint8_t>& values);
-//    \brief Adds a numeric array field to the BAM tags.
-
-//    Does NOT modify an existing tag - use \link BamAlignment::EditTag() \endlink instead.
-
-//    \param tag    2-character tag name
-//    \param values vector of uint8_t values to store
-
-//    \return \c true if the \b new tag was added successfully
-//    \sa \samSpecURL for more details on reserved tag names, supported tag types, etc.
-//*/
-
 /*! \fn bool BamAlignment::BuildCharData(void)
     \brief Populates alignment string fields (read name, bases, qualities, tag data).
 
@@ -162,7 +128,7 @@ bool BamAlignment::BuildCharData(void) {
 
     // calculate character lengths/offsets
     const unsigned int dataLength     = SupportData.BlockLength - Constants::BAM_CORE_SIZE;
-    const unsigned int seqDataOffset  = SupportData.QueryNameLength + (SupportData.NumCigarOperations * 4);
+    const unsigned int seqDataOffset  = SupportData.QueryNameLength + (SupportData.NumCigarOperations*4);
     const unsigned int qualDataOffset = seqDataOffset + (SupportData.QuerySequenceLength+1)/2;
     const unsigned int tagDataOffset  = qualDataOffset + SupportData.QuerySequenceLength;
     const unsigned int tagDataLength  = dataLength - tagDataOffset;
@@ -185,8 +151,8 @@ bool BamAlignment::BuildCharData(void) {
     QueryBases.clear();
     if ( hasSeqData ) {
         QueryBases.reserve(SupportData.QuerySequenceLength);
-        for (unsigned int i = 0; i < SupportData.QuerySequenceLength; ++i) {
-            char singleBase = Constants::BAM_DNA_LOOKUP[ ( (seqData[(i/2)] >> (4*(1-(i%2)))) & 0xf ) ];
+        for ( size_t i = 0; i < SupportData.QuerySequenceLength; ++i ) {
+            const char singleBase = Constants::BAM_DNA_LOOKUP[ ( (seqData[(i/2)] >> (4*(1-(i%2)))) & 0xf ) ];
             QueryBases.append(1, singleBase);
         }
     }
@@ -195,8 +161,8 @@ bool BamAlignment::BuildCharData(void) {
     Qualities.clear();
     if ( hasQualData ) {
         Qualities.reserve(SupportData.QuerySequenceLength);
-        for (unsigned int i = 0; i < SupportData.QuerySequenceLength; ++i) {
-            char singleQuality = (char)(qualData[i]+33);
+        for ( size_t i = 0; i < SupportData.QuerySequenceLength; ++i ) {
+            const char singleQuality = static_cast<const char>(qualData[i]+33);
             Qualities.append(1, singleQuality);
         }
     }
@@ -218,7 +184,7 @@ bool BamAlignment::BuildCharData(void) {
         for ( ; cigarIter != cigarEnd; ++cigarIter ) {
             const CigarOp& op = (*cigarIter);
 
-            switch (op.Type) {
+            switch ( op.Type ) {
 
                 // for 'M', 'I', '=', 'X' - write bases
                 case (Constants::BAM_CIGAR_MATCH_CHAR)    :
@@ -253,11 +219,11 @@ bool BamAlignment::BuildCharData(void) {
                 case (Constants::BAM_CIGAR_HARDCLIP_CHAR) :
                     break;
 
-                // shouldn't get here
+                // invalid CIGAR op-code
                 default:
-                    cerr << "BamAlignment ERROR: invalid CIGAR operation type: "
-                         << op.Type << endl;
-                    exit(1);
+                    const string message = string("invalid CIGAR operation type: ") + op.Type;
+                    SetErrorString("BamAlignment::BuildCharData", message);
+                    return false;
             }
         }
     }
@@ -266,8 +232,8 @@ bool BamAlignment::BuildCharData(void) {
     TagData.clear();
     if ( hasTagData ) {
         if ( IsBigEndian ) {
-            int i = 0;
-            while ( (unsigned int)i < tagDataLength ) {
+            size_t i = 0;
+            while ( i < tagDataLength ) {
 
                 i += Constants::BAM_TAG_TAGSIZE;  // skip tag chars (e.g. "RG", "NM", etc.)
                 const char type = tagData[i];     // get tag type at position i
@@ -313,12 +279,12 @@ bool BamAlignment::BuildCharData(void) {
 
                         // swap endian-ness of number of elements in place, then retrieve for loop
                         BamTools::SwapEndian_32p(&tagData[i]);
-                        int32_t numElements;
+                        uint32_t numElements;
                         memcpy(&numElements, &tagData[i], sizeof(uint32_t));
                         i += sizeof(uint32_t);
 
                         // swap endian-ness of array elements
-                        for ( int j = 0; j < numElements; ++j ) {
+                        for ( size_t j = 0; j < numElements; ++j ) {
                             switch (arrayType) {
                                 case (Constants::BAM_TAG_TYPE_INT8)  :
                                 case (Constants::BAM_TAG_TYPE_UINT8) :
@@ -337,9 +303,8 @@ bool BamAlignment::BuildCharData(void) {
                                     i += sizeof(uint32_t);
                                     break;
                                 default:
-                                    // error case
-                                    cerr << "BamAlignment ERROR: unknown binary array type encountered: "
-                                         << arrayType << endl;
+                                    const string message = string("invalid binary array type: ") + arrayType;
+                                    SetErrorString("BamAlignment::BuildCharData", message);
                                     return false;
                             }
                         }
@@ -347,63 +312,34 @@ bool BamAlignment::BuildCharData(void) {
                         break;
                     }
 
-                    // shouldn't get here
+                    // invalid tag type-code
                     default :
-                        cerr << "BamAlignment ERROR: invalid tag value type: "
-                             << type << endl;
-                        exit(1);
+                        const string message = string("invalid tag type: ") + type;
+                        SetErrorString("BamAlignment::BuildCharData", message);
+                        return false;
                 }
             }
         }
 
         // store tagData in alignment
         TagData.resize(tagDataLength);
-        memcpy((char*)TagData.data(), tagData, tagDataLength);
+        memcpy((char*)(TagData.data()), tagData, tagDataLength);
     }
 
-    // clear the core-only flag
+    // clear core-only flag & return success
     SupportData.HasCoreOnly = false;
-
-    // return success
     return true;
 }
 
-///*! \fn bool BamAlignment::EditTag(const std::string& tag, const std::string& type, const std::string& value)
-//    \brief Edits a BAM tag field containing string data.
-
-//    If \a tag does not exist, a new entry is created.
-
-//    \param tag   2-character tag name
-//    \param type  1-character tag type (must be "Z" or "H")
-//    \param value string data to store
-
-//    \return \c true if the tag was modified/created successfully
-
-//    \sa BamAlignment::RemoveTag()
-//    \sa \samSpecURL for more details on reserved tag names, supported tag types, etc.
-//*/
-
-///*! \fn bool EditTag(const std::string& tag, const std::vector<uint8_t>& values);
-//    \brief Edits a BAM tag field containing a numeric array.
-
-//    If \a tag does not exist, a new entry is created.
-
-//    \param tag   2-character tag name
-//    \param value vector of uint8_t values to store
-
-//    \return \c true if the tag was modified/created successfully
-//    \sa \samSpecURL for more details on reserved tag names, supported tag types, etc.
-//*/
-
-/*! \fn bool BamAlignment::FindTag(const std::string& tag, char*& pTagData, const unsigned int& tagDataLength, unsigned int& numBytesParsed)
+/*! \fn bool BamAlignment::FindTag(const std::string& tag, char*& pTagData, const unsigned int& tagDataLength, unsigned int& numBytesParsed) const
     \internal
 
     Searches for requested tag in BAM tag data.
 
-    \param tag            requested 2-character tag name
-    \param pTagData       pointer to current position in BamAlignment::TagData
-    \param tagDataLength  length of BamAlignment::TagData
-    \param numBytesParsed number of bytes parsed so far
+    \param[in]     tag            requested 2-character tag name
+    \param[in,out] pTagData       pointer to current position in BamAlignment::TagData
+    \param[in]     tagDataLength  length of BamAlignment::TagData
+    \param[in,out] numBytesParsed number of bytes parsed so far
 
     \return \c true if found
 
@@ -414,7 +350,7 @@ bool BamAlignment::BuildCharData(void) {
 bool BamAlignment::FindTag(const std::string& tag,
                            char*& pTagData,
                            const unsigned int& tagDataLength,
-                           unsigned int& numBytesParsed)
+                           unsigned int& numBytesParsed) const
 {
 
     while ( numBytesParsed < tagDataLength ) {
@@ -438,35 +374,22 @@ bool BamAlignment::FindTag(const std::string& tag,
     return false;
 }
 
-/*! \fn bool BamAlignment::GetEditDistance(uint32_t& editDistance) const
-    \brief Retrieves value of edit distance tag ("NM").
+/*! \fn int BamAlignment::GetEndPosition(bool usePadded = false, bool closedInterval = false) const
+    \brief Calculates alignment end position, based on its starting position and CIGAR data.
 
-    \deprecated Instead use BamAlignment::GetTag()
-        \code
-            BamAlignment::GetTag("NM", editDistance);
-        \endcode
+    \warning The position returned now represents a zero-based, HALF-OPEN interval.
+    In previous versions of BamTools (0.x & 1.x) all intervals were treated
+    as zero-based, CLOSED.
 
-    \param editDistance destination for retrieved value
-
-    \return \c true if found
-*/
-
-// TODO : REMOVE THIS METHOD
-bool BamAlignment::GetEditDistance(uint32_t& editDistance) const {
-    return GetTag("NM", (uint32_t&)editDistance);
-}
-
-/*! \fn int BamAlignment::GetEndPosition(bool usePadded = false, bool zeroBased = true) const
-    \brief Calculates alignment end position, based on starting position and CIGAR data.
-
-    \param usePadded Inserted bases affect reported position. Default is false, so that reported
-                     position stays 'sync-ed' with reference coordinates.
-    \param zeroBased Return (BAM standard) 0-based coordinate. Setting this to false can be useful
-                     when using BAM data with half-open formats (e.g. BED).
+    \param[in] usePadded      Allow inserted bases to affect the reported position. Default is
+                              false, so that reported position stays synced with reference
+                              coordinates.
+    \param[in] closedInterval Setting this to true will return a 0-based end coordinate. Default is
+                              false, so that his value represents a standard, half-open interval.
 
     \return alignment end position
 */
-int BamAlignment::GetEndPosition(bool usePadded, bool zeroBased) const {
+int BamAlignment::GetEndPosition(bool usePadded, bool closedInterval) const {
 
     // initialize alignment end to starting position
     int alignEnd = Position;
@@ -475,74 +398,73 @@ int BamAlignment::GetEndPosition(bool usePadded, bool zeroBased) const {
     vector<CigarOp>::const_iterator cigarIter = CigarData.begin();
     vector<CigarOp>::const_iterator cigarEnd  = CigarData.end();
     for ( ; cigarIter != cigarEnd; ++cigarIter) {
-        const char cigarType = (*cigarIter).Type;
-        const uint32_t& cigarLength = (*cigarIter).Length;
-
-        if ( cigarType == Constants::BAM_CIGAR_MATCH_CHAR ||
-             cigarType == Constants::BAM_CIGAR_DEL_CHAR ||
-             cigarType == Constants::BAM_CIGAR_REFSKIP_CHAR )
-            alignEnd += cigarLength;
-        else if ( usePadded && cigarType == Constants::BAM_CIGAR_INS_CHAR )
-            alignEnd += cigarLength;
+        const CigarOp& op = (*cigarIter);
+
+        switch ( op.Type ) {
+
+            // increase end position on CIGAR chars [DMXN=]
+            case Constants::BAM_CIGAR_DEL_CHAR      :
+            case Constants::BAM_CIGAR_MATCH_CHAR    :
+            case Constants::BAM_CIGAR_MISMATCH_CHAR :
+            case Constants::BAM_CIGAR_REFSKIP_CHAR  :
+            case Constants::BAM_CIGAR_SEQMATCH_CHAR :
+                alignEnd += op.Length;
+                break;
+
+            // increase end position on insertion, only if @usePadded is true
+            case Constants::BAM_CIGAR_INS_CHAR :
+                if ( usePadded )
+                    alignEnd += op.Length;
+                break;
+
+            // all other CIGAR chars do not affect end position
+            default :
+                break;
+        }
     }
 
-    // adjust for zero-based coordinates, if requested
-    if ( zeroBased ) alignEnd -= 1;
+    // adjust for closedInterval, if requested
+    if ( closedInterval )
+        alignEnd -= 1;
 
     // return result
     return alignEnd;
 }
 
-/*! \fn bool BamAlignment::GetReadGroup(std::string& readGroup) const
-    \brief Retrieves value of read group tag ("RG").
+/*! \fn std::string BamAlignment::GetErrorString(void) const
+    \brief Returns a human-readable description of the last error that occurred
 
-    \deprecated Instead use BamAlignment::GetTag()
-        \code
-            BamAlignment::GetTag("RG", readGroup);
-        \endcode
+    This method allows elimination of STDERR pollution. Developers of client code
+    may choose how the messages are displayed to the user, if at all.
 
-    \param readGroup destination for retrieved value
-
-    \return \c true if found
+    \return error description
 */
-
-// TODO : REMOVE THIS METHOD
-bool BamAlignment::GetReadGroup(std::string& readGroup) const {
-    return GetTag("RG", readGroup);
+std::string BamAlignment::GetErrorString(void) const {
+    return ErrorString;
 }
 
-///*! \fn bool BamAlignment::GetTag(const std::string& tag, std::string& destination) const
-//    \brief Retrieves the string value associated with a BAM tag.
-
-//    \param tag         2-character tag name
-//    \param destination destination for retrieved value
-
-//    \return \c true if found
-//*/
-
-///*! \fn bool BamAlignment::GetTag(const std::string& tag, std::vector<uint32_t>& destination) const
-//    \brief Retrieves the numeric array data associated with a BAM tag
-
-//    \param tag         2-character tag name
-//    \param destination destination for retrieved data
-
-//    \return \c true if found
-//*/
-
 /*! \fn bool BamAlignment::GetTagType(const std::string& tag, char& type) const
     \brief Retrieves the BAM tag type-code associated with requested tag name.
 
-    \param tag  2-character tag name
-    \param type destination for the retrieved (1-character) tag type
+    \param[in]  tag  2-character tag name
+    \param[out] type retrieved (1-character) type-code
 
     \return \c true if found
     \sa \samSpecURL for more details on reserved tag names, supported tag types, etc.
 */
 bool BamAlignment::GetTagType(const std::string& tag, char& type) const {
   
-    // make sure tag data exists
-    if ( SupportData.HasCoreOnly || TagData.empty() ) 
+    // skip if alignment is core-only
+    if ( SupportData.HasCoreOnly ) {
+        // TODO: set error string?
+        return false;
+    }
+
+    // skip if no tags present
+    if ( TagData.empty() ) {
+        // TODO: set error string?
         return false;
+    }
 
     // localize the tag data
     char* pTagData = (char*)TagData.data();
@@ -550,8 +472,10 @@ bool BamAlignment::GetTagType(const std::string& tag, char& type) const {
     unsigned int numBytesParsed = 0;
     
     // if tag not found, return failure
-    if ( !FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
+    if ( !FindTag(tag, pTagData, tagDataLength, numBytesParsed) ){
+        // TODO: set error string?
         return false;
+    }
 
     // otherwise, retrieve & validate tag type code
     type = *(pTagData - 1);
@@ -571,15 +495,16 @@ bool BamAlignment::GetTagType(const std::string& tag, char& type) const {
 
         // unknown tag type
         default:
-            cerr << "BamAlignment ERROR: unknown tag type encountered: "
-                 << type << endl;
+            const string message = string("invalid tag type: ") + type;
+            SetErrorString("BamAlignment::GetTagType", message);
             return false;
     }
 }
 
 /*! \fn bool BamAlignment::HasTag(const std::string& tag) const
     \brief Returns true if alignment has a record for requested tag.
-    \param tag 2-character tag name
+
+    \param[in] tag 2-character tag name
     \return \c true if alignment has a record for tag
 */
 bool BamAlignment::HasTag(const std::string& tag) const {
@@ -674,29 +599,26 @@ bool BamAlignment::IsSecondMate(void) const {
     return ( (AlignmentFlag & Constants::BAM_ALIGNMENT_READ_2) != 0 );
 }
 
-/*! \fn bool BamAlignment::IsValidSize(const string& tag, const string& type) const
+/*! \fn bool BamAlignment::IsValidSize(const std::string& tag, const std::string& type) const
     \internal
 
     Checks that tag name & type strings are expected sizes.
-    \a tag  should have length
-    \a type should have length 1
 
-    \param tag  BAM tag name
-    \param type BAM tag type-code
-
-    \return \c true if both \a tag and \a type are correct sizes
+    \param tag[in]  BAM tag name
+    \param type[in] BAM tag type-code
+    \return \c true if both input strings are valid sizes
 */
-bool BamAlignment::IsValidSize(const string& tag, const string& type) {
+bool BamAlignment::IsValidSize(const std::string& tag, const std::string& type) const {
     return (tag.size()  == Constants::BAM_TAG_TAGSIZE) &&
            (type.size() == Constants::BAM_TAG_TYPESIZE);
 }
 
-/*! \fn bool BamAlignment::RemoveTag(const std::string& tag)
+/*! \fn void BamAlignment::RemoveTag(const std::string& tag)
     \brief Removes field from BAM tags.
 
-    \return \c true if tag was removed successfully (or didn't exist before)
+    \param[in] tag 2-character name of field to remove
 */
-bool BamAlignment::RemoveTag(const std::string& tag) {
+void BamAlignment::RemoveTag(const std::string& tag) {
   
     // if char data not populated, do that first
     if ( SupportData.HasCoreOnly )
@@ -704,7 +626,7 @@ bool BamAlignment::RemoveTag(const std::string& tag) {
 
     // skip if no tags available
     if ( TagData.empty() )
-        return false;
+        return;
   
     // localize the tag data
     char* pOriginalTagData = (char*)TagData.data();
@@ -713,19 +635,19 @@ bool BamAlignment::RemoveTag(const std::string& tag) {
     unsigned int newTagDataLength = 0;
     unsigned int numBytesParsed = 0;
 
-    // if tag not found, simply return true
+    // skip if tag not found
     if  ( !FindTag(tag, pTagData, originalTagDataLength, numBytesParsed) )
-        return true;
+        return;
 
     // otherwise, remove it
-    char* newTagData = new char[originalTagDataLength];
+    RaiiBuffer newTagData(originalTagDataLength);
 
     // copy original tag data up til desired tag
     pTagData       -= 3;
     numBytesParsed -= 3;
     const unsigned int beginningTagDataLength = numBytesParsed;
     newTagDataLength += beginningTagDataLength;
-    memcpy(newTagData, pOriginalTagData, numBytesParsed);
+    memcpy(newTagData.Buffer, pOriginalTagData, numBytesParsed);
 
     // attemp to skip to next tag
     const char* pTagStorageType = pTagData + 2;
@@ -736,15 +658,24 @@ bool BamAlignment::RemoveTag(const std::string& tag) {
         // squeeze remaining tag data
         const unsigned int skippedDataLength = (numBytesParsed - beginningTagDataLength);
         const unsigned int endTagDataLength = originalTagDataLength - beginningTagDataLength - skippedDataLength;
-        memcpy(newTagData + beginningTagDataLength, pTagData, endTagDataLength );
+        memcpy(newTagData.Buffer + beginningTagDataLength, pTagData, endTagDataLength );
 
         // save modified tag data in alignment
-        TagData.assign(newTagData, beginningTagDataLength + endTagDataLength);
+        TagData.assign(newTagData.Buffer, beginningTagDataLength + endTagDataLength);
     }
+}
 
-    // clean up & return success
-    delete[] newTagData;
-    return true;
+/*! \fn void BamAlignment::SetErrorString(const std::string& where, const std::string& what) const
+    \internal
+
+    Sets a formatted error string for this alignment.
+
+    \param[in] where class/method where error occurred
+    \param[in] what  description of error
+*/
+void BamAlignment::SetErrorString(const std::string& where, const std::string& what) const {
+    static const string SEPARATOR = ": ";
+    ErrorString = where + SEPARATOR + what;
 }
 
 /*! \fn void BamAlignment::SetIsDuplicate(bool ok)
@@ -787,15 +718,6 @@ void BamAlignment::SetIsMateMapped(bool ok) {
     else    AlignmentFlag |=  Constants::BAM_ALIGNMENT_MATE_UNMAPPED;
 }
 
-/*! \fn void BamAlignment::SetIsMateUnmapped(bool ok)
-    \brief Complement of using SetIsMateMapped().
-    \deprecated For sake of symmetry with the query methods
-    \sa IsMateMapped(), SetIsMateMapped()
-*/
-void BamAlignment::SetIsMateUnmapped(bool ok) {
-    SetIsMateMapped(!ok);
-}
-
 /*! \fn void BamAlignment::SetIsMateReverseStrand(bool ok)
     \brief Sets "alignment's mate mapped to reverse strand" flag to \a ok.
 */
@@ -836,15 +758,6 @@ void BamAlignment::SetIsReverseStrand(bool ok) {
     else    AlignmentFlag &= ~Constants::BAM_ALIGNMENT_REVERSE_STRAND;
 }
 
-/*! \fn void BamAlignment::SetIsSecondaryAlignment(bool ok)
-    \brief Complement of using SetIsPrimaryAlignment().
-    \deprecated For sake of symmetry with the query methods
-    \sa IsPrimaryAlignment(), SetIsPrimaryAlignment()
-*/
-void BamAlignment::SetIsSecondaryAlignment(bool ok) {
-    SetIsPrimaryAlignment(!ok);
-}
-
 /*! \fn void BamAlignment::SetIsSecondMate(bool ok)
     \brief Sets "alignment is second mate on read" flag to \a ok.
 */
@@ -853,31 +766,23 @@ void BamAlignment::SetIsSecondMate(bool ok) {
     else    AlignmentFlag &= ~Constants::BAM_ALIGNMENT_READ_2;
 }
 
-/*! \fn void BamAlignment::SetIsUnmapped(bool ok)
-    \brief Complement of using SetIsMapped().
-    \deprecated For sake of symmetry with the query methods
-    \sa IsMapped(), SetIsMapped()
-*/
-void BamAlignment::SetIsUnmapped(bool ok) {
-    SetIsMapped(!ok);
-}
-
-/*! \fn bool BamAlignment::SkipToNextTag(const char storageType, char*& pTagData, unsigned int& numBytesParsed)
+/*! \fn bool BamAlignment::SkipToNextTag(const char storageType, char*& pTagData, unsigned int& numBytesParsed) const
     \internal
 
     Moves to next available tag in tag data string
 
-    \param storageType    BAM tag type-code that determines how far to move cursor
-    \param pTagData       pointer to current position (cursor) in tag string
-    \param numBytesParsed report of how many bytes were parsed (cumulatively)
+    \param[in]     storageType    BAM tag type-code that determines how far to move cursor
+    \param[in,out] pTagData       pointer to current position (cursor) in tag string
+    \param[in,out] numBytesParsed report of how many bytes were parsed (cumulatively)
 
     \return \c if storageType was a recognized BAM tag type
-    \post \a pTagData will point to the byte where the next tag data begins.
+
+    \post \a pTagData       will point to the byte where the next tag data begins.
           \a numBytesParsed will correspond to the cursor's position in the full TagData string.
 */
 bool BamAlignment::SkipToNextTag(const char storageType,
                                  char*& pTagData,
-                                 unsigned int& numBytesParsed)
+                                 unsigned int& numBytesParsed) const
 {
     switch (storageType) {
 
@@ -922,7 +827,7 @@ bool BamAlignment::SkipToNextTag(const char storageType,
 
             // read number of elements
             int32_t numElements;
-            memcpy(&numElements, pTagData, sizeof(uint32_t)); // already endian-swapped if necessary
+            memcpy(&numElements, pTagData, sizeof(uint32_t)); // already endian-swapped, if needed
             numBytesParsed += sizeof(uint32_t);
             pTagData       += sizeof(uint32_t);
 
@@ -943,8 +848,8 @@ bool BamAlignment::SkipToNextTag(const char storageType,
                     bytesToSkip = numElements*sizeof(uint32_t);
                     break;
                 default:
-                    cerr << "BamAlignment ERROR: unknown binary array type encountered: "
-                         << arrayType << endl;
+                    const string message = string("invalid binary array type: ") + arrayType;
+                    SetErrorString("BamAlignment::SkipToNextTag", message);
                     return false;
             }
 
@@ -955,11 +860,11 @@ bool BamAlignment::SkipToNextTag(const char storageType,
         }
 
         default:
-            cerr << "BamAlignment ERROR: unknown tag type encountered"
-                 << storageType << endl;
+            const string message = string("invalid tag type: ") + storageType;
+            SetErrorString("BamAlignment::SkipToNextTag", message);
             return false;
     }
 
-    // return success
+    // if we get here, tag skipped OK - return success
     return true;
 }