Replace Progs/RNAalifold with x64 binary and add all other programs
[jabaws.git] / binaries / src / ViennaRNA / lib / convert_epars.c
1 /*
2 ###################################
3 # convert energy parameter files  #
4 # from ViennaRNAPackage 1.8.4 to  #
5 # 2.0 format                      #
6 #                                 #
7 # Ronny Lorenz                    #
8 ###################################
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include <math.h>
16
17 #include "utils.h"
18 #include "fold_vars.h"
19 #include "read_epars.h"
20 #include "pair_mat.h"
21
22 #include "1.8.4_epars.h"
23 #include "1.8.4_intloops.h"
24
25 #include "convert_epars.h"
26
27 enum parset_184 {UNKNOWN_184= -1, QUIT_184, S_184, SH_184, HP_184, B_184, IL_184, MMI_184, MMH_184, MMM_184, MM_H_184,
28              DE5_184, DE3_184, DE5_H_184, DE3_H_184, ML_184, TL_184, TRI_184, TE_184, NIN_184, MISC_184,
29              INT11_184, INT11_H_184, INT21_184, INT21_H_184, INT22_184, INT22_H_184};
30
31
32 PRIVATE unsigned int  read_old_parameter_file(FILE *ifile, int skip_header);
33 PRIVATE void          write_new_parameter_file(FILE *ofile, unsigned int options);
34 PRIVATE void          rd_stacks(int stack[NBPAIRS+1][NBPAIRS+1], FILE *fp);
35 PRIVATE void          rd_loop(int looparray[31], FILE *fp);
36 PRIVATE void          rd_mismatch(int mismatch[NBPAIRS+1][5][5], FILE *fp);
37 PRIVATE void          rd_int11(int int11[NBPAIRS+1][NBPAIRS+1][5][5], FILE *fp);
38 PRIVATE void          rd_int21(int int21[NBPAIRS+1][NBPAIRS+1][5][5][5], FILE *fp);
39 PRIVATE void          rd_int22(int int22[NBPAIRS+1][NBPAIRS+1][5][5][5][5], FILE *fp);
40 PRIVATE void          rd_dangle(int dangles[NBPAIRS+1][5], FILE *fp);
41 PRIVATE void          rd_MLparams(FILE *fp);
42 PRIVATE void          rd_misc(FILE *fp);
43 PRIVATE void          rd_ninio(FILE *fp);
44 PRIVATE void          rd_Tetra_loop(FILE *fp);
45 PRIVATE void          rd_Tri_loop(FILE *fp);
46 PRIVATE void          check_symmetry(void);
47 PRIVATE enum          parset_184 gettype_184(char ident[]);
48 PRIVATE char          *get_array1(int *arr, int size, FILE *fp);
49 PRIVATE void          ignore_comment(char *line);
50 PRIVATE void          display_array(int *p, int size, int line, FILE *fp);
51
52
53 PUBLIC void convert_parameter_file(const char *iname, const char *oname, unsigned int options){
54   FILE          *ifile, *ofile;
55   unsigned int  old_options = 0;
56   int           skip_input_header = 0;
57
58   if(options & VRNA_CONVERT_OUTPUT_DUMP){
59     if(oname == NULL) oname = iname;
60     skip_input_header = 1;
61   }
62   else{
63     if(iname == NULL){
64       ifile = stdin;
65       skip_input_header = 1;
66     }
67     else if(!(ifile=fopen(iname,"r"))){
68       fprintf(stderr, "convert_epars: can't open file %s\n", iname);
69       return;
70     }
71     /* read old (1.8.4 format) parameter file */
72     old_options = read_old_parameter_file(ifile, skip_input_header);
73     if(ifile != stdin) fclose(ifile);
74     check_symmetry();
75   }
76
77   if(options & VRNA_CONVERT_OUTPUT_VANILLA)
78     options = old_options;
79
80   if(oname == NULL) ofile = stdout;
81   else if(!(ofile=fopen(oname,"a+"))){
82     fprintf(stderr, "convert_epars: can't open file %s for writing\n", oname);
83     return;
84   }
85   write_new_parameter_file(ofile, options);
86   if(ofile != stdout) fclose(ofile);
87 }
88
89
90 /*------------------------------------------------------------*/
91 PRIVATE unsigned int read_old_parameter_file(FILE *ifile, int skip_header){
92   char                  *line, ident[32];
93   enum      parset_184  type;
94   int                   r, last;
95   unsigned  int         read_successfully = 0;
96
97   if (!(line = get_line(ifile))) {
98     warn_user("convert_epars: can't read input parameter file");
99     return 0;
100   }
101   if(!skip_header){
102     if (strncmp(line,"## RNAfold parameter file",25)!=0){
103       warn_user("convert_epars: Missing header line in input parameter file.\n"
104                 "May be this file has incorrect format?");
105       free(line);
106       return 0;
107     }
108     free(line);
109     line = get_line(ifile);
110   }
111   last = 0;
112   do{
113     r = sscanf(line, "# %31s", ident);
114     if (r==1){
115       type = gettype_184(ident);
116       switch (type){
117         case QUIT_184:    if(ifile == stdin){
118                             fprintf(stderr, "press ENTER to continue...\n");
119                             fflush(stderr);
120                           }
121                           last = 1;
122                           break;
123         case SH_184:      rd_stacks(enthalpies_184, ifile);
124                           read_successfully |= VRNA_CONVERT_OUTPUT_STACK;
125                           break;
126         case S_184:       rd_stacks(stack37_184, ifile);
127                           read_successfully |= VRNA_CONVERT_OUTPUT_STACK;
128                           break;
129         case HP_184:      rd_loop(hairpin37_184, ifile);
130                           read_successfully |= VRNA_CONVERT_OUTPUT_HP;
131                           break;
132         case B_184:       rd_loop(bulge37_184, ifile);
133                           read_successfully |= VRNA_CONVERT_OUTPUT_BULGE;
134                           break;
135         case IL_184:      rd_loop(internal_loop37_184, ifile);
136                           read_successfully |= VRNA_CONVERT_OUTPUT_INT;
137                           break;
138         case MMH_184:     rd_mismatch(mismatchH37_184, ifile);
139                           read_successfully |= VRNA_CONVERT_OUTPUT_MM_HP;
140                           break;
141         case MMI_184:     rd_mismatch(mismatchI37_184, ifile);
142                           read_successfully |= VRNA_CONVERT_OUTPUT_MM_INT
143                                               |VRNA_CONVERT_OUTPUT_MM_INT_1N  /* since 1:n-interior loop mismatches are treated seperately in 2.0 */
144                                               |VRNA_CONVERT_OUTPUT_MM_INT_23; /* since 2:3-interior loop mismatches are treated seperately in 2.0 */
145                           break;
146         case MMM_184:     rd_mismatch(mismatchM37_184, ifile);
147                           read_successfully |= VRNA_CONVERT_OUTPUT_MM_MULTI;
148                           break;
149         case MM_H_184:    rd_mismatch(mism_H_184, ifile);
150                           read_successfully |= VRNA_CONVERT_OUTPUT_MM_HP      /* since hairpin mismatches are treated seperately in 2.0 */
151                                               |VRNA_CONVERT_OUTPUT_MM_INT     /* since interior loop  mismatches are treated seperately in 2.0 */
152                                               |VRNA_CONVERT_OUTPUT_MM_INT_1N  /* since 1:n-interior loop mismatches are treated seperately in 2.0 */
153                                               |VRNA_CONVERT_OUTPUT_MM_INT_23  /* since 2:3-interior loop mismatches are treated seperately in 2.0 */
154                                               |VRNA_CONVERT_OUTPUT_MM_MULTI;  /* since multi loop mismatches are treated seperately in 2.0 */
155                           break;
156         case INT11_184:   rd_int11(int11_37_184, ifile);
157                           read_successfully |= VRNA_CONVERT_OUTPUT_INT_11;
158                           break;
159         case INT11_H_184: rd_int11(int11_H_184, ifile);
160                           read_successfully |= VRNA_CONVERT_OUTPUT_INT_11;
161                           break;
162         case INT21_184:   rd_int21(int21_37_184, ifile);
163                           read_successfully |= VRNA_CONVERT_OUTPUT_INT_21;
164                           break;
165         case INT21_H_184: rd_int21(int21_H_184, ifile);
166                           read_successfully |= VRNA_CONVERT_OUTPUT_INT_21;
167                           break;
168         case INT22_184:   rd_int22(int22_37_184, ifile);
169                           read_successfully |= VRNA_CONVERT_OUTPUT_INT_22;
170                           break;
171         case INT22_H_184: rd_int22(int22_H_184, ifile);
172                           read_successfully |= VRNA_CONVERT_OUTPUT_INT_22;
173                           break;
174         case DE5_184:     rd_dangle(dangle5_37_184, ifile);
175                           read_successfully |= VRNA_CONVERT_OUTPUT_DANGLE5
176                                               |VRNA_CONVERT_OUTPUT_MM_MULTI /* since multi loop mismatches were treated as dangle contribution */
177                                               |VRNA_CONVERT_OUTPUT_MM_EXT;  /* since exterior loop mismatches were treated as dangle contribution */
178                           break;
179         case DE5_H_184:   rd_dangle(dangle5_H_184, ifile);
180                           read_successfully |= VRNA_CONVERT_OUTPUT_DANGLE5
181                                               |VRNA_CONVERT_OUTPUT_MM_MULTI /* since multi loop mismatches were treated as dangle contribution */
182                                               |VRNA_CONVERT_OUTPUT_MM_EXT;  /* since exterior loop mismatches were treated as dangle contribution */
183                           break;
184         case DE3_184:     rd_dangle(dangle3_37_184, ifile);
185                           read_successfully |= VRNA_CONVERT_OUTPUT_DANGLE3
186                                               |VRNA_CONVERT_OUTPUT_MM_MULTI /* since multi loop mismatches were treated as dangle contribution */
187                                               |VRNA_CONVERT_OUTPUT_MM_EXT;  /* since exterior loop mismatches were treated as dangle contribution */
188                           break;
189         case DE3_H_184:   rd_dangle(dangle3_H_184, ifile);
190                           read_successfully |= VRNA_CONVERT_OUTPUT_DANGLE3
191                                               |VRNA_CONVERT_OUTPUT_MM_MULTI /* since multi loop mismatches were treated as dangle contribution */
192                                               |VRNA_CONVERT_OUTPUT_MM_EXT;  /* since exterior loop mismatches were treated as dangle contribution */
193                           break;
194         case ML_184:      rd_MLparams(ifile);
195                           read_successfully |= VRNA_CONVERT_OUTPUT_ML
196                                               |VRNA_CONVERT_OUTPUT_MISC;    /* since TerminalAU went to "misc" section */
197                           break;
198         case NIN_184:     rd_ninio(ifile);
199                           read_successfully |= VRNA_CONVERT_OUTPUT_NINIO;
200                           break;
201         case TL_184:      rd_Tetra_loop(ifile);
202                           read_successfully |= VRNA_CONVERT_OUTPUT_SPECIAL_HP;
203                           break;
204         case TRI_184:     rd_Tri_loop(ifile);
205                           read_successfully |= VRNA_CONVERT_OUTPUT_SPECIAL_HP;
206                           break;
207         case MISC_184:    rd_misc(ifile);
208                           read_successfully |= VRNA_CONVERT_OUTPUT_MISC;
209                           break;
210         default:          /* do nothing but complain */
211                           fprintf(stderr,"convert_parameter_file: Unknown field identifier in `%s'\n", line);
212       }
213     } /* else ignore line */
214     free(line);
215   } while((line=get_line(ifile)) && !last);
216   return read_successfully;
217 }
218
219 PRIVATE void display_array(int *p, int size, int nl, FILE *fp){
220   int i;
221   for (i=1; i<=size; i++, p++) {
222     switch(*p){
223       case  INF: fprintf(fp,"   INF");    break;
224       case -INF: fprintf(fp,"  -INf");    break;
225       case  DEF: fprintf(fp,"   DEF");    break;
226       default:   fprintf(fp,"%6d",  *p);  break;
227     }
228     if ((i%nl)==0) fprintf(fp,"\n");
229   }
230   if (size%nl) fprintf(fp,"\n");
231   return;
232 }
233
234 PRIVATE char *get_array1(int *arr, int size, FILE *fp){
235   int    i, p, pos, pp, r, last;
236   char  *line, buf[16];
237   i = last = 0;
238   while( i<size ) {
239     line = get_line(fp);
240     if (!line) nrerror("convert_epars: unexpected end of file in get_array1");
241     ignore_comment(line);
242     pos=0;
243     while ((i<size)&&(sscanf(line+pos,"%15s%n", buf, &pp)==1)) {
244       pos += pp;
245       if (buf[0]=='*') {i++; continue;}
246       else if (buf[0]=='x') { /* should only be used for loop parameters */
247         if (i==0) nrerror("convert_epars: can't extrapolate first value");
248         p = arr[last] + (int) (0.5+ lxc37_184*log(((double) i)/(double)(last)));
249       }
250       else if (strcmp(buf,"DEF") == 0) p = DEF;
251       else if (strcmp(buf,"INF") == 0) p = INF;
252       else if (strcmp(buf,"NST") == 0) p = NST;
253       else {
254         r=sscanf(buf,"%d", &p);
255         if (r!=1) {
256           return line+pos;
257           fprintf(stderr, "convert_epars: can't interpret `%s' in get_array1\n", buf);
258           exit(1);
259         }
260         last = i;
261       }
262       arr[i++]=p;
263     }
264     free(line);
265   }
266
267   return NULL;
268 }
269 /*------------------------------------------------------------*/
270
271 PRIVATE void  rd_stacks(int stacks[NBPAIRS+1][NBPAIRS+1], FILE *fp)
272 {
273   int    i;
274   char  *cp;
275   for (i=1; i<=NBPAIRS; i++) {
276     cp = get_array1(stacks[i]+1,NBPAIRS, fp);
277     if (cp) {
278       fprintf(stderr,"convert_epars: \nrd_stacks: %s\n", cp);
279       exit(1);
280     }
281   }
282   return;
283 }
284 /*------------------------------------------------------------*/
285
286 PRIVATE void rd_loop(int loop[31], FILE *fp)
287 {
288   char *cp;
289
290   cp   = get_array1(loop, 31, fp);
291
292   if (cp) {
293     fprintf(stderr,"convert_epars: \nrd_loop: %s\n", cp);
294     exit(1);
295   }
296   return;
297 }
298 /*------------------------------------------------------------*/
299
300 PRIVATE void rd_mismatch(int mismatch[NBPAIRS+1][5][5], FILE *fp)
301 {
302   char  *cp;
303   int    i;
304
305   for (i=1; i<NBPAIRS+1; i++) {
306     cp = get_array1(mismatch[i][0],5*5, fp);
307     if (cp) {
308       fprintf(stderr, "convert_epars: rd_mismatch: in field mismatch[%d]\n\t%s\n", i, cp);
309       exit(1);
310     }
311   }
312   return;
313 }
314
315 /*------------------------------------------------------------*/
316 PRIVATE void rd_int11(int int11[NBPAIRS+1][NBPAIRS+1][5][5], FILE *fp)
317 {
318   char  *cp;
319   int    i, j;
320
321   for (i=1; i<NBPAIRS+1; i++) {
322     for (j=1; j<NBPAIRS+1; j++) {
323       cp = get_array1(int11[i][j][0],5*5, fp);
324       if (cp) {
325         fprintf(stderr, "convert_epars: rd_int11: in field int11[%d][%d]\n\t%s\n", i, j, cp);
326         exit(1);
327       }
328     }
329   }
330   return;
331 }
332
333 /*------------------------------------------------------------*/
334 PRIVATE void rd_int21(int int21[NBPAIRS+1][NBPAIRS+1][5][5][5], FILE *fp)
335 {
336   char  *cp;
337   int    i, j, k;
338
339   for (i=1; i<NBPAIRS+1; i++) {
340     for (j=1; j<NBPAIRS+1; j++) {
341       for (k=0; k<5; k++) {
342         cp = get_array1(int21[i][j][k][0],5*5, fp);
343         if (cp) {
344           fprintf(stderr, "convert_epars: rd_int21: in field int21[%d][%d][%d]\n\t%s\n",
345                   i, j, k, cp);
346           exit(1);
347         }
348       }
349     }
350   }
351   return;
352 }
353
354 /*------------------------------------------------------------*/
355 PRIVATE void rd_int22(int int22[NBPAIRS+1][NBPAIRS+1][5][5][5][5], FILE *fp)
356 {
357   char  *cp;
358   int    i, j, k, l, m;
359
360   for (i=1; i<NBPAIRS+1; i++)
361     for (j=1; j<NBPAIRS+1; j++)
362       for (k=1; k<5; k++)
363         for (l=1; l<5; l++)
364           for (m=1; m<5; m++) {
365             cp = get_array1(int22[i][j][k][l][m]+1,4, fp);
366             if (cp) {
367               fprintf(stderr, "convert_epars: rd_int22: in field "
368                       "int22[%d][%d][%d][%d][%d]\n\t%s\n",
369                       i, j, k, l, m, cp);
370               exit(1);
371             }
372           }
373
374   return;
375 }
376
377 /*------------------------------------------------------------*/
378 PRIVATE void  rd_dangle(int dangle[NBPAIRS+1][5], FILE *fp)
379 {
380   int   i;
381   char *cp;
382
383   for (i=0; i< NBPAIRS+1; i++) {
384     cp = get_array1(dangle[i],5, fp);
385     if (cp) {
386       fprintf(stderr,"convert_epars: \nrd_dangle: %s\n", cp);
387       exit(1);
388     }
389   }
390   return;
391 }
392
393 /*------------------------------------------------------------*/
394 PRIVATE void  rd_MLparams(FILE *fp)
395 {
396   char *cp;
397   int values[4];
398
399   cp   = get_array1(values,4, fp);
400   if (cp) {
401     fprintf(stderr,"convert_epars: rd_MLparams: %s\n", cp);
402     exit(1);
403   }
404
405   ML_BASE37_184     = values[0];
406   ML_closing37_184  = values[1];
407   ML_intern37_184   = values[2];
408   TerminalAU_184    = values[3];
409
410   return;
411 }
412
413 /*------------------------------------------------------------*/
414
415 PRIVATE void  rd_misc(FILE *fp)
416 {
417   char *cp;
418   int values[1]; /* so far just one */
419
420   cp   = get_array1(values,1, fp);
421   if (cp) {
422     fprintf(stderr,"convert_epars: rd_misc: %s\n", cp);
423     exit(1);
424   }
425
426   DuplexInit_184 = values[0];
427
428   return;
429 }
430
431 /*------------------------------------------------------------*/
432
433 PRIVATE void  rd_ninio(FILE *fp)
434 {
435   char  *cp;
436   int temp[2];
437
438   cp = get_array1(temp, 2, fp);
439
440   if (cp) {
441     fprintf(stderr,"convert_epars: rd_F_ninio: %s\n", cp);
442     exit(1);
443   }
444   F_ninio37_184[2] = temp[0];
445   MAX_NINIO_184  = temp[1];
446   return;
447 }
448
449 /*------------------------------------------------------------*/
450 PRIVATE void  rd_Tetra_loop(FILE *fp)
451 {
452   int    i, r;
453   char   *buf;
454
455   i=0;
456   memset(&Tetraloops_184, 0, 1400);
457   memset(&TETRA_ENERGY37_184, 0, sizeof(int)*200);
458   do {
459     buf = get_line(fp);
460     if (buf==NULL) break;
461     r = sscanf(buf,"%6s %d", &Tetraloops_184[7*i], &TETRA_ENERGY37_184[i]);
462     strcat(Tetraloops_184, " ");
463     free(buf);
464     i++;
465   } while((r==2)&&(i<200));
466   return;
467 }
468
469 /*------------------------------------------------------------*/
470 PRIVATE void  rd_Tri_loop(FILE *fp)
471 {
472   int    i, r;
473   char   *buf;
474
475   i=0;
476   memset(&Triloops_184, 0, 241);
477   memset(&Triloop_E37_184, 0, sizeof(int)*40);
478   do {
479     buf = get_line(fp);
480     if (buf==NULL) break;
481     r = sscanf(buf,"%5s %d", &Triloops_184[6*i], &Triloop_E37_184[i]);
482     Triloops_184[6*i+5]=' ';
483     free(buf);
484     i++;
485   } while((r==2)&&(i<40));
486   return;
487 }
488
489 /*------------------------------------------------------------*/
490
491
492 PRIVATE void ignore_comment(char * line)
493 {
494   /* excise C style comments */
495   /* only one comment per line, no multiline comments */
496   char *cp1, *cp2;
497
498   if ((cp1=strstr(line, "/*"))) {
499     cp2 = strstr(cp1, "*/");
500     if (cp2==NULL)
501       nrerror("convert_epars: unclosed comment in parameter file");
502     /* can't use strcpy for overlapping strings */
503     for (cp2+=2; *cp2!='\0'; cp2++, cp1++)
504       *cp1 = *cp2;
505     *cp1 = '\0';
506   }
507
508   return;
509 }
510
511 PRIVATE enum parset_184 gettype_184(char ident[]){
512   if (strcmp(ident,"stack_enthalpies") == 0)          return SH_184;
513   else if (strcmp(ident,"stack_energies") == 0)       return S_184;
514   else if (strcmp(ident,"hairpin" ) == 0)             return HP_184;
515   else if (strcmp(ident,"bulge") == 0)                return B_184;
516   else if (strcmp(ident,"internal_loop") == 0)        return IL_184;
517   else if (strcmp(ident,"mismatch_hairpin") == 0)     return MMH_184;
518   else if (strcmp(ident,"mismatch_interior") == 0)    return MMI_184;
519   else if (strcmp(ident,"mismatch_multi") == 0)       return MMM_184;
520   else if (strcmp(ident,"mismatch_enthalpies") == 0)  return MM_H_184;
521   else if (strcmp(ident,"int11_energies") == 0)       return INT11_184;
522   else if (strcmp(ident,"int11_enthalpies") == 0)     return INT11_H_184;
523   else if (strcmp(ident,"int21_energies") == 0)       return INT21_184;
524   else if (strcmp(ident,"int21_enthalpies") == 0)     return INT21_H_184;
525   else if (strcmp(ident,"int22_energies") == 0)       return INT22_184;
526   else if (strcmp(ident,"int22_enthalpies") == 0)     return INT22_H_184;
527   else if (strcmp(ident,"dangle5")== 0)               return DE5_184;
528   else if (strcmp(ident,"dangle3")== 0)               return DE3_184;
529   else if (strcmp(ident,"dangle5_enthalpies")== 0)    return DE5_H_184;
530   else if (strcmp(ident,"dangle3_enthalpies")== 0)    return DE3_H_184;
531   else if (strcmp(ident,"ML_params")== 0)             return ML_184;
532   else if (strcmp(ident,"NINIO") == 0)                return NIN_184;
533   else if (strcmp(ident,"Tetraloops") == 0)           return TL_184;
534   else if (strcmp(ident,"Triloops") == 0)             return TRI_184;
535   else if (strcmp(ident,"END") == 0)                  return QUIT_184;
536   else return UNKNOWN_184;
537 }
538
539 PRIVATE void write_new_parameter_file(FILE *ofile, unsigned int option_bits){
540   int           c;
541   char          *pnames[] = {"NP", "CG", "GC", "GU", "UG", "AU", "UA", " @"};
542   char          bnames[]  = "@ACGU";
543   unsigned  int options   = 0;
544
545   options = (option_bits & VRNA_CONVERT_OUTPUT_ALL) ?
546               VRNA_CONVERT_OUTPUT_HP
547             | VRNA_CONVERT_OUTPUT_STACK
548             | VRNA_CONVERT_OUTPUT_MM_HP
549             | VRNA_CONVERT_OUTPUT_MM_INT
550             | VRNA_CONVERT_OUTPUT_MM_INT_1N
551             | VRNA_CONVERT_OUTPUT_MM_INT_23
552             | VRNA_CONVERT_OUTPUT_MM_MULTI
553             | VRNA_CONVERT_OUTPUT_MM_EXT
554             | VRNA_CONVERT_OUTPUT_DANGLE5
555             | VRNA_CONVERT_OUTPUT_DANGLE3
556             | VRNA_CONVERT_OUTPUT_INT_11
557             | VRNA_CONVERT_OUTPUT_INT_21
558             | VRNA_CONVERT_OUTPUT_INT_22
559             | VRNA_CONVERT_OUTPUT_BULGE
560             | VRNA_CONVERT_OUTPUT_INT
561             | VRNA_CONVERT_OUTPUT_ML
562             | VRNA_CONVERT_OUTPUT_MISC
563             | VRNA_CONVERT_OUTPUT_SPECIAL_HP
564             | VRNA_CONVERT_OUTPUT_NINIO
565             :
566               option_bits;
567
568   make_pair_matrix(); /* needed for special loop energy contributions */
569
570   fprintf(ofile,"## RNAfold parameter file v2.0\n");
571
572   if(options & VRNA_CONVERT_OUTPUT_STACK){
573     fprintf(ofile,"\n# %s\n", settype(S));
574     fprintf(ofile,"/*  CG    GC    GU    UG    AU    UA    @  */\n");
575     for (c=1; c<NBPAIRS+1; c++)
576       display_array(stack37_184[c]+1,NBPAIRS,NBPAIRS, ofile);
577     fprintf(ofile,"\n# %s\n", settype(S_H));
578     fprintf(ofile,"/*  CG    GC    GU    UG    AU    UA    @  */\n");
579     for (c=1; c<NBPAIRS+1; c++)
580       display_array(enthalpies_184[c]+1,NBPAIRS,NBPAIRS, ofile);
581   }
582
583   if(options & VRNA_CONVERT_OUTPUT_MM_HP){
584     fprintf(ofile,"\n# %s\n", settype(MMH));
585     { int i,k;
586       for (k=1; k<NBPAIRS+1; k++)
587         for (i=0; i<5; i++)
588           display_array(mismatchH37_184[k][i],5,5, ofile);
589     }
590     fprintf(ofile,"\n# %s\n", settype(MMH_H));
591     { int i,k;
592       for (k=1; k<NBPAIRS+1; k++)
593         for (i=0; i<5; i++)
594           display_array(mism_H_184[k][i],5,5, ofile);
595     }
596   }
597
598   if(options & VRNA_CONVERT_OUTPUT_MM_INT){
599     fprintf(ofile,"\n# %s\n", settype(MMI));
600     { int i,k;
601       for (k=1; k<NBPAIRS+1; k++)
602         for (i=0; i<5; i++)
603           display_array(mismatchI37_184[k][i],5,5, ofile);
604     }
605     fprintf(ofile,"\n# %s\n", settype(MMI_H));
606     { int i,k;
607       for (k=1; k<NBPAIRS+1; k++)
608         for (i=0; i<5; i++)
609           display_array(mism_H_184[k][i],5,5, ofile);
610     }
611   }
612
613   if(options & VRNA_CONVERT_OUTPUT_MM_INT_1N){
614     fprintf(ofile,"\n# %s\n", settype(MMI1N));
615     { int i,k;
616       for (k=1; k<NBPAIRS+1; k++)
617         for (i=0; i<5; i++)
618           display_array(mismatchI37_184[k][i],5,5, ofile);
619     }
620     fprintf(ofile,"\n# %s\n", settype(MMI1N_H));
621     { int i,k;
622     for (k=1; k<NBPAIRS+1; k++)
623       for (i=0; i<5; i++)
624         display_array(mism_H_184[k][i],5,5, ofile);
625     }
626   }
627
628   if(options & VRNA_CONVERT_OUTPUT_MM_INT_23){
629     fprintf(ofile,"\n# %s\n", settype(MMI23));
630     { int i,k;
631       for (k=1; k<NBPAIRS+1; k++)
632         for (i=0; i<5; i++)
633           display_array(mismatchI37_184[k][i],5,5, ofile);
634     }
635     fprintf(ofile,"\n# %s\n", settype(MMI23_H));
636     { int i,k;
637     for (k=1; k<NBPAIRS+1; k++)
638       for (i=0; i<5; i++)
639         display_array(mism_H_184[k][i],5,5, ofile);
640     }
641   }
642
643   if(options & VRNA_CONVERT_OUTPUT_MM_MULTI){
644     fprintf(ofile,"\n# %s\n", settype(MMM));
645     fprintf(ofile,"/*  @     A     C     G     U   */\n");
646     { int i,j,k;
647       int bla[5];
648       for (k=1; k<NBPAIRS+1; k++)
649         for (i=0; i<5; i++){
650           for(j=0;j<5; j++)
651             bla[j] = ((dangle5_37_184[k][i] == INF) ? 0 : dangle5_37_184[k][i]) + ((dangle3_37_184[k][j] == INF) ? 0 : dangle3_37_184[k][j]);
652           display_array(bla,5,5, ofile);
653         }
654     }
655     fprintf(ofile,"\n# %s\n", settype(MMM_H));
656     fprintf(ofile,"/*  @     A     C     G     U   */\n");
657     { int i,j,k,bla[5];
658       for (k=1; k<NBPAIRS+1; k++)
659         for (i=0; i<5; i++){
660           for(j=0;j<5; j++)
661             bla[j] = ((dangle5_H_184[k][i] == INF) ? 0 : dangle5_H_184[k][i]) + ((dangle3_H_184[k][j] == INF) ? 0 : dangle3_H_184[k][j]);
662           display_array(bla,5,5, ofile);
663         }
664     }
665   }
666
667   if(options & VRNA_CONVERT_OUTPUT_MM_EXT){
668     fprintf(ofile,"\n# %s\n", settype(MME));
669     fprintf(ofile,"/*  @     A     C     G     U   */\n");
670     { int i,j,k;
671       int bla[5];
672       for (k=1; k<NBPAIRS+1; k++)
673         for (i=0; i<5; i++){
674           for(j=0;j<5; j++)
675             bla[j] = ((dangle5_37_184[k][i] == INF) ? 0 : dangle5_37_184[k][i]) + ((dangle3_37_184[k][j] == INF) ? 0 : dangle3_37_184[k][j]);
676           display_array(bla,5,5, ofile);
677         }
678     }
679     fprintf(ofile,"\n# %s\n", settype(MME_H));
680     fprintf(ofile,"/*  @     A     C     G     U   */\n");
681     { int i,j,k,bla[5];
682       for (k=1; k<NBPAIRS+1; k++)
683         for (i=0; i<5; i++){
684           for(j=0;j<5; j++)
685             bla[j] = ((dangle5_37_184[k][i] == INF) ? 0 : dangle5_H_184[k][i]) + ((dangle3_H_184[k][j] == INF) ? 0 : dangle3_H_184[k][j]);
686           display_array(bla,5,5, ofile);
687         }
688     }
689   }
690
691   if(options & VRNA_CONVERT_OUTPUT_DANGLE5){
692     fprintf(ofile,"\n# %s\n", settype(D5));
693     fprintf(ofile,"/*  @     A     C     G     U   */\n");
694     for (c=1; c<NBPAIRS+1; c++)
695       display_array(dangle5_37_184[c], 5, 5, ofile);
696     fprintf(ofile,"\n# %s\n", settype(D5_H));
697     fprintf(ofile,"/*  @     A     C     G     U   */\n");
698     for (c=1; c<NBPAIRS+1; c++)
699       display_array(dangle5_H_184[c], 5, 5, ofile);
700   }
701
702   if(options & VRNA_CONVERT_OUTPUT_DANGLE3){
703     fprintf(ofile,"\n# %s\n", settype(D3));
704     fprintf(ofile,"/*  @     A     C     G     U   */\n");
705     for (c=1; c<NBPAIRS+1; c++)
706       display_array(dangle3_37_184[c], 5, 5, ofile);
707     fprintf(ofile,"\n# %s\n", settype(D3_H));
708     fprintf(ofile,"/*  @     A     C     G     U   */\n");
709     for (c=1; c<NBPAIRS+1; c++)
710       display_array(dangle3_H_184[c], 5, 5, ofile);
711   }
712
713   if(options & VRNA_CONVERT_OUTPUT_INT_11){
714     /* don't print "no pair" entries for interior loop arrays */
715     fprintf(ofile,"\n# %s\n", settype(INT11));
716     { int i,k,l;
717       for (k=1; k<NBPAIRS+1; k++)
718         for (l=1; l<NBPAIRS+1; l++){
719           fprintf(ofile, "/* %2s..%2s */\n", pnames[k], pnames[l]);
720           for (i=0; i<5; i++)
721             display_array(int11_37_184[k][l][i], 5, 5, ofile);
722         }
723     }
724     fprintf(ofile,"\n# %s\n", settype(INT11_H));
725     { int i,k,l;
726       for (k=1; k<NBPAIRS+1; k++)
727         for (l=1; l<NBPAIRS+1; l++){
728           fprintf(ofile, "/* %2s..%2s */\n", pnames[k], pnames[l]);
729           for (i=0; i<5; i++)
730             display_array(int11_H_184[k][l][i],5,5, ofile);
731         }
732     }
733   }
734
735   if(options & VRNA_CONVERT_OUTPUT_INT_21){
736     fprintf(ofile,"\n# %s\n", settype(INT21));
737     { int p1, p2, i, j;
738       for (p1=1; p1<NBPAIRS+1; p1++)
739         for (p2=1; p2<NBPAIRS+1; p2++)
740           for (i=0; i<5; i++){
741             fprintf(ofile, "/* %2s.%c..%2s */\n", pnames[p1], bnames[i], pnames[p2]);
742             for (j=0; j<5; j++)
743               display_array(int21_37_184[p1][p2][i][j],5,5, ofile);
744           }
745     }
746     fprintf(ofile,"\n# %s\n", settype(INT21_H));
747     { int p1, p2, i, j;
748       for (p1=1; p1<NBPAIRS+1; p1++)
749         for (p2=1; p2<NBPAIRS+1; p2++)
750           for (i=0; i<5; i++){
751             fprintf(ofile, "/* %2s.%c..%2s */\n", pnames[p1], bnames[i], pnames[p2]);
752             for (j=0; j<5; j++)
753               display_array(int21_H_184[p1][p2][i][j],5,5, ofile);
754           }
755     }
756   }
757
758   if(options & VRNA_CONVERT_OUTPUT_INT_22){
759     fprintf(ofile,"\n# %s\n", settype(INT22));
760     { int p1, p2, i, j, k;
761       for (p1=1; p1<NBPAIRS; p1++)
762         for (p2=1; p2<NBPAIRS; p2++)
763           for (i=1; i<5; i++)
764             for (j=1; j<5; j++){
765               fprintf(ofile, "/* %2s.%c%c..%2s */\n", pnames[p1], bnames[i], bnames[j], pnames[p2]);
766               for (k=1; k<5; k++)
767                 display_array(int22_37_184[p1][p2][i][j][k]+1,4,5, ofile);
768             }
769     }
770     fprintf(ofile,"\n# %s\n", settype(INT22_H));
771     { int p1, p2, i, j, k;
772       for (p1=1; p1<NBPAIRS; p1++)
773         for (p2=1; p2<NBPAIRS; p2++)
774           for (i=1; i<5; i++)
775             for (j=1; j<5; j++){
776               fprintf(ofile, "/* %2s.%c%c..%2s */\n", pnames[p1], bnames[i], bnames[j], pnames[p2]);
777               for (k=1; k<5; k++)
778                 display_array(int22_H_184[p1][p2][i][j][k]+1,4,5, ofile);
779             }
780     }
781   }
782
783   if(options & VRNA_CONVERT_OUTPUT_HP){
784     fprintf(ofile,"\n# %s\n", settype(HP));
785     display_array(hairpin37_184, 31, 10, ofile);
786     /* we had no hairpin enthalpies before, so
787     *  we just pretend to have had some with value 0
788     */
789     fprintf(ofile,"\n# %s\n", settype(HP_H));
790     {
791       fprintf(ofile, "   INF   INF   INF");
792       for(c=4;c<=31; c++){
793         fprintf(ofile, "%6d", 0);
794         if(c%10 == 0) fprintf(ofile, "\n");
795       }
796     }
797     fprintf(ofile,"\n");
798   }
799
800   if(options & VRNA_CONVERT_OUTPUT_BULGE){
801     fprintf(ofile,"\n# %s\n", settype(B));
802     display_array(bulge37_184, 31, 10, ofile);
803
804     /* we had no bulge enthalpies before, so
805     *  we just pretend to have had some with value 0
806     */
807     fprintf(ofile,"\n# %s\n", settype(B_H));
808     {
809       fprintf(ofile, "   INF");
810       for(c=2;c<=31; c++){
811         fprintf(ofile, "%6d", 0);
812         if(c%10 == 0) fprintf(ofile, "\n");
813       }
814     }
815     fprintf(ofile,"\n");
816   }
817
818   if(options & VRNA_CONVERT_OUTPUT_INT){
819     fprintf(ofile,"\n# %s\n", settype(IL));
820     display_array(internal_loop37_184, 31, 10, ofile);
821
822     /* we had no internal_loop enthalpies before, so
823     *  we just pretend to have had some with value 0
824     */
825     fprintf(ofile,"\n# %s\n", settype(IL_H));
826     {
827       fprintf(ofile, "   INF   INF   INF   INF");
828       for(c=5;c<=31; c++){
829         fprintf(ofile, "%6d", 0);
830         if(c%10 == 0) fprintf(ofile, "\n");
831       }
832     }
833     fprintf(ofile,"\n");
834     fprintf(ofile,"\n# %s\n"
835                   "/* Ninio = MIN(max, m*|n1-n2| */\n"
836                   "/*\t    m\t  m_dH     max  */\n"
837                   "\t%6d\t%6d\t%6d\n", settype(NIN), F_ninio37_184[2], 0, MAX_NINIO_184);
838   }
839
840   if(options & VRNA_CONVERT_OUTPUT_ML){
841     fprintf(ofile,"\n# %s\n", settype(ML));
842     fprintf(ofile,"/* F = cu*n_unpaired + cc + ci*loop_degree (+TermAU) */\n");
843     fprintf(ofile,"/*\t    cu\t cu_dH\t    cc\t cc_dH\t    ci\t ci_dH  */\n");
844     fprintf(ofile,"\t%6d\t%6d\t%6d\t%6d\t%6d\t%6d\n", ML_BASE37_184, 0, ML_closing37_184, 0, ML_intern37_184, 0);
845   }
846
847   if(options & VRNA_CONVERT_OUTPUT_MISC){
848     fprintf(ofile,"\n# %s\n", settype(MISC));
849     fprintf(ofile,"/* all parameters are pairs of 'energy enthalpy' */\n");
850     fprintf(ofile,"/*    DuplexInit     TerminalAU   LXC  */\n");
851     fprintf(ofile,"   %6d %6d %6d %6d   %3.6f %6d\n", DuplexInit_184, 0, TerminalAU_184, 0, lxc37_184, 0);
852   }
853
854   if(options & VRNA_CONVERT_OUTPUT_SPECIAL_HP){
855     fprintf(ofile,"\n# %s\n", settype(TRI));
856     {
857       int base_en = hairpin37_184[3];
858       int base_dH = TETRA_ENTH37_184;
859       for (c=0; c< (int)strlen(Triloops_184)/6; c++){
860         int en = base_en;
861         char bla[5];
862         strncpy(bla, Triloops_184+c*6, 5);
863         int type = pair[(short)encode_char(toupper(bla[0]))][(short)encode_char(toupper(bla[4]))];
864         if(type > 2) en += TerminalAU_184;
865         fprintf(ofile,"\t%.5s %6d %6d\n", Triloops_184+c*6, Triloop_E37_184[c] + en, base_dH);
866       }
867     }
868
869     /* since the old hairpin loop function treated the tabulated tetraloop energy as bonus
870     *  and the new one takes this tabulated energy as a total energy, we have to compute some
871     *  things now...
872     */
873     fprintf(ofile,"\n# %s\n", settype(TL));
874     {
875       int base_en = hairpin37_184[4];
876       int base_dH = TETRA_ENTH37_184;
877       for (c=0; c< (int)strlen(Tetraloops_184)/7; c++){
878         char bla[6];
879         int en = base_en;
880         int dH = base_dH;
881         strncpy(bla, Tetraloops_184+c*7, 6);
882         short si  = (short)encode_char(toupper(bla[1]));
883         short sj  = (short)encode_char(toupper(bla[4]));
884         int type  = pair[(short)encode_char(toupper(bla[0]))][(short)encode_char(toupper(bla[5]))];
885         en   += mismatchH37_184[type][si][sj];
886         dH   += mism_H_184[type][si][sj];
887         fprintf(ofile,"\t%.6s %6d %6d\n", Tetraloops_184+c*7, en + TETRA_ENERGY37_184[c], dH);
888       }
889     }
890     fprintf(ofile,"\n# %s\n", settype(HEX));
891     {
892       fprintf(ofile, "\n");
893     }
894   }
895
896   fprintf(ofile, "\n# %s\n", settype(QUIT));
897 }
898
899 PRIVATE void check_symmetry(void) {
900   int i,j,k,l;
901
902   for (i=0; i<=NBPAIRS; i++)
903     for (j=0; j<=NBPAIRS; j++)
904       if (stack37_184[i][j] != stack37_184[j][i])
905         fprintf(stderr, "WARNING: stacking energies not symmetric\n");
906
907   for (i=0; i<=NBPAIRS; i++)
908     for (j=0; j<=NBPAIRS; j++)
909       if (enthalpies_184[i][j] != enthalpies_184[j][i])
910         fprintf(stderr, "WARNING: stacking enthalpies not symmetric\n");
911
912
913   /* interior 1x1 loops */
914   for (i=0; i<=NBPAIRS; i++)
915     for (j=0; j<=NBPAIRS; j++)
916       for (k=0; k<5; k++)
917         for (l=0; l<5; l++)
918           if (int11_37_184[i][j][k][l] != int11_37_184[j][i][l][k])
919             fprintf(stderr, "WARNING: int11 energies not symmetric\n");
920
921   for (i=0; i<=NBPAIRS; i++)
922     for (j=0; j<=NBPAIRS; j++)
923       for (k=0; k<5; k++)
924         for (l=0; l<5; l++)
925           if (int11_H_184[i][j][k][l] != int11_H_184[j][i][l][k])
926             fprintf(stderr, "WARNING: int11 enthalpies not symmetric\n");
927
928   /* interior 2x2 loops */
929   for (i=0; i<=NBPAIRS; i++)
930     for (j=0; j<=NBPAIRS; j++)
931       for (k=0; k<5; k++)
932         for (l=0; l<5; l++) {
933           int m,n;
934           for (m=0; m<5; m++)
935             for (n=0; n<5; n++)
936               if (int22_37_184[i][j][k][l][m][n] != int22_37_184[j][i][m][n][k][l])
937                 fprintf(stderr, "WARNING: int22 energies not symmetric\n");
938         }
939
940   for (i=0; i<=NBPAIRS; i++)
941     for (j=0; j<=NBPAIRS; j++)
942       for (k=0; k<5; k++)
943         for (l=0; l<5; l++) {
944           int m,n;
945           for (m=0; m<5; m++)
946             for (n=0; n<5; n++)
947               if (int22_H_184[i][j][k][l][m][n] != int22_H_184[j][i][m][n][k][l])
948                 fprintf(stderr, "WARNING: int22 enthalpies not symmetric: %d %d %d %d %d %d\n", i,j,k,l,m,n);
949         }
950 }