-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBGZF.h
103 lines (85 loc) · 2.94 KB
/
BGZF.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/////////////////////////////////////////////////////////////////////////////
// SOFTWARE COPYRIGHT NOTICE AGREEMENT //
// This software and its documentation are copyright (2008) by the //
// Broad Institute/Massachusetts Institute of Technology. All rights //
// are reserved. This software is supplied without any warranty or //
// guaranteed support whatsoever. Neither the Broad Institute nor MIT //
// can be responsible for its use, misuse, or functionality. //
/////////////////////////////////////////////////////////////////////////////
/*
* \file BGZF.h
* \author tsharpe
* \date May 21, 2009
*
* \brief Utilities for writing BAM files.
*/
#ifndef LOOKUP_BGZF_H_
#define LOOKUP_BGZF_H_
#include <fstream>
class GZIPHeader
{
public:
GZIPHeader()
: mID1(31), mID2(139), mCompressionMethod(8), mFlags(4), mModTime(0),
mExtraFlags(0), mOpSys(255), mXLen(6), mSI1('B'), mSI2('C'), mSLen(2)
{}
// compiler-supplied copying and destructor are OK
private:
unsigned char mID1;
unsigned char mID2;
unsigned char mCompressionMethod;
unsigned char mFlags;
unsigned int mModTime;
unsigned char mExtraFlags;
unsigned char mOpSys;
unsigned short mXLen;
unsigned char mSI1;
unsigned char mSI2;
unsigned short mSLen;
protected:
unsigned short mBlockSizeLessOne;
};
class BGZFBlock : public GZIPHeader
{
public:
// compiler-supplied no-arg constructor, copying and destructor are OK
// returns the amount of uncompressed data put into the block.
// if len is too big, or the data is too incompressible, not all of the supplied
// data will fit into a block.
unsigned int compress( void* data, unsigned int len );
unsigned int getBlockSize()
{ return mBlockSizeLessOne + 1U; }
private:
bool tryCompress( void* data, unsigned int& len );
// mBlockSizeLessOne is 16 bits, so 64K is the max block length and there are 26 bytes of header and footer
unsigned char mDataBlock[64*1024UL-26UL];
unsigned int mCRC32; // these last two members actually immediately follow the variable-length data block
unsigned int mInputSize; // this struct is what a maximum-size block looks like
static int const WINDOW_BITS = -15;
static int const MEM_LEVEL = 8;
};
class BGZFStreambuf : public std::streambuf
{
public:
BGZFStreambuf( std::streambuf* psb )
: mpSB(psb)
{ setp(mBuf,mBuf+sizeof(mBuf)-1); }
~BGZFStreambuf()
{ sync(); }
private:
BGZFStreambuf( BGZFStreambuf const& ); // undefined -- no copying
BGZFStreambuf& operator=( BGZFStreambuf const& ); // undefined -- no copying
int_type overflow( int_type ch );
int sync();
std::streambuf* mpSB;
char mBuf[128UL*1024UL];
};
class BAMostream : public std::ostream
{
public:
BAMostream( char const* bamFile );
void close();
std::filebuf mFilebuf;
BGZFStreambuf mSB;
};
#endif /* LOOKUP_BGZF_H_ */