BamTools  2.4.0
BamAux.h
Go to the documentation of this file.
1 // ***************************************************************************
2 // BamAux.h (c) 2009 Derek Barnett, Michael Str�mberg
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 25 October 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides data structures & utility methods that are used throughout the API.
8 // ***************************************************************************
9 
10 #ifndef BAMAUX_H
11 #define BAMAUX_H
12 
13 #include "api/api_global.h"
14 #include <cstring>
15 #include <fstream>
16 #include <iostream>
17 #include <string>
18 #include <vector>
19 
31 namespace BamTools {
32 
33 // ----------------------------------------------------------------
34 // CigarOp
35 
42 
43  char Type;
44  uint32_t Length;
45 
47  CigarOp(const char type = '\0',
48  const uint32_t& length = 0)
49  : Type(type)
50  , Length(length)
51  { }
52 };
53 
54 // ----------------------------------------------------------------
55 // RefData
56 
61 
62  std::string RefName;
63  int32_t RefLength;
64 
66  RefData(const std::string& name = "",
67  const int32_t& length = 0)
68  : RefName(name)
69  , RefLength(length)
70  { }
71 };
72 
74 typedef std::vector<RefData> RefVector;
75 
76 // ----------------------------------------------------------------
77 // BamRegion
78 
89 
90  int LeftRefID;
92  int RightRefID;
94 
96  BamRegion(const int& leftID = -1,
97  const int& leftPos = -1,
98  const int& rightID = -1,
99  const int& rightPos = -1)
100  : LeftRefID(leftID)
101  , LeftPosition(leftPos)
102  , RightRefID(rightID)
103  , RightPosition(rightPos)
104  { }
105 
107  BamRegion(const BamRegion& other)
108  : LeftRefID(other.LeftRefID)
109  , LeftPosition(other.LeftPosition)
110  , RightRefID(other.RightRefID)
111  , RightPosition(other.RightPosition)
112  { }
113 
115  void clear(void) {
116  LeftRefID = -1; LeftPosition = -1;
117  RightRefID = -1; RightPosition = -1;
118  }
119 
121  bool isLeftBoundSpecified(void) const {
122  return ( LeftRefID >= 0 && LeftPosition >= 0 );
123  }
124 
126  bool isNull(void) const {
127  return ( !isLeftBoundSpecified() && !isRightBoundSpecified() );
128  }
129 
131  bool isRightBoundSpecified(void) const {
132  return ( RightRefID >= 0 && RightPosition >= 1 );
133  }
134 };
135 
137  std::string TagName;
138  std::string TagValue;
139 };
140 
141 
142 // ----------------------------------------------------------------
143 // General utility methods
144 
148 API_EXPORT inline bool FileExists(const std::string& filename) {
149  std::ifstream f(filename.c_str(), std::ifstream::in);
150  return !f.fail();
151 }
152 
156 API_EXPORT inline void SwapEndian_16(int16_t& x) {
157  x = ((x >> 8) | (x << 8));
158 }
159 
163 API_EXPORT inline void SwapEndian_16(uint16_t& x) {
164  x = ((x >> 8) | (x << 8));
165 }
166 
170 API_EXPORT inline void SwapEndian_32(int32_t& x) {
171  x = ( (x >> 24) |
172  ((x << 8) & 0x00FF0000) |
173  ((x >> 8) & 0x0000FF00) |
174  (x << 24)
175  );
176 }
177 
181 API_EXPORT inline void SwapEndian_32(uint32_t& x) {
182  x = ( (x >> 24) |
183  ((x << 8) & 0x00FF0000) |
184  ((x >> 8) & 0x0000FF00) |
185  (x << 24)
186  );
187 }
188 
192 API_EXPORT inline void SwapEndian_64(int64_t& x) {
193  x = ( (x >> 56) |
194  ((x << 40) & 0x00FF000000000000ll) |
195  ((x << 24) & 0x0000FF0000000000ll) |
196  ((x << 8) & 0x000000FF00000000ll) |
197  ((x >> 8) & 0x00000000FF000000ll) |
198  ((x >> 24) & 0x0000000000FF0000ll) |
199  ((x >> 40) & 0x000000000000FF00ll) |
200  (x << 56)
201  );
202 }
203 
207 API_EXPORT inline void SwapEndian_64(uint64_t& x) {
208  x = ( (x >> 56) |
209  ((x << 40) & 0x00FF000000000000ll) |
210  ((x << 24) & 0x0000FF0000000000ll) |
211  ((x << 8) & 0x000000FF00000000ll) |
212  ((x >> 8) & 0x00000000FF000000ll) |
213  ((x >> 24) & 0x0000000000FF0000ll) |
214  ((x >> 40) & 0x000000000000FF00ll) |
215  (x << 56)
216  );
217 }
218 
222 API_EXPORT inline void SwapEndian_16p(char* data) {
223  uint16_t& value = (uint16_t&)*data;
224  SwapEndian_16(value);
225 }
226 
230 API_EXPORT inline void SwapEndian_32p(char* data) {
231  uint32_t& value = (uint32_t&)*data;
232  SwapEndian_32(value);
233 }
234 
238 API_EXPORT inline void SwapEndian_64p(char* data) {
239  uint64_t& value = (uint64_t&)*data;
240  SwapEndian_64(value);
241 }
242 
247 API_EXPORT inline bool SystemIsBigEndian(void) {
248  const uint16_t one = 0x0001;
249  return ((*(char*) &one) == 0 );
250 }
251 
258 API_EXPORT inline void PackUnsignedInt(char* buffer, unsigned int value) {
259  buffer[0] = (char)value;
260  buffer[1] = (char)(value >> 8);
261  buffer[2] = (char)(value >> 16);
262  buffer[3] = (char)(value >> 24);
263 }
264 
271 API_EXPORT inline void PackUnsignedShort(char* buffer, unsigned short value) {
272  buffer[0] = (char)value;
273  buffer[1] = (char)(value >> 8);
274 }
275 
282 API_EXPORT inline double UnpackDouble(const char* buffer) {
283  union { double value; unsigned char valueBuffer[sizeof(double)]; } un;
284  un.value = 0;
285  un.valueBuffer[0] = buffer[0];
286  un.valueBuffer[1] = buffer[1];
287  un.valueBuffer[2] = buffer[2];
288  un.valueBuffer[3] = buffer[3];
289  un.valueBuffer[4] = buffer[4];
290  un.valueBuffer[5] = buffer[5];
291  un.valueBuffer[6] = buffer[6];
292  un.valueBuffer[7] = buffer[7];
293  return un.value;
294 }
295 
304 API_EXPORT inline double UnpackDouble(char* buffer) {
305  return UnpackDouble( (const char*)buffer );
306 }
307 
314 API_EXPORT inline float UnpackFloat(const char* buffer) {
315  union { float value; unsigned char valueBuffer[sizeof(float)]; } un;
316  un.value = 0;
317  un.valueBuffer[0] = buffer[0];
318  un.valueBuffer[1] = buffer[1];
319  un.valueBuffer[2] = buffer[2];
320  un.valueBuffer[3] = buffer[3];
321  return un.value;
322 }
323 
332 API_EXPORT inline float UnpackFloat(char* buffer) {
333  return UnpackFloat( (const char*)buffer );
334 }
335 
342 API_EXPORT inline signed int UnpackSignedInt(const char* buffer) {
343  union { signed int value; unsigned char valueBuffer[sizeof(signed int)]; } un;
344  un.value = 0;
345  un.valueBuffer[0] = buffer[0];
346  un.valueBuffer[1] = buffer[1];
347  un.valueBuffer[2] = buffer[2];
348  un.valueBuffer[3] = buffer[3];
349  return un.value;
350 }
351 
360 API_EXPORT inline signed int UnpackSignedInt(char* buffer) {
361  return UnpackSignedInt( (const char*) buffer );
362 }
363 
370 API_EXPORT inline signed short UnpackSignedShort(const char* buffer) {
371  union { signed short value; unsigned char valueBuffer[sizeof(signed short)]; } un;
372  un.value = 0;
373  un.valueBuffer[0] = buffer[0];
374  un.valueBuffer[1] = buffer[1];
375  return un.value;
376 }
377 
386 API_EXPORT inline signed short UnpackSignedShort(char* buffer) {
387  return UnpackSignedShort( (const char*)buffer );
388 }
389 
396 API_EXPORT inline unsigned int UnpackUnsignedInt(const char* buffer) {
397  union { unsigned int value; unsigned char valueBuffer[sizeof(unsigned int)]; } un;
398  un.value = 0;
399  un.valueBuffer[0] = buffer[0];
400  un.valueBuffer[1] = buffer[1];
401  un.valueBuffer[2] = buffer[2];
402  un.valueBuffer[3] = buffer[3];
403  return un.value;
404 }
405 
414 API_EXPORT inline unsigned int UnpackUnsignedInt(char* buffer) {
415  return UnpackUnsignedInt( (const char*)buffer );
416 }
417 
424 API_EXPORT inline unsigned short UnpackUnsignedShort(const char* buffer) {
425  union { unsigned short value; unsigned char valueBuffer[sizeof(unsigned short)]; } un;
426  un.value = 0;
427 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
428  un.valueBuffer[0] = buffer[0];
429  un.valueBuffer[1] = buffer[1];
430 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
431  un.valueBuffer[0] = buffer[1];
432  un.valueBuffer[1] = buffer[0];
433 #else
434  #error "Unsupported hardware"
435 #endif
436  return un.value;
437 }
438 
447 API_EXPORT inline unsigned short UnpackUnsignedShort(char* buffer) {
448  return UnpackUnsignedShort( (const char*)buffer );
449 }
450 
451 // ----------------------------------------------------------------
452 // 'internal' helper structs
453 
457 struct RaiiBuffer {
458 
459  // data members
460  char* Buffer;
461  const size_t NumBytes;
462 
463  // ctor & dtor
464  RaiiBuffer(const size_t n)
465  : Buffer( new char[n]() )
466  , NumBytes(n)
467  { }
468 
469  ~RaiiBuffer(void) {
470  delete[] Buffer;
471  }
472 
473  // add'l methods
474  void Clear(void) {
475  memset(Buffer, 0, NumBytes);
476  }
477 };
478 
479 } // namespace BamTools
480 
481 #endif // BAMAUX_H
int RightRefID
reference ID for region&#39;s right boundary
Definition: BamAux.h:92
API_EXPORT signed short UnpackSignedShort(const char *buffer)
reads a signed short integer value from byte buffer
Definition: BamAux.h:370
API_EXPORT double UnpackDouble(const char *buffer)
reads a double value from byte buffer
Definition: BamAux.h:282
bool isRightBoundSpecified(void) const
Returns true if region has a right boundary.
Definition: BamAux.h:131
API_EXPORT void SwapEndian_32p(char *data)
swaps endianness of the next 4 bytes in a buffer, in place
Definition: BamAux.h:230
API_EXPORT unsigned int UnpackUnsignedInt(const char *buffer)
reads an unsigned integer value from byte buffer
Definition: BamAux.h:396
Represents a sequential genomic region.
Definition: BamAux.h:88
API_EXPORT float UnpackFloat(const char *buffer)
reads a float value from byte buffer
Definition: BamAux.h:314
int LeftPosition
position for region&#39;s left boundary
Definition: BamAux.h:91
API_EXPORT bool FileExists(const std::string &filename)
returns true if the file exists
Definition: BamAux.h:148
CigarOp(const char type= '\0', const uint32_t &length=0)
constructor
Definition: BamAux.h:47
API_EXPORT void SwapEndian_32(int32_t &x)
swaps endianness of signed 32-bit integer, in place
Definition: BamAux.h:170
API_EXPORT signed int UnpackSignedInt(const char *buffer)
reads a signed integer value from byte buffer
Definition: BamAux.h:342
#define API_EXPORT
Definition: api_global.h:18
API_EXPORT void PackUnsignedShort(char *buffer, unsigned short value)
stores unsigned short integer value in a byte buffer
Definition: BamAux.h:271
int LeftRefID
reference ID for region&#39;s left boundary
Definition: BamAux.h:90
API_EXPORT void SwapEndian_16p(char *data)
swaps endianness of the next 2 bytes in a buffer, in place
Definition: BamAux.h:222
BamRegion(const int &leftID=-1, const int &leftPos=-1, const int &rightID=-1, const int &rightPos=-1)
constructor
Definition: BamAux.h:96
char Type
CIGAR operation type (MIDNSHPX=)
Definition: BamAux.h:43
std::string RefName
name of reference sequence
Definition: BamAux.h:62
Represents a CIGAR alignment operation.
Definition: BamAux.h:41
std::string TagValue
Definition: BamAux.h:138
API_EXPORT void SwapEndian_64(int64_t &x)
swaps endianness of signed 64-bit integer, in place
Definition: BamAux.h:192
RefData(const std::string &name="", const int32_t &length=0)
constructor
Definition: BamAux.h:66
bool isNull(void) const
Returns true if region boundaries are not defined.
Definition: BamAux.h:126
BamRegion(const BamRegion &other)
copy constructor
Definition: BamAux.h:107
API_EXPORT void SwapEndian_64p(char *data)
swaps endianness of the next 8 bytes in a buffer, in place
Definition: BamAux.h:238
int RightPosition
position for region&#39;s right boundary
Definition: BamAux.h:93
Represents a reference sequence entry.
Definition: BamAux.h:60
int32_t RefLength
length of reference sequence
Definition: BamAux.h:63
uint32_t Length
CIGAR operation length (number of bases)
Definition: BamAux.h:44
void clear(void)
Clears region boundaries.
Definition: BamAux.h:115
API_EXPORT void PackUnsignedInt(char *buffer, unsigned int value)
stores unsigned integer value in a byte buffer
Definition: BamAux.h:258
API_EXPORT bool SystemIsBigEndian(void)
checks host architecture&#39;s byte order
Definition: BamAux.h:247
Contains all BamTools classes & methods.
Definition: Sort.h:24
API_EXPORT void SwapEndian_16(int16_t &x)
swaps endianness of signed 16-bit integer, in place
Definition: BamAux.h:156
std::string TagName
Definition: BamAux.h:137
std::vector< RefData > RefVector
convenience typedef for vector of RefData entries
Definition: BamAux.h:74
API_EXPORT unsigned short UnpackUnsignedShort(const char *buffer)
reads an unsigned short integer value from byte buffer
Definition: BamAux.h:424
bool isLeftBoundSpecified(void) const
Returns true if region has a left boundary.
Definition: BamAux.h:121
Definition: BamAux.h:136