-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtntblast_util.cpp
More file actions
1844 lines (1403 loc) · 48 KB
/
tntblast_util.cpp
File metadata and controls
1844 lines (1403 loc) · 48 KB
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
#ifdef USE_MPI
#include <mpi.h>
#endif // USE_MPI
#include "tntblast.h"
#include "degenerate_na.h"
#include "primer.h"
#include "throw.h"
using namespace std;
// Global variables
extern int mpi_numtasks;
extern int mpi_rank;
inline string toupper(const string &m_str)
{
string ret(m_str);
for(string::iterator i = ret.begin();i != ret.end();++i){
*i = toupper(*i);
}
return ret;
}
// Allow sorting objects of type hybrid_sig by assay oligos to remove redundant assays that
// may have been created by multiplex assay expansion. Keep in mind that the forward and reverse
// primer oligos can be reversed and still yeild the *same* assay!
struct sort_by_seq // < using oligo sequences
{
inline bool operator()(const hybrid_sig &m_a, const hybrid_sig &m_b) const
{
// Impose a consistent order on all assay oligo
size_t F_a = m_a.forward_oligo_str_index;
size_t R_a = m_a.reverse_oligo_str_index;
if(F_a < R_a){
swap(F_a, R_a);
}
size_t F_b = m_b.forward_oligo_str_index;
size_t R_b = m_b.reverse_oligo_str_index;
if(F_b < R_b){
swap(F_b, R_b);
}
if(F_a == F_b){
if(R_a == R_b){
return m_a.probe_oligo_str_index < m_b.probe_oligo_str_index;
}
return R_a < R_b;
}
return F_a < F_b;
};
};
struct compare_by_seq // == using oligo sequences
{
inline bool operator()(const hybrid_sig &m_a, const hybrid_sig &m_b) const
{
// Impose a consistent order on all assay oligo
size_t F_a = m_a.forward_oligo_str_index;
size_t R_a = m_a.reverse_oligo_str_index;
if(F_a < R_a){
swap(F_a, R_a);
}
size_t F_b = m_b.forward_oligo_str_index;
size_t R_b = m_b.reverse_oligo_str_index;
if(F_b < R_b){
swap(F_b, R_b);
}
if( F_a != F_b){
return false;
}
if( R_a != R_b){
return false;
}
return m_a.probe_oligo_str_index == m_b.probe_oligo_str_index;
};
};
string top_strand(const string &m_align);
void mask_binding_sites(string &m_amplicon, const hybrid_sig &m_sig, const int &m_mask,
const float &m_min_primer_tm, const float &m_min_probe_tm, NucCruc &m_melt,
const float &m_forward_primer_strand,
const float &m_reverse_primer_strand, const float &m_probe_strand,
const vector<string> &m_oligo_table)
{
if(m_mask == NO_MASK){
return;
}
if( m_sig.has_primers() ){
m_melt.strand(m_forward_primer_strand, 0.0f);
mask_primer_5(
m_amplicon,
index_to_str(m_sig.forward_oligo_str_index, m_oligo_table),
m_melt,
(m_mask & MASK_PRIMERS) == 0 ? false : true,
(m_mask & REPLACE_PRIMERS) == 0 ? false : true);
m_melt.strand(m_reverse_primer_strand, 0.0f);
mask_primer_3(
m_amplicon,
index_to_str(m_sig.reverse_oligo_str_index, m_oligo_table),
m_melt,
(m_mask & MASK_PRIMERS) == 0 ? false : true,
(m_mask & REPLACE_PRIMERS) == 0 ? false : true);
}
if( (m_mask & MASK_PROBE) && m_sig.has_probe() ){
m_melt.strand(m_probe_strand, 0.0f);
mask_probe(
m_amplicon,
index_to_str(m_sig.probe_oligo_str_index, m_oligo_table),
m_melt, m_min_probe_tm);
}
}
void mask_primer_5(string &m_amp, const string &m_oligo, NucCruc &m_melt,
const bool &m_mask, const bool &m_replace)
{
// If we don't need to mask or replace, return right away!
if(!m_mask && !m_replace){
return;
}
const size_t len = m_amp.size();
const size_t oligo_len = m_oligo.size();
// If the oligo sequence binds to the amplicon, mask the binding site with lower
// case letters
m_melt.set_query(m_oligo);
m_melt.clear_target();
bool valid_base = true;
unsigned int gap_offset = 0;
// Load the reverse complement of the target (while avoiding any illegal bases)
for(unsigned int i = 0;valid_base && (i < oligo_len);i++){
switch(toupper(m_amp[i])){
case 'A':
m_melt.push_front_target(BASE::T);
break;
case 'T':
m_melt.push_front_target(BASE::A);
break;
case 'G':
m_melt.push_front_target(BASE::C);
break;
case 'C':
m_melt.push_front_target(BASE::G);
break;
case 'I':
m_melt.push_front_target(BASE::I);
break;
// IUPAC degenerate bases
case 'M': // A or C
m_melt.push_front_target(BASE::K);
break;
case 'R': // G or A
m_melt.push_front_target(BASE::Y);
break;
case 'S': // G or C
m_melt.push_front_target(BASE::S);
break;
case 'V': // G or C or A
m_melt.push_front_target(BASE::B);
break;
case 'W': // A or T
m_melt.push_front_target(BASE::W);
break;
case 'Y': // T or C
m_melt.push_front_target(BASE::R);
break;
case 'H': // A or C or T
m_melt.push_front_target(BASE::D);
break;
case 'K': // G or T
m_melt.push_front_target(BASE::M);
break;
case 'D': // G or A or T
m_melt.push_front_target(BASE::H);
break;
case 'B': // G or T or C
m_melt.push_front_target(BASE::V);
break;
case 'N': // A or T or G or C
m_melt.push_front_target(BASE::N);
break;
case '-':
// Count any terminal gap symbols added to pad the amplicon
++gap_offset;
break;
default:
// Stop adding bases if we encounter a bad base
valid_base = false;
break;
};
}
const unsigned int target_len = m_melt.size_target();
m_melt.approximate_tm_heterodimer();
// What is the range of target bases that have been bound? The
// maximum range is [0, oligo_len].
pair<unsigned int, unsigned int> range = m_melt.alignment_range_target();
range.first = gap_offset + target_len - range.first - 1;
range.second = gap_offset + target_len - range.second - 1;
if(m_replace){
// Append the primer sequence to the 5' tail of the amplicon
m_amp = m_oligo + m_amp.substr(range.first + 1, len - range.first - 1);
if(m_mask){
for(unsigned int j = 0;j < oligo_len;j++){
m_amp[j] = tolower(m_amp[j]);
}
}
}
else{
if(m_mask){
// Mask this region of the amplicon
for(int j = (int)range.second;j <= (int)range.first;j++){
m_amp[j] = tolower(m_amp[j]);
}
}
}
}
void mask_primer_3(string &m_amp, const string &m_oligo, NucCruc &m_melt,
const bool &m_mask, const bool &m_replace)
{
// If we don't need to mask or replace, return right away!
if(!m_mask && !m_replace){
return;
}
const size_t len = m_amp.size();
const size_t oligo_len = m_oligo.size();
// If the oligo sequence binds to the amplicon, mask the binding site with lower
// case letters
m_melt.set_query(m_oligo);
m_melt.clear_target();
size_t i;
unsigned int gap_offset = 0;
// Load the target (while avoiding any illegal bases)
for(i = len - oligo_len;i < len;i++){
switch( toupper(m_amp[i]) ){
case 'A':
m_melt.push_back_target(BASE::A);
break;
case 'T':
m_melt.push_back_target(BASE::T);
break;
case 'G':
m_melt.push_back_target(BASE::G);
break;
case 'C':
m_melt.push_back_target(BASE::C);
break;
case 'I':
m_melt.push_back_target(BASE::I);
break;
// IUPAC degenerate bases
case 'M': // A or C
m_melt.push_back_target(BASE::K);
break;
case 'R': // G or A
m_melt.push_back_target(BASE::Y);
break;
case 'S': // G or C
m_melt.push_back_target(BASE::S);
break;
case 'V': // G or C or A
m_melt.push_back_target(BASE::B);
break;
case 'W': // A or T
m_melt.push_back_target(BASE::W);
break;
case 'Y': // T or C
m_melt.push_back_target(BASE::R);
break;
case 'H': // A or C or T
m_melt.push_back_target(BASE::D);
break;
case 'K': // G or T
m_melt.push_back_target(BASE::M);
break;
case 'D': // G or A or T
m_melt.push_back_target(BASE::H);
break;
case 'B': // G or T or C
m_melt.push_back_target(BASE::V);
break;
case 'N': // A or T or G or C
m_melt.push_back_target(BASE::N);
break;
case '-':
// Count any terminal gap symbols added to pad the amplicon
++gap_offset;
break;
default:
m_melt.clear_target();
break;
};
}
const unsigned int target_len = m_melt.size_target();
m_melt.approximate_tm_heterodimer();
// What is the range of target bases that have been bound? The
// maximum range is [0, oligo_len].
pair<int, int> range = m_melt.alignment_range_target();
range.first -= gap_offset;
range.second -= gap_offset;
if(m_replace){
// Compute the reverse complement of m_oligo
string oligo_complement(oligo_len, 'A');
i = 0;
for(string::const_reverse_iterator iter = m_oligo.rbegin();iter != m_oligo.rend();iter++, i++){
oligo_complement[i] = base_complement(*iter);
}
// Append the primer sequence to the 3' tail of the amplicon
m_amp = m_amp.substr(0, len - target_len + range.first) + oligo_complement;
if(m_mask){
// Compute the new amplicon length
const size_t new_len = m_amp.size();
for(size_t j = new_len - oligo_len;j < new_len;j++){
m_amp[j] = tolower(m_amp[j]);
}
}
}
else{
if(m_mask){
// Mask this region of the amplicon
const size_t stop = (len + range.second + 1) - target_len;
for(size_t j = len - target_len + range.first;j < stop;j++){
m_amp[j] = tolower(m_amp[j]);
}
}
}
}
void mask_probe(string &m_amp, const string &m_oligo, NucCruc &m_melt, const float &m_min_tm)
{
const size_t len = m_amp.size();
const size_t oligo_len = m_oligo.size();
const size_t padded_oligo_len = oligo_len + 2; // Oligo length + dangling bases
// If the oligo sequence binds to the amplicon, mask the binding site with lower
// case letters
m_melt.set_query(m_oligo);
m_melt.clear_target();
size_t i;
// Test the oligo against the plus strand of the amplicon
for(i = 0;i < len;i++){
switch(toupper(m_amp[i])){
case 'A':
m_melt.push_back_target(BASE::A);
break;
case 'T':
m_melt.push_back_target(BASE::T);
break;
case 'G':
m_melt.push_back_target(BASE::G);
break;
case 'C':
m_melt.push_back_target(BASE::C);
break;
case 'I':
m_melt.push_back_target(BASE::I);
break;
// IUPAC degenerate bases
case 'M': // A or C
m_melt.push_back_target(BASE::K);
break;
case 'R': // G or A
m_melt.push_back_target(BASE::Y);
break;
case 'S': // G or C
m_melt.push_back_target(BASE::S);
break;
case 'V': // G or C or A
m_melt.push_back_target(BASE::B);
break;
case 'W': // A or T
m_melt.push_back_target(BASE::W);
break;
case 'Y': // T or C
m_melt.push_back_target(BASE::R);
break;
case 'H': // A or C or T
m_melt.push_back_target(BASE::D);
break;
case 'K': // G or T
m_melt.push_back_target(BASE::M);
break;
case 'D': // G or A or T
m_melt.push_back_target(BASE::H);
break;
case 'B': // G or T or C
m_melt.push_back_target(BASE::V);
break;
case 'N': // A or T or G or C
m_melt.push_back_target(BASE::N);
break;
default:
// We have encountered a gap or unknown base
m_melt.clear_target();
break;
};
// Compute the melting temperatures
// Tm for the plus strand
const float tm = m_melt.approximate_tm_heterodimer();
if(tm >= m_min_tm){
// What is the range of target bases that have been bound? The
// maximum range is [0, m_melt.size_target()].
pair<unsigned int, unsigned int> range = m_melt.alignment_range_target();
const unsigned int target_len = m_melt.size_target();
// Shift the range into the target coordinate system
range.first += (unsigned int)( i - (target_len - 1) );
range.second += (unsigned int)( i - (target_len - 1) );
// Mask this region of the amplicon
for(int j = (int)range.second;j >= (int)range.first;j--){
m_amp[j] = tolower(m_amp[j]);
}
}
if(m_melt.size_target() == padded_oligo_len){
m_melt.pop_front_target();
}
}
m_melt.clear_target();
// Test the oligo against the minus strand of the amplicon
for(i = 0;i < len;i++){
switch( toupper(m_amp[i]) ){
case 'A':
m_melt.push_front_target(BASE::T);
break;
case 'T':
m_melt.push_front_target(BASE::A);
break;
case 'G':
m_melt.push_front_target(BASE::C);
break;
case 'C':
m_melt.push_front_target(BASE::G);
break;
case 'I':
m_melt.push_front_target(BASE::I);
break;
// IUPAC degenerate bases
case 'M': // A or C
m_melt.push_front_target(BASE::K);
break;
case 'R': // G or A
m_melt.push_front_target(BASE::Y);
break;
case 'S': // G or C
m_melt.push_front_target(BASE::S);
break;
case 'V': // G or C or A
m_melt.push_front_target(BASE::B);
break;
case 'W': // A or T
m_melt.push_front_target(BASE::W);
break;
case 'Y': // T or C
m_melt.push_front_target(BASE::R);
break;
case 'H': // A or C or T
m_melt.push_front_target(BASE::D);
break;
case 'K': // G or T
m_melt.push_front_target(BASE::M);
break;
case 'D': // G or A or T
m_melt.push_front_target(BASE::H);
break;
case 'B': // G or T or C
m_melt.push_front_target(BASE::V);
break;
case 'N': // A or T or G or C
m_melt.push_front_target(BASE::N);
break;
default:
// We have encountered a gap or unknown base
m_melt.clear_target();
break;
};
// Compute the melting temperatures
// Tm for the plus strand
const float tm = m_melt.approximate_tm_heterodimer();
if(tm >= m_min_tm){
// What is the range of target bases that have been bound? The
// maximum range is [0, m_melt.size_target()].
pair<unsigned int, unsigned int> range = m_melt.alignment_range_target();
// Shift the range into the target coordinate system
range.first = (unsigned int)(i - range.first);
range.second = (unsigned int)(i - range.second);
// Mask this region of the amplicon
for(int j = (int)range.second;j <= (int)range.first;j++){
m_amp[j] = tolower(m_amp[j]);
}
}
if(m_melt.size_target() == padded_oligo_len){
m_melt.pop_back_target();
}
}
}
// Expand degenerate NA bases as needed
vector<hybrid_sig> expand_degenerate_signatures(const vector<hybrid_sig> &m_sig,
const bool &m_degen_rescale_ct,
const vector<string> &m_oligo_table,
unordered_map<string, size_t> &m_str_table)
{
vector<hybrid_sig> ret;
// Renumber all of the assay id's to ensure that
// a) they start at 0
// b) are contiguous
int id = 0;
for(vector<hybrid_sig>::const_iterator iter = m_sig.begin();iter != m_sig.end();iter++){
list< pair<string, string> > primers;
if( (iter->forward_oligo_str_index != INVALID_INDEX) && (iter->reverse_oligo_str_index != INVALID_INDEX) ){
primers.push_back( make_pair(
index_to_str(iter->forward_oligo_str_index, m_oligo_table),
index_to_str(iter->reverse_oligo_str_index, m_oligo_table) ) );
}
try{
primers = expand_nucleic_acid(primers);
}
catch(const char* error){
cerr << "Error expanding primers in assay: " << index_to_str(iter->name_str_index, m_oligo_table) << endl;
THROW(error);
}
list<string> probes;
try{
if(iter->probe_oligo_str_index != INVALID_INDEX){
probes = expand_nucleic_acid( index_to_str(iter->probe_oligo_str_index, m_oligo_table) );
}
}
catch(const char* error){
cerr << "Error expanding probe in assay: " << index_to_str(iter->name_str_index, m_oligo_table) << endl;
THROW(error);
}
const size_t num_expanded_assays = primers.size() * probes.size();
if(num_expanded_assays > 1){
cout << "Expanded degenerate bases in "
<< index_to_str(iter->name_str_index, m_oligo_table) << " to make "
<< num_expanded_assays << " non-degenerate assays" << endl;
}
const int degen_forward = m_degen_rescale_ct ?
degeneracy( index_to_str(iter->forward_oligo_str_index, m_oligo_table) ) : 1;
const int degen_reverse = m_degen_rescale_ct ?
degeneracy( index_to_str(iter->reverse_oligo_str_index, m_oligo_table) ) : 1;
const int degen_probe = m_degen_rescale_ct ?
degeneracy( index_to_str(iter->probe_oligo_str_index, m_oligo_table) ) : 1;
list< pair<string, string> >::const_iterator primer_iter;
list<string>::const_iterator probe_iter;
if( primers.empty() ){
for(probe_iter = probes.begin();probe_iter != probes.end();probe_iter++){
hybrid_sig tmp( iter->name_str_index,
str_to_index(*probe_iter, m_str_table),
iter->my_id() );
tmp.probe_degen = degen_probe;
// "Real" assays derived from degenerate, "virtual" assays, get an additional identifier.
// This additional identifier is needed to make the search results unique when using
// target fragmentation.
tmp.my_degen_id(id++);
ret.push_back(tmp);
}
}
else{
for(primer_iter = primers.begin();primer_iter != primers.end();primer_iter++){
if( probes.empty() ){
hybrid_sig tmp( iter->name_str_index,
str_to_index(primer_iter->first, m_str_table),
str_to_index(primer_iter->second, m_str_table),
iter->my_id() );
tmp.forward_degen = degen_forward;
tmp.reverse_degen = degen_reverse;
// "Real" assays derived from degenerate, "virtual" assays, get an additional identifier.
// This additional identifier is needed to make the search results unique when using
// target fragmentation.
tmp.my_degen_id(id++);
ret.push_back(tmp);
}
else{
for(probe_iter = probes.begin();probe_iter != probes.end();probe_iter++){
// This was the first attempt (every "real" assay created from a degenerate assay
// got a new unique assay id).
//hybrid_sig tmp(iter->name, primer_iter->first, primer_iter->second,
// *probe_iter, id++);
// This is the second attempt (every "real" assay created from a degenerate assay
// uses the assay id of its degenerate parent).
hybrid_sig tmp( iter->name_str_index,
str_to_index(primer_iter->first, m_str_table),
str_to_index(primer_iter->second, m_str_table),
str_to_index(*probe_iter, m_str_table),
iter->my_id() );
tmp.forward_degen = degen_forward;
tmp.reverse_degen = degen_reverse;
tmp.probe_degen = degen_probe;
// "Real" assays derived from degenerate, "virtual" assays, get an additional identifier.
// This additional identifier is needed to make the search results unique when using
// target fragmentation.
tmp.my_degen_id(id++);
ret.push_back(tmp);
}
}
}
}
}
return ret;
}
vector<hybrid_sig> multiplex_expansion(const vector<hybrid_sig> &m_sig,
const unsigned int &m_format, vector<string> &m_index_table,
unordered_map<string, size_t> &m_str_table)
{
vector<hybrid_sig> ret;
// Renumber all of the assay id's to insure that
// a) they start at 0
// b) are contiguous
int id = 0;
if( (m_format == ASSAY_PADLOCK) || (m_format == ASSAY_MIPS) ){
// Assume that all Padlock/MOLpcr probes are stored as forward-reverse pairs in the input file.
//
// MIP assay oligos are tethered togther by a linker, so no multiplexing is needed.
// It is possible to ligate *different* MIPS to form a single DNA molecule:
// - When a linear DNA molecule is created by ligating two MIPS together, this molecule will not be sequenced
// and could mask an intended MIPS product (false negative)
// - When a pair of *nested* MIPS are ligated to form a circular DNA molecule, the expected target sequence
// of the outer MIP can be masked (partial negative).
for(vector<hybrid_sig>::const_iterator i = m_sig.begin();i != m_sig.end();i++){
for(vector<hybrid_sig>::const_iterator j = m_sig.begin();j != m_sig.end();j++){
const string assay_name = (i == j) ? index_to_str(i->name_str_index, m_index_table)
: index_to_str(i->name_str_index, m_index_table) + "(5')/" +
index_to_str(j->name_str_index, m_index_table) + "(3')";
hybrid_sig tmp(
str_to_index(assay_name, m_str_table),
i->forward_oligo_str_index, j->reverse_oligo_str_index, id++);
ret.push_back(tmp);
}
}
}
if(m_format == ASSAY_PCR){
// Is there at least one probe in the collection of input assays?
bool has_probes = false;
for(vector<hybrid_sig>::const_iterator i = m_sig.begin();i != m_sig.end();i++){
// Is this a probe-only assay?
if(i->forward_oligo_str_index == INVALID_INDEX){
hybrid_sig tmp(i->name_str_index, i->probe_oligo_str_index, id++);
continue;
}
// Count the number of PCR primer pairs that have an associated probe
if(i->probe_oligo_str_index != INVALID_INDEX){
has_probes = true;
}
for(vector<hybrid_sig>::const_iterator j = m_sig.begin();j != m_sig.end();j++){
// Since tntblast automatically tests for PCR with the *same* primers
// (i.e. two forward or two reverse), there is no need to enumerate
// an assay with the same primer in both positions
if(i->forward_oligo_str_index == j->reverse_oligo_str_index){
continue;
}
const string assay_name = index_to_str(i->name_str_index, m_index_table) + "(F)/" +
index_to_str(j->name_str_index, m_index_table) + "(R)";
hybrid_sig tmp(
str_to_index(assay_name, m_str_table),
i->forward_oligo_str_index, j->reverse_oligo_str_index, id++);
ret.push_back(tmp);
}
}
for(vector<hybrid_sig>::const_iterator i = m_sig.begin();i != m_sig.end();i++){
// Is this a probe-only assay?
if(i->forward_oligo_str_index == INVALID_INDEX){
continue;
}
for(vector<hybrid_sig>::const_iterator j = m_sig.begin();j != m_sig.end();j++){
// Since tntblast automatically tests for PCR with the *same* primers
// (i.e. two forward or two reverse), there is no need to enumerate
// an assay with the same primer in both positions
if(i->forward_oligo_str_index == j->forward_oligo_str_index){
continue;
}
const string assay_name = index_to_str(i->name_str_index, m_index_table) + "(F)/" +
index_to_str(j->name_str_index, m_index_table) + "(F)";
hybrid_sig tmp(
str_to_index(assay_name, m_str_table),
i->forward_oligo_str_index, j->forward_oligo_str_index, id++);
ret.push_back(tmp);
}
}
for(vector<hybrid_sig>::const_iterator i = m_sig.begin();i != m_sig.end();i++){
// Is this a probe-only assay?
if(i->forward_oligo_str_index == INVALID_INDEX){
continue;
}
for(vector<hybrid_sig>::const_iterator j = m_sig.begin();j != m_sig.end();j++){
// Since tntblast automatically tests for PCR with the *same* primers
// (i.e. two forward or two reverse), there is no need to enumerate
// an assay with the same primer in both positions
if(i->reverse_oligo_str_index == j->reverse_oligo_str_index){
continue;
}
const string assay_name = index_to_str(i->name_str_index, m_index_table) + "(R)/" +
index_to_str(j->name_str_index, m_index_table) + "(R)";
hybrid_sig tmp(
str_to_index(assay_name, m_str_table),
i->reverse_oligo_str_index, j->reverse_oligo_str_index, id++);
ret.push_back(tmp);
}
}
if(has_probes){
// Add probes to all of the assays!
vector<hybrid_sig> ret_with_probe;
// Reset the id
id = 0;
// Update the oligo table to account for the new names referenced in ret
m_index_table = ordered_keys(m_str_table);
for(vector<hybrid_sig>::const_iterator i = ret.begin();i != ret.end();i++){
for(vector<hybrid_sig>::const_iterator j = m_sig.begin();j != m_sig.end();j++){
// Allow a mixture of assay with and without probes.
// If one or more probes is present, then *all* of the
// resulting assays will have probes.
if(j->probe_oligo_str_index == INVALID_INDEX){
continue;
}
const string assay_name = index_to_str(i->name_str_index, m_index_table) + "+" +
index_to_str(j->name_str_index, m_index_table) + "(P)";
hybrid_sig tmp(
str_to_index(assay_name, m_str_table),
i->forward_oligo_str_index, i->reverse_oligo_str_index,
j->probe_oligo_str_index, id++);
ret_with_probe.push_back(tmp);
}
}
// Update the oligo table (again!) to account for the assay names that now include probes
m_index_table = ordered_keys(m_str_table);
ret = ret_with_probe;
}
}
if(m_format == ASSAY_AFFYMETRIX){
// This is a probe-only assay, no multiplexing is needed
return m_sig;
}
// If our input assays shared oligos in common, than we have created additional
// redundant assay
sort(ret.begin(), ret.end(), sort_by_seq() );
// Remove the redundant assays
ret.erase( unique( ret.begin(), ret.end(), compare_by_seq() ), ret.end() );
// Reset the id
id = 0;
for(vector<hybrid_sig>::iterator i = ret.begin();i != ret.end();++i){
i->my_id(id);
i->my_degen_id(id);
++id;
}
cerr << "Multiplexing has created " << ret.size() << " assays from " << m_sig.size()
<< " input assays" << endl;
return ret;
}
string primer_heuristics(const string &m_primer)
{
ASSY_HEURISTIC::PCRPrimer pcr_test(POLY_3_GC | MULTI_5_GC | NO_POLY_RUNS | NO_3_T);
// Set the run length -- we should make this a user parameter!
pcr_test.run(5);
return pcr_test.error( pcr_test(m_primer) );
}
char base_complement(const char &m_base)
{
switch( toupper(m_base) ){
case 'A':
return 'T';
case 'T':
return 'A';
case 'G':
return 'C';
case 'C':
return 'G';
};
return 'N';
}
#ifdef USE_MPI
void distribute_queries(const vector<hybrid_sig> &m_sig)
{
vector<hybrid_sig>::const_iterator iter;
unsigned int num_query = m_sig.size();
unsigned int buffer_size = sizeof(unsigned int);
// Send all queries to all nodes
for(iter = m_sig.begin();iter != m_sig.end();iter++){
buffer_size += mpi_size(*iter);
}
// Tell the workers the amount of memory to allocate
MPI_Bcast(&buffer_size, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
unsigned char *buffer = new unsigned char [buffer_size];
if(buffer == NULL){
THROW(__FILE__ "distribute_queries: Unable to allocate send buffer");
}
unsigned char *ptr = buffer;
memcpy( ptr, &num_query, sizeof(unsigned int) );
ptr += sizeof(unsigned int);
for(iter = m_sig.begin();iter != m_sig.end();iter++){
ptr = mpi_pack(ptr, *iter);
}
MPI_Bcast(buffer, buffer_size, MPI_BYTE, 0, MPI_COMM_WORLD);
delete [] buffer;
}
void receive_queries(vector<hybrid_sig> &m_sig)
{
unsigned int buffer_size = 0;
unsigned int num_query = 0;
MPI_Bcast(&buffer_size, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD);
unsigned char *buffer = new unsigned char [buffer_size];
if(buffer == NULL){
THROW(__FILE__ "receive_queries: Unable to allocate send buffer");
}
MPI_Bcast(buffer, buffer_size, MPI_BYTE, 0, MPI_COMM_WORLD);
unsigned char *ptr = buffer;
// The number of assays to read from the buffer that the master just
// sent us.
memcpy( &num_query, ptr, sizeof(unsigned int) );