forked from vgteam/vg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.cpp
650 lines (573 loc) · 23.3 KB
/
mapper.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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
#include "mapper.hpp"
namespace vg {
Mapper::Mapper(Index* idex)
: index(idex)
, best_clusters(0)
, cluster_min(2)
, hit_max(100)
, hit_size_threshold(0)
, kmer_min(11)
, kmer_threshold(1)
, kmer_sensitivity_step(3)
, thread_extension(1)
, thread_extension_max(80)
, max_thread_gap(30)
, max_attempts(7)
, softclip_threshold(0)
, prefer_forward(false)
, greedy_accept(false)
, target_score_per_bp(1.5)
, min_kmer_entropy(0)
, debug(false)
{
kmer_sizes = index->stored_kmer_sizes();
if (kmer_sizes.empty()) {
cerr << "error:[vg::Mapper] the index ("
<< index->name << ") does not include kmers" << endl;
exit(1);
}
}
Mapper::~Mapper(void) {
// noop
}
Alignment Mapper::align(string& seq, int kmer_size, int stride, int band_width) {
Alignment aln;
aln.set_sequence(seq);
return align(aln, kmer_size, stride, band_width);
}
// align read2 near read1's mapping location
void Mapper::align_mate_in_window(Alignment& read1, Alignment& read2, int pair_window) {
if (read1.score() == 0) return; // bail out if we haven't aligned the first
// try to recover in region
Path* path = read1.mutable_path();
int64_t idf = path->mutable_mapping(0)->position().node_id();
int64_t idl = path->mutable_mapping(path->mapping_size()-1)->position().node_id();
// but which way should we expand? this will make things much easier
// just use the whole "window" for now
int64_t first = max((int64_t)0, idf - pair_window);
int64_t last = idl + (int64_t) pair_window;
VG* graph = new VG;
index->get_range(first, last, *graph);
graph->remove_orphan_edges();
read2.clear_path();
read2.set_score(0);
graph->align(read2);
delete graph;
}
pair<Alignment, Alignment> Mapper::align_paired(Alignment& read1, Alignment& read2, int kmer_size, int stride, int band_width, int pair_window) {
// use paired-end resolution techniques
//
// attempt mapping of first mate
// if it works, expand the search space for the second (try to avoid new kmer lookups)
// (alternatively, do the whole kmer lookup thing but then restrict the constructed
// graph to a range near the first alignment)
// if it doesn't work, try the second, then expand the range to align the first
//
// problem: need to develop model of pair orientations
// solution: collect a buffer of alignments and then align them using unpaired approach
// detect read orientation and mean (and sd) of pair distance
//if (try_both_first) {
Alignment aln1 = align(read1, kmer_size, stride, band_width);
Alignment aln2 = align(read2, kmer_size, stride, band_width);
// link the fragments
aln1.mutable_fragment_next()->set_name(aln2.name());
aln2.mutable_fragment_prev()->set_name(aln1.name());
// and then try to rescue unmapped mates
if (aln1.score() == 0 && aln2.score()) {
// should we reverse the read??
if (aln2.is_reverse()) {
aln1.set_sequence(reverse_complement(aln1.sequence()));
aln1.set_is_reverse(true);
}
align_mate_in_window(aln2, aln1, pair_window);
} else if (aln2.score() == 0 && aln1.score()) {
if (aln1.is_reverse()) {
aln2.set_sequence(reverse_complement(aln2.sequence()));
aln2.set_is_reverse(true);
}
align_mate_in_window(aln1, aln2, pair_window);
}
// TODO
// mark them as discordant if there is an issue?
// this needs to be detected with care using statistics built up from a bunch of reads
return make_pair(aln1, aln2);
}
Alignment Mapper::align_banded(Alignment& read, int kmer_size, int stride, int band_width) {
// split the alignment up into overlapping chunks of band_width size
list<Alignment> alignments;
assert(read.sequence().size() > band_width);
int div = 2;
while (read.sequence().size()/div > band_width) {
++div;
}
int segment_size = read.sequence().size()/div;
// and overlap them too
Alignment merged;
for (int i = 0; i < div; ++i) {
{
Alignment aln = read;
aln.set_sequence(read.sequence().substr(i*segment_size, segment_size));
if (i == 0) {
merged = align(aln, kmer_size, stride);
} else {
merge_alignments(merged, align(aln, kmer_size, stride));
}
}
// and the overlapped bit --- here we're using 50% overlap
if (i != div-1) { // if we're not at the last sequence
Alignment aln = read;
aln.set_sequence(read.sequence().substr(i*segment_size+segment_size/2, segment_size));
merge_alignments(merged, align(aln, kmer_size, stride));
}
}
return merged;
}
Alignment Mapper::align(Alignment& aln, int kmer_size, int stride, int band_width) {
if (aln.sequence().size() > band_width) {
return align_banded(aln, kmer_size, stride, band_width);
}
std::chrono::time_point<std::chrono::system_clock> start_both, end_both;
if (debug) start_both = std::chrono::system_clock::now();
const string& sequence = aln.sequence();
// if kmer size is not specified, pick it up from the index
// for simplicity, use the first available kmer size; this could change
if (kmer_size == 0) kmer_size = *kmer_sizes.begin();
// and start with stride such that we barely cover the read with kmers
if (stride == 0)
stride = sequence.size()
/ ceil((double)sequence.size() / kmer_size);
int kmer_hit_count = 0;
int kept_kmer_count = 0;
if (debug) cerr << "aligning " << aln.sequence() << endl;
// forward
Alignment alignment_f = aln;
// reverse
Alignment alignment_r = aln;
alignment_r.set_sequence(reverse_complement(aln.sequence()));
alignment_r.set_is_reverse(true);
auto increase_sensitivity = [this,
&kmer_size,
&stride,
&sequence,
&alignment_f,
&alignment_r]() {
kmer_size -= kmer_sensitivity_step;
stride = sequence.size() / ceil((double)sequence.size() / kmer_size);
if (debug) cerr << "realigning with " << kmer_size << " " << stride << endl;
/*
if ((double)stride/kmer_size < 0.5 && kmer_size -5 >= kmer_min) {
kmer_size -= 5;
stride = sequence.size() / ceil((double)sequence.size() / kmer_size);
if (debug) cerr << "realigning with " << kmer_size << " " << stride << endl;
} else if ((double)stride/kmer_size >= 0.5 && kmer_size >= kmer_min) {
stride = max(1, stride/3);
if (debug) cerr << "realigning with " << kmer_size << " " << stride << endl;
}
*/
};
int attempt = 0;
int kmer_count_f = 0;
int kmer_count_r = 0;
while (alignment_f.score() == 0 && alignment_r.score() == 0 && attempt < max_attempts) {
{
std::chrono::time_point<std::chrono::system_clock> start, end;
if (debug) start = std::chrono::system_clock::now();
align_threaded(alignment_f, kmer_count_f, kmer_size, stride, attempt);
if (debug) {
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
cerr << elapsed_seconds.count() << "\t" << "+" << "\t" << alignment_f.sequence() << endl;
}
}
if (!(prefer_forward && (float)alignment_f.score() / (float)sequence.size() >= target_score_per_bp))
{
std::chrono::time_point<std::chrono::system_clock> start, end;
if (debug) start = std::chrono::system_clock::now();
align_threaded(alignment_r, kmer_count_r, kmer_size, stride, attempt);
if (debug) {
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
cerr << elapsed_seconds.count() << "\t" << "-" << "\t" << alignment_r.sequence() << endl;
}
}
++attempt;
if (alignment_f.score() == 0 && alignment_r.score() == 0) {
increase_sensitivity();
} else {
break;
}
}
if (debug) {
end_both = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end_both-start_both;
cerr << elapsed_seconds.count() << "\t" << "b" << "\t" << sequence << endl;
}
if (alignment_r.score() > alignment_f.score()) {
return alignment_r;
} else {
return alignment_f;
}
}
Alignment& Mapper::align_threaded(Alignment& alignment, int& kmer_count, int kmer_size, int stride, int attempt) {
// parameters, some of which should probably be modifiable
// TODO -- move to Mapper object
if (index == NULL) {
cerr << "error:[vg::Mapper] no index loaded, cannot map alignment!" << endl;
exit(1);
}
const string& sequence = alignment.sequence();
auto kmers = balanced_kmers(sequence, kmer_size, stride);
//vector<uint64_t> sizes;
//index->approx_sizes_of_kmer_matches(kmers, sizes);
vector<map<int64_t, vector<int32_t> > > positions(kmers.size());
int i = 0;
for (auto& k : kmers) {
if (!allATGC(k)) continue; // we can't handle Ns in this scheme
//if (debug) cerr << "kmer " << k << " entropy = " << entropy(k) << endl;
if (min_kmer_entropy > 0 && entropy(k) < min_kmer_entropy) continue;
uint64_t approx_matches = index->approx_size_of_kmer_matches(k);
if (debug) cerr << k << "\t" << approx_matches << endl;
// if we have more than one block worth of kmers on disk, consider this kmer non-informative
// we can do multiple mapping by relaxing this
if (approx_matches > hit_size_threshold) {
continue;
}
auto& kmer_positions = positions.at(i);
index->get_kmer_positions(k, kmer_positions);
// ignore this kmer if it has too many hits
// typically this will be filtered out by the approximate matches filter
if (kmer_positions.size() > hit_max) kmer_positions.clear();
kmer_count += kmer_positions.size();
// break when we get more than a threshold number of kmers to seed further alignment
//if (kmer_count >= kmer_threshold) break;
++i;
}
if (debug) cerr << "kept kmer hits " << kmer_count << endl;
// make threads
// these start whenever we have a kmer match which is outside of
// one of the last positions (for the previous kmer) + the kmer stride % wobble (hmm)
map<int64_t, vector<int> > node_kmer_order;
map<pair<int64_t, int32_t>, vector<int64_t> > position_threads;
map<int64_t, vector<int64_t> > node_threads;
//int node_wobble = 0; // turned off...
int position_wobble = 2;
int max_iter = sequence.size();
int iter = 0;
int64_t max_subgraph_size = 0;
i = 0;
for (auto& p : positions) {
auto& kmer = kmers.at(i++);
for (auto& x : p) {
int64_t id = x.first;
vector<int32_t>& pos = x.second;
node_kmer_order[id].push_back(i-1);
for (auto& y : pos) {
//cerr << kmer << "\t" << i << "\t" << id << "\t" << y << endl;
// thread rules
// if we find the previous position
int m = 0;
vector<int64_t> thread;
for (int j = 0; j < 2*position_wobble + 1; ++j) {
if (j == 0) { // on point
} else if (j % 2 == 0) { // subtract
m *= -1;
} else { // add
m *= -1; ++m;
}
//cerr << "checking " << id << " " << y << " - " << kmer_size << " + " << m << endl;
auto previous = position_threads.find(make_pair(id, y - stride + m));
if (previous != position_threads.end()) {
//length = position_threads[make_pair(id, y - stride + m)] + 1;
thread = previous->second;
position_threads.erase(previous);
//cerr << "thread is " << thread.size() << " long" << endl;
break;
}
}
thread.push_back(id);
position_threads[make_pair(id, y)] = thread;
node_threads[id] = thread;
}
}
}
map<int, vector<vector<int64_t> > > threads_by_length;
for (auto& t : node_threads) {
auto& thread = t.second;
auto& threads = threads_by_length[thread.size()];
threads.push_back(thread);
}
// now sort the threads and re-cluster them
if (debug) {
cerr << "initial threads" << endl;
for (auto& t : threads_by_length) {
auto& length = t.first;
auto& threads = t.second;
cerr << length << ":" << endl;
for (auto& thread : threads) {
cerr << "\t";
for (auto& id : thread) {
cerr << id << " ";
}
cerr << endl;
}
cerr << endl;
}
}
// sort threads by ids
set<vector<int64_t> > sorted_threads;
auto tl = threads_by_length.rbegin();
for (auto& t : node_threads) {
auto& thread = t.second;
sorted_threads.insert(thread);
}
threads_by_length.clear();
// go back through and combine closely-linked threads
// ... but only if their kmer order is proper
map<int64_t, vector<int64_t> > threads_by_last;
// go from threads that are longer to ones that are shorter
for (auto& thread : sorted_threads) {
//cerr << thread.front() << "-" << thread.back() << endl;
auto prev = threads_by_last.upper_bound(thread.front()-max_thread_gap);
//if (prev != threads_by_last.begin()) --prev;
// now we should be at the highest thread within the bounds
//cerr << prev->first << " " << thread.front() << endl;
// todo: it may also make sense to check that the kmer order makes sense
// what does this mean? it means that the previous
if (prev != threads_by_last.end()
&& prev->first > thread.front() - max_thread_gap) {
vector<int64_t> new_thread;
auto& prev_thread = prev->second;
new_thread.reserve(prev_thread.size() + thread.size());
new_thread.insert(new_thread.end(), prev_thread.begin(), prev_thread.end());
new_thread.insert(new_thread.end(), thread.begin(), thread.end());
threads_by_last.erase(prev);
// this will clobber... not good
// maybe overwrite only if longer?
threads_by_last[new_thread.back()] = new_thread;
} else {
threads_by_last[thread.back()] = thread;
}
}
// debugging
/*
if (debug) {
cerr << "threads by last" << endl;
for (auto& t : threads_by_last) {
auto& thread = t.second;
cerr << t.first << "\t";
for (auto& id : thread) {
cerr << id << " ";
}
cerr << endl;
}
}
*/
// rebuild our threads_by_length set
for (auto& t : threads_by_last) {
auto& thread = t.second;
if (thread.size() >= cluster_min) {
auto& threads = threads_by_length[thread.size()];
threads.push_back(thread);
}
}
if (debug) {
cerr << "threads ready for alignment" << endl;
for (auto& t : threads_by_length) {
auto& length = t.first;
auto& threads = t.second;
cerr << length << ":" << endl;
for (auto& thread : threads) {
cerr << "\t";
for (auto& id : thread) {
cerr << id << " ";
}
cerr << endl;
}
cerr << endl;
}
}
int thread_ex = thread_extension;
map<vector<int64_t>*, Alignment> alignments;
// collect the nodes from the best N threads by length
// and expand subgraphs as before
//cerr << "extending by " << thread_ex << endl;
tl = threads_by_length.rbegin();
bool accepted = false;
for (int i = 0;
!accepted
&& tl != threads_by_length.rend()
&& (best_clusters == 0 || i < best_clusters);
++i, ++tl) {
auto& threads = tl->second;
// by definition, our thread should construct a contiguous graph
for (auto& thread : threads) {
// thread extension should be determined during iteration
// note that there is a problem and hits tend to be imbalanced
int64_t first = max((int64_t)0, *thread.begin() - thread_ex);
int64_t last = *thread.rbegin() + thread_ex;
//int64_t first = *thread.begin();
//int64_t last = *thread.rbegin();
// so we can pick it up efficiently from the index by pulling the range from first to last
if (debug) cerr << "getting node range " << first << "-" << last << endl;
VG* graph = new VG;
index->get_range(first, last, *graph);
Alignment& ta = alignments[&thread];
ta = alignment;
// by default, expand the graph a bit so we are likely to map
//index->get_connected_nodes(*graph);
graph->remove_orphan_edges();
// align
ta.clear_path();
ta.set_score(0);
graph->align(ta);
// check if we start or end with soft clips
// if so, try to expand the graph until we don't have any more (or we hit a threshold)
// expand in the direction where there were soft clips
int sc_start = softclip_start(ta);
int sc_end = softclip_end(ta);
if (sc_start > softclip_threshold || sc_end > softclip_threshold) {
if (debug) cerr << "softclip handling " << sc_start << " " << sc_end << endl;
Path* path = ta.mutable_path();
int64_t idf = path->mutable_mapping(0)->position().node_id();
int64_t idl = path->mutable_mapping(path->mapping_size()-1)->position().node_id();
// step towards the side where there were soft clips
// using 10x the thread_extension
int64_t f = max((int64_t)0, idf - (int64_t) max(thread_ex, 1) * 10);
int64_t l = idl + (int64_t) max(thread_ex, 1) * 10;
if (debug) cerr << "getting node range " << f << "-" << l << endl;
index->get_range(f, l, *graph);
graph->remove_orphan_edges();
ta.clear_path();
ta.set_score(0);
graph->align(ta);
if (debug) cerr << "softclip after " << softclip_start(ta) << " " << softclip_end(ta) << endl;
}
delete graph;
if (debug) cerr << "score per bp is " << (float)ta.score() / (float)ta.sequence().size() << endl;
if (greedy_accept && (float)ta.score() / (float)ta.sequence().size() >= target_score_per_bp) {
if (debug) cerr << "greedy accept" << endl;
accepted = true;
break;
}
}
}
// now find the best alignment
int sum_score = 0;
double mean_score = 0;
map<int, set<Alignment*> > alignment_by_score;
for (auto& ta : alignments) {
Alignment* aln = &ta.second;
alignment_by_score[aln->score()].insert(aln);
}
// get the best alignment
set<Alignment*>& best = alignment_by_score.rbegin()->second;
//cerr << alignment_by_score.size() << endl;
if (!alignment_by_score.empty()) {
alignment = **best.begin();
if (debug) {
cerr << "best alignment score " << alignment.score() << endl;
}
} else {
alignment.clear_path();
alignment.set_score(0);
}
if (debug && alignment.score() == 0) cerr << "failed alignment" << endl;
return alignment;
}
int softclip_start(Alignment& alignment) {
if (alignment.mutable_path()->mapping_size() > 0) {
Path* path = alignment.mutable_path();
Mapping* first_mapping = path->mutable_mapping(0);
Edit* first_edit = first_mapping->mutable_edit(0);
if (first_edit->from_length() == 0 && first_edit->to_length() > 0) {
return first_edit->to_length();
}
}
return 0;
}
int softclip_end(Alignment& alignment) {
if (alignment.mutable_path()->mapping_size() > 0) {
Path* path = alignment.mutable_path();
Mapping* last_mapping = path->mutable_mapping(path->mapping_size()-1);
Edit* last_edit = last_mapping->mutable_edit(last_mapping->edit_size()-1);
if (last_edit->from_length() == 0 && last_edit->to_length() > 0) {
return last_edit->to_length();
}
}
return 0;
}
Alignment& Mapper::align_simple(Alignment& alignment, int kmer_size, int stride) {
if (index == NULL) {
cerr << "error:[vg::Mapper] no index loaded, cannot map alignment!" << endl;
exit(1);
}
// establish kmers
const string& sequence = alignment.sequence();
//
auto kmers = balanced_kmers(sequence, kmer_size, stride);
map<string, int32_t> kmer_counts;
vector<map<int64_t, vector<int32_t> > > positions(kmers.size());
int i = 0;
for (auto& k : kmers) {
index->get_kmer_positions(k, positions.at(i++));
kmer_counts[k] = positions.at(i-1).size();
}
positions.clear();
VG* graph = new VG;
for (auto& c : kmer_counts) {
if (c.second < hit_max) {
index->get_kmer_subgraph(c.first, *graph);
}
}
int max_iter = sequence.size();
int iter = 0;
int context_step = 1;
int64_t max_subgraph_size = 0;
// use kmers which are informative
// and build up the graph
auto get_max_subgraph_size = [this, &max_subgraph_size, &graph]() {
list<VG> subgraphs;
graph->disjoint_subgraphs(subgraphs);
for (auto& subgraph : subgraphs) {
max_subgraph_size = max(subgraph.total_length_of_nodes(), max_subgraph_size);
}
};
get_max_subgraph_size();
while (max_subgraph_size < sequence.size()*2 && iter < max_iter) {
index->expand_context(*graph, context_step);
index->get_connected_nodes(*graph);
get_max_subgraph_size();
++iter;
}
// ensure we have a complete graph prior to alignment
index->get_connected_nodes(*graph);
/*
ofstream f("vg_align.vg");
graph->serialize_to_ostream(f);
f.close();
*/
graph->align(alignment);
return alignment;
}
const int balanced_stride(int read_length, int kmer_size, int stride) {
double r = read_length;
double k = kmer_size;
double j = stride;
if (r > j) {
return round((r-k)/round((r-k)/j));
} else {
return j;
}
}
const vector<string> balanced_kmers(const string& seq, const int kmer_size, const int stride) {
// choose the closest stride that will generate balanced kmers
vector<string> kmers;
int b = balanced_stride(seq.size(), kmer_size, stride);
if (!seq.empty()) {
for (int i = 0; i+kmer_size < seq.size(); i+=b) {
kmers.push_back(seq.substr(i,kmer_size));
}
}
return kmers;
}
}