]> 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 // 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 // ***************************************************************************
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 <api/IBamIODevice.h>
29 #include "zlib.h"
30 #include <cstdio>
31 #include <string>
32
33 namespace BamTools {
34 namespace Internal {
35
36 class BgzfStream {
37
38     // constructor & destructor
39     public:
40         BgzfStream(void);
41         ~BgzfStream(void);
42
43     // main interface methods
44     public:
45         // closes BGZF file
46         void Close(void);
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);
63
64     // internal methods
65     private:
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);
72         // reads a BGZF block
73         bool ReadBlock(void);
74
75     // static 'utility' methods
76     public:
77         // checks BGZF block header
78         static inline bool CheckBlockHeader(char* header);
79
80     // data members
81     public:
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;
87
88         char* m_uncompressedBlock;
89         char* m_compressedBlock;
90
91         bool m_isOpen;
92         bool m_isWriteOnly;
93         bool m_isWriteCompressed;
94
95         IBamIODevice* m_device;
96         FILE* m_stream;
97 };
98
99 // -------------------------------------------------------------
100 // static 'utility' method implementations
101
102 // checks BGZF block header
103 inline
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 );
113 }
114
115 } // namespace Internal
116 } // namespace BamTools
117
118 #endif // BGZFSTREAM_P_H