-
Notifications
You must be signed in to change notification settings - Fork 10
/
nuc_cruc.cpp
3255 lines (2577 loc) · 87.6 KB
/
nuc_cruc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "nuc_cruc.h"
#include <math.h>
#include <algorithm>
#include <iostream>
#include <sstream>
using namespace std;
using namespace BASE;
int find_loop_index(const CircleBuffer<nucleic_acid, MAX_SEQUENCE_LENGTH> &m_q,
const unsigned int &m_start, const unsigned int &m_len);
inline nucleic_acid resolve_degenerate(const nucleic_acid &m_target_base, const nucleic_acid &m_query_base)
{
nucleic_acid best_target = m_target_base;
// When a degenerate target is not complementary to the query,
// choose a "real" base to represent the target in the following, arbitrary, order:
// A, T, G, C
switch(m_target_base){
// Real bases
case A: case C: case G: case T: case I:
// "Virtual bases"
case E: case GAP:
break;
// IUPAC degenerate bases
case M: // A or C
switch(m_query_base){
case T:
best_target = A;
break;
case G:
best_target = C;
break;
default:
best_target = A;
break;
};
break;
case R: // G or A
switch(m_query_base){
case T:
best_target = A;
break;
case C:
best_target = G;
break;
default:
best_target = A;
break;
};
break;
case S: // G or C
switch(m_query_base){
case G:
best_target = C;
break;
case C:
best_target = G;
break;
default:
best_target = G;
break;
};
break;
case V: // G or C or A
switch(m_query_base){
case G:
best_target = C;
break;
case C:
best_target = G;
break;
case T:
best_target = A;
break;
default:
best_target = A;
break;
};
break;
case W: // A or T
switch(m_query_base){
case A:
best_target = T;
break;
case T:
best_target = A;
break;
default:
best_target = A;
break;
};
break;
case Y: // T or C
switch(m_query_base){
case G:
best_target = C;
break;
case A:
best_target = T;
break;
default:
best_target = T;
break;
};
break;
case H: // A or C or T
switch(m_query_base){
case T:
best_target = A;
break;
case G:
best_target = C;
break;
case A:
best_target = T;
break;
default:
best_target = A;
break;
};
break;
case K: // G or T
switch(m_query_base){
case C:
best_target = G;
break;
case A:
best_target = T;
break;
default:
best_target = T;
break;
};
break;
case D: // G or A or T
switch(m_query_base){
case C:
best_target = G;
break;
case T:
best_target = A;
break;
case A:
best_target = T;
break;
default:
best_target = A;
break;
};
break;
case B: // G or T or C
switch(m_query_base){
case C:
best_target = G;
break;
case A:
best_target = T;
break;
case G:
best_target = C;
break;
default:
best_target = T;
break;
};
case N: // A or T or G or C
switch(m_query_base){
case A:
best_target = T;
break;
case T:
best_target = A;
break;
case G:
best_target = C;
break;
case C:
best_target = G;
break;
default:
best_target = A;
break;
};
break;
};
return best_target;
}
inline int best_base_pair(const nucleic_acid &m_target_base, const nucleic_acid &m_query_base)
{
// Currently, degenerate bases are only allowed in either the target base or
// the query base, but not both. When we have a degenerate nucleotide in either the
// target or query position, return the most optimistic base pair -- that is, pick the
// complementary pair if allowed by the degeneracy.
return BASE_PAIR(
resolve_degenerate(m_target_base, m_query_base),
resolve_degenerate(m_query_base, m_target_base)
);
}
#ifdef __DEBUG
const char *bp[] = {
"AA", "AC", "AG", "AT", "AI", "AE", "A_",
"CA", "CC", "CG", "CT", "CI", "CE", "C_",
"GA", "GC", "GG", "GT", "GI", "GE", "G_",
"TA", "TC", "TG", "TT", "TI", "TE", "T_",
"IA", "IC", "IG", "IT", "II", "IE", "I_",
"EA", "EC", "EG", "ET", "EI", "EE", "E_",
"_A", "_C", "_G", "_T", "_I", "_E", "__"};
#endif // __DEBUG
NucCruc::NucCruc(const unsigned int &m_param_set /*= SANTA_LUCIA*/, const float &m_tm /* = 310.15 */)
{
// Are a pair of bases a watson and crick match (i.e. A & T, G & C, I & any base)
memset( watson_and_crick, 0, NUM_BASE_PAIR*sizeof(bool) );
watson_and_crick[AT] = watson_and_crick[TA] = true;
watson_and_crick[CG] = watson_and_crick[GC] = true;
watson_and_crick[AI] = watson_and_crick[IA] = true;
watson_and_crick[TI] = watson_and_crick[IT] = true;
watson_and_crick[GI] = watson_and_crick[IG] = true;
watson_and_crick[CI] = watson_and_crick[IC] = true;
watson_and_crick[II] = true;
// The supplementary parameters from the TM program of Leber and Kaderali.
// The associated paper is Leber and Kaderali et. al. Bioiformatics, 2005
// (but these parameters only show up in the program source code).
// delta G@37C = 0.3
//param_supp[LOOP_H] = 0.0f;
//param_supp[LOOP_S] = -0.97e-3f;
// delta G@37C = 0.4
//param_supp[BULGE_H] = 0.0f;
//param_supp[BULGE_S] = -1.3e-3f;
// delta G@37C = 1.75
//param_supp[TERMINAL_MATCH_AT_H] = -2.66f;
//param_supp[TERMINAL_MATCH_AT_S] = -14.22e-3f;
// delta G@37C = 1.75
//param_supp[TERMINAL_MATCH_GC_H] = -2.66f;
//param_supp[TERMINAL_MATCH_GC_S] = -14.22e-3f;
// delta G@37C = 1.75
//param_supp[TERMINAL_MATCH_I_H] = -2.66f;
//param_supp[TERMINAL_MATCH_I_S] = -14.22e-3f;
// delta G@37C = 2.0
//param_supp[TERMINAL_MISMATCH_H] = 0.0f;
//param_supp[TERMINAL_MISMATCH_S] = -6.45e-3f;
//////////////////////////////////////////////////////////////////////////
// Supplementary parameters determined by minimization of delta G
// LOOP //////////////////////////////////////////////////////////////////
param_supp[LOOP_H] = -5.779f;
param_supp[LOOP_S] = -2.330e-2f;
// BULGE //////////////////////////////////////////////////////////////////
param_supp[BULGE_H] = 5.247e-1f;
param_supp[BULGE_S] = 3.318e-4f;
// TERMINAL MATCH AT ///////////////////////////////////////////////////////
param_supp[TERMINAL_MATCH_AT_H] = -4.474f;
param_supp[TERMINAL_MATCH_AT_S] = -2.091e-2f;
// TERMINAL MATCH GC ///////////////////////////////////////////////////////
param_supp[TERMINAL_MATCH_GC_H] = -3.000f;
param_supp[TERMINAL_MATCH_GC_S] = -1.318e-2f;
// TERMINAL MATCH I ////////////////////////////////////////////////////////
// For now, consider matches with inosine to be equivalent to
// A-T matches
param_supp[TERMINAL_MATCH_I_H] = param_supp[TERMINAL_MATCH_AT_H];
param_supp[TERMINAL_MATCH_I_S] = param_supp[TERMINAL_MATCH_AT_S];
// TERMINAL MISMATCH ///////////////////////////////////////////////////////
param_supp[TERMINAL_MISMATCH_H] = -2.421f;
param_supp[TERMINAL_MISMATCH_S] = -1.180e-2f;
// Initialize the salt correction mutlipliers
param_supp_salt[LOOP_SALT] = 3.08f;
param_supp_salt[BULGE_SALT] = 0.69f;
param_supp_salt[TERMINAL_MATCH_SALT] = 0.56f;
param_supp_salt[TERMINAL_MISMATCH_SALT] = 1.31f;
// Initialize the parameter set
switch(m_param_set){
case SANTA_LUCIA:
init_param_Santa_Lucia();
break;
default:
throw "NucCruc: Unknowm parameter set!";
};
target_T = -1.0f;
use_dinkelbach = false;
// By default, always perform full alignments
diagonal_alignment = false;
// The dynamic programming parameters will be initialized when the user sets the salt
// concentraion
target_T = m_tm;
// Initialize the concentrations with non-physical parameters
// to force the user to specify them!
na_concentration = -1.0f;
strand_concentration = -1.0f;
// By default, allow dangling bases on both ends of the alignment
enable_dangle = make_pair(true, true);
// Explore at most max_dp_path_enum high scoring paths through
// the dynamic programming matrix
max_dp_path_enum = 16;
dp_matrix = new NC_Elem [(MAX_SEQUENCE_LENGTH + 1)*(MAX_SEQUENCE_LENGTH + 1)];
// There is *no* current valid alignment
tm_mode = INVALID;
};
void NucCruc::update_dp_param()
{
if(target_T < 0.0f){
throw "update_dp_param: target_T < 0";
}
if(na_concentration <= 0.0f){
throw "update_dp_param: na_concentration <= 0";
}
// The total salt correction has the form:
// dS_total = param_SALT * (0.5f*num_base - 1) * log(na_concentration)
// Where num_base is the total number of aligned bases in the *duplex*. The salt correction
// per pair of duplex bases is approximately:
// dS_per_base = param_SALT * log(na_concentration)
const float salt_correction = param_SALT*log(na_concentration);
const float loop_sc = salt_correction*param_supp_salt[LOOP_SALT];
const float bulge_sc = salt_correction*param_supp_salt[BULGE_SALT];
const float term_match_sc = salt_correction*param_supp_salt[TERMINAL_MATCH_SALT];
const float term_mismatch_sc = salt_correction*param_supp_salt[TERMINAL_MISMATCH_SALT];
for(unsigned int i = 0;i < NUM_BASE_PAIR*NUM_BASE_PAIR;i++){
// For these pairs, assume that all four bases receive a salt
// correction (the pairs for which this is not true are handled below).
// Since each pair of bound bases is counted twice in the NN model,
// only apply a single pair salt correction to each set of NN terms.
delta_g[i] = NC_SCORE_SCALE(param_H[i] - target_T*(param_S[i] + salt_correction) );
}
///////////////////////////////////////////////////////////////////////////////////
// Double mismatches and pairs with gaps. These parameters are not specified
// by SantaLucia et. al. The score for these pairs is *always* greater than
// or equal to zero (i.e. always energetically unfavored).
///////////////////////////////////////////////////////////////////////////////////
for(char i = BASE::A;i <= BASE::I;i++){
for(char j = BASE::A;j <= BASE::I;j++){
const int curr = BASE_PAIR(i, j);
for(char k = BASE::A;k <= BASE::I;k++){
const int prev1 = BASE_PAIR(k, BASE::GAP);
const int prev2 = BASE_PAIR(BASE::GAP, k);
if (watson_and_crick[curr]){
// bulge terminal match
if( (curr == AT) || (curr == TA) ){
NC_Score tmp_dg =
NC_SCORE_SCALE(param_supp[TERMINAL_MATCH_AT_H] -
target_T*(param_supp[TERMINAL_MATCH_AT_S] +
term_match_sc) );
delta_g[SCORE_INDEX(curr, prev1)] = delta_g[SCORE_INDEX(prev1, curr)] =
delta_g[SCORE_INDEX(curr, prev2)] = delta_g[SCORE_INDEX(prev2, curr)] =
max(0, tmp_dg);
}
else{
if( (curr == GC) || (curr == CG) ){
NC_Score tmp_dg =
NC_SCORE_SCALE(param_supp[TERMINAL_MATCH_GC_H] -
target_T*(param_supp[TERMINAL_MATCH_GC_S] +
term_match_sc) );
delta_g[SCORE_INDEX(curr, prev1)] = delta_g[SCORE_INDEX(prev1, curr)] =
delta_g[SCORE_INDEX(curr, prev2)] = delta_g[SCORE_INDEX(prev2, curr)] =
max(0, tmp_dg);
}
else{ // curr contains inosine
NC_Score tmp_dg =
NC_SCORE_SCALE(param_supp[TERMINAL_MATCH_I_H] -
target_T*(param_supp[TERMINAL_MATCH_I_S] +
term_match_sc) );
delta_g[SCORE_INDEX(curr, prev1)] = delta_g[SCORE_INDEX(prev1, curr)] =
delta_g[SCORE_INDEX(curr, prev2)] = delta_g[SCORE_INDEX(prev2, curr)] =
max(0, tmp_dg);
}
}
}
else{ // watson_and_crick[curr] == false
// mismatch bulge opening -- no salt correction
// since this pair is internal to a loop
NC_Score tmp_dg =
NC_SCORE_SCALE(param_supp[TERMINAL_MISMATCH_H] -
target_T*(param_supp[TERMINAL_MISMATCH_S] +
term_mismatch_sc) );
delta_g[SCORE_INDEX(curr, prev1)] = delta_g[SCORE_INDEX(prev1, curr)] =
delta_g[SCORE_INDEX(curr, prev2)] = delta_g[SCORE_INDEX(prev2, curr)] =
max(0, tmp_dg);
}
}
for(char k = BASE::A;k <= BASE::I;k++){
for(char l = BASE::A;l <= BASE::I;l++){
const int prev = BASE_PAIR(k, l);
// Is this pair a double mismatch?
if(!watson_and_crick[curr] && !watson_and_crick[prev]){
// No salt correction for pairs of mismatches
// (by definition, this NN-pair is internal to a loop).
NC_Score tmp_dg =
NC_SCORE_SCALE(param_supp[LOOP_H] -
target_T*(param_supp[LOOP_S] + loop_sc) );
delta_g[SCORE_INDEX(curr, prev)] = max(0, tmp_dg);
}
}
}
}
}
for(char i = BASE::A;i <= BASE::I; i++){
for(char j = BASE::A;j <= BASE::I;j++){
int curr = BASE_PAIR(i, BASE::GAP);
int prev = BASE_PAIR(j, BASE::GAP);
NC_Score tmp_dg =
NC_SCORE_SCALE(param_supp[BULGE_H] -
target_T*(param_supp[BULGE_S] + bulge_sc) );
delta_g[SCORE_INDEX(curr, prev)] = max(0, tmp_dg);
curr = BASE_PAIR(BASE::GAP, i);
prev = BASE_PAIR(BASE::GAP, j);
tmp_dg = NC_SCORE_SCALE(param_supp[BULGE_H] -
target_T*(param_supp[BULGE_S] + bulge_sc) );
delta_g[SCORE_INDEX(curr, prev)] = max(0, tmp_dg);
}
}
}
// Compute the Smith-Waterman local alignment between two sequences; the query
// and the target. Both the query and target sequences are assumed to be
// in 5'-3' orientation.
NC_Score NucCruc::align_dimer(const CircleBuffer<nucleic_acid, MAX_SEQUENCE_LENGTH> &m_q,
const CircleBuffer<nucleic_acid, MAX_SEQUENCE_LENGTH> &m_t)
{
// Reset the largest element pointer buffer
max_ptr.clear();
const unsigned int query_len = m_q.size();
const unsigned int target_len = m_t.size();
// Copy the target sequence into the target buffer
for(unsigned int i = 0;i < target_len;i++){
target_buffer[i] = m_t[i];
}
NC_Score max_score = -1;
for(unsigned int i = 1;i <= query_len;i++){
// Since both query and target sequences are in 5'-3' orientation, we must
// reverse one of the sequences. Reverse the query sequence.
const nucleic_acid query_base = m_q[query_len - i];
const nucleic_acid prev_query_base = (i == 1) ? GAP : m_q[query_len - (i - 1)];
// A B
// C X <-- dp[i][j]
NC_Elem *C_ptr = dp_matrix + i*(MAX_SEQUENCE_LENGTH + 1);
NC_Elem *X_ptr = C_ptr + 1;
NC_Elem *A_ptr = C_ptr - (MAX_SEQUENCE_LENGTH + 1);
NC_Elem *B_ptr = A_ptr + 1;
for(unsigned int j = 1;j <= target_len;j++,
A_ptr++, B_ptr++, C_ptr++, X_ptr++){
const nucleic_acid target_base = target_buffer[j - 1];
const nucleic_acid prev_target_base = ( (j == 1) ? GAP : target_buffer[j - 2] );
int cur_base_pair = best_base_pair(target_base, query_base);
// Match or mismatch
int prev_base_pair = best_base_pair(prev_target_base, prev_query_base);
//const NC_Score dg1 = (NC_Score(0) < A_ptr->M) ? A_ptr->M - delta_g[prev_base_pair][cur_base_pair] :
// - delta_g[prev_base_pair][cur_base_pair];
const NC_Score dg1 = max(NC_Score(0), A_ptr->M) - delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)];
// gap the query
prev_base_pair = best_base_pair(prev_target_base, GAP);
//const NC_Score dg2 = (NC_Score(0) < A_ptr->I_query) ? A_ptr->I_query - delta_g[prev_base_pair][cur_base_pair] :
// - delta_g[prev_base_pair][cur_base_pair];
const NC_Score dg2 = max(NC_Score(0), A_ptr->I_query) - delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)];
// gap the target
prev_base_pair = best_base_pair(GAP, prev_query_base);
//const NC_Score dg3 = (NC_Score(0) < A_ptr->I_target) ? A_ptr->I_target - delta_g[prev_base_pair][cur_base_pair] :
// - delta_g[prev_base_pair][cur_base_pair];
const NC_Score dg3 = max(NC_Score(0), A_ptr->I_target) - delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)];
if(dg1 >= dg2){
if(dg1 >= dg3){
X_ptr->M = dg1;
X_ptr->M_trace = im1_jm1;
#ifdef ENUMERATE_PATH
// Allow for enumeration of multiple, equal scoring paths through
// the dynamic programing matrix
if(dg1 == dg2){
X_ptr->M_trace |= i_jm1;
}
if(dg1 == dg3){
X_ptr->M_trace |= im1_j;
}
#endif // ENUMERATE_PATH
}
else{ // dg1 < dg3
X_ptr->M = dg3;
X_ptr->M_trace = im1_j;
}
}
else{ // dg1 < dg2
if(dg2 >= dg3){
X_ptr->M = dg2;
X_ptr->M_trace = i_jm1;
#ifdef ENUMERATE_PATH
// Allow for enumeration of multiple, equal scoring paths through
// the dynamic programing matrix
if(dg2 == dg3){
X_ptr->M_trace |= im1_j;
}
#endif // ENUMERATE_PATH
}
else{ // dg2 < dg3
X_ptr->M = dg3;
X_ptr->M_trace = im1_j;
}
}
cur_base_pair = best_base_pair(target_base, GAP);
prev_base_pair = best_base_pair(prev_target_base, query_base);
//NC_Score insert_gap = (NC_Score(0) < C_ptr->M) ? C_ptr->M - delta_g[prev_base_pair][cur_base_pair] :
// - delta_g[prev_base_pair][cur_base_pair];
NC_Score insert_gap = max(NC_Score(0), C_ptr->M) - delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)];
prev_base_pair = best_base_pair(prev_target_base, GAP);
//NC_Score extend_gap = (NC_Score(0) < C_ptr->I_query) ? C_ptr->I_query - delta_g[prev_base_pair][cur_base_pair] :
// - delta_g[prev_base_pair][cur_base_pair];
NC_Score extend_gap = max(NC_Score(0), C_ptr->I_query) - delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)];
if(insert_gap >= extend_gap){
X_ptr->I_query = insert_gap;
X_ptr->I_query_trace = im1_jm1;
#ifdef ENUMERATE_PATH
// Allow for enumeration of multiple, equal scoring paths through
// the dynamic programing matrix
if(insert_gap == extend_gap){
X_ptr->I_query_trace |= i_jm1;
}
#endif // ENUMERATE_PATH
}
else{
X_ptr->I_query = extend_gap;
X_ptr->I_query_trace = i_jm1;
}
cur_base_pair = best_base_pair(GAP, query_base);
prev_base_pair = best_base_pair(target_base, prev_query_base);
//insert_gap = (NC_Score(0) < B_ptr->M) ? B_ptr->M - delta_g[prev_base_pair][cur_base_pair] :
// - delta_g[prev_base_pair][cur_base_pair];
insert_gap = max(NC_Score(0), B_ptr->M) - delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)];
prev_base_pair = best_base_pair(GAP, prev_query_base);
//extend_gap = (NC_Score(0) < B_ptr->I_target) ? B_ptr->I_target - delta_g[prev_base_pair][cur_base_pair] :
// - delta_g[prev_base_pair][cur_base_pair];
extend_gap = max(NC_Score(0), B_ptr->I_target) - delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)];
if(insert_gap >= extend_gap){
X_ptr->I_target = insert_gap;
X_ptr->I_target_trace = im1_jm1;
#ifdef ENUMERATE_PATH
// Allow for enumeration of multiple, equal scoring paths through
// the dynamic programing matrix
if(insert_gap == extend_gap){
X_ptr->I_target_trace |= im1_j;
}
#endif // ENUMERATE_PATH
}
else{
X_ptr->I_target = extend_gap;
X_ptr->I_target_trace = im1_j;
}
// Since gaps can only decrease the score, only test the match state for new,
// high scoring cells
if(X_ptr->M >= max_score){
#ifdef ENUMERATE_PATH
if(X_ptr->M > max_score){
max_score = X_ptr->M;
max_ptr.clear();
max_ptr.push_back(X_ptr);
}
else{
if(X_ptr->M == max_score){
max_ptr.push_back(X_ptr);
}
}
#else
max_score = X_ptr->M;
max_ptr.clear();
max_ptr.push_back(X_ptr);
#endif // ENUMERATE_PATH
}
}
}
return max_score;
}
// Fast, diagonal only alignment
// 1) Both the query and target sequences are assumed to be in 5'-3' orientation.
// 2) ** No gaps are allowed ** (The only states are mismatch or perfect match)
NC_Score NucCruc::align_dimer_diagonal(const CircleBuffer<nucleic_acid, MAX_SEQUENCE_LENGTH> &m_q,
const CircleBuffer<nucleic_acid, MAX_SEQUENCE_LENGTH> &m_t)
{
// Reset the largest element pointer buffer
max_ptr.clear();
const unsigned int query_len = m_q.size();
const unsigned int target_len = m_t.size();
// We are only aligning along the diagonal. Use the minimum(query, target) length
const unsigned int len = min(query_len, target_len);
// Copy the target sequence into the target buffer
for(unsigned int i = 0;i < target_len;i++){
target_buffer[i] = m_t[i];
}
NC_Score max_score = -1;
NC_Elem *A_ptr = dp_matrix;
NC_Elem *X_ptr = dp_matrix + (MAX_SEQUENCE_LENGTH + 2);
int cur_base_pair;
int prev_base_pair = best_base_pair(GAP, GAP);
for(unsigned int i = 1;i <= len;i++,
A_ptr += (MAX_SEQUENCE_LENGTH + 2), X_ptr += (MAX_SEQUENCE_LENGTH + 2),
prev_base_pair = cur_base_pair){
// A B
// C X <-- dp[i][j]
cur_base_pair = best_base_pair(target_buffer[i - 1], m_q[query_len - i]);
// Match or mismatch
X_ptr->M = (NC_Score(0) < A_ptr->M) ? A_ptr->M - delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)] :
- delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)];
X_ptr->M_trace = im1_jm1;
// Since gaps can only decrease the score, only test the match state for new,
// high scoring cells
if(X_ptr->M >= max_score){
#ifdef ENUMERATE_PATH
if(X_ptr->M > max_score){
max_score = X_ptr->M;
max_ptr.clear();
max_ptr.push_back(X_ptr);
}
else{
if(X_ptr->M == max_score){
max_ptr.push_back(X_ptr);
}
}
#else
max_score = X_ptr->M;
max_ptr.clear();
max_ptr.push_back(X_ptr);
#endif // ENUMERATE_PATH
}
}
return max_score;
}
// Compute the Smith-Waterman local alignment between a query sequence and itself to
// check for hairpin secondary structure
NC_Score NucCruc::align_hairpin(const CircleBuffer<nucleic_acid, MAX_SEQUENCE_LENGTH> &m_q)
{
// We are performing a hairpin alignment (this info is needed to print the alignment
// correctly).
tm_mode = HAIRPIN;
// Reset the largest element pointer buffer
max_ptr.clear();
// The smallest sterically allowed hairpin loop + a 1 base anchor
const unsigned int steric_limit = 3u + 1u;
const unsigned int query_len = m_q.size();
if(query_len == 0){
throw ":NucCruc::align_hairpin: Empty query sequence";
}
int max_stem_len = (int)query_len - (int)steric_limit;
// If max_stem_len <= 0, no alignment is performed!
NC_Score max_score = -1;
for(int i = 1;i <= max_stem_len;i++){
// Reverse the query sequence
const nucleic_acid query_base = m_q[query_len - i];
const nucleic_acid prev_query_base = (i == 1) ? GAP : m_q[query_len - (i - 1)];
const int upper_j = max_stem_len - (i - 1);
// A B
// C X <-- dp[i][j]
NC_Elem *C_ptr = dp_matrix + i*(MAX_SEQUENCE_LENGTH + 1);
NC_Elem *X_ptr = C_ptr + 1;
NC_Elem *A_ptr = C_ptr - (MAX_SEQUENCE_LENGTH + 1);
NC_Elem *B_ptr = A_ptr + 1;
for(int j = 0;j < upper_j;j++,
A_ptr++, B_ptr++, C_ptr++, X_ptr++){
const nucleic_acid target_base = m_q[j];
const nucleic_acid prev_target_base = ( (j == 0) ? GAP : m_q[j - 1] );
int cur_base_pair = best_base_pair(target_base, query_base);
// Match or mismatch
int prev_base_pair = best_base_pair(prev_target_base, prev_query_base);
const NC_Score dg1 = (NC_Score(0) < A_ptr->M) ? A_ptr->M - delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)] :
- delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)];
// gap the query
prev_base_pair = best_base_pair(prev_target_base, GAP);
const NC_Score dg2 = (NC_Score(0) < A_ptr->I_query) ? A_ptr->I_query - delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)] :
- delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)];
// gap the target
prev_base_pair = best_base_pair(GAP, prev_query_base);
const NC_Score dg3 = (NC_Score(0) < A_ptr->I_target) ? A_ptr->I_target - delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)] :
- delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)];
if(dg1 >= dg2){
if(dg1 >= dg3){
X_ptr->M = dg1;
X_ptr->M_trace = im1_jm1;
#ifdef ENUMERATE_PATH
// Allow for enumeration of multiple, equal scoring paths through
// the dynamic programing matrix
if(dg1 == dg2){
X_ptr->M_trace |= i_jm1;
}
if(dg1 == dg3){
X_ptr->M_trace |= im1_j;
}
#endif // ENUMERATE_PATH
}
else{ // dg1 < dg3
X_ptr->M = dg3;
X_ptr->M_trace = im1_j;
}
}
else{ // dg1 < dg2
if(dg2 >= dg3){
X_ptr->M = dg2;
X_ptr->M_trace = i_jm1;
#ifdef ENUMERATE_PATH
// Allow for enumeration of multiple, equal scoring paths through
// the dynamic programing matrix
if(dg2 == dg3){
X_ptr->M_trace |= im1_j;
}
#endif // ENUMERATE_PATH
}
else{ // dg2 < dg3
X_ptr->M = dg3;
X_ptr->M_trace = im1_j;
}
}
cur_base_pair = best_base_pair(target_base, GAP);
prev_base_pair = best_base_pair(prev_target_base, query_base);
NC_Score insert_gap = (NC_Score(0) < C_ptr->M) ? C_ptr->M - delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)] :
- delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)];
prev_base_pair = best_base_pair(prev_target_base, GAP);
NC_Score extend_gap = (NC_Score(0) < C_ptr->I_query) ? C_ptr->I_query - delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)] :
- delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)];
if(insert_gap >= extend_gap){
X_ptr->I_query = insert_gap;
X_ptr->I_query_trace = im1_jm1;
#ifdef ENUMERATE_PATH
// Allow for enumeration of multiple, equal scoring paths through
// the dynamic programing matrix
if(insert_gap == extend_gap){
X_ptr->I_query_trace |= i_jm1;
}
#endif // ENUMERATE_PATH
}
else{
X_ptr->I_query = extend_gap;
X_ptr->I_query_trace = i_jm1;
}
cur_base_pair = best_base_pair(GAP, query_base);
prev_base_pair = best_base_pair(target_base, prev_query_base);
insert_gap = (NC_Score(0) < B_ptr->M) ? B_ptr->M - delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)] :
- delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)];
prev_base_pair = best_base_pair(GAP, prev_query_base);
extend_gap = (NC_Score(0) < B_ptr->I_target) ? B_ptr->I_target - delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)] :
- delta_g[SCORE_INDEX(prev_base_pair, cur_base_pair)];
if(insert_gap >= extend_gap){
X_ptr->I_target = insert_gap;
X_ptr->I_target_trace = im1_jm1;
#ifdef ENUMERATE_PATH
// Allow for enumeration of multiple, equal scoring paths through
// the dynamic programing matrix
if(insert_gap == extend_gap){
X_ptr->I_target_trace |= im1_j;
}
#endif // ENUMERATE_PATH
}
else{
X_ptr->I_target = extend_gap;
X_ptr->I_target_trace = im1_j;
}
// Since gaps can only decrease the score, only test the match state for new,
// high scoring cells
if(X_ptr->M >= max_score){
#ifdef ENUMERATE_PATH
if(X_ptr->M > max_score){
max_score = X_ptr->M;
max_ptr.clear();
max_ptr.push_back(X_ptr);
}
else{
if(X_ptr->M == max_score){
max_ptr.push_back(X_ptr);
}
}
#else
max_score = X_ptr->M;
max_ptr.clear();
max_ptr.push_back(X_ptr);
#endif // ENUMERATE_PATH
}
}
}
return max_score;
}
void NucCruc::enumerate_dimer_alignments(NC_Elem *m_dp_matrix, NC_Elem *m_max_ptr,
alignment &m_best_align,
const CircleBuffer<nucleic_acid, MAX_SEQUENCE_LENGTH> &m_q,
const CircleBuffer<nucleic_acid, MAX_SEQUENCE_LENGTH> &m_t,
const mode &m_mode)
{
bool first_time = true;
deque<trace_branch> trace_stack;
int zero_count = -1;
// Keep track of the number of alternate paths through the DP matrix
// that we have explored
unsigned int trace_count = 0;
float best_dg = m_best_align.dH - target_T*m_best_align.dS;;
const int query_len = int( m_q.size() );
const int target_len = int( m_t.size() );
while(true){
// Test for zero_count <= 0 to catch
// 1) single paths with any number of zeros (zero_count == 0 at the end)
// 2) multiple paths that will have zero_count == -1 after the last path
// has been completed
if( !first_time && trace_stack.empty() && (zero_count <= 0) ){
break;
}