Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sam.h
Go to the documentation of this file.
1 #ifndef gamgee__sam__guard
2 #define gamgee__sam__guard
3 
4 #include "sam_header.h"
5 #include "read_bases.h"
6 #include "base_quals.h"
7 #include "cigar.h"
8 #include "sam_tag.h"
9 
10 #include "htslib/sam.h"
11 
12 #include <string>
13 #include <memory>
14 
15 namespace gamgee {
16 
20 class Sam {
21  public:
27  Sam() = default;
28 
35  explicit Sam(const std::shared_ptr<bam_hdr_t>& header, const std::shared_ptr<bam1_t>& body) noexcept;
36 
47  Sam(const Sam& other);
48 
52  Sam& operator=(const Sam& other);
53 
57  Sam(Sam&& other) = default;
58 
62  Sam& operator=(Sam&& other) = default;
63 
70  return SamHeader{m_header};
71  }
72 
80  uint32_t chromosome() const { return uint32_t(m_body->core.tid); }
81 
87  uint32_t alignment_start() const { return uint32_t(m_body->core.pos+1); }
88 
94  uint32_t alignment_stop() const { return uint32_t(bam_endpos(m_body.get())); }
95 
107  uint32_t unclipped_start() const ;
108 
119  uint32_t unclipped_stop() const ;
120 
126  uint32_t mate_chromosome() const { return uint32_t(m_body->core.mtid); }
127 
132  uint32_t mate_alignment_start() const { return uint32_t(m_body->core.mpos+1); }
133 
139  uint32_t mate_alignment_stop() const ;
140 
168  uint32_t mate_alignment_stop(const SamTag<std::string>& mate_cigar_tag) const ;
169 
174  uint32_t mate_unclipped_start() const;
175 
203  uint32_t mate_unclipped_start(const SamTag<std::string>& mate_cigar_tag) const;
204 
208  uint32_t mate_unclipped_stop() const ;
209 
237  uint32_t mate_unclipped_stop(const SamTag<std::string>& mate_cigar_tag) const;
238 
242  uint8_t mapping_qual() const { return uint8_t(m_body->core.qual); }
243 
256  int32_t insert_size() const { return m_body->core.isize; }
257 
258  // modify non-variable length fields (things outside of the data member)
259  void set_chromosome(const uint32_t chr) { m_body->core.tid = int32_t(chr); }
260  void set_alignment_start(const uint32_t start) { m_body->core.pos = int32_t(start-1); }
261  void set_mate_chromosome(const uint32_t mchr) { m_body->core.mtid = int32_t(mchr); }
262  void set_mate_alignment_start(const uint32_t mstart) { m_body->core.mpos = int32_t(mstart - 1); }
263  void set_mapping_qual(const uint8_t mapq) { m_body->core.qual = mapq; }
264  void set_insert_size(const int32_t isize) { m_body->core.isize = isize; }
265 
266  // getters for fields inside the data field
267  std::string name() const { return std::string{bam_get_qname(m_body.get())}; }
268  Cigar cigar() const { return Cigar{m_body}; }
269  ReadBases bases() const { return ReadBases{m_body}; }
270  BaseQuals base_quals() const { return BaseQuals{m_body}; }
271 
272  // getters for tagged values within the aux part of the data field
273  SamTag<int32_t> integer_tag(const std::string& tag_name) const;
274  SamTag<double> double_tag(const std::string& tag_name) const;
275  SamTag<char> char_tag(const std::string& tag_name) const;
276  SamTag<std::string> string_tag(const std::string& tag_name) const;
277 
278  // getters for flags
279  bool paired() const { return m_body->core.flag & BAM_FPAIRED; }
280  bool properly_paired() const { return m_body->core.flag & BAM_FPROPER_PAIR; }
281  bool unmapped() const { return m_body->core.flag & BAM_FUNMAP; }
282  bool mate_unmapped() const { return m_body->core.flag & BAM_FMUNMAP; }
283  bool reverse() const { return m_body->core.flag & BAM_FREVERSE; }
284  bool mate_reverse() const { return m_body->core.flag & BAM_FMREVERSE; }
285  bool first() const { return m_body->core.flag & BAM_FREAD1; }
286  bool last() const { return m_body->core.flag & BAM_FREAD2; }
287  bool secondary() const { return m_body->core.flag & BAM_FSECONDARY; }
288  bool fail() const { return m_body->core.flag & BAM_FQCFAIL; }
289  bool duplicate() const { return m_body->core.flag & BAM_FDUP; }
290  bool supplementary() const { return m_body->core.flag & BAM_FSUPPLEMENTARY; }
291 
292  // modify flags
293  void set_paired() { m_body->core.flag |= BAM_FPAIRED; }
294  void set_not_paired() { m_body->core.flag &= ~BAM_FPAIRED; }
295  void set_unmapped() { m_body->core.flag |= BAM_FUNMAP; }
296  void set_not_unmapped() { m_body->core.flag &= ~BAM_FUNMAP; }
297  void set_mate_unmapped() { m_body->core.flag |= BAM_FMUNMAP; }
298  void set_not_mate_unmapped() { m_body->core.flag &= ~BAM_FMUNMAP; }
299  void set_reverse() { m_body->core.flag |= BAM_FREVERSE; }
300  void set_not_reverse() { m_body->core.flag &= ~BAM_FREVERSE; }
301  void set_mate_reverse() { m_body->core.flag |= BAM_FMREVERSE; }
302  void set_not_mate_reverse() { m_body->core.flag &= ~BAM_FMREVERSE; }
303  void set_first() { m_body->core.flag |= BAM_FREAD1; }
304  void set_not_first() { m_body->core.flag &= ~BAM_FREAD1; }
305  void set_last() { m_body->core.flag |= BAM_FREAD2; }
306  void set_not_last() { m_body->core.flag &= ~BAM_FREAD2; }
307  void set_secondary() { m_body->core.flag |= BAM_FSECONDARY; }
308  void set_not_secondary() { m_body->core.flag &= ~BAM_FSECONDARY; }
309  void set_fail() { m_body->core.flag |= BAM_FQCFAIL; }
310  void set_not_fail() { m_body->core.flag &= ~BAM_FQCFAIL; }
311  void set_duplicate() { m_body->core.flag |= BAM_FDUP; }
312  void set_not_duplicate() { m_body->core.flag &= ~BAM_FDUP; }
313  void set_supplementary() { m_body->core.flag |= BAM_FSUPPLEMENTARY; }
314  void set_not_supplementary() { m_body->core.flag &= ~BAM_FSUPPLEMENTARY; }
315 
316  bool empty() const { return m_body == nullptr; }
317 
318  private:
319  std::shared_ptr<bam_hdr_t> m_header;
320  std::shared_ptr<bam1_t> m_body;
321 
322  friend class SamWriter;
323  friend class SamBuilder;
324 };
325 
326 } // end of namespace
327 
328 #endif /* defined(gamgee__sam__guard) */
uint32_t alignment_stop() const
returns a (1-based and inclusive) alignment stop position.
Definition: sam.h:94
int32_t insert_size() const
inferred insert size as reported by the aligner
Definition: sam.h:256
void set_first()
Definition: sam.h:303
uint32_t mate_unclipped_stop() const
returns a (1-based and inclusive) mate's unclipped alignment stop position.
Definition: sam.cpp:103
void set_not_paired()
Definition: sam.h:294
Utility class to hold the header of a sam file.
Definition: sam_header.h:16
void set_not_first()
Definition: sam.h:304
void set_duplicate()
Definition: sam.h:311
uint32_t mate_unclipped_start() const
returns a (1-based and inclusive) mate's unclipped alignment start position.
Definition: sam.cpp:83
uint32_t alignment_start() const
the reference position of the first base in the read
Definition: sam.h:87
class to build Sam objects from existing data or from scratch
Definition: sam_builder.h:58
void set_reverse()
Definition: sam.h:299
BaseQuals base_quals() const
returns the base qualities.
Definition: sam.h:270
bool empty() const
whether or not this Sam object is empty, meaning that the internal memory has not been initialized (i...
Definition: sam.h:316
uint32_t mate_chromosome() const
returns the integer representation of the mate's chromosome.
Definition: sam.h:126
uint32_t chromosome() const
chromosome index of the read.
Definition: sam.h:80
void set_not_last()
Definition: sam.h:306
#define BAM_FPAIRED
Definition: sam.h:103
bool paired() const
whether or not this read is paired
Definition: sam.h:279
#define BAM_FREAD2
Definition: sam.h:117
SamHeader header()
the header of the Sam record
Definition: sam.h:69
int32_t bam_endpos(const bam1_t *b)
Definition: sam.c:261
void set_not_duplicate()
Definition: sam.h:312
#define BAM_FPROPER_PAIR
Definition: sam.h:105
bool last() const
whether or not this read is the last read in a pair (or multiple pairs)
Definition: sam.h:286
bool secondary() const
whether or not this read is a secondary alignment (see definition in BAM spec)
Definition: sam.h:287
void set_fail()
Definition: sam.h:309
void set_mapping_qual(const uint8_t mapq)
simple setter for the alignment quality
Definition: sam.h:263
bool fail() const
whether or not this read is marked as failing vendor (sequencer) quality control
Definition: sam.h:288
utility class to write out a SAM/BAM/CRAM file to any stream
Definition: sam_writer.h:20
bool mate_reverse() const
whether or not the mate read is from the reverse strand
Definition: sam.h:284
uint32_t unclipped_stop() const
calculates the theoretical alignment stop of a read that has soft/hard-clips preceding the alignment ...
Definition: sam.cpp:70
SamTag< std::string > string_tag(const std::string &tag_name) const
retrieve a string-valued tag by name.
Definition: sam.cpp:182
Sam & operator=(const Sam &other)
creates a deep copy of a sam record
Definition: sam.cpp:28
Utility class to handle the memory management of the sam record object for a read base qualities...
Definition: base_quals.h:13
SamTag< double > double_tag(const std::string &tag_name) const
retrieve an double/float-valued tag by name.
Definition: sam.cpp:151
bool first() const
whether or not this read is the first read in a pair (or multiple pairs)
Definition: sam.h:285
void set_mate_alignment_start(const uint32_t mstart)
simple setter for the mate's alignment start.
Definition: sam.h:262
uint32_t unclipped_start() const
calculates the theoretical alignment start of a read that has soft/hard-clips preceding the alignment...
Definition: sam.cpp:57
bool duplicate() const
whether or not this read is a duplicate
Definition: sam.h:289
bool unmapped() const
whether or not this read is unmapped
Definition: sam.h:281
void set_mate_unmapped()
Definition: sam.h:297
#define BAM_FMREVERSE
Definition: sam.h:113
uint32_t mate_alignment_stop() const
returns a (1-based and inclusive) mate's alignment stop position.
Definition: sam.cpp:50
bool properly_paired() const
whether or not this read is properly paired (see definition in BAM spec)
Definition: sam.h:280
#define BAM_FQCFAIL
Definition: sam.h:121
#define BAM_FSUPPLEMENTARY
Definition: sam.h:125
SamTag< int32_t > integer_tag(const std::string &tag_name) const
retrieve an integer-valued tag by name.
Definition: sam.cpp:137
uint8_t mapping_qual() const
returns the mapping quality of this alignment
Definition: sam.h:242
uint32_t mate_alignment_start() const
returns a (1-based and inclusive) mate's alignment start position (as you would see in a Sam file)...
Definition: sam.h:132
#define BAM_FSECONDARY
Definition: sam.h:119
void set_not_secondary()
Definition: sam.h:308
std::string name() const
returns the read name
Definition: sam.h:267
Definition: exceptions.h:9
#define BAM_FMUNMAP
Definition: sam.h:109
void set_mate_reverse()
Definition: sam.h:301
Sam()=default
initializes a null Sam.
void set_mate_chromosome(const uint32_t mchr)
simple setter for the mate's chromosome index. Index is 0-based.
Definition: sam.h:261
class to represent a Sam TAG:TYPE:VALUE entry
Definition: sam_tag.h:12
void set_not_unmapped()
Definition: sam.h:296
bool reverse() const
whether or not this read is from the reverse strand
Definition: sam.h:283
void set_alignment_start(const uint32_t start)
simple setter for the alignment start.
Definition: sam.h:260
#define BAM_FUNMAP
Definition: sam.h:107
void set_last()
Definition: sam.h:305
void set_supplementary()
Definition: sam.h:313
void set_not_reverse()
Definition: sam.h:300
Utility class to manipulate a Sam record.
Definition: sam.h:20
#define BAM_FREAD1
Definition: sam.h:115
void set_insert_size(const int32_t isize)
simple setter for the insert size
Definition: sam.h:264
void set_not_supplementary()
Definition: sam.h:314
Utility class to handle the memory management of the sam record object for read bases.
Definition: read_bases.h:26
void set_paired()
Definition: sam.h:293
SamTag< char > char_tag(const std::string &tag_name) const
retrieve a char-valued tag by name.
Definition: sam.cpp:165
#define BAM_FREVERSE
Definition: sam.h:111
bool mate_unmapped() const
whether or not the mate read is unmapped
Definition: sam.h:282
#define bam_get_qname(b)
Definition: sam.h:196
void set_chromosome(const uint32_t chr)
simple setter for the chromosome index. Index is 0-based.
Definition: sam.h:259
Utility class to manage the memory of the cigar structure.
Definition: cigar.h:24
void set_not_fail()
Definition: sam.h:310
void set_not_mate_reverse()
Definition: sam.h:302
ReadBases bases() const
returns the read bases.
Definition: sam.h:269
bool supplementary() const
whether or not this read is a supplementary alignment (see definition in the BAM spec) ...
Definition: sam.h:290
#define BAM_FDUP
Definition: sam.h:123
Cigar cigar() const
returns the cigar.
Definition: sam.h:268
void set_secondary()
Definition: sam.h:307
void set_unmapped()
Definition: sam.h:295
void set_not_mate_unmapped()
Definition: sam.h:298