// ***************************************************************************\r
-// BGZF.h (c) 2009 Derek Barnett, Michael Strömberg\r
+// BGZF.h (c) 2009 Derek Barnett, Michael Str�mberg\r
// Marth Lab, Department of Biology, Boston College\r
// All rights reserved.\r
// ---------------------------------------------------------------------------\r
-// Last modified: 11 January 2010 (DB)\r
+// Last modified: 9 July 2010 (DB)\r
// ---------------------------------------------------------------------------\r
// BGZF routines were adapted from the bgzf.c code developed at the Broad\r
// Institute.\r
// Platform-specific type definitions\r
#ifndef BAMTOOLS_TYPES\r
#define BAMTOOLS_TYPES\r
- #ifdef _MSC_VER\r
- typedef char int8_t;\r
- typedef unsigned char uint8_t;\r
- typedef short int16_t;\r
- typedef unsigned short uint16_t;\r
- typedef int int32_t;\r
- typedef unsigned int uint32_t;\r
- typedef long long int64_t;\r
- typedef unsigned long long uint64_t;\r
- #else\r
- #include <stdint.h>\r
- #endif\r
+ #ifdef _MSC_VER\r
+ typedef char int8_t;\r
+ typedef unsigned char uint8_t;\r
+ typedef short int16_t;\r
+ typedef unsigned short uint16_t;\r
+ typedef int int32_t;\r
+ typedef unsigned int uint32_t;\r
+ typedef long long int64_t;\r
+ typedef unsigned long long uint64_t;\r
+ #else \r
+ #include <stdint.h>\r
+ #endif\r
#endif // BAMTOOLS_TYPES\r
\r
namespace BamTools {\r
\r
struct BgzfData {\r
\r
+ // ---------------------------------\r
// data members\r
+ \r
unsigned int UncompressedBlockSize;\r
unsigned int CompressedBlockSize;\r
unsigned int BlockLength;\r
char* UncompressedBlock;\r
char* CompressedBlock;\r
\r
+ // ---------------------------------\r
// constructor & destructor\r
+ \r
BgzfData(void);\r
~BgzfData(void);\r
\r
+ // ---------------------------------\r
+ // main interface methods\r
+ \r
// closes BGZF file\r
void Close(void);\r
- // compresses the current block\r
- int DeflateBlock(void);\r
- // flushes the data in the BGZF block\r
- void FlushBlock(void);\r
- // de-compresses the current block\r
- int InflateBlock(const int& blockLength);\r
- // opens the BGZF file for reading (mode is either "rb" for reading, or "wb" for writing\r
- void Open(const std::string& filename, const char* mode);\r
+ // opens the BGZF file (mode is either "rb" for reading, or "wb" for writing)\r
+ bool Open(const std::string& filename, const char* mode);\r
// reads BGZF data into a byte buffer\r
int Read(char* data, const unsigned int dataLength);\r
- // reads BGZF block\r
- int ReadBlock(void);\r
- // seek to position in BAM file\r
+ // seek to position in BGZF file\r
bool Seek(int64_t position);\r
- // get file position in BAM file\r
+ // get file position in BGZF file\r
int64_t Tell(void);\r
// writes the supplied data into the BGZF buffer\r
unsigned int Write(const char* data, const unsigned int dataLen);\r
\r
+ // ---------------------------------\r
+ // internal methods\r
+ \r
+ // compresses the current block\r
+ int DeflateBlock(void);\r
+ // flushes the data in the BGZF block\r
+ void FlushBlock(void);\r
+ // de-compresses the current block\r
+ int InflateBlock(const int& blockLength);\r
+ // reads a BGZF block\r
+ bool ReadBlock(void);\r
+ \r
+ // ---------------------------------\r
+ // static 'utility' methods\r
+ \r
// checks BGZF block header\r
static inline bool CheckBlockHeader(char* header);\r
// packs an unsigned integer into the specified buffer\r
static inline void PackUnsignedInt(char* buffer, unsigned int value);\r
// packs an unsigned short into the specified buffer\r
static inline void PackUnsignedShort(char* buffer, unsigned short value);\r
+ // unpacks a buffer into a double\r
+ static inline double UnpackDouble(char* buffer);\r
+ static inline double UnpackDouble(const char* buffer);\r
+ // unpacks a buffer into a float\r
+ static inline float UnpackFloat(char* buffer);\r
+ static inline float UnpackFloat(const char* buffer);\r
// unpacks a buffer into a signed int\r
static inline signed int UnpackSignedInt(char* buffer);\r
- // unpacks a buffer into a unsigned int\r
+ static inline signed int UnpackSignedInt(const char* buffer);\r
+ // unpacks a buffer into a signed short\r
+ static inline signed short UnpackSignedShort(char* buffer);\r
+ static inline signed short UnpackSignedShort(const char* buffer);\r
+ // unpacks a buffer into an unsigned int\r
static inline unsigned int UnpackUnsignedInt(char* buffer);\r
- // unpacks a buffer into a unsigned short\r
+ static inline unsigned int UnpackUnsignedInt(const char* buffer);\r
+ // unpacks a buffer into an unsigned short\r
static inline unsigned short UnpackUnsignedShort(char* buffer);\r
+ static inline unsigned short UnpackUnsignedShort(const char* buffer);\r
};\r
\r
// -------------------------------------------------------------\r
+// static 'utility' method implementations\r
\r
+// checks BGZF block header\r
inline\r
bool BgzfData::CheckBlockHeader(char* header) {\r
return (header[0] == GZIP_ID1 &&\r
BgzfData::UnpackUnsignedShort(&header[14]) == BGZF_LEN );\r
}\r
\r
-// packs an unsigned integer into the specified buffer\r
+// 'packs' an unsigned integer into the specified buffer\r
inline\r
void BgzfData::PackUnsignedInt(char* buffer, unsigned int value) {\r
buffer[0] = (char)value;\r
buffer[3] = (char)(value >> 24);\r
}\r
\r
-// packs an unsigned short into the specified buffer\r
+// 'packs' an unsigned short into the specified buffer\r
inline\r
void BgzfData::PackUnsignedShort(char* buffer, unsigned short value) {\r
buffer[0] = (char)value;\r
buffer[1] = (char)(value >> 8);\r
}\r
\r
-// unpacks a buffer into a signed int\r
+// 'unpacks' a buffer into a double (includes both non-const & const char* flavors)\r
+inline\r
+double BgzfData::UnpackDouble(char* buffer) {\r
+ union { double value; unsigned char valueBuffer[sizeof(double)]; } un;\r
+ un.value = 0;\r
+ un.valueBuffer[0] = buffer[0];\r
+ un.valueBuffer[1] = buffer[1];\r
+ un.valueBuffer[2] = buffer[2];\r
+ un.valueBuffer[3] = buffer[3];\r
+ un.valueBuffer[4] = buffer[4];\r
+ un.valueBuffer[5] = buffer[5];\r
+ un.valueBuffer[6] = buffer[6];\r
+ un.valueBuffer[7] = buffer[7];\r
+ return un.value;\r
+}\r
+\r
+inline\r
+double BgzfData::UnpackDouble(const char* buffer) {\r
+ union { double value; unsigned char valueBuffer[sizeof(double)]; } un;\r
+ un.value = 0;\r
+ un.valueBuffer[0] = buffer[0];\r
+ un.valueBuffer[1] = buffer[1];\r
+ un.valueBuffer[2] = buffer[2];\r
+ un.valueBuffer[3] = buffer[3];\r
+ un.valueBuffer[4] = buffer[4];\r
+ un.valueBuffer[5] = buffer[5];\r
+ un.valueBuffer[6] = buffer[6];\r
+ un.valueBuffer[7] = buffer[7];\r
+ return un.value;\r
+}\r
+\r
+// 'unpacks' a buffer into a float (includes both non-const & const char* flavors)\r
+inline\r
+float BgzfData::UnpackFloat(char* buffer) {\r
+ union { float value; unsigned char valueBuffer[sizeof(float)]; } un;\r
+ un.value = 0;\r
+ un.valueBuffer[0] = buffer[0];\r
+ un.valueBuffer[1] = buffer[1];\r
+ un.valueBuffer[2] = buffer[2];\r
+ un.valueBuffer[3] = buffer[3];\r
+ return un.value;\r
+}\r
+\r
+inline\r
+float BgzfData::UnpackFloat(const char* buffer) {\r
+ union { float value; unsigned char valueBuffer[sizeof(float)]; } un;\r
+ un.value = 0;\r
+ un.valueBuffer[0] = buffer[0];\r
+ un.valueBuffer[1] = buffer[1];\r
+ un.valueBuffer[2] = buffer[2];\r
+ un.valueBuffer[3] = buffer[3];\r
+ return un.value;\r
+}\r
+\r
+// 'unpacks' a buffer into a signed int (includes both non-const & const char* flavors)\r
inline\r
signed int BgzfData::UnpackSignedInt(char* buffer) {\r
union { signed int value; unsigned char valueBuffer[sizeof(signed int)]; } un;\r
return un.value;\r
}\r
\r
-// unpacks a buffer into an unsigned int\r
+inline\r
+signed int BgzfData::UnpackSignedInt(const char* buffer) {\r
+ union { signed int value; unsigned char valueBuffer[sizeof(signed int)]; } un;\r
+ un.value = 0;\r
+ un.valueBuffer[0] = buffer[0];\r
+ un.valueBuffer[1] = buffer[1];\r
+ un.valueBuffer[2] = buffer[2];\r
+ un.valueBuffer[3] = buffer[3];\r
+ return un.value;\r
+}\r
+\r
+// 'unpacks' a buffer into a signed short (includes both non-const & const char* flavors)\r
+inline\r
+signed short BgzfData::UnpackSignedShort(char* buffer) {\r
+ union { signed short value; unsigned char valueBuffer[sizeof(signed short)]; } un;\r
+ un.value = 0;\r
+ un.valueBuffer[0] = buffer[0];\r
+ un.valueBuffer[1] = buffer[1];\r
+ return un.value;\r
+}\r
+\r
+inline\r
+signed short BgzfData::UnpackSignedShort(const char* buffer) {\r
+ union { signed short value; unsigned char valueBuffer[sizeof(signed short)]; } un;\r
+ un.value = 0;\r
+ un.valueBuffer[0] = buffer[0];\r
+ un.valueBuffer[1] = buffer[1];\r
+ return un.value;\r
+}\r
+\r
+// 'unpacks' a buffer into an unsigned int (includes both non-const & const char* flavors)\r
inline\r
unsigned int BgzfData::UnpackUnsignedInt(char* buffer) {\r
union { unsigned int value; unsigned char valueBuffer[sizeof(unsigned int)]; } un;\r
return un.value;\r
}\r
\r
-// unpacks a buffer into an unsigned short\r
+inline\r
+unsigned int BgzfData::UnpackUnsignedInt(const char* buffer) {\r
+ union { unsigned int value; unsigned char valueBuffer[sizeof(unsigned int)]; } un;\r
+ un.value = 0;\r
+ un.valueBuffer[0] = buffer[0];\r
+ un.valueBuffer[1] = buffer[1];\r
+ un.valueBuffer[2] = buffer[2];\r
+ un.valueBuffer[3] = buffer[3];\r
+ return un.value;\r
+}\r
+\r
+// 'unpacks' a buffer into an unsigned short (includes both non-const & const char* flavors)\r
inline\r
unsigned short BgzfData::UnpackUnsignedShort(char* buffer) {\r
union { unsigned short value; unsigned char valueBuffer[sizeof(unsigned short)]; } un;\r
return un.value;\r
}\r
\r
+inline\r
+unsigned short BgzfData::UnpackUnsignedShort(const char* buffer) {\r
+ union { unsigned short value; unsigned char valueBuffer[sizeof(unsigned short)]; } un;\r
+ un.value = 0;\r
+ un.valueBuffer[0] = buffer[0];\r
+ un.valueBuffer[1] = buffer[1];\r
+ return un.value;\r
+}\r
+\r
} // namespace BamTools\r
\r
#endif // BGZF_H\r