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
20 // ----------------------------------------------------------------
\r
21 // ----------------------------------------------------------------
\r
22 // Platform-specific type definitions
\r
24 #ifndef BAMTOOLS_TYPES
\r
25 #define BAMTOOLS_TYPES
\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
38 #endif // BAMTOOLS_TYPES
\r
40 namespace BamTools {
\r
42 // ----------------------------------------------------------------
\r
43 // ----------------------------------------------------------------
\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
58 // ----------------------------------------------------------------
\r
59 // ----------------------------------------------------------------
\r
60 // Data structs & typedefs
\r
62 // CIGAR operation data structure
\r
66 char Type; // Operation type (MIDNSHP)
\r
67 uint32_t Length; // Operation length (number of bases)
\r
70 CigarOp(const char type = '\0',
\r
71 const uint32_t length = 0)
\r
77 // Reference data entry
\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
86 RefData(const int32_t& length = 0,
\r
89 , RefHasAlignments(ok)
\r
92 typedef std::vector<RefData> RefVector;
\r
94 // General (sequential) genome region
\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
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
121 // ----------------------------------------------------------------
\r
122 // ----------------------------------------------------------------
\r
123 // General utilities
\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
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
136 inline void SwapEndian_16(uint16_t& x) {
\r
137 x = ((x >> 8) | (x << 8));
\r
140 // swaps endianness of 32-bit value 'in-place'
\r
141 inline void SwapEndian_32(int32_t& x) {
\r
143 ((x << 8) & 0x00FF0000) |
\r
144 ((x >> 8) & 0x0000FF00) |
\r
149 inline void SwapEndian_32(uint32_t& x) {
\r
151 ((x << 8) & 0x00FF0000) |
\r
152 ((x >> 8) & 0x0000FF00) |
\r
157 // swaps endianness of 64-bit value 'in-place'
\r
158 inline void SwapEndian_64(int64_t& x) {
\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
170 inline void SwapEndian_64(uint64_t& x) {
\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
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
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
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
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
206 } // namespace BamTools
\r