-
Notifications
You must be signed in to change notification settings - Fork 0
/
na.c
1615 lines (1534 loc) · 60.6 KB
/
na.c
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 "ctools/random.h"
#include "na.h"
#include "nn_param.h"
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#define DEBUG printf
const uint NA_NBASES = 4;
const char NA_BASE_NAMES[] = { 'A', 'C', 'G', 'U' };
const uint NA_NBASEPAIRS = 6;
const char NA_BASEPAIR_NAMES[][3] = { "CG", "GC", "GU", "UG", "AU", "UA" };
/* TODO: build these arrays at runtime from the above arrays */
const uint NA_2BASES_TO_PAIRTYPE[4][4] = {
/* A C G U */
/* A */ { NA_UNDEF, NA_UNDEF, NA_UNDEF, 4, },
/* C */ { NA_UNDEF, NA_UNDEF, 0, NA_UNDEF, },
/* G */ { NA_UNDEF, 1, NA_UNDEF, 2, },
/* U */ { 5, NA_UNDEF, 3, NA_UNDEF, },
};
const uint NA_BASEPAIR_TO_BASES[][2] = {
{1, 2}, /* CG */
{2, 1}, /* GC */
{2, 3}, /* GU */
{3, 2}, /* UG */
{0, 3}, /* AU */
{3, 0}, /* UA */
};
/* TODO: these two arrays should be spit out by vienna_parse_param.py */
/* TODO: the next extra define is only necessary because the name
NN_G_NON_GC_PENALTY is too long */
#define NA_NONGC NN_G_NON_GC_PENALTY
const int NN_NON_GC_PENALTY_FOR_BP[] = {
0, 0, NA_NONGC, NA_NONGC, NA_NONGC, NA_NONGC
};
const int NN_NON_GC_PENALTY_FOR_BASES[][4] = {
/* A C G U */
/* A */ { 0, 0, 0, NA_NONGC, },
/* C */ { 0, 0, 0, 0, },
/* G */ { 0, 0, 0, NA_NONGC, },
/* U */ { NA_NONGC, 0, NA_NONGC, 0, },
};
/*
representations for nucleic acid secondary structure
----------------------------------------------------
0 1 2 3 4 5 6 7 8
vienna string: ( ( ( . . . ) ) )
pairlist: 8 7 6 . . . 2 1 0 -- here the dot stands for NA_UNPAIRED
*/
/* Transform a structure given in vienna bracket notation to a
pairlist, exiting on failure. */
void
xvienna_to_pairs(uint n, const char *vienna, uint *pairs)
{
bool verbose = true;
int err = vienna_to_pairs(n, vienna, verbose, pairs);
if (err != EXIT_SUCCESS) {
exit(EXIT_FAILURE);
}
}
/* Transform a structure given in vienna bracket notation to a
pairlist. */
int
vienna_to_pairs(uint n, const char *vienna, bool verbose, uint *pairs)
{
uint i, j;
uint *stack, idx_stack = 0; /* idx_stack is the next free slot */
stack = xmalloc(n * sizeof(*stack));
for (i = 0; i < n; i++) {
switch (vienna[i]) {
case '(':
stack[idx_stack++] = i;
break;
case ')':
if (idx_stack > 0) {
j = stack[--idx_stack];
pairs[i] = j;
pairs[j] = i;
} else {
if (verbose) {
printf("ERROR: too many closing parentheses in vienna"
" string %s\n", vienna);
}
return EXIT_FAILURE;
}
break;
case '.':
pairs[i] = NA_UNPAIRED;
break;
default:
if (verbose) {
printf("ERROR: illegal character '%c' in vienna string '%s'\n",
vienna[i], vienna);
}
return EXIT_FAILURE;
break;
}
}
if (idx_stack != 0) {
if (verbose) {
printf("ERROR: not enough closing parentheses in vienna string %s\n",
vienna);
}
return EXIT_FAILURE;
}
free(stack);
return EXIT_SUCCESS;
}
/* Convert pairlist `pairs` of length `n` to a secondary structure
`vienna` in dot-bracket notation.
The pairlist is assumed to be valid and of a non-pseudoknotted
secondary structure, i.e. only one type of bracket pairs `()` is
needed to write the structrue.
Output parameter: the generated secondary structure is written to
`vienna`.
Return value: Same as `vienna`. */
char *
xpairs_to_vienna(uint n, const uint *pairs, char *vienna)
{
bool verbose = true;
int retcode = pairs_to_vienna(n, pairs, verbose, vienna);
if (retcode == EXIT_FAILURE) {
exit(EXIT_FAILURE);
}
return vienna;
}
/* Convert pairlist `pairs` of length `n` to a secondary structure
`vienna` in dot-bracket notation.
The pairlist is assumed to be valid and of a non-pseudoknotted
secondary structure, i.e. only one type of bracket pairs `()` is
needed to write the structrue. If `verbose` is set to `true`,
errors during conversion are printed to stdout.
Output parameter: the generated secondary structure is written to
`vienna`.
Return value: `EXIT_FAILURE` on failure and `EXIT_SUCCESS` on
success. */
int
pairs_to_vienna(uint n, const uint *pairs, bool verbose, char *vienna)
{
uint i;
vienna[n] = '\0';
for (i = 0; i < n; i++) {
if (pairs[i] == NA_UNPAIRED) {
vienna[i] = '.';
} else {
if (i != pairs[pairs[i]]) {
if (verbose) {
printf("ERROR: illegal pairlist entry, pairs[%u] == %u, pairs[%u] == %u\n",
i, pairs[i], pairs[i], pairs[pairs[i]]);
}
return EXIT_FAILURE;
}
if (i < pairs[i]) {
vienna[i] = '(';
} else if (i > pairs[i]) {
vienna[i] = ')';
} else {
/* i == pairs[i] */
if (verbose) {
printf("ERROR: illegal entry in pairlist, base %u paired to itself\n", i);
}
return EXIT_FAILURE;
}
}
}
return EXIT_SUCCESS;
}
/* translate an ASCII character to its internal representation */
static uint
xencode_char(char c)
{
uint j;
c = toupper(c);
for (j = 0; j < sizeof(NA_BASE_NAMES); j++)
if (NA_BASE_NAMES[j] == c)
break;
if (j >= sizeof(NA_BASE_NAMES)) {
printf("ERROR: illegal character %c in sequence\n", c);
exit(EXIT_FAILURE);
}
return j;
}
/* translate from a string representation of a sequence to its internal
representation */
void
xstr_to_useq(uint n, const char *str, uint *useq)
{
uint i;
for (i = 0; i < n; i++)
useq[i] = xencode_char(str[i]);
}
/* translate a continuous sequence representation into a string by
taking the most likely character at each position */
void
pseq_to_str(double **p, uint n, uint ndim, char *str)
{
uint i, j, j_max;
for (i = 0; i < n; i++) {
j_max = 0;
for (j = 1; j < ndim; j++)
if (p[i][j] > p[i][j_max])
j_max = j;
str[i] = NA_BASE_NAMES[j_max];
}
str[n] = '\0';
}
/* translate from a string representation of a sequence to a
probability representation */
void
xstr_to_pseq(uint n, uint ndim, const char *str, double **p)
{
uint i, j;
/* ASSERT: ndim == length of NA_BASE_NAMES */
for (i = 0; i < n; i++) {
for (j = 0; j < ndim; j++)
p[i][j] = 0;
p[i][xencode_char(str[i])] = 1;
}
}
/* translate from uint sequence representation to string, exiting on
errors */
void
xuseq_to_str(uint n, const uint *useq, char *str)
{
bool verbose = true;
int retcode = useq_to_str(n, useq, verbose, str);
if (retcode != EXIT_SUCCESS) {
exit(EXIT_FAILURE);
}
}
/* translate from uint sequence representation to string, return
EXIT_SUCCESS on success */
int
useq_to_str(uint n, const uint *useq, bool verbose, char *str)
{
uint i;
str[n] = '\0';
for (i = 0; i < n; i++) {
if (useq[i] >= NA_NBASES) {
if (verbose) {
printf("ERROR: illegal base at position %u, base id = %u\n", i, useq[i]);
}
return EXIT_FAILURE;
}
str[i] = NA_BASE_NAMES[useq[i]];
}
return EXIT_SUCCESS;
}
void
nn_multiloop_xalloc(struct nn_multiloop *ml, uint nstems, uint ndangle5,
uint ndangle3)
{
ml->nstems = nstems;
ml->stems = xmalloc(nstems * sizeof(*ml->stems));
ml->ndangle5 = ndangle5;
ml->dangle5 = xmalloc(ndangle5 * sizeof(*ml->dangle5));
ml->ndangle3 = ndangle3;
ml->dangle3 = xmalloc(ndangle3 * sizeof(*ml->dangle3));
}
void
nn_multiloop_free(struct nn_multiloop *ml)
{
free(ml->stems);
free(ml->dangle5);
free(ml->dangle3);
}
struct nn_inter *
nn_inter_xnew(uint n)
{
/* TODO: memory waste here, but works ok for now (always allocates
size n or n^2 structures) */
uint i;
struct nn_inter *inter;
inter = xmalloc(sizeof(*inter));
inter->n = n;
inter->pairs = xmalloc(n * sizeof(*inter->pairs));
for (i = 0; i < n; i++) {
inter->pairs[i] = NA_UNPAIRED;
}
inter->nstack = 0;
inter->stack = xmalloc(n * sizeof(*inter->stack));
inter->nhairpin = 0;
inter->hairpin = xmalloc(n * sizeof(*inter->hairpin));
inter->nbulge = 0;
inter->bulge = xmalloc(n * sizeof(*inter->bulge));
inter->nintloop = 0;
inter->intloop = xmalloc(n * sizeof(*inter->intloop));
inter->nmultiloop = 0;
inter->multiloop = xmalloc(n * sizeof(*inter->multiloop));
/* TODO: temporary hack until nn_multiloop_xfind is fixed */
inter->extloop.stems = xmalloc(n * sizeof(*inter->extloop.stems));
inter->extloop.dangle5 = xmalloc(n * sizeof(*inter->extloop.dangle5));
inter->extloop.dangle3 = xmalloc(n * sizeof(*inter->extloop.dangle3));
inter->extloop.unpaired = 0;
inter->extloop.nstems = 0;
inter->extloop.ndangle5 = 0;
inter->extloop.ndangle3 = 0;
/* TODO: temporary hack until nn_multiloop_xfind is fixed */
for (i = 0; i < n; i++) {
inter->multiloop[i].stems =
xmalloc(n * sizeof(*inter->multiloop[i].stems));
inter->multiloop[i].dangle5 =
xmalloc(n * sizeof(*inter->multiloop[i].dangle5));
inter->multiloop[i].dangle3 =
xmalloc(n * sizeof(*inter->multiloop[i].dangle3));
}
return inter;
}
void
nn_inter_delete(struct nn_inter *inter)
{
uint i;
free(inter->pairs);
free(inter->stack);
free(inter->hairpin);
free(inter->bulge);
free(inter->intloop);
nn_multiloop_free(&inter->extloop);
/* TODO: currently assume that n multiloops were allocated */
for (i = 0; i < inter->n; i++)
nn_multiloop_free(&inter->multiloop[i]);
free(inter->multiloop);
free(inter);
}
/* TODO: this routine assumes that the multiloop has been fully allocated
and the arrays contained inside have enough room. this clearly
is not very good -- should first collect the data, then allocate
sufficient space and move the data over. the name should also be
changed to something_alloc to reflect this */
/* TODO: simplify this (storing of the outer base pair manually, but
what about extloops ?) */
/* run through the multiloop enclosed by the bases (i, j) and store
information on the location of stems, 5' and 3' dangles and the
number of unpaired bases.
Note: the enclosing base pair is *not* stored, this needs to be
done manually afterwards */
void
nn_multiloop_xfind(struct nn_multiloop *ml, uint n, uint *pairs, uint i, uint j)
{
ml->unpaired = ml->nstems = ml->ndangle5 = ml->ndangle3 = 0;
while (i <= j) {
if (pairs[i] == NA_UNPAIRED) {
ml->unpaired++;
} else if (i < pairs[i]) {
/* add stem */
ml->stems[ml->nstems][0] = i;
ml->stems[ml->nstems][1] = pairs[i];
ml->nstems++;
if (i > 0) {
/* add dangle5 */
ml->dangle5[ml->ndangle5][0] = i;
ml->dangle5[ml->ndangle5][1] = pairs[i];
ml->dangle5[ml->ndangle5][2] = i - 1;
ml->ndangle5++;
}
if (pairs[i] < n - 1) {
/* add dangle3 */
ml->dangle3[ml->ndangle3][0] = i;
ml->dangle3[ml->ndangle3][1] = pairs[i];
ml->dangle3[ml->ndangle3][2] = pairs[i] + 1;
ml->ndangle3++;
}
i = pairs[i];
} else {
printf("ERROR: nn_inter_set_multiloop: this should not be\n");
exit(EXIT_FAILURE);
}
i += 1;
}
}
/* find interactions for a pairlist given as part of a
struct nn_inter */
void
find_interactions(struct nn_inter *inter)
{
uint i, p, q, size1, size2, i1, j1, i2, j2;
uint n = inter->n, *pairs = inter->pairs;
struct nn_multiloop *ml;
/* exterior loop */
nn_multiloop_xfind(&inter->extloop, inter->n, inter->pairs,
0, inter->n - 1);
/* hairpins, interior loops (including stacking basepairs) and multiloops */
i = 0;
while (i < n) {
/* continue until we have found an opening basepairing
i.e. we're at i of basepair (i, pairs[i]) */
if (pairs[i] == NA_UNPAIRED || i > pairs[i]) {
i++;
continue;
}
/* Now search inwards from basepair (i, pairs[i]) for the next
two base pairs.
Remember that we are dealing with balanced brackets, where
the number number of opening and closing brackets must be
balanced for the whole sequence and any subsequence
enclosed by brackets.
The possible cases therefore are:
- hairpin: p and q run past each other, there are no more
base pairs inside
- intloop: we have found the base pair (p,q),
i.e. pairs[p] == q
- multiloop: we have found base pairs (p,pairs[p]) and
(pairs[q],q) with pairs[p] != q
*/
p = i + 1;
q = pairs[i] - 1;
while (pairs[p] == NA_UNPAIRED && p < pairs[i])
p++;
while (pairs[q] == NA_UNPAIRED && q > i)
q--;
/* now we can tell what loop type we have */
if (q < p) {
/* hairpin loop - pairs have run past each other */
struct nn_hairpin hp = { .i = i, .j = pairs[i],
.size = pairs[i] - i - 1 };
inter->hairpin[inter->nhairpin++] = hp;
} else {
/* p and q have found a basepair inside. we now must
discern if they belong to the same or separate pairs;
this will tell us if we are dealing with an internal
loop or a multiloop */
if (pairs[p] == q) {
/* stacking pair, bulge loop or internal loop - pairs are
the same */
size1 = p - i - 1;
size2 = pairs[i] - q - 1;
i1 = i;
j1 = pairs[i];
i2 = p;
j2 = pairs[p];
if (size1 == 0 && size2 == 0) {
/* stacking pair of basepairs */
struct nn_stack st = { .i1 = i1, .j1 = j1,
.i2 = i2, .j2 = j2 };
inter->stack[inter->nstack++] = st;
} else if (size1 == 0 || size2 == 0) {
/* bulge loop */
struct nn_bulge bg = { .i1 = i1, .j1 = j1,
.i2 = i2, .j2 = j2,
.size = MAX(size1, size2) };
inter->bulge[inter->nbulge++] = bg;
} else {
/* generic internal loop */
struct nn_intloop in = { .i1 = i1, .j1 = j1,
.i2 = i2, .j2 = j2,
.size1 = size1, .size2 = size2};
inter->intloop[inter->nintloop++] = in;
}
} else {
/* multiloop - pairs are different */
ml = &inter->multiloop[inter->nmultiloop];
nn_multiloop_xfind(ml, inter->n, inter->pairs, i + 1, pairs[i] - 1);
/* TODO: perhaps move this into nn_multiloop_xfind
assumes there is enough space in arrays */
/* add the basepair initiating the multiloop to the stems */
ml = &inter->multiloop[inter->nmultiloop];
ml->stems[ml->nstems][0] = i;
ml->stems[ml->nstems][1] = pairs[i];
ml->nstems++;
/* Add extra dangles for outer basepair of multiloop.
Note the reversed order of i and pairs[i], this is
needed as the opening base pair of the multiloop is
really the other way around if we want to have the
dangling base on the inside of the loop */
ml->dangle5[ml->ndangle5][0] = pairs[i];
ml->dangle5[ml->ndangle5][1] = i;
ml->dangle5[ml->ndangle5][2] = pairs[i] - 1;
ml->ndangle5++;
ml->dangle3[ml->ndangle3][0] = pairs[i];
ml->dangle3[ml->ndangle3][1] = i;
ml->dangle3[ml->ndangle3][2] = i + 1;
ml->ndangle3++;
inter->nmultiloop++;
}
}
/* move to next paired base */
i = p;
}
}
void
print_multiloop(const struct nn_multiloop *ml, const char *indent)
{
uint i;
printf("%sunpaired: %u\n", indent, ml->unpaired);
printf("%sstems: ", indent);
for (i = 0; i < ml->nstems; i++)
printf(" (%u, %u) ", ml->stems[i][0], ml->stems[i][1]);
printf("\n");
printf("%sdangle5: ", indent);
for (i = 0; i < ml->ndangle5; i++)
printf(" ((%u, %u), %u) ", ml->dangle5[i][0], ml->dangle5[i][1],
ml->dangle5[i][2]);
printf("\n");
printf("%sdangle3: ", indent);
for (i = 0; i < ml->ndangle3; i++)
printf(" ((%u, %u), %u) ", ml->dangle3[i][0], ml->dangle3[i][1],
ml->dangle3[i][2]);
printf("\n");
}
void
print_interactions(const struct nn_inter *inter)
{
uint i;
printf("interactions\n"
"------------\n");
printf("exterior loop\n");
print_multiloop(&inter->extloop, " ");
printf("stacking basepairs\n");
for (i = 0; i < inter->nstack; i++)
printf(" (%u, %u) - (%u, %u)\n",
inter->stack[i].i1, inter->stack[i].j1,
inter->stack[i].i2, inter->stack[i].j2);
printf("bulge loops\n");
for (i = 0; i < inter->nbulge; i++)
printf(" (%u, %u) - (%u, %u), size = %u\n",
inter->bulge[i].i1, inter->bulge[i].j1,
inter->bulge[i].i2, inter->bulge[i].j2, inter->bulge[i].size);
printf("internal loops\n");
for (i = 0; i < inter->nintloop; i++)
printf(" (%u, %u) - (%u, %u), size1 = %u, size2 = %u\n",
inter->intloop[i].i1, inter->intloop[i].j1,
inter->intloop[i].i2, inter->intloop[i].j2,
inter->intloop[i].size1, inter->intloop[i].size2);
printf("hairpins\n");
for (i = 0; i < inter->nhairpin; i++)
printf(" (%u, %u), size = %u\n",
inter->hairpin[i].i, inter->hairpin[i].j,
inter->hairpin[i].size);
printf("multiloops\n");
for (i = 0; i < inter->nmultiloop; i++)
print_multiloop(&inter->multiloop[i], " ");
}
/* TODO: statistics are wrong, don't use when unbiased random
secondary structures are needed. Also the stuctures turn out
to be very sparse */
/* generate random structures with minimum size of hairpins of hpmin */
static void helper_random_pairs_iter(uint *pairs, uint i, uint j, uint hpmin);
void
random_pairs(uint n, uint *pairs, uint hpmin)
{
uint i;
if (n == 0) {
/* avoid uint underflow of `n - 1` used later on */
return;
}
for (i = 0; i < n; i++)
pairs[i] = NA_UNPAIRED;
helper_random_pairs_iter(pairs, 0, n - 1, hpmin);
}
static void
helper_random_pairs_iter(uint *pairs, uint i, uint j, uint hpmin)
{
uint p, q;
/* test `j < j - hpmin - 1` is true for uint underflow */
if (j < j - hpmin - 1 || i > j - hpmin - 1)
return;
p = random_uint(i, j - hpmin - 1);
q = random_uint(p + hpmin + 1, j);
if (p != i - 1) {
pairs[p] = q;
pairs[q] = p;
helper_random_pairs_iter(pairs, p + 1, q - 1, hpmin);
helper_random_pairs_iter(pairs, q + 1, j, hpmin);
}
}
/* generate a random sequence compatible with a given secondary
structure */
void
random_useq(uint n, const uint *pairs, uint *useq)
{
uint i, bp;
for (i = 0; i < n; i++) {
if (pairs[i] == NA_UNPAIRED) {
useq[i] = random_uint(0, NA_NBASES - 1);
} else if (i < pairs[i]) {
/* we found i of the pair (i, pairs[i]) */
bp = random_uint(0, NA_NBASEPAIRS - 1);
useq[i] = NA_BASEPAIR_TO_BASES[bp][0];
useq[pairs[i]] = NA_BASEPAIR_TO_BASES[bp][1];
}
}
}
/* random pure sequence composition compatible with a given set of
base pairs */
void
random_pseq(uint n, uint ndim, const uint *pairs, double **p)
{
uint i, j, bp;
for (i = 0; i < n; i++) {
if (pairs[i] == NA_UNPAIRED) {
for (j = 0; j < ndim; j++)
p[i][j] = 0;
p[i][random_uint(0, NA_NBASES - 1)] = 1;
} else if (i < pairs[i]) {
/* we found i of the pair (i, pairs[i]) */
for (j = 0; j < ndim; j++)
p[i][j] = 0;
for (j = 0; j < ndim; j++)
p[pairs[i]][j] = 0;
bp = random_uint(0, NA_NBASEPAIRS - 1);
p[i][NA_BASEPAIR_TO_BASES[bp][0]] = 1;
p[pairs[i]][NA_BASEPAIR_TO_BASES[bp][1]] = 1;
}
}
}
int
calc_interactions_useq(const struct nn_inter *inter, const uint *useq)
{
uint i;
int G = 0, Ge = 0, Gs = 0, Gb = 0, Gi = 0, Gh = 0, Gm = 0;
/* external loop */
Ge = G_extloop_multiloop(useq, inter->extloop.unpaired,
inter->extloop.nstems, inter->extloop.stems,
inter->extloop.ndangle5,
inter->extloop.dangle5,
inter->extloop.ndangle3,
inter->extloop.dangle3,
false);
/* stacking pair of basepairs */
for (i = 0; i < inter->nstack; i++)
Gs += G_stack(useq, inter->stack[i].i1, inter->stack[i].j1,
inter->stack[i].i2, inter->stack[i].j2);
/* bulge loops */
for (i = 0; i < inter->nbulge; i++)
Gb += G_bulgeloop(useq, inter->bulge[i].size,
inter->bulge[i].i1, inter->bulge[i].j1,
inter->bulge[i].i2, inter->bulge[i].j2);
/* internal loops */
for (i = 0; i < inter->nintloop; i++)
Gi += G_intloop(useq,
inter->intloop[i].size1, inter->intloop[i].size2,
inter->intloop[i].i1, inter->intloop[i].j1,
inter->intloop[i].i2, inter->intloop[i].j2);
/* hairpins */
for (i = 0; i < inter->nhairpin; i++)
Gh += G_hairpin(useq, inter->hairpin[i].size,
inter->hairpin[i].i, inter->hairpin[i].j);
/* multiloops */
for (i = 0; i < inter->nmultiloop; i++)
Gm += G_extloop_multiloop(useq, inter->multiloop[i].unpaired,
inter->multiloop[i].nstems,
inter->multiloop[i].stems,
inter->multiloop[i].ndangle5,
inter->multiloop[i].dangle5,
inter->multiloop[i].ndangle3,
inter->multiloop[i].dangle3,
true);
#if 0
printf("nintloop = %u\n", inter->nintloop);
printf("extloop = %d\n"
"stack = %d\n"
"bulge = %d\n"
"intloop = %d\n"
"hairpin = %d\n"
"multiloop = %d\n",
Ge, Gs, Gb, Gi, Gh, Gm);
#endif
G = Ge + Gs + Gb + Gi + Gh + Gm;
return G;
}
double
calc_interactions_pseq(const struct nn_inter *inter, double **p)
{
uint i;
double G = 0, Ge = 0, Gs = 0, Gb = 0, Gi = 0, Gh = 0, Gm = 0;
/* external loop */
Ge = G_extloop_multiloop_pseq(p, inter->extloop.unpaired,
inter->extloop.nstems, inter->extloop.stems,
inter->extloop.ndangle5,
inter->extloop.dangle5,
inter->extloop.ndangle3,
inter->extloop.dangle3,
false);
/* stacking pair of basepairs */
for (i = 0; i < inter->nstack; i++)
Gs += G_stack_pseq(p, inter->stack[i].i1, inter->stack[i].j1,
inter->stack[i].i2, inter->stack[i].j2);
/* bulge loops */
for (i = 0; i < inter->nbulge; i++)
Gb += G_bulgeloop_pseq(p, inter->bulge[i].size,
inter->bulge[i].i1, inter->bulge[i].j1,
inter->bulge[i].i2, inter->bulge[i].j2);
/* internal loops */
for (i = 0; i < inter->nintloop; i++)
Gi += G_intloop_pseq(p,
inter->intloop[i].size1, inter->intloop[i].size2,
inter->intloop[i].i1, inter->intloop[i].j1,
inter->intloop[i].i2, inter->intloop[i].j2);
/* hairpins */
for (i = 0; i < inter->nhairpin; i++)
Gh += G_hairpin_pseq(p, inter->hairpin[i].size,
inter->hairpin[i].i, inter->hairpin[i].j);
/* multiloops */
for (i = 0; i < inter->nmultiloop; i++)
Gm += G_extloop_multiloop_pseq(p, inter->multiloop[i].unpaired,
inter->multiloop[i].nstems,
inter->multiloop[i].stems,
inter->multiloop[i].ndangle5,
inter->multiloop[i].dangle5,
inter->multiloop[i].ndangle3,
inter->multiloop[i].dangle3,
true);
#if 0
printf("nintloop = %u\n", inter->nintloop);
printf("extloop = %f\n"
"stack = %f\n"
"bulge = %f\n"
"intloop = %f\n"
"hairpin = %f\n"
"multiloop = %f\n",
Ge, Gs, Gb, Gi, Gh, Gm);
#endif
G = Ge + Gs + Gb + Gi + Gh + Gm;
return G;
}
/* TODO: assumes dGdp array has been initialised to 0 */
void
calc_interactions_dGdp_pseq(const struct nn_inter *inter, double **p,
double **dGdp)
{
uint i;
/* external loop */
dGdp_extloop_multiloop_pseq(p,
inter->extloop.nstems, inter->extloop.stems,
inter->extloop.ndangle5, inter->extloop.dangle5,
inter->extloop.ndangle3, inter->extloop.dangle3,
dGdp);
/* stacking pair of basepairs */
for (i = 0; i < inter->nstack; i++)
dGdp_stack_pseq(p, inter->stack[i].i1, inter->stack[i].j1,
inter->stack[i].i2, inter->stack[i].j2, dGdp);
/* bulge loops */
for (i = 0; i < inter->nbulge; i++)
dGdp_bulgeloop_pseq(p, inter->bulge[i].size,
inter->bulge[i].i1, inter->bulge[i].j1,
inter->bulge[i].i2, inter->bulge[i].j2, dGdp);
/* internal loops */
for (i = 0; i < inter->nintloop; i++)
dGdp_intloop_pseq(p,
inter->intloop[i].size1, inter->intloop[i].size2,
inter->intloop[i].i1, inter->intloop[i].j1,
inter->intloop[i].i2, inter->intloop[i].j2, dGdp);
/* hairpins */
for (i = 0; i < inter->nhairpin; i++)
dGdp_hairpin_pseq(p, inter->hairpin[i].size,
inter->hairpin[i].i, inter->hairpin[i].j, dGdp);
/* multiloops */
for (i = 0; i < inter->nmultiloop; i++)
dGdp_extloop_multiloop_pseq(p,
inter->multiloop[i].nstems,
inter->multiloop[i].stems,
inter->multiloop[i].ndangle5,
inter->multiloop[i].dangle5,
inter->multiloop[i].ndangle3,
inter->multiloop[i].dangle3,
dGdp);
}
/*
functions for the free energy contributions of different loop types
*/
/* helper functions */
uint
idx_to_pairtype(const uint *seq, uint i, uint j)
{
return NA_2BASES_TO_PAIRTYPE[seq[i]][seq[j]]; /* TODO: array hardcoded */
}
int
G_non_gc_penalty(uint pairtype)
{
return NN_NON_GC_PENALTY_FOR_BP[pairtype];
}
double
G_non_gc_penalty_pseq(double **p, uint i1, uint j1)
{
uint i, j;
double G = 0;
for (i = 0; i < NA_NBASES; i++)
for (j = 0; j < NA_NBASES; j++)
G += p[i1][i] * p[j1][j] * NN_NON_GC_PENALTY_FOR_BASES[i][j];
return G;
}
void
dGdp_non_gc_penalty_pseq(double **p, uint i1, uint j1, double **dGdp)
{
uint i, j;
for (i = 0; i < NA_NBASES; i++) {
for (j = 0; j < NA_NBASES; j++) {
dGdp[i1][i] += p[j1][j] * NN_NON_GC_PENALTY_FOR_BASES[i][j];
dGdp[j1][j] += p[i1][i] * NN_NON_GC_PENALTY_FOR_BASES[i][j];
}
}
}
int
G_dangle5(const uint *seq, uint i1, uint j1, uint b)
{
uint pair_type = idx_to_pairtype(seq, i1, j1);
uint base_type = seq[b];
return nn_G_dangle5[pair_type][base_type];
}
double
G_dangle5_pseq(double **p, uint i1, uint j1, uint b)
{
uint i, j, bt_i1, bt_j1;
double G = 0;
for (i = 0; i < NA_NBASEPAIRS; i++) {
for (j = 0; j < NA_NBASES; j++) {
bt_i1 = NA_BASEPAIR_TO_BASES[i][0];
bt_j1 = NA_BASEPAIR_TO_BASES[i][1];
G += p[i1][bt_i1] * p[j1][bt_j1] * p[b][j] * nn_G_dangle5[i][j];
}
}
return G;
}
void
dGdp_dangle5_pseq(double **p, uint i1, uint j1, uint b, double **dGdp)
{
uint i, j, bt_i1, bt_j1;
for (i = 0; i < NA_NBASEPAIRS; i++) {
for (j = 0; j < NA_NBASES; j++) {
bt_i1 = NA_BASEPAIR_TO_BASES[i][0];
bt_j1 = NA_BASEPAIR_TO_BASES[i][1];
dGdp[i1][bt_i1] += p[j1][bt_j1] * p[b][j] * nn_G_dangle5[i][j];
dGdp[j1][bt_j1] += p[i1][bt_i1] * p[b][j] * nn_G_dangle5[i][j];
dGdp[b][j] += p[i1][bt_i1] * p[j1][bt_j1] * nn_G_dangle5[i][j];
}
}
}
int
G_dangle3(const uint *seq, uint i1, uint j1, uint b)
{
uint pair_type = idx_to_pairtype(seq, i1, j1);
uint base_type = seq[b];
return nn_G_dangle3[pair_type][base_type];
}
double
G_dangle3_pseq(double **p, uint i1, uint j1, uint b)
{
uint i, j, bt_i1, bt_j1;
double G = 0;
for (i = 0; i < NA_NBASEPAIRS; i++) {
for (j = 0; j < NA_NBASES; j++) {
bt_i1 = NA_BASEPAIR_TO_BASES[i][0];
bt_j1 = NA_BASEPAIR_TO_BASES[i][1];
G += p[i1][bt_i1] * p[j1][bt_j1] * p[b][j] * nn_G_dangle3[i][j];
}
}
return G;
}
void
dGdp_dangle3_pseq(double **p, uint i1, uint j1, uint b, double **dGdp)
{
uint i, j, bt_i1, bt_j1;
for (i = 0; i < NA_NBASEPAIRS; i++) {
for (j = 0; j < NA_NBASES; j++) {
bt_i1 = NA_BASEPAIR_TO_BASES[i][0];
bt_j1 = NA_BASEPAIR_TO_BASES[i][1];
dGdp[i1][bt_i1] += p[j1][bt_j1] * p[b][j] * nn_G_dangle3[i][j];
dGdp[j1][bt_j1] += p[i1][bt_i1] * p[b][j] * nn_G_dangle3[i][j];
dGdp[b][j] += p[i1][bt_i1] * p[j1][bt_j1] * nn_G_dangle3[i][j];
}
}
}
/* loop free energy calculations */
int
G_stack(const uint *seq, uint i1, uint j1, uint i2, uint j2)
{
uint pt1, pt2;
pt1 = idx_to_pairtype(seq, i1, j1);
pt2 = idx_to_pairtype(seq, j2, i2); /* note order of i2,j2 swapped */
return nn_G_stack[pt1][pt2];
}
double
G_stack_pseq(double **p, uint i1, uint j1, uint i2, uint j2)
{
uint i, j, bt_i1, bt_j1, bt_i2, bt_j2;
double G = 0;
for (i = 0; i < NA_NBASEPAIRS; i++) {
for (j = 0; j < NA_NBASEPAIRS; j++) {
bt_i1 = NA_BASEPAIR_TO_BASES[i][0];
bt_j1 = NA_BASEPAIR_TO_BASES[i][1];
bt_i2 = NA_BASEPAIR_TO_BASES[j][1];/* note order of i2,j2 swapped */
bt_j2 = NA_BASEPAIR_TO_BASES[j][0];
G += p[i1][bt_i1] * p[j1][bt_j1]
* p[i2][bt_i2] * p[j2][bt_j2]
* nn_G_stack[i][j];
}
}
return G;
}
void
dGdp_stack_pseq(double **p, uint i1, uint j1, uint i2, uint j2,
double **dGdp)
{
uint i, j, bt_i1, bt_j1, bt_i2, bt_j2;
for (i = 0; i < NA_NBASEPAIRS; i++) {
for (j = 0; j < NA_NBASEPAIRS; j++) {
bt_i1 = NA_BASEPAIR_TO_BASES[i][0];
bt_j1 = NA_BASEPAIR_TO_BASES[i][1];
bt_i2 = NA_BASEPAIR_TO_BASES[j][1];/* note order of i2,j2 swapped */
bt_j2 = NA_BASEPAIR_TO_BASES[j][0];
dGdp[i1][bt_i1] += p[j1][bt_j1] * p[i2][bt_i2] * p[j2][bt_j2]
* nn_G_stack[i][j];
dGdp[j1][bt_j1] += p[i1][bt_i1] * p[i2][bt_i2] * p[j2][bt_j2]
* nn_G_stack[i][j];
dGdp[i2][bt_i2] += p[i1][bt_i1] * p[j1][bt_j1] * p[j2][bt_j2]
* nn_G_stack[i][j];
dGdp[j2][bt_j2] += p[i1][bt_i1] * p[j1][bt_j1] * p[i2][bt_i2]
* nn_G_stack[i][j];
}
}
}
int
G_bulgeloop(const uint *seq, uint size, uint i1, uint j1, uint i2, uint j2)
{
uint pt1, pt2;
int G = 0;
pt1 = idx_to_pairtype(seq, i1, j1);
pt2 = idx_to_pairtype(seq, j2, i2); /* note order of i2,j2 swapped */
if (size < NN_N_BULGELOOP) {
G += nn_G_bulge_loop[size];
} else {
G += nn_G_bulge_loop[NN_N_BULGELOOP - 1]
+ (int) (NN_LXC37 * log((double) size / (NN_N_BULGELOOP - 1)));
}
if (size == 1) {
G += G_stack(seq, i1, j1, i2, j2);
} else {
/* bulge loops larger than 1 get penalty term for non-gc closing
basepairs */
G += G_non_gc_penalty(pt1);
G += G_non_gc_penalty(pt2);
}
return G;