BamTools  2.5.2
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 
42 struct API_EXPORT CigarOp
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 
61 struct API_EXPORT RefData
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 
89 struct API_EXPORT BamRegion
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  void clear()
108  {
109  LeftRefID = -1;
110  LeftPosition = -1;
111  RightRefID = -1;
112  RightPosition = -1;
113  }
114 
116  bool isLeftBoundSpecified() const
117  {
118  return (LeftRefID >= 0 && LeftPosition >= 0);
119  }
120 
122  bool isNull() const
123  {
124  return (!isLeftBoundSpecified() && !isRightBoundSpecified());
125  }
126 
129  {
130  return (RightRefID >= 0 && RightPosition >= 1);
131  }
132 };
133 
134 struct API_EXPORT CustomHeaderTag
135 {
136  std::string TagName;
137  std::string TagValue;
138 };
139 
140 // ----------------------------------------------------------------
141 // General utility methods
142 
146 inline bool FileExists(const std::string& filename)
147 {
148  std::ifstream f(filename.c_str(), std::ifstream::in);
149  return !f.fail();
150 }
151 
155 inline void SwapEndian_16(uint16_t& x)
156 {
157  x = ((x & 0xFF00) >> 8) | ((x & 0x00FF) << 8);
158 }
159 
163 inline void SwapEndian_16(int16_t& x)
164 {
165  uint16_t val = x;
166  SwapEndian_16(val);
167  x = val;
168 }
169 
173 inline void SwapEndian_32(uint32_t& x)
174 {
175  x = ((x & 0xFF000000) >> 24) | ((x & 0x00FF0000) >> 8) | ((x & 0x0000FF00) << 8) |
176  ((x & 0x000000FF) << 24);
177 }
178 
182 inline void SwapEndian_32(int32_t& x)
183 {
184  uint32_t val = x;
185  SwapEndian_32(val);
186  x = val;
187 }
188 
192 inline void SwapEndian_64(uint64_t& x)
193 {
194  x = ((x & 0xFF00000000000000ull) >> 56) | ((x & 0x00FF000000000000ull) >> 40) |
195  ((x & 0x0000FF0000000000ull) >> 24) | ((x & 0x000000FF00000000ull) >> 8) |
196  ((x & 0x00000000FF000000ull) << 8) | ((x & 0x0000000000FF0000ull) << 24) |
197  ((x & 0x000000000000FF00ull) << 40) | ((x & 0x00000000000000FFull) << 56);
198 }
199 
203 inline void SwapEndian_64(int64_t& x)
204 {
205  uint64_t val = x;
206  SwapEndian_64(val);
207  x = val;
208 }
209 
213 inline void SwapEndian_16p(char* data)
214 {
215  uint16_t value;
216  std::memcpy(&value, data, sizeof(uint16_t));
217  SwapEndian_16(value);
218  std::memcpy(data, &value, sizeof(uint16_t));
219 }
220 
224 inline void SwapEndian_32p(char* data)
225 {
226  uint32_t value;
227  std::memcpy(&value, data, sizeof(uint32_t));
228  SwapEndian_32(value);
229  std::memcpy(data, &value, sizeof(uint32_t));
230 }
231 
235 inline void SwapEndian_64p(char* data)
236 {
237  uint64_t value;
238  std::memcpy(&value, data, sizeof(uint64_t));
239  SwapEndian_64(value);
240  std::memcpy(data, &value, sizeof(uint64_t));
241 }
242 
247 inline bool SystemIsBigEndian()
248 {
249  const uint16_t one = 0x0001;
250  return ((*(char*)&one) == 0);
251 }
252 
259 inline void PackUnsignedInt(char* buffer, unsigned int value)
260 {
261  buffer[0] = (char)value;
262  buffer[1] = (char)(value >> 8);
263  buffer[2] = (char)(value >> 16);
264  buffer[3] = (char)(value >> 24);
265 }
266 
273 inline void PackUnsignedShort(char* buffer, unsigned short value)
274 {
275  buffer[0] = (char)value;
276  buffer[1] = (char)(value >> 8);
277 }
278 
285 inline double UnpackDouble(const char* buffer)
286 {
287  double result;
288  std::memcpy(&result, buffer, sizeof(double));
289  return result;
290 }
291 
298 inline float UnpackFloat(const char* buffer)
299 {
300  float result;
301  std::memcpy(&result, buffer, sizeof(float));
302  return result;
303 }
304 
311 inline signed int UnpackSignedInt(const char* buffer)
312 {
313  signed int result;
314  std::memcpy(&result, buffer, sizeof(signed int));
315  return result;
316 }
317 
324 inline signed short UnpackSignedShort(const char* buffer)
325 {
326  signed short result;
327  std::memcpy(&result, buffer, sizeof(signed short));
328  return result;
329 }
330 
337 inline unsigned int UnpackUnsignedInt(const char* buffer)
338 {
339  unsigned int result;
340  std::memcpy(&result, buffer, sizeof(unsigned int));
341  return result;
342 }
343 
350 inline unsigned short UnpackUnsignedShort(const char* buffer)
351 {
352  unsigned short result;
353  std::memcpy(&result, buffer, sizeof(unsigned short));
354 
355 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
356  // no further operations required
357 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
358  SwapEndian_16(result);
359 #else
360 #error "Unsupported hardware"
361 #endif
362 
363  return result;
364 }
365 
366 // ----------------------------------------------------------------
367 // 'internal' helper structs
368 
372 struct API_NO_EXPORT RaiiBuffer
373 {
374 
375  // data members
376  char* Buffer;
377  const std::size_t NumBytes;
378 
379  // ctor & dtor
380  RaiiBuffer(const std::size_t n)
381  : Buffer(new char[n]())
382  , NumBytes(n)
383  {}
384 
385  ~RaiiBuffer()
386  {
387  delete[] Buffer;
388  }
389 
390  // add'l methods
391  void Clear()
392  {
393  std::memset(Buffer, 0, NumBytes);
394  }
395 };
396 
397 } // namespace BamTools
398 
399 #endif // BAMAUX_H
Contains all BamTools classes & methods.
Definition: Sort.h:24
void SwapEndian_32p(char *data)
swaps endianness of the next 4 bytes in a buffer, in place
Definition: BamAux.h:224
signed int UnpackSignedInt(const char *buffer)
reads a signed integer value from byte buffer
Definition: BamAux.h:311
unsigned int UnpackUnsignedInt(const char *buffer)
reads an unsigned integer value from byte buffer
Definition: BamAux.h:337
void SwapEndian_16p(char *data)
swaps endianness of the next 2 bytes in a buffer, in place
Definition: BamAux.h:213
void SwapEndian_32(uint32_t &x)
swaps endianness of unsigned 32-bit integer, in place
Definition: BamAux.h:173
signed short UnpackSignedShort(const char *buffer)
reads a signed short integer value from byte buffer
Definition: BamAux.h:324
bool SystemIsBigEndian()
checks host architecture's byte order
Definition: BamAux.h:247
void PackUnsignedInt(char *buffer, unsigned int value)
stores unsigned integer value in a byte buffer
Definition: BamAux.h:259
bool FileExists(const std::string &filename)
returns true if the file exists
Definition: BamAux.h:146
void SwapEndian_64p(char *data)
swaps endianness of the next 8 bytes in a buffer, in place
Definition: BamAux.h:235
unsigned short UnpackUnsignedShort(const char *buffer)
reads an unsigned short integer value from byte buffer
Definition: BamAux.h:350
float UnpackFloat(const char *buffer)
reads a float value from byte buffer
Definition: BamAux.h:298
void SwapEndian_16(uint16_t &x)
swaps endianness of unsigned 16-bit integer, in place
Definition: BamAux.h:155
std::vector< RefData > RefVector
convenience typedef for vector of RefData entries
Definition: BamAux.h:75
void SwapEndian_64(uint64_t &x)
swaps endianness of unsigned 64-bit integer, in place
Definition: BamAux.h:192
void PackUnsignedShort(char *buffer, unsigned short value)
stores unsigned short integer value in a byte buffer
Definition: BamAux.h:273
double UnpackDouble(const char *buffer)
reads a double value from byte buffer
Definition: BamAux.h:285
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
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:122
void clear()
Clears region boundaries.
Definition: BamAux.h:107
bool isLeftBoundSpecified() const
Returns true if region has a left boundary.
Definition: BamAux.h:116
bool isRightBoundSpecified() const
Returns true if region has a right boundary.
Definition: BamAux.h:128
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:135
std::string TagValue
Definition: BamAux.h:137
std::string TagName
Definition: BamAux.h:136
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