/
scaffold.cpp
2207 lines (1983 loc) · 98.8 KB
/
scaffold.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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//author: Roland Faure
//creation date: 2024-01-17
//aim of the program: scaffold an assembly based on reads. Also break existing contigs if necessary
#include "scaffold.h"
#include "align.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <set>
#include <ctime>
#include "robin_hood.h"
#include "clipp.h" //library to build command line interfaces
using std::string;
using std::vector;
using std::map;
using std::pair;
using std::cout;
using std::endl;
using std::set;
using robin_hood::unordered_map;
using std::min;
using std::max;
// ANSI escape codes for text color
#define RED_TEXT "\033[1;31m"
#define GREEN_TEXT "\033[1;32m"
#define RESET_TEXT "\033[0m"
string version = "0.3.1";
string last_update = "2024-05-02";
vector<string> split(string& s, string& delimiter){
vector<string> res;
size_t pos = 0;
string token = "";
for (char c : s){
bool is_delimiter = false;
for (char d : delimiter){
if (c == d){
is_delimiter = true;
break;
}
}
if (!is_delimiter){
token += c;
}
else {
res.push_back(token);
token = "";
}
}
if (token != ""){
res.push_back(token);
}
return res;
}
string reverse_complement(string seq){
string res = "";
for (int i = seq.size()-1; i >= 0; i--){
if (seq[i] == 'A'){
res += 'T';
}
else if (seq[i] == 'T'){
res += 'A';
}
else if (seq[i] == 'C'){
res += 'G';
}
else if (seq[i] == 'G'){
res += 'C';
}
else{
res += 'N';
}
}
return res;
}
/**
* @brief From the gaf file, check which reads are aligned end to end, which are partially aligned and which are not aligned at all
*
* @param gaf_file
* @param read_file
* @param number_of_well_aligned_reads
* @param number_of_partially_aligned_reads
* @param number_of_unaligned_reads
*/
void check_which_reads_are_well_aligned(std::string gaf_file, std::string read_file, int &number_of_well_aligned_reads, int& number_of_partially_aligned_reads, int& number_of_unaligned_reads){
std::ifstream gaf_stream(gaf_file);
int too_long = 1000; //minimum length of unaligned part to consider it worth reassembling
//first inventoriate the unaligned parts of the reads
string line;
string current_read;
int current_length;
unordered_map<string, bool> full_alignment_detected;
unordered_map<string, int> length_of_reads;
unordered_map<string, int> length_of_alignments_of_reads;
while (std::getline(gaf_stream, line)){
string name_of_read;
int length_of_read;
int start_of_mapping;
int end_of_mapping;
string strand;
string path;
int path_length;
int path_start;
int path_end;
string nothing;
int quality;
std::istringstream iss(line);
iss >> name_of_read >> length_of_read >> start_of_mapping >> end_of_mapping >> strand >> path >> path_length >> path_start >> path_end >> nothing >> nothing >> quality;
//check if the read aligns end to end
if (quality > 0 && start_of_mapping <= min((double)too_long, 0.2*length_of_read) && end_of_mapping >= max(0.8*length_of_read, (double)length_of_read-too_long)){
full_alignment_detected[name_of_read] = true;
length_of_reads[name_of_read] = length_of_read;
length_of_alignments_of_reads[name_of_read] = end_of_mapping-start_of_mapping;
}
else if (quality > 0){
if (length_of_alignments_of_reads.find(name_of_read) == length_of_alignments_of_reads.end()){
length_of_alignments_of_reads[name_of_read] = end_of_mapping-start_of_mapping;
length_of_reads[name_of_read] = length_of_read;
}
else{
length_of_alignments_of_reads[name_of_read] += end_of_mapping-start_of_mapping;
}
}
}
gaf_stream.close();
//now go through the reads and count
std::ifstream read_stream(read_file);
string read_name;
while (std::getline(read_stream, line)){
if (line[0] == '>' || line[0] == '@'){
read_name = line.substr(1, line.find_first_of(" \t\n")-1);
if (length_of_reads.find(read_name) != length_of_reads.end()){
if (full_alignment_detected[read_name]){
number_of_well_aligned_reads++;
}
else if (length_of_alignments_of_reads[read_name] > 0.8*length_of_reads[read_name]){ //aligned but on multiple contigs
number_of_partially_aligned_reads++;
// cout << read_name << endl;
}
else{
number_of_unaligned_reads++;
// cout << "read " << read_name << " is not aligned at all" << endl;
}
}
else{
number_of_unaligned_reads++;
}
}
}
}
/**
* @brief Module to inventoriate all the reads that do not map well on the assembly and re-assemble them using raven
*
* @param gaf_file
* @param read_file
* @param new_assembly_file
* @param new_gaf_file
* @param path_minimap2
* @param path_miniasm
* @param path_minipolish
*/
void reassemble_unaligned_reads(std::string gaf_file, std::string read_file, std::string old_assembly, std::string new_assembly_file, std::string new_gaf_file, std::string path_raven, std::string path_minigraph, int threads){
//go through the gaf file and retrieve the parts of the reads that are not aligned (at least too_long bp)
string file_of_unaligned_reads = "tmp_unaligned_reads.fa";
string file_of_full_unaligned_reads = "tmp_full_unaligned_reads.fa";
std::ifstream gaf_stream(gaf_file);
int too_long = 1000; //minimum length of unaligned part to consider it worth reassembling
unordered_map<string,vector<pair<int,int>>> unaligned_reads; //name of the read and the start and end of the unaligned part
//first inventoriate the unaligned parts of the reads
string line;
string current_read;
int current_length;
vector<pair<int,int>> aligned_part_of_this_read;
while (std::getline(gaf_stream, line)){
string name_of_read;
int length_of_read;
int start_of_mapping;
int end_of_mapping;
string strand;
string path;
int path_length;
int path_start;
int path_end;
string nothing;
int quality;
std::istringstream iss(line);
iss >> name_of_read >> length_of_read >> start_of_mapping >> end_of_mapping >> strand >> path >> path_length >> path_start >> path_end >> nothing >> nothing >> quality;
if (name_of_read != current_read){
if (aligned_part_of_this_read.size() > 0){
//sort the aligned parts of the read by start position
std::sort(aligned_part_of_this_read.begin(), aligned_part_of_this_read.end(), [](pair<int,int>& a, pair<int,int>& b){return a.first < b.first;});
//then go through the aligned parts and see if there are unaligned parts
int start_of_unaligned = 0;
for (auto part : aligned_part_of_this_read){
if (part.first-start_of_unaligned > too_long){
if (unaligned_reads.find(current_read) == unaligned_reads.end()){
unaligned_reads[current_read] = {};
}
unaligned_reads[current_read].push_back({start_of_unaligned, part.first});
}
start_of_unaligned = part.second;
}
if (current_length-start_of_unaligned > too_long){
if (unaligned_reads.find(current_read) == unaligned_reads.end()){
unaligned_reads[current_read] = {};
}
unaligned_reads[current_read].push_back({start_of_unaligned, current_length});
}
}
current_length = length_of_read;
current_read = name_of_read;
aligned_part_of_this_read.clear();
aligned_part_of_this_read.push_back({start_of_mapping, end_of_mapping});
}
else{
if (quality == 60 && end_of_mapping-start_of_mapping > too_long){
aligned_part_of_this_read.push_back({start_of_mapping, end_of_mapping});
}
}
}
gaf_stream.close();
//now output the unaligned parts of the reads in a file
std::ifstream read_stream(read_file);
std::ofstream unaligned_parts_of_reads(file_of_unaligned_reads);
std::ofstream full_unaligned_parts_of_reads(file_of_full_unaligned_reads);
string current_seq;
while (std::getline(read_stream, line)){
if (line[0] == '>' || line[0] == '@'){
if (current_read != ""){
if (unaligned_reads.find(current_read) != unaligned_reads.end()){
for (auto part : unaligned_reads[current_read]){
unaligned_parts_of_reads << ">" << current_read << "_" << part.first << "_" << part.second << endl;
unaligned_parts_of_reads << current_seq.substr(part.first, part.second-part.first) << endl;
}
full_unaligned_parts_of_reads << ">" << current_read << endl;
full_unaligned_parts_of_reads << current_seq << endl;
}
}
current_read = line.substr(1, line.find_first_of(" \t\n")-1);
current_seq = "";
}
else{
current_seq += line;
}
}
if (unaligned_reads.find(current_read) != unaligned_reads.end()){
for (auto part : unaligned_reads[current_read]){
unaligned_parts_of_reads << ">" << current_read << "_" << part.first << "_" << part.second << endl;
unaligned_parts_of_reads << current_seq.substr(part.first, part.second-part.first) << endl;
}
full_unaligned_parts_of_reads << ">" << current_read << endl;
full_unaligned_parts_of_reads << current_seq << endl;
}
unaligned_parts_of_reads.close();
full_unaligned_parts_of_reads.close();
read_stream.close();
//then reassemble the unaligned parts of the reads using raven
string raven_asm = "tmp_raven_asm.fa";
string command = path_raven + " " + file_of_unaligned_reads + " -t " + std::to_string(threads) + " > " + raven_asm + " 2> raven.log";
int res = system(command.c_str());
if (res != 0){
// cout << RED_TEXT << "ERROR: raven failed to reassemble the unaligned parts of the reads" << RESET_TEXT << endl;
// cout << "Command was: " << command << endl;
// exit(1);
}
//read the assembly file (fasta format) and add all the new contigs to the new_assembly_file (gfa format)
std::ifstream raven_asm_stream(raven_asm);
std::ofstream new_assembly_stream(new_assembly_file);
string depth = "0";
while(std::getline(raven_asm_stream, line)){
if (line[0] == '>'){
new_assembly_stream << "S\traven_created_" << line.substr(1, line.find_first_of(" \t\n")-1) << "\t";
//extract the RC:i: field to get the depth of the contig
int start = line.find("RC:i:")+5;
int end = line.find_first_of(" \t\n", start);
depth = line.substr(start, end-start);
}
else{
new_assembly_stream << line << "\tdp:i:" << depth << endl;
}
}
raven_asm_stream.close();
new_assembly_stream.close();
//append the old assembly with the new assembly
system(("cat " + old_assembly + " " + new_assembly_file + " > tmp_assembly.gfa").c_str());
system(("mv tmp_assembly.gfa " + new_assembly_file).c_str());
//map the unaligned reads on the new assembly with minigraph
command = path_minigraph + " -c -t " + std::to_string(threads) + " " + new_assembly_file + " " + file_of_full_unaligned_reads + " > " + new_gaf_file + " 2> minigraph.log";
res = system(command.c_str());
if (res != 0){
cout << RED_TEXT << "ERROR: minigraph failed to map the unaligned reads on the new assembly" << RESET_TEXT << endl;
exit(1);
}
//concat the old and new gaf file, but delete all the re-mapped reads from the old gaf file
std::ifstream old_gaf_stream(gaf_file);
string new_new_gaf_file = "tmp_new_new_gaf_file.gaf";
std::ofstream new_new_gaf_stream(new_new_gaf_file);
string old_line;
while (std::getline(old_gaf_stream, old_line)){
string name_of_read;
std::istringstream iss(old_line);
iss >> name_of_read;
if (unaligned_reads.find(name_of_read) == unaligned_reads.end()){
new_new_gaf_stream << old_line << endl;
}
}
old_gaf_stream.close();
std::ifstream new_gaf_stream(new_gaf_file);
string new_line;
while (std::getline(new_gaf_stream, new_line)){
new_new_gaf_stream << new_line << endl;
}
new_gaf_stream.close();
new_new_gaf_stream.close();
//mv the new new gaf file to the new gaf file
system(("mv " + new_new_gaf_file + " " + new_gaf_file).c_str());
//remove the temporary files
system(("rm " + raven_asm).c_str());
system("rm raven.cereal");
system(("rm " + file_of_unaligned_reads).c_str());
system(("rm " + file_of_full_unaligned_reads).c_str());
system("rm raven.log");
system("rm minigraph.log");
}
/**
* @brief inventoriate the bridges in a gaf file
* @details a bridge is defined by a read that maps first to a contig and then to another contig that is not linked to the first one in the assembly graph
*
* @param gaf_file
* @param bridges
*/
void inventoriate_bridges_and_piers(std::string gaf_file, std::vector<Bridge>& bridges, std::vector<Pier>& piers, std::string& assembly_file){
std::unordered_map<std::string, long int> length_of_contigs;
std::ifstream assembly_stream(assembly_file);
string line;
while (std::getline(assembly_stream, line)){
if (line[0] == 'S'){
std::istringstream iss(line);
std::string token;
std::string name_of_contig;
iss >> token;
iss >> name_of_contig;
iss >> token;
length_of_contigs[name_of_contig] = token.size();
}
}
assembly_stream.close();
std::ifstream gaf_stream(gaf_file);
unordered_map<string, vector<Mapping>> mappings;
string delimiter = "<>";
while (std::getline(gaf_stream, line)){
string name_of_read;
int length_of_read;
int start_of_mapping;
int end_of_mapping;
string strand;
string path;
int path_length;
int path_start;
int path_end;
string nothing;
int quality;
std::istringstream iss(line);
iss >> name_of_read >> length_of_read >> start_of_mapping >> end_of_mapping >> strand >> path >> path_length >> path_start >> path_end >> nothing >> nothing >> quality;
if (strand != "+"){
cout << "ERROR: strand in GAF is not +... panicking !! SOS !!" << endl;
exit(1);
}
int min_length_for_breakpoint = max(0.2*length_of_read, 500.0); //minimum length of overhang to consider a breakpoint AND minimum length of mapping to be confident
if (quality == 60) //read is not mapped end to end !
{
vector<string> all_contigs = split(path, delimiter);
vector<char> orientations;
//remove the first element
all_contigs.erase(all_contigs.begin());
//get the orientations
for (char c : path){
if (c == '<' || c == '>'){
orientations.push_back(c);
}
}
Mapping mapping;
mapping.read = name_of_read;
mapping.length_of_read = length_of_read;
mapping.contig1 = all_contigs[0];
if (length_of_contigs.find(all_contigs[0]) == length_of_contigs.end()){
cout << "ERROR: contig " << all_contigs[0] << " not found in the assembly file " << assembly_file << endl;
exit(1);
}
mapping.position_on_contig1 = path_start;
mapping.pos_on_read1 = start_of_mapping;
mapping.orientation_on_contig1 = false;
mapping.orientation_on_read1 = false;
if (orientations[0] == '<' ){
mapping.position_on_contig1 = length_of_contigs[all_contigs[0]] - path_start;
mapping.orientation_on_contig1 = true;
}
mapping.contig2 = all_contigs[all_contigs.size()-1];
if (length_of_contigs.find(all_contigs[0]) == length_of_contigs.end()){
cout << "ERROR: contig " << all_contigs[0] << " not found in the assembly file " << assembly_file << endl;
exit(1);
}
mapping.position_on_contig2 = length_of_contigs[all_contigs[all_contigs.size()-1]] - (path_length - path_end);
mapping.pos_on_read2 = end_of_mapping;
mapping.orientation_on_contig2 = true;
mapping.orientation_on_read2 = true;
if (orientations[orientations.size()-1] == '<'){
mapping.position_on_contig2 = path_length - path_end;
mapping.orientation_on_contig2 = false;
}
mapping.breakpoint1 = false;
if (start_of_mapping > min_length_for_breakpoint && end_of_mapping-start_of_mapping > min_length_for_breakpoint){ //do not flag a breakpoint if the mapping is too short
mapping.breakpoint1 = true;
}
mapping.breakpoint2 = false;
if (length_of_read-end_of_mapping > min_length_for_breakpoint && end_of_mapping-start_of_mapping > min_length_for_breakpoint){
mapping.breakpoint2 = true;
}
mappings[name_of_read].push_back(mapping);
// if ("685d55c2-401c-44ae-9dcd-cfcede23cd75" == name_of_read.substr(0,36) || "8f2b22a9-35df-409d-a939-baa12ef1862f" == name_of_read){
// cout << "read " << name_of_read << endl;
// cout << "Mapping " << mapping.contig1 << " " << mapping.position_on_contig1 << " " << mapping.orientation_on_contig1 << " " << mapping.breakpoint1
// << " " << mapping.pos_on_read1 << " " << mapping.pos_on_read2 << " " << mapping.breakpoint2 << " " << mapping.contig2 << " " << mapping.position_on_contig2
// << " " << mapping.orientation_on_contig2 << endl;
// }
}
}
gaf_stream.close();
//sort the mappings by position on read
for (auto& mapping : mappings){
std::sort(mapping.second.begin(), mapping.second.end(), [](Mapping& a, Mapping& b){return a.pos_on_read1 < b.pos_on_read1;});
//see if there are links between the breakpoints
for (int b = 0; b < mapping.second.size(); b++){
//see if there is a pier left of the first breakpoint
if (b == 0 && mapping.second[b].breakpoint1 == true){
//check that the full length of the read align after this
int length_of_alignment = 0;
for (int c = 0; c < mapping.second.size(); c++){
length_of_alignment += mapping.second[c].pos_on_read2 - mapping.second[c].pos_on_read1;
}
if (length_of_alignment > (mapping.second[0].length_of_read-mapping.second[0].pos_on_read1)*0.8 && mapping.second[0].pos_on_read1 > max(500.0, 0.5*mapping.second[0].length_of_read)){
Pier pier;
pier.contig = mapping.second[b].contig1;
pier.position = mapping.second[b].position_on_contig1;
pier.strand = mapping.second[b].orientation_on_contig1;
pier.read_name = mapping.first;
pier.pos_read_on_contig = mapping.second[b].pos_on_read1;
pier.strand_read = false;
piers.push_back(pier);
}
}
if (b < mapping.second.size()-1 && mapping.second[b].breakpoint2 == true && mapping.second[b+1].breakpoint1 == true ){ //then the two breakpoints are linked !!
//if there is only a small overlap between the two mappings, adjust the overlap to remove it
if (mapping.second[b+1].pos_on_read1-mapping.second[b].pos_on_read2 < 0){
int overlap_length = mapping.second[b].pos_on_read2 - mapping.second[b+1].pos_on_read1;
if (overlap_length < 0.1*(mapping.second[b].pos_on_read2-mapping.second[b].pos_on_read1) && overlap_length < 0.1*(mapping.second[b+1].pos_on_read2-mapping.second[b+1].pos_on_read1)){
//choose which overlap to trim based on which contig is the longest
if (length_of_contigs[mapping.second[b].contig2] < length_of_contigs[mapping.second[b+1].contig1]){
mapping.second[b].pos_on_read2 -= min((int) length_of_contigs[mapping.second[b].contig2]-1, overlap_length);
if (mapping.second[b].orientation_on_contig2 == false){
mapping.second[b].position_on_contig2 += overlap_length;
}
else{
mapping.second[b].position_on_contig2 -= overlap_length;
}
}
else{
mapping.second[b+1].pos_on_read1 += min((int) length_of_contigs[mapping.second[b+1].contig1]-1, overlap_length);
if (mapping.second[b+1].orientation_on_contig1 == false){
mapping.second[b+1].position_on_contig1 += overlap_length;
}
else{
mapping.second[b+1].position_on_contig1 -= overlap_length;
}
}
}
}
if (mapping.second[b+1].pos_on_read1-mapping.second[b].pos_on_read2 >= 0) { //if there is still an overlap, then we can't use this bridge
Bridge bridge;
bridge.contig1 = mapping.second[b].contig2;
bridge.pos_read_on_contig1 = mapping.second[b].pos_on_read2;
bridge.position1 = mapping.second[b].position_on_contig2;
bridge.strand1 = mapping.second[b].orientation_on_contig2;
bridge.contig2 = mapping.second[b+1].contig1;
bridge.pos_read_on_contig2 = mapping.second[b+1].pos_on_read1;
bridge.position2 = mapping.second[b+1].position_on_contig1;
bridge.strand2 = mapping.second[b+1].orientation_on_contig1;
bridge.read_name = mapping.first;
bridges.push_back(bridge);
}
}
//see if there is a pier right of the last breakpoint
if (b == mapping.second.size()-1 && mapping.second[b].breakpoint2 == true){
//check that the full length of the read align before this
int length_of_alignment = 0;
for (int c = 0; c < mapping.second.size(); c++){
length_of_alignment += mapping.second[c].pos_on_read2 - mapping.second[c].pos_on_read1;
}
if (length_of_alignment > mapping.second[0].pos_on_read2*0.8 && mapping.second[b].pos_on_read2 > max(500.0, 0.5*mapping.second[0].length_of_read)){
Pier pier;
pier.contig = mapping.second[b].contig2;
pier.position = mapping.second[b].position_on_contig2;
pier.strand = mapping.second[b].orientation_on_contig2;
pier.read_name = mapping.first;
pier.pos_read_on_contig = mapping.second[b].pos_on_read2;
pier.strand_read = true;
piers.push_back(pier);
}
}
}
}
}
/**
* @brief agregate bridges into solid bridges
*
* @param bridges
* @param solid_bridges
* @param aggregative_distance distance in base pairs to agregate bridges
* @param min_number_of_reads
*/
void agregate_bridges_and_piers(std::vector<Bridge>& bridges, std::vector<Pier>& piers, std::vector<SolidBridge>& solid_bridges, std::vector<SolidPier>& solid_piers, int aggregative_distance, int min_number_of_reads){
//agregate bridges into solid bridges
for (Bridge& bridge : bridges){
bool found = false;
for (auto& solid_bridge : solid_bridges){
if (solid_bridge.contig1 == bridge.contig1 && solid_bridge.contig2 == bridge.contig2
&& abs(solid_bridge.position1 - bridge.position1) <= aggregative_distance && abs(solid_bridge.position2 - bridge.position2) <= aggregative_distance
&& abs(abs(solid_bridge.pos_read_on_contig1[0]-solid_bridge.pos_read_on_contig2[0]) - abs(bridge.pos_read_on_contig1-bridge.pos_read_on_contig2) ) <= aggregative_distance
&& solid_bridge.strand1 == bridge.strand1 && solid_bridge.strand2 == bridge.strand2){ // the two reads are on the same strand
if (bridge.pos_read_on_contig2-bridge.pos_read_on_contig1 < solid_bridge.pos_read_on_contig2[0]-solid_bridge.pos_read_on_contig1[0]){
solid_bridge.position1 = bridge.position1;
solid_bridge.position2 = bridge.position2;
solid_bridge.read_names.insert(solid_bridge.read_names.begin(), bridge.read_name);
solid_bridge.pos_read_on_contig1.insert(solid_bridge.pos_read_on_contig1.begin(), bridge.pos_read_on_contig1);
solid_bridge.pos_read_on_contig2.insert(solid_bridge.pos_read_on_contig2.begin(), bridge.pos_read_on_contig2);
solid_bridge.strand.insert(solid_bridge.strand.begin(), true);
}
else{
solid_bridge.read_names.push_back(bridge.read_name);
solid_bridge.pos_read_on_contig1.push_back(bridge.pos_read_on_contig1);
solid_bridge.pos_read_on_contig2.push_back(bridge.pos_read_on_contig2);
solid_bridge.strand.push_back(true);
}
found = true;
break;
}
else if (solid_bridge.contig1 == bridge.contig2 && solid_bridge.contig2 == bridge.contig1
&& abs(solid_bridge.position1 - bridge.position2) <= aggregative_distance && abs(solid_bridge.position2 - bridge.position1) <= aggregative_distance
&& abs(abs(solid_bridge.pos_read_on_contig1[0]-solid_bridge.pos_read_on_contig2[0]) - abs(bridge.pos_read_on_contig1-bridge.pos_read_on_contig2) ) <= aggregative_distance
&& solid_bridge.strand1 == bridge.strand2 && solid_bridge.strand2 == bridge.strand1){ // the two reads are on opposite strands
if (bridge.pos_read_on_contig2-bridge.pos_read_on_contig1 < solid_bridge.pos_read_on_contig2[0]-solid_bridge.pos_read_on_contig1[0]){
solid_bridge.position1 = bridge.position2;
solid_bridge.position2 = bridge.position1;
solid_bridge.read_names.insert(solid_bridge.read_names.begin(), bridge.read_name);
solid_bridge.pos_read_on_contig1.insert(solid_bridge.pos_read_on_contig1.begin(), bridge.pos_read_on_contig1);
solid_bridge.pos_read_on_contig2.insert(solid_bridge.pos_read_on_contig2.begin(), bridge.pos_read_on_contig2);
solid_bridge.strand.insert(solid_bridge.strand.begin(), false);
}
else{
solid_bridge.read_names.push_back(bridge.read_name);
solid_bridge.pos_read_on_contig1.push_back(bridge.pos_read_on_contig1);
solid_bridge.pos_read_on_contig2.push_back(bridge.pos_read_on_contig2);
solid_bridge.strand.push_back(false);
}
found = true;
break;
}
}
if (!found){
SolidBridge solid_bridge;
solid_bridge.contig1 = bridge.contig1;
solid_bridge.contig2 = bridge.contig2;
solid_bridge.position1 = bridge.position1;
solid_bridge.position2 = bridge.position2;
solid_bridge.strand1 = bridge.strand1;
solid_bridge.strand2 = bridge.strand2;
solid_bridge.read_names.push_back(bridge.read_name);
solid_bridge.pos_read_on_contig1.push_back(bridge.pos_read_on_contig1);
solid_bridge.pos_read_on_contig2.push_back(bridge.pos_read_on_contig2);
solid_bridge.strand.push_back(true);
solid_bridges.push_back(solid_bridge);
}
}
//keep only the solid bridges that have at least min_number_of_reads reads
std::vector<SolidBridge> solid_bridges_kept;
for (auto solid_bridge : solid_bridges){
if (solid_bridge.read_names.size() >= min_number_of_reads){
solid_bridges_kept.push_back(solid_bridge);
}
}
solid_bridges = solid_bridges_kept;
//agregate piers into solid piers
for (Pier& pier : piers){
bool found = false;
for (auto& solid_pier : solid_piers){
if (solid_pier.contig == pier.contig && abs(solid_pier.position - pier.position) <= aggregative_distance
&& solid_pier.strand == pier.strand){
solid_pier.read_names.push_back(pier.read_name);
solid_pier.pos_read_on_contig.push_back(pier.pos_read_on_contig);
solid_pier.strands_read.push_back(pier.strand_read);
found = true;
// if (abs(pier.position - 2026206) < 1000){
// cout << "Adding pier " << pier.read_name << " to solid pier " << solid_pier.read_names[0] << " at position " << pier.position << " and solid pier position " << solid_pier.position << endl;
// }
break;
}
}
if (!found){
SolidPier solid_pier;
solid_pier.contig = pier.contig;
solid_pier.position = pier.position;
solid_pier.strand = pier.strand;
solid_pier.read_names.push_back(pier.read_name);
solid_pier.pos_read_on_contig.push_back(pier.pos_read_on_contig);
solid_pier.strands_read.push_back(pier.strand_read);
solid_piers.push_back(solid_pier);
}
}
//now keep only the solid piers that have at least min_number_of_reads reads AND that are not just one end of a bridge but not long enough to be a bridge
std::vector<SolidPier> solid_piers_kept;
for (auto solid_pier : solid_piers){
if (solid_pier.read_names.size() >= min_number_of_reads){
bool is_pier = true;
for (auto solid_bridge : solid_bridges){
if (solid_bridge.contig1 == solid_pier.contig || solid_bridge.contig2 == solid_pier.contig){
if ((abs(solid_bridge.position1 - solid_pier.position) <= aggregative_distance && solid_bridge.strand1 == solid_pier.strand)
|| (abs(solid_bridge.position2 - solid_pier.position) <= aggregative_distance && solid_bridge.strand2 == solid_pier.strand)){
is_pier = false; //the pier is just one end of a bridge
break;
}
}
}
if (is_pier){
solid_piers_kept.push_back(solid_pier);
}
}
}
solid_piers = solid_piers_kept;
}
/**
* @brief transform solid bridges into links, ie with precise coordinates and if needed gap filling
*
* @param solid_bridges
* @param read_file
* @param links result of the transformation
*/
void transform_bridges_in_links(std::vector<SolidBridge>& solid_bridges, std::string read_file, std::string assembly_file, std::vector<Link>& links,
std::string& path_minimap2, std::string& path_racon){
//index the positions of the reads in the read file
robin_hood::unordered_map<std::string, long int> read_positions;
std::ifstream read_stream(read_file);
string line;
long int position = 0;
auto pos = read_stream.tellg();
while (std::getline(read_stream, line)){
pos = read_stream.tellg();
if (line[0] == '>' || line[0] == '@'){
std::istringstream iss(line);
std::string token;
iss >> token;
read_positions[token.substr(1)] = pos;
}
}
read_stream.close();
//index the positions of the contigs in the assembly file
robin_hood::unordered_map<std::string, long int> contig_positions;
std::ifstream assembly_stream(assembly_file);
pos = assembly_stream.tellg();
while (std::getline(assembly_stream, line)){
if (line[0] == 'S'){
std::istringstream iss(line);
std::string token;
iss >> token;
iss >> token;
contig_positions[token] = pos;
}
pos = assembly_stream.tellg();
}
//then transform the solid bridges into links
for (auto solid_bridge : solid_bridges){
Link link;
link.contig1 = solid_bridge.contig1;
link.contig2 = solid_bridge.contig2;
link.strand1 = solid_bridge.strand1;
link.strand2 = solid_bridge.strand2;
link.coverage = solid_bridge.read_names.size();
//compute the sequence left_of_the_junction + read_chunk + sequence_right_of_the_junction and see if we adjust the coordinates
//retrieve the sequence of the read in the junction
std::ifstream read_stream(read_file);
read_stream.seekg(read_positions[solid_bridge.read_names[0]]);
std::string gap_filling_seq;
std::string seq_with_overhangs;
std::getline(read_stream, line);
if (solid_bridge.strand[0] == true){
gap_filling_seq = line.substr(solid_bridge.pos_read_on_contig1[0], solid_bridge.pos_read_on_contig2[0]-solid_bridge.pos_read_on_contig1[0]);
}
else {
gap_filling_seq = reverse_complement(line.substr(solid_bridge.pos_read_on_contig1[0], solid_bridge.pos_read_on_contig2[0]-solid_bridge.pos_read_on_contig1[0]));
}
read_stream.close();
//retrieve the sequence of the contig on the left of the junction
std::ifstream assembly_stream(assembly_file);
std::string contig1_sequence, contig2_sequence;
assembly_stream.seekg(contig_positions[solid_bridge.contig1]);
std::getline(assembly_stream, line);
string seq_1, seq_2, nothing;
std::istringstream iss(line);
iss >> nothing >> nothing >> seq_1;
if (solid_bridge.strand1 == true){
int overhang_1 = min(solid_bridge.position1 , 200);
contig1_sequence = seq_1.substr(solid_bridge.position1-overhang_1, overhang_1);
}
else{
int overhang_1 = min(200, (int) seq_1.size()-solid_bridge.position1);
contig1_sequence = reverse_complement(seq_1.substr(solid_bridge.position1, overhang_1));
}
//retrieve the sequence of the contig on the right of the junction
assembly_stream.seekg(contig_positions[solid_bridge.contig2]);
std::getline(assembly_stream, line);
std::istringstream iss2(line);
iss2 >> nothing >> nothing >> seq_2;
cout << "retrieving the contig right of the jucntion " << solid_bridge.position2 << endl;
if (solid_bridge.strand2 == true){
int overhang_2 = min(200, solid_bridge.position2);
contig2_sequence = reverse_complement(seq_2.substr(solid_bridge.position2-overhang_2, overhang_2));
}
else{
int overhang_2 = min((int) seq_2.size() - solid_bridge.position2, 200);
contig2_sequence = seq_2.substr(solid_bridge.position2, overhang_2);
}
assembly_stream.close();
seq_with_overhangs = contig1_sequence + gap_filling_seq + contig2_sequence;
//now retrieve all the reads that make the link to polish the gap filling sequence
std::vector<std::string> reads;
for (auto read_num = 1 ; read_num < solid_bridge.read_names.size() ; read_num++){
string read_name = solid_bridge.read_names[read_num];
std::ifstream read_stream(read_file);
read_stream.seekg(read_positions[read_name]);
std::getline(read_stream, line);
//retrieve the gap filling sequence only, with 100 bp on each side
int start = max(solid_bridge.pos_read_on_contig1[read_num]-300, 0);
int end = min(solid_bridge.pos_read_on_contig2[read_num]+300, (int) line.size());
if (solid_bridge.strand[read_num] == true){
reads.push_back(line.substr(start, end-start));
}
else {
reads.push_back(reverse_complement(line.substr(start, end-start)));
}
read_stream.close();
}
//polish the gap filling sequence
string polished_gap_filling_seq = polish(seq_with_overhangs, reads, path_minimap2, path_racon);
//now align the gap filling sequence to the contigs left and right to see where the junction is exactly and what to put in between
string contig1_sequence2, contig2_sequence2; //sequences on the other side of the junction compared to before, i.e. towards the inside of the junction
if (solid_bridge.strand1 == true){
int overhang_1 = min(seq_1.size() - solid_bridge.position1 , gap_filling_seq.size());
contig1_sequence2 = seq_1.substr(solid_bridge.position1, overhang_1);
}
else{
int overhang_1 = min( (int) gap_filling_seq.size(), solid_bridge.position1);
contig1_sequence2 = reverse_complement(seq_1.substr(solid_bridge.position1-overhang_1, overhang_1));
}
string cigar = align(contig1_sequence2, 0, contig1_sequence2.size(), gap_filling_seq, 0, gap_filling_seq.size());
int end_of_match_gap_fill = 0;
int end_of_match_contig1 = 0;
int pos_c1 = 0;
int pos_gf = 0;
int consecutive_matches = 5;
int num_indel = 0;
int num_matches = 0;
for (auto c = 0 ; c < cigar.size() ; c++){
if (cigar[c] == '='){
consecutive_matches++;
pos_c1++;
pos_gf++;
num_matches++;
}
else{
consecutive_matches = 0;
if (cigar[c] == 'I'){
pos_gf++;
}
else if (cigar[c] == 'D'){
pos_c1++;
}
else if (cigar[c] == 'X'){
pos_c1++;
pos_gf++;
}
num_indel++;
}
if (consecutive_matches >= 5 && num_indel <= 0.2*num_matches){
end_of_match_gap_fill = pos_gf;
end_of_match_contig1 = pos_c1;
}
}
//slide the gap filling sequence and the contig to the right by end_of_match_gap_fill and end_of_match_contig1
if (solid_bridge.strand1 == true){
link.position1 = solid_bridge.position1 + end_of_match_contig1;
}
else{
link.position1 = solid_bridge.position1 - end_of_match_contig1;
}
gap_filling_seq = gap_filling_seq.substr(end_of_match_gap_fill, gap_filling_seq.size()-end_of_match_gap_fill);
if (solid_bridge.strand2 == true){
int overhang_2 = min(seq_2.size() - solid_bridge.position2 , gap_filling_seq.size());
contig2_sequence2 = reverse_complement(seq_2.substr(solid_bridge.position2, overhang_2));
}
else{
int overhang_2 = min( (int) gap_filling_seq.size(), solid_bridge.position2);
contig2_sequence2 = seq_2.substr(solid_bridge.position2-overhang_2, overhang_2);
}
//align to gap filling seq but with reverse complement
string rcc2 = reverse_complement(contig2_sequence2);
string rcgf = reverse_complement(gap_filling_seq);
string cigar2 = align(rcc2, 0, contig2_sequence2.size(), rcgf, 0, gap_filling_seq.size());
int end_of_match_gap_fill2 = 0;
int end_of_match_contig2 = 0;
int pos_c2 = 0;
int pos_gf2 = 0;
consecutive_matches = 5;
num_indel = 0;
num_matches = 0;
for (auto c = 0 ; c < cigar2.size() ; c++){
if (cigar2[c] == '='){
consecutive_matches++;
pos_c2++;
pos_gf2++;
num_matches++;
}
else{
consecutive_matches = 0;
if (cigar2[c] == 'I'){
pos_gf2++;
}
else if (cigar2[c] == 'D'){
pos_c2++;
}
else if (cigar2[c] == 'X'){
pos_c2++;
pos_gf2++;
}
num_indel++;
}
if (consecutive_matches >= 5 && num_indel <= 0.2*num_matches){
end_of_match_gap_fill2 = pos_gf2;
end_of_match_contig2 = pos_c2;
}
}
//slide the gap filling sequence and the contig to the right by end_of_match_gap_fill and end_of_match_contig2
if (solid_bridge.strand2 == true){
link.position2 = solid_bridge.position2 + end_of_match_contig2;
}
else{
link.position2 = solid_bridge.position2 - end_of_match_contig2;
}
gap_filling_seq = gap_filling_seq.substr(end_of_match_gap_fill2, gap_filling_seq.size()-end_of_match_gap_fill2);
link.extra_sequence = gap_filling_seq;
links.push_back(link);
// cout << "solid bridge: " << endl;
// cout << "contig1: " << solid_bridge.contig1 << " " << solid_bridge.position1 << " " << solid_bridge.strand1 << " " << seq_1.size() << endl;
// cout << "CIGAR of the gap filling seq on the right of contig 1: " << cigar << endl;
// cout << "I would slide the gap filling seq by " << end_of_match_gap_fill << " bp and the contig by " << end_of_match_contig1 << endl;
// cout << "contig 2: " << solid_bridge.contig2 << " " << solid_bridge.position2 << " " << solid_bridge.strand2 << " " << seq_2.size() << endl;
// cout << "CIGAR of the gap filling seq on the left of contig 2: " << cigar2 << endl;
// cout << "I would slide the gap filling seq by " << end_of_match_gap_fill2 << " bp and the contig by " << end_of_match_contig2 << endl;
// cout << endl;
// exit(1);
}
}
/**
* @brief
*
* @param solid_piers
* @param read_file
* @param assembly_file
* @param end_contigs
* @param path_minimap2
* @param path_racon
*/
void build_piers(std::vector<SolidPier>& solid_piers, std::string read_file, std::string assembly_file, std::vector<End_contig>& end_contigs,
std::string& path_minimap2, std::string& path_racon){
//index the positions of the reads in the read file
robin_hood::unordered_map<std::string, long int> read_positions;
std::ifstream read_stream(read_file);
string line;
long int position = 0;
auto pos = read_stream.tellg();
while (std::getline(read_stream, line)){
pos = read_stream.tellg();
if (line[0] == '>' || line[0] == '@'){
std::istringstream iss(line);
std::string token;
iss >> token;
read_positions[token.substr(1)] = pos;
}
}
read_stream.close();
//index the positions of the contigs in the assembly file