-
Notifications
You must be signed in to change notification settings - Fork 0
/
fusion.cpp
448 lines (382 loc) · 14.7 KB
/
fusion.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
#include "fusion.hpp"
#include "process.hpp"
#include "revcomp.hpp"
using namespace std;
Fusion::Fusion( Contiglist *contigs, Readlist *reads ) : contigs(contigs), reads(reads){}
// creates id of fused contigs
string Fusion::get_fused_id( string contig1_id, string contig2_id ){
if( contig1_id.length() >= 5 && contig1_id.compare( 0, 5, "fused" ) == 0 ){
contig1_id = contig1_id.substr( 5 );
}
if( contig1_id.length() >= 5 && contig2_id.compare( 0, 5, "fused" ) == 0 ){
contig2_id = contig2_id.substr( 5 );
}
return contig1_id+"_<>_"+contig2_id;
}
// complete contig_fusion process
void Fusion::contig_fusion_log( Mismatch fus ){
// print messages to logfile about current actions
Log::Inst()->log_it( "Contig fused: " );
Log::Inst()->log_it( " Overlap length: " + to_string(fus.get_length()) );
Log::Inst()->log_it( " Mismatch_score: " + to_string(fus.get_score()) );
Log::Inst()->log_it( " Contig_i: " + contigs->get_contig(fus.get_index_i()).get_contig_id() );
Log::Inst()->log_it( " Contig_j: " + contigs->get_contig(fus.get_index_j()).get_contig_id() );
Log::Inst()->log_it( "" );
}
// complete contig_fusion process
void Fusion::commit_fusion( string fused, string fused_id, int index_i, int index_j ){
contigs->append_contig( 1, contigs->get_contig(index_i) );
Log::Inst()->log_it( "Contig moved to fused file: " + contigs->get_contig(index_i).get_contig_id() );
contigs->append_contig( 1, contigs->get_contig(index_j) );
Log::Inst()->log_it( "Contig moved to fused file: " + contigs->get_contig(index_j).get_contig_id() );
Log::Inst()->log_it( "Committing: " + fused_id );
Log::Inst()->log_it( "\t" + fused );
contigs->append_contig( 0, Contig( reads, fused, fused_id ));
}
// check overlap section for mismatches
Mismatch Fusion::overlap_check( string contig_a, string contig_b, int overlap, int end_i, int end_j ){
Mismatch mim;
mim.set_end_i( end_i );
mim.set_end_j( end_j );
double score_f = 1.0;
double score_r = 1.0;
double score = 1.0;
for( int i=overlap-1; i>=min_overlap; i-- ){
score_f = mismatch_score( contig_a.substr( contig_a.length() - i, i/2 ), contig_b.substr( 0, i/2 ) );
score_r = mismatch_score( contig_a.substr( contig_a.length() - (i-i/2) ), contig_b.substr( i/2, i-i/2 ) );
score = (score_f + score_r)/2;
// check if this overlap is the best so far
if( score < mim.get_score() ){
mim.set_score( score );
mim.set_length( i );
}
// if the score for the end of contig_a is better than the threshold, check the end of contig_b to see if there could be support in the reads for that end going in a different direction
else if( score_r <= mismatch_threshold ){
score_f = check_fusion_support( contig_b.substr(i/2, i-i/2), contig_a.substr( contig_a.length() - i, i/2 ) );
// make sure the support score is at least as good as the threshold
if( score_f <= mismatch_threshold ){
score = (score_f + score_r)/2;
if( score < mim.get_score() ){
mim.set_score( score );
mim.set_length( i );
}
}
}
// if the score for the end of contig_b is better than the threshold, check the end of contig_a to see if there could be support in the reads for that end going in a different direction
else if( score_f <= mismatch_threshold ){
score_r = check_fusion_support( contig_a.substr( contig_a.length() - (i-i/2) ), contig_b.substr( i/2, i-i/2 ) );
// make sure the support score is at least as good as the threshold
if( score_r <= mismatch_threshold ){
score = (score_r + score_f)/2;
if( score < mim.get_score() ){
mim.set_score( score );
mim.set_length( i );
}
}
}
}
return mim;
}
// create fused contig string
string Fusion::build_fusion_string( string contig_a, string contig_b, int overlap ){
string fused( contig_a.substr( 0, contig_a.length() - (overlap/2) ) );
fused.append( contig_b.substr( overlap/2 + overlap%2 ) );
return fused;
}
// remove duplicates from contig remove list
void Fusion::dedup_removals(){
for( int i=0; i<(int)contig_remove_list.size()-1; i++ ){
for( int j=i+1; j<contig_remove_list.size(); j++ ){
if( contig_remove_list[i] == contig_remove_list[j] ){
contig_remove_list.erase( contig_remove_list.begin() + j );
j--;
}
}
}
}
// sort index list for removing contigs
void Fusion::sort_removals(){
int plc_hld;
for( int i=0; i<(int)contig_remove_list.size()-1; i++ ){
int low_idx = i;
for( int j=i+1; j<(int)contig_remove_list.size(); j++ ){
if( contig_remove_list[j] < contig_remove_list[low_idx] ){
low_idx = j;
}
}
if( low_idx != i ){
plc_hld = contig_remove_list[i];
contig_remove_list[i] = contig_remove_list[low_idx];
contig_remove_list[low_idx] = plc_hld;
}
}
}
// remove fused contigs from contigs list
void Fusion::process_removals(){
dedup_removals();
sort_removals();
for( int i=(int)contig_remove_list.size()-1; i>=0; i-- ){
contigs->remove_contig( contig_remove_list[i] );
}
contig_remove_list.clear();
}
// compile list of best mismatch scores between contigs that meet the mismatch threshold
vector<Mismatch> Fusion::get_mismatch_scores( bool first_run ){
vector<Mismatch> match_list;
int overlap_len = extend_len * 4;
// loop through each contig to get the end of the contig
for( int i=0; i<contigs->get_list_size(); i++ ){
for( int j=i+1; j<contigs->get_list_size(); j++ ){
string contig_i( contigs->get_contig(i).get_sequence() );
string contig_j( contigs->get_contig(j).get_sequence() );
string contig_j_rev( revcomp( contig_j ) );
int overlap = overlap_len;
Mismatch mim;
if( first_run ){
overlap = (contig_i.length() < contig_j.length()) ? contig_i.length() : contig_j.length();
}
else{
if( overlap > contig_i.length() ){
overlap = contig_i.length();
}
if( overlap > contig_j.length() ){
overlap = contig_j.length();
}
}
//// Processing of rear end of the contig
// orientation: i to j
mim = overlap_check( contig_i, contig_j, overlap, 1, 0 );
if( mim.get_score() <= mismatch_threshold ){
mim.set_indices( i, j );
match_list.push_back( mim );
}
// orientation: i to j_rev
mim = overlap_check( contig_i, contig_j_rev, overlap, 1, 1 );
if( mim.get_score() <= mismatch_threshold ){
mim.set_indices( i, j );
match_list.push_back( mim );
}
//// Processing of front end of the contig
// orientation: j to i
mim = overlap_check( contig_j, contig_i, overlap, 0, 1);
if( mim.get_score() <= mismatch_threshold ){
mim.set_indices( i, j );
match_list.push_back( mim );
}
// orientation: j_rev to i
mim = overlap_check( contig_j_rev, contig_i, overlap, 0, 0 );
if( mim.get_score() <= mismatch_threshold ){
mim.set_indices( i, j );
match_list.push_back( mim );
}
}
}
return match_list;
}
// sort the match_list for easier
void Fusion::sort_matches(){
Mismatch temp_mim;
// sort by overlap length
for( int i=0; i<match_list.size(); i++ ){
for( int j=i; j<match_list.size(); j++ ){
if( match_list[i].get_length() < match_list[j].get_length() ){
temp_mim = match_list[i];
match_list[i] = match_list[j];
match_list[j] = temp_mim;
}
}
}
// sort by score
for( int i=0; i<match_list.size(); i++ ){
for( int j=i; j<match_list.size(); j++ ){
if( match_list[i].get_score() > match_list[j].get_score() ){
temp_mim = match_list[i];
match_list[i] = match_list[j];
match_list[j] = temp_mim;
}
}
}
}
// cleans match_list from conflicting matches
void Fusion::clean_matches(){
for( int i=0; i<(int)match_list.size()-1; i++ ){
int index_i = match_list[i].get_index_i();
int end_i = match_list[i].get_end_i();
int index_j = match_list[i].get_index_j();
int end_j = match_list[i].get_end_j();
for( int j=i+1; j<match_list.size(); j++ ){
if( match_list[j].get_index_i() == index_i && match_list[j].get_end_i() == end_i ){
match_list.erase( match_list.begin() + j );
j--;
}
else if( match_list[j].get_index_j() == index_i && match_list[j].get_end_j() == end_i ){
match_list.erase( match_list.begin() + j );
j--;
}
else if( match_list[j].get_index_j() == index_j && match_list[j].get_end_j() == end_j ){
match_list.erase( match_list.begin() + j );
j--;
}
else if( match_list[j].get_index_i() == index_j && match_list[j].get_end_i() == end_j ){
match_list.erase( match_list.begin() + j );
j--;
}
}
}
}
// process the compiled list of fusions
void Fusion::process_fusions(){
for( int i=0; i<match_list.size(); i++ ){
// fuse contigs
int index_i = match_list[i].get_index_i();
int index_j = match_list[i].get_index_j();
string contig_i = contigs->get_contig(index_i).get_sequence();
string contig_j = contigs->get_contig(index_j).get_sequence();
string fused_id("");
string fused("");
string rev_i("");
string rev_j("");
// find the reverse compliment of the contigs where necessary
if( match_list[i].get_end_i() == 0 ){
contig_i = revcomp( contig_i );
rev_i = "_r";
}
if( match_list[i].get_end_j() == 1 ){
contig_j = revcomp( contig_j );
rev_j = "_r";
}
contig_fusion_log( match_list[i] );
fused_id = get_fused_id( contigs->get_contig(index_i).get_contig_id()+rev_i, contigs->get_contig(index_j).get_contig_id()+rev_j );
fused_id = "fused(" + fused_id + ")";
// push each index onto the remove vector
contig_remove_list.push_back( index_i );
contig_remove_list.push_back( index_j );
// create fused contig
fused = build_fusion_string( contig_i, contig_j, match_list[i].get_length() );
// commit fusion here
commit_fusion( fused, fused_id, index_i, index_j );
int new_index = contigs->get_list_size()-1;
// check for additional appearances of the current contigs in match_list
for( int j=i+1; j<match_list.size(); j++ ){
// change reference index and end variables where necessary
if( index_i == match_list[j].get_index_i() ){
match_list[j].set_index_i( new_index );
match_list[j].set_end_i( 0 );
}
else if( index_i == match_list[j].get_index_j() ){
match_list[j].set_index_j( new_index );
match_list[j].set_end_j( 0 );
}
else if( index_j == match_list[j].get_index_i() ){
match_list[j].set_index_i( new_index );
match_list[j].set_end_i( 1 );
}
else if( index_j == match_list[j].get_index_j() ){
match_list[j].set_index_j( new_index );
match_list[j].set_end_j( 1 );
}
}
}
}
// contig_fusion: Attempt to support fusion in case of possibly poorly constructed end.. returns new score from section in question
// ::> contig object is the second while contig_ref is the first and the extension is being made off the front of the object
double Fusion::check_fusion_support( string contig, string contig_ref ){
Extension *exten = new Extension( reads, contig_ref.length(), contig );
string support_string( "" );
double score = 0;
const int pos = contig_ref.length() - 1;
const int start = -1;
exten->matches.start_match();
// return if matches found is less than min_cov
if( exten->matches.get_matchlist_size() < min_cov ){
delete exten;
return 1.0;
}
// create missed bp's vector to keep track of how many bp's each read contains that are below the max at that position
vector<int> mismatch( exten->matches.get_matchlist_size(), 0 );
vector<int> ambiguous_bp( exten->matches.get_matchlist_size(), 0 );
int mismatch_tot = 0;
int mismatch_avg = 0;
int cmp_len = 0;
// count mismatch's in each read match
for( int i=0; i<contig_ref.length(); i++ ){
int next_char_ref = contig_ref[pos-i];
for( int j=0; j<exten->matches.get_matchlist_size(); j++ ){
int next_char = exten->matches.get_pos( j, start-i );
if( next_char == 'N' ){
ambiguous_bp[j]++;
}
// check if next_char matches or exists in the current read
else if( next_char != next_char_ref && next_char != -1 ){
mismatch[j]++;
}
}
}
// remove reads with more than the max_missed
exten->set_missed_bp( mismatch );
exten->set_missed_avg( max_missed );
exten->error_removal();
// remove reads with more than 3 N's
exten->set_missed_bp( ambiguous_bp );
exten->set_missed_avg( 3 );
exten->error_removal();
// if matchlist is smaller than min_cov, bail, return 1.0 (the least supportive return value)
if( exten->matches.get_matchlist_size() < min_cov ){
delete exten;
return 1.0;
}
// get string built from remaining matches.
exten->build_string();
support_string = exten->get_extension();
cmp_len = support_string.length();
// use the smaller of contig_ref.length() and min_overlap/2 as min_length to compare support_string with matching contig
if( cmp_len < contig_ref.length() && cmp_len < (min_overlap/2 + min_overlap%2) ){
delete exten;
return 1.0;
}
// get smallest of string lengths
if( cmp_len > contig_ref.length() ){
cmp_len = contig_ref.length();
}
score = mismatch_score( support_string.substr( support_string.length() - cmp_len), contig_ref.substr( contig_ref.length() - cmp_len ) );
delete exten;
return score;
}
// fuse contigs wherever possible
void Fusion::run_fusion( bool first_run ){
Log::Inst()->log_it( "Fuse contigs" );
// MISMATCH SCORES //
match_list = get_mismatch_scores( first_run );
// SORT AND CLEAN MATCH LIST //
sort_matches();
clean_matches();
if( verbose ){
for( int i=0; i<match_list.size(); i++ ){
Log::Inst()->log_it( "score: " + to_string( match_list[i].get_score() ) );
Log::Inst()->log_it( "i: " + to_string( match_list[i].get_index_i() ) + " j: " + to_string( match_list[i].get_index_j() ) );
}
}
// FUSE CONTIGS //
process_fusions();
// remove fused contigs from the vector
process_removals();
}
// tally mismatches in substrings passed and return score in the form of misatches per length
double Fusion::mismatch_score( string contig_sub_a, string contig_sub_b ){
int mismatch = 0;
// protect against division by zero
if( contig_sub_a.length() == 0 ){
return 1.0;
}
for( int i=0; i<contig_sub_a.length(); i++ ){
if( contig_sub_a[i] != contig_sub_b[i] ){
mismatch++;
}
// bail when too many mismatches have been tallied.. avoid excessive processing
if( double(mismatch) /contig_sub_a.length() > mismatch_threshold ){
return 1.0;
}
}
double score = double(mismatch) / contig_sub_a.length();
return score;
}