]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/BgzfStream_p.h
Merge branches 'master' and 'iodevice' into iodevice
[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 <api/IBamIODevice.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         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);
62
63     // internal methods
64     private:
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);
71         // reads a BGZF block
72         bool ReadBlock(void);
73
74     // static 'utility' methods
75     public:
76         // checks BGZF block header
77         static inline bool CheckBlockHeader(char* header);
78
79     // data members
80     public:
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;
86
87         char* m_uncompressedBlock;
88         char* m_compressedBlock;
89
90         bool m_isOpen;
91         bool m_isWriteOnly;
92         bool m_isWriteCompressed;
93
94         IBamIODevice* m_device;
95         FILE* m_stream;
96 };
97
98 // -------------------------------------------------------------
99 // static 'utility' method implementations
100
101 // checks BGZF block header
102 inline
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 );
112 }
113
114 } // namespace Internal
115 } // namespace BamTools
116
117 #endif // BGZFSTREAM_P_H