1 // ***************************************************************************
2 // BgzfStream_p.h (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // All rights reserved.
5 // ---------------------------------------------------------------------------
6 // Last modified: 5 April 2011(DB)
7 // ---------------------------------------------------------------------------
8 // Based on BGZF routines developed at the Broad Institute.
9 // Provides the basic functionality for reading & writing BGZF files
10 // Replaces the old BGZF.* files to avoid clashing with other toolkits
11 // ***************************************************************************
13 #ifndef BGZFSTREAM_P_H
14 #define BGZFSTREAM_P_H
20 // This file is not part of the BamTools API. It exists purely as an
21 // implementation detail. This header file may change from version to version
22 // without notice, or even be removed.
26 #include <api/BamAux.h>
27 #include <api/BamConstants.h>
37 // constructor & destructor
42 // main interface methods
46 // opens the BGZF file (mode is either "rb" for reading, or "wb" for writing)
47 bool Open(const std::string& filename, const char* mode);
48 // reads BGZF data into a byte buffer
49 int Read(char* data, const unsigned int dataLength);
50 // seek to position in BGZF file
51 bool Seek(const int64_t& position);
52 // enable/disable compressed output
53 void SetWriteCompressed(bool ok);
54 // get file position in BGZF file
55 int64_t Tell(void) const;
56 // writes the supplied data into the BGZF buffer
57 unsigned int Write(const char* data, const unsigned int dataLen);
61 // compresses the current block
62 int DeflateBlock(void);
63 // flushes the data in the BGZF block
64 void FlushBlock(void);
65 // de-compresses the current block
66 int InflateBlock(const int& blockLength);
70 // static 'utility' methods
72 // checks BGZF block header
73 static inline bool CheckBlockHeader(char* header);
77 unsigned int UncompressedBlockSize;
78 unsigned int CompressedBlockSize;
79 unsigned int BlockLength;
80 unsigned int BlockOffset;
81 uint64_t BlockAddress;
84 bool IsWriteCompressed;
86 char* UncompressedBlock;
87 char* CompressedBlock;
90 // -------------------------------------------------------------
91 // static 'utility' method implementations
93 // checks BGZF block header
95 bool BgzfStream::CheckBlockHeader(char* header) {
96 return (header[0] == Constants::GZIP_ID1 &&
97 header[1] == (char)Constants::GZIP_ID2 &&
98 header[2] == Z_DEFLATED &&
99 (header[3] & Constants::FLG_FEXTRA) != 0 &&
100 BamTools::UnpackUnsignedShort(&header[10]) == Constants::BGZF_XLEN &&
101 header[12] == Constants::BGZF_ID1 &&
102 header[13] == Constants::BGZF_ID2 &&
103 BamTools::UnpackUnsignedShort(&header[14]) == Constants::BGZF_LEN );
106 } // namespace Internal
107 } // namespace BamTools
109 #endif // BGZFSTREAM_P_H