-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_smooth_test_weird_dbs.cpp
2184 lines (2047 loc) · 86.9 KB
/
old_smooth_test_weird_dbs.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
#include "./../kmc_api/kmc_file.h"
#include "./../kmc_api/kmer_api.h"
#include "nc_utils.h"
#include "stdafx.h"
#include <algorithm>
#include <bits/stdc++.h>
#include <ctime>
#include <fstream>
#include <iostream>
#include <list>
#include <math.h>
#include <mutex>
#include <numeric>
#include <stdlib.h>
#include <thread>
#include <unistd.h>
#include <vector>
#include "uniq.hpp"
#include <unordered_map>
#include <chrono>
//smooth kmcdb reads.fastq errremoved.fasta erredits.fasta errpaths.fasta hetremoved.fasta hetedits.fasta hetpath1.fasta hetpath2.fasta hetpath3.fasta hetpath4.fasta hetpath5.fasta hetpath6.fasta error_threshold het_threshold unique_threshold anchor_threshold allowed_err_fraction allowed_rep_fraction max_nodes_to_search distance_multiplier strict > counts.txt
int get_type(uint32_t& coverage, int& error_threshold, int& het_threshold, int& unique_threshold)
{
if (coverage <= error_threshold)
{
return 0;
}
else if (coverage <= het_threshold)
{
return 1;
}
else if (coverage <= unique_threshold)
{
return 2;
}
else
{
return 3;
}
}
std::vector<std::string> get_adjacent(CKMCFile& file, std::vector<int>& shared_reads, const unordered_map<auto, vector<string>>& local_kmer_db, std::string& kmer, int& error_threshold, int& het_threshold, int& unique_threshold, bool& going_right, int k)
{
//std::cout << "Getting adjacent\n";
//std::cout.flush();
std::vector<uint32_t> v;
std::vector<std::string> adjacent_kmers;
//for all possible nucleotide extensions from kmer
for (char const &c: "ACGT")
{
std::string adjacent_kmer;
if (going_right)
{
adjacent_kmer = kmer.substr(1)+c;
}
else
{
//cout << "Going left\n";
//cout << kmer << "\n";
//cout << kmer.length() << "\n";
//cout << c << "\n";
adjacent_kmer = c+kmer.substr(0, kmer.length()-1);
}
//std::cout << kmer << "\n" ;
//std::cout.flush();
v.clear();
if (shared_reads.size() > 30) {
get_local_counts(adjacent_kmer, local_kmer_db, v, k);//TODO
} else {
file.GetCountersForRead(adjacent_kmer, v);
}
if (v.size() == 0){
v.push_back(0);
}
//std::cout << v.size() << "\n" ;
//std::cout.flush();
//std::cout << kmer << "\t" << v[0] << "\n" ;
/*for (uint32_t something: v) {
std::cout << something << "\n" ;
}*/
int current_type = get_type(v[0], error_threshold, het_threshold, unique_threshold);
//if the adjacent kmer is not an error
if ((current_type > 0)) //&& (current_type < 3)) //Added the 2nd condition
{
adjacent_kmers.push_back(adjacent_kmer);
}
}
return adjacent_kmers;
}
bool is_left_anchor(CKMCFile& file, std::vector<int>& shared_reads, std::string& previous_kmer, int& previous_count, int& k, const unordered_map<auto, vector<string>>& local_kmer_db, int& error_threshold, int& het_threshold, int& unique_threshold, int& anchor_threshold)
{
if ((previous_kmer.length() != k) || (previous_count > unique_threshold)) // > unique_threshold or anchor_threshold
//if (previous_kmer.length() != k)
{
return false;
}
bool going_right = true;
std::vector<std::string> adjacent_kmers = get_adjacent( file, shared_reads, local_kmer_db, previous_kmer, error_threshold, het_threshold, unique_threshold, going_right, k);
if (adjacent_kmers.size() == 2)
{
std::vector<uint32_t> v1;
//file.GetCountersForRead(adjacent_kmers[0], v1);
if (shared_reads.size() > 30) {
get_local_counts(adjacent_kmers[0], local_kmer_db, v1, k);
} else {
file.GetCountersForRead(adjacent_kmers[0], v1);
}
int count1 = v1[0];
std::vector<uint32_t> v2;
//file.GetCountersForRead(adjacent_kmers[1], v2);
if (shared_reads.size() > 30) {
get_local_counts(adjacent_kmers[1], local_kmer_db, v2, k);
} else {
file.GetCountersForRead(adjacent_kmers[1], v2);
}
int count2 = v2[0];
//If the sum of the coverages of the two branches is within 3 of the homozygous portion
if ((previous_count - 3 <= count1 + count2) && (count1 + count2 <= previous_count + 3))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
bool is_right_anchor(CKMCFile& file, std::vector<int>& shared_reads, std::string& current_kmer, int& current_count, int& k, const unordered_map<auto, vector<string>>& local_kmer_db, int& error_threshold, int& het_threshold, int& unique_threshold, int& anchor_threshold)
{
if ((current_kmer.length() != k) || (current_count > unique_threshold)) // > unique_threshold or anchor_threshold?
//if (current_kmer.length() != k)
{
return false;
}
bool going_right = false;
std::vector<std::string> adjacent_kmers = get_adjacent(file, shared_reads, local_kmer_db, current_kmer, error_threshold, het_threshold, unique_threshold, going_right, k);
if (adjacent_kmers.size() == 2)
{
std::vector<uint32_t> v1;
//file.GetCountersForRead(adjacent_kmers[0], v1);
if (shared_reads.size() > 30) {
get_local_counts(adjacent_kmers[0], local_kmer_db, v1, k);
} else {
file.GetCountersForRead(adjacent_kmers[0], v1);
}
int count1 = v1[0];
std::vector<uint32_t> v2;
//file.GetCountersForRead(adjacent_kmers[1], v2);
if (shared_reads.size() > 30) {
get_local_counts(adjacent_kmers[1], local_kmer_db, v2, k);
} else {
file.GetCountersForRead(adjacent_kmers[1], v2);
}
int count2 = v2[0];
//If the sum of the coverages of the two branches is within 3 of the homozygous portion
if ((current_count - 3 <= count1 + count2) && (count1 + count2 <= current_count + 3))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
int get_type_het(CKMCFile& file, std::vector<int>& shared_reads, int& previous_type, std::string& previous_kmer, std::string& current_kmer, int& previous_count, int& current_count, int& k, const unordered_map<auto, vector<string>>& local_kmer_db, int& error_threshold, int& het_threshold, int& unique_threshold, int& anchor_threshold, std::string& anchor_found)
{
//If "previously" at the beginning of the read
if (previous_type == -1)
{
//if current kmer is a right anchor
if ((current_count > (previous_count + error_threshold)) && is_right_anchor(file,shared_reads, current_kmer, current_count, k, local_kmer_db, error_threshold, het_threshold, unique_threshold, anchor_threshold))
//((current_count > (previous_count + error_threshold)) && is_right_anchor(shared_reads, current_kmer, current_count, k, local_kmer_db, error_threshold, het_threshold, unique_threshold, anchor_threshold))
{
anchor_found = "right";
return 2;
}
//if current kmer is a left anchor
else if (is_left_anchor(file,shared_reads, current_kmer, current_count, k, local_kmer_db, error_threshold, het_threshold, unique_threshold, anchor_threshold))
{
anchor_found = "left";
return 2;
}
//To keep it consistent with previous get_type, we need type to be -1 only before we start the read
//So, in this case we will do what was done before, using the coverage and coverage thresholds to
//determine whether the kmer is homozygous or not
else if ((het_threshold < current_count) && (current_count <= unique_threshold))
{
anchor_found = "none";
return 2;
}
else
{
anchor_found = "none";
return 1;
}
}
//If previously in a nonhomozygous region
else if (previous_type == 1)
{
//If current kmer is a right anchor
if ((current_count > (previous_count + error_threshold )) && is_right_anchor(file,shared_reads, current_kmer, current_count, k, local_kmer_db, error_threshold, het_threshold, unique_threshold, anchor_threshold))
//((current_count > (previous_count + error_threshold )) && is_right_anchor(shared_reads, current_kmer, current_count, k, local_kmer_db, error_threshold, het_threshold, unique_threshold, anchor_threshold))
{
//we have left the nonhom region
anchor_found = "right";
return 2;
}
//If current kmer is a left anchor
else if (is_left_anchor(file,shared_reads, current_kmer, current_count, k, local_kmer_db, error_threshold, het_threshold, unique_threshold, anchor_threshold))
{
//this is a weird case where we find two left anchors in a row before a right anchor
anchor_found = "left";
return 2;
}
//else current kmer is not an anchor
else
{
//we continue the nonhom region
anchor_found = "none";
return 1;
}
}
//If previously in a homozygous region
//else if (previous_type == 2)
else
{
//If current kmer is a right anchor
//if ((current_count > (previous_count + error_threshold)) && is_right_anchor(current_kmer, current_count, k, file, error_threshold, het_threshold, unique_threshold, anchor_threshold))
//{
//this is a weird case where we find two right anchors in a row before a left anchor
//we don't need to set anchor_found or return 2 because we really care
//if current kmer is a left anchor, ending the homozygous region
//}
//If current kmer is a left anchor
if (is_left_anchor(file,shared_reads, current_kmer, current_count, k, local_kmer_db, error_threshold, het_threshold, unique_threshold, anchor_threshold))
{
anchor_found = "left";
return 2;
}
//If previous kmer is a left anchor
else if ((current_count < (previous_count - error_threshold )) && is_left_anchor(file,shared_reads, previous_kmer, previous_count, k, local_kmer_db, error_threshold, het_threshold, unique_threshold, anchor_threshold))
//((current_count < (previous_count - error_threshold )) && is_left_anchor(shared_reads, previous_kmer, previous_count, k, local_kmer_db, error_threshold, het_threshold, unique_threshold, anchor_threshold))
{
//we have left the hom region
anchor_found = "left";
return 1;
}
//else previous kmer is not a left anchor (and current kmer is not a left anchor)
else
{
//we continue the hom region
anchor_found = "none";
return 2;
}
}
}
std::vector<std::string> get_paths(CKMCFile& file, std::vector<int>& shared_reads, const unordered_map<auto, vector<string>>& local_kmer_db, int& error_threshold, int& het_threshold, int& unique_threshold, std::string& left_anchor_kmer, std::string& right_anchor_kmer, int& min_distance_of_path, int& max_distance_of_path, int& max_nodes_to_search, int& k, bool& queue_broken)
{
//std::cout << "Getting paths\n";
//std::cout.flush();
std::string starting_anchor_kmer;
bool going_right;
if (left_anchor_kmer.empty())
{
//We are at the beginning of the read.
//This function will find paths starting from right_anchor_kmer continuing left to the
//beginning of the read where each kmer in the path is a nonerror kmer.
starting_anchor_kmer = right_anchor_kmer;
going_right = false;
}
else if (right_anchor_kmer.empty())
{
//We are at the end of the read.
//This function will find paths starting from left_anchor_kmer continuing right to the
//end of the read where each kmer in the path is a nonerror kmer.
starting_anchor_kmer = left_anchor_kmer;
going_right = true;
}
else
{
//We are in the middle of the read.
//This function will find paths starting from left_anchor_kmer continuing right until
//right_anchor_kmer where each kmer in the path is a nonerror kmer.
starting_anchor_kmer = left_anchor_kmer;
going_right = true;
}
//std::cout << "We are either going left or right\n";
//std::cout.flush();
//In every case, we follow all paths, but cut the depth of any path to max_distance_of_path.
//We also ensure a minimum depth equal to min_distance_of_path.
std::list<std::string> queue;
queue.push_back(starting_anchor_kmer);
//Initialize paths to store all the paths that are found.
std::vector<std::string> paths;
//We use i as a counter for how many nodes have been visited in the search.
//If we haven't finished the search within max_nodes_to_search nodes,
//we break the search.
//This drastically speeds up the run time for some regions.
//Thankfully, it doesn't seem to impact effectiveness, since most searches
//complete before this threshold.
int i = 0;
//This flag keeps track of whether we had to stop the search early.
queue_broken = false;
while(!queue.empty())
{
//std::cout << "Queue not empty\n";
//std::cout.flush();
i++;
std::string current_path = queue.front();
std::string current_kmer;
if (going_right)
{
current_kmer = current_path.substr(current_path.length()-k);
}
else
{
current_kmer = current_path.substr(0, k);
}
queue.pop_front();
int current_depth = current_path.length()-k;
//If we have to terminate search early
if (i > max_nodes_to_search)
{
queue_broken = true;
break;
}
//cout << "Checking the node depth\n";
//If the depth of this node hasn't exceeded the max distance of the path
if (current_depth <= max_distance_of_path)
{
//std::cout << "About to get adjacent\n";
//std::cout.flush();
//Extend the path by one nucleotide, keep the ones that are not error kmers
std::vector<std::string> adjacent_kmers = get_adjacent(file,shared_reads, local_kmer_db, current_kmer, error_threshold, het_threshold, unique_threshold, going_right, k);
//std::cout << "Extending the path by one nuc\n";
//std::cout.flush();
for (auto adjacent_kmer : adjacent_kmers)
{
std::string path;
if (going_right)
{
path = current_path + adjacent_kmer.back();
}
else
{
path = adjacent_kmer.front() + current_path;
}
bool end_condition;
//If we are in the middle of the read, we end when we have found a path
//of nonerror kmers which bridges the anchor kmers
//and doesn't terminate too early (i.e. before min_distance_of_path)
if (!left_anchor_kmer.empty() && !right_anchor_kmer.empty())
{
end_condition = ((adjacent_kmer == right_anchor_kmer) && (current_depth + 1 >= min_distance_of_path));
}
//If we are at either end of the read, we end when we have found a path
//of nonerror kmers which continues until the end of the read
//and doesn't terminate too early (i.e. before min_distance_of_path)
else
{
end_condition = ((current_depth + 1 == max_distance_of_path) && (current_depth + 1 >= min_distance_of_path));
}
if (end_condition)
{
//added this case for if the right and left anchors overlap
if (!left_anchor_kmer.empty() && !right_anchor_kmer.empty() && (path.size() < 2*k))
{
int total_overlaps = 2*k - path.size();
path.clear();
for (int number_overlaps = 0; number_overlaps < total_overlaps; number_overlaps++)
{
path += "-";
}
paths.push_back(path);
}
else
{
if (!left_anchor_kmer.empty())
{
//remove left_anchor_kmer from path
path.erase(path.begin(), path.begin()+k);
}
if (!right_anchor_kmer.empty())
{
//remove right_anchor_kmer from path
path.erase(path.end()-k, path.end());
}
paths.push_back(path);
}
}
// "No path yet\n";
//Else we haven't found a path yet
else
{
// "No path yet\n";
queue.push_back(path);
}
}
}
}
return paths;
}
int minDis(std::string s1, std::string s2, int n, int m, std::vector<std::vector<int>> &dp)
{
// If any string is empty,
// return the remaining characters of other string
if (n==0)
{
return m;
}
if (m==0)
{
return n;
}
// To check if the recursive tree
// for given n & m has already been executed
if (dp[n][m]!=-1)
{
return dp[n][m];
}
// If characters are equal, execute
// recursive function for n-1, m-1
if (s1[n-1] == s2[m-1])
{
if (dp[n-1][m-1] == -1)
{
return dp[n][m] = minDis(s1, s2, n-1, m-1, dp);
}
else
{
return dp[n][m] = dp[n-1][m-1];
}
}
// If characters are nt equal, we need to
// find the minimum cost out of all 3 operations.
else
{
int m1, m2, m3; // temp variables
if (dp[n-1][m]!=-1)
{
m1 = dp[n-1][m];
}
else
{
m1 = minDis(s1, s2, n-1, m, dp);
}
if (dp[n][m-1]!=-1)
{
m2 = dp[n][m-1];
}
else
{
m2 = minDis(s1, s2, n, m-1, dp);
}
if (dp[n-1][m-1]!=-1)
{
m3 = dp[n-1][m-1];
}
else
{
m3 = minDis(s1, s2, n-1, m-1, dp);
}
return dp[n][m] = 1 + std::min(m1, std::min(m2, m3));
}
}
void write_error_paths(CKMCFile& file, std::vector<int>& shared_reads, int k, bool& queue_broken, std::vector<std::string>& edited_error_portions, std::ofstream& errpaths_queuecomplete0_numpaths0_output_file, std::ofstream& errpaths_queuecomplete0_numpaths1to2_output_file, std::ofstream& errpaths_queuecomplete0_numpaths3plus_output_file, std::ofstream& errpaths_queuecomplete1_numpaths0_output_file, std::ofstream& errpaths_queuecomplete1_numpaths1to2_output_file, std::ofstream& errpaths_queuecomplete1_numpaths3plus_output_file, std::ofstream& erredits_output_file, std::string& edited_read, int& read_number, int& first_error_idx, int& last_error_idx, std::string& before_first_error_kmer, std::string& original_error_portion, std::string& after_last_error_kmer, const unordered_map<auto, vector<string>>& local_kmer_db, std::mutex& outputfileMutex)
{
//std::cout << "Writing error paths\n";
//std::cout.flush();
std::string left_part;
if (!before_first_error_kmer.empty())
{
left_part = before_first_error_kmer.substr(1);
}
else
{
left_part = before_first_error_kmer;
}
std::string right_part;
if (!after_last_error_kmer.empty())
{
right_part = after_last_error_kmer.substr(0, after_last_error_kmer.length()-1);
}
else
{
right_part = after_last_error_kmer;
}
std::size_t found = original_error_portion.find("-");
std::string original;
if (found!=std::string::npos)
{
original = left_part + right_part.substr(original_error_portion.size());
}
else
{
original = left_part + original_error_portion + right_part;
}
auto is_less_than = [&](std::string edited_error_portion1, std::string edited_error_portion2)
{
int n = original.length();
std::size_t found1 = edited_error_portion1.find("-");
std::string portion1;
if (found1!=std::string::npos)
{
portion1 = left_part + right_part.substr(edited_error_portion1.size());
}
else
{
portion1 = left_part + edited_error_portion1 + right_part;
}
int m = portion1.length();
std::vector<std::vector<int>> dp(n+1, std::vector<int>(m+1, -1));
int dist1 = minDis(original, portion1, n, m, dp);
std::size_t found2 = edited_error_portion2.find("-");
std::string portion2;
if (found2!=std::string::npos)
{
portion2 = left_part + right_part.substr(edited_error_portion2.size());
}
else
{
portion2 = left_part + edited_error_portion2 + right_part;
}
m = portion2.length();
std::vector<std::vector<int>> dp2(n+1, std::vector<int>(m+1, -1));
int dist2 = minDis(original, portion2, n, m, dp2);
return dist1 < dist2;
};
std::sort(edited_error_portions.begin(), edited_error_portions.end(), is_less_than);
std::ofstream* errwrite_output_file;
bool was_edited = false;
//we finished the search and presumably we have found one homozygous path or two heterozygous paths
//if ((!queue_broken) && ((edited_error_portions.size() == 1) or (edited_error_portions.size() == 2)))
if (edited_error_portions.size() >= 1)
{
//errwrite_output_file = &erredits_output_file;
//std::cout << "Finished the search\n";
//std::cout.flush();
found = edited_error_portions[0].find("-");
if (found!=std::string::npos)
{
edited_read += left_part.substr(0, left_part.size() - edited_error_portions[0].size());
}
else
{
edited_read += left_part + edited_error_portions[0];
}
if (edited_error_portions[0] != original_error_portion)
{
was_edited = true;
}
//if (!before_first_error_kmer.empty())
//{
// edited_read += before_first_error_kmer.substr(1) + edited_error_portions[0];
//}
//else
//{
// edited_read += edited_error_portions[0];
//}
}
//there are no paths, or there are more than two paths, or the search wasn't finished.
//We are currently not editing.
else
{
//errwrite_output_file = &errpaths_output_file;
found = original_error_portion.find("-");
if (found!=std::string::npos)
{
edited_read += left_part.substr(0, left_part.size() - original_error_portion.size());
}
else
{
edited_read += left_part + original_error_portion;
}
//if (!before_first_error_kmer.empty())
//{
// edited_read += before_first_error_kmer.substr(1) + original_error_portion;
//}
//else
//{
// edited_read += original_error_portion;
//}
}
if ((!queue_broken) && ((edited_error_portions.size() == 1) or (edited_error_portions.size() == 2)))
{
errwrite_output_file = &errpaths_queuecomplete1_numpaths1to2_output_file;
}
if ((!queue_broken) && (edited_error_portions.size() < 1))
{
errwrite_output_file = &errpaths_queuecomplete1_numpaths0_output_file;
}
if ((!queue_broken) && (edited_error_portions.size() > 2))
{
errwrite_output_file = &errpaths_queuecomplete1_numpaths3plus_output_file;
}
if ((queue_broken) && ((edited_error_portions.size() == 1) or (edited_error_portions.size() == 2)))
{
errwrite_output_file = &errpaths_queuecomplete0_numpaths1to2_output_file;
}
if ((queue_broken) && (edited_error_portions.size() < 1))
{
errwrite_output_file = &errpaths_queuecomplete0_numpaths0_output_file;
}
if ((queue_broken) && (edited_error_portions.size() > 2))
{
errwrite_output_file = &errpaths_queuecomplete0_numpaths3plus_output_file;
}
outputfileMutex.lock();
std::string original_error_block;
found = original_error_portion.find("-");
if (found!=std::string::npos)
{
original_error_block = before_first_error_kmer + after_last_error_kmer.substr(original_error_portion.size());
}
else
{
original_error_block = before_first_error_kmer + original_error_portion + after_last_error_kmer;
}
*errwrite_output_file << ">read" << read_number << "_firsterrorkmer" << first_error_idx << "_lasterrorkmer" << last_error_idx << "_original" << '\n';
*errwrite_output_file << before_first_error_kmer << " " << original_error_portion << " " << after_last_error_kmer << '\n';
if (was_edited)
{
erredits_output_file << ">read" << read_number << "_firsterrorkmer" << first_error_idx << "_lasterrorkmer" << last_error_idx << "_original" << '\n';
erredits_output_file << before_first_error_kmer << " " << original_error_portion << " " << after_last_error_kmer << '\n';
}
std::vector<uint32_t> w;
//file.GetCountersForRead(original_error_block, w);
if (shared_reads.size() > 30) {
get_local_counts(original_error_block, local_kmer_db, w, k);
} else {
file.GetCountersForRead(original_error_block, w);
}
for (int j=0; j < w.size(); j++)
{
*errwrite_output_file << w.at(j) << " ";
if (was_edited)
{
erredits_output_file << w.at(j) << " ";
}
}
*errwrite_output_file << '\n';
if (was_edited)
{
erredits_output_file << '\n';
}
for (int l = 0; l < edited_error_portions.size(); l++)
{
std::string edited_error_portion = edited_error_portions[l];
std::string edited_error_block;
found = edited_error_portion.find("-");
if (found!=std::string::npos)
{
edited_error_block = before_first_error_kmer + after_last_error_kmer.substr(edited_error_portion.size());
}
else
{
edited_error_block = before_first_error_kmer + edited_error_portion + after_last_error_kmer;
}
*errwrite_output_file << ">read" << read_number << "_firsterrorkmer" << first_error_idx << "_lasterrorkmer" << last_error_idx << "_path" << l << '\n';
*errwrite_output_file << before_first_error_kmer << " " << edited_error_portion << " " << after_last_error_kmer << '\n';
if ((l==0) && (was_edited))
{
erredits_output_file << ">read" << read_number << "_firsterrorkmer" << first_error_idx << "_lasterrorkmer" << last_error_idx << "_edited" << '\n';
erredits_output_file << before_first_error_kmer << " " << edited_error_portion << " " << after_last_error_kmer << '\n';
}
//file.GetCountersForRead(edited_error_block, w);
w.clear();
if (shared_reads.size() > 30) {
get_local_counts(edited_error_block, local_kmer_db, w, k);
} else {
file.GetCountersForRead(edited_error_block, w);
}
for (int j=0; j < w.size(); j++)
{
*errwrite_output_file << w.at(j) << " ";
if ((l==0) && (was_edited))
{
erredits_output_file << w.at(j) << " ";
}
}
*errwrite_output_file << '\n';
if ((l==0) && (was_edited))
{
erredits_output_file << '\n';
}
}
outputfileMutex.unlock();
}
void write_nonhom_paths(CKMCFile& file, std::vector<int>& shared_reads, int k, bool& queue_broken, std::vector<std::string>& smoothed_nonhom_portions, std::ofstream& hetpaths_queuecomplete0_numpaths0to1_output_file, std::ofstream& hetpaths_queuecomplete0_numpaths2_output_file, std::ofstream& hetpaths_queuecomplete0_numpaths3plus_output_file, std::ofstream& hetpaths_queuecomplete1_numpaths0to1_output_file, std::ofstream& hetpaths_queuecomplete1_numpaths2_output_file, std::ofstream& hetpaths_queuecomplete1_numpaths3plus_output_file, std::ofstream& hetedits_output_file, std::string& smoothed_read, int& read_number, int& first_nonhom_idx, int& last_nonhom_idx, std::string& before_first_nonhom_kmer, std::string& original_nonhom_portion, std::string& after_last_nonhom_kmer, const unordered_map<auto, vector<string>>& local_kmer_db, int& strict, std::mutex& outputfileMutex)
{
std::string left_part;
if (!before_first_nonhom_kmer.empty())
{
left_part = before_first_nonhom_kmer.substr(1);
}
else
{
left_part = before_first_nonhom_kmer;
}
std::string right_part;
if (!after_last_nonhom_kmer.empty())
{
right_part = after_last_nonhom_kmer.substr(0, after_last_nonhom_kmer.length()-1);
}
else
{
right_part = after_last_nonhom_kmer;
}
auto is_greater_than = [&](std::string smoothed_nonhom_portion1, std::string smoothed_nonhom_portion2)
{
std::vector<uint32_t> w;
std::size_t found1 = smoothed_nonhom_portion1.find("-");
//added this case to account for when anchors overlap
if (found1!=std::string::npos)
{
string left_right = left_part + right_part.substr(smoothed_nonhom_portion1.size());
if (shared_reads.size() > 30) {
get_local_counts(left_right, local_kmer_db, w, k);
} else {
file.GetCountersForRead(left_right, w);
}
//file.GetCountersForRead(left_part + right_part.substr(smoothed_nonhom_portion1.size()), w);
}
else
{
string left_right = left_part + smoothed_nonhom_portion1 + right_part;
if (shared_reads.size() > 30) {
get_local_counts(left_right, local_kmer_db, w, k);
} else {
file.GetCountersForRead(left_right, w);
}
//file.GetCountersForRead(left_part + smoothed_nonhom_portion1 + right_part, w);
}
//std::sort(w.begin(), w.end());
float average1 = std::accumulate(w.begin(), w.end(), 0.0)/w.size();
std::size_t found2 = smoothed_nonhom_portion2.find("-");
//added this case to account for when anchors overlap
if (found2!=std::string::npos)
{
string left_right = left_part + right_part.substr(smoothed_nonhom_portion2.size());
if (shared_reads.size() > 30) {
get_local_counts(left_right, local_kmer_db, w, k);
} else {
file.GetCountersForRead(left_right, w);
}
//file.GetCountersForRead(left_part + right_part.substr(smoothed_nonhom_portion2.size()), w);
}
else
{
string left_right = left_part + smoothed_nonhom_portion2 + right_part;
if (shared_reads.size() > 30) {
get_local_counts(left_right, local_kmer_db, w, k);
} else {
file.GetCountersForRead(left_right, w);
}
//file.GetCountersForRead(left_part + smoothed_nonhom_portion2 + right_part, w);
}
//std::sort(w.begin(), w.end());
float average2 = std::accumulate(w.begin(), w.end(), 0.0)/w.size();
return average1 > average2;
};
std::sort(smoothed_nonhom_portions.begin(), smoothed_nonhom_portions.end(), is_greater_than);
std::ofstream* hetwrite_output_file;
//we finished the search and presumably we have found two heterozygous paths
//actually, let's relax the !queue_broken requirement, jk restoring this requirement
//and when strict==1 we only smooth when there are exactly two paths
//and when strict==0 we smooth when there are at least two paths
//if ((!queue_broken) && (smoothed_nonhom_portions.size() == 2))
bool end_condition;
bool was_smoothed = false;
std::size_t found;
if (strict==1)
{
end_condition = ((!queue_broken) && (smoothed_nonhom_portions.size() == 2));
}
else
{
end_condition = ((!queue_broken) && (smoothed_nonhom_portions.size() >= 2));
}
if (end_condition)
{
found = smoothed_nonhom_portions[0].find("-");
if (found!=std::string::npos)
{
smoothed_read += left_part.substr(0, left_part.size() - smoothed_nonhom_portions[0].size());
}
else
{
smoothed_read += left_part + smoothed_nonhom_portions[0];
}
//We have checked that the condition for smoothing was met
//Now we check whether the path chosen actually differs from the original
//If so, then there was a true smooth and we add this to hetedits_output_file
if (smoothed_nonhom_portions[0] != original_nonhom_portion)
{
was_smoothed = true;
}
}
//there is not exactly two paths, or the search wasn't finished.
//We are currently not smoothing.
else
{
found = original_nonhom_portion.find("-");
if (found!=std::string::npos)
{
smoothed_read += left_part.substr(0, left_part.size() - original_nonhom_portion.size());
}
else
{
smoothed_read += left_part + original_nonhom_portion;
}
}
if ((!queue_broken) && (smoothed_nonhom_portions.size() == 2))
{
hetwrite_output_file = &hetpaths_queuecomplete1_numpaths2_output_file;
}
if ((!queue_broken) && (smoothed_nonhom_portions.size() < 2))
{
hetwrite_output_file = &hetpaths_queuecomplete1_numpaths0to1_output_file;
}
if ((!queue_broken) && (smoothed_nonhom_portions.size() > 2))
{
hetwrite_output_file = &hetpaths_queuecomplete1_numpaths3plus_output_file;
}
if ((queue_broken) && (smoothed_nonhom_portions.size() == 2))
{
hetwrite_output_file = &hetpaths_queuecomplete0_numpaths2_output_file;
}
if ((queue_broken) && (smoothed_nonhom_portions.size() < 2))
{
hetwrite_output_file = &hetpaths_queuecomplete0_numpaths0to1_output_file;
}
if ((queue_broken) && (smoothed_nonhom_portions.size() > 2))
{
hetwrite_output_file = &hetpaths_queuecomplete0_numpaths3plus_output_file;
}
outputfileMutex.lock();
std::string original_nonhom_block;
found = original_nonhom_portion.find("-");
if (found!=std::string::npos)
{
original_nonhom_block = before_first_nonhom_kmer + after_last_nonhom_kmer.substr(original_nonhom_portion.size());
}
else
{
original_nonhom_block = before_first_nonhom_kmer + original_nonhom_portion + after_last_nonhom_kmer;
}
*hetwrite_output_file << ">read" << read_number << "_firstnonhomkmer" << first_nonhom_idx << "_lastnonhomkmer" << last_nonhom_idx << "_original" << '\n';
*hetwrite_output_file << before_first_nonhom_kmer << " " << original_nonhom_portion << " " << after_last_nonhom_kmer << '\n';
if (was_smoothed)
{
hetedits_output_file << ">read" << read_number << "_firstnonhomkmer" << first_nonhom_idx << "_lastnonhomkmer" << last_nonhom_idx << "_original" << '\n';
hetedits_output_file << before_first_nonhom_kmer << " " << original_nonhom_portion << " " << after_last_nonhom_kmer << '\n';
}
std::vector<uint32_t> w;
if (shared_reads.size() > 30) {
get_local_counts(original_nonhom_block, local_kmer_db, w, k);
} else {
file.GetCountersForRead(original_nonhom_block, w);
}
//file.GetCountersForRead(original_nonhom_block, w);
for (int j=0; j < w.size(); j++)
{
*hetwrite_output_file << w.at(j) << " ";
if (was_smoothed)
{
hetedits_output_file << w.at(j) << " ";
}
}
*hetwrite_output_file << '\n';
if (was_smoothed)
{
hetedits_output_file << '\n';
}
for (int l = 0; l < smoothed_nonhom_portions.size(); l++)
{
std::string smoothed_nonhom_portion = smoothed_nonhom_portions[l];
std::string smoothed_nonhom_block;
found = smoothed_nonhom_portion.find("-");
if (found!=std::string::npos)
{
smoothed_nonhom_block = before_first_nonhom_kmer + after_last_nonhom_kmer.substr(smoothed_nonhom_portion.size());
}
else
{
smoothed_nonhom_block = before_first_nonhom_kmer + smoothed_nonhom_portion + after_last_nonhom_kmer;
}
*hetwrite_output_file << ">read" << read_number << "_firstnonhomkmer" << first_nonhom_idx << "_lastnonhomkmer" << last_nonhom_idx << "_path" << l << '\n';
*hetwrite_output_file << before_first_nonhom_kmer << " " << smoothed_nonhom_portion << " " << after_last_nonhom_kmer << '\n';
if ((l==0) && (was_smoothed))
{
hetedits_output_file << ">read" << read_number << "_firstnonhomkmer" << first_nonhom_idx << "_lastnonhomkmer" << last_nonhom_idx << "_smoothed" << '\n';
hetedits_output_file << before_first_nonhom_kmer << " " << smoothed_nonhom_portion << " " << after_last_nonhom_kmer << '\n';
}
w.clear();
if (shared_reads.size() > 30) {
get_local_counts(smoothed_nonhom_block, local_kmer_db, w, k);
} else {
file.GetCountersForRead(smoothed_nonhom_block, w);
}
//file.GetCountersForRead(smoothed_nonhom_block, w);
for (int j=0; j < w.size(); j++)
{
*hetwrite_output_file << w.at(j) << " ";
if ((l==0) && (was_smoothed))
{
hetedits_output_file << w.at(j) << " ";
}
}
*hetwrite_output_file << '\n';
if ((l==0) && (was_smoothed))
{
hetedits_output_file << '\n';
}
}
outputfileMutex.unlock();
}
std::string remove_err (CKMCFile& file, std::vector<int>& shared_reads, std::vector<uint32_t>& v, std::string& read, int& read_number, const unordered_map<auto, vector<string>>& local_kmer_db, std::ofstream& errpaths_queuecomplete0_numpaths0_output_file, std::ofstream& errpaths_queuecomplete0_numpaths1to2_output_file, std::ofstream& errpaths_queuecomplete0_numpaths3plus_output_file, std::ofstream& errpaths_queuecomplete1_numpaths0_output_file, std::ofstream& errpaths_queuecomplete1_numpaths1to2_output_file, std::ofstream& errpaths_queuecomplete1_numpaths3plus_output_file, std::ofstream& erredits_output_file, int& error_threshold, int& het_threshold, int& unique_threshold, int& max_nodes_to_search, double& distance_multiplier, int& k, std::mutex& outputfileMutex)
{
//initialize variables
std::string edited_read;
//int k = 21;
//iterate over counts to edit errors
int previous_type = -1;
int first_nonerror_idx;
int last_nonerror_idx;
int first_error_idx;
int last_error_idx;
std::string before_first_error_kmer;
std::string after_last_error_kmer;
for (int i = 0; i < v.size(); i++)
{
int current_type = get_type(v[i], error_threshold, het_threshold, unique_threshold);
//std::cout << "Current type: " << current_type << "\n";
//std::cout.flush();
//if kmer is error
if (current_type == 0)
{
//if this is the first kmer of the read
if (previous_type == -1)
{
first_error_idx = i;
last_nonerror_idx = i-1;