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