BamAux.h

Go to the documentation of this file.
00001 // ***************************************************************************
00002 // BamAux.h (c) 2009 Derek Barnett, Michael Str�mberg
00003 // Marth Lab, Department of Biology, Boston College
00004 // ---------------------------------------------------------------------------
00005 // Last modified: 25 October 2011 (DB)
00006 // ---------------------------------------------------------------------------
00007 // Provides data structures & utility methods that are used throughout the API.
00008 // ***************************************************************************
00009 
00010 #ifndef BAMAUX_H
00011 #define BAMAUX_H
00012 
00013 #include "api/api_global.h"
00014 #include <cstring>
00015 #include <fstream> 
00016 #include <iostream>
00017 #include <string>
00018 #include <vector>
00019 
00031 namespace BamTools {
00032 
00033 // ----------------------------------------------------------------
00034 // CigarOp
00035 
00041 struct API_EXPORT CigarOp {
00042   
00043     char     Type;   
00044     uint32_t Length; 
00045     
00047     CigarOp(const char type = '\0', 
00048             const uint32_t& length = 0)
00049         : Type(type)
00050         , Length(length) 
00051     { }
00052 };
00053 
00054 // ----------------------------------------------------------------
00055 // RefData
00056 
00060 struct API_EXPORT RefData {
00061    
00062     std::string RefName;    
00063     int32_t     RefLength;  
00064     
00066     RefData(const std::string& name = "",
00067             const int32_t& length = 0)
00068         : RefName(name)
00069         , RefLength(length)
00070     { }
00071 };
00072 
00074 typedef std::vector<RefData> RefVector;
00075 
00076 // ----------------------------------------------------------------
00077 // BamRegion
00078 
00088 struct API_EXPORT BamRegion {
00089   
00090     int LeftRefID;      
00091     int LeftPosition;   
00092     int RightRefID;     
00093     int RightPosition;  
00094     
00096     BamRegion(const int& leftID   = -1, 
00097               const int& leftPos  = -1,
00098               const int& rightID  = -1,
00099               const int& rightPos = -1)
00100         : LeftRefID(leftID)
00101         , LeftPosition(leftPos)
00102         , RightRefID(rightID)
00103         , RightPosition(rightPos)
00104     { }
00105     
00107     BamRegion(const BamRegion& other)
00108         : LeftRefID(other.LeftRefID)
00109         , LeftPosition(other.LeftPosition)
00110         , RightRefID(other.RightRefID)
00111         , RightPosition(other.RightPosition)
00112     { }
00113     
00115     void clear(void) {
00116         LeftRefID  = -1; LeftPosition  = -1;
00117         RightRefID = -1; RightPosition = -1;
00118     }
00119 
00121     bool isLeftBoundSpecified(void) const {
00122         return ( LeftRefID >= 0 && LeftPosition >= 0 );
00123     }
00124 
00126     bool isNull(void) const {
00127         return ( !isLeftBoundSpecified() && !isRightBoundSpecified() );
00128     }
00129 
00131     bool isRightBoundSpecified(void) const {
00132         return ( RightRefID >= 0 && RightPosition >= 1 );
00133     }
00134 };
00135 
00136 // ----------------------------------------------------------------
00137 // General utility methods
00138 
00142 API_EXPORT inline bool FileExists(const std::string& filename) {
00143     std::ifstream f(filename.c_str(), std::ifstream::in);
00144     return !f.fail();
00145 }
00146 
00150 API_EXPORT inline void SwapEndian_16(int16_t& x) {
00151     x = ((x >> 8) | (x << 8));
00152 }
00153 
00157 API_EXPORT inline void SwapEndian_16(uint16_t& x) {
00158     x = ((x >> 8) | (x << 8));
00159 }
00160 
00164 API_EXPORT inline void SwapEndian_32(int32_t& x) {
00165     x = ( (x >> 24) | 
00166          ((x << 8) & 0x00FF0000) | 
00167          ((x >> 8) & 0x0000FF00) | 
00168           (x << 24)
00169         );
00170 }
00171 
00175 API_EXPORT inline void SwapEndian_32(uint32_t& x) {
00176     x = ( (x >> 24) | 
00177          ((x << 8) & 0x00FF0000) | 
00178          ((x >> 8) & 0x0000FF00) | 
00179           (x << 24)
00180         );
00181 }
00182 
00186 API_EXPORT inline void SwapEndian_64(int64_t& x) {
00187     x = ( (x >> 56) | 
00188          ((x << 40) & 0x00FF000000000000ll) |
00189          ((x << 24) & 0x0000FF0000000000ll) |
00190          ((x << 8)  & 0x000000FF00000000ll) |
00191          ((x >> 8)  & 0x00000000FF000000ll) |
00192          ((x >> 24) & 0x0000000000FF0000ll) |
00193          ((x >> 40) & 0x000000000000FF00ll) |
00194           (x << 56)
00195         );
00196 }
00197 
00201 API_EXPORT inline void SwapEndian_64(uint64_t& x) {
00202     x = ( (x >> 56) | 
00203          ((x << 40) & 0x00FF000000000000ll) |
00204          ((x << 24) & 0x0000FF0000000000ll) |
00205          ((x << 8)  & 0x000000FF00000000ll) |
00206          ((x >> 8)  & 0x00000000FF000000ll) |
00207          ((x >> 24) & 0x0000000000FF0000ll) |
00208          ((x >> 40) & 0x000000000000FF00ll) |
00209           (x << 56)
00210         );
00211 }
00212 
00216 API_EXPORT inline void SwapEndian_16p(char* data) {
00217     uint16_t& value = (uint16_t&)*data; 
00218     SwapEndian_16(value);
00219 }
00220 
00224 API_EXPORT inline void SwapEndian_32p(char* data) {
00225     uint32_t& value = (uint32_t&)*data; 
00226     SwapEndian_32(value);
00227 }
00228 
00232 API_EXPORT inline void SwapEndian_64p(char* data) {
00233     uint64_t& value = (uint64_t&)*data; 
00234     SwapEndian_64(value);
00235 }
00236 
00241 API_EXPORT inline bool SystemIsBigEndian(void) {
00242    const uint16_t one = 0x0001;
00243    return ((*(char*) &one) == 0 );
00244 }
00245 
00252 API_EXPORT inline void PackUnsignedInt(char* buffer, unsigned int value) {
00253     buffer[0] = (char)value;
00254     buffer[1] = (char)(value >> 8);
00255     buffer[2] = (char)(value >> 16);
00256     buffer[3] = (char)(value >> 24);
00257 }
00258 
00265 API_EXPORT inline void PackUnsignedShort(char* buffer, unsigned short value) {
00266     buffer[0] = (char)value;
00267     buffer[1] = (char)(value >> 8);
00268 }
00269 
00276 API_EXPORT inline double UnpackDouble(const char* buffer) {
00277     union { double value; unsigned char valueBuffer[sizeof(double)]; } un;
00278     un.value = 0;
00279     un.valueBuffer[0] = buffer[0];
00280     un.valueBuffer[1] = buffer[1];
00281     un.valueBuffer[2] = buffer[2];
00282     un.valueBuffer[3] = buffer[3];
00283     un.valueBuffer[4] = buffer[4];
00284     un.valueBuffer[5] = buffer[5];
00285     un.valueBuffer[6] = buffer[6];
00286     un.valueBuffer[7] = buffer[7];
00287     return un.value;
00288 }
00289 
00298 API_EXPORT inline double UnpackDouble(char* buffer) {
00299     return UnpackDouble( (const char*)buffer );
00300 }
00301 
00308 API_EXPORT inline float UnpackFloat(const char* buffer) {
00309     union { float value; unsigned char valueBuffer[sizeof(float)]; } un;
00310     un.value = 0;
00311     un.valueBuffer[0] = buffer[0];
00312     un.valueBuffer[1] = buffer[1];
00313     un.valueBuffer[2] = buffer[2];
00314     un.valueBuffer[3] = buffer[3];
00315     return un.value;
00316 }
00317 
00326 API_EXPORT inline float UnpackFloat(char* buffer) {
00327     return UnpackFloat( (const char*)buffer );
00328 }
00329 
00336 API_EXPORT inline signed int UnpackSignedInt(const char* buffer) {
00337     union { signed int value; unsigned char valueBuffer[sizeof(signed int)]; } un;
00338     un.value = 0;
00339     un.valueBuffer[0] = buffer[0];
00340     un.valueBuffer[1] = buffer[1];
00341     un.valueBuffer[2] = buffer[2];
00342     un.valueBuffer[3] = buffer[3];
00343     return un.value;
00344 }
00345 
00354 API_EXPORT inline signed int UnpackSignedInt(char* buffer) {
00355     return UnpackSignedInt( (const char*) buffer );
00356 }
00357 
00364 API_EXPORT inline signed short UnpackSignedShort(const char* buffer) {
00365     union { signed short value; unsigned char valueBuffer[sizeof(signed short)]; } un;
00366     un.value = 0;
00367     un.valueBuffer[0] = buffer[0];
00368     un.valueBuffer[1] = buffer[1];
00369     return un.value;
00370 }
00371 
00380 API_EXPORT inline signed short UnpackSignedShort(char* buffer) {
00381     return UnpackSignedShort( (const char*)buffer );
00382 }
00383 
00390 API_EXPORT inline unsigned int UnpackUnsignedInt(const char* buffer) {
00391     union { unsigned int value; unsigned char valueBuffer[sizeof(unsigned int)]; } un;
00392     un.value = 0;
00393     un.valueBuffer[0] = buffer[0];
00394     un.valueBuffer[1] = buffer[1];
00395     un.valueBuffer[2] = buffer[2];
00396     un.valueBuffer[3] = buffer[3];
00397     return un.value;
00398 }
00399 
00408 API_EXPORT inline unsigned int UnpackUnsignedInt(char* buffer) {
00409     return UnpackUnsignedInt( (const char*)buffer );
00410 }
00411 
00418 API_EXPORT inline unsigned short UnpackUnsignedShort(const char* buffer) {
00419     union { unsigned short value; unsigned char valueBuffer[sizeof(unsigned short)]; } un;
00420     un.value = 0;
00421     un.valueBuffer[0] = buffer[0];
00422     un.valueBuffer[1] = buffer[1];
00423     return un.value;
00424 }
00425 
00434 API_EXPORT inline unsigned short UnpackUnsignedShort(char* buffer) {
00435     return UnpackUnsignedShort( (const char*)buffer );
00436 }
00437 
00438 // ----------------------------------------------------------------
00439 // 'internal' helper structs
00440 
00444 struct RaiiBuffer {
00445 
00446     // data members
00447     char* Buffer;
00448     const size_t NumBytes;
00449 
00450     // ctor & dtor
00451     RaiiBuffer(const size_t n)
00452         : Buffer( new char[n]() )
00453         , NumBytes(n)
00454     { }
00455 
00456     ~RaiiBuffer(void) {
00457         delete[] Buffer;
00458     }
00459 
00460     // add'l methods
00461     void Clear(void) {
00462         memset(Buffer, 0, NumBytes);
00463     }
00464 };
00465 
00466 } // namespace BamTools
00467 
00468 #endif // BAMAUX_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Aug 29 17:43:46 2012 for BamTools by  doxygen 1.6.3