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>
27 #include <api/IBamIODevice.h>
37 // constructor & destructor
42 // main interface methods
46 bool IsOpen(void) const;
47 // opens the BGZF stream in requested mode
48 bool Open(const std::string& filename, const char* mode);
49 bool Open(const std::string& filename, const IBamIODevice::OpenMode mode);
50 // reads BGZF data into a byte buffer
51 unsigned int Read(char* data, const unsigned int dataLength);
52 // seek to position in BGZF file
53 bool Seek(const int64_t& position);
54 // sets IO device (closes previous, if any, but does not attempt to open)
55 void SetIODevice(IBamIODevice* device);
56 // enable/disable compressed output
57 void SetWriteCompressed(bool ok);
58 // get file position in BGZF file
59 int64_t Tell(void) const;
60 // writes the supplied data into the BGZF buffer
61 unsigned int Write(const char* data, const unsigned int dataLength);
65 // compresses the current block
66 unsigned int DeflateBlock(void);
67 // flushes the data in the BGZF block
68 void FlushBlock(void);
69 // de-compresses the current block
70 int InflateBlock(const int& blockLength);
74 // static 'utility' methods
76 // checks BGZF block header
77 static inline bool CheckBlockHeader(char* header);
81 unsigned int m_uncompressedBlockSize;
82 unsigned int m_compressedBlockSize;
83 unsigned int m_blockLength;
84 unsigned int m_blockOffset;
85 uint64_t m_blockAddress;
87 char* m_uncompressedBlock;
88 char* m_compressedBlock;
92 bool m_isWriteCompressed;
94 IBamIODevice* m_device;
98 // -------------------------------------------------------------
99 // static 'utility' method implementations
101 // checks BGZF block header
103 bool BgzfStream::CheckBlockHeader(char* header) {
104 return (header[0] == Constants::GZIP_ID1 &&
105 header[1] == (char)Constants::GZIP_ID2 &&
106 header[2] == Z_DEFLATED &&
107 (header[3] & Constants::FLG_FEXTRA) != 0 &&
108 BamTools::UnpackUnsignedShort(&header[10]) == Constants::BGZF_XLEN &&
109 header[12] == Constants::BGZF_ID1 &&
110 header[13] == Constants::BGZF_ID2 &&
111 BamTools::UnpackUnsignedShort(&header[14]) == Constants::BGZF_LEN );
114 } // namespace Internal
115 } // namespace BamTools
117 #endif // BGZFSTREAM_P_H