From: Derek Date: Tue, 27 Jul 2010 16:52:58 +0000 (-0400) Subject: Added AddTag/EditTag/RemoveTag methods to BamAlignment. Refactored existing GetTag... X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=9d0a3d3a733ff4c4a662ddef52ee476144d50440;p=bamtools.git Added AddTag/EditTag/RemoveTag methods to BamAlignment. Refactored existing GetTag methods. --- diff --git a/BamAux.h b/BamAux.h index 833f52f..73e8838 100644 --- a/BamAux.h +++ b/BamAux.h @@ -3,7 +3,7 @@ // Marth Lab, Department of Biology, Boston College // All rights reserved. // --------------------------------------------------------------------------- -// Last modified: 22 July 2010 (DB) +// Last modified: 27 July 2010 (DB) // --------------------------------------------------------------------------- // Provides the basic constants, data structures, etc. for using BAM files // *************************************************************************** @@ -24,9 +24,6 @@ #include #include -#include -#include - // Platform-specific type definitions #ifndef BAMTOOLS_TYPES #define BAMTOOLS_TYPES @@ -106,44 +103,73 @@ struct BamAlignment { // Tag data access methods public: + // ------------------------------------------------------------------------------------- + // N.B. - The following tag-modifying methods may not be used on BamAlignments fetched + // using BamReader::GetNextAlignmentCore(). Attempting to use them will not result in + // error message (to keep output clean) but will ALWAYS return false. Only user- + // generated BamAlignments or those retrieved using BamReader::GetNextAlignment() are valid. + + // add tag data (create new TAG entry with TYPE and VALUE) + // TYPE is one of {A, i, f, Z, H} depending on VALUE - see SAM/BAM spec for details + // returns true if new data added, false if error or TAG already exists + // N.B. - will NOT modify existing tag. Use EditTag() instead + bool AddTag(const std::string& tag, const std::string& type, const std::string& value); // type must be Z or H + bool AddTag(const std::string& tag, const std::string& type, const uint32_t& value); // type must be A or i + bool AddTag(const std::string& tag, const std::string& type, const int32_t& value); // type must be A or i + bool AddTag(const std::string& tag, const std::string& type, const float& value); // type must be A, i, or f + + // edit tag data (sets existing TAG with TYPE to VALUE or adds new TAG if not already present) + // TYPE is one of {A, i, f, Z, H} depending on VALUE - see SAM/BAM spec for details + // returns true if edit was successfaul, false if error + bool EditTag(const std::string& tag, const std::string& type, const std::string& value); // type must be Z or H + bool EditTag(const std::string& tag, const std::string& type, const uint32_t& value); // type must be A or i + bool EditTag(const std::string& tag, const std::string& type, const int32_t& value); // type must be A or i + bool EditTag(const std::string& tag, const std::string& type, const float& value); // type must be A, i, or f + + // specific tag data access methods - these only remain for legacy support + bool GetEditDistance(uint32_t& editDistance) const; // get "NM" tag data (implemented as GetTag("NM", editDistance)) + bool GetReadGroup(std::string& readGroup) const; // get "RG" tag data (implemented as GetTag("RG", readGroup)) + // generic tag data access methods bool GetTag(const std::string& tag, std::string& destination) const; // access variable-length char or hex strings bool GetTag(const std::string& tag, uint32_t& destination) const; // access unsigned integer data bool GetTag(const std::string& tag, int32_t& destination) const; // access signed integer data bool GetTag(const std::string& tag, float& destination) const; // access floating point data - // specific tag data access methods - only remain here for legacy support - bool GetEditDistance(uint8_t& editDistance) const; // get "NM" tag data - contributed by Aaron Quinlan - bool GetReadGroup(std::string& readGroup) const; // get "RG" tag data - + // remove tag data + // returns true if removal was successful, false if error + // N.B. - returns false if TAG does not exist (no removal can occur) + bool RemoveTag(const std::string& tag); // Additional data access methods public: - int GetEndPosition(bool usePadded = false) const; // calculates alignment end position, based on starting position and CIGAR operations + int GetEndPosition(bool usePadded = false) const; // calculates alignment end position, based on starting position and CIGAR operations // 'internal' utility methods private: + static bool FindTag(const std::string& tag, char* &pTagData, const unsigned int& tagDataLength, unsigned int& numBytesParsed); static bool SkipToNextTag(const char storageType, char* &pTagData, unsigned int& numBytesParsed); // Data members public: - std::string Name; // Read name - int32_t Length; // Query length - std::string QueryBases; // 'Original' sequence (as reported from sequencing machine) - std::string AlignedBases; // 'Aligned' sequence (includes any indels, padding, clipping) - std::string Qualities; // FASTQ qualities (ASCII characters, not numeric values) - std::string TagData; // Tag data (accessor methods will pull the requested information out) - int32_t RefID; // ID number for reference sequence - int32_t Position; // Position (0-based) where alignment starts - uint16_t Bin; // Bin in BAM file where this alignment resides - uint16_t MapQuality; // Mapping quality score - uint32_t AlignmentFlag; // Alignment bit-flag - see Is() methods to query this value, SetIs() methods to manipulate + std::string Name; // Read name + int32_t Length; // Query length + std::string QueryBases; // 'Original' sequence (as reported from sequencing machine) + std::string AlignedBases; // 'Aligned' sequence (includes any indels, padding, clipping) + std::string Qualities; // FASTQ qualities (ASCII characters, not numeric values) + std::string TagData; // Tag data (accessor methods will pull the requested information out) + int32_t RefID; // ID number for reference sequence + int32_t Position; // Position (0-based) where alignment starts + uint16_t Bin; // Bin in BAM file where this alignment resides + uint16_t MapQuality; // Mapping quality score + uint32_t AlignmentFlag; // Alignment bit-flag - see Is() methods to query this value, SetIs() methods to manipulate std::vector CigarData; // CIGAR operations for this alignment - int32_t MateRefID; // ID number for reference sequence where alignment's mate was aligned - int32_t MatePosition; // Position (0-based) where alignment's mate starts - int32_t InsertSize; // Mate-pair insert size - + int32_t MateRefID; // ID number for reference sequence where alignment's mate was aligned + int32_t MatePosition; // Position (0-based) where alignment's mate starts + int32_t InsertSize; // Mate-pair insert size + // internal data + private: struct BamAlignmentSupportData { // data members @@ -164,7 +190,13 @@ struct BamAlignment { { } }; - BamAlignmentSupportData SupportData; // Contains raw character data & lengths + // contains raw character data & lengths + BamAlignmentSupportData SupportData; + + // allow these classes access to BamAlignment private members (SupportData) + // but client code should not need to touch this data + friend class BamReader; + friend class BamWriter; // Alignment flag query constants // Use the get/set methods above instead @@ -238,15 +270,93 @@ struct BamRegion { { } }; +// ---------------------------------------------------------------- +// Added: 3-35-2010 DWB +// Fixed: Routines to provide endian-correctness +// ---------------------------------------------------------------- + +// returns true if system is big endian +inline bool SystemIsBigEndian(void) { + const uint16_t one = 0x0001; + return ((*(char*) &one) == 0 ); +} + +// swaps endianness of 16-bit value 'in place' +inline void SwapEndian_16(int16_t& x) { + x = ((x >> 8) | (x << 8)); +} + +inline void SwapEndian_16(uint16_t& x) { + x = ((x >> 8) | (x << 8)); +} + +// swaps endianness of 32-bit value 'in-place' +inline void SwapEndian_32(int32_t& x) { + x = ( (x >> 24) | + ((x << 8) & 0x00FF0000) | + ((x >> 8) & 0x0000FF00) | + (x << 24) + ); +} + +inline void SwapEndian_32(uint32_t& x) { + x = ( (x >> 24) | + ((x << 8) & 0x00FF0000) | + ((x >> 8) & 0x0000FF00) | + (x << 24) + ); +} + +// swaps endianness of 64-bit value 'in-place' +inline void SwapEndian_64(int64_t& x) { + x = ( (x >> 56) | + ((x << 40) & 0x00FF000000000000ll) | + ((x << 24) & 0x0000FF0000000000ll) | + ((x << 8) & 0x000000FF00000000ll) | + ((x >> 8) & 0x00000000FF000000ll) | + ((x >> 24) & 0x0000000000FF0000ll) | + ((x >> 40) & 0x000000000000FF00ll) | + (x << 56) + ); +} + +inline void SwapEndian_64(uint64_t& x) { + x = ( (x >> 56) | + ((x << 40) & 0x00FF000000000000ll) | + ((x << 24) & 0x0000FF0000000000ll) | + ((x << 8) & 0x000000FF00000000ll) | + ((x >> 8) & 0x00000000FF000000ll) | + ((x >> 24) & 0x0000000000FF0000ll) | + ((x >> 40) & 0x000000000000FF00ll) | + (x << 56) + ); +} + +// swaps endianness of 'next 2 bytes' in a char buffer (in-place) +inline void SwapEndian_16p(char* data) { + uint16_t& value = (uint16_t&)*data; + SwapEndian_16(value); +} + +// swaps endianness of 'next 4 bytes' in a char buffer (in-place) +inline void SwapEndian_32p(char* data) { + uint32_t& value = (uint32_t&)*data; + SwapEndian_32(value); +} + +// swaps endianness of 'next 8 bytes' in a char buffer (in-place) +inline void SwapEndian_64p(char* data) { + uint64_t& value = (uint64_t&)*data; + SwapEndian_64(value); +} + // ---------------------------------------------------------------- // BamAlignment member methods // constructors & destructor -inline -BamAlignment::BamAlignment(void) { } +inline BamAlignment::BamAlignment(void) { } -inline -BamAlignment::BamAlignment(const BamAlignment& other) +inline BamAlignment::BamAlignment(const BamAlignment& other) : Name(other.Name) , Length(other.Length) , QueryBases(other.QueryBases) @@ -265,8 +375,7 @@ BamAlignment::BamAlignment(const BamAlignment& other) , SupportData(other.SupportData) { } -inline -BamAlignment::~BamAlignment(void) { } +inline BamAlignment::~BamAlignment(void) { } // Queries against alignment flags inline bool BamAlignment::IsDuplicate(void) const { return ( (AlignmentFlag & DUPLICATE) != 0 ); } @@ -316,208 +425,381 @@ int BamAlignment::GetEndPosition(bool usePadded) const { return alignEnd; } -// get "NM" tag data - contributed by Aaron Quinlan -// stores data in 'editDistance', returns success/fail -inline -bool BamAlignment::GetEditDistance(uint8_t& editDistance) const { - - // make sure tag data exists - if ( TagData.empty() ) return false; - +inline +bool BamAlignment::AddTag(const std::string& tag, const std::string& type, const std::string& value) { + + if ( SupportData.HasCoreOnly ) return false; + if ( tag.size() != 2 || type.size() != 1 ) return false; + if ( type != "Z" && type != "H" ) return false; + // localize the tag data char* pTagData = (char*)TagData.data(); - const unsigned int tagDataLen = TagData.size(); + const unsigned int tagDataLength = TagData.size(); unsigned int numBytesParsed = 0; + + // if tag already exists, return false + // use EditTag explicitly instead + if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) return false; + + // otherwise, copy tag data to temp buffer + std::string newTag = tag + type + value; + const int newTagDataLength = tagDataLength + newTag.size() + 1; // leave room for null-term + char originalTagData[newTagDataLength]; + memcpy(originalTagData, TagData.c_str(), tagDataLength + 1); // '+1' for TagData null-term + + // append newTag + strcat(originalTagData + tagDataLength, newTag.data()); // removes original null-term, appends newTag + null-term + + // store temp buffer back in TagData + const char* newTagData = (const char*)originalTagData; + TagData.assign(newTagData, newTagDataLength); + + // return success + return true; +} - bool foundEditDistanceTag = false; - while ( numBytesParsed < tagDataLen ) { - - const char* pTagType = pTagData; - const char* pTagStorageType = pTagData + 2; - pTagData += 3; - numBytesParsed += 3; - - // check the current tag - if ( strncmp(pTagType, "NM", 2) == 0 ) { - foundEditDistanceTag = true; - break; - } +inline +bool BamAlignment::AddTag(const std::string& tag, const std::string& type, const uint32_t& value) { + + if ( SupportData.HasCoreOnly ) return false; + if ( tag.size() != 2 || type.size() != 1 ) return false; + if ( type == "f" || type == "Z" || type == "H" ) return false; + + // localize the tag data + char* pTagData = (char*)TagData.data(); + const unsigned int tagDataLength = TagData.size(); + unsigned int numBytesParsed = 0; + + // if tag already exists, return false + // use EditTag explicitly instead + if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) return false; + + // otherwise, convert value to string + union { unsigned int value; char valueBuffer[sizeof(unsigned int)]; } un; + un.value = value; + + // copy original tag data to temp buffer + std::string newTag = tag + type; + const int newTagDataLength = tagDataLength + newTag.size() + 4; // leave room for new integer + char originalTagData[newTagDataLength]; + memcpy(originalTagData, TagData.c_str(), tagDataLength + 1); // '+1' for TagData null-term + + // append newTag + strcat(originalTagData + tagDataLength, newTag.data()); + memcpy(originalTagData + tagDataLength + newTag.size(), un.valueBuffer, sizeof(unsigned int)); + + // store temp buffer back in TagData + const char* newTagData = (const char*)originalTagData; + TagData.assign(newTagData, newTagDataLength); + + // return success + return true; +} - // get the storage class and find the next tag - if ( *pTagStorageType == '\0' ) return false; - if ( !SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) ) return false; - if ( *pTagData == '\0' ) return false; - } - // return if the edit distance tag was not present - if ( !foundEditDistanceTag ) return false; +inline +bool BamAlignment::AddTag(const std::string& tag, const std::string& type, const int32_t& value) { + return AddTag(tag, type, (const uint32_t&)value); +} - // assign the editDistance value - std::memcpy(&editDistance, pTagData, 1); +inline +bool BamAlignment::AddTag(const std::string& tag, const std::string& type, const float& value) { + + if ( SupportData.HasCoreOnly ) return false; + if ( tag.size() != 2 || type.size() != 1 ) return false; + if ( type == "Z" || type == "H" ) return false; + + // localize the tag data + char* pTagData = (char*)TagData.data(); + const unsigned int tagDataLength = TagData.size(); + unsigned int numBytesParsed = 0; + + // if tag already exists, return false + // use EditTag explicitly instead + if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) return false; + + // otherwise, convert value to string + union { float value; char valueBuffer[sizeof(float)]; } un; + un.value = value; + + // copy original tag data to temp buffer + std::string newTag = tag + type; + const int newTagDataLength = tagDataLength + newTag.size() + 4; // leave room for new float + char originalTagData[newTagDataLength]; + memcpy(originalTagData, TagData.c_str(), tagDataLength + 1); // '+1' for TagData null-term + + // append newTag + strcat(originalTagData + tagDataLength, newTag.data()); + memcpy(originalTagData + tagDataLength + newTag.size(), un.valueBuffer, sizeof(float)); + + // store temp buffer back in TagData + const char* newTagData = (const char*)originalTagData; + TagData.assign(newTagData, newTagDataLength); + + // return success return true; } -// get "RG" tag data -// stores data in 'readGroup', returns success/fail -inline -bool BamAlignment::GetReadGroup(std::string& readGroup) const { +inline +bool BamAlignment::EditTag(const std::string& tag, const std::string& type, const std::string& value) { + + if ( SupportData.HasCoreOnly ) return false; + if ( tag.size() != 2 || type.size() != 1 ) return false; + if ( type != "Z" && type != "H" ) return false; + + // localize the tag data + char* pOriginalTagData = (char*)TagData.data(); + char* pTagData = pOriginalTagData; + const unsigned int originalTagDataLength = TagData.size(); + + unsigned int newTagDataLength = 0; + unsigned int numBytesParsed = 0; + + // if tag found, store data in readGroup, return success + if ( FindTag(tag, pTagData, originalTagDataLength, numBytesParsed) ) { + + // make sure array is more than big enough + char newTagData[originalTagDataLength + value.size()]; - // make sure tag data exists - if ( TagData.empty() ) return false; + // copy original tag data up til desired tag + const unsigned int beginningTagDataLength = numBytesParsed; + newTagDataLength += beginningTagDataLength; + memcpy(newTagData, pOriginalTagData, numBytesParsed); + + // copy new VALUE in place of current tag data + const unsigned int dataLength = strlen(value.c_str()); + memcpy(newTagData + beginningTagDataLength, (char*)value.c_str(), dataLength+1 ); + + // skip to next tag (if tag for removal is last, return true) + const char* pTagStorageType = pTagData - 1; + if ( !SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) ) return true; + + // copy everything from current tag (the next one after tag for removal) to end + const unsigned int skippedDataLength = (numBytesParsed - beginningTagDataLength); + const unsigned int endTagOffset = beginningTagDataLength + dataLength + 1; + const unsigned int endTagDataLength = originalTagDataLength - beginningTagDataLength - skippedDataLength; + memcpy(newTagData + endTagOffset, pTagData, endTagDataLength); + + // ensure null-terminator + newTagData[ endTagOffset + endTagDataLength + 1 ] = 0; + + // save new tag data + TagData.assign(newTagData, endTagOffset + endTagDataLength); + return true; + } + + // tag not found, attempt AddTag + else return AddTag(tag, type, value); +} - // localize the tag data - char* pTagData = (char*)TagData.data(); - const unsigned int tagDataLen = TagData.size(); +inline +bool BamAlignment::EditTag(const std::string& tag, const std::string& type, const uint32_t& value) { + + if ( SupportData.HasCoreOnly ) return false; + if ( tag.size() != 2 || type.size() != 1 ) return false; + if ( type == "f" || type == "Z" || type == "H" ) return false; + + // localize the tag data + char* pOriginalTagData = (char*)TagData.data(); + char* pTagData = pOriginalTagData; + const unsigned int originalTagDataLength = TagData.size(); + + unsigned int newTagDataLength = 0; unsigned int numBytesParsed = 0; + + // if tag found, store data in readGroup, return success + if ( FindTag(tag, pTagData, originalTagDataLength, numBytesParsed) ) { + + // make sure array is more than big enough + char newTagData[originalTagDataLength + sizeof(value)]; - bool foundReadGroupTag = false; - while( numBytesParsed < tagDataLen ) { + // copy original tag data up til desired tag + const unsigned int beginningTagDataLength = numBytesParsed; + newTagDataLength += beginningTagDataLength; + memcpy(newTagData, pOriginalTagData, numBytesParsed); + + // copy new VALUE in place of current tag data + union { unsigned int value; char valueBuffer[sizeof(unsigned int)]; } un; + un.value = value; + memcpy(newTagData + beginningTagDataLength, un.valueBuffer, sizeof(unsigned int)); + + // skip to next tag (if tag for removal is last, return true) + const char* pTagStorageType = pTagData - 1; + if ( !SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) ) return true; + + // copy everything from current tag (the next one after tag for removal) to end + const unsigned int skippedDataLength = (numBytesParsed - beginningTagDataLength); + const unsigned int endTagOffset = beginningTagDataLength + sizeof(unsigned int); + const unsigned int endTagDataLength = originalTagDataLength - beginningTagDataLength - skippedDataLength; + memcpy(newTagData + endTagOffset, pTagData, endTagDataLength); + + // ensure null-terminator + newTagData[ endTagOffset + endTagDataLength + 1 ] = 0; + + // save new tag data + TagData.assign(newTagData, endTagOffset + endTagDataLength); + return true; + } + + // tag not found, attempt AddTag + else return AddTag(tag, type, value); +} - const char* pTagType = pTagData; - const char* pTagStorageType = pTagData + 2; - pTagData += 3; - numBytesParsed += 3; +inline +bool BamAlignment::EditTag(const std::string& tag, const std::string& type, const int32_t& value) { + return EditTag(tag, type, (const uint32_t&)value); +} - // check the current tag - if ( std::strncmp(pTagType, "RG", 2) == 0 ) { - foundReadGroupTag = true; - break; - } +inline +bool BamAlignment::EditTag(const std::string& tag, const std::string& type, const float& value) { + + if ( SupportData.HasCoreOnly ) return false; + if ( tag.size() != 2 || type.size() != 1 ) return false; + if ( type == "Z" || type == "H" ) return false; + + // localize the tag data + char* pOriginalTagData = (char*)TagData.data(); + char* pTagData = pOriginalTagData; + const unsigned int originalTagDataLength = TagData.size(); + + unsigned int newTagDataLength = 0; + unsigned int numBytesParsed = 0; + + // if tag found, store data in readGroup, return success + if ( FindTag(tag, pTagData, originalTagDataLength, numBytesParsed) ) { + + // make sure array is more than big enough + char newTagData[originalTagDataLength + sizeof(value)]; - // get the storage class and find the next tag - if ( *pTagStorageType == '\0' ) return false; - if ( !SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) ) return false; - if ( *pTagData == '\0' ) return false; + // copy original tag data up til desired tag + const unsigned int beginningTagDataLength = numBytesParsed; + newTagDataLength += beginningTagDataLength; + memcpy(newTagData, pOriginalTagData, numBytesParsed); + + // copy new VALUE in place of current tag data + union { float value; char valueBuffer[sizeof(float)]; } un; + un.value = value; + memcpy(newTagData + beginningTagDataLength, un.valueBuffer, sizeof(float)); + + // skip to next tag (if tag for removal is last, return true) + const char* pTagStorageType = pTagData - 1; + if ( !SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) ) return true; + + // copy everything from current tag (the next one after tag for removal) to end + const unsigned int skippedDataLength = (numBytesParsed - beginningTagDataLength); + const unsigned int endTagOffset = beginningTagDataLength + sizeof(float); + const unsigned int endTagDataLength = originalTagDataLength - beginningTagDataLength - skippedDataLength; + memcpy(newTagData + endTagOffset, pTagData, endTagDataLength); + + // ensure null-terminator + newTagData[ endTagOffset + endTagDataLength + 1 ] = 0; + + // save new tag data + TagData.assign(newTagData, endTagOffset + endTagDataLength); + return true; } + + // tag not found, attempt AddTag + else return AddTag(tag, type, value); +} - // return if the read group tag was not present - if ( !foundReadGroupTag ) return false; +// get "NM" tag data - originally contributed by Aaron Quinlan +// stores data in 'editDistance', returns success/fail +inline +bool BamAlignment::GetEditDistance(uint32_t& editDistance) const { + return GetTag("NM", (uint32_t&)editDistance); +} - // assign the read group - const unsigned int readGroupLen = std::strlen(pTagData); - readGroup.resize(readGroupLen); - std::memcpy( (char*)readGroup.data(), pTagData, readGroupLen ); - return true; +// get "RG" tag data +// stores data in 'readGroup', returns success/fail +inline +bool BamAlignment::GetReadGroup(std::string& readGroup) const { + return GetTag("RG", readGroup); } inline bool BamAlignment::GetTag(const std::string& tag, std::string& destination) const { - + // make sure tag data exists - if ( TagData.empty() ) return false; + if ( SupportData.HasCoreOnly || TagData.empty() ) + return false; // localize the tag data char* pTagData = (char*)TagData.data(); - const unsigned int tagDataLen = TagData.size(); + const unsigned int tagDataLength = TagData.size(); unsigned int numBytesParsed = 0; - - bool foundReadGroupTag = false; - while ( numBytesParsed < tagDataLen ) { - - const char* pTagType = pTagData; - const char* pTagStorageType = pTagData + 2; - pTagData += 3; - numBytesParsed += 3; - - // check the current tag - if ( std::strncmp(pTagType, tag.c_str(), 2) == 0 ) { - foundReadGroupTag = true; - break; - } - - // get the storage class and find the next tag - if ( *pTagStorageType == '\0' ) return false; - if ( !SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) ) return false; - if ( *pTagData == '\0' ) return false; + + // if tag found, store data in readGroup, return success + if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) { + const unsigned int dataLength = strlen(pTagData); + destination.clear(); + destination.resize(dataLength); + memcpy( (char*)destination.data(), pTagData, dataLength ); + return true; } - - // return if the read group tag was not present - if ( !foundReadGroupTag ) return false; - - // assign the read group - const unsigned int dataLen = std::strlen(pTagData); - destination.resize(dataLen); - std::memcpy( (char*)destination.data(), pTagData, dataLen ); - return true; + + // tag not found, return failure + return false; } inline bool BamAlignment::GetTag(const std::string& tag, uint32_t& destination) const { - // make sure data exists - if ( TagData.empty() ) return false; - - // clear out destination - destination = 0; + // make sure tag data exists + if ( SupportData.HasCoreOnly || TagData.empty() ) + return false; // localize the tag data char* pTagData = (char*)TagData.data(); - const unsigned int tagDataLen = TagData.size(); + const unsigned int tagDataLength = TagData.size(); unsigned int numBytesParsed = 0; - - int destinationLength = 0; - bool foundDesiredTag = false; - while ( numBytesParsed < tagDataLen ) { - - const char* pTagType = pTagData; - const char* pTagStorageType = pTagData + 2; - pTagData += 3; - numBytesParsed += 3; - - // check the current tag - if ( strncmp(pTagType, tag.c_str(), 2) == 0 ) { - - // determine actual length of data depending on tag type - // this is necessary because some tags may be of variable byte-lengths (i.e. char or short) - const char type = *pTagStorageType; - switch(type) { - - // 1 byte data - case 'A': - case 'c': - case 'C': - destinationLength = 1; - break; - - // 2 byte data - case 's': - case 'S': - destinationLength = 2; - break; - - // 4 byte data - case 'i': - case 'I': - destinationLength = 4; - break; - - // unsupported type for integer destination (float & var-length strings) - case 'f': - case 'Z': - case 'H': - printf("ERROR: Cannot store tag of type %c in integer destination\n", type); - return false; - - // unknown tag type - default: - printf("ERROR: Unknown tag storage class encountered: [%c]\n", *pTagData); - return false; - } - - foundDesiredTag = true; - break; + + // if tag found, determine data byte-length, store data in readGroup, return success + if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) { + + // determine data byte-length + const char type = *(pTagData - 1); + int destinationLength = 0; + switch (type) { + // 1 byte data + case 'A': + case 'c': + case 'C': + destinationLength = 1; + break; + + // 2 byte data + case 's': + case 'S': + destinationLength = 2; + break; + + // 4 byte data + case 'i': + case 'I': + destinationLength = 4; + break; + + // unsupported type for integer destination (float or var-length strings) + case 'f': + case 'Z': + case 'H': + printf("ERROR: Cannot store tag of type %c in integer destination\n", type); + return false; + + // unknown tag type + default: + printf("ERROR: Unknown tag storage class encountered: [%c]\n", type); + return false; } - - // get the storage class and find the next tag - if ( *pTagStorageType == '\0' ) return false; - if ( !SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) ) return false; - if ( *pTagData == '\0' ) return false; + + // store in destination + destination = 0; + memcpy(&destination, pTagData, destinationLength); + return true; } - // return if the edit distance tag was not present - if ( !foundDesiredTag ) return false; - - // assign the editDistance value - std::memcpy(&destination, pTagData, destinationLength); - return true; + + // tag not found, return failure + return false; } inline @@ -528,81 +810,134 @@ bool BamAlignment::GetTag(const std::string& tag, int32_t& destination) const { inline bool BamAlignment::GetTag(const std::string& tag, float& destination) const { - // make sure data exists - if ( TagData.empty() ) return false; - - // clear out destination - destination = 0.0; + // make sure tag data exists + if ( SupportData.HasCoreOnly || TagData.empty() ) + return false; // localize the tag data char* pTagData = (char*)TagData.data(); - const unsigned int tagDataLen = TagData.size(); + const unsigned int tagDataLength = TagData.size(); unsigned int numBytesParsed = 0; + + // if tag found, determine data byte-length, store data in readGroup, return success + if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) { + //pTagData += numBytesParsed; + + // determine data byte-length + const char type = *(pTagData - 1); + int destinationLength = 0; + switch(type) { + + // 1 byte data + case 'A': + case 'c': + case 'C': + destinationLength = 1; + break; + + // 2 byte data + case 's': + case 'S': + destinationLength = 2; + break; + + // 4 byte data + case 'f': + case 'i': + case 'I': + destinationLength = 4; + break; + + // unsupported type (var-length strings) + case 'Z': + case 'H': + printf("ERROR: Cannot store tag of type %c in integer destination\n", type); + return false; + + // unknown tag type + default: + printf("ERROR: Unknown tag storage class encountered: [%c]\n", type); + return false; + } + + // store in destination + destination = 0.0; + memcpy(&destination, pTagData, destinationLength); + return true; + } + + // tag not found, return failure + return false; +} - int destinationLength = 0; - bool foundDesiredTag = false; - while( numBytesParsed < tagDataLen ) { +inline +bool BamAlignment::RemoveTag(const std::string& tag) { + + // BamAlignments fetched using BamReader::GetNextAlignmentCore() are not allowed + // also, return false if no data present to remove + if ( SupportData.HasCoreOnly || TagData.empty() ) return false; + + // localize the tag data + char* pOriginalTagData = (char*)TagData.data(); + char* pTagData = pOriginalTagData; + const unsigned int originalTagDataLength = TagData.size(); + unsigned int newTagDataLength = 0; + unsigned int numBytesParsed = 0; + + // if tag found, store data in readGroup, return success + if ( FindTag(tag, pTagData, originalTagDataLength, numBytesParsed) ) { + + char 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); + + // skip to next tag (if tag for removal is last, return true) + const char* pTagStorageType = pTagData + 2; + pTagData += 3; + numBytesParsed += 3; + if ( !SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) ) return true; + + // copy everything from current tag (the next one after tag for removal) to end + const unsigned int skippedDataLength = (numBytesParsed - beginningTagDataLength); + const unsigned int endTagDataLength = originalTagDataLength - beginningTagDataLength - skippedDataLength; + memcpy(newTagData + beginningTagDataLength, pTagData, endTagDataLength ); + + // save new tag data + TagData.assign(newTagData, beginningTagDataLength + endTagDataLength); + return true; + } + + // tag not found, no removal - return failure + return false; +} - const char* pTagType = pTagData; +inline +bool BamAlignment::FindTag(const std::string& tag, char* &pTagData, const unsigned int& tagDataLength, unsigned int& numBytesParsed) { + + while ( numBytesParsed < tagDataLength ) { + + const char* pTagType = pTagData; const char* pTagStorageType = pTagData + 2; pTagData += 3; numBytesParsed += 3; - // check the current tag - if ( strncmp(pTagType, tag.c_str(), 2) == 0 ) { - - // determine actual length of data depending on tag type - // this is necessary because some tags may be of variable byte-lengths (i.e. char or short) - const char type = *pTagStorageType; - switch(type) { - - // 1 byte data - case 'A': - case 'c': - case 'C': - destinationLength = 1; - break; - - // 2 byte data - case 's': - case 'S': - destinationLength = 2; - break; - - // 4 byte data - case 'f': - case 'i': - case 'I': - destinationLength = 4; - break; - - // unsupported type (var-length strings) - case 'Z': - case 'H': - printf("ERROR: Cannot store tag of type %c in integer destination\n", type); - return false; - - // unknown tag type - default: - printf("ERROR: Unknown tag storage class encountered: [%c]\n", *pTagData); - return false; - } - - foundDesiredTag = true; - break; - } + // check the current tag, return true on match + if ( std::strncmp(pTagType, tag.c_str(), 2) == 0 ) + return true; // get the storage class and find the next tag - if ( *pTagStorageType == '\0' ) return false; + if ( *pTagStorageType == '\0' ) return false; if ( !SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) ) return false; if ( *pTagData == '\0' ) return false; } - // return if the edit distance tag was not present - if ( !foundDesiredTag ) return false; - - // assign the editDistance value - std::memcpy(&destination, pTagData, destinationLength); - return true; + + // checked all tags, none match + return false; } inline @@ -636,17 +971,14 @@ bool BamAlignment::SkipToNextTag(const char storageType, char* &pTagData, unsign ++numBytesParsed; ++pTagData; } - // --------------------------- - // Added: 3-25-2010 DWB - // Contributed: ARQ - // Fixed: error parsing variable length tag data + // increment for null-terminator + ++numBytesParsed; ++pTagData; - // --------------------------- break; default: // error case - printf("ERROR: Unknown tag storage class encountered: [%c]\n", *pTagData); + printf("ERROR: Unknown tag storage class encountered: [%c]\n", storageType); return false; } @@ -654,86 +986,6 @@ bool BamAlignment::SkipToNextTag(const char storageType, char* &pTagData, unsign return true; } -// ---------------------------------------------------------------- -// Added: 3-35-2010 DWB -// Fixed: Routines to provide endian-correctness -// ---------------------------------------------------------------- - -// returns true if system is big endian -inline bool SystemIsBigEndian(void) { - const uint16_t one = 0x0001; - return ((*(char*) &one) == 0 ); -} - -// swaps endianness of 16-bit value 'in place' -inline void SwapEndian_16(int16_t& x) { - x = ((x >> 8) | (x << 8)); -} - -inline void SwapEndian_16(uint16_t& x) { - x = ((x >> 8) | (x << 8)); -} - -// swaps endianness of 32-bit value 'in-place' -inline void SwapEndian_32(int32_t& x) { - x = ( (x >> 24) | - ((x << 8) & 0x00FF0000) | - ((x >> 8) & 0x0000FF00) | - (x << 24) - ); -} - -inline void SwapEndian_32(uint32_t& x) { - x = ( (x >> 24) | - ((x << 8) & 0x00FF0000) | - ((x >> 8) & 0x0000FF00) | - (x << 24) - ); -} - -// swaps endianness of 64-bit value 'in-place' -inline void SwapEndian_64(int64_t& x) { - x = ( (x >> 56) | - ((x << 40) & 0x00FF000000000000ll) | - ((x << 24) & 0x0000FF0000000000ll) | - ((x << 8) & 0x000000FF00000000ll) | - ((x >> 8) & 0x00000000FF000000ll) | - ((x >> 24) & 0x0000000000FF0000ll) | - ((x >> 40) & 0x000000000000FF00ll) | - (x << 56) - ); -} - -inline void SwapEndian_64(uint64_t& x) { - x = ( (x >> 56) | - ((x << 40) & 0x00FF000000000000ll) | - ((x << 24) & 0x0000FF0000000000ll) | - ((x << 8) & 0x000000FF00000000ll) | - ((x >> 8) & 0x00000000FF000000ll) | - ((x >> 24) & 0x0000000000FF0000ll) | - ((x >> 40) & 0x000000000000FF00ll) | - (x << 56) - ); -} - -// swaps endianness of 'next 2 bytes' in a char buffer (in-place) -inline void SwapEndian_16p(char* data) { - uint16_t& value = (uint16_t&)*data; - SwapEndian_16(value); -} - -// swaps endianness of 'next 4 bytes' in a char buffer (in-place) -inline void SwapEndian_32p(char* data) { - uint32_t& value = (uint32_t&)*data; - SwapEndian_32(value); -} - -// swaps endianness of 'next 8 bytes' in a char buffer (in-place) -inline void SwapEndian_64p(char* data) { - uint64_t& value = (uint64_t&)*data; - SwapEndian_64(value); -} - } // namespace BamTools #endif // BAMAUX_H