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 {
153 std::string AllCharData;
154 uint32_t BlockLength;
155 uint32_t NumCigarOperations;
156 uint32_t QueryNameLength;
157 uint32_t QuerySequenceLength;
161 BamAlignmentSupportData(
void)
163 , NumCigarOperations(0)
165 , QuerySequenceLength(0)
169 BamAlignmentSupportData SupportData;
170 friend class Internal::BamReaderPrivate;
171 friend class Internal::BamWriterPrivate;
173 mutable std::string ErrorString;
195 if ( SupportData.HasCoreOnly )
199 if ( !IsValidSize(tag, type) ) {
205 if ( !TagTypeHelper<T>::CanConvertTo(type.at(0)) ) {
211 char* pTagData = (
char*)
TagData.data();
212 const unsigned int tagDataLength =
TagData.size();
213 unsigned int numBytesParsed = 0;
217 if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
223 union { T value;
char valueBuffer[
sizeof(T)]; } un;
227 const std::string newTag = tag + type;
228 const size_t newTagDataLength = tagDataLength + newTag.size() +
sizeof(T);
229 RaiiBuffer originalTagData(newTagDataLength);
230 memcpy(originalTagData.Buffer,
TagData.c_str(), tagDataLength + 1);
233 strcat(originalTagData.Buffer + tagDataLength, newTag.data());
234 memcpy(originalTagData.Buffer + tagDataLength + newTag.size(), un.valueBuffer,
sizeof(T));
237 const char* newTagData = (
const char*)originalTagData.Buffer;
238 TagData.assign(newTagData, newTagDataLength);
243 inline bool BamAlignment::AddTag<std::string>(
const std::string& tag,
244 const std::string& type,
245 const std::string& value)
248 if ( SupportData.HasCoreOnly )
252 if ( !IsValidSize(tag, type) ) {
258 if ( !TagTypeHelper<std::string>::CanConvertTo(type.at(0)) ) {
264 char* pTagData = (
char*)TagData.data();
265 const unsigned int tagDataLength = TagData.size();
266 unsigned int numBytesParsed = 0;
270 if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
276 const std::string newTag = tag + type + value;
277 const size_t newTagDataLength = tagDataLength + newTag.size() + 1;
278 RaiiBuffer originalTagData(newTagDataLength);
279 memcpy(originalTagData.Buffer, TagData.c_str(), tagDataLength + 1);
282 strcat(originalTagData.Buffer + tagDataLength, newTag.data());
285 const char* newTagData = (
const char*)originalTagData.Buffer;
286 TagData.assign(newTagData, newTagDataLength);
304 if ( SupportData.HasCoreOnly )
312 char* pTagData = (
char*)
TagData.data();
313 const unsigned int tagDataLength =
TagData.size();
314 unsigned int numBytesParsed = 0;
318 if ( FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
327 newTagBase[3] = TagTypeHelper<T>::TypeCode();
330 const int32_t numElements = values.size();
331 memcpy(newTagBase + 4, &numElements,
sizeof(int32_t));
334 const size_t newTagDataLength = tagDataLength +
336 numElements*
sizeof(T);
337 RaiiBuffer originalTagData(newTagDataLength);
338 memcpy(originalTagData.Buffer,
TagData.c_str(), tagDataLength+1);
341 strcat(originalTagData.Buffer + tagDataLength, (
const char*)newTagBase);
345 for (
int i = 0 ; i < numElements; ++i ) {
346 const T& value = values.at(i);
347 memcpy(originalTagData.Buffer + elementsBeginOffset + i*
sizeof(T), &value,
sizeof(T));
351 const char* newTagData = (
const char*)originalTagData.Buffer;
352 TagData.assign(newTagData, newTagDataLength);
374 if ( SupportData.HasCoreOnly )
380 return AddTag(tag, type, value);
398 if ( SupportData.HasCoreOnly )
404 return AddTag(tag, values);
419 if ( SupportData.HasCoreOnly ) {
431 char* pTagData = (
char*)
TagData.data();
432 const unsigned int tagDataLength =
TagData.size();
433 unsigned int numBytesParsed = 0;
436 if ( !FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
442 const char type = *(pTagData - 1);
443 if ( !TagTypeHelper<T>::CanConvertFrom(type) ) {
449 int destinationLength = 0;
456 destinationLength = 1;
462 destinationLength = 2;
469 destinationLength = 4;
476 SetErrorString(
"BamAlignment::GetTag",
477 "cannot store variable length tag data into a numeric destination");
482 const std::string message = std::string(
"invalid tag type: ") + type;
483 SetErrorString(
"BamAlignment::GetTag", message);
489 memcpy(&destination, pTagData, destinationLength);
496 inline bool BamAlignment::GetTag<std::string>(
const std::string& tag,
497 std::string& destination)
const
500 if ( SupportData.HasCoreOnly ) {
506 if ( TagData.empty() ) {
512 char* pTagData = (
char*)TagData.data();
513 const unsigned int tagDataLength = TagData.size();
514 unsigned int numBytesParsed = 0;
517 if ( !FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
523 const unsigned int dataLength = strlen(pTagData);
525 destination.resize(dataLength);
526 memcpy( (
char*)destination.data(), pTagData, dataLength );
543 if ( SupportData.HasCoreOnly ) {
555 char* pTagData = (
char*)
TagData.data();
556 const unsigned int tagDataLength =
TagData.size();
557 unsigned int numBytesParsed = 0;
560 if ( !FindTag(tag, pTagData, tagDataLength, numBytesParsed) ) {
566 const char tagType = *(pTagData - 1);
568 SetErrorString(
"BamAlignment::GetTag",
"cannot store a non-array tag in array destination");
573 const char elementType = *pTagData;
574 if ( !TagTypeHelper<T>::CanConvertFrom(elementType) ) {
581 int elementLength = 0;
582 switch ( elementType ) {
586 elementLength =
sizeof(uint8_t);
591 elementLength =
sizeof(uint16_t);
597 elementLength =
sizeof(uint32_t);
604 SetErrorString(
"BamAlignment::GetTag",
605 "invalid array data, variable-length elements are not allowed");
610 const std::string message = std::string(
"invalid array element type: ") + elementType;
611 SetErrorString(
"BamAlignment::GetTag", message);
617 memcpy(&numElements, pTagData,
sizeof(int32_t));
620 destination.reserve(numElements);
624 for (
int i = 0 ; i < numElements; ++i ) {
625 memcpy(&value, pTagData,
sizeof(T));
626 pTagData +=
sizeof(T);
627 destination.push_back(value);
638 #endif // BAMALIGNMENT_H
#define API_EXPORT
Definition: api_global.h:18