-
Notifications
You must be signed in to change notification settings - Fork 294
/
read_aligner.cc
525 lines (466 loc) · 16.3 KB
/
read_aligner.cc
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//
// This file is part of khmer, http://github.com/ged-lab/khmer/, and is
// Copyright (C) Michigan State University, 2009-2013. It is licensed under
// the three-clause BSD license; see doc/LICENSE.txt. Contact: ctb@msu.edu
//
#include "read_aligner.hh"
#include "khmer_exception.hh"
namespace khmer
{
struct del_alignment_node_t {
del_alignment_node_t& operator()(AlignmentNode* p)
{
delete p;
return *this;
}
};
del_alignment_node_t del_alignment_node()
{
return del_alignment_node_t();
}
/*
Compute the null model (random sequence) log odds probability
for a given length
*/
double GetNull(size_t length)
{
return log2(.25) * length + log2(1.0 / (length + 1));
}
/*
Turn two states in to a transition, or disallowed if the
transition isn't modeled
*/
Transition get_trans(State s1, State s2)
{
if (s1 == MATCH) {
if (s2 == MATCH) {
return MM;
} else if (s2 == INSERT_GRAPH) {
return MIg;
} else if (s2 == INSERT_READ) {
return MIr;
} else if (s2 == MATCH_UNTRUSTED) {
return MMu;
} else if (s2 == INSERT_GRAPH_UNTRUSTED) {
return MIgu;
} else if (s2 == INSERT_READ_UNTRUSTED) {
return MIru;
}
} else if (s1 == INSERT_GRAPH) {
if (s2 == MATCH) {
return IgM;
} else if (s2 == INSERT_GRAPH) {
return IgIg;
} else if (s2 == MATCH_UNTRUSTED) {
return IgMu;
} else if (s2 == INSERT_GRAPH_UNTRUSTED) {
return IgIgu;
}
} else if (s1 == INSERT_READ) {
if(s2 == MATCH) {
return IrM;
} else if (s2 == INSERT_READ) {
return IrIr;
} else if(s2 == MATCH_UNTRUSTED) {
return IrMu;
} else if (s2 == INSERT_READ_UNTRUSTED) {
return IrIru;
}
} else if (s1 == MATCH_UNTRUSTED) {
if (s2 == MATCH) {
return MuM;
} else if (s2 == INSERT_GRAPH) {
return MuIg;
} else if (s2 == INSERT_READ) {
return MuIr;
} else if (s2 == MATCH_UNTRUSTED) {
return MuMu;
} else if (s2 == INSERT_GRAPH_UNTRUSTED) {
return MuIgu;
} else if (s2 == INSERT_READ_UNTRUSTED) {
return MuIru;
}
} else if (s1 == INSERT_GRAPH_UNTRUSTED) {
if (s2 == MATCH) {
return IguM;
} else if (s2 == INSERT_GRAPH) {
return IguIg;
} else if (s2 == MATCH_UNTRUSTED) {
return IguMu;
} else if (s2 == INSERT_GRAPH_UNTRUSTED) {
return IguIgu;
}
} else if (s1 == INSERT_READ_UNTRUSTED) {
if(s2 == MATCH) {
return IruM;
} else if (s2 == INSERT_READ) {
return IruIr;
} else if(s2 == MATCH_UNTRUSTED) {
return IruMu;
} else if (s2 == INSERT_READ_UNTRUSTED) {
return IruIru;
}
}
return disallowed;
}
void ReadAligner::Enumerate(
NodeHeap& open,
std::vector<AlignmentNode*>& all_nodes,
AlignmentNode* curr,
bool forward,
const std::string& seq
)
{
size_t next_seq_idx;
size_t remaining;
double hcost;
double sc;
Transition trans;
HashIntoType fwd = curr->fwd_hash;
HashIntoType rc = curr->rc_hash;
HashIntoType next_fwd, next_rc;
AlignmentNode* next;
if (forward) {
next_seq_idx = curr->seq_idx + 1;
remaining = seq.size() - next_seq_idx;
if (next_seq_idx >= seq.size()) {
return;
}
} else {
next_seq_idx = curr->seq_idx - 1;
remaining = next_seq_idx;
}
double match_sc;
double mismatch_sc;
int start_state;
int end_state;
// loop for MATCHes and INSERT_READs
for (int i = A; i <= T; i++) {
unsigned char next_nucl = nucl_lookup[i];
if(forward) {
next_fwd = next_f(fwd, next_nucl);
next_rc = next_r(rc, next_nucl);
} else {
next_fwd = prev_f(fwd, next_nucl);
next_rc = prev_r(rc, next_nucl);
}
HashIntoType hash = uniqify_rc(next_fwd, next_rc);
BoundedCounterType kmerCov = m_ch->get_count(hash);
if (kmerCov == 0) {
continue;
} else if (kmerCov < m_trusted_cutoff) {
start_state = MATCH_UNTRUSTED;
end_state = INSERT_GRAPH_UNTRUSTED;
//match_sc = m_sm.untrusted_match;
//mismatch_sc = m_sm.untrusted_mismatch;
match_sc = m_sm.trusted_match;
mismatch_sc = m_sm.trusted_mismatch;
} else {
start_state = MATCH;
end_state = INSERT_GRAPH;
match_sc = m_sm.trusted_match;
mismatch_sc = m_sm.trusted_mismatch;
}
for(int next_state_iter = start_state;
next_state_iter <= end_state;
next_state_iter++) {
State next_state = static_cast<State>(next_state_iter);
trans = get_trans(curr->state, next_state);
hcost = m_sm.tsc[get_trans(next_state, MATCH)]
+ (m_sm.tsc[MM] + m_sm.trusted_match)
* ((remaining == 0) ?
0 : (remaining - 1));
if(trans == disallowed) {
continue;
}
if(next_state == MATCH || next_state == MATCH_UNTRUSTED) {
if(next_nucl == seq[next_seq_idx]) {
sc = match_sc;
} else {
sc = mismatch_sc;
}
} else {
sc = background_prob;
}
if(next_state == MATCH || next_state == MATCH_UNTRUSTED) {
next = new AlignmentNode(curr, (Nucl)i,
next_seq_idx, (State)next_state, trans,
next_fwd, next_rc, curr->length + 1);
next->num_indels = curr->num_indels;
} else if(next_state == INSERT_READ || next_state == INSERT_READ_UNTRUSTED) {
next = new AlignmentNode(curr, (Nucl)i,
next_seq_idx, (State)next_state, trans,
curr->fwd_hash, curr->rc_hash,
curr->length + 1);
next->num_indels = curr->num_indels + 1;
} else if(next_state == INSERT_GRAPH || next_state == INSERT_GRAPH_UNTRUSTED) {
next = new AlignmentNode(curr, (Nucl)i,
curr->seq_idx, (State)next_state, trans,
next_fwd, next_rc, curr->length);
next->num_indels = curr->num_indels + 1;
}
next->score = curr->score + sc + m_sm.tsc[trans];
next->trusted = (kmerCov >= m_trusted_cutoff);
next->h_score = hcost;
next->f_score = next->score + next->h_score;
// TODO(fishjord) make max indels tunable)
if (next->num_indels < 3
&& next->score - GetNull(next->length) > next->length * m_bits_theta) {
open.push(next);
all_nodes.push_back(next);
} else {
delete next;
}
}
}
}
#if READ_ALIGNER_DEBUG
void ReadAligner::WriteNode(AlignmentNode* curr)
{
std::cerr << "curr: " << curr << " "
<< curr->prev << " " << " state=" << curr->state << " "
<< _revhash(curr->fwd_hash, m_ch->ksize()) << " "
<< _revhash(curr->rc_hash, m_ch->ksize())
<< " cov="
<< m_ch->get_count(uniqify_rc(curr->fwd_hash, curr->rc_hash))
<< " emission=" << curr->base
<< " seqidx=" << curr->seq_idx
<< " score=" << curr->score
<< " fscore=" << curr->f_score
<< " bits_saved=" << curr->score - GetNull(curr->length)
<< std::endl;
}
#endif
Alignment* ReadAligner::Subalign(AlignmentNode* start_vert,
size_t seqLen,
bool forward,
const std::string& seq)
{
std::vector<AlignmentNode*> all_nodes;
NodeHeap open;
std::map<AlignmentNode, unsigned int> closed;
open.push(start_vert);
AlignmentNode* curr = NULL;
AlignmentNode* best = NULL;
std::map<AlignmentNode, unsigned int>::iterator tmp;
unsigned int times_closed = 0;
while (!open.empty()) {
curr = open.top();
#if READ_ALIGNER_DEBUG
WriteNode(curr);
if (curr->prev == NULL) {
std::cerr << "\tprev = null" << std::endl;
} else {
std::cerr << "\tprev = ";
WriteNode(curr->prev);
}
#endif
open.pop();
if(best == NULL ||
(best->score - GetNull(best->length) <
curr->score - GetNull(curr->length))) {
best = curr;
}
if (curr->seq_idx == seqLen-1 ||
curr->seq_idx == 0) {
best = curr;
break;
}
tmp = closed.find(*curr);
if(tmp == closed.end()) { //Hasn't been closed yet
//do nothing
times_closed = 0;
} else if (tmp->first.score > curr->score) { //Better than what we've closed
times_closed = tmp->second;
closed.erase(tmp);
} else if (tmp->first.score == curr->score) { //Same as what we've closed
times_closed = tmp->second;
closed.erase(tmp);
} else {
continue;
}
if (times_closed > 200) {
continue;
}
closed[*curr] = times_closed + 1;
Enumerate(open, all_nodes, curr, forward, seq);
}
Alignment* ret = ExtractAlignment(best, forward, seq);
std::for_each(all_nodes.begin(), all_nodes.end(), del_alignment_node());
return ret;
}
Alignment* ReadAligner::ExtractAlignment(AlignmentNode* node,
bool forward,
const std::string& read)
{
Alignment* ret = new Alignment;
if(node == NULL) {
ret->score = 0;
ret->read_alignment = "";
ret->graph_alignment = "";
ret->trusted = "";
ret->truncated = true;
return ret;
}
if (!(node->seq_idx < read.length())) {
delete ret;
throw khmer_exception();
}
std::string read_alignment = "";
std::string graph_alignment = "";
std::string trusted = "";
ret->score = node->score;
ret->truncated = (node->seq_idx != 0)
&& (node->seq_idx != read.length() - 1);
#if READ_ALIGNER_DEBUG
std::cerr << "Alignment end: " << node->prev << " "
<< node->base << " " << node->seq_idx << " "
<< node->state << " " << node->score << std::endl;
#endif
char read_base;
char graph_base;
#if READ_ALIGNER_DEBUG
std::cerr << "graph_base" << "\t" << "read_base" << "\t"
<< "score\th_score\tf_score\tlength\tstate"
<< "\ttrusted?\tseq_idx\tfwd_hash\trc_hash" << std::endl;
#endif
while(node != NULL && node->prev != NULL) {
if(node->state == MATCH || node->state == MATCH_UNTRUSTED) {
graph_base = toupper(nucl_lookup[node->base]);
read_base = read[node->seq_idx];
} else if(node->state == INSERT_READ || node->state == INSERT_READ_UNTRUSTED) {
graph_base = '-';
read_base = tolower(read[node->seq_idx]);
} else if(node->state == INSERT_GRAPH
|| node->state == INSERT_GRAPH_UNTRUSTED) {
graph_base = tolower(nucl_lookup[node->base]);
read_base = '-';
} else {
graph_base = '?';
read_base = '?';
}
#if READ_ALIGNER_DEBUG
std::cerr << graph_base << "\t" << read_base << "\t"
<< node->score << "\t" << node->h_score << "\t"
<< node->f_score << "\t" << node->length << "\t"
<< node->state << "\t" << node->trusted << "\t"
<< node->seq_idx << "\t"
<< _revhash(node->fwd_hash, m_ch->ksize()) << "\t"
<< _revhash(node->rc_hash, m_ch->ksize()) << std::endl;
#endif
if(forward) {
graph_alignment = graph_base + graph_alignment;
read_alignment = read_base + read_alignment;
trusted = ((node->trusted)? "T" : "F") + trusted;
} else {
graph_alignment = graph_alignment + graph_base;
read_alignment = read_alignment + read_base;
trusted = trusted + ((node->trusted)? "T" : "F");
}
node = node->prev;
}
ret->graph_alignment = graph_alignment;
ret->read_alignment = read_alignment;
ret->trusted = trusted;
return ret;
}
struct SearchStart {
size_t kmer_idx;
size_t k_cov;
std::string kmer;
};
Alignment* ReadAligner::Align(const std::string& read)
{
int k = m_ch->ksize();
size_t num_kmers = read.length() - k + 1;
SearchStart start;
start.k_cov = 0;
start.kmer_idx = 0;
for (size_t i = 0; i < num_kmers; i++) {
std::string kmer = read.substr(i, k);
size_t kCov = m_ch->get_count(kmer.c_str());
if(kCov > start.k_cov) {
start.kmer_idx = i;
start.k_cov = kCov;
start.kmer = kmer;
}
}
if(start.k_cov > 0) {
HashIntoType fhash = 0, rhash = 0;
_hash(start.kmer.c_str(), k, fhash, rhash);
#if READ_ALIGNER_DEBUG
std::cerr << "Starting kmer: " << start.kmer << " "
<< _revhash(fhash, m_ch->ksize()) << " "
<< _revhash(rhash, m_ch->ksize())
<< " cov: " << start.k_cov << " idx: " << start.kmer_idx << ", "
<< start.kmer_idx + k - 1
<< " emission: " << start.kmer[k - 1] << std::endl;
#endif
char base = toupper(start.kmer[k - 1]);
Nucl e = A;
switch(base) {
case 'A':
e = A;
break;
case 'C':
e = C;
break;
case 'G':
e = G;
break;
case 'T':
case 'U':
e = T;
break;
}
AlignmentNode startingNode = AlignmentNode(NULL,
e, start.kmer_idx + k - 1,
MATCH, MM, fhash, rhash, k);
startingNode.f_score = 0;
startingNode.h_score = 0;
Alignment* forward = NULL;
Alignment* reverse = NULL;
size_t final_length = 0;
if(start.k_cov >= m_trusted_cutoff) {
startingNode.score = k * m_sm.trusted_match + k * m_sm.tsc[MM];
} else {
startingNode.score = k * m_sm.untrusted_match + k * m_sm.tsc[MM];
}
forward = Subalign(&startingNode, read.length(), true, read);
final_length = forward->read_alignment.length() + k;
startingNode.seq_idx = start.kmer_idx;
reverse = Subalign(&startingNode, read.length(), false, read);
final_length += reverse->read_alignment.length();
Alignment* ret = new Alignment;
//We've actually counted the starting node score
//twice, so we need to adjust for that
ret->score = reverse->score + forward->score - startingNode.score;
ret->read_alignment = reverse->read_alignment +
start.kmer + forward->read_alignment;
ret->graph_alignment = reverse->graph_alignment +
start.kmer + forward->graph_alignment;
ret->score = ret->score - GetNull(final_length);
ret->truncated = forward->truncated || reverse->truncated;
#if READ_ALIGNER_DEBUG
fprintf(stderr,
"FORWARD\n\tread_aln:%s\n\tgraph_aln:%s\n\tscore:%f\n\ttrunc:%d\n",
forward->read_alignment.c_str(), forward->graph_alignment.c_str(),
forward->score, forward->truncated);
fprintf(stderr,
"REVERSE\n\tread_aln:%s\n\tgraph_aln:%s\n\tscore:%f\n\ttrunc:%d\n",
reverse->read_alignment.c_str(), reverse->graph_alignment.c_str(),
reverse->score, reverse->truncated);
#endif
delete forward;
delete reverse;
return ret;
} else {
Alignment* ret = new Alignment;
ret->score = -std::numeric_limits<double>::infinity();
ret->read_alignment = "";
ret->graph_alignment = "";
ret->truncated = true;
return ret;
}
}
}