-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcigar_utils.cpp
57 lines (51 loc) · 1.68 KB
/
cigar_utils.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
/* 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.
*/
#include "cigar_utils.hpp"
#include <exception>
#include <sstream>
#include <string>
using std::runtime_error;
using std::string;
using std::to_string;
void apply_cigar(const string &cigar, string &to_inflate,
const char inflation_symbol) {
std::istringstream iss(cigar);
string inflated_seq;
size_t n;
char op;
size_t i = 0;
auto to_inflate_beg = std::begin(to_inflate);
while (iss >> n >> op) {
if (consumes_reference(op) && consumes_query(op)) {
inflated_seq.append(to_inflate_beg + i, to_inflate_beg + i + n);
i += n;
}
else if (consumes_query(op)) {
// no addition of symbols to query
i += n;
}
else if (consumes_reference(op)) {
inflated_seq.append(n, inflation_symbol);
// no increment of index within query
}
}
// sum of total M/I/S/=/X/N operations must equal length of seq
const size_t orig_len = to_inflate.length();
if (i != orig_len)
throw runtime_error(
"inconsistent number of qseq ops in cigar: " + to_inflate + " " +
cigar + " " + to_string(i) + " " + to_string(orig_len));
to_inflate.swap(inflated_seq);
}