// Marth Lab, Department of Biology, Boston College
// All rights reserved.
// ---------------------------------------------------------------------------
-// Last modified: 18 September 2010 (DB)
+// Last modified: 19 September 2010 (DB)
// ---------------------------------------------------------------------------
// Provides the BamAlignment data structure
// ***************************************************************************
#include <utility>
#include "BamAlignment.h"
using namespace BamTools;
+using namespace std;
// default ctor
BamAlignment::BamAlignment(void)
int alignEnd = Position;
// iterate over cigar operations
- std::vector<CigarOp>::const_iterator cigarIter = CigarData.begin();
- std::vector<CigarOp>::const_iterator cigarEnd = CigarData.end();
+ vector<CigarOp>::const_iterator cigarIter = CigarData.begin();
+ vector<CigarOp>::const_iterator cigarEnd = CigarData.end();
for ( ; cigarIter != cigarEnd; ++cigarIter) {
const char cigarType = (*cigarIter).Type;
if ( cigarType == 'M' || cigarType == 'D' || cigarType == 'N' )
return alignEnd;
}
-bool BamAlignment::AddTag(const std::string& tag, const std::string& type, const std::string& value) {
+bool BamAlignment::AddTag(const string& tag, const string& type, const string& value) {
if ( SupportData.HasCoreOnly ) return false;
if ( tag.size() != 2 || type.size() != 1 ) return false;
if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) return false;
// otherwise, copy tag data to temp buffer
- std::string newTag = tag + type + value;
+ 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
return true;
}
-bool BamAlignment::AddTag(const std::string& tag, const std::string& type, const uint32_t& value) {
+bool BamAlignment::AddTag(const string& tag, const string& type, const uint32_t& value) {
if ( SupportData.HasCoreOnly ) return false;
if ( tag.size() != 2 || type.size() != 1 ) return false;
un.value = value;
// copy original tag data to temp buffer
- std::string newTag = tag + type;
+ 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
return true;
}
-bool BamAlignment::AddTag(const std::string& tag, const std::string& type, const int32_t& value) {
+bool BamAlignment::AddTag(const string& tag, const string& type, const int32_t& value) {
return AddTag(tag, type, (const uint32_t&)value);
}
-bool BamAlignment::AddTag(const std::string& tag, const std::string& type, const float& value) {
+bool BamAlignment::AddTag(const string& tag, const string& type, const float& value) {
if ( SupportData.HasCoreOnly ) return false;
if ( tag.size() != 2 || type.size() != 1 ) return false;
un.value = value;
// copy original tag data to temp buffer
- std::string newTag = tag + type;
+ 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
return true;
}
-bool BamAlignment::EditTag(const std::string& tag, const std::string& type, const std::string& value) {
+bool BamAlignment::EditTag(const string& tag, const string& type, const string& value) {
if ( SupportData.HasCoreOnly ) return false;
if ( tag.size() != 2 || type.size() != 1 ) return false;
else return AddTag(tag, type, value);
}
-bool BamAlignment::EditTag(const std::string& tag, const std::string& type, const uint32_t& value) {
+bool BamAlignment::EditTag(const string& tag, const string& type, const uint32_t& value) {
if ( SupportData.HasCoreOnly ) return false;
if ( tag.size() != 2 || type.size() != 1 ) return false;
else return AddTag(tag, type, value);
}
-bool BamAlignment::EditTag(const std::string& tag, const std::string& type, const int32_t& value) {
+bool BamAlignment::EditTag(const string& tag, const string& type, const int32_t& value) {
return EditTag(tag, type, (const uint32_t&)value);
}
-bool BamAlignment::EditTag(const std::string& tag, const std::string& type, const float& value) {
+bool BamAlignment::EditTag(const string& tag, const string& type, const float& value) {
if ( SupportData.HasCoreOnly ) return false;
if ( tag.size() != 2 || type.size() != 1 ) return false;
// get "RG" tag data
// stores data in 'readGroup', returns success/fail
-bool BamAlignment::GetReadGroup(std::string& readGroup) const {
+bool BamAlignment::GetReadGroup(string& readGroup) const {
return GetTag("RG", readGroup);
}
-bool BamAlignment::GetTag(const std::string& tag, std::string& destination) const {
+bool BamAlignment::GetTag(const string& tag, string& destination) const {
// make sure tag data exists
if ( SupportData.HasCoreOnly || TagData.empty() )
return false;
}
-bool BamAlignment::GetTag(const std::string& tag, uint32_t& destination) const {
+bool BamAlignment::GetTag(const string& tag, uint32_t& destination) const {
// make sure tag data exists
if ( SupportData.HasCoreOnly || TagData.empty() )
return false;
}
-bool BamAlignment::GetTag(const std::string& tag, int32_t& destination) const {
+bool BamAlignment::GetTag(const string& tag, int32_t& destination) const {
return GetTag(tag, (uint32_t&)destination);
}
-bool BamAlignment::GetTag(const std::string& tag, float& destination) const {
+bool BamAlignment::GetTag(const string& tag, float& destination) const {
// make sure tag data exists
if ( SupportData.HasCoreOnly || TagData.empty() )
// 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);
return false;
}
-bool BamAlignment::RemoveTag(const std::string& tag) {
+bool BamAlignment::GetTagType(const string& tag, char& type) const {
+
+ // make sure tag data exists
+ if ( SupportData.HasCoreOnly || TagData.empty() )
+ return false;
+
+ // localize the tag data
+ char* pTagData = (char*)TagData.data();
+ const unsigned int tagDataLength = TagData.size();
+ unsigned int numBytesParsed = 0;
+
+ // lookup tag
+ if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
+
+ // retrieve tag type code
+ type = *(pTagData - 1);
+
+ // validate that type is a proper BAM tag type
+ switch(type) {
+ case 'A':
+ case 'c':
+ case 'C':
+ case 's':
+ case 'S':
+ case 'f':
+ case 'i':
+ case 'I':
+ case 'Z':
+ case 'H':
+ return true;
+
+ // unknown tag type
+ default:
+ fprintf(stderr, "ERROR: Unknown tag storage class encountered: [%c]\n", type);
+ return false;
+ }
+ }
+
+ // tag not found, return failure
+ return false;
+}
+
+bool BamAlignment::RemoveTag(const string& tag) {
// BamAlignments fetched using BamReader::GetNextAlignmentCore() are not allowed
// also, return false if no data present to remove
return false;
}
-bool BamAlignment::FindTag(const std::string& tag, char* &pTagData, const unsigned int& tagDataLength, unsigned int& numBytesParsed) {
+bool BamAlignment::FindTag(const string& tag, char* &pTagData, const unsigned int& tagDataLength, unsigned int& numBytesParsed) {
while ( numBytesParsed < tagDataLength ) {
numBytesParsed += 3;
// check the current tag, return true on match
- if ( std::strncmp(pTagType, tag.c_str(), 2) == 0 )
+ if ( strncmp(pTagType, tag.c_str(), 2) == 0 )
return true;
// get the storage class and find the next tag