]> git.donarmstrong.com Git - bamtools.git/blob - src/api/BamConstants.h
First stab at templated tag access in BamAlignment
[bamtools.git] / src / api / BamConstants.h
1 // ***************************************************************************
2 // BamConstants.h (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 4 October 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides basic constants for handling BAM files.
8 // ***************************************************************************
9
10 #ifndef BAM_CONSTANTS_H
11 #define BAM_CONSTANTS_H
12
13 #include <api/api_global.h>
14 #include <cassert>
15 #include <string>
16
17 /*! \namespace BamTools::Constants
18     \brief Provides basic constants for handling BAM files.
19 */
20
21 namespace BamTools {
22 namespace Constants {
23
24 const int BAM_SIZEOF_INT = 4;
25
26 // header magic number
27 const char* const  BAM_HEADER_MAGIC = "BAM\1";
28 const unsigned int BAM_HEADER_MAGIC_LENGTH = 4;
29
30 // BAM alignment core size
31 const int BAM_CORE_SIZE = 32;
32 const int BAM_CORE_BUFFER_SIZE = 8;
33
34 // BAM alignment flags
35 const int BAM_ALIGNMENT_PAIRED              = 0x0001;
36 const int BAM_ALIGNMENT_PROPER_PAIR         = 0x0002;
37 const int BAM_ALIGNMENT_UNMAPPED            = 0x0004;
38 const int BAM_ALIGNMENT_MATE_UNMAPPED       = 0x0008;
39 const int BAM_ALIGNMENT_REVERSE_STRAND      = 0x0010;
40 const int BAM_ALIGNMENT_MATE_REVERSE_STRAND = 0x0020;
41 const int BAM_ALIGNMENT_READ_1              = 0x0040;
42 const int BAM_ALIGNMENT_READ_2              = 0x0080;
43 const int BAM_ALIGNMENT_SECONDARY           = 0x0100;
44 const int BAM_ALIGNMENT_QC_FAILED           = 0x0200;
45 const int BAM_ALIGNMENT_DUPLICATE           = 0x0400;
46
47 // CIGAR constants
48 const char* const BAM_CIGAR_LOOKUP = "MIDNSHP=X";
49 const int BAM_CIGAR_MATCH    = 0;
50 const int BAM_CIGAR_INS      = 1;
51 const int BAM_CIGAR_DEL      = 2;
52 const int BAM_CIGAR_REFSKIP  = 3;
53 const int BAM_CIGAR_SOFTCLIP = 4;
54 const int BAM_CIGAR_HARDCLIP = 5;
55 const int BAM_CIGAR_PAD      = 6;
56 const int BAM_CIGAR_SEQMATCH = 7;
57 const int BAM_CIGAR_MISMATCH = 8;
58
59 const char BAM_CIGAR_MATCH_CHAR    = 'M';
60 const char BAM_CIGAR_INS_CHAR      = 'I';
61 const char BAM_CIGAR_DEL_CHAR      = 'D';
62 const char BAM_CIGAR_REFSKIP_CHAR  = 'N';
63 const char BAM_CIGAR_SOFTCLIP_CHAR = 'S';
64 const char BAM_CIGAR_HARDCLIP_CHAR = 'H';
65 const char BAM_CIGAR_PAD_CHAR      = 'P';
66 const char BAM_CIGAR_SEQMATCH_CHAR = '=';
67 const char BAM_CIGAR_MISMATCH_CHAR = 'X';
68
69 const int BAM_CIGAR_SHIFT    = 4;
70 const int BAM_CIGAR_MASK     = ((1 << BAM_CIGAR_SHIFT) - 1);
71
72 // BAM tag types
73 const char BAM_TAG_TYPE_ASCII  = 'A';
74 const char BAM_TAG_TYPE_UINT8  = 'c';
75 const char BAM_TAG_TYPE_INT8   = 'C';
76 const char BAM_TAG_TYPE_UINT16 = 's';
77 const char BAM_TAG_TYPE_INT16  = 'S';
78 const char BAM_TAG_TYPE_UINT32 = 'i';
79 const char BAM_TAG_TYPE_INT32  = 'I';
80 const char BAM_TAG_TYPE_FLOAT  = 'f';
81 const char BAM_TAG_TYPE_STRING = 'Z';
82 const char BAM_TAG_TYPE_HEX    = 'H';
83 const char BAM_TAG_TYPE_ARRAY  = 'B';
84
85 const size_t BAM_TAG_TAGSIZE  = 2;
86 const size_t BAM_TAG_TYPESIZE = 1;
87 const int BAM_TAG_ARRAYBASE_SIZE = 8;
88
89 // DNA bases
90 const char* const BAM_DNA_LOOKUP = "=ACMGRSVTWYHKDBN";
91 const unsigned char BAM_BASECODE_EQUAL = 0;
92 const unsigned char BAM_BASECODE_A     = 1;
93 const unsigned char BAM_BASECODE_C     = 2;
94 const unsigned char BAM_BASECODE_G     = 4;
95 const unsigned char BAM_BASECODE_T     = 8;
96 const unsigned char BAM_BASECODE_N     = 15;
97
98 const char BAM_DNA_EQUAL   = '=';
99 const char BAM_DNA_A       = 'A';
100 const char BAM_DNA_C       = 'C';
101 const char BAM_DNA_G       = 'G';
102 const char BAM_DNA_T       = 'T';
103 const char BAM_DNA_N       = 'N';
104 const char BAM_DNA_DEL     = '-';
105 const char BAM_DNA_PAD     = '*';
106
107 // zlib constants
108 const int GZIP_ID1   = 31;
109 const int GZIP_ID2   = 139;
110 const int CM_DEFLATE = 8;
111 const int FLG_FEXTRA = 4;
112 const int OS_UNKNOWN = 255;
113 const int BGZF_XLEN  = 6;
114 const int BGZF_ID1   = 66;
115 const int BGZF_ID2   = 67;
116 const int BGZF_LEN   = 2;
117 const int GZIP_WINDOW_BITS    = -15;
118 const int Z_DEFAULT_MEM_LEVEL = 8;
119
120 // BZGF constants
121 const int BGZF_BLOCK_HEADER_LENGTH = 18;
122 const int BGZF_BLOCK_FOOTER_LENGTH = 8;
123 const int BGZF_MAX_BLOCK_SIZE      = 65536;
124 const int BGZF_DEFAULT_BLOCK_SIZE  = 65536;
125
126 } // namespace Constants
127
128 // -------------------------
129 // tag-type helper structs
130 // -------------------------
131
132 // fail on any types not specified below
133 template<typename T>
134 struct TagTypeHelper {
135     static bool CanConvertFrom(const char) { assert(false); return false; }
136     static bool CanConvertTo(const char) { assert(false); return false; }
137     static char TypeCode(void) { assert(false); return 0; }
138 };
139
140 template<>
141 struct TagTypeHelper<uint8_t> {
142     static bool CanConvertFrom(const char c) {
143         return ( c == Constants::BAM_TAG_TYPE_ASCII ||
144                  c == Constants::BAM_TAG_TYPE_UINT8 );
145     }
146     static bool CanConvertTo(const char c) {
147         return ( c == Constants::BAM_TAG_TYPE_ASCII  ||
148                  c == Constants::BAM_TAG_TYPE_UINT8  ||
149                  c == Constants::BAM_TAG_TYPE_UINT16 ||
150                  c == Constants::BAM_TAG_TYPE_UINT32 );
151     }
152
153     static char TypeCode(void) { return Constants::BAM_TAG_TYPE_UINT8; }
154 };
155
156 template<>
157 struct TagTypeHelper<int8_t> {
158     static bool CanConvertFrom(const char c) {
159         return ( c == Constants::BAM_TAG_TYPE_ASCII ||
160                  c == Constants::BAM_TAG_TYPE_INT8 );
161     }
162     static bool CanConvertTo(const char c) {
163         return ( c == Constants::BAM_TAG_TYPE_ASCII ||
164                  c == Constants::BAM_TAG_TYPE_INT8  ||
165                  c == Constants::BAM_TAG_TYPE_INT16 ||
166                  c == Constants::BAM_TAG_TYPE_INT32 );
167     }
168     static char TypeCode(void) { return Constants::BAM_TAG_TYPE_INT8; }
169 };
170
171 template<>
172 struct TagTypeHelper<uint16_t> {
173     static bool CanCovnertFrom(const char c) {
174         return ( c == Constants::BAM_TAG_TYPE_ASCII ||
175                  c == Constants::BAM_TAG_TYPE_UINT8 ||
176                  c == Constants::BAM_TAG_TYPE_UINT16 );
177     }
178     static bool CanConvertTo(const char c) {
179         return ( c == Constants::BAM_TAG_TYPE_UINT16 ||
180                  c == Constants::BAM_TAG_TYPE_UINT32);
181     }
182     static char TypeCode(void) { return Constants::BAM_TAG_TYPE_UINT16; }
183 };
184
185 template<>
186 struct TagTypeHelper<int16_t> {
187     static bool CanConvertFrom(const char c) {
188         return ( c == Constants::BAM_TAG_TYPE_ASCII ||
189                  c == Constants::BAM_TAG_TYPE_INT8 ||
190                  c == Constants::BAM_TAG_TYPE_INT16 );
191     }
192     static bool CanConvertTo(const char c) {
193         return ( c == Constants::BAM_TAG_TYPE_INT16 ||
194                  c == Constants::BAM_TAG_TYPE_INT32);
195     }
196     static char TypeCode(void) { return Constants::BAM_TAG_TYPE_INT16; }
197 };
198
199 template<>
200 struct TagTypeHelper<uint32_t> {
201     static bool CanConvertFrom(const char c) {
202         return ( c == Constants::BAM_TAG_TYPE_ASCII  ||
203                  c == Constants::BAM_TAG_TYPE_UINT8  ||
204                  c == Constants::BAM_TAG_TYPE_UINT16 ||
205                  c == Constants::BAM_TAG_TYPE_UINT32 );
206     }
207     static bool CanConvertTo(const char c) {
208         return ( c == Constants::BAM_TAG_TYPE_UINT32 );
209     }
210     static char TypeCode(void) { return Constants::BAM_TAG_TYPE_UINT32; }
211 };
212
213 template<>
214 struct TagTypeHelper<int32_t> {
215     static bool CanConvertFrom(const char c) {
216         return ( c == Constants::BAM_TAG_TYPE_ASCII  ||
217                  c == Constants::BAM_TAG_TYPE_INT8  ||
218                  c == Constants::BAM_TAG_TYPE_INT16 ||
219                  c == Constants::BAM_TAG_TYPE_INT32 );
220     }
221     static bool CanConvertTo(const char c) {
222         return ( c == Constants::BAM_TAG_TYPE_INT32 );
223     }
224     static char TypeCode(void) { return Constants::BAM_TAG_TYPE_INT32; }
225 };
226
227 template<>
228 struct TagTypeHelper<float> {
229     static bool CanConvertFrom(const char c) {
230         return ( c == Constants::BAM_TAG_TYPE_ASCII  ||
231                  c == Constants::BAM_TAG_TYPE_UINT8  ||
232                  c == Constants::BAM_TAG_TYPE_INT8   ||
233                  c == Constants::BAM_TAG_TYPE_UINT16 ||
234                  c == Constants::BAM_TAG_TYPE_INT16  ||
235                  c == Constants::BAM_TAG_TYPE_UINT32 ||
236                  c == Constants::BAM_TAG_TYPE_INT32  ||
237                  c == Constants::BAM_TAG_TYPE_FLOAT);
238     }
239     static bool CanConvertTo(const char c) {
240         return ( c == Constants::BAM_TAG_TYPE_FLOAT );
241     }
242     static char TypeCode(void) { return Constants::BAM_TAG_TYPE_FLOAT; }
243 };
244
245 template<>
246 struct TagTypeHelper<std::string> {
247     static bool CanConvertFrom(const char c) {
248         return ( c == Constants::BAM_TAG_TYPE_HEX ||
249                  c == Constants::BAM_TAG_TYPE_STRING );
250     }
251     static bool CanConvertTo(const char c) {
252         return ( c == Constants::BAM_TAG_TYPE_HEX ||
253                  c == Constants::BAM_TAG_TYPE_STRING );
254     }
255     static char TypeCode(void) { return Constants::BAM_TAG_TYPE_STRING; }
256 };
257
258 } // namespace BamTools
259
260 #endif // BAM_CONSTANTS_H