-
Notifications
You must be signed in to change notification settings - Fork 19
/
guidedassembler.hpp
1514 lines (1369 loc) · 76.6 KB
/
guidedassembler.hpp
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
/*===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
*/
#ifndef _GuidedAssembler_
#define _GuidedAssembler_
#include <smmintrin.h>
using namespace std;
namespace DeBruijn {
class CGuidedAssembler {
public:
CGuidedAssembler(int kmer_len, int secondary_kmer_len, bool extend_ends, bool protect_ends, bool keep_subgraphs, int min_count, double fraction, int word_size, int gap_open, int gap_extend,
int drop_off, int kmer_complexity, double max_fork_density, int buf_length, int ncores, list<array<CReadHolder,2>>& raw_reads,
int estimated_kmer_num, bool skip_bloom_filter, int not_aligned_len, int not_aligned_count, int aligned_count, int maxp, double target_coverage, int min_hit_len,
bool no_reads, bool no_pairs, int secondary_kmer_threshold, bool remove_homopolymer_indels, int homopolymer_len, double homopolymer_ratio) :
m_kmer_len(kmer_len), m_secondary_kmer_len(secondary_kmer_len),
m_extend_ends(extend_ends), m_protect_ends(protect_ends), m_keep_subgraphs(keep_subgraphs), m_min_count(min_count), m_fraction(fraction), m_word_size(word_size),
m_gap_open(gap_open), m_gap_extend(gap_extend), m_drop_off(drop_off), m_kmer_complexity(kmer_complexity), m_buf_length(buf_length), m_ncores(ncores),
m_not_aligned_len(not_aligned_len), m_not_aligned_count(not_aligned_count), m_aligned_count(aligned_count), m_maxp(maxp), m_target_coverage(target_coverage), m_min_hit_len(min_hit_len),
m_graph_uniformity(0), m_no_reads(no_reads), m_no_pairs(no_pairs), m_raw_reads(raw_reads),
m_secondary_kmer_threshold(secondary_kmer_threshold), m_remove_homopolymer_indels(remove_homopolymer_indels), m_homopolymer_len(homopolymer_len), m_homopolymer_ratio(homopolymer_ratio) {
if(m_kmer_complexity == 0)
m_kmer_complexity = numeric_limits<int>::max();
m_min_bases_per_fork = 0;
if(max_fork_density > 0)
m_min_bases_per_fork = 1./max_fork_density;
m_graphp.reset(CreateGraph(estimated_kmer_num, skip_bloom_filter, m_kmer_len, m_min_count));
if(m_secondary_kmer_len != m_kmer_len)
m_secondary_graphp.reset(CreateGraph(estimated_kmer_num, skip_bloom_filter, m_secondary_kmer_len, m_min_count));
else
m_secondary_graphp = m_graphp;
}
virtual ~CGuidedAssembler() = default;
void PrintRslt(ofstream& gfa_out, ofstream& all_variants, int maxv, ofstream& selected_variants, bool use_ambiguous) {
/*
for(auto& gfa_graph : m_gfa_collection) {
gfa_graph.CutToChunks();
gfa_graph.GenerateKmersAndScores(*m_secondary_graphp);
}
*/
if(use_ambiguous) {
SVarNum variants = 0;
for(auto& gfa_graph : m_gfa_collection) {
gfa_graph.SnpsToAmbig();
variants += gfa_graph.NumberOfVariants();
}
cerr << "Variants after SNP collaps: " << variants.ToString() << endl;
}
for(auto& gfa_graph : m_gfa_collection)
gfa_graph.PrintGFA(gfa_out);
gfa_out.close();
if(!gfa_out) {
cerr << "Can't write to gfa output" << endl;
exit(1);
}
if(all_variants.is_open()) {
set<string> seqs;
for(auto& gfa_graph : m_gfa_collection) {
if(!m_keep_subgraphs)
seqs.clear();
gfa_graph.PrintAllVariants(all_variants, maxv, seqs);
}
all_variants.close();
if(!all_variants) {
cerr << "Can't write to all variants output" << endl;
exit(1);
}
}
if(selected_variants.is_open()) {
for(auto& gfa_graph : m_gfa_collection)
gfa_graph.PrintSelectedVariants(selected_variants);
selected_variants.close();
if(!selected_variants) {
cerr << "Can't write to selected variants output" << endl;
exit(1);
}
}
}
DBGraph& Graph() { return *m_graphp; }
protected:
virtual void ReadTargets(ifstream& targets_in) = 0;
virtual void AssemblerJob(TGFACollection& rslts) = 0;
void AssembleGraphs() { // assemble
vector<TGFACollection> thread_rslts(m_ncores);
list<function<void()>> jobs;
for(int thr = 0; thr < m_ncores; ++thr) {
jobs.push_back(bind(&CGuidedAssembler::AssemblerJob, this, ref(thread_rslts[thr])));
}
RunThreads(m_ncores, jobs);
for(auto& trslt : thread_rslts)
m_gfa_collection.splice(m_gfa_collection.end(), trslt);
}
void Assemble(ifstream& targets_in, bool clip_to_codons) {
int jump = 50; //not really used
int low_count = m_min_count;
m_graphdiggerp.reset(new GraphDigger(*m_graphp, m_fraction, jump, low_count));
m_secondary_graphdiggerp.reset(new GraphDigger(*m_secondary_graphp, m_fraction, jump, low_count));
//read fasta
ReadTargets(targets_in);
CStopWatch timer;
timer.Restart();
CreateGraphHash();
cerr << "Graph hash in " << timer.Elapsed();
timer.Restart();
AssembleGraphs();
cerr << "Assembling in " << timer.Elapsed();
timer.Restart();
ClearGraphHash();
cerr << "Graph hash clear in " << timer.Elapsed();
if(m_keep_subgraphs) {
SortCollection(m_gfa_collection);
EnumerateCollection(m_gfa_collection);
} else {
timer.Restart();
RemoveRedundantGraphs(m_gfa_collection);
cerr << "Remove redundant in " << timer.Elapsed();
}
SVarNum total_variants;
for(auto& gfa_graph : m_gfa_collection) {
SVarNum vars = gfa_graph.NumberOfVariants();
total_variants += vars;
auto& acc = gfa_graph.Target();
int group = gfa_graph.front().m_group;
cerr << acc << ":" << group << " variants before " << vars.ToString() << endl;
}
cerr << "Variants: " << total_variants.ToString() << endl;
if((m_no_reads && m_no_pairs) || m_gfa_collection.empty())
return;
GraphCleaner<TGFACollection> graph_cleaner(m_gfa_collection, &m_targets, *m_graphp, m_fraction, 0.51, m_not_aligned_len, m_not_aligned_count, m_aligned_count, m_maxp,
m_no_reads, m_no_pairs, m_raw_reads, m_ncores);
{
TGFACollection rslts;
for(auto& gfa_graph : m_gfa_collection) {
gfa_graph.RemoveHair(*m_secondary_graphdiggerp, m_fraction);
gfa_graph.CalculateChainLength();
int min_len = get<1>(m_targets[gfa_graph.Target()]);
gfa_graph.TrimGroups(m_graph_uniformity, min_len);
gfa_graph.MergeRedundantLinks();
if(clip_to_codons)
gfa_graph.ClipToCodons();
gfa_graph.AssignGroupNumber();
auto splitted = gfa_graph.SplitGroups();
for(auto& graph : splitted) {
if(m_min_count == 1) {
graph.RemoveSinglReadSnps(*m_secondary_graphp);
}
if(graph.CollapsFreeEnds() > 0)
graph.MergeRedundantLinks();
if(m_extend_ends) {
graph.GenerateKmers(*m_graphp); // kmers for calculating extension
graph.ExtendToFirstFork(*m_graphdiggerp);
}
if(m_remove_homopolymer_indels)
graph.RemoveHomopolymerIndels(*m_secondary_graphp, m_homopolymer_ratio, m_homopolymer_len);
graph.GenerateKmersAndScores(*m_secondary_graphp); // kmers for scores
graph.ScoreGraph(get<2>(m_targets[graph.Target()]), m_word_size);
}
rslts.splice(rslts.end(), splitted);
}
swap(m_gfa_collection, rslts);
}
if(m_keep_subgraphs) {
SortCollection(m_gfa_collection);
EnumerateCollection(m_gfa_collection);
} else {
timer.Restart();
RemoveRedundantGraphs(m_gfa_collection);
cerr << "Remove redundant in " << timer.Elapsed();
}
total_variants = 0;
for(auto& gfa_graph : m_gfa_collection) {
SVarNum vars = gfa_graph.NumberOfVariants();
total_variants += vars;
auto& acc = gfa_graph.Target();
int group = gfa_graph.front().m_group;
cerr << acc << ":" << group << " variants after " << vars.ToString() << endl;
}
cerr << "Variants: " << total_variants.ToString() << endl;
}
CDBHashGraph* CreateGraph(int estimated_kmer_num, bool skip_bloom_filter, int kmer_len, int min_count) {
int64_t M = 1000000;
CKmerHashCounter kmer_counter(m_raw_reads, kmer_len, min_count, M*estimated_kmer_num, true, m_ncores, skip_bloom_filter);
if(kmer_counter.KmerNum() == 0)
throw runtime_error("Not enough quality reads which are at least "+to_string(kmer_len)+"bp long");
kmer_counter.GetBranches();
return new CDBHashGraph(move(kmer_counter.Kmers()), true);
}
typedef vector<vector<pair<Node,int>>> TGraphHash; // pair is node,count
void CreateGraphHash() { // hash for kmers
m_kmer_hash.resize(1ULL << 2*m_word_size);
vector<SAtomic<uint8_t>> centinel(m_kmer_hash.size());
{
vector<typename DBGraph::Iterator> chunks = m_graphp->Chunks(m_ncores);
list<function<void()>> jobs;
for(int thr = 0; thr < (int)chunks.size()-1; ++thr)
jobs.push_back(bind(&CGuidedAssembler::GraphHashJob, this, chunks[thr], chunks[thr+1], ref(centinel)));
RunThreads(m_ncores, jobs);
}
{
list<function<void()>> jobs;
for(int thr = 0; thr < m_ncores; ++thr)
jobs.push_back(bind(&CGuidedAssembler::GraphHashSortJob, this, ref(centinel)));
RunThreads(m_ncores, jobs);
}
}
void GraphHashJob(typename DBGraph::Iterator from, typename DBGraph::Iterator to, vector<SAtomic<uint8_t>>& centinel) {
uint64_t mask = (1ULL << 2*m_word_size)-1;
for(auto it = from; it != to; ++it) {
int abundance = m_graphp->Abundance(it);
if(abundance <= 1) // for min_count 1
continue;
TKmer kmer = m_graphp->GetNodeKmer(it);
uint64_t word = *kmer.getPointer()&mask; // last symbols of forward kmer
while(!centinel[word].Set(1, 0));
m_kmer_hash[word].emplace_back(it, abundance);
centinel[word] = 0;
TKmer rkmer = revcomp(kmer, m_kmer_len);
word = *rkmer.getPointer()&mask; // last symbols of reversed kmer
while(!centinel[word].Set(1, 0));
m_kmer_hash[word].emplace_back(m_graphp->ReverseComplement(it), abundance);
centinel[word] = 0;
}
}
void GraphHashSortJob(vector<SAtomic<uint8_t>>& centinel) {
for(size_t i = 0; i < centinel.size(); ++i) {
if(!centinel[i].Set(1,0))
continue;
sort(m_kmer_hash[i].begin(), m_kmer_hash[i].end(), [](const pair<Node,int>& a, const pair<Node, int>& b) { return a.second > b.second; });
}
}
void ClearGraphHash() {
int step = m_kmer_hash.size()/m_ncores+1;
list<function<void()>> jobs;
for(int thr = 0; thr < m_ncores; ++thr)
jobs.push_back(bind(&CGuidedAssembler::GraphHashClearJob, this, step*thr, step));
RunThreads(m_ncores, jobs);
m_kmer_hash.clear();
}
void GraphHashClearJob(size_t from, size_t step) {
for(size_t i = from; i < min(from+step, m_kmer_hash.size()); ++i)
m_kmer_hash[i].clear();
}
/*
int BlclLim(int block) {
int num = pow(4, block);
vector<double> transision(num+1);
for(int n = 0; n <= num; ++n)
transision[n] = double(n)/num;
vector<double> previous(num+1);
previous[0] = 1;
for(int m = 0; m < m_kmer_len/block; ++m) {
vector<double> next(num+1);
for(int n = 0; n <= num; ++n) {
double p = transision[n];
next[n] += previous[n]*p;
if(n < num)
next[n+1] += previous[n]*(1-p);
}
swap(previous, next);
}
int lim = 0;
double eps = 0;
for( ; lim < num && eps+previous[lim+1] <= m_lc_level; ++lim, eps += previous[lim]);
return lim;
}
bool SimplicityCheck(const TKmer& kmer, int block, int lim) {
uint64_t present = 0; // each bit represents different symbol combination
uint64_t mask = (1ULL << 2*block) - 1;
const uint64_t* data = kmer.getPointer();
for(int s = 0; s <= m_kmer_len-block; s += block) {
int p = s/32;
int shift = s%32;
int remain = 32-shift;
uint64_t w = (data[p] >> 2*shift);
if(remain < block) // need symbols from next word
w |= (data[p+1] << 2*(block-remain));
w &= mask;
present |= (1ULL << w);
if(_mm_popcnt_u64(present) > lim)
return false;
}
return true;
}
bool SimplKmer(const TKmer& kmer) {
return SimplicityCheck(kmer, 2, m_blc2) || SimplicityCheck(kmer, 3, m_blc3);
}
int BCounter(const TKmer& kmer, int block) {
uint64_t present = 0; // each bit represents different symbol combination
uint64_t mask = (1ULL << 2*block) - 1;
const uint64_t* data = kmer.getPointer();
for(int s = 0; s <= m_kmer_len-block; s += block) {
int p = s/32;
int shift = s%32;
int remain = 32-shift;
uint64_t w = (data[p] >> 2*shift);
if(remain < block) // need symbols from next word
w |= (data[p+1] << 2*(block-remain));
w &= mask;
present |= (1ULL << w);
}
return _mm_popcnt_u64(present);
}
*/
bool CheckSeed(Node seed, int left_target_len, int right_target_len) {
int margin = numeric_limits<int>::min();
if(m_protect_ends)
margin = max(100, m_kmer_len);
for(int dir = 0; dir < 2; ++dir) {
set<Node> kmers;
kmers.insert(seed);
for(int step = 0; step < m_kmer_len; ++step) {
set<Node> new_kmers;
for(auto& node : kmers) {
int remaining_len = right_target_len-step;
int back_step_len = left_target_len+step+1; // 1 because we will make a 1bp step
bool check_forward = margin < remaining_len;
bool check_backward = margin < back_step_len;
vector<Successor> neighbors = m_graphdiggerp->GetReversibleNodeSuccessorsF(node, nullptr, check_forward, check_backward);
for(auto& suc : neighbors)
new_kmers.insert(suc.m_node);
}
if(new_kmers.empty())
return false;
swap(kmers, new_kmers);
}
seed = seed.ReverseComplement();
swap(left_target_len, right_target_len);
}
return true;
}
TGraphHash m_kmer_hash;
TGFACollection m_gfa_collection;
TTargets m_targets; // [accession], seq, min_len, words, centinel
unique_ptr<GraphDigger> m_graphdiggerp;
unique_ptr<GraphDigger> m_secondary_graphdiggerp;
shared_ptr<DBGraph> m_graphp;
shared_ptr<DBGraph> m_secondary_graphp;
int m_kmer_len;
int m_secondary_kmer_len;
bool m_extend_ends;
bool m_protect_ends;
bool m_keep_subgraphs;
int m_min_count;
double m_fraction;
int m_word_size;
int m_gap_open;
int m_gap_extend;
int m_drop_off;
unsigned m_kmer_complexity;
double m_min_bases_per_fork;
int m_buf_length;
int m_ncores;
int m_not_aligned_len;
int m_not_aligned_count;
int m_aligned_count;
int m_maxp;
double m_target_coverage;
int m_min_hit_len;
double m_graph_uniformity;
bool m_no_reads;
bool m_no_pairs;
list<array<CReadHolder,2>>& m_raw_reads;
int m_secondary_kmer_threshold;
bool m_remove_homopolymer_indels;
int m_homopolymer_len;
double m_homopolymer_ratio;
mutex m_out_mutex;
};
class CGuidedAssemblerNA : public CGuidedAssembler {
public:
CGuidedAssemblerNA(int kmer_len, int secondary_kmer_len, bool extend_ends, bool protect_ends, bool keep_subgraphs, int min_count, double fraction, int seed_prec, int word_size, int match, int mismatch, int gap_open, int gap_extend,
int drop_off, int kmer_complexity, double max_fork_density, int buf_length, int ncores, list<array<CReadHolder,2>>& raw_reads, ifstream& targets_in,
int estimated_kmer_num, bool skip_bloom_filter, int not_aligned_len, int not_aligned_count, int aligned_count, int maxp, double target_coverage, int min_hit_len,
bool no_reads, bool no_pairs, int secondary_kmer_threshold, bool remove_homopolymer_indels, int homopolymer_len, double homopolymer_ratio) :
CGuidedAssembler(kmer_len, secondary_kmer_len, extend_ends, protect_ends, keep_subgraphs, min_count, fraction, word_size, gap_open, gap_extend,
drop_off, kmer_complexity, max_fork_density, buf_length, ncores, raw_reads,
estimated_kmer_num, skip_bloom_filter, not_aligned_len, not_aligned_count, aligned_count, maxp, target_coverage, min_hit_len,
no_reads, no_pairs, secondary_kmer_threshold, remove_homopolymer_indels, homopolymer_len, homopolymer_ratio),
m_seed_prec(seed_prec), m_match(match), m_mismatch(mismatch), m_delta(match, mismatch) {
Assemble(targets_in, false);
}
private:
void ReadTargets(ifstream& targets_in) {
char c;
if(!(targets_in >> c) || c != '>')
throw runtime_error("Invalid fasta file format for targets");
string record;
while(getline(targets_in, record, '>')) {
while(!targets_in.eof() && record.back() != '\n') {
string part;
getline(targets_in, part, '>');
record += '>'+part;
}
size_t first_ret = min(record.size(),record.find('\n'));
if(first_ret == string::npos)
throw runtime_error("Invalid fasta file format for targets");
string acc = record.substr(0, first_ret);
acc = acc.substr(0, acc.find_first_of(" \t"));
string target = record.substr(first_ret+1);
target.erase(remove(target.begin(),target.end(),'\n'),target.end());
for(char& c : target) c = toupper(c);
if(target.find_first_not_of("ACGTYRWSKMDVHBXN") != string::npos)
throw runtime_error("Invalid sequence in fasta file for targets");
get<0>(m_targets[acc]) = target;
get<1>(m_targets[acc]) = min(target.size()*m_target_coverage, (double)m_min_hit_len);
}
if(targets_in.bad())
throw runtime_error("Error in reading targets");
}
void AssemblerJob(TGFACollection& rslts) {
for(auto& item : m_targets) {
if(!get<3>(item.second).Set(1,0))
continue;
string& target = get<0>(item.second);
const string& acc = item.first;
double anchor_frac = 0.25;
int min_len = get<1>(item.second);
int target_len = target.size();
if((int)target.size() < m_kmer_len) {
lock_guard<mutex> guard(m_out_mutex);
cerr << "Skipped short target: " << acc << " " << target.size() << endl;
continue;
}
{
lock_guard<mutex> guard(m_out_mutex);
cerr << "Started assembling: " << acc << endl;
}
string tcopy;
for(char c : target)
tcopy.push_back(toupper(c));
for(int p = 0; p <= (int)tcopy.size()-m_word_size; ++p) {
string seed = tcopy.substr(p, m_word_size);
if(seed.find_first_not_of("ACGT") != string::npos)
continue;
uint32_t word = 0;
for(char c : seed) {
word = word << 2;
word += (find(bin2NT.begin(), bin2NT.end(), c) - bin2NT.begin());
}
get<2>(item.second).insert(word);
}
size_t collapsedL = 0;
size_t collapsedR = 0;
CStopWatch timer;
timer.Restart();
double seed_prec = min((double)m_kmer_len-1, (double)m_mismatch/(m_match+m_mismatch)*m_kmer_len+m_seed_prec);
vector<set<Node>> kmers_for_pos(target_len-(m_kmer_len-1));
vector<int> max_counts_for_pos(target_len-(m_kmer_len-1));
for(int p = 0; p < target_len-m_word_size; ++p) {
string seed = tcopy.substr(p, m_word_size); // last symbols
if(seed.find_first_not_of("ACGT") != string::npos)
continue;
uint32_t word = 0;
for(char c : seed) {
word = word << 2;
word += (find(bin2NT.begin(), bin2NT.end(), c) - bin2NT.begin());
}
array<int,4> pos = {p+m_word_size-m_kmer_len, p+m_word_size-m_kmer_len, p, p}; // before word direct, before word reversed, after word direct, after word reversed
array<unique_ptr<TKmer>,4> tkmerp;
if(pos[0] >= 0) {
string tkmer = tcopy.substr(pos[0], m_kmer_len); // kmer before word
if(tkmer.find_first_not_of("ACGT") == string::npos) {
tkmerp[0].reset(new TKmer(tkmer));
ReverseComplementSeq(tkmer.begin(), tkmer.end());
tkmerp[1].reset(new TKmer(tkmer));
}
}
if(pos[2] <= target_len-m_kmer_len) {
string tkmer = tcopy.substr(p, m_kmer_len); // kmer after word
if(tkmer.find_first_not_of("ACGT") == string::npos) {
tkmerp[2].reset(new TKmer(tkmer));
ReverseComplementSeq(tkmer.begin(), tkmer.end());
tkmerp[3].reset(new TKmer(tkmer));
}
}
if(!tkmerp[0] && !tkmerp[2])
continue;
auto CountMatches = [](uint64_t a, uint64_t b) {
uint64_t w = ~(a^b); // each match will produce 11
w &= (w << 1); // upper bit is 1 only if both bits are 1 (only for matches)
w &= 0xAAAAAAAAAAAAAAAAUL; // each match has 10; all mismatches 00
return _mm_popcnt_u64(w); // count number set of bits == matches
};
for(int wdir = 0; wdir < 2; ++wdir) {
for(auto& node_count : m_kmer_hash[word]) {
Node node = node_count.first;
int kdir = node.isMinus();
if(wdir > 0)
node = node.ReverseComplement();
int templ;
if(wdir == 0 && kdir == 0)
templ = 0;
else if(wdir == 0 && kdir > 0)
templ = 1;
else if(wdir > 0 && kdir == 0)
templ = 3;
else
templ = 2;
if(tkmerp[templ]) {
int leftp = pos[templ];
int count = node_count.second;
const uint64_t* targetp = tkmerp[templ]->getPointer();
const uint64_t* graphp = m_graphp->getPointer(node);
int prec = tkmerp[templ]->getSize()/64;// number of 8-byte blocks
int matches = -(prec*32-m_kmer_len); // empty positions will match
for(int i = 0; i < prec; ++i)
matches += CountMatches(*(targetp+i), *(graphp+i));
if(matches > seed_prec) {
kmers_for_pos[leftp].insert(node);
max_counts_for_pos[leftp] = max(max_counts_for_pos[leftp], count);
if(kmers_for_pos[leftp].size() >= m_kmer_complexity)
break;
}
}
}
if(wdir == 0) {
//reverse
word = ((word & 0x33333333) << 2) | ((word >> 2) & 0x33333333); // swap adjacent pairs
word = ((word & 0x0F0F0F0F) << 4) | ((word >> 4) & 0x0F0F0F0F); // swap nibbles
word = ((word & 0x00FF00FF) << 8) | ((word >> 8) & 0x00FF00FF); // swap bytes
word = ((word & 0x0000FFFF) << 16) | ((word >> 16) & 0x0000FFFF); // swap 16 bit chunks
//complement
word ^= 0xAAAAAAAA;
//shift
word >>= 2*(16-m_word_size);
}
}
}
/*
{
lock_guard<mutex> guard(m_out_mutex);
for(int l = 0; l < kmers_for_pos.size(); ++l)
cerr << "Word hits: " << acc << " " << l << " " << kmers_for_pos[l].size() << endl;
}
*/
{
int ksize = kmers_for_pos.size();
vector<unsigned> kcount(ksize);
for(int p = 0; p < ksize; ++p)
kcount[p] = kmers_for_pos[p].size();
for(int p = 0; p < ksize; ++p) {
if(kcount[p] >= m_kmer_complexity) {
for(int l = p; l < p+m_kmer_len; ++l)
target[l] = 'N';
for(int l = max(0,p-m_kmer_len+1); l < min(p+m_kmer_len,ksize); ++l)
kmers_for_pos[l].clear();
}
}
}
int simpl_kmers = 0;
for(char c : target) {
if(c == 'N')
++simpl_kmers;
}
for(int p = 0; p < (int)kmers_for_pos.size(); ++p) {
auto& kfp = kmers_for_pos[p];
int mcount = max_counts_for_pos[p];
for(auto it_loop = kfp.begin(); it_loop != kfp.end(); ) {
auto it = it_loop++;
auto abundance = m_graphp->Abundance(*it);
if(abundance == 1 || abundance < m_fraction*mcount)
kfp.erase(it);
}
}
unordered_map<Node, list<int>, Node::Hash> positions;
for(int p = 0; p < (int)kmers_for_pos.size(); ++p) {
for(Node node : kmers_for_pos[p])
positions[node].push_back(p);
}
set<int> periodic_points;
for(auto pos : positions) {
if(pos.second.size() > 1) {
for(int p : pos.second)
periodic_points.insert(p);
}
}
unordered_map<Node, int, Node::Hash> target_kmers; // [node] position on target
for(int p = 0; p < (int)kmers_for_pos.size(); ++p) {
if(periodic_points.count(p))
continue;
for(Node node : kmers_for_pos[p])
target_kmers.emplace(node, p);
}
{
lock_guard<mutex> guard(m_out_mutex);
cerr << "Periodic points for : " << acc << " " << periodic_points.size() << " " << target_len << "/" << simpl_kmers << "\n";
cerr << "Seed kmers for " << acc << " " << target_kmers.size() << " in " << timer.Elapsed();
timer.Restart();
}
CGuidedGraph gugraph(m_kmer_len, m_secondary_kmer_len);
while(!target_kmers.empty()) {
unordered_map<Node, int, Node::Hash>::iterator first_kmerp;
typedef unordered_map<Node, int, Node::Hash>::value_type elem_t;
first_kmerp = min_element(target_kmers.begin(), target_kmers.end(),
[this](const elem_t& a, const elem_t& b)
{
if(a.second != b.second)
return a.second < b.second;
else if(m_graphp->Abundance(a.first) != m_graphp->Abundance(b.first))
return m_graphp->Abundance(a.first) > m_graphp->Abundance(b.first);
else
return m_graphp->GetNodeSeq(a.first) < m_graphp->GetNodeSeq(b.first);
});
Node initial_node = first_kmerp->first;
string kmer_seq = m_graphp->GetNodeSeq(initial_node);
int first_matching_kmer = first_kmerp->second; // position on target
target_kmers.erase(first_kmerp);
auto inode = m_graphp->GetNodeKmer(initial_node);
if(!CheckSeed(initial_node, first_matching_kmer, target_len-first_matching_kmer-m_kmer_len))
continue;
int left_not_aligned = 0;
int right_not_aligned = 0;
int left_penalty = 0;
int right_penalty = 0;
{
int lminscore = numeric_limits<int>::max();
int lminpos = -1;
int score = 0;
for(int p = 0; p < m_kmer_len; ++p) {
if(kmer_seq[p] == target[first_matching_kmer+p])
score += m_match;
else
score -= m_mismatch;
if(score <= lminscore) {
lminscore = score;
lminpos = p;
}
}
int rminscore = numeric_limits<int>::max();
int rminpos = -1;
score = 0;
for(int p = 0; p < m_kmer_len; ++p) {
if(kmer_seq[m_kmer_len-1-p] == target[first_matching_kmer+m_kmer_len-1-p])
score += m_match;
else
score -= m_mismatch;
if(score <= rminscore) {
rminscore = score;
rminpos = p;
}
}
if(lminscore <= 0) {
left_not_aligned = lminpos+1;
left_penalty = -lminscore;
}
if(rminscore <= 0) {
right_not_aligned = rminpos+1;
right_penalty = -rminscore;
}
}
if(left_not_aligned+right_not_aligned > 0.5*m_kmer_len)
continue;
gugraph.StartNewAssembly(kmer_seq, left_not_aligned, right_not_aligned);
size_t assembled_len = 0;
if(first_matching_kmer > 0) { // needs left extension
string left_target = target.substr(0, first_matching_kmer);
ReverseComplementSeq(left_target.begin(), left_target.end());
CGuidedPathNA extender(m_graphp->ReverseComplement(initial_node), left_penalty, left_not_aligned, m_protect_ends, left_target, target_len-first_matching_kmer-m_kmer_len,
*m_graphdiggerp, *m_secondary_graphdiggerp, m_delta, m_gap_open, m_gap_extend, m_drop_off, anchor_frac, m_secondary_kmer_threshold);
map<typename CGuidedGraph::TAnchor, int> lanchors;
map<Node, int> lnotaligned;
int total = 0;
deque<int> forks(m_buf_length+1);
unordered_set<Node, Node::Hash> dead_ends;
while(extender.ProcessNextEdge()) {
if(extender.PathEnd() || (extender.NotAligned() >= m_kmer_len && dead_ends.count(extender.LastStepNode()))) {
if(extender.NotAligned() >= m_kmer_len) {
int nkmer = extender.NotAligned()-m_kmer_len+1;
for(int p = extender.AssembledSeqLength()-nkmer; p < extender.AssembledSeqLength(); ++p) {
auto& node = extender.AssembledSeq()[p].m_node;
if(node.isValid())
dead_ends.insert(node);
}
}
SPathChunk chunk = extender.GetLastSegment();
total += chunk.m_seq.size();
gugraph.AddLeftSegment(chunk, lanchors, lnotaligned, gugraph.End());
int check_len = extender.AssembledSeqLength();
check_len = min(check_len, check_len-extender.NotAligned()+m_kmer_len-1);
for(int p = extender.StartingShift(); p < check_len; ++p)
target_kmers.erase(m_graphp->ReverseComplement(extender.AssembledSeq()[p].m_node));
lanchors.clear();
lnotaligned.clear();
if(extender.PathEnd())
extender.DeleteNotAlignedForks(extender.AssembledSeqLength()-extender.NotAligned()+m_kmer_len);
else
extender.DeleteLastBranch();
gugraph.RewindLeftBranch(extender.StartingShift(), anchor_frac);
} else if(extender.LastStepNode().isValid()) {
typename CGuidedGraph::TAnchor anchor(extender.LastStepNode(), first_matching_kmer-1-extender.GetMaxPos()); // left end of alignment on target
CGuidedGraph::TSegmentP hook(gugraph.End());
int chunk_len = extender.AssembledSeqLength()-extender.StartingShift();
if(extender.SolidKmer()) {
hook = gugraph.KnownLeftAnchor(anchor);
lanchors[anchor] = chunk_len-1;
} else if(chunk_len > m_kmer_len) {
lnotaligned[extender.LastStepNode()] = chunk_len-1;
hook = gugraph.KnownLeftNotAligned(extender.LastStepNode(), chunk_len, extender.StartingShift(), extender.AssembledSeq());
if(hook != gugraph.End())
++collapsedL;
}
if(hook != gugraph.End()) {
SPathChunk chunk = extender.GetLastSegment();
total += chunk.m_seq.size();
gugraph.AddLeftSegment(chunk, lanchors, lnotaligned, hook);
for(int p = extender.StartingShift(); p < extender.AssembledSeqLength(); ++p)
target_kmers.erase(m_graphp->ReverseComplement(extender.AssembledSeq()[p].m_node));
lanchors.clear();
lnotaligned.clear();
extender.DeleteLastBranch();
gugraph.RewindLeftBranch(extender.StartingShift(), anchor_frac);
}
}
forks.pop_front();
forks.push_back(extender.ForkCount());
if(total > 10*target_len || (total > 0 && m_min_bases_per_fork*(forks.back()-forks.front()) > m_buf_length)) {
lock_guard<mutex> guard(m_out_mutex);
cerr << "Interrupted assemblyA " << acc << " kmer " << kmer_seq << " " << first_matching_kmer << endl;
break;
}
}
assembled_len += total;
}
gugraph.CleanBranch();
if(first_matching_kmer < target_len-m_kmer_len) {
string right_target = target.substr(first_matching_kmer+m_kmer_len);
CGuidedPathNA extender(initial_node, right_penalty, right_not_aligned, m_protect_ends, target.substr(first_matching_kmer+m_kmer_len), first_matching_kmer,
*m_graphdiggerp, *m_secondary_graphdiggerp, m_delta, m_gap_open, m_gap_extend, m_drop_off, anchor_frac, m_secondary_kmer_threshold);
map<typename CGuidedGraph::TAnchor, int> ranchors;
map<Node, int> rnotaligned;
int total = 0;
deque<int> forks(m_buf_length+1);
unordered_set<Node, Node::Hash> dead_ends;
while(extender.ProcessNextEdge()) {
if(extender.PathEnd() || (extender.NotAligned() >= m_kmer_len && dead_ends.count(extender.LastStepNode()))) {
if(extender.NotAligned() >= m_kmer_len) {
int nkmer = extender.NotAligned()-m_kmer_len+1;
for(int p = extender.AssembledSeqLength()-nkmer; p < extender.AssembledSeqLength(); ++p) {
auto& node = extender.AssembledSeq()[p].m_node;
if(node.isValid())
dead_ends.insert(node);
}
}
SPathChunk chunk = extender.GetLastSegment();
total += chunk.m_seq.size();
gugraph.AddRightSegment(chunk, ranchors, rnotaligned, gugraph.End());
int check_len = extender.AssembledSeqLength();
check_len = min(check_len, check_len-extender.NotAligned()+m_kmer_len-1);
for(int p = extender.StartingShift(); p < check_len; ++p)
target_kmers.erase(extender.AssembledSeq()[p].m_node);
ranchors.clear();
rnotaligned.clear();
if(extender.PathEnd())
extender.DeleteNotAlignedForks(extender.AssembledSeqLength()-extender.NotAligned()+m_kmer_len);
else
extender.DeleteLastBranch();
gugraph.RewindRightBranch(extender.StartingShift(), anchor_frac);
} else if(extender.LastStepNode().isValid()) {
CGuidedGraph::TSegmentP hook(gugraph.End());
int chunk_len = extender.AssembledSeqLength()-extender.StartingShift();
if(extender.SolidKmer()) {
typename CGuidedGraph::TAnchor anchor(extender.LastStepNode(), first_matching_kmer+m_kmer_len+extender.GetMaxPos()); // right end of alignment on target
hook = gugraph.KnownRightAnchor(anchor);
ranchors[anchor] = chunk_len-1;
} else if(chunk_len > m_kmer_len) {
rnotaligned[extender.LastStepNode()] = chunk_len-1;
hook = gugraph.KnownRightNotAligned(extender.LastStepNode(), chunk_len, extender.StartingShift(), extender.AssembledSeq());
if(hook != gugraph.End())
++collapsedR;
}
if(hook != gugraph.End()) {
SPathChunk chunk = extender.GetLastSegment();
total += chunk.m_seq.size();
gugraph.AddRightSegment(chunk, ranchors, rnotaligned, hook);
for(int p = extender.StartingShift(); p < extender.AssembledSeqLength(); ++p)
target_kmers.erase(extender.AssembledSeq()[p].m_node);
ranchors.clear();
rnotaligned.clear();
extender.DeleteLastBranch();
gugraph.RewindRightBranch(extender.StartingShift(), anchor_frac);
}
}
forks.pop_front();
forks.push_back(extender.ForkCount());
if(total > 10*target_len || (total > 0 && m_min_bases_per_fork*(forks.back()-forks.front()) > m_buf_length)) {
lock_guard<mutex> guard(m_out_mutex);
cerr << "Interrupted assemblyB " << acc << " kmer " << kmer_seq << " " << first_matching_kmer << endl;
break;
}
}
assembled_len += total;
}
gugraph.RemoveNotAlignedSegments(anchor_frac);
}
GFAGraph gfa_graph(acc, m_kmer_len);
gugraph.GetGFAGraph(gfa_graph);
gfa_graph.CalculateChainLength();
// gfa_graph.RemoveShortChains(min_len);
gfa_graph.AssignGroupNumber();
gfa_graph.TrimGroups(m_graph_uniformity, min_len);
gfa_graph.MergeRedundantLinks();
gfa_graph.AssignGroupNumber();
size_t gsize = gfa_graph.Size();
TGFACollection splitted = gfa_graph.SplitGroups();
for(auto& graph : splitted) {
if(m_min_count == 1) {
if(graph.RemoveHair(*m_secondary_graphdiggerp, m_fraction))
graph.MergeForks();
graph.RemoveSinglReadSnps(*m_secondary_graphp);
}
if(m_remove_homopolymer_indels)
graph.RemoveHomopolymerIndels(*m_secondary_graphp, m_homopolymer_ratio, m_homopolymer_len);
if(m_no_reads && m_no_pairs) {
if(graph.CollapsFreeEnds() > 0)
graph.MergeRedundantLinks();
if(m_extend_ends) {
graph.GenerateKmers(*m_graphp);
graph.ExtendToFirstFork(*m_graphdiggerp);
}
graph.GenerateKmersAndScores(*m_secondary_graphp);
} else {
graph.GenerateKmersAndScores(*m_graphp);
}
graph.ScoreGraph(get<2>(item.second), m_word_size);
}
rslts.splice(rslts.end(), splitted);
{
lock_guard<mutex> guard(m_out_mutex);
cerr << "Finished assembling: " << acc << " " << collapsedL << " " << collapsedR << " " << gsize << " in " << timer.Elapsed();
}
}
}
int m_seed_prec;
int m_match;
int m_mismatch;
SMatrix m_delta;
};
class CGuidedAssemblerAA : public CGuidedAssembler {
public:
CGuidedAssemblerAA(int kmer_len, int secondary_kmer_len, bool extend_ends, bool protect_ends, bool keep_subgraphs, int min_count, double fraction, int word_size, int gap_open, int gap_extend,
int drop_off, int kmer_complexity, double max_fork_density, int buf_length, int ncores, list<array<CReadHolder,2>>& raw_reads, ifstream& targets_in,
int estimated_kmer_num, bool skip_bloom_filter, int not_aligned_len, int not_aligned_count, int aligned_count, int maxp, double target_coverage, int min_hit_len,
bool no_reads, bool no_pairs, int genetic_code, bool translate_na, int fs_open, bool allow_frameshifts,
int secondary_kmer_threshold, bool remove_homopolymer_indels, int homopolymer_len, double homopolymer_ratio) :
CGuidedAssembler(kmer_len, secondary_kmer_len, extend_ends, protect_ends, keep_subgraphs, min_count, fraction, word_size, gap_open, gap_extend,
drop_off, kmer_complexity, max_fork_density, buf_length, ncores, raw_reads,
estimated_kmer_num, skip_bloom_filter, not_aligned_len, not_aligned_count, aligned_count, maxp, target_coverage, min_hit_len,
no_reads, no_pairs, secondary_kmer_threshold, remove_homopolymer_indels, homopolymer_len, homopolymer_ratio), m_genetic_code(genetic_code), m_translate_na(translate_na),
m_fs_open(fs_open), m_allow_frameshifts(allow_frameshifts) {
Assemble(targets_in, !m_allow_frameshifts);
}
void TranslateToAA() {
for(auto& gfa_graph : m_gfa_collection)
gfa_graph.TranslateToAA(m_genetic_code, *m_graphp);
}
private:
void ReadTargets(ifstream& targets_in) {
string accepted_symbols = m_translate_na ? "ACGTYRWSKMDVHBXN" : "UARNDCQEGHILKMFPSTWYVBJZX*";
char c;
if(!(targets_in >> c) || c != '>')
throw runtime_error("Invalid fasta file format for targets");
string record;
while(getline(targets_in, record, '>')) {
while(!targets_in.eof() && record.back() != '\n') {
string part;
getline(targets_in, part, '>');
record += '>'+part;
}
size_t first_ret = min(record.size(),record.find('\n'));
if(first_ret == string::npos)
throw runtime_error("Invalid fasta file format for targets");
string acc = record.substr(0, first_ret);