Gamgee
You miserable little maggot. I'll stove your head in!
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
sam.h
Go to the documentation of this file.
1 #ifndef __gamgee__sam__
2 #define __gamgee__sam__
3 
4 #include "sam_header.h"
5 #include "read_bases.h"
6 #include "base_quals.h"
7 #include "cigar.h"
8 
9 #include "htslib/sam.h"
10 
11 #include <string>
12 #include <memory>
13 
14 namespace gamgee {
15 
19 class Sam {
20  public:
21  Sam() = default;
22  explicit Sam(const std::shared_ptr<bam_hdr_t>& header, const std::shared_ptr<bam1_t>& body) noexcept;
23  Sam(const Sam& other);
24  Sam(Sam&& other) noexcept;
25  Sam& operator=(const Sam& other);
26  Sam& operator=(Sam&& other) noexcept;
27 
28  SamHeader header() { return SamHeader{m_header}; }
29 
30  // getters for non-variable length fields (things outside of the data member)
31  uint32_t chromosome() const { return uint32_t(m_body->core.tid); }
32  uint32_t alignment_start() const { return uint32_t(m_body->core.pos+1); }
33  uint32_t alignment_stop() const { return uint32_t(bam_endpos(m_body.get())+1); }
34  uint32_t unclipped_start() const ;
35  uint32_t unclipped_stop() const ;
36  uint32_t mate_chromosome() const { return uint32_t(m_body->core.mtid); }
37  uint32_t mate_alignment_start() const { return uint32_t(m_body->core.mpos+1); }
38  uint32_t mate_alignment_stop() const ;
39  uint32_t mate_unclipped_start() const { return alignment_start(); }
40  uint32_t mate_unclipped_stop() const ;
41 
42  // modify non-variable length fields (things outside of the data member)
43  void set_chromosome(const uint32_t chr) { m_body->core.tid = int32_t(chr); }
44  void set_alignment_start(const uint32_t start) { m_body->core.pos = int32_t(start-1); }
45  void set_mate_chromosome(const uint32_t mchr) { m_body->core.mtid = int32_t(mchr); }
46  void set_mate_alignment_start(const uint32_t mstart) { m_body->core.mpos = int32_t(mstart - 1); }
47 
48  // getters for fields inside the data field
49  std::string name() const { return std::string{bam_get_qname(m_body.get())}; }
50  ReadBases bases() const { return ReadBases{m_body}; }
51  BaseQuals base_quals() const { return BaseQuals{m_body}; }
52  Cigar cigar() const { return Cigar{m_body}; }
53 
54  // getters for flags
55  bool paired() const { return m_body->core.flag & BAM_FPAIRED; }
56  bool properly_paired() const { return m_body->core.flag & BAM_FPROPER_PAIR; }
57  bool unmapped() const { return m_body->core.flag & BAM_FUNMAP; }
58  bool next_unmapped() const { return m_body->core.flag & BAM_FMUNMAP; }
59  bool reverse() const { return m_body->core.flag & BAM_FREVERSE; }
60  bool next_reverse() const { return m_body->core.flag & BAM_FMREVERSE; }
61  bool first() const { return m_body->core.flag & BAM_FREAD1; }
62  bool last() const { return m_body->core.flag & BAM_FREAD2; }
63  bool secondary() const { return m_body->core.flag & BAM_FSECONDARY; }
64  bool fail() const { return m_body->core.flag & BAM_FQCFAIL; }
65  bool duplicate() const { return m_body->core.flag & BAM_FDUP; }
66  bool supplementary() const { return m_body->core.flag & BAM_FSUPPLEMENTARY; }
67 
68  // modify flags
69  void set_paired() { m_body->core.flag |= BAM_FPAIRED; }
70  void set_not_paired() { m_body->core.flag &= ~BAM_FPAIRED; }
71  void set_unmapped() { m_body->core.flag |= BAM_FUNMAP; }
72  void set_not_unmapped() { m_body->core.flag &= ~BAM_FUNMAP; }
73  void set_next_unmapped() { m_body->core.flag |= BAM_FMUNMAP; }
74  void set_not_next_unmapped() { m_body->core.flag &= ~BAM_FMUNMAP; }
75  void set_reverse() { m_body->core.flag |= BAM_FREVERSE; }
76  void set_not_reverse() { m_body->core.flag &= ~BAM_FREVERSE; }
77  void set_next_reverse() { m_body->core.flag |= BAM_FMREVERSE; }
78  void set_not_next_reverse() { m_body->core.flag &= ~BAM_FMREVERSE; }
79  void set_first() { m_body->core.flag |= BAM_FREAD1; }
80  void set_not_first() { m_body->core.flag &= ~BAM_FREAD1; }
81  void set_last() { m_body->core.flag |= BAM_FREAD2; }
82  void set_not_last() { m_body->core.flag &= ~BAM_FREAD2; }
83  void set_secondary() { m_body->core.flag |= BAM_FSECONDARY; }
84  void set_not_secondary() { m_body->core.flag &= ~BAM_FSECONDARY; }
85  void set_fail() { m_body->core.flag |= BAM_FQCFAIL; }
86  void set_not_fail() { m_body->core.flag &= ~BAM_FQCFAIL; }
87  void set_duplicate() { m_body->core.flag |= BAM_FDUP; }
88  void set_not_duplicate() { m_body->core.flag &= ~BAM_FDUP; }
89  void set_supplementary() { m_body->core.flag |= BAM_FSUPPLEMENTARY; }
90  void set_not_supplementary() { m_body->core.flag &= ~BAM_FSUPPLEMENTARY; }
91 
92  bool empty() const { return m_body == nullptr; }
93 
94  private:
95  std::shared_ptr<bam_hdr_t> m_header;
96  std::shared_ptr<bam1_t> m_body;
97 
98  friend class SamWriter;
99 };
100 
101 } // end of namespace
102 
103 #endif /* defined(__gamgee__sam__) */
uint32_t alignment_stop() const
returns a (1-based and inclusive) alignment stop position (as you would see in a Sam file)...
Definition: sam.h:33
void set_first()
Definition: sam.h:79
uint32_t mate_unclipped_stop() const
void set_not_paired()
Definition: sam.h:70
Utility class to hold the header of a sam file.
Definition: sam_header.h:13
void set_not_first()
Definition: sam.h:80
void set_duplicate()
Definition: sam.h:87
bool next_reverse() const
whether or not the next read is from the reverse strand
Definition: sam.h:60
uint32_t mate_unclipped_start() const
Definition: sam.h:39
uint32_t alignment_start() const
returns a (1-based and inclusive) alignment start position (as you would see in a Sam file)...
Definition: sam.h:32
void set_reverse()
Definition: sam.h:75
BaseQuals base_quals() const
returns the base qualities
Definition: sam.h:51
bool empty() const
whether or not this Sam object is empty, meaning that the internal memory has not been initialized (i...
Definition: sam.h:92
uint32_t mate_chromosome() const
returns the integer representation of the mate's chromosome. Notice that chromosomes are listed in in...
Definition: sam.h:36
uint32_t chromosome() const
returns the integer representation of the chromosome. Notice that chromosomes are listed in index ord...
Definition: sam.h:31
void set_not_last()
Definition: sam.h:82
bool paired() const
whether or not this read is paired
Definition: sam.h:55
SamHeader header()
Definition: sam.h:28
void set_not_duplicate()
Definition: sam.h:88
bool last() const
whether or not this read is the last read in a pair (or multiple pairs)
Definition: sam.h:62
bool secondary() const
whether or not this read is a secondary alignment (see definition in BAM spec)
Definition: sam.h:63
void set_fail()
Definition: sam.h:85
bool fail() const
whether or not this read is marked as failing vendor (sequencer) quality control
Definition: sam.h:64
utility class to write out a SAM/BAM/CRAM file to any stream
Definition: sam_writer.h:16
uint32_t unclipped_stop() const
calculates the theoretical alignment stop of a read that has soft/hard-clips preceding the alignment ...
Definition: sam.cpp:101
Sam & operator=(const Sam &other)
deep copy assignment of a Sam and it's header. Shared pointers maintain state to all other associated...
Definition: sam.cpp:49
bool next_unmapped() const
whether or not the next read is unmapped
Definition: sam.h:58
Utility class to handle the memory management of the sam record object for a read base qualities...
Definition: base_quals.h:13
bool first() const
whether or not this read is the first read in a pair (or multiple pairs)
Definition: sam.h:61
void set_mate_alignment_start(const uint32_t mstart)
simple setter for the mate's alignment start.
Definition: sam.h:46
uint32_t unclipped_start() const
calculates the theoretical alignment start of a read that has soft/hard-clips preceding the alignment...
Definition: sam.cpp:78
bool duplicate() const
whether or not this read is a duplicate
Definition: sam.h:65
bool unmapped() const
whether or not this read is unmapped
Definition: sam.h:57
uint32_t mate_alignment_stop() const
returns a (1-based and inclusive) mate'salignment stop position (as you would see in a Sam file)...
bool properly_paired() const
whether or not this read is properly paired (see definition in BAM spec)
Definition: sam.h:56
void set_next_unmapped()
Definition: sam.h:73
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:37
void set_not_secondary()
Definition: sam.h:84
std::string name() const
returns the read name
Definition: sam.h:49
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:45
void set_not_next_reverse()
Definition: sam.h:78
void set_not_unmapped()
Definition: sam.h:72
bool reverse() const
whether or not this read is from the reverse strand
Definition: sam.h:59
void set_alignment_start(const uint32_t start)
simple setter for the alignment start.
Definition: sam.h:44
void set_last()
Definition: sam.h:81
void set_supplementary()
Definition: sam.h:89
void set_next_reverse()
Definition: sam.h:77
void set_not_reverse()
Definition: sam.h:76
void set_not_next_unmapped()
Definition: sam.h:74
Utility class to manipulate a Sam record.
Definition: sam.h:19
void set_not_supplementary()
Definition: sam.h:90
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:69
void set_chromosome(const uint32_t chr)
simple setter for the chromosome index. Index is 0-based.
Definition: sam.h:43
Utility class to manage the memory of the cigar structure.
Definition: cigar.h:20
void set_not_fail()
Definition: sam.h:86
ReadBases bases() const
returns the read bases
Definition: sam.h:50
bool supplementary() const
whether or not this read is a supplementary alignment (see definition in the BAM spec) ...
Definition: sam.h:66
Cigar cigar() const
returns the cigar
Definition: sam.h:52
void set_secondary()
Definition: sam.h:83
void set_unmapped()
Definition: sam.h:71