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