|
| 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 | +} |
0 commit comments