From: Derek Barnett Date: Mon, 19 Nov 2012 04:23:13 +0000 (-0500) Subject: Added GetTagNames method to BamAlignment X-Git-Url: https://git.donarmstrong.com/?p=bamtools.git;a=commitdiff_plain;h=8897acb8a7562a08b76ec04093f8d2065ee7bb3b Added GetTagNames method to BamAlignment --- diff --git a/src/api/BamAlignment.cpp b/src/api/BamAlignment.cpp index 251c5e0..b8482f6 100644 --- a/src/api/BamAlignment.cpp +++ b/src/api/BamAlignment.cpp @@ -2,7 +2,7 @@ // BamAlignment.cpp (c) 2009 Derek Barnett // Marth Lab, Department of Biology, Boston College // --------------------------------------------------------------------------- -// Last modified: 4 April 2012 (DB) +// Last modified: 18 November 2012 (DB) // --------------------------------------------------------------------------- // Provides the BamAlignment data structure // *************************************************************************** @@ -551,6 +551,45 @@ bool BamAlignment::GetSoftClips(vector& clipSizes, return softClipFound; } +/*! \fn std::vector BamAlignment::GetTagNames(void) const + \brief Retrieves the BAM tag names. + + When paired with GetTagType() and GetTag(), this method allows you + to iterate over an alignment's tag data without knowing the names (or types) + beforehand. + + \return \c vector containing all tag names found (empty if none available) + \sa \samSpecURL for more details on reserved tag names, supported tag types, etc. +*/ +std::vector BamAlignment::GetTagNames(void) const { + + std::vector result; + if ( SupportData.HasCoreOnly || TagData.empty() ) + return result; + + char* pTagData = (char*)TagData.data(); + const unsigned int tagDataLength = TagData.size(); + unsigned int numBytesParsed = 0; + while ( numBytesParsed < tagDataLength ) { + + // get current tag name & type + const char* pTagName = pTagData; + const char* pTagType = pTagData + 2; + pTagData += 3; + numBytesParsed +=3; + + // store tag name + result.push_back( std::string(pTagName, 2) ); + + // find the next tag + if ( *pTagType == '\0' ) break; + if ( !SkipToNextTag(*pTagType, pTagData, numBytesParsed) ) break; + if ( *pTagData == '\0' ) break; + } + + return result; +} + /*! \fn bool BamAlignment::GetTagType(const std::string& tag, char& type) const \brief Retrieves the BAM tag type-code associated with requested tag name. diff --git a/src/api/BamAlignment.h b/src/api/BamAlignment.h index a2349ea..d18b239 100644 --- a/src/api/BamAlignment.h +++ b/src/api/BamAlignment.h @@ -2,7 +2,7 @@ // BamAlignment.h (c) 2009 Derek Barnett // Marth Lab, Department of Biology, Boston College // --------------------------------------------------------------------------- -// Last modified: 16 October 2011 (DB) +// Last modified: 18 November 2012 (DB) // --------------------------------------------------------------------------- // Provides the BamAlignment data structure // *************************************************************************** @@ -80,6 +80,9 @@ struct API_EXPORT BamAlignment { template bool GetTag(const std::string& tag, T& destination) const; template bool GetTag(const std::string& tag, std::vector& destination) const; + // retrieves all current tag names + std::vector GetTagNames(void) const; + // retrieves the SAM/BAM type-code for requested tag name bool GetTagType(const std::string& tag, char& type) const;