Skip to content

Commit 8483985

Browse files
Adding the apply_cigar function to cigar_utils.hpp and cpp
1 parent d3908ad commit 8483985

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

cigar_utils.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Copyright (C) 2019 Andrew D. Smith
2+
*
3+
* Authors: Andrew D. Smith
4+
*
5+
* This is free software: you can redistribute it and/or modify it under the
6+
* terms of the GNU General Public License as published by the Free Software
7+
* Foundation, either version 3 of the License, or (at your option) any later
8+
* version.
9+
*
10+
* This software is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13+
* details.
14+
*/
15+
16+
#include "cigar_utils.hpp"
17+
18+
#include <exception>
19+
#include <sstream>
20+
#include <string>
21+
22+
using std::string;
23+
using std::to_string;
24+
using std::runtime_error;
25+
26+
void
27+
apply_cigar(const string &cigar, string &to_inflate,
28+
const char inflation_symbol) {
29+
std::istringstream iss(cigar);
30+
31+
string inflated_seq;
32+
size_t n;
33+
char op;
34+
size_t i = 0;
35+
auto to_inflate_beg = std::begin(to_inflate);
36+
while (iss >> n >> op) {
37+
if (consumes_reference(op) && consumes_query(op)) {
38+
inflated_seq.append(to_inflate_beg + i, to_inflate_beg + i + n);
39+
i += n;
40+
}
41+
else if (consumes_query(op)) {
42+
// no addition of symbols to query
43+
i += n;
44+
}
45+
else if (consumes_reference(op)) {
46+
inflated_seq.append(n, inflation_symbol);
47+
// no increment of index within query
48+
}
49+
}
50+
51+
// sum of total M/I/S/=/X/N operations must equal length of seq
52+
const size_t orig_len = to_inflate.length();
53+
if (i != orig_len)
54+
throw runtime_error("inconsistent number of qseq ops in cigar: " +
55+
to_inflate + " " + cigar + " " +
56+
to_string(i) + " " +
57+
to_string(orig_len));
58+
to_inflate.swap(inflated_seq);
59+
}

cigar_utils.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515

1616
#ifndef CIGAR_UTILS_HPP
1717
#define CIGAR_UTILS_HPP
18+
1819
#include <algorithm>
20+
#include <cctype> // isdigit
21+
#include <string>
1922

20-
bool
23+
inline bool
2124
consumes_query(const char op) {
2225
return ((op == 'M') ||
2326
(op == 'I') ||
@@ -26,7 +29,7 @@ consumes_query(const char op) {
2629
(op == 'X'));
2730
}
2831

29-
bool
32+
inline bool
3033
consumes_reference(const char op) {
3134
return ((op == 'M') ||
3235
(op == 'D') ||
@@ -330,4 +333,8 @@ uncompress_cigar(T1 c_itr, const T1 c_end, T2 &cigar) {
330333
}
331334
}
332335

336+
void
337+
apply_cigar(const std::string &cigar, std::string &to_inflate,
338+
const char inflation_symbol = 'N');
339+
333340
#endif

0 commit comments

Comments
 (0)