BamTools  2.5.1
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 <cstddef>
14 #include <cstring>
15 #include <fstream>
16 #include <iostream>
17 #include <string>
18 #include <vector>
19 #include "api/api_global.h"
20 
32 namespace BamTools {
33 
34 // ----------------------------------------------------------------
35 // CigarOp
36 
43 {
44 
45  char Type;
46  uint32_t Length;
47 
49  CigarOp(const char type = '\0', const uint32_t& length = 0)
50  : Type(type)
51  , Length(length)
52  {}
53 };
54 
55 // ----------------------------------------------------------------
56 // RefData
57 
62 {
63 
64  std::string RefName;
65  int32_t RefLength;
66 
68  RefData(const std::string& name = std::string(), const int32_t& length = 0)
69  : RefName(name)
70  , RefLength(length)
71  {}
72 };
73 
75 typedef std::vector<RefData> RefVector;
76 
77 // ----------------------------------------------------------------
78 // BamRegion
79 
90 {
91 
92  int LeftRefID;
94  int RightRefID;
96 
98  BamRegion(const int& leftID = -1, const int& leftPos = -1, 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()
116  {
117  LeftRefID = -1;
118  LeftPosition = -1;
119  RightRefID = -1;
120  RightPosition = -1;
121  }
122 
124  bool isLeftBoundSpecified() const
125  {
126  return (LeftRefID >= 0 && LeftPosition >= 0);
127  }
128 
130  bool isNull() const
131  {
132  return (!isLeftBoundSpecified() && !isRightBoundSpecified());
133  }
134 
137  {
138  return (RightRefID >= 0 && RightPosition >= 1);
139  }
140 };
141 
143 {
144  std::string TagName;
145  std::string TagValue;
146 };
147 
148 // ----------------------------------------------------------------
149 // General utility methods
150 
154 API_EXPORT inline bool FileExists(const std::string& filename)
155 {
156  std::ifstream f(filename.c_str(), std::ifstream::in);
157  return !f.fail();
158 }
159 
163 API_EXPORT inline void SwapEndian_16(int16_t& x)
164 {
165  x = ((x >> 8) | (x << 8));
166 }
167 
171 API_EXPORT inline void SwapEndian_16(uint16_t& x)
172 {
173  x = ((x >> 8) | (x << 8));
174 }
175 
179 API_EXPORT inline void SwapEndian_32(int32_t& x)
180 {
181  x = ((x >> 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x << 24));
182 }
183 
187 API_EXPORT inline void SwapEndian_32(uint32_t& x)
188 {
189  x = ((x >> 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x << 24));
190 }
191 
195 API_EXPORT inline void SwapEndian_64(int64_t& x)
196 {
197  x = ((x >> 56) | ((x << 40) & 0x00FF000000000000ll) | ((x << 24) & 0x0000FF0000000000ll) |
198  ((x << 8) & 0x000000FF00000000ll) | ((x >> 8) & 0x00000000FF000000ll) |
199  ((x >> 24) & 0x0000000000FF0000ll) | ((x >> 40) & 0x000000000000FF00ll) | (x << 56));
200 }
201 
205 API_EXPORT inline void SwapEndian_64(uint64_t& x)
206 {
207  x = ((x >> 56) | ((x << 40) & 0x00FF000000000000ll) | ((x << 24) & 0x0000FF0000000000ll) |
208  ((x << 8) & 0x000000FF00000000ll) | ((x >> 8) & 0x00000000FF000000ll) |
209  ((x >> 24) & 0x0000000000FF0000ll) | ((x >> 40) & 0x000000000000FF00ll) | (x << 56));
210 }
211 
215 API_EXPORT inline void SwapEndian_16p(char* data)
216 {
217  uint16_t& value = (uint16_t&)*data;
218  SwapEndian_16(value);
219 }
220 
224 API_EXPORT inline void SwapEndian_32p(char* data)
225 {
226  uint32_t& value = (uint32_t&)*data;
227  SwapEndian_32(value);
228 }
229 
233 API_EXPORT inline void SwapEndian_64p(char* data)
234 {
235  uint64_t& value = (uint64_t&)*data;
236  SwapEndian_64(value);
237 }
238 
244 {
245  const uint16_t one = 0x0001;
246  return ((*(char*)&one) == 0);
247 }
248 
255 API_EXPORT inline void PackUnsignedInt(char* buffer, unsigned int value)
256 {
257  buffer[0] = (char)value;
258  buffer[1] = (char)(value >> 8);
259  buffer[2] = (char)(value >> 16);
260  buffer[3] = (char)(value >> 24);
261 }
262 
269 API_EXPORT inline void PackUnsignedShort(char* buffer, unsigned short value)
270 {
271  buffer[0] = (char)value;
272  buffer[1] = (char)(value >> 8);
273 }
274 
281 API_EXPORT inline double UnpackDouble(const char* buffer)
282 {
283  union
284  {
285  double value;
286  unsigned char valueBuffer[sizeof(double)];
287  } un;
288  un.value = 0;
289  un.valueBuffer[0] = buffer[0];
290  un.valueBuffer[1] = buffer[1];
291  un.valueBuffer[2] = buffer[2];
292  un.valueBuffer[3] = buffer[3];
293  un.valueBuffer[4] = buffer[4];
294  un.valueBuffer[5] = buffer[5];
295  un.valueBuffer[6] = buffer[6];
296  un.valueBuffer[7] = buffer[7];
297  return un.value;
298 }
299 
308 API_EXPORT inline double UnpackDouble(char* buffer)
309 {
310  return UnpackDouble((const char*)buffer);
311 }
312 
319 API_EXPORT inline float UnpackFloat(const char* buffer)
320 {
321  union
322  {
323  float value;
324  unsigned char valueBuffer[sizeof(float)];
325  } un;
326  un.value = 0;
327  un.valueBuffer[0] = buffer[0];
328  un.valueBuffer[1] = buffer[1];
329  un.valueBuffer[2] = buffer[2];
330  un.valueBuffer[3] = buffer[3];
331  return un.value;
332 }
333 
342 API_EXPORT inline float UnpackFloat(char* buffer)
343 {
344  return UnpackFloat((const char*)buffer);
345 }
346 
353 API_EXPORT inline signed int UnpackSignedInt(const char* buffer)
354 {
355  union
356  {
357  signed int value;
358  unsigned char valueBuffer[sizeof(signed int)];
359  } un;
360  un.value = 0;
361  un.valueBuffer[0] = buffer[0];
362  un.valueBuffer[1] = buffer[1];
363  un.valueBuffer[2] = buffer[2];
364  un.valueBuffer[3] = buffer[3];
365  return un.value;
366 }
367 
376 API_EXPORT inline signed int UnpackSignedInt(char* buffer)
377 {
378  return UnpackSignedInt((const char*)buffer);
379 }
380 
387 API_EXPORT inline signed short UnpackSignedShort(const char* buffer)
388 {
389  union
390  {
391  signed short value;
392  unsigned char valueBuffer[sizeof(signed short)];
393  } un;
394  un.value = 0;
395  un.valueBuffer[0] = buffer[0];
396  un.valueBuffer[1] = buffer[1];
397  return un.value;
398 }
399 
408 API_EXPORT inline signed short UnpackSignedShort(char* buffer)
409 {
410  return UnpackSignedShort((const char*)buffer);
411 }
412 
419 API_EXPORT inline unsigned int UnpackUnsignedInt(const char* buffer)
420 {
421  union
422  {
423  unsigned int value;
424  unsigned char valueBuffer[sizeof(unsigned int)];
425  } un;
426  un.value = 0;
427  un.valueBuffer[0] = buffer[0];
428  un.valueBuffer[1] = buffer[1];
429  un.valueBuffer[2] = buffer[2];
430  un.valueBuffer[3] = buffer[3];
431  return un.value;
432 }
433 
442 API_EXPORT inline unsigned int UnpackUnsignedInt(char* buffer)
443 {
444  return UnpackUnsignedInt((const char*)buffer);
445 }
446 
453 API_EXPORT inline unsigned short UnpackUnsignedShort(const char* buffer)
454 {
455  union
456  {
457  unsigned short value;
458  unsigned char valueBuffer[sizeof(unsigned short)];
459  } un;
460  un.value = 0;
461 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
462  un.valueBuffer[0] = buffer[0];
463  un.valueBuffer[1] = buffer[1];
464 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
465  un.valueBuffer[0] = buffer[1];
466  un.valueBuffer[1] = buffer[0];
467 #else
468 #error "Unsupported hardware"
469 #endif
470  return un.value;
471 }
472 
481 API_EXPORT inline unsigned short UnpackUnsignedShort(char* buffer)
482 {
483  return UnpackUnsignedShort((const char*)buffer);
484 }
485 
486 // ----------------------------------------------------------------
487 // 'internal' helper structs
488 
492 struct RaiiBuffer
493 {
494 
495  // data members
496  char* Buffer;
497  const std::size_t NumBytes;
498 
499  // ctor & dtor
500  RaiiBuffer(const std::size_t n)
501  : Buffer(new char[n]())
502  , NumBytes(n)
503  {}
504 
505  ~RaiiBuffer()
506  {
507  delete[] Buffer;
508  }
509 
510  // add'l methods
511  void Clear()
512  {
513  memset(Buffer, 0, NumBytes);
514  }
515 };
516 
517 } // namespace BamTools
518 
519 #endif // BAMAUX_H
#define API_EXPORT
Definition: api_global.h:18
Contains all BamTools classes & methods.
Definition: Sort.h:24
API_EXPORT void SwapEndian_32p(char *data)
swaps endianness of the next 4 bytes in a buffer, in place
Definition: BamAux.h:224
API_EXPORT signed int UnpackSignedInt(const char *buffer)
reads a signed integer value from byte buffer
Definition: BamAux.h:353
API_EXPORT unsigned int UnpackUnsignedInt(const char *buffer)
reads an unsigned integer value from byte buffer
Definition: BamAux.h:419
API_EXPORT void SwapEndian_16p(char *data)
swaps endianness of the next 2 bytes in a buffer, in place
Definition: BamAux.h:215
API_EXPORT signed short UnpackSignedShort(const char *buffer)
reads a signed short integer value from byte buffer
Definition: BamAux.h:387
API_EXPORT bool SystemIsBigEndian()
checks host architecture's byte order
Definition: BamAux.h:243
API_EXPORT void PackUnsignedInt(char *buffer, unsigned int value)
stores unsigned integer value in a byte buffer
Definition: BamAux.h:255
API_EXPORT bool FileExists(const std::string &filename)
returns true if the file exists
Definition: BamAux.h:154
API_EXPORT void SwapEndian_32(int32_t &x)
swaps endianness of signed 32-bit integer, in place
Definition: BamAux.h:179
API_EXPORT void SwapEndian_64p(char *data)
swaps endianness of the next 8 bytes in a buffer, in place
Definition: BamAux.h:233
API_EXPORT void SwapEndian_64(int64_t &x)
swaps endianness of signed 64-bit integer, in place
Definition: BamAux.h:195
API_EXPORT unsigned short UnpackUnsignedShort(const char *buffer)
reads an unsigned short integer value from byte buffer
Definition: BamAux.h:453
API_EXPORT float UnpackFloat(const char *buffer)
reads a float value from byte buffer
Definition: BamAux.h:319
std::vector< RefData > RefVector
convenience typedef for vector of RefData entries
Definition: BamAux.h:75
API_EXPORT void PackUnsignedShort(char *buffer, unsigned short value)
stores unsigned short integer value in a byte buffer
Definition: BamAux.h:269
API_EXPORT double UnpackDouble(const char *buffer)
reads a double value from byte buffer
Definition: BamAux.h:281
API_EXPORT void SwapEndian_16(int16_t &x)
swaps endianness of signed 16-bit integer, in place
Definition: BamAux.h:163
Represents a sequential genomic region.
Definition: BamAux.h:90
int RightRefID
reference ID for region's right boundary
Definition: BamAux.h:94
int LeftRefID
reference ID for region's left boundary
Definition: BamAux.h:92
BamRegion(const int &leftID=-1, const int &leftPos=-1, const int &rightID=-1, const int &rightPos=-1)
constructor
Definition: BamAux.h:98
int RightPosition
position for region's right boundary
Definition: BamAux.h:95
BamRegion(const BamRegion &other)
copy constructor
Definition: BamAux.h:107
int LeftPosition
position for region's left boundary
Definition: BamAux.h:93
bool isNull() const
Returns true if region boundaries are not defined.
Definition: BamAux.h:130
void clear()
Clears region boundaries.
Definition: BamAux.h:115
bool isLeftBoundSpecified() const
Returns true if region has a left boundary.
Definition: BamAux.h:124
bool isRightBoundSpecified() const
Returns true if region has a right boundary.
Definition: BamAux.h:136
Represents a CIGAR alignment operation.
Definition: BamAux.h:43
CigarOp(const char type='\0', const uint32_t &length=0)
constructor
Definition: BamAux.h:49
uint32_t Length
CIGAR operation length (number of bases)
Definition: BamAux.h:46
char Type
CIGAR operation type (MIDNSHPX=)
Definition: BamAux.h:45
Definition: BamAux.h:143
std::string TagValue
Definition: BamAux.h:145
std::string TagName
Definition: BamAux.h:144
Represents a reference sequence entry.
Definition: BamAux.h:62
std::string RefName
name of reference sequence
Definition: BamAux.h:64
RefData(const std::string &name=std::string(), const int32_t &length=0)
constructor
Definition: BamAux.h:68
int32_t RefLength
length of reference sequence
Definition: BamAux.h:65