This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
/
sam.cpp
201 lines (177 loc) · 7.23 KB
/
sam.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "sam.h"
#include "cigar.h"
#include "utils/hts_memory.h"
#include "htslib/sam.h"
#include "missing.h"
#include <iostream>
#include <string>
using namespace std;
namespace gamgee {
constexpr auto MATE_CIGAR_TAG = "MC";
/**
* @brief creates a sam record that points to htslib memory already allocated
*
* @note the resulting Sam shares ownership of the pre-allocated memory via shared_ptr
* reference counting
*/
Sam::Sam(const std::shared_ptr<bam_hdr_t>& header, const std::shared_ptr<bam1_t>& body) noexcept :
m_header {header},
m_body {body}
{}
/**
* @brief creates a deep copy of a sam record
*
* @note the copy will have exclusive ownership over the newly-allocated htslib memory
* until a data field (cigar, bases, etc.) is accessed, after which it will be
* shared via reference counting with the Cigar, etc. objects
* @note does not perform a deep copy of the sam header; to copy the header,
* first get it via the header() function and then copy it via the usual C++
* semantics
*/
Sam::Sam(const Sam& other) :
m_header { other.m_header },
m_body { utils::make_shared_sam(utils::sam_deep_copy(other.m_body.get())) }
{}
/**
* @brief moves a sam record, transferring ownership of the underlying htslib memory
*/
Sam::Sam(Sam&& other) noexcept :
m_header { move(other.m_header) },
m_body { move(other.m_body) }
{}
/**
* @brief creates a deep copy of a sam record
*
* @note the copy will have exclusive ownership over the newly-allocated htslib memory
* until a data field (cigar, bases, etc.) is accessed, after which it will be
* shared via reference counting with the Cigar, etc. objects
* @note does not perform a deep copy of the sam header; to copy the header,
* first get it via the header() function and then copy it via the usual C++
* semantics
*/
Sam& Sam::operator=(const Sam& other) {
if ( &other == this )
return *this;
m_header = other.m_header; ///< shared_ptr assignment will take care of deallocating old sam record if necessary
m_body = utils::make_shared_sam(utils::sam_deep_copy(other.m_body.get())); ///< shared_ptr assignment will take care of deallocating old sam record if necessary
return *this;
}
/**
* @brief moves a sam record, transferring ownership of the underlying htslib memory
*/
Sam& Sam::operator=(Sam&& other) noexcept {
if ( &other == this )
return *this;
m_header = move(other.m_header); ///< shared_ptr assignment will take care of decrementing the reference count for the old managed object (and destroying it if necessary)
m_body = move(other.m_body); ///< shared_ptr assignment will take care of decrementing the reference count for the old managed object (and destroying it if necessary)
return *this;
}
uint32_t Sam::mate_alignment_stop() const {
auto result = mate_alignment_start();
const auto mate_cigar = string_tag(MATE_CIGAR_TAG);
if (missing(mate_cigar))
throw std::invalid_argument{string{"Cannot find the mate alignment stop on a record without the tag: "} + MATE_CIGAR_TAG};
stringstream cigar_stream {mate_cigar.value()};
while (cigar_stream.peek() != std::char_traits<char>::eof()) {
const auto element = Cigar::parse_next_cigar_element(cigar_stream);
result += Cigar::consumes_reference_bases(Cigar::cigar_op(element)) ? Cigar::cigar_oplen(element) : 0;
}
return result-1; // we want the last base to be 1-based and **inclusive**
}
/**
* @brief calculates the theoretical alignment start of a read that has soft/hard-clips preceding the alignment
*
* @return the alignment start (1-based, inclusive) adjusted for clipped bases. For example if the read
* has an alignment start of 100 but the first 4 bases were clipped (hard or soft clipped)
* then this method will return 96.
*
* Invalid to call on an unmapped read.
* Invalid to call with cigar = null
*/
uint32_t Sam::unclipped_start() const {
auto pos = alignment_start();
const auto* cigar = bam_get_cigar(m_body.get());
for (auto i = 0u; i != m_body->core.n_cigar; ++i) {
const auto op = bam_cigar_op(cigar[i]);
if (op == BAM_CSOFT_CLIP || op == BAM_CHARD_CLIP)
pos -= bam_cigar_oplen(cigar[i]);
else
break;
}
return pos;
}
/**
* @brief calculates the theoretical alignment stop of a read that has soft/hard-clips preceding the alignment
*
* @return the alignment stop (1-based, inclusive) adjusted for clipped bases. For example if the read
* has an alignment stop of 100 but the last 4 bases were clipped (hard or soft clipped)
* then this method will return 104.
*
* Invalid to call on an unmapped read.
* Invalid to call with cigar = null
*/
uint32_t Sam::unclipped_stop() const {
auto pos = alignment_stop();
const auto* cigar = bam_get_cigar(m_body.get());
for (auto i = int{m_body->core.n_cigar - 1}; i >= 0; --i) {
const auto op = bam_cigar_op(cigar[i]);
if (op == BAM_CSOFT_CLIP || op == BAM_CHARD_CLIP)
pos += bam_cigar_oplen(cigar[i]);
else
break;
}
return pos;
}
/**
* @brief retrieve an integer-valued tag by name
*
* @note returns a SamTag with missing() == true if the read has no tag by this name
*/
SamTag<int32_t> Sam::integer_tag(const std::string& tag_name) const {
const auto aux_ptr = bam_aux_get(m_body.get(), tag_name.c_str());
return aux_ptr == nullptr ? SamTag<int32_t>(tag_name, 0, true) :
SamTag<int32_t>(tag_name, bam_aux2i(aux_ptr));
// TODO: htslib bam_aux2i() returns 0 if the tag is not of integer type,
// but 0 is a valid value for a correctly-typed tag. This should be patched.
}
/**
* @brief retrieve a double/float-valued tag by name
*
* @note returns a SamTag with missing() == true if the read has no tag by this name
*/
SamTag<double> Sam::double_tag(const std::string& tag_name) const {
const auto aux_ptr = bam_aux_get(m_body.get(), tag_name.c_str());
return aux_ptr == nullptr ? SamTag<double>(tag_name, 0.0, true) :
SamTag<double>(tag_name, bam_aux2f(aux_ptr));
// TODO: htslib bam_aux2f() returns 0.0 if the tag is not of double/float type,
// but 0.0 is a valid value for a correctly-typed tag. This should be patched.
}
/**
* @brief retrieve a char-valued tag by name
*
* @note returns a SamTag with missing() == true if the read has no tag by this name
*/
SamTag<char> Sam::char_tag(const std::string& tag_name) const {
const auto aux_ptr = bam_aux_get(m_body.get(), tag_name.c_str());
if ( aux_ptr == nullptr ) // tag doesn't exist
return SamTag<char>(tag_name, '\0', true);
const auto char_val = bam_aux2A(aux_ptr);
if ( char_val == '\0' ) // tag not of char type
return SamTag<char>(tag_name, '\0', true);
return SamTag<char>(tag_name, char_val);
}
/**
* @brief retrieve a string-valued tag by name
*
* @note returns a SamTag with missing() == true if the read has no tag by this name
*/
SamTag<std::string> Sam::string_tag(const std::string& tag_name) const {
const auto aux_ptr = bam_aux_get(m_body.get(), tag_name.c_str());
if ( aux_ptr == nullptr ) // tag doesn't exist
return SamTag<string>(tag_name, "", true);
const auto str_ptr = bam_aux2Z(aux_ptr);
if ( str_ptr == nullptr ) // tag not of string type
return SamTag<string>(tag_name, "", true);
return SamTag<string>(tag_name, string{str_ptr});
}
}