Loading...
Searching...
No Matches
BamAux.h
Go to the documentation of this file.
// ***************************************************************************
// BamAux.h (c) 2009 Derek Barnett, Michael Str�mberg
// Marth Lab, Department of Biology, Boston College
// ---------------------------------------------------------------------------
// Last modified: 25 October 2011 (DB)
// ---------------------------------------------------------------------------
// Provides data structures & utility methods that are used throughout the API.
// ***************************************************************************
#ifndef BAMAUX_H
#define BAMAUX_H
#include <cstddef>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "api/api_global.h"
namespace BamTools {
// ----------------------------------------------------------------
// CigarOp
struct API_EXPORT CigarOp
{
char Type;
uint32_t Length;
CigarOp(const char type = '\0', const uint32_t& length = 0)
: Type(type)
, Length(length)
{}
};
// ----------------------------------------------------------------
// RefData
struct API_EXPORT RefData
{
std::string RefName;
int32_t RefLength;
RefData(const std::string& name = std::string(), const int32_t& length = 0)
: RefName(name)
, RefLength(length)
{}
};
typedef std::vector<RefData> RefVector;
// ----------------------------------------------------------------
// BamRegion
struct API_EXPORT BamRegion
{
int LeftRefID;
int LeftPosition;
int RightRefID;
int RightPosition;
BamRegion(const int& leftID = -1, const int& leftPos = -1, const int& rightID = -1,
const int& rightPos = -1)
: LeftRefID(leftID)
, LeftPosition(leftPos)
, RightRefID(rightID)
, RightPosition(rightPos)
{}
void clear()
{
LeftRefID = -1;
LeftPosition = -1;
RightRefID = -1;
RightPosition = -1;
}
bool isLeftBoundSpecified() const
{
return (LeftRefID >= 0 && LeftPosition >= 0);
}
bool isNull() const
{
return (!isLeftBoundSpecified() && !isRightBoundSpecified());
}
bool isRightBoundSpecified() const
{
return (RightRefID >= 0 && RightPosition >= 1);
}
};
struct API_EXPORT CustomHeaderTag
{
std::string TagName;
std::string TagValue;
};
// ----------------------------------------------------------------
// General utility methods
inline bool FileExists(const std::string& filename)
{
std::ifstream f(filename.c_str(), std::ifstream::in);
return !f.fail();
}
inline void SwapEndian_16(uint16_t& x)
{
x = ((x & 0xFF00) >> 8) | ((x & 0x00FF) << 8);
}
inline void SwapEndian_16(int16_t& x)
{
uint16_t val = x;
SwapEndian_16(val);
x = val;
}
inline void SwapEndian_32(uint32_t& x)
{
x = ((x & 0xFF000000) >> 24) | ((x & 0x00FF0000) >> 8) | ((x & 0x0000FF00) << 8) |
((x & 0x000000FF) << 24);
}
inline void SwapEndian_32(int32_t& x)
{
uint32_t val = x;
SwapEndian_32(val);
x = val;
}
inline void SwapEndian_64(uint64_t& x)
{
x = ((x & 0xFF00000000000000ull) >> 56) | ((x & 0x00FF000000000000ull) >> 40) |
((x & 0x0000FF0000000000ull) >> 24) | ((x & 0x000000FF00000000ull) >> 8) |
((x & 0x00000000FF000000ull) << 8) | ((x & 0x0000000000FF0000ull) << 24) |
((x & 0x000000000000FF00ull) << 40) | ((x & 0x00000000000000FFull) << 56);
}
inline void SwapEndian_64(int64_t& x)
{
uint64_t val = x;
SwapEndian_64(val);
x = val;
}
inline void SwapEndian_16p(char* data)
{
uint16_t value;
std::memcpy(&value, data, sizeof(uint16_t));
SwapEndian_16(value);
std::memcpy(data, &value, sizeof(uint16_t));
}
inline void SwapEndian_32p(char* data)
{
uint32_t value;
std::memcpy(&value, data, sizeof(uint32_t));
SwapEndian_32(value);
std::memcpy(data, &value, sizeof(uint32_t));
}
inline void SwapEndian_64p(char* data)
{
uint64_t value;
std::memcpy(&value, data, sizeof(uint64_t));
SwapEndian_64(value);
std::memcpy(data, &value, sizeof(uint64_t));
}
inline bool SystemIsBigEndian()
{
const uint16_t one = 0x0001;
return ((*(char*)&one) == 0);
}
inline void PackUnsignedInt(char* buffer, unsigned int value)
{
buffer[0] = (char)value;
buffer[1] = (char)(value >> 8);
buffer[2] = (char)(value >> 16);
buffer[3] = (char)(value >> 24);
}
inline void PackUnsignedShort(char* buffer, unsigned short value)
{
buffer[0] = (char)value;
buffer[1] = (char)(value >> 8);
}
inline double UnpackDouble(const char* buffer)
{
double result;
std::memcpy(&result, buffer, sizeof(double));
return result;
}
inline float UnpackFloat(const char* buffer)
{
float result;
std::memcpy(&result, buffer, sizeof(float));
return result;
}
inline signed int UnpackSignedInt(const char* buffer)
{
signed int result;
std::memcpy(&result, buffer, sizeof(signed int));
return result;
}
inline signed short UnpackSignedShort(const char* buffer)
{
signed short result;
std::memcpy(&result, buffer, sizeof(signed short));
return result;
}
inline unsigned int UnpackUnsignedInt(const char* buffer)
{
unsigned int result;
std::memcpy(&result, buffer, sizeof(unsigned int));
return result;
}
inline unsigned short UnpackUnsignedShort(const char* buffer)
{
unsigned short result;
std::memcpy(&result, buffer, sizeof(unsigned short));
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
// no further operations required
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
SwapEndian_16(result);
#else
#error "Unsupported hardware"
#endif
return result;
}
// ----------------------------------------------------------------
// 'internal' helper structs
struct API_NO_EXPORT RaiiBuffer
{
// data members
char* Buffer;
const std::size_t NumBytes;
// ctor & dtor
RaiiBuffer(const std::size_t n)
: Buffer(new char[n]())
, NumBytes(n)
{}
~RaiiBuffer()
{
delete[] Buffer;
}
// add'l methods
void Clear()
{
std::memset(Buffer, 0, NumBytes);
}
};
} // namespace BamTools
#endif // BAMAUX_H
api_global.h
BamTools
Contains all BamTools classes & methods.
Definition Sort.h:24
BamTools::SwapEndian_32p
void SwapEndian_32p(char *data)
swaps endianness of the next 4 bytes in a buffer, in place
Definition BamAux.h:224
BamTools::UnpackSignedInt
signed int UnpackSignedInt(const char *buffer)
reads a signed integer value from byte buffer
Definition BamAux.h:311
BamTools::UnpackUnsignedInt
unsigned int UnpackUnsignedInt(const char *buffer)
reads an unsigned integer value from byte buffer
Definition BamAux.h:337
BamTools::SwapEndian_16p
void SwapEndian_16p(char *data)
swaps endianness of the next 2 bytes in a buffer, in place
Definition BamAux.h:213
BamTools::SwapEndian_32
void SwapEndian_32(uint32_t &x)
swaps endianness of unsigned 32-bit integer, in place
Definition BamAux.h:173
BamTools::UnpackSignedShort
signed short UnpackSignedShort(const char *buffer)
reads a signed short integer value from byte buffer
Definition BamAux.h:324
BamTools::SystemIsBigEndian
bool SystemIsBigEndian()
checks host architecture's byte order
Definition BamAux.h:247
BamTools::PackUnsignedInt
void PackUnsignedInt(char *buffer, unsigned int value)
stores unsigned integer value in a byte buffer
Definition BamAux.h:259
BamTools::FileExists
bool FileExists(const std::string &filename)
returns true if the file exists
Definition BamAux.h:146
BamTools::UnpackFloat
float UnpackFloat(const char *buffer)
reads a float value from byte buffer
Definition BamAux.h:298
BamTools::SwapEndian_64p
void SwapEndian_64p(char *data)
swaps endianness of the next 8 bytes in a buffer, in place
Definition BamAux.h:235
BamTools::UnpackUnsignedShort
unsigned short UnpackUnsignedShort(const char *buffer)
reads an unsigned short integer value from byte buffer
Definition BamAux.h:350
BamTools::SwapEndian_16
void SwapEndian_16(uint16_t &x)
swaps endianness of unsigned 16-bit integer, in place
Definition BamAux.h:155
BamTools::RefVector
std::vector< RefData > RefVector
convenience typedef for vector of RefData entries
Definition BamAux.h:75
BamTools::SwapEndian_64
void SwapEndian_64(uint64_t &x)
swaps endianness of unsigned 64-bit integer, in place
Definition BamAux.h:192
BamTools::PackUnsignedShort
void PackUnsignedShort(char *buffer, unsigned short value)
stores unsigned short integer value in a byte buffer
Definition BamAux.h:273
BamTools::UnpackDouble
double UnpackDouble(const char *buffer)
reads a double value from byte buffer
Definition BamAux.h:285
BamTools::BamRegion::RightRefID
int RightRefID
reference ID for region's right boundary
Definition BamAux.h:94
BamTools::BamRegion::LeftRefID
int LeftRefID
reference ID for region's left boundary
Definition BamAux.h:92
BamTools::BamRegion::BamRegion
BamRegion(const int &leftID=-1, const int &leftPos=-1, const int &rightID=-1, const int &rightPos=-1)
constructor
Definition BamAux.h:98
BamTools::BamRegion::RightPosition
int RightPosition
position for region's right boundary
Definition BamAux.h:95
BamTools::BamRegion::LeftPosition
int LeftPosition
position for region's left boundary
Definition BamAux.h:93
BamTools::BamRegion::isNull
bool isNull() const
Returns true if region boundaries are not defined.
Definition BamAux.h:122
BamTools::BamRegion::clear
void clear()
Clears region boundaries.
Definition BamAux.h:107
BamTools::BamRegion::isLeftBoundSpecified
bool isLeftBoundSpecified() const
Returns true if region has a left boundary.
Definition BamAux.h:116
BamTools::BamRegion::isRightBoundSpecified
bool isRightBoundSpecified() const
Returns true if region has a right boundary.
Definition BamAux.h:128
BamTools::CigarOp::CigarOp
CigarOp(const char type='\0', const uint32_t &length=0)
constructor
Definition BamAux.h:49
BamTools::CigarOp::Length
uint32_t Length
CIGAR operation length (number of bases).
Definition BamAux.h:46
BamTools::CigarOp::Type
char Type
CIGAR operation type (MIDNSHPX=).
Definition BamAux.h:45
BamTools::CustomHeaderTag
Definition BamAux.h:135
BamTools::CustomHeaderTag::TagValue
std::string TagValue
Definition BamAux.h:137
BamTools::CustomHeaderTag::TagName
std::string TagName
Definition BamAux.h:136
BamTools::RefData::RefName
std::string RefName
name of reference sequence
Definition BamAux.h:64
BamTools::RefData::RefData
RefData(const std::string &name=std::string(), const int32_t &length=0)
constructor
Definition BamAux.h:68
BamTools::RefData::RefLength
int32_t RefLength
length of reference sequence
Definition BamAux.h:65