10 #ifndef BAMALIGNMENT_H 11 #define BAMALIGNMENT_H 26 class BamReaderPrivate;
27 class BamWriterPrivate;
42 bool IsDuplicate(
void)
const;
43 bool IsFailedQC(
void)
const;
44 bool IsFirstMate(
void)
const;
45 bool IsMapped(
void)
const;
46 bool IsMateMapped(
void)
const;
47 bool IsMateReverseStrand(
void)
const;
48 bool IsPaired(
void)
const;
49 bool IsPrimaryAlignment(
void)
const;
50 bool IsProperPair(
void)
const;
51 bool IsReverseStrand(
void)
const;
52 bool IsSecondMate(
void)
const;
56 void SetIsDuplicate(
bool ok);
57 void SetIsFailedQC(
bool ok);
58 void SetIsFirstMate(
bool ok);
59 void SetIsMapped(
bool ok);
60 void SetIsMateMapped(
bool ok);
61 void SetIsMateReverseStrand(
bool ok);
62 void SetIsPaired(
bool ok);
63 void SetIsPrimaryAlignment(
bool ok);
64 void SetIsProperPair(
bool ok);
65 void SetIsReverseStrand(
bool ok);
66 void SetIsSecondMate(
bool ok);
72 template<
typename T>
bool AddTag(
const std::string& tag,
const std::string& type,
const T& value);
73 template<
typename T>
bool AddTag(
const std::string& tag,
const std::vector<T>& values);
76 template<
typename T>
bool EditTag(
const std::string& tag,
const std::string& type,
const T& value);
77 template<
typename T>
bool EditTag(
const std::string& tag,
const std::vector<T>& values);
80 template<
typename T>
bool GetTag(
const std::string& tag, T& destination)
const;
81 template<
typename T>
bool GetTag(
const std::string& tag, std::vector<T>& destination)
const;
84 std::vector<std::string> GetTagNames(
void)
const;
87 bool GetTagType(
const std::string& tag,
char& type)
const;
90 bool GetArrayTagType(
const std::string& tag,
char& type)
const;
93 bool HasTag(
const std::string& tag)
const;
96 void RemoveTag(
const std::string& tag);
101 bool BuildCharData(
void);
104 int GetEndPosition(
bool usePadded =
false,
bool closedInterval =
false)
const;
107 std::string GetErrorString(
void)
const;
110 bool GetSoftClips(std::vector<int>& clipSizes,
111 std::vector<int>& readPositions,
112 std::vector<int>& genomePositions,
113 bool usePadded =
false)
const;
137 bool FindTag(
const std::string& tag,
139 const unsigned int& tagDataLength,
140 unsigned int& numBytesParsed)
const;
141 bool IsValidSize(
const std::string& tag,
const std::string& type)
const;
142 void SetErrorString(
const std::string& where,
const std::string& what)
const;
143 bool SkipToNextTag(
const char storageType,
145 unsigned int& numBytesParsed)
const;
150 struct BamAlignmentSupportData {
154 std::string AllCharData;
155 uint32_t BlockLength;
156 uint32_t NumCigarOperations;
157 uint32_t QueryNameLength;
158 uint32_t QuerySequenceLength;
163 BamAlignmentSupportData(
void)
165 , NumCigarOperations(0)
167 , QuerySequenceLength(0)
171 BamAlignmentSupportData SupportData;
172 friend class Internal::BamReaderPrivate;
173 friend class Internal::BamWriterPrivate;
175 mutable std::string ErrorString;
196 if ( SupportData.HasCoreOnly )
200 if ( !IsValidSize(tag, type) ) {
206 if ( !TagTypeHelper<T>::CanConvertTo(type.at(0)) ) {
212 char* pTagData = (
char*)TagData.data();
213 const unsigned int tagDataLength = TagData.size();
214 unsigned int numBytesParsed = 0;
218 if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
224 union { T value;
char valueBuffer[
sizeof(T)]; } un;
228 const std::string newTag = tag + type;
229 const size_t newTagDataLength = tagDataLength + newTag.size() +
sizeof(T);
230 RaiiBuffer originalTagData(newTagDataLength);
231 memcpy(originalTagData.Buffer, TagData.c_str(), tagDataLength + 1);
234 strcat(originalTagData.Buffer + tagDataLength, newTag.data());
235 memcpy(originalTagData.Buffer + tagDataLength + newTag.size(), un.valueBuffer,
sizeof(T));
238 const char* newTagData = (
const char*)originalTagData.Buffer;
239 TagData.assign(newTagData, newTagDataLength);
244 inline bool BamAlignment::AddTag<std::string>(
const std::string& tag,
245 const std::string& type,
246 const std::string& value)
249 if ( SupportData.HasCoreOnly )
253 if ( !IsValidSize(tag, type) ) {
259 if ( !TagTypeHelper<std::string>::CanConvertTo(type.at(0)) ) {
265 char* pTagData = (
char*)TagData.data();
266 const unsigned int tagDataLength = TagData.size();
267 unsigned int numBytesParsed = 0;
271 if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
277 const std::string newTag = tag + type + value;
278 const size_t newTagDataLength = tagDataLength + newTag.size() + 1;
279 RaiiBuffer originalTagData(newTagDataLength);
280 memcpy(originalTagData.Buffer, TagData.c_str(), tagDataLength + 1);
283 strcat(originalTagData.Buffer + tagDataLength, newTag.data());
286 const char* newTagData = (
const char*)originalTagData.Buffer;
287 TagData.assign(newTagData, newTagDataLength);
305 if ( SupportData.HasCoreOnly )
313 char* pTagData = (
char*)TagData.data();
314 const unsigned int tagDataLength = TagData.size();
315 unsigned int numBytesParsed = 0;
319 if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
328 newTagBase[3] = TagTypeHelper<T>::TypeCode();
331 const int32_t numElements = values.size();
332 memcpy(newTagBase + 4, &numElements,
sizeof(int32_t));
335 const size_t newTagDataLength = tagDataLength +
337 numElements*
sizeof(T);
338 RaiiBuffer originalTagData(newTagDataLength);
339 memcpy(originalTagData.Buffer, TagData.c_str(), tagDataLength+1);
342 strcat(originalTagData.Buffer + tagDataLength, (
const char*)newTagBase);
346 for (
int i = 0 ; i < numElements; ++i ) {
347 const T& value = values.at(i);
348 memcpy(originalTagData.Buffer + elementsBeginOffset + i*
sizeof(T), &value,
sizeof(T));
352 const char* newTagData = (
const char*)originalTagData.Buffer;
353 TagData.assign(newTagData, newTagDataLength);
375 if ( SupportData.HasCoreOnly )
381 return AddTag(tag, type, value);
399 if ( SupportData.HasCoreOnly )
405 return AddTag(tag, values);
420 if ( SupportData.HasCoreOnly ) {
426 if ( TagData.empty() ) {
432 char* pTagData = (
char*)TagData.data();
433 const unsigned int tagDataLength = TagData.size();
434 unsigned int numBytesParsed = 0;
437 if ( !FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
443 const char type = *(pTagData - 1);
444 if ( !TagTypeHelper<T>::CanConvertFrom(type) ) {
450 int destinationLength = 0;
457 destinationLength = 1;
463 destinationLength = 2;
470 destinationLength = 4;
477 SetErrorString(
"BamAlignment::GetTag",
478 "cannot store variable length tag data into a numeric destination");
483 const std::string message = std::string(
"invalid tag type: ") + type;
484 SetErrorString(
"BamAlignment::GetTag", message);
490 memcpy(&destination, pTagData, destinationLength);
497 inline bool BamAlignment::GetTag<std::string>(
const std::string& tag,
498 std::string& destination)
const 501 if ( SupportData.HasCoreOnly ) {
507 if ( TagData.empty() ) {
513 char* pTagData = (
char*)TagData.data();
514 const unsigned int tagDataLength = TagData.size();
515 unsigned int numBytesParsed = 0;
518 if ( !FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
524 const unsigned int dataLength = strlen(pTagData);
526 destination.resize(dataLength);
527 memcpy( (
char*)destination.data(), pTagData, dataLength );
544 if ( SupportData.HasCoreOnly ) {
550 if ( TagData.empty() ) {
556 char* pTagData = (
char*)TagData.data();
557 const unsigned int tagDataLength = TagData.size();
558 unsigned int numBytesParsed = 0;
561 if ( !FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
567 const char tagType = *(pTagData - 1);
569 SetErrorString(
"BamAlignment::GetTag",
"cannot store a non-array tag in array destination");
574 const char elementType = *pTagData;
575 if ( !TagTypeHelper<T>::CanConvertFrom(elementType) ) {
582 int elementLength = 0;
583 switch ( elementType ) {
587 elementLength =
sizeof(uint8_t);
592 elementLength =
sizeof(uint16_t);
598 elementLength =
sizeof(uint32_t);
605 SetErrorString(
"BamAlignment::GetTag",
606 "invalid array data, variable-length elements are not allowed");
611 const std::string message = std::string(
"invalid array element type: ") + elementType;
612 SetErrorString(
"BamAlignment::GetTag", message);
618 memcpy(&numElements, pTagData,
sizeof(int32_t));
621 destination.reserve(numElements);
625 for (
int i = 0 ; i < numElements; ++i ) {
626 memcpy(&value, pTagData,
sizeof(T));
627 pTagData +=
sizeof(T);
628 destination.push_back(value);
639 #endif // BAMALIGNMENT_H
#define API_EXPORT
Definition: api_global.h:18