]> git.donarmstrong.com Git - bamtools.git/blob - src/api/BamAux.h
Implemented index cache mode for both BAI & BTI formats
[bamtools.git] / src / api / BamAux.h
1 // ***************************************************************************\r
2 // BamAux.h (c) 2009 Derek Barnett, Michael Str�mberg\r
3 // Marth Lab, Department of Biology, Boston College\r
4 // All rights reserved.\r
5 // ---------------------------------------------------------------------------\r
6 // Last modified: 9 October 2010 (DB)\r
7 // ---------------------------------------------------------------------------\r
8 // Provides the basic constants, data structures, utilities etc. \r
9 // used throughout the API for handling BAM files\r
10 // ***************************************************************************\r
11 \r
12 #ifndef BAMAUX_H\r
13 #define BAMAUX_H\r
14 \r
15 #include <fstream> \r
16 #include <iostream>\r
17 #include <string>\r
18 #include <vector>\r
19 \r
20 // Platform-specific large-file support\r
21 #ifndef BAMTOOLS_LFS\r
22 #define BAMTOOLS_LFS\r
23     #ifdef WIN32\r
24         #define ftell64(a)     _ftelli64(a)\r
25         #define fseek64(a,b,c) _fseeki64(a,b,c)\r
26     #else\r
27         #define ftell64(a)     ftello(a)\r
28         #define fseek64(a,b,c) fseeko(a,b,c)\r
29     #endif\r
30 #endif // BAMTOOLS_LFS\r
31 \r
32 // Platform-specific type definitions\r
33 #ifndef BAMTOOLS_TYPES\r
34 #define BAMTOOLS_TYPES\r
35     #ifdef _MSC_VER\r
36         typedef char                 int8_t;\r
37         typedef unsigned char       uint8_t;\r
38         typedef short               int16_t;\r
39         typedef unsigned short     uint16_t;\r
40         typedef int                 int32_t;\r
41         typedef unsigned int       uint32_t;\r
42         typedef long long           int64_t;\r
43         typedef unsigned long long uint64_t;\r
44     #else\r
45         #include <stdint.h>\r
46     #endif\r
47 #endif // BAMTOOLS_TYPES\r
48 \r
49 namespace BamTools {\r
50 \r
51 // ----------------------------------------------------------------\r
52 // ----------------------------------------------------------------\r
53 // BAM constants\r
54 \r
55 const int BAM_CMATCH      = 0;\r
56 const int BAM_CINS        = 1;\r
57 const int BAM_CDEL        = 2;\r
58 const int BAM_CREF_SKIP   = 3;\r
59 const int BAM_CSOFT_CLIP  = 4;\r
60 const int BAM_CHARD_CLIP  = 5;\r
61 const int BAM_CPAD        = 6;\r
62 const int BAM_CIGAR_SHIFT = 4;\r
63 const int BAM_CIGAR_MASK  = ((1 << BAM_CIGAR_SHIFT) - 1);\r
64 const int BAM_CORE_SIZE   = 32;\r
65 const int BT_SIZEOF_INT   = 4;\r
66 \r
67 // ----------------------------------------------------------------\r
68 // ----------------------------------------------------------------\r
69 // Data structs & typedefs\r
70 \r
71 // CIGAR operation data structure\r
72 struct CigarOp {\r
73   \r
74     // data members\r
75     char     Type;   // Operation type (MIDNSHP)\r
76     uint32_t Length; // Operation length (number of bases)\r
77     \r
78     // constructor\r
79     CigarOp(const char type = '\0', \r
80             const uint32_t length = 0) \r
81         : Type(type)\r
82         , Length(length) \r
83     { }\r
84 };\r
85 \r
86 // Reference data entry\r
87 struct RefData {\r
88    \r
89     // data members\r
90     std::string RefName;          // Name of reference sequence\r
91     int32_t     RefLength;        // Length of reference sequence\r
92     bool        RefHasAlignments; // True if BAM file contains alignments mapped to reference sequence\r
93     \r
94     // constructor\r
95     RefData(const int32_t& length = 0, \r
96             bool ok = false)\r
97         : RefLength(length)\r
98         , RefHasAlignments(ok)\r
99     { }\r
100 };\r
101 typedef std::vector<RefData> RefVector;\r
102 \r
103 // General (sequential) genome region\r
104 struct BamRegion {\r
105   \r
106     // data members\r
107     int LeftRefID;\r
108     int LeftPosition;\r
109     int RightRefID;\r
110     int RightPosition;\r
111     \r
112     // constructor\r
113     BamRegion(const int& leftID   = -1, \r
114               const int& leftPos  = -1,\r
115               const int& rightID  = -1,\r
116               const int& rightPos = -1)\r
117         : LeftRefID(leftID)\r
118         , LeftPosition(leftPos)\r
119         , RightRefID(rightID)\r
120         , RightPosition(rightPos)\r
121     { }\r
122     \r
123     // copy constructor\r
124     BamRegion(const BamRegion& other)\r
125         : LeftRefID(other.LeftRefID)\r
126         , LeftPosition(other.LeftPosition)\r
127         , RightRefID(other.RightRefID)\r
128         , RightPosition(other.RightPosition)\r
129     { }\r
130     \r
131     // member functions\r
132     void clear(void) { LeftRefID = -1; LeftPosition = -1; RightRefID = -1; RightPosition = -1; }\r
133     bool isLeftBoundSpecified(void) const { return ( LeftRefID != -1 && LeftPosition != -1 ); }\r
134     bool isNull(void) const { return ( !isLeftBoundSpecified() && !isRightBoundSpecified() ); }\r
135     bool isRightBoundSpecified(void) const { return ( RightRefID != -1 && RightPosition != -1 ); }\r
136 };\r
137 \r
138 // ----------------------------------------------------------------\r
139 // ----------------------------------------------------------------\r
140 // General utilities \r
141 \r
142 // returns true if system is big endian\r
143 inline bool SystemIsBigEndian(void) {\r
144    const uint16_t one = 0x0001;\r
145    return ((*(char*) &one) == 0 );\r
146 }\r
147 \r
148 // swaps endianness of 16-bit value 'in place'\r
149 inline void SwapEndian_16(int16_t& x) {\r
150     x = ((x >> 8) | (x << 8));\r
151 }\r
152 \r
153 inline void SwapEndian_16(uint16_t& x) {\r
154     x = ((x >> 8) | (x << 8));\r
155 }\r
156 \r
157 // swaps endianness of 32-bit value 'in-place'\r
158 inline void SwapEndian_32(int32_t& x) {\r
159     x = ( (x >> 24) | \r
160          ((x << 8) & 0x00FF0000) | \r
161          ((x >> 8) & 0x0000FF00) | \r
162           (x << 24)\r
163         );\r
164 }\r
165 \r
166 inline void SwapEndian_32(uint32_t& x) {\r
167     x = ( (x >> 24) | \r
168          ((x << 8) & 0x00FF0000) | \r
169          ((x >> 8) & 0x0000FF00) | \r
170           (x << 24)\r
171         );\r
172 }\r
173 \r
174 // swaps endianness of 64-bit value 'in-place'\r
175 inline void SwapEndian_64(int64_t& x) {\r
176     x = ( (x >> 56) | \r
177          ((x << 40) & 0x00FF000000000000ll) |\r
178          ((x << 24) & 0x0000FF0000000000ll) |\r
179          ((x << 8)  & 0x000000FF00000000ll) |\r
180          ((x >> 8)  & 0x00000000FF000000ll) |\r
181          ((x >> 24) & 0x0000000000FF0000ll) |\r
182          ((x >> 40) & 0x000000000000FF00ll) |\r
183           (x << 56)\r
184         );\r
185 }\r
186 \r
187 inline void SwapEndian_64(uint64_t& x) {\r
188     x = ( (x >> 56) | \r
189          ((x << 40) & 0x00FF000000000000ll) |\r
190          ((x << 24) & 0x0000FF0000000000ll) |\r
191          ((x << 8)  & 0x000000FF00000000ll) |\r
192          ((x >> 8)  & 0x00000000FF000000ll) |\r
193          ((x >> 24) & 0x0000000000FF0000ll) |\r
194          ((x >> 40) & 0x000000000000FF00ll) |\r
195           (x << 56)\r
196         );\r
197 }\r
198 \r
199 // swaps endianness of 'next 2 bytes' in a char buffer (in-place)\r
200 inline void SwapEndian_16p(char* data) {\r
201     uint16_t& value = (uint16_t&)*data; \r
202     SwapEndian_16(value);\r
203 }\r
204 \r
205 // swaps endianness of 'next 4 bytes' in a char buffer (in-place)\r
206 inline void SwapEndian_32p(char* data) {\r
207     uint32_t& value = (uint32_t&)*data; \r
208     SwapEndian_32(value);\r
209 }\r
210 \r
211 // swaps endianness of 'next 8 bytes' in a char buffer (in-place)\r
212 inline void SwapEndian_64p(char* data) {\r
213     uint64_t& value = (uint64_t&)*data; \r
214     SwapEndian_64(value);\r
215 }\r
216 \r
217 // returns whether file exists (can be opened OK)\r
218 inline bool FileExists(const std::string& filename) {\r
219     std::ifstream f(filename.c_str(), std::ifstream::in);\r
220     return !f.fail();\r
221 }\r
222 \r
223 } // namespace BamTools\r
224 \r
225 #endif // BAMAUX_H\r