forked from smithlabcode/smithlab_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcigar_utils.hpp
More file actions
338 lines (304 loc) · 9.51 KB
/
cigar_utils.hpp
File metadata and controls
338 lines (304 loc) · 9.51 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* Copyright (C) 2019 Andrew D. Smith
*
* Authors: Andrew D. Smith
*
* This is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*/
#ifndef CIGAR_UTILS_HPP
#define CIGAR_UTILS_HPP
#include <algorithm>
#include <cctype> // isdigit
#include <string>
inline bool
consumes_query(const char op) {
return ((op == 'M') ||
(op == 'I') ||
(op == 'S') ||
(op == '=') ||
(op == 'X'));
}
inline bool
consumes_reference(const char op) {
return ((op == 'M') ||
(op == 'D') ||
(op == 'N') ||
(op == '=') ||
(op == 'X'));
}
template <class InItr>
static size_t
extract_op_count(InItr &itr, const InItr last) {
size_t x = 0;
while (itr != last && std::isdigit(*itr))
x = x*10 + (*itr++ - '0');
return x;
}
template <class InputItr>
size_t
cigar_total_ops(InputItr itr, const InputItr last) {
size_t op_count = 0;
while (itr != last) {
op_count += extract_op_count(itr, last);
if (itr != last) *itr++; // eat the op code
}
return op_count;
}
template <class InputItr>
size_t
cigar_qseq_ops(InputItr itr, const InputItr last) {
size_t op_count = 0;
while (itr != last) {
const size_t curr_val = extract_op_count(itr, last);
op_count += curr_val*consumes_query(*itr++);
}
return op_count;
}
template <class T>
size_t
cigar_qseq_ops(const T &cigar) {
return cigar_qseq_ops(std::begin(cigar), std::end(cigar));
}
template <class InputItr>
size_t
cigar_rseq_ops(InputItr itr, const InputItr last) {
size_t op_count = 0;
while (itr != last) {
const size_t curr_val = extract_op_count(itr, last);
op_count += curr_val*consumes_reference(*itr++);
}
return op_count;
}
template <class T> constexpr
size_t
cigar_rseq_ops(const T &cigar) {
return cigar_rseq_ops(std::begin(cigar), std::end(cigar));
}
template <class InputItr>
void
reverse_cigar(InputItr left, const InputItr last) {
std::reverse(left, last);
InputItr right(left);
while (left != last) {
right = std::find_if(++right, last, // ++right eats op in rev orientation
[](const char c) {return !std::isdigit(c);});
std::reverse(left, right);
left = right;
}
}
template <class T>
void
reverse_cigar(T &cigar) {
reverse_cigar(std::begin(cigar), std::end(cigar));
}
template <class InputItr>
InputItr
truncate_cigar(InputItr left, const InputItr last, const size_t target_ops) {
/* truncate cigar after specified number of total operations */
InputItr right(left);
size_t prev_ops = 0, curr_ops = 0;
char op = '\0';
while (left != last && prev_ops + curr_ops <= target_ops) {
left = right;
prev_ops += curr_ops;
curr_ops = extract_op_count(right, last);
op = *right++; // cigar always ends with an op
}
if (left != last && target_ops > prev_ops)
left += snprintf(&(*left), 16, "%lu%c", target_ops - prev_ops, op);
return left;
}
template <class InputItr>
InputItr
truncate_cigar_q(InputItr left, const InputItr last, const size_t target_ops) {
/* truncate cigar after specified number of query operations */
InputItr right(left);
size_t prev_ops = 0, curr_ops = 0;
char op = '\0';
while (left != last && prev_ops + curr_ops <= target_ops) {
left = right;
prev_ops += curr_ops;
curr_ops = extract_op_count(right, last);
op = *right++; // cigar always ends with an op
if (!consumes_query(op)) curr_ops = 0;
}
if (left != last && target_ops > prev_ops)
left += snprintf(&(*left), 16, "%lu%c", target_ops - prev_ops, op);
return left;
}
template <class T>
void
truncate_cigar_q(T &cigar, const size_t target_ops) {
/* truncate cigar after specified number of query operations */
auto c_beg(std::begin(cigar));
auto updated_end = truncate_cigar_q(c_beg, std::end(cigar), target_ops);
cigar.resize(std::distance(c_beg, updated_end));
}
template <class InputItr>
InputItr
truncate_cigar_r(InputItr left, const InputItr last, const size_t target_ops) {
/* truncate cigar after specified number of reference operations */
InputItr right(left);
size_t prev_ops = 0, curr_ops = 0;
char op = '\0';
while (left != last && prev_ops + curr_ops <= target_ops) {
left = right;
prev_ops += curr_ops;
curr_ops = extract_op_count(right, last);
op = *right++; // cigar always ends with an op
if (!consumes_reference(op)) curr_ops = 0;
}
if (left != last && target_ops > prev_ops)
left += snprintf(&(*left), 16, "%lu%c", target_ops - prev_ops, op);
return left;
}
template <class T>
void
truncate_cigar_r(T &cigar, const size_t target_ops) {
/* truncate cigar after specified number of reference operations */
auto c_beg(std::begin(cigar));
auto updated_end = truncate_cigar_r(c_beg, std::end(cigar), target_ops);
cigar.resize(std::distance(c_beg, updated_end));
}
template <class InputItr>
InputItr
merge_equal_neighbor_cigar_ops(InputItr rd_itr, const InputItr last) {
/* renders a cigar valid by merging consecutive identical operations */
InputItr wr_itr(rd_itr);
size_t prev_op_count = extract_op_count(rd_itr, last);
char prev_op = *rd_itr++;
while (rd_itr != last) {
const size_t curr_op_count = extract_op_count(rd_itr, last);
const char op = *rd_itr++;
if (op != prev_op) {
wr_itr += snprintf(&(*wr_itr), 16, "%lu%c", prev_op_count, prev_op);
prev_op_count = 0;
}
prev_op_count += curr_op_count;
prev_op = op;
}
wr_itr += snprintf(&(*wr_itr), 16, "%lu%c", prev_op_count, prev_op);
return wr_itr;
}
template <class T>
void
merge_equal_neighbor_cigar_ops(T &cigar) {
/* renders a cigar valid by merging consecutive identical operations */
auto c_beg(std::begin(cigar));
auto updated_end = merge_equal_neighbor_cigar_ops(c_beg, std::end(cigar));
cigar.resize(std::distance(c_beg, updated_end));
}
template <class InputItr>
void
internal_S_to_M(InputItr rd_itr, const InputItr last) {
/* converts internal soft-clip (S) symbols into match/mismatch (M) symbols */
extract_op_count(rd_itr, last); // move past first op count
rd_itr++; // move past first op
while (rd_itr != last) {
extract_op_count(rd_itr, last); // past curr op count
auto curr_op = rd_itr++; // save curr op iterator
if (rd_itr != last && *curr_op == 'S') {
*curr_op = 'M';
}
}
}
template <class T>
void
internal_S_to_M(T &cigar) {
/* converts internal soft-clip (S) symbols into match/mismatch (M) symbols */
internal_S_to_M(std::begin(cigar), std::end(cigar));
}
template <class T>
void
terminal_S_to_M(T &cigar) {
/* converts a terminal soft-clip symbol to a match/mismatch (M) symbol */
if (cigar.back() == 'S') cigar.back() = 'M';
}
template <class InputItr>
void
initial_S_to_M(InputItr rd_itr, const InputItr last) {
/* converts an initial soft-clip symbol to a match/mismatch (M) symbol */
extract_op_count(rd_itr, last);
if (*rd_itr == 'S')
*rd_itr = 'M';
}
template <class T>
void
initial_S_to_M(T &cigar) {
/* converts an initial soft-clip symbol to a match/mismatch (M) symbol */
initial_S_to_M(std::begin(cigar), std::end(cigar));
}
template <class InputItr>
size_t
get_soft_clip_size(InputItr rd_itr, const InputItr last) {
/* determine the total size of the soft-clips at both ends */
size_t r = 0;
size_t curr_op_count = extract_op_count(rd_itr, last);
if (*rd_itr++ == 'S')
r += curr_op_count;
char curr_op = '\0';
while (rd_itr != last) {
curr_op_count = extract_op_count(rd_itr, last);
curr_op = *rd_itr++;
}
return (curr_op == 'S') ? r + curr_op_count : r;
}
template <class T> constexpr
size_t
get_soft_clip_size(T &cigar) {
/* determine the total size of the soft-clips at both ends */
return get_soft_clip_size(std::begin(cigar), std::end(cigar));
}
template <class InputItr>
size_t
get_soft_clip_size_start(InputItr rd_itr, const InputItr last) {
/* determine the size of the soft-clip at the start of cigar */
const size_t curr_op_count = extract_op_count(rd_itr, last);
return (*rd_itr == 'S') ? curr_op_count : 0;
}
template <class T> constexpr
size_t
get_soft_clip_size_start(T &cigar) {
/* determine the size of the soft-clip at the start of cigar */
return get_soft_clip_size_start(std::begin(cigar), std::end(cigar));
}
// turns cigar symbols into compressed format
template <class T1, class T2>
void
compress_cigar(T1 c_itr, const T1 c_end, T2 &cigar) {
/* convert a sequence of cigar symbols into a valid cigar string */
const size_t N = cigar.size();
unsigned j = 0, n = 0;
char op = *c_itr;
for (; c_itr != c_end; ++c_itr, ++n) {
if (*c_itr != op) {
j += snprintf(&cigar[j], N - j, "%d%c", n, op);
op = *c_itr;
n = 0;
}
}
j += snprintf(&cigar[j], N - j, "%d%c", n, op);
cigar.resize(j);
}
// turns a cigar string into a string of cigar symbols
template <class T1, class T2>
void
uncompress_cigar(T1 c_itr, const T1 c_end, T2 &cigar) {
/* convert a valid cigar string into a sequence of cigar symbols */
cigar.clear(); // ADS: should this be assumed?
while (c_itr != c_end) {
const size_t op_count = extract_op_count(c_itr, c_end);
fill_n(std::back_inserter(cigar), op_count, *c_itr++);
}
}
void
apply_cigar(const std::string &cigar, std::string &to_inflate,
const char inflation_symbol = 'N');
#endif