-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpapara.cpp
1737 lines (1206 loc) · 56.4 KB
/
papara.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
/*
* Copyright (C) 2009-2012 Simon A. Berger
*
* This file is part of papara.
*
* papara is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* papara is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with papara. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <iomanip>
#include <boost/bind.hpp>
#include <boost/dynamic_bitset.hpp>
#include <iterator>
#include "ivymike/fasta.h"
#include "ivymike/demangle.h"
#include "ivymike/time.h"
#include "papara.h"
#include "vec_unit.h"
#include "align_pvec_vec.h"
#include "stepwise_align.h"
#include "align_utils.h"
using namespace ivy_mike;
using namespace ivy_mike::tree_parser_ms;
using namespace papara;
bool papara::g_dump_aux = false;
class log_stream_buffer : public std::streambuf
{
public:
log_stream_buffer() : buffer_(120) {
}
void post( char overflow, char *start, char *end ) {
for( std::vector< std::ostream* >::iterator it = log_tees.begin(); it != log_tees.end(); ++it ) {
std::copy( start, end, std::ostream_iterator<char>( *(*it) ));
if( overflow != 0 ) {
(*it)->put(overflow);
}
}
for( std::vector< log_sink* >::iterator it = log_sinks.begin(); it != log_sinks.end(); ++it ) {
(*it)->post( overflow, start, end );
}
}
int_type overflow(int c) {
post( c, pbase(), epptr() );
setp(&buffer_.front(), (&buffer_.back()) + 1);
return 1;
}
int sync() {
// std::cout << "sync:";
// std::copy( pbase(), pptr(), std::ostream_iterator<char>(std::cout));
// std::cout << std::endl;
post( 0, pbase(), pptr() );
setp(&buffer_.front(), (&buffer_.back()) + 1);
return 0;
}
void add_tee( std::ostream *os ) {
log_tees.push_back(os);
}
void remove_tee( std::ostream *os ) {
std::vector<std::ostream *>::iterator it = std::find( log_tees.begin(), log_tees.end(), os );
if( it != log_tees.end() ) {
log_tees.erase(it);
}
}
void add_sink( log_sink *s ) {
log_sinks.push_back(s);
}
void remove_sink( log_sink *s ) {
std::vector<log_sink *>::iterator it = std::find( log_sinks.begin(), log_sinks.end(), s );
if( it != log_sinks.end() ) {
log_sinks.erase(it);
}
}
private:
// copy ctor and assignment not implemented;
// copying not allowed
log_stream_buffer(const log_stream_buffer &);
log_stream_buffer &operator= (const log_stream_buffer &);
std::vector<char> buffer_;
std::vector<std::ostream *> log_tees;
std::vector<log_sink *> log_sinks;
};
// // lifted from http://www.horstmann.com/cpp/iostreams.html
// class log_stream_buffer : public std::filebuf
// {
// std::vector<std::ostream *> log_tees;
//
// public:
// log_stream_buffer() {
// //filebuf::open("NUL", ios::out);
// }
//
// void open(const char *fname);
// // void close() {
// // // _win.close(); filebuf::close();
// //
// // }
// virtual int sync();
//
// void add_tee( std::ostream *os ) {
// log_tees.push_back(os);
// }
// void remove_tee( std::ostream *os ) {
// std::vector<std::ostream *>::iterator it = std::find( log_tees.begin(), log_tees.end(), os );
// if( it != log_tees.end() ) {
// log_tees.erase(it);
// }
// }
// private:
//
// };
// void log_stream_buffer::open(const char *fname)
// {
// std::filebuf::close();
// if( fname != 0 ) {
// std::cerr << "log file: " << fname << "\n";
// std::filebuf::open(fname, std::ios::out | std::ios::app | std::ios::trunc );
// assert(std::filebuf::is_open());
// }
//
// }
// int log_stream_buffer::sync()
// {
// // pbase();
// // pptr()
// //
// // int count = std::filebuf::out_waiting();
// // _win.append(pbase(), count);
// std::cerr << "sync: " << std::distance(pbase(), pptr()) << "\n";
// for( std::vector< std::ostream* >::iterator it = log_tees.begin(); it != log_tees.end(); ++it ) {
// std::copy( pbase(), pptr(), std::ostream_iterator<char>( *(*it) ));
// }
// return std::filebuf::sync();
// }
static log_stream_buffer ls_buf;
std::ostream papara::lout(&ls_buf);
static ivy_mike::mutex log_buffer_mutex;
// open_log_file::open_log_file( const char *filename ) {
//
// ivy_mike::lock_guard<ivy_mike::mutex> lock( log_buffer_mutex );
// if( log_is_open ) {
// std::cerr << "papara logfile is already open." << std::endl;
// abort();
// }
//
//
// ls_buf.open(filename);
// log_is_open = true;
// }
// open_log_file::~open_log_file() {
// ivy_mike::lock_guard<ivy_mike::mutex> lock( log_buffer_mutex );
// if( !log_is_open ) {
// std::cerr << "papara logfile is already closed. (This should be impossible) " << std::endl;
// abort();
// }
// ls_buf.close();
// log_is_open = false;
// }
add_log_tee::add_log_tee( std::ostream &os ) : os_(os) {
ivy_mike::lock_guard<ivy_mike::mutex> lock( log_buffer_mutex );
ls_buf.add_tee(&os);
}
add_log_tee::~add_log_tee() {
ivy_mike::lock_guard<ivy_mike::mutex> lock( log_buffer_mutex );
ls_buf.remove_tee(&os_);
}
add_log_sink::add_log_sink( log_sink *s ) : s_(s) {
ivy_mike::lock_guard<ivy_mike::mutex> lock( log_buffer_mutex );
ls_buf.add_sink(s);
}
add_log_sink::~add_log_sink() {
ivy_mike::lock_guard<ivy_mike::mutex> lock( log_buffer_mutex );
ls_buf.remove_sink(s_);
}
std::string papara::get_version_string() {
return std::string( "2.5" );
}
template<typename seq_tag>
queries<seq_tag>::queries( const std::string &opt_qs_name ) {
// if( !opt_qs_name.empty() ) {
//
// read query sequences
//
if( !opt_qs_name.empty() ) {
std::ifstream qsf( opt_qs_name.c_str() );
if( !qsf.good() ) {
throw std::runtime_error( "cannot open qs file");
}
// mix them with the qs from the ref alignment <-- WTF? must have been sniffing whiteboard cleaner... the qs are read before the ref seqs...
read_fasta( qsf, m_qs_names, m_qs_seqs);
}
// if( m_qs_names.empty() ) {
// throw std::runtime_error( "no qs" );
// }
std::for_each( m_qs_names.begin(), m_qs_names.end(), std::ptr_fun( normalize_name ));
//
// setup qs best-score/best-edge lists
//
m_qs_pvecs.resize( m_qs_names.size() );
// }
// m_qs_bestscore.resize(m_qs_names.size());
// std::fill( m_qs_bestscore.begin(), m_qs_bestscore.end(), 32000);
// m_qs_bestedge.resize(m_qs_names.size());
}
template<typename seq_tag>
void queries<seq_tag>::preprocess() {
//
// preprocess query sequences
//
if( m_qs_seqs.empty() ) {
throw std::runtime_error( "no query sequences" );
}
assert( m_qs_seqs.size() == m_qs_names.size() );
m_qs_pvecs.resize(m_qs_seqs.size());
m_qs_cseqs.resize(m_qs_seqs.size());
std::vector<bool> bad_characters( 256, false );
for( size_t i = 0; i < m_qs_seqs.size(); i++ ) {
// seq_to_nongappy_pvec( m_qs_seqs[i], m_qs_pvecs[i] );
// static void seq_to_nongappy_pvec( std::vector<uint8_t> &seq, std::vector<uint8_t> &pvec ) {
// the following line means: transform sequence to pvec using seq_model::s2p as mapping
// function and only append the mapped character into pvec, if it corresponds to a single (=non gap)
// character.
std::vector<uint8_t> qs_tmp;
qs_tmp.reserve( m_qs_seqs[i].size() );
bool bad_char = false;
for( std::vector<uint8_t>::iterator it = m_qs_seqs[i].begin(), e = m_qs_seqs[i].end(); it != e; ++it ) {
if( !seq_model::is_known_sstate( *it ) ) {
bad_characters.at( *it ) = true;
bad_char = true;
} else {
qs_tmp.push_back( *it );
}
}
if( bad_char ) {
m_qs_seqs[i].swap( qs_tmp );
}
std::transform( m_qs_seqs[i].begin(), m_qs_seqs[i].end(),
back_insert_ifer(m_qs_cseqs[i], seq_model::cstate_is_single),
seq_model::s2c );
std::transform( m_qs_seqs[i].begin(), m_qs_seqs[i].end(),
back_insert_ifer(m_qs_pvecs[i], seq_model::pstate_is_single),
seq_model::s2p );
// std::cout << "preprocess: " << i << " " << m_qs_cseqs[i].size() << " " << m_qs_pvecs[i].size() << "\n";
if( m_qs_cseqs[i].size() != m_qs_pvecs[i].size() ) {
// check for quirks related to the p-state vs c-state representation
throw std::runtime_error( "mismatch between lengths of c-state and p-state representations of query sequence." );
}
// for( unsigned int i = 0; i < seq.size(); i++ ) {
// seq_model::pars_state_t ps = seq_model::s2p(seq[i]);
//
// if( seq_model::is_single(ps)) {
// pvec.push_back(ps);
// }
//
// }
}
// print warnings about deleted characters
bool warn_header = false;
for( std::vector<bool>::iterator it = bad_characters.begin(), e = bad_characters.end(); it != e; ++it ) {
if( *it ) {
if( !warn_header ) {
std::cout << "WARNING: there were unsupported characters in the query sequences. They will be deleted:\n";
warn_header = true;
}
std::cout << "deleted character: '" << uint8_t(std::distance( bad_characters.begin(), it )) << "'\n";
}
}
// if( write_testbench ) {
//
// write_qs_pvecs( "qs.bin" );
// write_ref_pvecs( "ref.bin" );
// }
}
// template<typename pvec_t, typename seq_tag>
// void queries<seq_tag>::init_partition_assignments( partassign::part_assignment &part_assign, references<pvec_t,seq_tag> &refs ) {
//
// }
template<typename seq_tag>
void queries<seq_tag>::add( const std::string& name, std::vector< uint8_t >& qs ) {
m_qs_names.push_back(name);
ivy_mike::push_back_swap(m_qs_seqs, qs );
}
template<typename seq_tag>
void queries<seq_tag>::write_pvecs(const char* name) {
std::ofstream os( name );
os << m_qs_pvecs.size();
for( typename std::vector< std::vector< pars_state_t > >::iterator it = m_qs_pvecs.begin(); it != m_qs_pvecs.end(); ++it ) {
os << " " << it->size() << " ";
os.write( (char *)it->data(), it->size() );
}
}
template<typename seq_tag>
size_t queries<seq_tag>::max_name_length() const {
size_t len = 0;
for( std::vector <std::string >::const_iterator it = m_qs_names.begin(); it != m_qs_names.end(); ++it ) {
len = std::max( len, it->size() );
}
return len;
}
template<typename seq_tag>
size_t queries<seq_tag>::calc_cups_per_ref(size_t ref_len) const {
size_t ct = 0;
typename std::vector<std::vector <pars_state_t> >::const_iterator first = m_qs_pvecs.begin();
const typename std::vector<std::vector <pars_state_t> >::const_iterator last = m_qs_pvecs.end();
for(; first != last; ++first ) {
//ct += (ref_len - first->size()) * first->size();
ct += ref_len * first->size(); // papara now uses the 'unbanded' aligner
}
return ct;
}
template<typename seq_tag>
void queries<seq_tag>::normalize_name(std::string& str) {
std::string ns;
// replace runs of one or more whitespaces with an underscores
bool in_ws_run = false;
for( std::string::iterator it = str.begin(); it != str.end(); ++it ) {
if( std::isspace(*it) ) {
if( !in_ws_run ) {
if( (*it) != '\r' ) { // just ignore stupid windows line ends
ns.push_back( '_' );
}
in_ws_run = true;
}
} else {
ns.push_back(*it);
in_ws_run = false;
}
}
str.swap(ns);
}
//////////////////////////////////////////////////////////////
// references stuff
//////////////////////////////////////////////////////////////
template<typename pvec_t, typename seq_tag>
references<pvec_t,seq_tag>::references(const char* opt_tree_name, const char* opt_alignment_name, queries<seq_tag>* qs) : m_ln_pool(new ln_pool( std::unique_ptr<node_data_factory>(new my_fact<my_adata>) )), spg_(pvec_pgap::pgap_model, &pm_)
{
//std::cerr << "papara_nt instantiated as: " << typeid(*this).name() << "\n";
lout << "references container instantiated as: " << ivy_mike::demangle(typeid(*this).name()) << "\n";
// std::cerr << ivy_mike::isa<papara_nt<pvec_cgap> >(*this) << " " << ivy_mike::isa<papara_nt<pvec_pgap> >(*this) << "\n";
// load input data: ref-tree, ref-alignment and query sequences
//
// parse the reference tree
//
ln_pool &pool = *m_ln_pool;
tree_parser_ms::parser tp( opt_tree_name, pool );
tree_parser_ms::lnode * n = tp.parse();
n = towards_tree( n );
tree_ = std::shared_ptr<im_tree_parser::lnode>(n->get_smart_ptr());
//
// create map from tip names to tip nodes
//
typedef tip_collector<lnode> tc_t;
tc_t tc;
visit_lnode( n, tc );
//boost::dynamic_bitset<> found_tree_taxa( tc.m_nodes.size(), true );
std::map<std::string, std::shared_ptr<lnode> > name_to_lnode;
for( std::vector< std::shared_ptr<lnode> >::iterator it = tc.m_nodes.begin(); it != tc.m_nodes.end(); ++it ) {
// std::cout << (*it)->m_data->tipName << "\n";
name_to_lnode[(*it)->m_data->tipName] = *it;
}
{
//
// read reference alignment: store the ref-seqs in the tips of the ref-tree
//
multiple_alignment ref_ma;
ref_ma.load_phylip( opt_alignment_name );
std::vector<my_adata *> tmp_adata;
boost::dynamic_bitset<> unmasked;
for( unsigned int i = 0; i < ref_ma.names.size(); i++ ) {
std::map< std::string, std::shared_ptr<lnode> >::iterator it = name_to_lnode.find(ref_ma.names[i]);
// process sequences from the ref_ma depending on, if they are contained in the tree.
// if they are, they are 'swapped' into m_ref_seqs
// if they are not, into m_qs_seqs. (gaps in the QS are removed later)
//
// additionally, all columns that contain only gaps are removed from the reference sequences.
if( it != name_to_lnode.end() ) {
std::shared_ptr< lnode > ln = it->second;
// adata *ad = ln->m_data.get();
assert( ivy_mike::isa<my_adata>(ln->m_data.get()) ); //typeid(*ln->m_data.get()) == typeid(my_adata ) );
my_adata *adata = static_cast<my_adata *> (ln->m_data.get());
// store the adata ptr corresponding to the current ref sequence for later use.
// (their indices in m_ref_seqs and tmp_adata correspond.)
assert( tmp_adata.size() == m_ref_seqs.size() );
tmp_adata.push_back(adata);
m_ref_names.push_back(std::string() );
m_ref_seqs.push_back(std::vector<uint8_t>() );
m_ref_names.back().swap( ref_ma.names[i] );
m_ref_seqs.back().swap( ref_ma.data[i] );
// mark all non-gap positions of the current reference in bit-vector 'unmasked'
const std::vector<uint8_t> &seq = m_ref_seqs.back();
if( unmasked.empty() ) {
unmasked.resize( seq.size() );
}
assert( unmasked.size() == seq.size() );
for( size_t j = 0, e = seq.size(); j != e; ++j ) {
try {
unmasked[j] |= !seq_model::pstate_is_gap( seq_model::s2p(seq[j]));
} catch( sequence_model::illegal_character x ) {
std::cerr << "illegal character in file '" << opt_alignment_name << "'" << std::endl;
std::cerr << "row: " << i + 1 << " (name: " << m_ref_names.back() << ")" << std::endl;
std::cerr << "col: " << j + 1 << " (char: '" << seq[j] << "')" << std::endl;
abort();
}
}
// erase it from the name to lnode* map, so that it can be used to ideantify tree-taxa without corresponding entries in the alignment
name_to_lnode.erase(it);
} else {
qs->add(ref_ma.names[i], ref_ma.data[i]); // REMARK: the second parameter is 'moved-from' (should be an rvalue-ref)
}
}
if( !name_to_lnode.empty() ) {
std::cerr << "error: there are " << name_to_lnode.size() << " taxa in the tree with no corresponding sequence in the reference alignment. names:\n";
for( std::map< std::string, std::shared_ptr< lnode > >::iterator it = name_to_lnode.begin(); it != name_to_lnode.end(); ++it ) {
std::cout << it->first << "\n";
}
throw std::runtime_error( "bailing out due to inconsitent input data\n" );
}
{
// remove all 'pure-gap' columns from the ref sequences
assert( tmp_adata.size() == m_ref_seqs.size() );
// retrieve a list of non-gap indices from the bit-vector
std::vector<size_t> unmasked_idx;
{
size_t i = unmasked.find_first();
while( i != unmasked.npos ) {
// std::cout << "um: " << i << "\n";
unmasked_idx.push_back(i);
i = unmasked.find_next(i);
}
}
for( size_t i = 0, e = m_ref_seqs.size(); i != e; ++i ) {
std::vector<uint8_t> seq_tmp;
seq_tmp.reserve(unmasked_idx.size());
const std::vector<uint8_t> &seq_orig = m_ref_seqs[i];
// copy all unmasked ref characters to seq_tmp
for( std::vector<size_t>::iterator it = unmasked_idx.begin(); it != unmasked_idx.end(); ++it ) {
assert( *it < seq_orig.size() );
seq_tmp.push_back( seq_orig[*it] );
}
m_ref_seqs[i].swap( seq_tmp );
//initialize the corresponding adata object with the cleaned ref seq.
tmp_adata.at(i)->init_pvec( m_ref_seqs[i] );
}
}
}
pm_.reset( m_ref_seqs );
std::cout << "p: " << pm_.setup_pmatrix(0.1) << "\n";
// initialize empty non-gap map. It is lazily filled as needed when necessary
ref_ng_map_.resize( m_ref_seqs.size() );
//
// collect list of edges
//
visit_edges( n, m_ec );
lout << "edges: " << m_ec.m_edges.size() << "\n";
}
template<typename pvec_t, typename seq_tag>
void references<pvec_t,seq_tag>::build_ref_vecs() {
// pre-create the ancestral state vectors. This step is necessary for the threaded version, because otherwise, each
// thread would need an independent copy of the tree to do concurrent newviews. Anyway, having a copy of the tree
// in each thread will most likely use more memory than storing the pre-calculated vectors.
// TODO: maybe try lazy create/cache of the asv's in the threads
ivy_mike::timer t1;
assert( m_ref_aux.empty() && m_ref_pvecs.empty() );
m_ref_pvecs.reserve( m_ec.m_edges.size() );
m_ref_aux.reserve( m_ec.m_edges.size() );
for( size_t i = 0; i < m_ec.m_edges.size(); i++ ) {
pvec_t root_pvec;
// std::cout << "newview for branch " << i << ": " << *(m_ec.m_edges[i].first->m_data) << " " << *(m_ec.m_edges[i].second->m_data) << "\n";
if( i == 340 ) {
g_dump_aux = true;
}
driver<pvec_t,seq_tag>::do_newview( root_pvec, m_ec.m_edges[i].first, m_ec.m_edges[i].second, true );
g_dump_aux = false;
// TODO: try something fancy with rvalue refs...
m_ref_pvecs.push_back( std::vector<int>() );
m_ref_aux.push_back( std::vector<unsigned int>() );
root_pvec.to_int_vec(m_ref_pvecs.back());
root_pvec.to_aux_vec(m_ref_aux.back());
m_ref_gapp.push_back( std::vector<double>() );
if( ivy_mike::same_type<pvec_t,pvec_pgap>::result ) {
// WTF: this is why mixing static and dynamic polymorphism is a BAD idea!
pvec_pgap *rvp = reinterpret_cast<pvec_pgap *>(&root_pvec);
rvp->to_gap_post_vec(m_ref_gapp.back());
// std::transform( m_ref_gapp.back().begin(), m_ref_gapp.back().end(), std::ostream_iterator<int>(std::cout), ivy_mike::scaler_clamp<double>(10,0,9) );
//
// std::cout << "\n";
}
}
// std::cout << "pvecs created: " << t1.elapsed() << "\n";
}
template<typename pvec_t, typename seq_tag>
const std::vector<int> &references<pvec_t,seq_tag>::ng_map_at( size_t i ) {
std::vector<int> &ng_map = ref_ng_map_.at(i);
if( !ng_map.empty() ) {
return ng_map;
}
//std::vector<int> map;
std::vector< uint8_t > &seq = m_ref_seqs.at(i);
assert( seq.size() < size_t(std::numeric_limits<int>::max()) );
for( size_t i = 0; i < seq.size(); ++i ) {
bool is_gap = seq_model::pstate_is_gap( seq_model::s2p(seq[i]));
if( !is_gap ) {
ng_map.push_back(int(i));
}
}
//ng_map.shrink_to_fit();
std::vector<int>(ng_map).swap(ng_map); // old fashioned shrink_to_fit
return ng_map;
}
template<typename pvec_t, typename seq_tag>
void references<pvec_t,seq_tag>::write_pvecs(const char* name) {
std::ofstream os( name );
os << m_ref_pvecs.size();
for( size_t i = 0; i < m_ref_pvecs.size(); ++i ) {
os << " " << m_ref_pvecs[i].size() << " ";
os.write( (char *)m_ref_pvecs[i].data(), m_ref_pvecs[i].size() * sizeof(int));
os.write( (char *)m_ref_aux[i].data(), m_ref_aux[i].size() * sizeof(unsigned int));
}
}
template<typename pvec_t, typename seq_tag>
size_t references<pvec_t,seq_tag>::max_name_length() const {
size_t len = 0;
for( std::vector <std::string >::const_iterator it = m_ref_names.begin(); it != m_ref_names.end(); ++it ) {
len = std::max( len, it->size() );
}
return len;
}
bool scoring_results::offer(size_t qs, size_t ref, int score) {
ivy_mike::lock_guard<ivy_mike::mutex> lock(mtx_);
candss_.at( qs ).offer( score, ref );
if( best_score_.at(qs) < score || (best_score_.at(qs) == score && ref < best_ref_.at(qs))) {
best_score_[qs] = score;
best_ref_.at(qs) = ref;
return true;
}
return false;
}
namespace papara {
void scoring_results::candidates::offer(int score, size_t ref) {
candidate c( score,ref);
insert(std::lower_bound( begin(), end(), c), c );
if( size() > max_num_) {
pop_back();
}
}
bool scoring_results::candidate::operator<(const scoring_results::candidate& other) const {
if( score_ > other.score_ ) {
return true;
} else {
if( score_ == other.score_ ) {
return ref_ < other.ref_;
} else {
return false;
}
}
}
////////////////////////////////////////////////////////
// driver stuff
////////////////////////////////////////////////////////
template<typename seq_tag>
class worker {
const static size_t VW = vu_config<seq_tag>::width;
typedef typename vu_config<seq_tag>::scalar vu_scalar_t;
typedef typename block_queue<seq_tag>::block_t block_t;
typedef model<seq_tag> seq_model;
block_queue<seq_tag> &block_queue_;
scoring_results &results_;
const queries<seq_tag> &qs_;
const size_t rank_;
const papara_score_parameters sp_;
static void copy_to_profile( const block_t &block, aligned_buffer<vu_scalar_t> *prof, aligned_buffer<vu_scalar_t> *aux_prof ) {
size_t reflen = block.ref_len;
assert( reflen * VW == prof->size() );
assert( reflen * VW == aux_prof->size() );
typename aligned_buffer<vu_scalar_t>::iterator it = prof->begin();
typename aligned_buffer<vu_scalar_t>::iterator ait = aux_prof->begin();
// std::cout << "reflen: " << reflen << " " << size_t(&(*it)) << "\n";
for( size_t i = 0; i < reflen; ++i ) {
for( size_t j = 0; j < VW; ++j ) {
// std::cout << "ij: " << i << " " << j << " " << pvecs[j].size() << "\n";
*it = vu_scalar_t(block.seqptrs[j][i]);
*ait = (block.auxptrs[j][i] == AUX_CGAP) ? vu_scalar_t(-1) : 0;
++it;
++ait;
}
}
assert( it == prof->end());
assert( ait == aux_prof->end());
}
public:
worker( block_queue<seq_tag> *bq, scoring_results *res, const queries<seq_tag> &qs, size_t rank, const papara_score_parameters &sp )
: block_queue_(*bq), results_(*res), qs_(qs), rank_(rank), sp_(sp) {}
void operator()() {
ivy_mike::timer tstatus;
ivy_mike::timer tprint;
uint64_t cups_per_ref = -1;
uint64_t ncup = 0;
uint64_t inner_iters = 0;
uint64_t ticks_all = 0;
uint64_t ncup_short = 0;
uint64_t inner_iters_short = 0;
uint64_t ticks_all_short = 0;
aligned_buffer<vu_scalar_t> pvec_prof;
aligned_buffer<vu_scalar_t> aux_prof;
align_vec_arrays<vu_scalar_t> arrays;
aligned_buffer<vu_scalar_t> out_scores(VW);
aligned_buffer<vu_scalar_t> out_scores2(VW);
size_t queue_size;
size_t init_queue_size = -1;
while( true ) {
block_t block;
if( !block_queue_.get_block(&block, &queue_size)) {
break;
}
if( init_queue_size == size_t(-1) ) {
init_queue_size = queue_size;
}
if( cups_per_ref == uint64_t(-1) ) {
cups_per_ref = qs_.calc_cups_per_ref(block.ref_len );
}
#if 1
// assert( VW == 8 );
pvec_prof.resize( VW * block.ref_len );
aux_prof.resize( VW * block.ref_len );
copy_to_profile(block, &pvec_prof, &aux_prof );
pvec_aligner_vec<vu_scalar_t,VW> pav( block.seqptrs, block.auxptrs, block.ref_len, sp_.match, sp_.match_cgap, sp_.gap_open, sp_.gap_extend, seq_model::c2p, seq_model::num_cstates() );
// const align_pvec_score<vu_scalar_t,VW> aligner( block.seqptrs, block.auxptrs, block.ref_len, score_mismatch, score_match_cgap, score_gap_open, score_gap_extend );
for( unsigned int i = 0; i < qs_.size(); i++ ) {
//align_pvec_score_vec<vu_scalar_t, VW, false, typename seq_model::pars_state_t>( pvec_prof, aux_prof, qs_.pvec_at(i), score_match, score_match_cgap, score_gap_open, score_gap_extend, out_scores, arrays );
std::pair<size_t,size_t> bounds = qs_.get_per_qs_bounds( i );
// std::cout << "bounds: " << bounds.first << " " << bounds.second << "\n";
// if no bounds are available, get_per_qs_bounds will return [size_t(-1),size_t(-1)], which align is supposed to interpret as 'full range'
pav.align( qs_.cseq_at(i).begin(), qs_.cseq_at(i).end(), sp_.match, sp_.match_cgap, sp_.gap_open, sp_.gap_extend, out_scores.begin(), bounds.first, bounds.second );
//aligner.align(qs_.pvec_at(i).begin(), qs_.pvec_at(i).end());
//const vu_scalar_t *score_vec = aligner.get_scores();
//ncup += block.num_valid * block.ref_len * qs_.pvec_at(i).size();
#if 0 // test against old version
align_pvec_score_vec<vu_scalar_t, VW>( pvec_prof.begin(), pvec_prof.end(), aux_prof.begin(), qs_.pvec_at(i).begin(), qs_.pvec_at(i).end(), score_match, score_match_cgap, score_gap_open, score_gap_extend, out_scores2.begin(), arrays );
bool eq = std::equal( out_scores.begin(), out_scores.end(), out_scores2.begin() );
if( !eq ) {
std::cout << "meeeeeeep!\n";
}
//std::cout << "eq: " << eq << "\n";
#endif
// std::cout << "scores: ";
// std::copy( out_scores.begin(), out_scores.end(), std::ostream_iterator<int>(std::cout, "\n" ) );
// std::cout << "\n";
results_.offer( i, block.edges, block.edges + block.num_valid, out_scores.begin() );
}
ncup += block.num_valid * cups_per_ref;
ncup_short += block.num_valid * cups_per_ref;
ticks_all += pav.ticks_all();
ticks_all_short += pav.ticks_all();
inner_iters += pav.inner_iters_all();
inner_iters_short += pav.inner_iters_all();
if( rank_ == 0 && tprint.elapsed() > 10 ) {
//std::cout << "thread " << rank_ << " " << ncup << " in " << tstatus.elapsed() << " : "
float fdone = (init_queue_size - queue_size) / float(init_queue_size);
lout << fdone * 100 << "% done. ";
lout << ncup / (tstatus.elapsed() * 1e9) << " gncup/s, " << ticks_all / double(inner_iters) << " tpili (short: " << ncup_short / (tprint.elapsed() * 1e9) << ", " << ticks_all_short / double(inner_iters_short) << ")" << std::endl;
ncup_short = 0;
ticks_all_short = 0;
inner_iters_short = 0;
tprint = ivy_mike::timer();
}
}
#else
// assert( block.gapp_ptrs[0] != 0 );
// assert( VW == 4 );
// const align_pvec_gapp_score<4> aligner( block.seqptrs, block.gapp_ptrs, block.ref_len, score_mismatch, score_match_cgap, score_gap_open, score_gap_extend );
// for( unsigned int i = 0; i < m_pnt.m_qs_names.size(); i++ ) {
//
// size_t stride = 1;
// size_t aux_stride = 1;
//
// aligner.align(m_pnt.m_qs_pvecs[i]);
// const float *score_vec = aligner.get_scores();
//
// ncup += block.num_valid * block.ref_len * m_pnt.m_qs_pvecs[i].size();
// {
// ivy_mike::lock_guard<ivy_mike::mutex> lock( m_pnt.m_qmtx );
//
// for( int k = 0; k < block.num_valid; k++ ) {
//
//
//
// if( score_vec[k] < m_pnt.m_qs_bestscore[i] || (score_vec[k] == m_pnt.m_qs_bestscore[i] && block.edges[k] < m_pnt.m_qs_bestedge[i] )) {
// const bool validate = false;
// if( validate ) {
// const int *seqptr = block.seqptrs[k];
// const double *gapp_ptr = block.gapp_ptrs[k];
//
//// std::vector<double> gapp_tmp(gapp_ptr, gapp_ptr + block.ref_len);
//
//
// pars_align_gapp_seq pas( seqptr, m_pnt.m_qs_pvecs[i].data(), block.ref_len, m_pnt.m_qs_pvecs[i].size(), stride, gapp_ptr, aux_stride, seq_arrays_gapp, 0, score_gap_open, score_gap_extend, score_mismatch, score_match_cgap );
// int res = pas.alignFreeshift(INT_MAX);
//
// if( res != score_vec[k] ) {
//
//
// std::cout << "meeeeeeep! score: " << score_vec[k] << " " << res << "\n";
// }
// }
//
// m_pnt.m_qs_bestscore[i] = score_vec[k];
// m_pnt.m_qs_bestedge[i] = block.edges[k];
// }
// }
// }
// }
// }
#endif
{