This repository has been archived by the owner on Dec 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
translate_cigar.cpp
409 lines (357 loc) · 10 KB
/
translate_cigar.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "translate_cigar.h"
#include "bwtaln.h"
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdexcept>
using namespace std;
namespace {
const char* OPS = "MIDSN";
int cigar_len(bwa_cigar_t* cigar, int n) {
int len = 0;
for (int i = 0; i < n; ++i) {
unsigned short op = __cigar_op(cigar[i]);
if (op <= 1)
len += __cigar_len(cigar[i]);
}
return len;
}
}
struct CigarBuilder {
CigarBuilder()
: cigar(0)
, len(0)
, cap(0)
{
}
~CigarBuilder() {
if (cigar)
free(cigar);
}
bwa_cigar_t* release() {
bwa_cigar_t* rv = cigar;
cigar = 0;
return rv;
}
void push(char op, int len) {
push(__cigar_create(op, len));
}
void push(bwa_cigar_t cig) {
if (len && __cigar_op(cig) == __cigar_op(cigar[len-1])) {
int newlen = __cigar_len(cig)+__cigar_len(cigar[len-1]);
cigar[len-1] = __cigar_create(__cigar_op(cig), newlen);
} else {
if (cap == len) {
++cap;
cap *= 2;
cigar = (bwa_cigar_t*)realloc(cigar, cap*sizeof(bwa_cigar_t));
if (cigar == NULL) {
cerr << "CigarBuilder: out of memory.\n";
exit(1);
}
}
cigar[len++] = cig;
}
}
bwa_cigar_t* cigar;
int len;
int cap;
};
struct CigarTranslator {
CigarTranslator(
const char* seq_cigar,
uint32_t start_pos, // start pos on seq
bwa_cigar_t* read_cigar,
int n_cigar,
int total_read_len
)
: seq_cigar(seq_cigar)
, p_seq_cigar(seq_cigar)
, start_pos(start_pos)
, read_cigar(read_cigar)
, read_cigar_idx(0)
, n_cigar(n_cigar)
, total_read_len(total_read_len)
, cpos(0)
{
seq_advance();
read_advance();
}
int tr_seqop(char op) {
switch (op) {
case 'M': return 0; break;
case 'I': return 1; break;
case 'D': return 2; break;
case 'S': return 3; break;
case 'N': return 4; break;
default:
throw runtime_error(string("Unknown cigar operation: ") + op);
break;
}
}
void in_match() {
/*
if (seq_len >= read_len) {
cb.push(read_op, read_len);
seq_len -= read_len;
read_len = 0;
} else {
cb.push(read_op, seq_len);
read_len -= seq_len;
seq_len = 0;
}
*/
switch (OPS[read_op]) {
case 'M':
case 'N':
case 'D':
if (seq_len >= read_len) {
cb.push(read_op, read_len);
seq_len -= read_len;
read_len = 0;
} else {
cb.push(read_op, seq_len);
read_len -= seq_len;
seq_len = 0;
}
break;
case 'I':
cb.push(read_op, read_len);
read_len = 0;
break;
default:
throw runtime_error("Unknown cigar op in read");
break;
}
}
void in_insertion() {
switch (OPS[read_op]) {
case 'M':
if (seq_len < read_len) {
cb.push(1, seq_len);
read_len -= seq_len;
seq_len = 0;
} else {
cb.push(1, read_len);
seq_len -= read_len;
read_len = 0;
}
break;
case 'I':
cb.push(read_op, read_len);
read_len = 0;
break;
case 'N':
case 'D':
if (seq_len > read_len) {
seq_len -= read_len;
read_len = 0;
} else {
read_len -= seq_len;
seq_len = 0;
}
break;
default:
throw runtime_error("Unknown cigar op in read");
break;
}
}
void in_deletion() {
switch (OPS[read_op]) {
case 'M':
cb.push(tr_seqop(seq_op), seq_len);
seq_advance();
break;
case 'I':
cb.push(tr_seqop(seq_op), seq_len);
seq_advance();
cb.push(read_op, read_len);
read_advance();
break;
case 'N':
case 'D':
cb.push(tr_seqop(seq_op), seq_len);
seq_len = 0;
break;
default:
throw runtime_error("Unknown cigar op in read");
break;
}
}
void exec() {
find_start_pos();
if (read_cigar == NULL) {
int len = 0;
while (len < total_read_len && !eos()) {
int dist = total_read_len - len;
if (seq_len < dist) {
cb.push(tr_seqop(seq_op), seq_len);
len += seq_len;
seq_advance();
} else {
cb.push(tr_seqop(seq_op), dist);
break;
}
}
return;
}
while (!eor() && !eos()) {
if (seq_len == 0) seq_advance();
if (read_len == 0) read_advance();
if (OPS[read_op] == 'S') {
cb.push(read_op, read_len);
read_len = 0;
if (!eor())
read_advance();
continue;
}
switch (seq_op) {
case '=':
case 'M':
case 'X':
in_match();
break;
case 'I':
in_insertion();
break;
case 'N':
case 'D':
in_deletion();
break;
default:
throw runtime_error(string("Invalid cigar character: ") + seq_op);
}
}
while (!eor()) {
if (read_len == 0)
read_advance();
if (OPS[read_op] == 'M' || OPS[read_op] == 'I' || OPS[read_op] == 'S')
cb.push(tr_seqop('S'), read_len);
read_len = 0;
}
}
void find_start_pos() {
while (cpos < start_pos && !eos()) {
if (seq_len == 0) seq_advance();
int dist = start_pos - cpos;
switch (seq_op) {
case '=':
case 'M':
case 'X':
case 'I':
if (seq_len > dist) {
seq_len -= start_pos - cpos;
cpos = start_pos;
} else {
cpos += seq_len;
seq_len = 0;
}
break;
case 'N':
case 'D':
seq_len = 0;
break;
default:
throw runtime_error(string("Invalid cigar character: ") + seq_op);
break;
}
}
if (cpos < start_pos) {
stringstream ss;
ss << "Failed to seek to position " << start_pos << " in cigar string '" << seq_cigar << "'";
throw runtime_error(ss.str());
}
}
bool eos() const {
return seq_len == 0 && *p_seq_cigar == 0;
}
bool eor() const {
return read_len == 0 && read_cigar_idx >= n_cigar;
}
void seq_advance() {
char* end;
seq_len = strtoul(p_seq_cigar, &end, 10);
p_seq_cigar = end;
seq_op = *p_seq_cigar++;
}
void read_advance() {
if (!read_cigar)
return;
read_len = __cigar_len(read_cigar[read_cigar_idx]);
read_op = __cigar_op(read_cigar[read_cigar_idx++]);
}
const char* seq_cigar;
const char* p_seq_cigar;
uint32_t start_pos;
bwa_cigar_t* read_cigar;
int read_cigar_idx;
int n_cigar;
int total_read_len;
uint32_t cpos;
char seq_op;
int seq_len;
int read_op;
int read_len;
CigarBuilder cb;
};
bwa_cigar_t* translate_cigar(
const char* cigar,
uint32_t start,
bwa_cigar_t* read_cigar,
int n_cigar,
int read_len,
int* n_cigar_out)
{
CigarTranslator ct(cigar, start, read_cigar, n_cigar, read_len);
try {
ct.exec();
*n_cigar_out = ct.cb.len;
return ct.cb.release();
} catch (const exception& e) {
cerr << "Error translating cigar string: " << e.what() << "\n";
return NULL;
}
}
#if 0
void print_cigar(bwa_cigar_t* cigar, int len) {
for (int i = 0; i < len; ++i) {
cout << __cigar_len(cigar[i]) << OPS[__cigar_op(cigar[i])];
}
cout << "\n";
}
int main(int argc, char** argv) {
if (argc != 4) {
cerr << "Give 2 cigar strings, pos\n";
return 1;
}
uint32_t pos = atoi(argv[3]);
const char* seq_cigar = argv[1];
CigarBuilder cb;
const char* p = argv[2];
static char trans[256] = {0};
trans['M'] = 0;
trans['I'] = 1;
trans['D'] = 2;
trans['S'] = 3;
while (*p) {
char* end;
int len = strtoul(p, &end, 10);
p = end;
cb.push(trans[*p++], len);
}
print_cigar(cb.cigar, cb.len);
int retlen;
int len = cigar_len(cb.cigar, cb.len);
bwa_cigar_t* ret = translate_cigar(argv[1], pos, cb.cigar, cb.len, len, &retlen);
if (ret) {
cout << "RESULT: ";
print_cigar(ret, retlen);
} else {
cerr << "NO RET!\n";
}
ret = translate_cigar(argv[1], pos, NULL, 0, 10, &retlen);
cout << "RESULT2: ";
print_cigar(ret, retlen);
return 0;
}
#endif