// ***************************************************************************
// BamAlignment.cpp (c) 2009 Derek Barnett
// Marth Lab, Department of Biology, Boston College
-// All rights reserved.
// ---------------------------------------------------------------------------
-// Last modified: 19 April 2011 (DB)
+// Last modified: 22 April 2011 (DB)
// ---------------------------------------------------------------------------
// Provides the BamAlignment data structure
// ***************************************************************************
#include <utility>
using namespace std;
-// internal utility methods
-namespace BamTools {
-namespace Internal {
-
-/*! \fn bool IsValidSize(const string& tag, const string& type)
- \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
-*/
-bool IsValidSize(const string& tag, const string& type) {
- return (tag.size() == Constants::BAM_TAG_TAGSIZE) &&
- (type.size() == Constants::BAM_TAG_TYPESIZE);
-}
-
-/*! \fn bool SkipToNextTag(const char storageType, char* &pTagData, unsigned int& numBytesParsed)
- \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)
-
- \return \c if storageType was a recognized BAM tag type
- \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 SkipToNextTag(const char storageType, char* &pTagData, unsigned int& numBytesParsed) {
-
- switch (storageType) {
-
- case (Constants::BAM_TAG_TYPE_ASCII) :
- case (Constants::BAM_TAG_TYPE_INT8) :
- case (Constants::BAM_TAG_TYPE_UINT8) :
- ++numBytesParsed;
- ++pTagData;
- break;
-
- case (Constants::BAM_TAG_TYPE_INT16) :
- case (Constants::BAM_TAG_TYPE_UINT16) :
- numBytesParsed += sizeof(uint16_t);
- pTagData += sizeof(uint16_t);
- break;
-
- case (Constants::BAM_TAG_TYPE_FLOAT) :
- case (Constants::BAM_TAG_TYPE_INT32) :
- case (Constants::BAM_TAG_TYPE_UINT32) :
- numBytesParsed += sizeof(uint32_t);
- pTagData += sizeof(uint32_t);
- break;
-
- case (Constants::BAM_TAG_TYPE_STRING) :
- case (Constants::BAM_TAG_TYPE_HEX) :
- while( *pTagData ) {
- ++numBytesParsed;
- ++pTagData;
- }
- // increment for null-terminator
- ++numBytesParsed;
- ++pTagData;
- break;
-
- case (Constants::BAM_TAG_TYPE_ARRAY) :
-
- {
- // read array type
- const char arrayType = *pTagData;
- ++numBytesParsed;
- ++pTagData;
-
- // read number of elements
- int32_t numElements;
- memcpy(&numElements, pTagData, sizeof(uint32_t)); // already endian-swapped if necessary
- numBytesParsed += sizeof(uint32_t);
- pTagData += sizeof(uint32_t);
-
- // calculate number of bytes to skip
- int bytesToSkip = 0;
- switch (arrayType) {
- case (Constants::BAM_TAG_TYPE_INT8) :
- case (Constants::BAM_TAG_TYPE_UINT8) :
- bytesToSkip = numElements;
- break;
- case (Constants::BAM_TAG_TYPE_INT16) :
- case (Constants::BAM_TAG_TYPE_UINT16) :
- bytesToSkip = numElements*sizeof(uint16_t);
- break;
- case (Constants::BAM_TAG_TYPE_FLOAT) :
- case (Constants::BAM_TAG_TYPE_INT32) :
- case (Constants::BAM_TAG_TYPE_UINT32) :
- bytesToSkip = numElements*sizeof(uint32_t);
- break;
- default:
- cerr << "BamAlignment ERROR: unknown binary array type encountered: "
- << arrayType << endl;
- return false;
- }
-
- // skip binary array contents
- numBytesParsed += bytesToSkip;
- pTagData += bytesToSkip;
- break;
- }
-
- default:
- cerr << "BamAlignment ERROR: unknown tag type encountered"
- << storageType << endl;
- return false;
- }
-
- // return success
- return true;
-}
-
-/*! \fn bool FindTag(const std::string& tag, char* &pTagData, const unsigned int& tagDataLength, unsigned int& numBytesParsed)
- \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
-
- \return \c true if found
-
- \post If \a tag is found, \a pTagData will point to the byte where the tag data begins.
- \a numBytesParsed will correspond to the position in the full TagData string.
-
-*/
-bool 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, return true on match
- if ( strncmp(pTagType, tag.c_str(), 2) == 0 )
- 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;
- }
-
- // checked all tags, none match
- return false;
-}
-
-} // namespace Internal
-} // namespace BamTools
-
/*! \class BamTools::BamAlignment
\brief The main BAM alignment data structure.
if ( SupportData.HasCoreOnly ) return false;
// validate tag/type size & that type is OK for string value
- if ( !Internal::IsValidSize(tag, type) ) return false;
+ if ( !IsValidSize(tag, type) ) return false;
if ( type.at(0) != Constants::BAM_TAG_TYPE_STRING &&
type.at(0) != Constants::BAM_TAG_TYPE_HEX
)
// if tag already exists, return false
// use EditTag explicitly instead
- if ( Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
+ if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
return false;
// otherwise, copy tag data to temp buffer
string newTag = tag + type + value;
const int newTagDataLength = tagDataLength + newTag.size() + 1; // leave room for null-term
- char originalTagData[newTagDataLength];
+ char* originalTagData = new char[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);
+ delete[] originalTagData;
+
// return success
return true;
}
if ( SupportData.HasCoreOnly ) return false;
// validate tag/type size & that type is OK for uint32_t value
- if ( !Internal::IsValidSize(tag, type) ) return false;
+ if ( !IsValidSize(tag, type) ) return false;
if ( type.at(0) == Constants::BAM_TAG_TYPE_FLOAT ||
type.at(0) == Constants::BAM_TAG_TYPE_STRING ||
type.at(0) == Constants::BAM_TAG_TYPE_HEX ||
// if tag already exists, return false
// use EditTag explicitly instead
- if ( Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
+ if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
return false;
// otherwise, convert value to string
// copy original tag data to temp buffer
string newTag = tag + type;
const int newTagDataLength = tagDataLength + newTag.size() + 4; // leave room for new integer
- char originalTagData[newTagDataLength];
+ char* originalTagData = new char[newTagDataLength];
memcpy(originalTagData, TagData.c_str(), tagDataLength + 1); // '+1' for TagData null-term
// append newTag
// store temp buffer back in TagData
const char* newTagData = (const char*)originalTagData;
TagData.assign(newTagData, newTagDataLength);
+ delete[] originalTagData;
// return success
return true;
if ( SupportData.HasCoreOnly ) return false;
// validate tag/type size & that type is OK for float value
- if ( !Internal::IsValidSize(tag, type) ) return false;
+ if ( !IsValidSize(tag, type) ) return false;
if ( type.at(0) == Constants::BAM_TAG_TYPE_STRING ||
type.at(0) == Constants::BAM_TAG_TYPE_HEX ||
type.at(0) == Constants::BAM_TAG_TYPE_ARRAY
// if tag already exists, return false
// use EditTag explicitly instead
- if ( Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
+ if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
return false;
// otherwise, convert value to string
// copy original tag data to temp buffer
string newTag = tag + type;
const int newTagDataLength = tagDataLength + newTag.size() + 4; // leave room for new float
- char originalTagData[newTagDataLength];
+ char* originalTagData = new char[newTagDataLength];
memcpy(originalTagData, TagData.c_str(), tagDataLength + 1); // '+1' for TagData null-term
// append newTag
const char* newTagData = (const char*)originalTagData;
TagData.assign(newTagData, newTagDataLength);
+ delete[] originalTagData;
+
// return success
return true;
}
// if tag already exists, return false
// use EditTag explicitly instead
- if ( Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
+ if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
return false;
// build new tag's base information
const int newTagDataLength = tagDataLength +
Constants::BAM_TAG_ARRAYBASE_SIZE +
numElements*sizeof(uint8_t);
- char originalTagData[newTagDataLength];
+ char* originalTagData = new char[newTagDataLength];
memcpy(originalTagData, TagData.c_str(), tagDataLength+1); // '+1' for TagData's null-term
// write newTagBase (removes old null term)
const char* newTagData = (const char*)originalTagData;
TagData.assign(newTagData, newTagDataLength);
+ delete[] originalTagData;
+
// return success
return true;
}
// if tag already exists, return false
// use EditTag explicitly instead
- if ( Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
+ if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
return false;
// build new tag's base information
const int newTagDataLength = tagDataLength +
Constants::BAM_TAG_ARRAYBASE_SIZE +
numElements*sizeof(int8_t);
- char originalTagData[newTagDataLength];
+ char* originalTagData = new char[newTagDataLength];
memcpy(originalTagData, TagData.c_str(), tagDataLength+1); // '+1' for TagData's null-term
// write newTagBase (removes old null term)
const char* newTagData = (const char*)originalTagData;
TagData.assign(newTagData, newTagDataLength);
+ delete[] originalTagData;
+
// return success
return true;
}
// if tag already exists, return false
// use EditTag explicitly instead
- if ( Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
+ if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
return false;
// build new tag's base information
const int newTagDataLength = tagDataLength +
Constants::BAM_TAG_ARRAYBASE_SIZE +
numElements*sizeof(uint16_t);
- char originalTagData[newTagDataLength];
+ char* originalTagData = new char[newTagDataLength];
memcpy(originalTagData, TagData.c_str(), tagDataLength+1); // '+1' for TagData's null-term
// write newTagBase (removes old null term)
const char* newTagData = (const char*)originalTagData;
TagData.assign(newTagData, newTagDataLength);
+ delete[] originalTagData;
+
// return success
return true;
}
// if tag already exists, return false
// use EditTag explicitly instead
- if ( Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
+ if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
return false;
// build new tag's base information
const int newTagDataLength = tagDataLength +
Constants::BAM_TAG_ARRAYBASE_SIZE +
numElements*sizeof(int16_t);
- char originalTagData[newTagDataLength];
+ char* originalTagData = new char[newTagDataLength];
memcpy(originalTagData, TagData.c_str(), tagDataLength+1); // '+1' for TagData's null-term
// write newTagBase (removes old null term)
const char* newTagData = (const char*)originalTagData;
TagData.assign(newTagData, newTagDataLength);
+ delete[] originalTagData;
+
// return success
return true;
}
// if tag already exists, return false
// use EditTag explicitly instead
- if ( Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
+ if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
return false;
// build new tag's base information
const int newTagDataLength = tagDataLength +
Constants::BAM_TAG_ARRAYBASE_SIZE +
numElements*sizeof(uint32_t);
- char originalTagData[newTagDataLength];
+ char* originalTagData = new char[newTagDataLength];
memcpy(originalTagData, TagData.c_str(), tagDataLength+1); // '+1' for TagData's null-term
// write newTagBase (removes old null term)
const char* newTagData = (const char*)originalTagData;
TagData.assign(newTagData, newTagDataLength);
+ delete[] originalTagData;
+
// return success
return true;
}
// if tag already exists, return false
// use EditTag explicitly instead
- if ( Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
+ if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
return false;
// build new tag's base information
const int newTagDataLength = tagDataLength +
Constants::BAM_TAG_ARRAYBASE_SIZE +
numElements*sizeof(int32_t);
- char originalTagData[newTagDataLength];
+ char* originalTagData = new char[newTagDataLength];
memcpy(originalTagData, TagData.c_str(), tagDataLength+1); // '+1' for TagData's null-term
// write newTagBase (removes old null term)
const char* newTagData = (const char*)originalTagData;
TagData.assign(newTagData, newTagDataLength);
+ delete[] originalTagData;
+
// return success
return true;
}
// if tag already exists, return false
// use EditTag explicitly instead
- if ( Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
+ if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
return false;
// build new tag's base information
const int newTagDataLength = tagDataLength +
Constants::BAM_TAG_ARRAYBASE_SIZE +
numElements*sizeof(float);
- char originalTagData[newTagDataLength];
+ char* originalTagData = new char[newTagDataLength];
memcpy(originalTagData, TagData.c_str(), tagDataLength+1); // '+1' for TagData's null-term
// write newTagBase (removes old null term)
const char* newTagData = (const char*)originalTagData;
TagData.assign(newTagData, newTagDataLength);
+ delete[] originalTagData;
+
// return success
return true;
}
if ( SupportData.HasCoreOnly ) return false;
// validate tag/type size & that type is OK for string value
- if ( !Internal::IsValidSize(tag, type) ) return false;
+ if ( !IsValidSize(tag, type) ) return false;
if ( type.at(0) != Constants::BAM_TAG_TYPE_STRING &&
type.at(0) != Constants::BAM_TAG_TYPE_HEX )
return false;
unsigned int numBytesParsed = 0;
// if tag found
- if ( Internal::FindTag(tag, pTagData, originalTagDataLength, numBytesParsed) ) {
+ if ( FindTag(tag, pTagData, originalTagDataLength, numBytesParsed) ) {
// make sure array is more than big enough
- char newTagData[originalTagDataLength + value.size()];
+ char* newTagData = new char[originalTagDataLength + value.size()];
// copy original tag data up til desired tag
const unsigned int beginningTagDataLength = numBytesParsed;
// skip to next tag (if tag for removal is last, return true)
const char* pTagStorageType = pTagData - 1;
- if ( !Internal::SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) )
+ if ( !SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) )
return true;
// copy everything from current tag (the next one after tag for removal) to end
// save new tag data
TagData.assign(newTagData, endTagOffset + endTagDataLength);
+
+ delete[] newTagData;
+
return true;
}
if ( SupportData.HasCoreOnly ) return false;
// validate tag/type size & that type is OK for uint32_t value
- if ( !Internal::IsValidSize(tag, type) ) return false;
+ if ( !IsValidSize(tag, type) ) return false;
if ( type.at(0) == Constants::BAM_TAG_TYPE_FLOAT ||
type.at(0) == Constants::BAM_TAG_TYPE_STRING ||
type.at(0) == Constants::BAM_TAG_TYPE_HEX ||
unsigned int numBytesParsed = 0;
// if tag found
- if ( Internal::FindTag(tag, pTagData, originalTagDataLength, numBytesParsed) ) {
+ if ( FindTag(tag, pTagData, originalTagDataLength, numBytesParsed) ) {
// make sure array is more than big enough
- char newTagData[originalTagDataLength + sizeof(value)];
+ char* newTagData = new char[originalTagDataLength + sizeof(value)];
// copy original tag data up til desired tag
const unsigned int beginningTagDataLength = numBytesParsed;
// skip to next tag (if tag for removal is last, return true)
const char* pTagStorageType = pTagData - 1;
- if ( !Internal::SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) )
+ if ( !SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) )
return true;
// copy everything from current tag (the next one after tag for removal) to end
// save new tag data
TagData.assign(newTagData, endTagOffset + endTagDataLength);
+
+ delete[] newTagData;
+
return true;
}
if ( SupportData.HasCoreOnly ) return false;
// validate tag/type size & that type is OK for float value
- if ( !Internal::IsValidSize(tag, type) ) return false;
+ if ( !IsValidSize(tag, type) ) return false;
if ( type.at(0) == Constants::BAM_TAG_TYPE_STRING ||
type.at(0) == Constants::BAM_TAG_TYPE_HEX ||
type.at(0) == Constants::BAM_TAG_TYPE_ARRAY
unsigned int numBytesParsed = 0;
// if tag found
- if ( Internal::FindTag(tag, pTagData, originalTagDataLength, numBytesParsed) ) {
+ if ( FindTag(tag, pTagData, originalTagDataLength, numBytesParsed) ) {
// make sure array is more than big enough
- char newTagData[originalTagDataLength + sizeof(value)];
+ char* newTagData = new char[originalTagDataLength + sizeof(value)];
// copy original tag data up til desired tag
const unsigned int beginningTagDataLength = numBytesParsed;
// skip to next tag (if tag for removal is last, return true)
const char* pTagStorageType = pTagData - 1;
- if ( !Internal::SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) )
+ if ( !SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) )
return true;
// copy everything from current tag (the next one after tag for removal) to end
// save new tag data
TagData.assign(newTagData, endTagOffset + endTagDataLength);
+
+ delete[] newTagData;
+
return true;
}
return AddTag(tag, values);
}
+/*! \fn bool BamAlignment::FindTag(const std::string& tag, char*& pTagData, const unsigned int& tagDataLength, unsigned int& numBytesParsed)
+ \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
+
+ \return \c true if found
+
+ \post If \a tag is found, \a pTagData will point to the byte where the tag data begins.
+ \a numBytesParsed will correspond to the position in the full TagData string.
+
+*/
+bool BamAlignment::FindTag(const std::string& tag,
+ char*& pTagData,
+ const unsigned int& tagDataLength,
+ unsigned int& numBytesParsed) const
+{
+
+ while ( numBytesParsed < tagDataLength ) {
+
+ const char* pTagType = pTagData;
+ const char* pTagStorageType = pTagData + 2;
+ pTagData += 3;
+ numBytesParsed += 3;
+
+ // check the current tag, return true on match
+ if ( strncmp(pTagType, tag.c_str(), 2) == 0 )
+ 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;
+ }
+
+ // checked all tags, none match
+ return false;
+}
+
/*! \fn bool BamAlignment::GetEditDistance(uint32_t& editDistance) const
\brief Retrieves value of edit distance tag ("NM").
unsigned int numBytesParsed = 0;
// if tag found
- if ( Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
+ if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
const unsigned int dataLength = strlen(pTagData);
destination.clear();
destination.resize(dataLength);
unsigned int numBytesParsed = 0;
// if tag found
- if ( Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
+ if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
// determine data byte-length
const char type = *(pTagData - 1);
unsigned int numBytesParsed = 0;
// if tag found
- if ( Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
+ if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
// determine data byte-length
const char type = *(pTagData - 1);
unsigned int numBytesParsed = 0;
// return false if tag not found
- if ( !Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
+ if ( !FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
return false;
// check that tag is array type
unsigned int numBytesParsed = 0;
// return false if tag not found
- if ( !Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
+ if ( !FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
return false;
// check that tag is array type
unsigned int numBytesParsed = 0;
// return false if tag not found
- if ( !Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
+ if ( !FindTag(tag, pTagData, tagDataLength, numBytesParsed) )
return false;
// check that tag is array type
unsigned int numBytesParsed = 0;
// lookup tag
- if ( Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
+ if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
// retrieve tag type code
type = *(pTagData - 1);
unsigned int numBytesParsed = 0;
// if result of tag lookup
- return Internal::FindTag(tag, pTagData, tagDataLength, numBytesParsed);
+ return FindTag(tag, pTagData, tagDataLength, numBytesParsed);
}
/*! \fn bool BamAlignment::IsDuplicate(void) const
return ( (AlignmentFlag & Constants::BAM_ALIGNMENT_READ_2) != 0 );
}
+/*! \fn bool BamAlignment::IsValidSize(const string& tag, const 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
+*/
+bool BamAlignment::IsValidSize(const string& tag, const string& type) const {
+ return (tag.size() == Constants::BAM_TAG_TAGSIZE) &&
+ (type.size() == Constants::BAM_TAG_TYPESIZE);
+}
+
/*! \fn bool BamAlignment::RemoveTag(const std::string& tag)
\brief Removes field from BAM tags.
unsigned int numBytesParsed = 0;
// if tag found
- if ( Internal::FindTag(tag, pTagData, originalTagDataLength, numBytesParsed) ) {
+ if ( FindTag(tag, pTagData, originalTagDataLength, numBytesParsed) ) {
- char newTagData[originalTagDataLength];
+ char* newTagData = new char[originalTagDataLength];
// copy original tag data up til desired tag
pTagData -= 3;
const char* pTagStorageType = pTagData + 2;
pTagData += 3;
numBytesParsed += 3;
- if ( !Internal::SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) )
+ if ( !SkipToNextTag(*pTagStorageType, pTagData, numBytesParsed) )
return true;
// copy everything from current tag (the next one after tag for removal) to end
// save new tag data
TagData.assign(newTagData, beginningTagDataLength + endTagDataLength);
+
+ delete[] newTagData;
+
return true;
}
void BamAlignment::SetIsUnmapped(bool ok) {
SetIsMapped(!ok);
}
+
+/*! \fn bool BamAlignment::SkipToNextTag(const char storageType, char*& pTagData, unsigned int& numBytesParsed)
+ \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)
+
+ \return \c if storageType was a recognized BAM tag type
+ \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) const
+{
+ switch (storageType) {
+
+ case (Constants::BAM_TAG_TYPE_ASCII) :
+ case (Constants::BAM_TAG_TYPE_INT8) :
+ case (Constants::BAM_TAG_TYPE_UINT8) :
+ ++numBytesParsed;
+ ++pTagData;
+ break;
+
+ case (Constants::BAM_TAG_TYPE_INT16) :
+ case (Constants::BAM_TAG_TYPE_UINT16) :
+ numBytesParsed += sizeof(uint16_t);
+ pTagData += sizeof(uint16_t);
+ break;
+
+ case (Constants::BAM_TAG_TYPE_FLOAT) :
+ case (Constants::BAM_TAG_TYPE_INT32) :
+ case (Constants::BAM_TAG_TYPE_UINT32) :
+ numBytesParsed += sizeof(uint32_t);
+ pTagData += sizeof(uint32_t);
+ break;
+
+ case (Constants::BAM_TAG_TYPE_STRING) :
+ case (Constants::BAM_TAG_TYPE_HEX) :
+ while( *pTagData ) {
+ ++numBytesParsed;
+ ++pTagData;
+ }
+ // increment for null-terminator
+ ++numBytesParsed;
+ ++pTagData;
+ break;
+
+ case (Constants::BAM_TAG_TYPE_ARRAY) :
+
+ {
+ // read array type
+ const char arrayType = *pTagData;
+ ++numBytesParsed;
+ ++pTagData;
+
+ // read number of elements
+ int32_t numElements;
+ memcpy(&numElements, pTagData, sizeof(uint32_t)); // already endian-swapped if necessary
+ numBytesParsed += sizeof(uint32_t);
+ pTagData += sizeof(uint32_t);
+
+ // calculate number of bytes to skip
+ int bytesToSkip = 0;
+ switch (arrayType) {
+ case (Constants::BAM_TAG_TYPE_INT8) :
+ case (Constants::BAM_TAG_TYPE_UINT8) :
+ bytesToSkip = numElements;
+ break;
+ case (Constants::BAM_TAG_TYPE_INT16) :
+ case (Constants::BAM_TAG_TYPE_UINT16) :
+ bytesToSkip = numElements*sizeof(uint16_t);
+ break;
+ case (Constants::BAM_TAG_TYPE_FLOAT) :
+ case (Constants::BAM_TAG_TYPE_INT32) :
+ case (Constants::BAM_TAG_TYPE_UINT32) :
+ bytesToSkip = numElements*sizeof(uint32_t);
+ break;
+ default:
+ cerr << "BamAlignment ERROR: unknown binary array type encountered: "
+ << arrayType << endl;
+ return false;
+ }
+
+ // skip binary array contents
+ numBytesParsed += bytesToSkip;
+ pTagData += bytesToSkip;
+ break;
+ }
+
+ default:
+ cerr << "BamAlignment ERROR: unknown tag type encountered"
+ << storageType << endl;
+ return false;
+ }
+
+ // return success
+ return true;
+}