]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/BgzfStream_p.h
838f30cde3f2f2a163fa72cd2852ea00315a4887
[bamtools.git] / src / api / internal / BgzfStream_p.h
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 // ***************************************************************************
11
12 #ifndef BGZFSTREAM_P_H
13 #define BGZFSTREAM_P_H
14
15 //  -------------
16 //  W A R N I N G
17 //  -------------
18 //
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.
22 //
23 // We mean it.
24
25 #include <api/BamAux.h>
26 #include <api/BamConstants.h>
27 #include "zlib.h"
28 #include <cstdio>
29 #include <string>
30
31 namespace BamTools {
32 namespace Internal {
33
34 class BgzfStream {
35
36     // constructor & destructor
37     public:
38         BgzfStream(void);
39         ~BgzfStream(void);
40
41     // main interface methods
42     public:
43         // closes BGZF file
44         void Close(void);
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);
57
58     // internal methods
59     private:
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);
66         // reads a BGZF block
67         bool ReadBlock(void);
68
69     // static 'utility' methods
70     public:
71         // checks BGZF block header
72         static inline bool CheckBlockHeader(char* header);
73
74     // data members
75     public:
76         unsigned int UncompressedBlockSize;
77         unsigned int CompressedBlockSize;
78         unsigned int BlockLength;
79         unsigned int BlockOffset;
80         uint64_t BlockAddress;
81         bool IsOpen;
82         bool IsWriteOnly;
83         bool IsWriteCompressed;
84         FILE* Stream;
85         char* UncompressedBlock;
86         char* CompressedBlock;
87 };
88
89 // -------------------------------------------------------------
90 // static 'utility' method implementations
91
92 // checks BGZF block header
93 inline
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 );
103 }
104
105 } // namespace Internal
106 } // namespace BamTools
107
108 #endif // BGZFSTREAM_P_H