Replace Progs/RNAalifold with x64 binary and add all other programs
[jabaws.git] / binaries / src / ViennaRNA / Kinfold / globals.c
1 /*
2   Last changed Time-stamp: <2010-06-24 17:15:16 ivo>
3   c  Christoph Flamm and Ivo L Hofacker
4   {xtof,ivo}@tbi.univie.ac.at
5   Kinfold: $Name:  $
6   $Id: globals.c,v 1.8 2008/10/07 09:03:14 ivo Exp $
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <assert.h>
13 #include <errno.h>
14 #include <getopt.h>
15 #include "utils.h"
16 #include "fold_vars.h"
17 #include "globals.h"
18 #include "cmdline.h"
19
20 /* forward declarations privat functions */
21 static void ini_globs(void);
22 static void ini_gtoggles (void);
23 static void ini_gvars(void);
24 static void ini_garrays (void);
25 static void usage(int status);
26 static void display_settings(void);
27 static void display_fileformat(void);
28 static char *verbose(int optval, const char *which);
29 static int process_options (int argc, char *argv[]);
30 static void process_options_gg (int argc, char *argv[]);
31
32 static const char *costring(const char *str);
33
34 static char UNUSED rcsid[] ="$Id: globals.c,v 1.8 2008/10/07 09:03:14 ivo Exp $";
35 #define MAXMSG 8
36 static char msg[MAXMSG][60] =
37 {{"off"},
38  {"on"},
39  {"current time"},
40  {"take from input file"},
41  {"open chain"},
42  {"mfe structure of sequence"},
43  {"ViennaRNA-Package-1.4 defaults"},
44  {""}
45 };
46
47 static struct option const long_options[] =
48 { {"dangle",  required_argument, 0, 0},
49   {"Temp",    required_argument, 0, 0},
50   {"Par",     required_argument, 0, 0},
51   {"phi",     required_argument, 0, 0},
52   {"pbounds", required_argument, 0, 0},
53   {"logML",   no_argument,       &GTV.logML, 0},
54   {"noShift", no_argument,       &GTV.noShift, 1},
55   {"noLP",    no_argument,       &GTV.noLP, 1},
56   {"seed",    required_argument, 0, 0},
57   {"time",    required_argument, 0, 0},
58   {"num",     required_argument, 0, 0},
59   {"start",   no_argument,       &GTV.start, 1},
60   {"stop",    no_argument,       &GTV.stop, 1},
61   {"fpt",     no_argument,       &GTV.fpt, 0},
62   {"met",      no_argument,       &GTV.mc, 1},
63   {"grow",    required_argument, 0,  0},
64   {"glen",    required_argument, 0,  0},
65   {"log",     required_argument, 0,  0},
66   {"silent",  no_argument,       0,  0},
67   {"lmin",    no_argument,       &GTV.lmin, 1},
68   {"cut",     required_argument, 0, 0},
69   {"help",    no_argument,       0, 'h'},
70   {"verbose", no_argument,       0, 0},
71   {NULL, 0, NULL, 0}
72 };
73
74 /**/
75 void decode_switches(int argc, char *argv[]) {
76   ini_globs();
77   strcpy(GAV.ProgramName, argv[0]);
78   process_options_gg(argc, argv);
79 }
80
81 /**/
82 void clean_up_globals(void) {
83   int i;
84   free(GAV.ProgramName);
85   free(GAV.BaseName);
86   free(GAV.farbe);
87   free(GAV.farbe_full);
88   free(GAV.startform);
89   free(GAV.currform);
90   free(GAV.prevform);
91   for (i = 0; i < GSV.maxS; i++) free(GAV.stopform[i]);
92   free(GAV.stopform);
93   free(GAV.sE);
94 }
95
96 /**/
97 static void usage(int status) {
98   fprintf(stderr, "\n%s - Kinetic Folding Program for Nucleic Acids -\n",
99           GAV.ProgramName);
100   fprintf(stderr, "Usage: %s [OPTION] < FILE\n", GAV.ProgramName);
101   fprintf(stderr,
102           "Options:\n"
103           " EnergyModel\n"
104           "  --dangle <0|1|2>  set dangling end model to (non|normal|double)\n"
105           "  --Temp <float>    set simulation temperature to <float>\n"
106           "  --Par <string>    use energy-parameter-file <string>\n"
107           "  --logML           use linear multiloop-function not logarithmic\n"
108           " MoveSet\n"
109           "  --noShift         turn off shift-moves\n"
110           "  --noLP            forbit structures with isolated base-pairs\n"
111           " Simulation\n"
112           "  --seed <int=int=int>  set random seed to <int=int=int>\n"
113           "  --time <float>        set maxtime of simulation to <float>\n"
114           "  --num <int>           set number of simulations to <int>\n"
115           "  --start               set start structure\n"
116           "  --stop                set stop structure(s)\n"
117           "  --met                 use Metropolis rule not Kawasaki rule\n"
118           "  --fpt                 stop stop structure(s) is reached\n"
119           "  --grow <float>        grow chain every <float> time steps\n"
120           "  --phi <double>        set phi value to <double>\n"
121           "  --pbounds <d1=d2=d3>  set phi_min to d1\n"
122           "                            phi_inc to d2\n"
123           "                            phi_max to d2\n"
124           "                            (d? is a double value)\n"
125           " Output\n"
126           "  --log <string>  set basename of log-file to <string>\n"
127           "  --err <string>  set basename of error-log-file to <string>\n"
128           "  --silent        no output to stdout\n"
129           "  --verbose       more information to stdout\n"
130           "  --lmin          output only local minima to stdout\n"
131           "  --cut <float>   output structures with E <= <float> to stdout\n");
132   display_settings();
133   display_fileformat();
134   exit (status);
135 }
136
137 /**/
138 static void display_fileformat(void) {
139   fprintf(stderr,
140           "Input File Format:\n"
141           "1st line sequence\n"
142           "2nd line start structure (if option --start is used)\n"
143           "following lines stop structures\n\n");
144 }
145
146 /**/
147 void log_prog_params(FILE *FP) {
148     fprintf( FP,
149              "#<\n#Date: %s"
150              "#EnergyModel: dangle=%d Temp=%.1f logML=%s Par=%s\n"
151              "#MoveSet: noShift=%s noLP=%s\n"
152              "#Simulation: num=%d time=%.2f seed=%s fpt=%s mc=%s\n"
153              "#Simulation: phi=%g pbounds=%s\n"
154              "#Output: log=%s silent=%s lmin=%s cut=%.2f\n",
155              time_stamp(),
156              GTV.dangle,
157              GSV.Temp,
158              verbose(GTV.logML, "logML"),
159              GAV.ParamFile,
160              verbose(GTV.noShift, NULL),
161              verbose(GTV.noLP, NULL),
162              GSV.num,
163              GSV.time,
164              verbose(GTV.seed, "seed"),
165              verbose(GTV.fpt, NULL),
166              verbose(GTV.mc, "met"),
167              GSV.phi,
168              verbose(GTV.phi, "pbounds"),
169              GAV.BaseName,
170              verbose(GTV.silent, NULL),
171              verbose(GTV.lmin, NULL),
172              GSV.cut);
173     fflush(FP);
174 }
175
176 /**/
177 void log_start_stop(FILE *FP) {
178   int i;
179   fprintf(FP, "#%s\n#%s (%6.2f)\n", costring(GAV.farbe),
180           costring(GAV.startform), GSV.startE);
181   for (i = 0; i < GSV.maxS; i++) {
182     fprintf(FP, "#%s (%6.2f) X%02d\n", costring(GAV.stopform[i]), GAV.sE[i], i+1);
183   }
184   fprintf(FP, "(%-5hu %5hu %5hu)", GAV.subi[0], GAV.subi[1], GAV.subi[2]);
185   costring(NULL);
186   fflush(FP);
187 }
188
189 /**/
190 static void display_settings(void) {
191   fprintf(stderr,
192           "Default Settings:\n"
193           " EnergyModel\n"
194           "  --dangle  = %d\n"
195           "  --Temp    = %.2f\n"
196           "  --Par     = %s\n"
197           "  --logML   = %s\n"
198           " MoveSet\n"
199           "  --noShift = %s\n"
200           "  --noLP    = %s\n"
201           " Simulation\n"
202           "  --seed    = %s\n"
203           "  --time    = %.1f\n"
204           "  --phi     = %g\n"
205           "  --pbounds = %s\n"
206           "  --num     = %d\n"
207           "  --start   = %s\n"
208           "  --stop    = %s\n"
209           "  --met     = %s\n"
210           "  --fpt     = %s\n"
211           " Output\n"
212           "  --log     = %s\n"
213           "  --silent  = %s\n"
214           "  --verbose = %s\n"
215           "  --lmin    = %s\n"
216           "  --cut     = %.2f\n",
217           GTV.dangle,
218           GSV.Temp,
219           GAV.ParamFile,
220           verbose(GTV.logML, "logML"),
221           verbose(GTV.noShift, NULL),
222           verbose(GTV.noLP, NULL),
223           verbose(GTV.seed, "seed"),
224           GSV.time,
225           GSV.phi,
226           verbose(GTV.phi, "pbounds"),
227           GSV.num,
228           verbose(GTV.start, "start"),
229           verbose(GTV.stop, "stop"),
230           verbose(GTV.mc, "met"),
231           verbose(GTV.fpt, NULL),
232           GAV.BaseName,
233           verbose(GTV.silent, NULL),
234           verbose(GTV.verbose, NULL),
235           verbose(GTV.lmin, NULL),
236           GSV.cut);
237 }
238
239 /**/
240 static char *verbose (int optval, const char *which) {
241   if (which == NULL) return msg[optval];
242   else {
243     if ( strcmp(which, "seed") == 0 ) {
244       if (optval == 0 ) return "clock";
245       else {
246         sprintf(msg[MAXMSG - 1],
247                 "%hu %hu %hu", GAV.subi[0], GAV.subi[1],GAV.subi[2]);
248         return msg[MAXMSG - 1];
249       }
250     }
251
252     if (strcmp(which, "pbounds") == 0) {
253       sprintf(msg[MAXMSG - 1],
254               "%g %g %g",
255               GAV.phi_bounds[0], GAV.phi_bounds[1], GAV.phi_bounds[2]);
256       return msg[MAXMSG - 1];
257     }
258
259     if ( strcmp(which, "met") == 0 ) {
260       if ( optval == 0 ) return "Kawasaki";
261       else return "Metropolis";
262     }
263
264     if ( strcmp(which, "logML") == 0 ) {
265       if ( optval == 0 ) return "linear";
266       else return "logarithmic";
267     }
268
269     if ( strcmp(which, "start") == 0 ) {
270       if ( optval == 0 ) return "OpenChain";
271       else return "input file";
272     }
273
274     if ( strcmp(which, "stop") == 0 ) {
275       if ( optval == 0 ) return "Mfe";
276       else return "input file";
277     }
278   }
279   return "hae?";
280 }
281
282 /**/
283 static void ini_globs (void) {
284   ini_gtoggles();
285   ini_gvars();
286   ini_garrays();
287 }
288
289 static void process_options_gg (int argc, char *argv[]) {
290   struct gengetopt_args_info args_info;
291   if (cmdline_parser (argc, argv, &args_info) != 0)
292     exit(1) ;
293   if (args_info.help_given) cmdline_parser_print_help();
294   if (args_info.full_help_given) cmdline_parser_print_full_help();
295
296   GTV.dangle = args_info.dangle_arg;
297   GSV.Temp = args_info.Temp_arg;
298   strncpy(GAV.ParamFile,255,args_info.Par_arg);
299   GTV.Par = args_info.Par_given;
300   GTV.logML = args_info.logML_flag;
301
302   GTV.noShift = args_info.noShift_flag;
303   GTV.noLP = args_info.noLP_flag;
304
305   GTV.start= args_info.start_flag;
306   GTV.stop = args_info.stop_flag;
307   GTV.mc   = args_info.met_flag;
308   GTV.lmin = args_info.lmin_flag;
309
310   GTV.verbose = args_info.verbose_flag;
311   GTV.silent = (GTV.verbose==0)?
312     args_info.silent_flag : 0;
313   if (args_info.seed_given)
314     if (sscanf(args_info.seed_arg, "%hu=%hu=%hu",
315                &GAV.subi[0], &GAV.subi[1], &GAV.subi[2]) != 3)
316       usage(EXIT_FAILURE);
317     else GTV.seed = 1;
318
319   /* laplace stuff */
320   if (args_info.phi_given) {
321     if (args_info.phi_arg>0) {
322       GTV.phi = 1;
323       GSV.phi = args_info.phi_arg;
324     }
325     else {
326       fprintf(stderr, "Value of --phi must be > 0 >%lf<\n", args_info.phi_arg);
327       exit(EXIT_FAILURE);
328     }
329   }
330   if (args_info.pbounds_given) {
331     if (sscanf(args_info.pbounds_arg, "%g=%g=%g",
332                &GAV.phi_bounds[0],
333                &GAV.phi_bounds[1],
334                &GAV.phi_bounds[2]) == 0)
335       usage(EXIT_FAILURE);
336     else
337       GTV.phi = 1;
338     /* check if values are proper */
339     if (GAV.phi_bounds[0] > GAV.phi_bounds[2]
340         || GAV.phi_bounds[1] > GAV.phi_bounds[2]) {
341       fprintf(stderr,
342               "Unmet requirements for pbounds:\n"
343               "phi_min < phi_max && phi_inc < phi_max\n"
344               "phi_min: %g phi_inc: %g phi_max: %g\n",
345               GAV.phi_bounds[0], GAV.phi_bounds[1], GAV.phi_bounds[2]);
346       exit(EXIT_FAILURE);
347     }
348   }
349   GSV.time = args_info.time_arg;
350   GSV.num = args_info.num_arg;
351   strncpy(GAV.BaseName, args_info.log_arg, 255);
352   GSV.cut = args_info.cut_arg;
353   GSV.grow = args_info.grow_arg;
354   GSV.glen = args_info.glen_arg;
355   GTV.lmin = args_info.lmin_flag;
356   GTV.fpt  = args_info.fpt_flag;
357   cmdline_parser_free(&args_info);
358 }
359 /**/
360 static int process_options (int argc, char *argv[]) {
361   int c, itmp;
362   float ftmp;
363   double dtmp;
364   int option_index = 0;
365   while ((c = getopt_long (argc, argv, "h",
366                              long_options, &option_index)) != EOF) {
367       switch (c) {
368       case 0:
369         if (strcmp(long_options[option_index].name,"dangle")==0) {
370           itmp = -1;
371           if (sscanf(optarg, "%d", &itmp) == 0)
372             usage(EXIT_FAILURE);
373           else if (itmp == 0 || itmp == 1 || itmp == 2 || itmp == 3)
374             GTV.dangle = itmp;
375           else {
376             fprintf(stderr, "Value of --dangle must be 0|1|2 >%d<\n", itmp);
377             usage (EXIT_FAILURE);
378           }
379         }
380
381         if (strcmp(long_options[option_index].name,"Temp")==0) {
382           ftmp = -1.0;
383           if (sscanf(optarg, "%f", &ftmp) == 0)
384             usage(EXIT_FAILURE);
385           else if ( ftmp >= 0 )
386             GSV.Temp = ftmp;
387           else {
388             fprintf(stderr, "Value of --Temp must be >= 0 >%.2f<\n", ftmp);
389             usage (EXIT_FAILURE);
390           }
391         }
392
393         if (strcmp(long_options[option_index].name,"Par")==0) {
394           if (sscanf(optarg, "%s", GAV.ParamFile) == 0)
395             usage(EXIT_FAILURE);
396           else {
397             GTV.Par = 1;
398           }
399         }
400
401         if (strcmp(long_options[option_index].name,"silent")==0) {
402           GTV.silent = 1;
403           GTV.verbose = 0;
404         }
405
406         if (strcmp(long_options[option_index].name,"verbose")==0) {
407           if (GTV.silent == 0)
408             GTV.verbose = 1;
409         }
410
411         if (strcmp(long_options[option_index].name,"seed")==0) {
412           if (sscanf(optarg, "%hu=%hu=%hu",
413                      &GAV.subi[0], &GAV.subi[1], &GAV.subi[2]) == 0)
414             usage(EXIT_FAILURE);
415           else GTV.seed = 1;
416         }
417
418         /* laplace stuff */
419         if (strcmp(long_options[option_index].name,"pbounds")==0) {
420           if (sscanf(optarg, "%g=%g=%g",
421                      &GAV.phi_bounds[0],
422                      &GAV.phi_bounds[1],
423                      &GAV.phi_bounds[2]) == 0)
424             usage(EXIT_FAILURE);
425           else
426             GTV.phi = 1;
427           /* check if values are proper */
428           if (GAV.phi_bounds[0] > GAV.phi_bounds[2]
429               || GAV.phi_bounds[1] > GAV.phi_bounds[2]) {
430             fprintf(stderr,
431                     "Unmet requirements for pbounds:\n"
432                     "phi_min < phi_max && phi_inc < phi_max\n"
433                     "phi_min: %g phi_inc: %g phi_max: %g\n",
434                     GAV.phi_bounds[0], GAV.phi_bounds[1], GAV.phi_bounds[2]);
435             exit(EXIT_FAILURE);
436           }
437         }
438
439         if (strcmp(long_options[option_index].name,"time")==0) {
440           dtmp = -1.0;
441           if (sscanf(optarg, "%lf", &dtmp) == 0)
442             usage(EXIT_FAILURE);
443           else if ( dtmp > 0 )
444             GSV.time = dtmp;
445           else {
446             fprintf(stderr, "Value of --time must be > 0 >%lf<\n", dtmp);
447             usage(EXIT_FAILURE);
448           }
449         }
450
451         /* laplace stuff */
452         if (strcmp(long_options[option_index].name, "phi")==0) {
453           dtmp = -1.0;
454           if (sscanf(optarg, "%lf", &dtmp) == 0)
455             usage(EXIT_FAILURE);
456           else if ( dtmp > 0 ) {
457             GSV.phi = dtmp;
458             GTV.phi = 1;
459           }
460           else {
461             fprintf(stderr, "Value of --phi must be > 0 >%lf<\n", dtmp);
462             exit(EXIT_FAILURE);
463           }
464         }
465
466         if (strcmp(long_options[option_index].name,"num")==0) {
467           itmp = -1;
468           if (sscanf(optarg, "%d", &itmp) == 0)
469             usage(EXIT_FAILURE);
470           else if ( itmp > 0 )
471             GSV.num = itmp;
472           else {
473             fprintf(stderr, "Value of --num must be > 0 >%d<\n", itmp);
474             usage(EXIT_FAILURE);
475           }
476         }
477
478         if (strcmp(long_options[option_index].name,"log")==0)
479           if (sscanf(optarg, "%s", GAV.BaseName) == 0)
480             usage(EXIT_FAILURE);
481
482         if (strcmp(long_options[option_index].name,"cut")==0)
483           if (sscanf(optarg, "%f", &GSV.cut) == 0)
484             usage(EXIT_FAILURE);
485
486         if (strcmp(long_options[option_index].name,"grow")==0)
487           if (sscanf(optarg, "%lf", &GSV.grow) == 0)
488             usage(EXIT_FAILURE);
489
490         if (strcmp(long_options[option_index].name,"glen")==0)
491           if (sscanf(optarg, "%d", &GSV.glen) == 0)
492             usage(EXIT_FAILURE);
493
494         break;
495
496       case 'h':
497         usage (0);
498       default:
499         usage (EXIT_FAILURE);
500       }
501   }
502   return optind;
503 }
504
505 /**/
506 static void ini_gtoggles(void) {
507   GTV.Par = 0;
508   GTV.seed = 0;
509   GTV.dangle = 2;
510   GTV.logML = 1;
511   GTV.noLP = 0;
512   GTV.noShift = 0;
513   GTV.start = 0;
514   GTV.stop = 0;
515   GTV.silent = 0;
516   GTV.phi = 0;
517   GTV.verbose = 0;
518   GTV.lmin = 0;
519   GTV.fpt = 1;
520   GTV.mc = 0;
521 }
522
523 /**/
524 static void ini_gvars(void) {
525   GSV.len = 0;
526   GSV.num = 1;
527   GSV.maxS = 99;
528   GSV.cut = 20;
529   GSV.Temp = 37.0;
530   GSV.startE = 0.0;
531   GSV.stopE = 0.0;
532   GSV.currE = 0.0;
533   GSV.time = 500.0;
534   GSV.phi = 1.0;
535   GSV.simTime = 0.0;
536   GSV.glen = 15;
537 }
538
539 /**/
540 static void ini_garrays(void) {
541   GAV.ProgramName = (char *)calloc((size_t)256, sizeof(char));
542   assert(GAV.ProgramName != NULL);
543   GAV.ParamFile   = (char *)calloc((size_t)256, sizeof(char));
544   assert(GAV.ParamFile != NULL);
545   strcpy(GAV.ParamFile,"VRNA-1.4");
546   GAV.BaseName    = (char *)calloc((size_t)256, sizeof(char));
547   assert(GAV.BaseName != NULL);
548   strcpy(GAV.BaseName, "kinout");
549   GAV.stopform = (char **)calloc(GSV.maxS + 1, sizeof(char *));
550   assert(GAV.stopform != NULL);
551   GAV.farbe = NULL;
552   GAV.startform = NULL;
553   GAV.currform = NULL;
554   GAV.prevform = NULL;
555   GAV.phi_bounds[0] = 0.1;
556   GAV.phi_bounds[1] = 0.1;
557   GAV.phi_bounds[2] = 2.0;
558 }
559
560 static const char *costring(const char *str) {
561   static char* buffer=NULL;
562   static int size=0;
563   int n;
564   if ((str==NULL) && (buffer)) {
565     /* make it possible to free buffer */
566     free(buffer);
567     return NULL;
568   }
569   n=strlen(str);
570   if (n>size) {
571     size = n+2;
572     buffer = realloc(buffer, size);
573   }
574   if ((cut_point>0)&&(cut_point<=n)) {
575     strncpy(buffer, str, cut_point-1);
576     buffer[cut_point-1] = '&';
577     strncpy(buffer+cut_point, str+cut_point-1, n-cut_point+1);
578     buffer[n+1] = '\0';
579   } else {
580     strncpy(buffer, str, n+1);
581   }
582   return buffer;
583 }
584 /* End of file */