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
20 // Platform-specific large-file support
\r
21 #ifndef BAMTOOLS_LFS
\r
22 #define BAMTOOLS_LFS
\r
24 #define ftell64(a) _ftelli64(a)
\r
25 #define fseek64(a,b,c) _fseeki64(a,b,c)
\r
27 #define ftell64(a) ftello(a)
\r
28 #define fseek64(a,b,c) fseeko(a,b,c)
\r
30 #endif // BAMTOOLS_LFS
\r
32 // Platform-specific type definitions
\r
33 #ifndef BAMTOOLS_TYPES
\r
34 #define BAMTOOLS_TYPES
\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
47 #endif // BAMTOOLS_TYPES
\r
49 namespace BamTools {
\r
51 // ----------------------------------------------------------------
\r
52 // ----------------------------------------------------------------
\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
67 // ----------------------------------------------------------------
\r
68 // ----------------------------------------------------------------
\r
69 // Data structs & typedefs
\r
71 // CIGAR operation data structure
\r
75 char Type; // Operation type (MIDNSHP)
\r
76 uint32_t Length; // Operation length (number of bases)
\r
79 CigarOp(const char type = '\0',
\r
80 const uint32_t length = 0)
\r
86 // Reference data entry
\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
95 RefData(const int32_t& length = 0,
\r
98 , RefHasAlignments(ok)
\r
101 typedef std::vector<RefData> RefVector;
\r
103 // General (sequential) genome region
\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
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
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
138 // ----------------------------------------------------------------
\r
139 // ----------------------------------------------------------------
\r
140 // General utilities
\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
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
153 inline void SwapEndian_16(uint16_t& x) {
\r
154 x = ((x >> 8) | (x << 8));
\r
157 // swaps endianness of 32-bit value 'in-place'
\r
158 inline void SwapEndian_32(int32_t& x) {
\r
160 ((x << 8) & 0x00FF0000) |
\r
161 ((x >> 8) & 0x0000FF00) |
\r
166 inline void SwapEndian_32(uint32_t& x) {
\r
168 ((x << 8) & 0x00FF0000) |
\r
169 ((x >> 8) & 0x0000FF00) |
\r
174 // swaps endianness of 64-bit value 'in-place'
\r
175 inline void SwapEndian_64(int64_t& x) {
\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
187 inline void SwapEndian_64(uint64_t& x) {
\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
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
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
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
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
223 } // namespace BamTools
\r