-
Notifications
You must be signed in to change notification settings - Fork 4
/
thal.c
executable file
·2829 lines (2600 loc) · 87.6 KB
/
thal.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
/*
Copyright (c) 1996,1997,1998,1999,2000,2001,2004,2006,2007,2009,2010,
2011,2012
Whitehead Institute for Biomedical Research, Steve Rozen
(http://purl.com/STEVEROZEN/), and Helen Skaletsky
All rights reserved.
This file is part of primer3 software suite.
This software suite is 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 2 of the License, or (at
your option) any later version.
This software 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 this software (file gpl-2.0.txt in the source
distribution); if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON A THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <limits.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include <ctype.h>
#include <math.h>
#include <unistd.h>
#if defined(__sun)
#include <ieeefp.h>
#endif
#include "thal.h"
/*#define DEBUG*/
#ifndef MIN_HRPN_LOOP
#define MIN_HRPN_LOOP 3 /* minimum size of hairpin loop */
#endif
#ifndef THAL_EXIT_ON_ERROR
#define THAL_EXIT_ON_ERROR 0
#endif
/* table where bp-s enthalpies, that retrieve to the most stable Tm, are saved */
#ifdef EnthalpyDPT
# undef EnthalpyDPT
#endif
#define EnthalpyDPT(i, j) enthalpyDPT[(j) + ((i-1)*len3) - (1)]
/* table where bp-s entropies, that retrieve to the most stable Tm, are saved */
#ifdef EntropyDPT
# undef EntropyDPT
#endif
#define EntropyDPT(i, j) entropyDPT[(j) + ((i-1)*len3) - (1)]
/* entropies of most stable hairpin terminal bp */
#ifndef SEND5
# define SEND5(i) send5[i]
#endif
/* enthalpies of most stable hairpin terminal bp */
#ifndef HEND5
# define HEND5(i) hend5[i]
#endif
#define CHECK_ERROR(COND,MSG) if (COND) { strcpy(o->msg, MSG); errno = 0; longjmp(_jmp_buf, 1); }
#define THAL_OOM_ERROR { strcpy(o->msg, "Out of memory"); errno = ENOMEM; longjmp(_jmp_buf, 1); }
#define THAL_IO_ERROR(f) { sprintf(o->msg, "Unable to open file %s", f); longjmp(_jmp_buf, 1); }
#define bpIndx(a, b) BPI[a][b] /* for traceing matrix BPI */
#define atPenaltyS(a, b) atpS[a][b]
#define atPenaltyH(a, b) atpH[a][b]
#define STR(X) #X
#define LONG_SEQ_ERR_STR(MAX_LEN) "Target sequence length > maximum allowed (" STR(MAX_LEN) ") in thermodynamic alignment"
#define XSTR(X) STR(X)
#define SMALL_NON_ZERO 0.000001
#define DBL_EQ(X,Y) (((X) - (Y)) < (SMALL_NON_ZERO) ? (1) : (2)) /* 1 when numbers are equal */
#ifdef INTEGER
# define isFinite(x) (x < _INFINITY / 2)
#else
# define isFinite(x) finite(x)
#endif
#define isPositive(x) ((x) > 0 ? (1) : (0))
/*** BEGIN CONSTANTS ***/
# ifdef INTEGER
const double _INFINITY = 999999.0;
# else
# ifdef INFINITY
const double _INFINITY = INFINITY;
# else
const double _INFINITY = 1.0 / 0.0;
# endif
# endif
static const double R = 1.9872; /* cal/Kmol */
static const double ILAS = (-300 / 310.15); /* Internal Loop Entropy ASymmetry correction -0.3kcal/mol*/
static const double ILAH = 0.0; /* Internal Loop EntHalpy Asymmetry correction */
static const double AT_H = 2200.0; /* AT penalty */
static const double AT_S = 6.9; /* AT penalty */
static const double MinEntropyCutoff = -2500.0; /* to filter out non-existing entropies */
static const double MinEntropy = -3224.0; /* initiation */
static const double G2 = 0.0; /* structures w higher G are considered to be unstabile */
const double ABSOLUTE_ZERO = 273.15;
const int MAX_LOOP = 30; /* the maximum size of loop that can be calculated; for larger loops formula must be implemented */
const int MIN_LOOP = 0;
static const char BASES[5] = {'A', 'C', 'G', 'T', 'N'}; /* bases to be considered - N is every symbol that is not A, G, C,$
*/
static const char BASE_PAIRS[4][4] = {"A-T", "C-G", "G-C", "T-A" }; /* allowed basepairs */
/* matrix for allowed; bp 0 - no bp, watson crick bp - 1 */
static const int BPI[5][5] = {
{0, 0, 0, 1, 0}, /* A, C, G, T, N; */
{0, 0, 1, 0, 0},
{0, 1, 0, 0, 0},
{1, 0, 0, 0, 0},
{0, 0, 0, 0, 0}};
/*** END OF CONSTANTS ***/
/*** BEGIN STRUCTs ***/
struct triloop {
char loop[5];
double value; };
struct tetraloop {
char loop[6];
double value; };
struct tracer /* structure for tracebacku - unimolecular str */ {
int i;
int j;
int mtrx; /* [0 1] EntropyDPT/EnthalpyDPT*/
struct tracer* next;
};
/*** END STRUCTs ***/
static int length_unsig_char(const unsigned char * str); /* returns length of unsigned char; to avoid warnings while compiling */
static unsigned char str2int(char c); /* converts DNA sequence to int; 0-A, 1-C, 2-G, 3-T, 4-whatever */
static double saltCorrectS (double mv, double dv, double dntp); /* part of calculating salt correction
for Tm by SantaLucia et al */
static FILE* openParamFile(const char* name, thal_results* o); /* file of thermodynamic params */
/* get thermodynamic tables */
static double readDouble(FILE *file, thal_results* o);
static void readLoop(FILE *file, double *v1, double *v2, double *v3, thal_results *o);
static int readTLoop(FILE *file, char *s, double *v, int triloop, thal_results *o);
static void getStack(double stackEntropies[5][5][5][5], double stackEnthalpies[5][5][5][5], thal_results* o);
/*static void verifyStackTable(double stack[5][5][5][5], char* type);*/ /* just for debugging; the method is turned off by default */
static void getStackint2(double stackEntropiesint2[5][5][5][5], double stackint2Enthalpies[5][5][5][5], thal_results* o);
static void getDangle(double dangleEntropies3[5][5][5], double dangleEnthalpies3[5][5][5], double dangleEntropies5[5][5][5],
double dangleEnthalpies5[5][5][5], thal_results* o);
static void getTstack(double tstackEntropies[5][5][5][5], double tstackEnthalpies[5][5][5][5], thal_results* o);
static void getTstack2(double tstack2Entropies[5][5][5][5], double tstack2Enthalpies[5][5][5][5], thal_results* o);
static void getTriloop(struct triloop**, struct triloop**, int* num, thal_results* o);
static void getTetraloop(struct tetraloop**, struct tetraloop**, int* num, thal_results* o);
static void getLoop(double hairpinLoopEnntropies[30], double interiorLoopEntropies[30], double bulgeLoopEntropiess[30],
double hairpinLoopEnthalpies[30], double interiorLoopEnthalpies[30], double bulgeLoopEnthalpies[30], thal_results* o);
static void tableStartATS(double atp_value, double atp[5][5]); /* creates table of entropy values for nucleotides
to which AT-penlty must be applied */
static void tableStartATH(double atp_value, double atp[5][5]);
static int comp3loop(const void*, const void*); /* checks if sequnece consists of specific triloop */
static int comp4loop(const void*, const void*); /* checks if sequnece consists of specific tetraloop */
static void initMatrix(); /* initiates thermodynamic parameter tables of entropy and enthalpy for dimer */
static void initMatrix2(); /* initiates thermodynamic parameter tables of entropy and enthalpy for monomer */
static void fillMatrix(int maxLoop, thal_results* o); /* calc-s thermod values into dynamic progr table (dimer) */
static void fillMatrix2(int maxLoop, thal_results* o); /* calc-s thermod values into dynamic progr table (monomer) */
static void maxTM(int i, int j); /* finds max Tm while filling the dyn progr table using stacking S and stacking H (dimer) */
static void maxTM2(int i, int j); /* finds max Tm while filling the dyn progr table using stacking S and stacking H (monomer) */
/* calculates bulges and internal loops for dimer structures */
static void calc_bulge_internal(int ii, int jj, int i, int j, double* EntropyEnthalpy, int traceback, int maxLoop);
/* calculates bulges and internal loops for monomer structures */
static void calc_bulge_internal2(int ii, int jj, int i, int j, double* EntropyEnthalpy, int traceback, int maxLoop);
/* carries out Bulge and Internal loop and stack calculations to hairpin */
static void CBI(int i, int j, double* EntropyEnthalpy, int traceback, int maxLoop);
/* finds monomer structure that has maximum Tm */
static void calc_hairpin(int i, int j, double* EntropyEnthalpy, int traceback);
static double Ss(int i, int j, int k); /* returns stack entropy */
static double Hs(int i, int j, int k); /* returns stack enthalpy */
/* calculate terminal entropy S and terminal enthalpy H starting reading from 5'end (Left hand/3' end - Right end) */
static void LSH(int i, int j, double* EntropyEnthalpy);
static void RSH(int i, int j, double* EntropyEnthalpy);
static void reverse(unsigned char *s);
static int max5(double, double, double, double, double);
/* Is sequence symmetrical */
static int symmetry_thermo(const unsigned char* seq);
/* traceback for dimers */
static void traceback(int i, int j, double RT, int* ps1, int* ps2, int maxLoop, thal_results* o);
/* traceback for hairpins */
static void tracebacku(int*, int, thal_results*);
/* prints ascii output of dimer structure */
static void drawDimer(int*, int*, double, double, double, int, double, thal_results *);
/* prints ascii output of hairpin structure */
static void drawHairpin(int*, double, double, int, double, thal_results *);
static int equal(double a, double b);
static void strcatc(char*, char);
static void push(struct tracer**, int, int, int, thal_results*); /* to add elements to struct */
/* terminal bp for monomer structure */
static void calc_terminal_bp(double temp);
/* executed in calc_terminal_bp; to find structure that corresponds to max Tm for terminal bp */
static double END5_1(int,int); /* END5_1(X,1/2) - 1=Enthalpy, 2=Entropy*/
static double END5_2(int,int);
static double END5_3(int,int);
static double END5_4(int,int);
static double Hd5(int,int); /* returns thermodynamic value (H) for 5' dangling end */
static double Hd3(int,int); /* returns thermodynamic value (H) for 3' dangling end */
static double Sd5(int,int); /* returns thermodynamic value (S) for 5' dangling end */
static double Sd3(int,int); /* returns thermodynamic value (S) for 3' dangling end */
static double Ststack(int,int); /* returns entropy value for terminal stack */
static double Htstack(int,int); /* returns enthalpy value for terminal stack */
/* memory stuff */
static void* safe_calloc(size_t, size_t, thal_results* o);
static void* safe_malloc(size_t, thal_results* o);
static void* safe_realloc(void*, size_t, thal_results* o);
static double* safe_recalloc(double* ptr, int m, int n, thal_results* o);
static char* parampath = NULL; /* path to parameter files */
static int numTriloops; /* hairpin triloop penalties */
static int numTetraloops; /* hairpin tetraloop penalties */
static double atpS[5][5]; /* AT penalty */
static double atpH[5][5]; /* AT penalty */
static double *send5, *hend5; /* calc 5' */
/* w/o init not constant anymore, cause for unimolecular and bimolecular foldings there are different values */
static double dplx_init_H; /* initiation enthalpy; for duplex 200, for unimolecular structure 0 */
static double dplx_init_S; /* initiation entropy; for duplex -5.7, for unimoleculat structure 0 */
static double saltCorrection; /* value calculated by saltCorrectS, includes correction for monovalent and divalent cations */
static double RC; /* universal gas constant multiplied w DNA conc - for melting temperature */
static double SHleft; /* var that helps to find str w highest melting temperature */
static int bestI, bestJ; /* starting position of most stable str */
static double* enthalpyDPT; /* matrix for values of enthalpy */
static double* entropyDPT; /* matrix for values of entropy */
static unsigned char *oligo1, *oligo2; /* inserted oligo sequenced */
static unsigned char *numSeq1, *numSeq2; /* same as oligo1 and oligo2 but converted to numbers */
static int len1, len2, len3; /* length of sequense 1 and 2 *//* 17.02.2009 int temponly;*/ /* print only temperature of the predicted structure */
static double dangleEntropies3[5][5][5]; /* thermodynamic paramteres for 3' dangling ends */
static double dangleEnthalpies3[5][5][5]; /* ther params for 3' dangling ends */
static double dangleEntropies5[5][5][5]; /* ther params for 5' dangling ends */
static double dangleEnthalpies5[5][5][5]; /* ther params for 5' dangling ends */
static double stackEntropies[5][5][5][5]; /* ther params for perfect match pairs */
static double stackEnthalpies[5][5][5][5]; /* ther params for perfect match pairs */
static double stackint2Entropies[5][5][5][5]; /*ther params for perfect match and internal mm */
static double stackint2Enthalpies[5][5][5][5]; /* ther params for perfect match and internal mm*/
static double interiorLoopEntropies[30]; /* interior loop params according to length of the loop */
static double bulgeLoopEntropies[30]; /* bulge loop params according to length of the loop */
static double hairpinLoopEntropies[30]; /* hairpin loop params accordint to length of the loop */
static double interiorLoopEnthalpies[30]; /* same as interiorLoopEntropies but values of entropy */
static double bulgeLoopEnthalpies[30]; /* same as bulgeLoopEntropies but values of entropy */
static double hairpinLoopEnthalpies[30]; /* same as hairpinLoopEntropies but values of entropy */
static double tstackEntropies[5][5][5][5]; /* ther params for terminal mismatches */
static double tstackEnthalpies[5][5][5][5]; /* ther params for terminal mismatches */
static double tstack2Entropies[5][5][5][5]; /* ther params for internal terminal mismatches */
static double tstack2Enthalpies[5][5][5][5]; /* ther params for internal terminal mismatches */
static struct triloop* triloopEntropies = NULL; /* ther penalties for given triloop seq-s */
static struct triloop* triloopEnthalpies = NULL; /* ther penalties for given triloop seq-s */
static struct tetraloop* tetraloopEntropies = NULL; /* ther penalties for given tetraloop seq-s */
static struct tetraloop* tetraloopEnthalpies = NULL; /* ther penalties for given tetraloop seq-s */
static jmp_buf _jmp_buf;
/* Read the thermodynamic values (parameters) from the parameter files
in the directory specified by 'path'. Return 0 on success and -1
on error. The thermodynamic values are stored in multiple static
variables. */
int
get_thermodynamic_values(const char* path, thal_results *o)
{
if (setjmp(_jmp_buf) != 0) {
return -1;
}
parampath = (char*) safe_malloc((strlen(path) + 1) * sizeof(char), o);
strcpy(parampath, path);
getStack(stackEntropies, stackEnthalpies, o);
/* verifyStackTable(stackEntropies, "entropy");
verifyStackTable(stackEnthalpies, "enthalpy"); */ /* this is for code debugging */
getStackint2(stackint2Entropies, stackint2Enthalpies, o);
getDangle(dangleEntropies3, dangleEnthalpies3, dangleEntropies5, dangleEnthalpies5, o);
getLoop(hairpinLoopEntropies, interiorLoopEntropies, bulgeLoopEntropies, hairpinLoopEnthalpies,
interiorLoopEnthalpies, bulgeLoopEnthalpies, o);
getTstack(tstackEntropies, tstackEnthalpies, o);
getTstack2(tstack2Entropies, tstack2Enthalpies, o);
getTriloop(&triloopEntropies, &triloopEnthalpies, &numTriloops, o);
getTetraloop(&tetraloopEntropies, &tetraloopEnthalpies, &numTetraloops, o);
/* getting the AT-penalties */
tableStartATS(AT_S, atpS);
tableStartATH(AT_H, atpH);
return 0;
}
void
destroy_thal_structures()
{
free(parampath);
free(triloopEntropies);
free(triloopEnthalpies);
free(tetraloopEntropies);
free(tetraloopEnthalpies);
}
/* central method: execute all sub-methods for calculating secondary
structure for dimer or for monomer */
void
thal(const unsigned char *oligo_f,
const unsigned char *oligo_r,
const thal_args *a,
thal_results *o)
{
double* SH;
int i, j;
int len_f, len_r;
double T1;
int k;
int *bp;
unsigned char *oligo2_rev = NULL;
double mh, ms;
send5 = hend5 = NULL;
enthalpyDPT = entropyDPT = NULL;
numSeq1 = numSeq2 = NULL;
oligo1 = oligo2 = NULL;
strcpy(o->msg, "");
o->temp = THAL_ERROR_SCORE;
errno = 0;
if (setjmp(_jmp_buf) != 0) {
o->temp = THAL_ERROR_SCORE;
return; /* If we get here, that means we returned via a
longjmp. In this case errno might be ENOMEM,
but not necessarily. */
}
CHECK_ERROR(NULL == oligo_f, "NULL first sequence");
CHECK_ERROR(NULL == oligo_r, "NULL second sequence");
len_f = length_unsig_char(oligo_f);
len_r = length_unsig_char(oligo_r);
/* The following error messages will be seen by end users and will
not be easy to understand. */
CHECK_ERROR((len_f > THAL_MAX_ALIGN) && (len_r > THAL_MAX_ALIGN),
"Both sequences longer than " XSTR(THAL_MAX_ALIGN)
" for thermodynamic alignment");
CHECK_ERROR((len_f > THAL_MAX_SEQ),
LONG_SEQ_ERR_STR(THAL_MAX_SEQ) " (1)");
CHECK_ERROR((len_r > THAL_MAX_SEQ),
LONG_SEQ_ERR_STR(THAL_MAX_SEQ) " (2)");
CHECK_ERROR(NULL == a, "NULL 'in' pointer");
if (NULL == o) return; /* Leave it to the caller to crash */
CHECK_ERROR(a->type != thal_any
&& a->type != thal_end1
&& a->type != thal_end2
&& a->type != thal_hairpin,
"Illegal type");
o->align_end_1 = -1;
o->align_end_2 = -1;
if ('\0' == oligo_f) {
strcpy(o->msg, "Empty first sequence");
o->temp = 0.0;
return;
}
if ('\0' == oligo_r) {
strcpy(o->msg, "Empty second sequence");
o->temp = 0.0;
return;
}
if (0 == len_f) {
o->temp = 0.0;
return;
}
if (0 == len_r) {
o->temp = 0.0;
return;
}
if(a->type!=3) {
oligo1 = (unsigned char*) safe_malloc((len_f + 1) * sizeof(unsigned char), o);
oligo2 = (unsigned char*) safe_malloc((len_r + 1) * sizeof(unsigned char), o);
strcpy((char*)oligo1,(const char*)oligo_f);
strcpy((char*)oligo2,(const char*)oligo_r);
} else {
oligo1 = (unsigned char*) safe_malloc((len_r + 1) * sizeof(unsigned char), o);
oligo2 = (unsigned char*) safe_malloc((len_f + 1) * sizeof(unsigned char), o);
strcpy((char*)oligo1,(const char*)oligo_r);
strcpy((char*)oligo2,(const char*)oligo_f);
}
/*** INIT values for unimolecular and bimolecular structures ***/
if (a->type==4) { /* unimolecular folding */
len2 = length_unsig_char(oligo2);
len3 = len2 -1;
dplx_init_H = 0.0;
dplx_init_S = -0.00000000001;
RC=0;
} else if(a->type!=4) {
/* hybridization of two oligos */
dplx_init_H = 200;
dplx_init_S = -5.7;
if(symmetry_thermo(oligo1) && symmetry_thermo(oligo2)) {
RC = R * log(a->dna_conc/1000000000.0);
} else {
RC = R * log(a->dna_conc/4000000000.0);
}
if(a->type!=3) {
oligo2_rev = (unsigned char*) safe_malloc((length_unsig_char(oligo_r) + 1) * sizeof(unsigned char), o);
strcpy((char*)oligo2_rev,(const char*)oligo_r);
} else {
oligo2_rev = (unsigned char*) safe_malloc((length_unsig_char(oligo_f) + 1) * sizeof(unsigned char), o);
strcpy((char*)oligo2_rev,(const char*)oligo_f);
}
reverse(oligo2_rev); /* REVERSE oligo2, so it goes to dpt 3'->5' direction */
free(oligo2);
oligo2=NULL;
oligo2=&oligo2_rev[0];
} else {
strcpy(o->msg, "Wrong alignment type!");
o->temp = THAL_ERROR_SCORE;
errno=0;
#ifdef DEBUG
fprintf(stderr, o->msg);
#endif
return;
}
len1 = length_unsig_char(oligo1);
len2 = length_unsig_char(oligo2);
/* convert nucleotides to numbers */
numSeq1 = (unsigned char*) safe_realloc(numSeq1, len1 + 2, o);
numSeq2 = (unsigned char*) safe_realloc(numSeq2, len2 + 2, o);
/*** Calc part of the salt correction ***/
saltCorrection=saltCorrectS(a->mv,a->dv,a->dntp); /* salt correction for entropy, must be multiplied with N, which is
the total number of phosphates in the duplex divided by 2; 8bp dplx N=7 */
if(a->type == 4){ /* monomer */
/* terminal basepairs */
send5 = (double*) safe_realloc(send5, (len1 + 1) * sizeof(double), o);
hend5 = (double*) safe_realloc(hend5, (len1 + 1) * sizeof(double), o);
}
for(i = 0; i < len1; i++) oligo1[i] = toupper(oligo1[i]);
for(i = 0; i < len2; i++) oligo2[i] = toupper(oligo2[i]);
for(i = 1; i <= len1; ++i) numSeq1[i] = str2int(oligo1[i - 1]);
for(i = 1; i <= len2; ++i) numSeq2[i] = str2int(oligo2[i - 1]);
numSeq1[0] = numSeq1[len1 + 1] = numSeq2[0] = numSeq2[len2 + 1] = 4; /* mark as N-s */
if (a->type==4) { /* calculate structure of monomer */
enthalpyDPT = safe_recalloc(enthalpyDPT, len1, len2, o);
entropyDPT = safe_recalloc(entropyDPT, len1, len2, o);
initMatrix2();
fillMatrix2(a->maxLoop, o);
calc_terminal_bp(a->temp);
mh = HEND5(len1);
ms = SEND5(len1);
o->align_end_1 = (int) mh;
o->align_end_2 = (int) ms;
bp = (int*) safe_calloc(len1, sizeof(int), o);
for (k = 0; k < len1; ++k) bp[k] = 0;
if(isFinite(mh)) {
tracebacku(bp, a->maxLoop, o);
/* traceback for unimolecular structure */
drawHairpin(bp, mh, ms, a->temponly,a->temp, o); /* if temponly=1 then return after printing basic therm data */
} else if(a->temponly==0) {
fputs("No secondary structure could be calculated\n",stderr);
}
if(o->temp==-_INFINITY && (!strcmp(o->msg, ""))) o->temp=0.0;
free(bp);
free(enthalpyDPT);
free(entropyDPT);
free(numSeq1);
free(numSeq2);
free(send5);
free(hend5);
free(oligo1);
free(oligo2);
return;
} else if(a->type!=4) { /* Hybridization of two moleculs */
len3 = len2;
enthalpyDPT = safe_recalloc(enthalpyDPT, len1, len2, o); /* dyn. programming table for dS and dH */
entropyDPT = safe_recalloc(entropyDPT, len1, len2, o); /* enthalpyDPT is 3D array represented as 1D array */
initMatrix();
fillMatrix(a->maxLoop, o);
SHleft = -_INFINITY;
SH = (double*) safe_malloc(2 * sizeof(double), o);
/* calculate terminal basepairs */
bestI = bestJ = 0;
if(a->type==1)
for (i = 1; i <= len1; i++) {
for (j = 1; j <= len2; j++) {
RSH(i, j, SH);
SH[0] = SH[0]+SMALL_NON_ZERO; /* this adding is done for compiler, optimization -O2 vs -O0 */
SH[1] = SH[1]+SMALL_NON_ZERO;
T1 = ((EnthalpyDPT(i, j)+ SH[1] + dplx_init_H) / ((EntropyDPT(i, j)) + SH[0] +
dplx_init_S + RC)) - ABSOLUTE_ZERO;
if (T1 > SHleft && ((EntropyDPT(i, j) + SH[0])<0 && (SH[1] + EnthalpyDPT(i, j))<0)) {
SHleft = T1;
bestI = i;
bestJ = j;
}
}
}
int *ps1, *ps2;
ps1 = (int*) safe_calloc(len1, sizeof(int), o);
ps2 = (int*) safe_calloc(len2, sizeof(int), o);
for (i = 0; i < len1; ++i)
ps1[i] = 0;
for (j = 0; j < len2; ++j)
ps2[j] = 0;
if(a->type == 2 || a->type == 3) {
/* THAL_END1 */
bestI = bestJ = 0;
bestI = len1;
i = len1;
SHleft = -_INFINITY;
for (j = 1; j <= len2; ++j) {
RSH(i, j, SH);
SH[0] = SH[0]+SMALL_NON_ZERO; /* this adding is done for compiler, optimization -O2 vs -O0,
that compiler could understand that SH is changed in this cycle */
SH[1] = SH[1]+SMALL_NON_ZERO;
T1 = ((EnthalpyDPT(i, j)+ SH[1] + dplx_init_H) / ((EntropyDPT(i, j)) + SH[0] +
dplx_init_S + RC)) - ABSOLUTE_ZERO;
if (T1 > SHleft && ((SH[0] + EntropyDPT(i, j))<0 && (SH[1] + EnthalpyDPT(i, j))<0)) {
SHleft = T1;
bestJ = j;
}
}
}
if (!isFinite(SHleft)) bestI = bestJ = 1;
double dH, dS;
RSH(bestI, bestJ, SH);
dH = EnthalpyDPT(bestI, bestJ)+ SH[1] + dplx_init_H;
dS = (EntropyDPT(bestI, bestJ) + SH[0] + dplx_init_S);
/* tracebacking */
for (i = 0; i < len1; ++i)
ps1[i] = 0;
for (j = 0; j < len2; ++j)
ps2[j] = 0;
if(isFinite(EnthalpyDPT(bestI, bestJ))){
traceback(bestI, bestJ, RC, ps1, ps2, a->maxLoop, o);
drawDimer(ps1, ps2, SHleft, dH, dS, a->temponly,a->temp, o);
o->align_end_1=bestI;
o->align_end_2=bestJ;
} else {
o->temp = 0.0;
}
free(ps1);
free(ps2);
free(SH);
free(oligo2_rev);
free(enthalpyDPT);
free(entropyDPT);
free(numSeq1);
free(numSeq2);
free(oligo1);
return;
}
return;
}
/*** END thal() ***/
/* Set default args */
void
set_thal_default_args(thal_args *a)
{
memset(a, 0, sizeof(*a));
a->debug = 0;
a->type = thal_any; /* thal_alignment_type THAL_ANY */
a->maxLoop = MAX_LOOP;
a->mv = 50; /* mM */
a->dv = 0.0; /* mM */
a->dntp = 0.8; /* mM */
a->dna_conc = 50; /* nM */
a->temp = 310.15; /* Kelvin */
a->temponly = 1; /* return only melting temperature of predicted structure */
a->dimer = 1; /* by default dimer structure is calculated */
}
/* Set default args for oligo */
void
set_thal_oligo_default_args(thal_args *a)
{
memset(a, 0, sizeof(*a));
a->debug = 0;
a->type = thal_any; /* thal_alignment_type THAL_ANY */
a->maxLoop = MAX_LOOP;
a->mv = 50; /* mM */
a->dv = 0.0; /* mM */
a->dntp = 0.0; /* mM */
a->dna_conc = 50; /* nM */
a->temp = 310.15; /* Kelvin */
a->temponly = 1; /* return only melting temperature of predicted structure */
a->dimer = 1; /* by default dimer structure is calculated */
}
static unsigned char
str2int(char c)
{
switch (c) {
case 'A': case '0':
return 0;
case 'C': case '1':
return 1;
case 'G': case '2':
return 2;
case 'T': case '3':
return 3;
}
return 4;
}
/* memory stuff */
static double*
safe_recalloc(double* ptr, int m, int n, thal_results* o)
{
return (double*) safe_realloc(ptr, m * n * sizeof(double), o);
}
static void*
safe_calloc(size_t m, size_t n, thal_results *o)
{
void* ptr;
if (!(ptr = calloc(m, n))) {
#ifdef DEBUG
fputs("Error in calloc()\n", stderr);
#endif
THAL_OOM_ERROR;
}
return ptr;
}
static void*
safe_malloc(size_t n, thal_results *o)
{
void* ptr;
if (!(ptr = malloc(n))) {
#ifdef DEBUG
fputs("Error in malloc()\n", stderr);
#endif
THAL_OOM_ERROR;
}
return ptr;
}
static void*
safe_realloc(void* ptr, size_t n, thal_results *o)
{
ptr = realloc(ptr, n);
if (ptr == NULL) {
#ifdef DEBUG
fputs("Error in realloc()\n", stderr);
#endif
THAL_OOM_ERROR;
}
return ptr;
}
static int
max5(double a, double b, double c, double d, double e)
{
if(a > b && a > c && a > d && a > e) return 1;
else if(b > c && b > d && b > e) return 2;
else if(c > d && c > e) return 3;
else if(d > e) return 4;
else return 5;
}
static void
push(struct tracer** stack, int i, int j, int mtrx, thal_results* o)
{
struct tracer* new_top;
new_top = (struct tracer*) safe_malloc(sizeof(struct tracer), o);
new_top->i = i;
new_top->j = j;
new_top->mtrx = mtrx;
new_top->next = *stack;
*stack = new_top;
}
static void
reverse(unsigned char *s)
{
int i,j;
char c;
for (i = 0, j = length_unsig_char(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
static FILE*
openParamFile(const char* fname, thal_results* o)
{
FILE* file;
char* paramdir;
file = fopen(fname, "rt");
if (!file) {
paramdir = (char*) safe_malloc(strlen(parampath) + strlen(fname) + 1, o);
strcpy(paramdir, parampath);
strcat(paramdir, fname);
if (!(file = fopen(paramdir, "rt"))) {
#ifdef DEBUG
perror(paramdir);
#endif
THAL_IO_ERROR(paramdir);
free(paramdir);
}
free(paramdir);
}
return file;
}
static double
saltCorrectS (double mv, double dv, double dntp)
{
if(dv<=0) dntp=dv;
return 0.368*((log((mv+120*(sqrt(fmax(0.0, dv-dntp))))/1000)));
}
#define INIT_BUF_SIZE 1024
static char*
p3_read_line(FILE *file, thal_results* o)
{
static size_t ssz;
static char *s = NULL;
size_t remaining_size;
char *p, *n;
if (NULL == s) {
ssz = INIT_BUF_SIZE;
s = (char *) safe_malloc(ssz, o);
}
p = s;
remaining_size = ssz;
while (1) {
if (fgets(p, remaining_size, file) == NULL) /* End of file. */
return p == s ? NULL : s;
if ((n = strchr(p, '\n')) != NULL) {
*n = '\0';
return s;
}
/* We did not get the whole line. */
if (ssz >= INT_MAX / 2)
ssz = INT_MAX;
else {
ssz *= 2;
}
s = (char *) safe_realloc(s, ssz, o);
p = strchr(s, '\0');
remaining_size = ssz - (p - s);
}
}
/* These functions are needed as "inf" cannot be read on Windows directly */
static double
readDouble(FILE *file, thal_results* o)
{
double result;
char *line = p3_read_line(file, o);
/* skip any spaces at beginning of the line */
while (isspace(*line)) line++;
if (!strncmp(line, "inf", 3))
return _INFINITY;
sscanf(line, "%lf", &result);
return result;
}
/* Reads a line containing 4 doubles, which can be specified as "inf". */
static void
readLoop(FILE *file, double *v1, double *v2, double *v3, thal_results *o)
{
char *line = p3_read_line(file, o);
char *p = line, *q;
/* skip first number on the line */
while (isspace(*p)) p++;
while (isdigit(*p)) p++;
while (isspace(*p)) p++;
/* read second number */
q = p;
while (!isspace(*q)) q++;
*q = '\0'; q++;
if (!strcmp(p, "inf")) *v1 = _INFINITY;
else sscanf(p, "%lf", v1);
while (isspace(*q)) q++;
/* read third number */
p = q;
while (!isspace(*p)) p++;
*p = '\0'; p++;
if (!strcmp(q, "inf")) *v2 = _INFINITY;
else sscanf(q, "%lf", v2);
while (isspace(*p)) p++;
/* read last number */
q = p;
while (!isspace(*q) && (*q != '\0')) q++;
*q = '\0';
if (!strcmp(p, "inf")) *v3 = _INFINITY;
else sscanf(p, "%lf", v3);
}
/* Reads a line containing a short string and a double, used for reading a triloop or tetraloop. */
static int
readTLoop(FILE *file, char *s, double *v, int triloop, thal_results *o)
{
char *line = p3_read_line(file, o);
if (!line) return -1;
char *p = line, *q;
/* skip first spaces */
while (isspace(*p)) p++;
/* read the string */
q = p;
while (isalpha(*q)) q++;
*q = '\0'; q++;
if (triloop) {
strncpy(s, p, 5); /*triloop string has 5 characters*/
s[5] = '\0';
} else {
strncpy(s, p, 6); /*tetraloop string has 6 characters*/
s[6] = '\0';
}
/* skip all spaces */
while (isspace(*q)) q++;
p = q;
while (!isspace(*p) && (*p != '\0')) p++;
*p = '\0';
if (!strcmp(q, "inf")) *v = _INFINITY;
else sscanf(q, "%lg", v);
return 0;
}
static void
getStack(double stackEntropies[5][5][5][5], double stackEnthalpies[5][5][5][5], thal_results* o)
{
int i, j, ii, jj;
FILE *sFile, *hFile;
sFile = openParamFile("stack.ds", o);
hFile = openParamFile("stack.dh", o);
for (i = 0; i < 5; ++i) {
for (ii = 0; ii < 5; ++ii) {
for (j = 0; j < 5; ++j) {
for (jj = 0; jj < 5; ++jj) {
if (i == 4 || j == 4 || ii == 4 || jj == 4) {
stackEntropies[i][ii][j][jj] = -1.0;
stackEnthalpies[i][ii][j][jj] = _INFINITY;
} else {
stackEntropies[i][ii][j][jj] = readDouble(sFile, o);
stackEnthalpies[i][ii][j][jj] = readDouble(hFile, o);
if (!isFinite(stackEntropies[i][ii][j][jj]) || !isFinite(stackEnthalpies[i][ii][j][jj])) {
stackEntropies[i][ii][j][jj] = -1.0;
stackEnthalpies[i][ii][j][jj] = _INFINITY;
}
}
}
}
}
}
fclose(sFile);
fclose(hFile);
}
static void
getStackint2(double stackint2Entropies[5][5][5][5], double stackint2Enthalpies[5][5][5][5], thal_results* o)
{
int i, j, ii, jj;
FILE *sFile, *hFile;
sFile = openParamFile("stackmm.ds", o);
hFile = openParamFile("stackmm.dh", o);
for (i = 0; i < 5; ++i) {
for (ii = 0; ii < 5; ++ii) {
for (j = 0; j < 5; ++j) {
for (jj = 0; jj < 5; ++jj) {
if (i == 4 || j == 4 || ii == 4 || jj == 4) {
stackint2Entropies[i][ii][j][jj] = -1.0;
stackint2Enthalpies[i][ii][j][jj] = _INFINITY;
} else {
stackint2Entropies[i][ii][j][jj] = readDouble(sFile, o);
stackint2Enthalpies[i][ii][j][jj] = readDouble(hFile, o);
if (!isFinite(stackint2Entropies[i][ii][j][jj]) || !isFinite(stackint2Enthalpies[i][ii][j][jj])) {
stackint2Entropies[i][ii][j][jj] = -1.0;
stackint2Enthalpies[i][ii][j][jj] = _INFINITY;
}
}
}
}
}
}
fclose(sFile);
fclose(hFile);
}
/*
static void
verifyStackTable(double stack[5][5][5][5], char* type)
{
int i, j, ii, jj;
for (i = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
for (ii = 0; ii < 4; ++ii)
for (jj = 0; jj < 4; ++jj)
if (stack[i][j][ii][jj] != stack[jj][ii][j][i])
#ifdef DEBUG
fprintf(stderr, "Warning: symmetrical stacks _are_ _not_ equal: %c-%c/%c-%c stack %s is %g; %c-%c/%c-%c stack %s is %g\n",
#endif
BASES[i], BASES[j], BASES[ii], BASES[jj], type, stack[i][j][ii][jj], BASES[jj],
BASES[ii], BASES[j], BASES[i], type, stack[jj][ii][j][i]);
}
*/
static void
getDangle(double dangleEntropies3[5][5][5], double dangleEnthalpies3[5][5][5], double dangleEntropies5[5][5][5],
double dangleEnthalpies5[5][5][5], thal_results* o)
{
int i, j, k;
FILE *sFile, *hFile;
sFile = openParamFile("dangle.ds", o);
hFile = openParamFile("dangle.dh", o);
for (i = 0; i < 5; ++i)
for (j = 0; j < 5; ++j)
for (k = 0; k < 5; ++k) {
if (i == 4 || j == 4) {
dangleEntropies3[i][k][j] = -1.0;
dangleEnthalpies3[i][k][j] = _INFINITY;
} else if (k == 4) {
dangleEntropies3[i][k][j] = -1.0;
dangleEnthalpies3[i][k][j] = _INFINITY;
} else {