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