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