Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cigar.h
Go to the documentation of this file.
1 #ifndef gamgee__cigar__guard
2 #define gamgee__cigar__guard
3 
4 #include "htslib/sam.h"
5 
6 #include <memory>
7 #include <string>
8 #include <vector>
9 #include <sstream>
10 
11 namespace gamgee {
12 
17 enum class CigarOperator { M, I, D, N, S, H, P, EQ, X, B };
18 
19 using CigarElement = uint32_t;
20 
24 class Cigar {
25  public:
26  explicit Cigar(const std::shared_ptr<bam1_t>& sam_record);
27  Cigar(const Cigar& other);
28  Cigar(Cigar&& other) = default;
29  Cigar& operator=(const Cigar& other);
30  Cigar& operator=(Cigar&& other) = default;
31  ~Cigar() = default;
32 
33  CigarElement operator[](const uint32_t index) const;
34  CigarElement& operator[](const uint32_t index);
35  uint32_t size() const { return m_num_cigar_elements; }
36  bool operator==(const Cigar& other) const;
37  bool operator!=(const Cigar& other) const;
38  std::string to_string() const;
39 
43  inline static CigarOperator cigar_op(const CigarElement cigar_element) {
44  return static_cast<CigarOperator>(bam_cigar_op(cigar_element));
45  }
46 
50  inline static uint32_t cigar_oplen(const CigarElement cigar_element) {
51  return bam_cigar_oplen(cigar_element);
52  }
53 
58  inline static CigarElement make_cigar_element(const uint32_t oplen, const CigarOperator op) {
59  return (oplen << BAM_CIGAR_SHIFT) | static_cast<uint32_t>(op);
60  }
61 
68  static const std::vector<int8_t> cigar_op_parse_table;
69 
84  static CigarElement parse_next_cigar_element (std::stringstream& cigar_stream);
85 
86  inline static bool consumes_read_bases(const CigarOperator op) {return bam_cigar_type(static_cast<int8_t>(op))&1;}
87  inline static bool consumes_reference_bases(const CigarOperator op) {return bam_cigar_type(static_cast<int8_t>(op))&2;}
88 
89 
90 
91  private:
92  std::shared_ptr<bam1_t> m_sam_record;
93  uint32_t* m_cigar;
94  uint32_t m_num_cigar_elements;
95 
96  static const char cigar_ops_as_chars[];
97 
98  friend class SamBuilder;
99 };
100 
101 }
102 
103 #endif /* gamgee__cigar__guard */
static const std::vector< int8_t > cigar_op_parse_table
Table used to parse chars representing cigar operations into their htslib encodings.
Definition: cigar.h:68
std::string to_string() const
produce a string representation of this Cigar
Definition: cigar.cpp:99
static CigarElement make_cigar_element(const uint32_t oplen, const CigarOperator op)
creates an encoded htslib cigar element suitable for direct insertion into a Cigar out of a length an...
Definition: cigar.h:58
#define bam_cigar_op(c)
Definition: sam.h:76
class to build Sam objects from existing data or from scratch
Definition: sam_builder.h:58
#define bam_cigar_type(o)
Definition: sam.h:100
uint32_t size() const
number of base qualities in the container
Definition: cigar.h:35
CigarOperator
comprehensive list of valid cigar operators
Definition: cigar.h:17
uint32_t CigarElement
Definition: cigar.h:19
#define BAM_CIGAR_SHIFT
Definition: sam.h:72
bool operator==(const Cigar &other) const
check for equality with another Cigar
Definition: cigar.cpp:79
static uint32_t cigar_oplen(const CigarElement cigar_element)
gets the length of an individual cigar element
Definition: cigar.h:50
static CigarOperator cigar_op(const CigarElement cigar_element)
gets the operator of an individual cigar element
Definition: cigar.h:43
Cigar(const std::shared_ptr< bam1_t > &sam_record)
creates a Cigar object that points to htslib memory already allocated
Definition: cigar.cpp:26
Definition: exceptions.h:9
static bool consumes_reference_bases(const CigarOperator op)
returns true if operator is one of the following: Match (M), Deletion (D), Reference-Skip (N)...
Definition: cigar.h:87
static bool consumes_read_bases(const CigarOperator op)
returns true if operator is one of the following: Match (M), Insertion (I), Soft-Clip (S)...
Definition: cigar.h:86
Cigar & operator=(const Cigar &other)
creates a deep copy of a Cigar object
Definition: cigar.cpp:48
~Cigar()=default
default destruction is sufficient, since our shared_ptr will handle deallocation
Utility class to manage the memory of the cigar structure.
Definition: cigar.h:24
static CigarElement parse_next_cigar_element(std::stringstream &cigar_stream)
utility function to parse cigar strings (in a stringstream) one element at a time ...
Definition: cigar.cpp:107
#define bam_cigar_oplen(c)
Definition: sam.h:77
CigarElement operator[](const uint32_t index) const
use freely as you would an array.
Definition: cigar.cpp:63
bool operator!=(const Cigar &other) const
check for inequality with another Cigar
Definition: cigar.cpp:91