Add missing binaty and statis library
[jabaws.git] / binaries / src / ViennaRNA / Progs / RNAcofold_cmdl.c
1 /*
2   File autogenerated by gengetopt version 2.22.5
3   generated with the following command:
4   gengetopt -i RNAcofold.ggo --file-name=RNAcofold_cmdl --include-getopt --default-optional --func-name=RNAcofold_cmdline_parser --arg-struct-name=RNAcofold_args_info
5
6   The developers of gengetopt consider the fixed text that goes in all
7   gengetopt output files to be in the public domain:
8   we make no copyright claims on it.
9 */
10
11 /* If we use autoconf.  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #ifndef FIX_UNUSED
21 #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
22 #endif
23
24
25 #include "RNAcofold_cmdl.h"
26
27 const char *RNAcofold_args_info_purpose = "calculate secondary structures of two RNAs with dimerization";
28
29 const char *RNAcofold_args_info_usage = "Usage: RNAcofold [OPTIONS]...";
30
31 const char *RNAcofold_args_info_description = "The program works much like RNAfold, but allows to specify two RNA sequences \nwich are then allowed to form a dimer structure. RNA sequences are read from \nstdin in the usual format, i.e. each line of input corresponds to one sequence, \nexcept for lines starting with \">\" which contain the name of the next \nsequence.\nTo compute the hybrid structure of two molecules, the two sequences must be \nconcatenated using the \'&\' character as separator.\nRNAcofold can compute minimum free energy (mfe) structures, as well as \npartition function (pf) and base pairing probability matrix (using the -p \nswitch)\nSince dimer formation is concentration dependent, RNAcofold can be used to \ncompute equilibrium concentrations for all five monomer and (homo/hetero)-dimer \nspecies, given input concentrations for the monomers.\nOutput consists of the mfe structure in bracket notation as well as PostScript \nstructure plots and \"dot plot\" files containing the pair probabilities, see \nthe RNAfold man page for details. In the dot plots a cross marks the chain \nbreak between the two concatenated sequences.\nThe program will continue to read new sequences until a line consisting of the \nsingle character @ or an end of file condition is encountered.\n\n";
32
33 const char *RNAcofold_args_info_detailed_help[] = {
34   "  -h, --help                    Print help and exit",
35   "      --detailed-help           Print help, including all details and hidden \n                                  options, and exit",
36   "      --full-help               Print help, including hidden options, and exit",
37   "  -V, --version                 Print version and exit",
38   "\nGeneral Options:",
39   "  Below are command line options which alter the general behavior of this \n  program\n",
40   "  -C, --constraint              Calculate structures subject to constraints.\n                                    (default=off)",
41   "  The program reads first the sequence, then a string containing constraints on \n  the structure encoded with the symbols:\n\n  . (no constraint for this base)\n\n  | (the corresponding base has to be paired\n\n  x (the base is unpaired)\n\n  < (base i is paired with a base j>i)\n\n  > (base i is paired with a base j<i)\n\n  and matching brackets ( ) (base i pairs base j)\n\n  With the exception of \"|\", constraints will disallow all pairs conflicting \n  with the constraint. This is usually sufficient to enforce the constraint, \n  but occasionally a base may stay unpaired in spite of constraints. PF folding \n  ignores constraints of type \"|\".\n\n",
42   "      --noconv                  Do not automatically substitude nucleotide \n                                  \"T\" with \"U\"\n\n                                    (default=off)",
43   "      --noPS                    Do not produce postscript output\n\n                                    (default=off)",
44   "\nAlgorithms:",
45   "  Select additional algorithms which should be included in the calculations.\n  The Minimum free energy (MFE) and a structure representative are calculated \n  in any case.\n\n",
46   "  -p, --partfunc[=INT]          Calculate the partition function and base \n                                  pairing probability matrix in addition to the \n                                  mfe structure. Default is calculation of mfe \n                                  structure only.\n                                    (default=`1')",
47   "  In addition to the MFE structure we print a coarse representation of the pair \n  probabilities in form of a pseudo bracket notation, followed by the ensemble \n  free energy, as well as the centroid structure derived from the pair \n  probabilities together with its free energy and distance to the ensemble. \n  Finally it prints the frequency of the mfe structure, and the structural \n  diversity (mean distance between the structures in the ensemble).\n  See the description of pf_fold() and mean_bp_dist() and centroid() in the \n  RNAlib documentation for details.\n  Note that unless you also specify -d2 or -d0, the partition function and mfe \n  calculations will use a slightly different energy model. See the discussion \n  of dangling end options below.\n\n  An additionally passed value to this option changes the behavior of partition \n  function calculation:\n\n   In order to calculate the partition function but not the pair probabilities \n  use the -p0 option and save about\n   50% in runtime. This prints the ensemble free energy -kT ln(Z).\n\n",
48   "  -a, --all_pf                  Compute the partition function and free \n                                  energies not only of the hetero-dimer \n                                  consisting of the two input sequences (the \n                                  \"AB dimer\"), but also of the homo-dimers AA \n                                  and BB as well as A and B monomers.\n                                    (default=off)",
49   "  The output will contain the free energies for each of these species, as well \n  as 5 dot plots containing the conditional pair probabilities, called \n  ABname5.ps, AAname5.ps and so on. For later use, these dot plot files also \n  contain the free energy of the ensemble as a comment. Using -a automatically \n  toggles the -p option.\n\n",
50   "  -c, --concentrations          In addition to everything listed under the -a \n                                  option, read in initial monomer \n                                  concentrations and compute the expected \n                                  equilibrium concentrations of the 5 possible \n                                  species (AB, AA, BB, A, B).\n                                    (default=off)",
51   "  Start concentrations are read from stdin (unless the -f option is used) in \n  [mol/l], equilibrium concentrations are given realtive to the sum of the two \n  inputs. An arbitrary number of initial concentrations can be specified (one \n  pair of concentrations per line).\n\n",
52   "  -f, --concfile=filename       Specify a file with initial concentrations for \n                                  the to sequences.",
53   "  The table consits of arbitrary many lines with just two numbers (the \n  concentration of sequence A and B). This option will automatically toggle the \n  -c (and thus -a and -p) options (see above).\n\n",
54   "  -S, --pfScale=scaling factor  In the calculation of the pf use scale*mfe as \n                                  an estimate for the ensemble free energy \n                                  (used to avoid overflows).\n",
55   "  The default is 1.07, useful values are 1.0 to 1.2. Occasionally needed for \n  long sequences.\n  You can also recompile the program to use double precision (see the README \n  file).\n\n",
56   "      --bppmThreshold=<value>   Set the threshold for base pair probabilities \n                                  included in the postscript output\n                                    (default=`1e-5')",
57   "  By setting the threshold the base pair probabilities that are included in the \n  output can be varied. By default only those exceeding 1e-5 in probability \n  will be shown as squares in the dot plot. Changing the threshold to any other \n  value allows for increase or decrease of data.\n\n",
58   "  -g, --gquad                   Incoorporate G-Quadruplex formation into the \n                                  structure prediction algorithm\n                                    (default=off)",
59   "\nModel Details:",
60   "  -T, --temp=DOUBLE             Rescale energy parameters to a temperature of \n                                  temp C. Default is 37C.\n\n",
61   "  -4, --noTetra                 Do not include special stabilizing energies for \n                                  certain tetra-loops. Mostly for testing.\n\n                                    (default=off)",
62   "  -d, --dangles=INT             How to treat \"dangling end\" energies for \n                                  bases adjacent to helices in free ends and \n                                  multi-loops\n                                    (default=`2')",
63   "  \n  With -d1 only unpaired bases can participate in at most one dangling end, \n  this is the default for mfe folding but unsupported for the partition \n  function folding.\n\n  With -d2 this check is ignored, dangling energies will be added for the bases \n  adjacent to a helix on both sides in any case; this is the default for \n  partition function folding (-p).\n  The option -d0 ignores dangling ends altogether (mostly for debugging).\n  With -d3 mfe folding will allow coaxial stacking of adjacent helices in \n  multi-loops. At the moment the implementation will not allow coaxial stacking \n  of the two interior pairs in a loop of degree 3 and works only for mfe \n  folding.\n\n  Note that by default (as well as with -d1 and -d3) pf and mfe folding treat \n  dangling ends differently. Use -d2 in addition to -p to ensure that both \n  algorithms use the same energy model.\n\n",
64   "      --noLP                    Produce structures without lonely pairs \n                                  (helices of length 1).\n                                    (default=off)",
65   "  For partition function folding this only disallows pairs that can only occur \n  isolated. Other pairs may still occasionally occur as helices of length 1.\n\n",
66   "      --noGU                    Do not allow GU pairs\n\n                                    (default=off)",
67   "      --noClosingGU             Do not allow GU pairs at the end of helices\n\n                                    (default=off)",
68   "  -P, --paramFile=paramfile     Read energy parameters from paramfile, instead \n                                  of using the default parameter set.\n",
69   "  A sample parameter file should accompany your distribution.\n  See the RNAlib documentation for details on the file format.\n\n",
70   "      --nsp=STRING              Allow other pairs in addition to the usual \n                                  AU,GC,and GU pairs.\n",
71   "  Its argument is a comma separated list of additionally allowed pairs. If the \n  first character is a \"-\" then AB will imply that AB and BA are allowed \n  pairs.\n  e.g. RNAfold -nsp -GA  will allow GA and AG pairs. Nonstandard pairs are \n  given 0 stacking energy.\n\n",
72   "  -e, --energyModel=INT         Rarely used option to fold sequences from the \n                                  artificial ABCD... alphabet, where A pairs B, \n                                  C-D etc.  Use the energy parameters for GC \n                                  (-e 1) or AU (-e 2) pairs.\n\n",
73   "      --betaScale=DOUBLE        Set the scaling of the Boltzmann factors\n                                    (default=`1.')",
74   "  The argument provided with this option enables to scale the thermodynamic \n  temperature used in the Boltzmann factors independently from the temperature \n  used to scale the individual energy contributions of the loop types. The \n  Boltzmann factors then become exp(-dG/(kT*betaScale)) where k is the \n  Boltzmann constant, dG the free energy contribution of the state and T the \n  absolute temperature.\n\n",
75   "\nIf in doubt our program is right, nature is at fault.\nComments should be sent to rna@tbi.univie.ac.at.\n",
76     0
77 };
78 static void
79 init_full_help_array(void)
80 {
81   RNAcofold_args_info_full_help[0] = RNAcofold_args_info_detailed_help[0];
82   RNAcofold_args_info_full_help[1] = RNAcofold_args_info_detailed_help[1];
83   RNAcofold_args_info_full_help[2] = RNAcofold_args_info_detailed_help[2];
84   RNAcofold_args_info_full_help[3] = RNAcofold_args_info_detailed_help[3];
85   RNAcofold_args_info_full_help[4] = RNAcofold_args_info_detailed_help[4];
86   RNAcofold_args_info_full_help[5] = RNAcofold_args_info_detailed_help[5];
87   RNAcofold_args_info_full_help[6] = RNAcofold_args_info_detailed_help[6];
88   RNAcofold_args_info_full_help[7] = RNAcofold_args_info_detailed_help[8];
89   RNAcofold_args_info_full_help[8] = RNAcofold_args_info_detailed_help[9];
90   RNAcofold_args_info_full_help[9] = RNAcofold_args_info_detailed_help[10];
91   RNAcofold_args_info_full_help[10] = RNAcofold_args_info_detailed_help[11];
92   RNAcofold_args_info_full_help[11] = RNAcofold_args_info_detailed_help[12];
93   RNAcofold_args_info_full_help[12] = RNAcofold_args_info_detailed_help[14];
94   RNAcofold_args_info_full_help[13] = RNAcofold_args_info_detailed_help[16];
95   RNAcofold_args_info_full_help[14] = RNAcofold_args_info_detailed_help[18];
96   RNAcofold_args_info_full_help[15] = RNAcofold_args_info_detailed_help[20];
97   RNAcofold_args_info_full_help[16] = RNAcofold_args_info_detailed_help[22];
98   RNAcofold_args_info_full_help[17] = RNAcofold_args_info_detailed_help[24];
99   RNAcofold_args_info_full_help[18] = RNAcofold_args_info_detailed_help[25];
100   RNAcofold_args_info_full_help[19] = RNAcofold_args_info_detailed_help[26];
101   RNAcofold_args_info_full_help[20] = RNAcofold_args_info_detailed_help[27];
102   RNAcofold_args_info_full_help[21] = RNAcofold_args_info_detailed_help[28];
103   RNAcofold_args_info_full_help[22] = RNAcofold_args_info_detailed_help[30];
104   RNAcofold_args_info_full_help[23] = RNAcofold_args_info_detailed_help[32];
105   RNAcofold_args_info_full_help[24] = RNAcofold_args_info_detailed_help[33];
106   RNAcofold_args_info_full_help[25] = RNAcofold_args_info_detailed_help[34];
107   RNAcofold_args_info_full_help[26] = RNAcofold_args_info_detailed_help[36];
108   RNAcofold_args_info_full_help[27] = RNAcofold_args_info_detailed_help[38];
109   RNAcofold_args_info_full_help[28] = RNAcofold_args_info_detailed_help[39];
110   RNAcofold_args_info_full_help[29] = RNAcofold_args_info_detailed_help[41];
111   RNAcofold_args_info_full_help[30] = 0; 
112   
113 }
114
115 const char *RNAcofold_args_info_full_help[31];
116
117 static void
118 init_help_array(void)
119 {
120   RNAcofold_args_info_help[0] = RNAcofold_args_info_detailed_help[0];
121   RNAcofold_args_info_help[1] = RNAcofold_args_info_detailed_help[1];
122   RNAcofold_args_info_help[2] = RNAcofold_args_info_detailed_help[2];
123   RNAcofold_args_info_help[3] = RNAcofold_args_info_detailed_help[3];
124   RNAcofold_args_info_help[4] = RNAcofold_args_info_detailed_help[4];
125   RNAcofold_args_info_help[5] = RNAcofold_args_info_detailed_help[5];
126   RNAcofold_args_info_help[6] = RNAcofold_args_info_detailed_help[6];
127   RNAcofold_args_info_help[7] = RNAcofold_args_info_detailed_help[8];
128   RNAcofold_args_info_help[8] = RNAcofold_args_info_detailed_help[9];
129   RNAcofold_args_info_help[9] = RNAcofold_args_info_detailed_help[10];
130   RNAcofold_args_info_help[10] = RNAcofold_args_info_detailed_help[11];
131   RNAcofold_args_info_help[11] = RNAcofold_args_info_detailed_help[12];
132   RNAcofold_args_info_help[12] = RNAcofold_args_info_detailed_help[14];
133   RNAcofold_args_info_help[13] = RNAcofold_args_info_detailed_help[16];
134   RNAcofold_args_info_help[14] = RNAcofold_args_info_detailed_help[18];
135   RNAcofold_args_info_help[15] = RNAcofold_args_info_detailed_help[24];
136   RNAcofold_args_info_help[16] = RNAcofold_args_info_detailed_help[25];
137   RNAcofold_args_info_help[17] = RNAcofold_args_info_detailed_help[26];
138   RNAcofold_args_info_help[18] = RNAcofold_args_info_detailed_help[27];
139   RNAcofold_args_info_help[19] = RNAcofold_args_info_detailed_help[28];
140   RNAcofold_args_info_help[20] = RNAcofold_args_info_detailed_help[30];
141   RNAcofold_args_info_help[21] = RNAcofold_args_info_detailed_help[32];
142   RNAcofold_args_info_help[22] = RNAcofold_args_info_detailed_help[33];
143   RNAcofold_args_info_help[23] = RNAcofold_args_info_detailed_help[34];
144   RNAcofold_args_info_help[24] = RNAcofold_args_info_detailed_help[41];
145   RNAcofold_args_info_help[25] = 0; 
146   
147 }
148
149 const char *RNAcofold_args_info_help[26];
150
151 typedef enum {ARG_NO
152   , ARG_FLAG
153   , ARG_STRING
154   , ARG_INT
155   , ARG_DOUBLE
156 } RNAcofold_cmdline_parser_arg_type;
157
158 static
159 void clear_given (struct RNAcofold_args_info *args_info);
160 static
161 void clear_args (struct RNAcofold_args_info *args_info);
162
163 static int
164 RNAcofold_cmdline_parser_internal (int argc, char **argv, struct RNAcofold_args_info *args_info,
165                         struct RNAcofold_cmdline_parser_params *params, const char *additional_error);
166
167
168 static char *
169 gengetopt_strdup (const char *s);
170
171 static
172 void clear_given (struct RNAcofold_args_info *args_info)
173 {
174   args_info->help_given = 0 ;
175   args_info->detailed_help_given = 0 ;
176   args_info->full_help_given = 0 ;
177   args_info->version_given = 0 ;
178   args_info->constraint_given = 0 ;
179   args_info->noconv_given = 0 ;
180   args_info->noPS_given = 0 ;
181   args_info->partfunc_given = 0 ;
182   args_info->all_pf_given = 0 ;
183   args_info->concentrations_given = 0 ;
184   args_info->concfile_given = 0 ;
185   args_info->pfScale_given = 0 ;
186   args_info->bppmThreshold_given = 0 ;
187   args_info->gquad_given = 0 ;
188   args_info->temp_given = 0 ;
189   args_info->noTetra_given = 0 ;
190   args_info->dangles_given = 0 ;
191   args_info->noLP_given = 0 ;
192   args_info->noGU_given = 0 ;
193   args_info->noClosingGU_given = 0 ;
194   args_info->paramFile_given = 0 ;
195   args_info->nsp_given = 0 ;
196   args_info->energyModel_given = 0 ;
197   args_info->betaScale_given = 0 ;
198 }
199
200 static
201 void clear_args (struct RNAcofold_args_info *args_info)
202 {
203   FIX_UNUSED (args_info);
204   args_info->constraint_flag = 0;
205   args_info->noconv_flag = 0;
206   args_info->noPS_flag = 0;
207   args_info->partfunc_arg = 1;
208   args_info->partfunc_orig = NULL;
209   args_info->all_pf_flag = 0;
210   args_info->concentrations_flag = 0;
211   args_info->concfile_arg = NULL;
212   args_info->concfile_orig = NULL;
213   args_info->pfScale_orig = NULL;
214   args_info->bppmThreshold_arg = 1e-5;
215   args_info->bppmThreshold_orig = NULL;
216   args_info->gquad_flag = 0;
217   args_info->temp_orig = NULL;
218   args_info->noTetra_flag = 0;
219   args_info->dangles_arg = 2;
220   args_info->dangles_orig = NULL;
221   args_info->noLP_flag = 0;
222   args_info->noGU_flag = 0;
223   args_info->noClosingGU_flag = 0;
224   args_info->paramFile_arg = NULL;
225   args_info->paramFile_orig = NULL;
226   args_info->nsp_arg = NULL;
227   args_info->nsp_orig = NULL;
228   args_info->energyModel_orig = NULL;
229   args_info->betaScale_arg = 1.;
230   args_info->betaScale_orig = NULL;
231   
232 }
233
234 static
235 void init_args_info(struct RNAcofold_args_info *args_info)
236 {
237   init_full_help_array(); 
238   init_help_array(); 
239   args_info->help_help = RNAcofold_args_info_detailed_help[0] ;
240   args_info->detailed_help_help = RNAcofold_args_info_detailed_help[1] ;
241   args_info->full_help_help = RNAcofold_args_info_detailed_help[2] ;
242   args_info->version_help = RNAcofold_args_info_detailed_help[3] ;
243   args_info->constraint_help = RNAcofold_args_info_detailed_help[6] ;
244   args_info->noconv_help = RNAcofold_args_info_detailed_help[8] ;
245   args_info->noPS_help = RNAcofold_args_info_detailed_help[9] ;
246   args_info->partfunc_help = RNAcofold_args_info_detailed_help[12] ;
247   args_info->all_pf_help = RNAcofold_args_info_detailed_help[14] ;
248   args_info->concentrations_help = RNAcofold_args_info_detailed_help[16] ;
249   args_info->concfile_help = RNAcofold_args_info_detailed_help[18] ;
250   args_info->pfScale_help = RNAcofold_args_info_detailed_help[20] ;
251   args_info->bppmThreshold_help = RNAcofold_args_info_detailed_help[22] ;
252   args_info->gquad_help = RNAcofold_args_info_detailed_help[24] ;
253   args_info->temp_help = RNAcofold_args_info_detailed_help[26] ;
254   args_info->noTetra_help = RNAcofold_args_info_detailed_help[27] ;
255   args_info->dangles_help = RNAcofold_args_info_detailed_help[28] ;
256   args_info->noLP_help = RNAcofold_args_info_detailed_help[30] ;
257   args_info->noGU_help = RNAcofold_args_info_detailed_help[32] ;
258   args_info->noClosingGU_help = RNAcofold_args_info_detailed_help[33] ;
259   args_info->paramFile_help = RNAcofold_args_info_detailed_help[34] ;
260   args_info->nsp_help = RNAcofold_args_info_detailed_help[36] ;
261   args_info->energyModel_help = RNAcofold_args_info_detailed_help[38] ;
262   args_info->betaScale_help = RNAcofold_args_info_detailed_help[39] ;
263   
264 }
265
266 void
267 RNAcofold_cmdline_parser_print_version (void)
268 {
269   printf ("%s %s\n",
270      (strlen(RNACOFOLD_CMDLINE_PARSER_PACKAGE_NAME) ? RNACOFOLD_CMDLINE_PARSER_PACKAGE_NAME : RNACOFOLD_CMDLINE_PARSER_PACKAGE),
271      RNACOFOLD_CMDLINE_PARSER_VERSION);
272 }
273
274 static void print_help_common(void) {
275   RNAcofold_cmdline_parser_print_version ();
276
277   if (strlen(RNAcofold_args_info_purpose) > 0)
278     printf("\n%s\n", RNAcofold_args_info_purpose);
279
280   if (strlen(RNAcofold_args_info_usage) > 0)
281     printf("\n%s\n", RNAcofold_args_info_usage);
282
283   printf("\n");
284
285   if (strlen(RNAcofold_args_info_description) > 0)
286     printf("%s\n\n", RNAcofold_args_info_description);
287 }
288
289 void
290 RNAcofold_cmdline_parser_print_help (void)
291 {
292   int i = 0;
293   print_help_common();
294   while (RNAcofold_args_info_help[i])
295     printf("%s\n", RNAcofold_args_info_help[i++]);
296 }
297
298 void
299 RNAcofold_cmdline_parser_print_full_help (void)
300 {
301   int i = 0;
302   print_help_common();
303   while (RNAcofold_args_info_full_help[i])
304     printf("%s\n", RNAcofold_args_info_full_help[i++]);
305 }
306
307 void
308 RNAcofold_cmdline_parser_print_detailed_help (void)
309 {
310   int i = 0;
311   print_help_common();
312   while (RNAcofold_args_info_detailed_help[i])
313     printf("%s\n", RNAcofold_args_info_detailed_help[i++]);
314 }
315
316 void
317 RNAcofold_cmdline_parser_init (struct RNAcofold_args_info *args_info)
318 {
319   clear_given (args_info);
320   clear_args (args_info);
321   init_args_info (args_info);
322 }
323
324 void
325 RNAcofold_cmdline_parser_params_init(struct RNAcofold_cmdline_parser_params *params)
326 {
327   if (params)
328     { 
329       params->override = 0;
330       params->initialize = 1;
331       params->check_required = 1;
332       params->check_ambiguity = 0;
333       params->print_errors = 1;
334     }
335 }
336
337 struct RNAcofold_cmdline_parser_params *
338 RNAcofold_cmdline_parser_params_create(void)
339 {
340   struct RNAcofold_cmdline_parser_params *params = 
341     (struct RNAcofold_cmdline_parser_params *)malloc(sizeof(struct RNAcofold_cmdline_parser_params));
342   RNAcofold_cmdline_parser_params_init(params);  
343   return params;
344 }
345
346 static void
347 free_string_field (char **s)
348 {
349   if (*s)
350     {
351       free (*s);
352       *s = 0;
353     }
354 }
355
356
357 static void
358 RNAcofold_cmdline_parser_release (struct RNAcofold_args_info *args_info)
359 {
360
361   free_string_field (&(args_info->partfunc_orig));
362   free_string_field (&(args_info->concfile_arg));
363   free_string_field (&(args_info->concfile_orig));
364   free_string_field (&(args_info->pfScale_orig));
365   free_string_field (&(args_info->bppmThreshold_orig));
366   free_string_field (&(args_info->temp_orig));
367   free_string_field (&(args_info->dangles_orig));
368   free_string_field (&(args_info->paramFile_arg));
369   free_string_field (&(args_info->paramFile_orig));
370   free_string_field (&(args_info->nsp_arg));
371   free_string_field (&(args_info->nsp_orig));
372   free_string_field (&(args_info->energyModel_orig));
373   free_string_field (&(args_info->betaScale_orig));
374   
375   
376
377   clear_given (args_info);
378 }
379
380
381 static void
382 write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
383 {
384   FIX_UNUSED (values);
385   if (arg) {
386     fprintf(outfile, "%s=\"%s\"\n", opt, arg);
387   } else {
388     fprintf(outfile, "%s\n", opt);
389   }
390 }
391
392
393 int
394 RNAcofold_cmdline_parser_dump(FILE *outfile, struct RNAcofold_args_info *args_info)
395 {
396   int i = 0;
397
398   if (!outfile)
399     {
400       fprintf (stderr, "%s: cannot dump options to stream\n", RNACOFOLD_CMDLINE_PARSER_PACKAGE);
401       return EXIT_FAILURE;
402     }
403
404   if (args_info->help_given)
405     write_into_file(outfile, "help", 0, 0 );
406   if (args_info->detailed_help_given)
407     write_into_file(outfile, "detailed-help", 0, 0 );
408   if (args_info->full_help_given)
409     write_into_file(outfile, "full-help", 0, 0 );
410   if (args_info->version_given)
411     write_into_file(outfile, "version", 0, 0 );
412   if (args_info->constraint_given)
413     write_into_file(outfile, "constraint", 0, 0 );
414   if (args_info->noconv_given)
415     write_into_file(outfile, "noconv", 0, 0 );
416   if (args_info->noPS_given)
417     write_into_file(outfile, "noPS", 0, 0 );
418   if (args_info->partfunc_given)
419     write_into_file(outfile, "partfunc", args_info->partfunc_orig, 0);
420   if (args_info->all_pf_given)
421     write_into_file(outfile, "all_pf", 0, 0 );
422   if (args_info->concentrations_given)
423     write_into_file(outfile, "concentrations", 0, 0 );
424   if (args_info->concfile_given)
425     write_into_file(outfile, "concfile", args_info->concfile_orig, 0);
426   if (args_info->pfScale_given)
427     write_into_file(outfile, "pfScale", args_info->pfScale_orig, 0);
428   if (args_info->bppmThreshold_given)
429     write_into_file(outfile, "bppmThreshold", args_info->bppmThreshold_orig, 0);
430   if (args_info->gquad_given)
431     write_into_file(outfile, "gquad", 0, 0 );
432   if (args_info->temp_given)
433     write_into_file(outfile, "temp", args_info->temp_orig, 0);
434   if (args_info->noTetra_given)
435     write_into_file(outfile, "noTetra", 0, 0 );
436   if (args_info->dangles_given)
437     write_into_file(outfile, "dangles", args_info->dangles_orig, 0);
438   if (args_info->noLP_given)
439     write_into_file(outfile, "noLP", 0, 0 );
440   if (args_info->noGU_given)
441     write_into_file(outfile, "noGU", 0, 0 );
442   if (args_info->noClosingGU_given)
443     write_into_file(outfile, "noClosingGU", 0, 0 );
444   if (args_info->paramFile_given)
445     write_into_file(outfile, "paramFile", args_info->paramFile_orig, 0);
446   if (args_info->nsp_given)
447     write_into_file(outfile, "nsp", args_info->nsp_orig, 0);
448   if (args_info->energyModel_given)
449     write_into_file(outfile, "energyModel", args_info->energyModel_orig, 0);
450   if (args_info->betaScale_given)
451     write_into_file(outfile, "betaScale", args_info->betaScale_orig, 0);
452   
453
454   i = EXIT_SUCCESS;
455   return i;
456 }
457
458 int
459 RNAcofold_cmdline_parser_file_save(const char *filename, struct RNAcofold_args_info *args_info)
460 {
461   FILE *outfile;
462   int i = 0;
463
464   outfile = fopen(filename, "w");
465
466   if (!outfile)
467     {
468       fprintf (stderr, "%s: cannot open file for writing: %s\n", RNACOFOLD_CMDLINE_PARSER_PACKAGE, filename);
469       return EXIT_FAILURE;
470     }
471
472   i = RNAcofold_cmdline_parser_dump(outfile, args_info);
473   fclose (outfile);
474
475   return i;
476 }
477
478 void
479 RNAcofold_cmdline_parser_free (struct RNAcofold_args_info *args_info)
480 {
481   RNAcofold_cmdline_parser_release (args_info);
482 }
483
484 /** @brief replacement of strdup, which is not standard */
485 char *
486 gengetopt_strdup (const char *s)
487 {
488   char *result = 0;
489   if (!s)
490     return result;
491
492   result = (char*)malloc(strlen(s) + 1);
493   if (result == (char*)0)
494     return (char*)0;
495   strcpy(result, s);
496   return result;
497 }
498
499 int
500 RNAcofold_cmdline_parser (int argc, char **argv, struct RNAcofold_args_info *args_info)
501 {
502   return RNAcofold_cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
503 }
504
505 int
506 RNAcofold_cmdline_parser_ext (int argc, char **argv, struct RNAcofold_args_info *args_info,
507                    struct RNAcofold_cmdline_parser_params *params)
508 {
509   int result;
510   result = RNAcofold_cmdline_parser_internal (argc, argv, args_info, params, 0);
511
512   if (result == EXIT_FAILURE)
513     {
514       RNAcofold_cmdline_parser_free (args_info);
515       exit (EXIT_FAILURE);
516     }
517   
518   return result;
519 }
520
521 int
522 RNAcofold_cmdline_parser2 (int argc, char **argv, struct RNAcofold_args_info *args_info, int override, int initialize, int check_required)
523 {
524   int result;
525   struct RNAcofold_cmdline_parser_params params;
526   
527   params.override = override;
528   params.initialize = initialize;
529   params.check_required = check_required;
530   params.check_ambiguity = 0;
531   params.print_errors = 1;
532
533   result = RNAcofold_cmdline_parser_internal (argc, argv, args_info, &params, 0);
534
535   if (result == EXIT_FAILURE)
536     {
537       RNAcofold_cmdline_parser_free (args_info);
538       exit (EXIT_FAILURE);
539     }
540   
541   return result;
542 }
543
544 int
545 RNAcofold_cmdline_parser_required (struct RNAcofold_args_info *args_info, const char *prog_name)
546 {
547   FIX_UNUSED (args_info);
548   FIX_UNUSED (prog_name);
549   return EXIT_SUCCESS;
550 }
551
552 /*
553  * Extracted from the glibc source tree, version 2.3.6
554  *
555  * Licensed under the GPL as per the whole glibc source tree.
556  *
557  * This file was modified so that getopt_long can be called
558  * many times without risking previous memory to be spoiled.
559  *
560  * Modified by Andre Noll and Lorenzo Bettini for use in
561  * GNU gengetopt generated files.
562  *
563  */
564
565 /* 
566  * we must include anything we need since this file is not thought to be
567  * inserted in a file already using getopt.h
568  *
569  * Lorenzo
570  */
571
572 struct option
573 {
574   const char *name;
575   /* has_arg can't be an enum because some compilers complain about
576      type mismatches in all the code that assumes it is an int.  */
577   int has_arg;
578   int *flag;
579   int val;
580 };
581
582 /* This version of `getopt' appears to the caller like standard Unix `getopt'
583    but it behaves differently for the user, since it allows the user
584    to intersperse the options with the other arguments.
585
586    As `getopt' works, it permutes the elements of ARGV so that,
587    when it is done, all the options precede everything else.  Thus
588    all application programs are extended to handle flexible argument order.
589 */
590 /*
591    If the field `flag' is not NULL, it points to a variable that is set
592    to the value given in the field `val' when the option is found, but
593    left unchanged if the option is not found.
594
595    To have a long-named option do something other than set an `int' to
596    a compiled-in constant, such as set a value from `custom_optarg', set the
597    option's `flag' field to zero and its `val' field to a nonzero
598    value (the equivalent single-letter option character, if there is
599    one).  For long options that have a zero `flag' field, `getopt'
600    returns the contents of the `val' field.  */
601
602 /* Names for the values of the `has_arg' field of `struct option'.  */
603 #ifndef no_argument
604 #define no_argument             0
605 #endif
606
607 #ifndef required_argument
608 #define required_argument       1
609 #endif
610
611 #ifndef optional_argument
612 #define optional_argument       2
613 #endif
614
615 struct custom_getopt_data {
616         /*
617          * These have exactly the same meaning as the corresponding global variables,
618          * except that they are used for the reentrant versions of getopt.
619          */
620         int custom_optind;
621         int custom_opterr;
622         int custom_optopt;
623         char *custom_optarg;
624
625         /* True if the internal members have been initialized.  */
626         int initialized;
627
628         /*
629          * The next char to be scanned in the option-element in which the last option
630          * character we returned was found.  This allows us to pick up the scan where
631          * we left off.  If this is zero, or a null string, it means resume the scan by
632          * advancing to the next ARGV-element.
633          */
634         char *nextchar;
635
636         /*
637          * Describe the part of ARGV that contains non-options that have been skipped.
638          * `first_nonopt' is the index in ARGV of the first of them; `last_nonopt' is
639          * the index after the last of them.
640          */
641         int first_nonopt;
642         int last_nonopt;
643 };
644
645 /*
646  * the variables optarg, optind, opterr and optopt are renamed with
647  * the custom_ prefix so that they don't interfere with getopt ones.
648  *
649  * Moreover they're static so they are visible only from within the
650  * file where this very file will be included.
651  */
652
653 /*
654  * For communication from `custom_getopt' to the caller.  When `custom_getopt' finds an
655  * option that takes an argument, the argument value is returned here.
656  */
657 static char *custom_optarg;
658
659 /*
660  * Index in ARGV of the next element to be scanned.  This is used for
661  * communication to and from the caller and for communication between
662  * successive calls to `custom_getopt'.
663  *
664  * On entry to `custom_getopt', 1 means this is the first call; initialize.
665  *
666  * When `custom_getopt' returns -1, this is the index of the first of the non-option
667  * elements that the caller should itself scan.
668  *
669  * Otherwise, `custom_optind' communicates from one call to the next how much of ARGV
670  * has been scanned so far.
671  *
672  * 1003.2 says this must be 1 before any call.
673  */
674 static int custom_optind = 1;
675
676 /*
677  * Callers store zero here to inhibit the error message for unrecognized
678  * options.
679  */
680 static int custom_opterr = 1;
681
682 /*
683  * Set to an option character which was unrecognized.  This must be initialized
684  * on some systems to avoid linking in the system's own getopt implementation.
685  */
686 static int custom_optopt = '?';
687
688 /*
689  * Exchange two adjacent subsequences of ARGV.  One subsequence is elements
690  * [first_nonopt,last_nonopt) which contains all the non-options that have been
691  * skipped so far.  The other is elements [last_nonopt,custom_optind), which contains
692  * all the options processed since those non-options were skipped.
693  * `first_nonopt' and `last_nonopt' are relocated so that they describe the new
694  * indices of the non-options in ARGV after they are moved.
695  */
696 static void exchange(char **argv, struct custom_getopt_data *d)
697 {
698         int bottom = d->first_nonopt;
699         int middle = d->last_nonopt;
700         int top = d->custom_optind;
701         char *tem;
702
703         /*
704          * Exchange the shorter segment with the far end of the longer segment.
705          * That puts the shorter segment into the right place.  It leaves the
706          * longer segment in the right place overall, but it consists of two
707          * parts that need to be swapped next.
708          */
709         while (top > middle && middle > bottom) {
710                 if (top - middle > middle - bottom) {
711                         /* Bottom segment is the short one.  */
712                         int len = middle - bottom;
713                         int i;
714
715                         /* Swap it with the top part of the top segment.  */
716                         for (i = 0; i < len; i++) {
717                                 tem = argv[bottom + i];
718                                 argv[bottom + i] =
719                                         argv[top - (middle - bottom) + i];
720                                 argv[top - (middle - bottom) + i] = tem;
721                         }
722                         /* Exclude the moved bottom segment from further swapping.  */
723                         top -= len;
724                 } else {
725                         /* Top segment is the short one.  */
726                         int len = top - middle;
727                         int i;
728
729                         /* Swap it with the bottom part of the bottom segment.  */
730                         for (i = 0; i < len; i++) {
731                                 tem = argv[bottom + i];
732                                 argv[bottom + i] = argv[middle + i];
733                                 argv[middle + i] = tem;
734                         }
735                         /* Exclude the moved top segment from further swapping.  */
736                         bottom += len;
737                 }
738         }
739         /* Update records for the slots the non-options now occupy.  */
740         d->first_nonopt += (d->custom_optind - d->last_nonopt);
741         d->last_nonopt = d->custom_optind;
742 }
743
744 /* Initialize the internal data when the first call is made.  */
745 static void custom_getopt_initialize(struct custom_getopt_data *d)
746 {
747         /*
748          * Start processing options with ARGV-element 1 (since ARGV-element 0
749          * is the program name); the sequence of previously skipped non-option
750          * ARGV-elements is empty.
751          */
752         d->first_nonopt = d->last_nonopt = d->custom_optind;
753         d->nextchar = NULL;
754         d->initialized = 1;
755 }
756
757 #define NONOPTION_P (argv[d->custom_optind][0] != '-' || argv[d->custom_optind][1] == '\0')
758
759 /* return: zero: continue, nonzero: return given value to user */
760 static int shuffle_argv(int argc, char *const *argv,const struct option *longopts,
761         struct custom_getopt_data *d)
762 {
763         /*
764          * Give FIRST_NONOPT & LAST_NONOPT rational values if CUSTOM_OPTIND has been
765          * moved back by the user (who may also have changed the arguments).
766          */
767         if (d->last_nonopt > d->custom_optind)
768                 d->last_nonopt = d->custom_optind;
769         if (d->first_nonopt > d->custom_optind)
770                 d->first_nonopt = d->custom_optind;
771         /*
772          * If we have just processed some options following some
773          * non-options, exchange them so that the options come first.
774          */
775         if (d->first_nonopt != d->last_nonopt &&
776                         d->last_nonopt != d->custom_optind)
777                 exchange((char **) argv, d);
778         else if (d->last_nonopt != d->custom_optind)
779                 d->first_nonopt = d->custom_optind;
780         /*
781          * Skip any additional non-options and extend the range of
782          * non-options previously skipped.
783          */
784         while (d->custom_optind < argc && NONOPTION_P)
785                 d->custom_optind++;
786         d->last_nonopt = d->custom_optind;
787         /*
788          * The special ARGV-element `--' means premature end of options.  Skip
789          * it like a null option, then exchange with previous non-options as if
790          * it were an option, then skip everything else like a non-option.
791          */
792         if (d->custom_optind != argc && !strcmp(argv[d->custom_optind], "--")) {
793                 d->custom_optind++;
794                 if (d->first_nonopt != d->last_nonopt
795                                 && d->last_nonopt != d->custom_optind)
796                         exchange((char **) argv, d);
797                 else if (d->first_nonopt == d->last_nonopt)
798                         d->first_nonopt = d->custom_optind;
799                 d->last_nonopt = argc;
800                 d->custom_optind = argc;
801         }
802         /*
803          * If we have done all the ARGV-elements, stop the scan and back over
804          * any non-options that we skipped and permuted.
805          */
806         if (d->custom_optind == argc) {
807                 /*
808                  * Set the next-arg-index to point at the non-options that we
809                  * previously skipped, so the caller will digest them.
810                  */
811                 if (d->first_nonopt != d->last_nonopt)
812                         d->custom_optind = d->first_nonopt;
813                 return -1;
814         }
815         /*
816          * If we have come to a non-option and did not permute it, either stop
817          * the scan or describe it to the caller and pass it by.
818          */
819         if (NONOPTION_P) {
820                 d->custom_optarg = argv[d->custom_optind++];
821                 return 1;
822         }
823         /*
824          * We have found another option-ARGV-element. Skip the initial
825          * punctuation.
826          */
827         d->nextchar = (argv[d->custom_optind] + 1 + (longopts != NULL && argv[d->custom_optind][1] == '-'));
828         return 0;
829 }
830
831 /*
832  * Check whether the ARGV-element is a long option.
833  *
834  * If there's a long option "fubar" and the ARGV-element is "-fu", consider
835  * that an abbreviation of the long option, just like "--fu", and not "-f" with
836  * arg "u".
837  *
838  * This distinction seems to be the most useful approach.
839  *
840  */
841 static int check_long_opt(int argc, char *const *argv, const char *optstring,
842                 const struct option *longopts, int *longind,
843                 int print_errors, struct custom_getopt_data *d)
844 {
845         char *nameend;
846         const struct option *p;
847         const struct option *pfound = NULL;
848         int exact = 0;
849         int ambig = 0;
850         int indfound = -1;
851         int option_index;
852
853         for (nameend = d->nextchar; *nameend && *nameend != '='; nameend++)
854                 /* Do nothing.  */ ;
855
856         /* Test all long options for either exact match or abbreviated matches */
857         for (p = longopts, option_index = 0; p->name; p++, option_index++)
858                 if (!strncmp(p->name, d->nextchar, nameend - d->nextchar)) {
859                         if ((unsigned int) (nameend - d->nextchar)
860                                         == (unsigned int) strlen(p->name)) {
861                                 /* Exact match found.  */
862                                 pfound = p;
863                                 indfound = option_index;
864                                 exact = 1;
865                                 break;
866                         } else if (pfound == NULL) {
867                                 /* First nonexact match found.  */
868                                 pfound = p;
869                                 indfound = option_index;
870                         } else if (pfound->has_arg != p->has_arg
871                                         || pfound->flag != p->flag
872                                         || pfound->val != p->val)
873                                 /* Second or later nonexact match found.  */
874                                 ambig = 1;
875                 }
876         if (ambig && !exact) {
877                 if (print_errors) {
878                         fprintf(stderr,
879                                 "%s: option `%s' is ambiguous\n",
880                                 argv[0], argv[d->custom_optind]);
881                 }
882                 d->nextchar += strlen(d->nextchar);
883                 d->custom_optind++;
884                 d->custom_optopt = 0;
885                 return '?';
886         }
887         if (pfound) {
888                 option_index = indfound;
889                 d->custom_optind++;
890                 if (*nameend) {
891                         if (pfound->has_arg != no_argument)
892                                 d->custom_optarg = nameend + 1;
893                         else {
894                                 if (print_errors) {
895                                         if (argv[d->custom_optind - 1][1] == '-') {
896                                                 /* --option */
897                                                 fprintf(stderr, "%s: option `--%s' doesn't allow an argument\n",
898                                                         argv[0], pfound->name);
899                                         } else {
900                                                 /* +option or -option */
901                                                 fprintf(stderr, "%s: option `%c%s' doesn't allow an argument\n",
902                                                         argv[0], argv[d->custom_optind - 1][0], pfound->name);
903                                         }
904
905                                 }
906                                 d->nextchar += strlen(d->nextchar);
907                                 d->custom_optopt = pfound->val;
908                                 return '?';
909                         }
910                 } else if (pfound->has_arg == required_argument) {
911                         if (d->custom_optind < argc)
912                                 d->custom_optarg = argv[d->custom_optind++];
913                         else {
914                                 if (print_errors) {
915                                         fprintf(stderr,
916                                                 "%s: option `%s' requires an argument\n",
917                                                 argv[0],
918                                                 argv[d->custom_optind - 1]);
919                                 }
920                                 d->nextchar += strlen(d->nextchar);
921                                 d->custom_optopt = pfound->val;
922                                 return optstring[0] == ':' ? ':' : '?';
923                         }
924                 }
925                 d->nextchar += strlen(d->nextchar);
926                 if (longind != NULL)
927                         *longind = option_index;
928                 if (pfound->flag) {
929                         *(pfound->flag) = pfound->val;
930                         return 0;
931                 }
932                 return pfound->val;
933         }
934         /*
935          * Can't find it as a long option.  If this is not getopt_long_only, or
936          * the option starts with '--' or is not a valid short option, then
937          * it's an error.  Otherwise interpret it as a short option.
938          */
939         if (print_errors) {
940                 if (argv[d->custom_optind][1] == '-') {
941                         /* --option */
942                         fprintf(stderr,
943                                 "%s: unrecognized option `--%s'\n",
944                                 argv[0], d->nextchar);
945                 } else {
946                         /* +option or -option */
947                         fprintf(stderr,
948                                 "%s: unrecognized option `%c%s'\n",
949                                 argv[0], argv[d->custom_optind][0],
950                                 d->nextchar);
951                 }
952         }
953         d->nextchar = (char *) "";
954         d->custom_optind++;
955         d->custom_optopt = 0;
956         return '?';
957 }
958
959 static int check_short_opt(int argc, char *const *argv, const char *optstring,
960                 int print_errors, struct custom_getopt_data *d)
961 {
962         char c = *d->nextchar++;
963         const char *temp = strchr(optstring, c);
964
965         /* Increment `custom_optind' when we start to process its last character.  */
966         if (*d->nextchar == '\0')
967                 ++d->custom_optind;
968         if (!temp || c == ':') {
969                 if (print_errors)
970                         fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
971
972                 d->custom_optopt = c;
973                 return '?';
974         }
975         if (temp[1] == ':') {
976                 if (temp[2] == ':') {
977                         /* This is an option that accepts an argument optionally.  */
978                         if (*d->nextchar != '\0') {
979                                 d->custom_optarg = d->nextchar;
980                                 d->custom_optind++;
981                         } else
982                                 d->custom_optarg = NULL;
983                         d->nextchar = NULL;
984                 } else {
985                         /* This is an option that requires an argument.  */
986                         if (*d->nextchar != '\0') {
987                                 d->custom_optarg = d->nextchar;
988                                 /*
989                                  * If we end this ARGV-element by taking the
990                                  * rest as an arg, we must advance to the next
991                                  * element now.
992                                  */
993                                 d->custom_optind++;
994                         } else if (d->custom_optind == argc) {
995                                 if (print_errors) {
996                                         fprintf(stderr,
997                                                 "%s: option requires an argument -- %c\n",
998                                                 argv[0], c);
999                                 }
1000                                 d->custom_optopt = c;
1001                                 if (optstring[0] == ':')
1002                                         c = ':';
1003                                 else
1004                                         c = '?';
1005                         } else
1006                                 /*
1007                                  * We already incremented `custom_optind' once;
1008                                  * increment it again when taking next ARGV-elt
1009                                  * as argument.
1010                                  */
1011                                 d->custom_optarg = argv[d->custom_optind++];
1012                         d->nextchar = NULL;
1013                 }
1014         }
1015         return c;
1016 }
1017
1018 /*
1019  * Scan elements of ARGV for option characters given in OPTSTRING.
1020  *
1021  * If an element of ARGV starts with '-', and is not exactly "-" or "--",
1022  * then it is an option element.  The characters of this element
1023  * (aside from the initial '-') are option characters.  If `getopt'
1024  * is called repeatedly, it returns successively each of the option characters
1025  * from each of the option elements.
1026  *
1027  * If `getopt' finds another option character, it returns that character,
1028  * updating `custom_optind' and `nextchar' so that the next call to `getopt' can
1029  * resume the scan with the following option character or ARGV-element.
1030  *
1031  * If there are no more option characters, `getopt' returns -1.
1032  * Then `custom_optind' is the index in ARGV of the first ARGV-element
1033  * that is not an option.  (The ARGV-elements have been permuted
1034  * so that those that are not options now come last.)
1035  *
1036  * OPTSTRING is a string containing the legitimate option characters.
1037  * If an option character is seen that is not listed in OPTSTRING,
1038  * return '?' after printing an error message.  If you set `custom_opterr' to
1039  * zero, the error message is suppressed but we still return '?'.
1040  *
1041  * If a char in OPTSTRING is followed by a colon, that means it wants an arg,
1042  * so the following text in the same ARGV-element, or the text of the following
1043  * ARGV-element, is returned in `custom_optarg'.  Two colons mean an option that
1044  * wants an optional arg; if there is text in the current ARGV-element,
1045  * it is returned in `custom_optarg', otherwise `custom_optarg' is set to zero.
1046  *
1047  * If OPTSTRING starts with `-' or `+', it requests different methods of
1048  * handling the non-option ARGV-elements.
1049  * See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
1050  *
1051  * Long-named options begin with `--' instead of `-'.
1052  * Their names may be abbreviated as long as the abbreviation is unique
1053  * or is an exact match for some defined option.  If they have an
1054  * argument, it follows the option name in the same ARGV-element, separated
1055  * from the option name by a `=', or else the in next ARGV-element.
1056  * When `getopt' finds a long-named option, it returns 0 if that option's
1057  * `flag' field is nonzero, the value of the option's `val' field
1058  * if the `flag' field is zero.
1059  *
1060  * The elements of ARGV aren't really const, because we permute them.
1061  * But we pretend they're const in the prototype to be compatible
1062  * with other systems.
1063  *
1064  * LONGOPTS is a vector of `struct option' terminated by an
1065  * element containing a name which is zero.
1066  *
1067  * LONGIND returns the index in LONGOPT of the long-named option found.
1068  * It is only valid when a long-named option has been found by the most
1069  * recent call.
1070  *
1071  * Return the option character from OPTS just read.  Return -1 when there are
1072  * no more options.  For unrecognized options, or options missing arguments,
1073  * `custom_optopt' is set to the option letter, and '?' is returned.
1074  *
1075  * The OPTS string is a list of characters which are recognized option letters,
1076  * optionally followed by colons, specifying that that letter takes an
1077  * argument, to be placed in `custom_optarg'.
1078  *
1079  * If a letter in OPTS is followed by two colons, its argument is optional.
1080  * This behavior is specific to the GNU `getopt'.
1081  *
1082  * The argument `--' causes premature termination of argument scanning,
1083  * explicitly telling `getopt' that there are no more options.  If OPTS begins
1084  * with `--', then non-option arguments are treated as arguments to the option
1085  * '\0'.  This behavior is specific to the GNU `getopt'.
1086  */
1087
1088 static int getopt_internal_r(int argc, char *const *argv, const char *optstring,
1089                 const struct option *longopts, int *longind,
1090                 struct custom_getopt_data *d)
1091 {
1092         int ret, print_errors = d->custom_opterr;
1093
1094         if (optstring[0] == ':')
1095                 print_errors = 0;
1096         if (argc < 1)
1097                 return -1;
1098         d->custom_optarg = NULL;
1099
1100         /* 
1101          * This is a big difference with GNU getopt, since optind == 0
1102          * means initialization while here 1 means first call.
1103          */
1104         if (d->custom_optind == 0 || !d->initialized) {
1105                 if (d->custom_optind == 0)
1106                         d->custom_optind = 1;   /* Don't scan ARGV[0], the program name.  */
1107                 custom_getopt_initialize(d);
1108         }
1109         if (d->nextchar == NULL || *d->nextchar == '\0') {
1110                 ret = shuffle_argv(argc, argv, longopts, d);
1111                 if (ret)
1112                         return ret;
1113         }
1114         if (longopts && (argv[d->custom_optind][1] == '-' ))
1115                 return check_long_opt(argc, argv, optstring, longopts,
1116                         longind, print_errors, d);
1117         return check_short_opt(argc, argv, optstring, print_errors, d);
1118 }
1119
1120 static int custom_getopt_internal(int argc, char *const *argv, const char *optstring,
1121         const struct option *longopts, int *longind)
1122 {
1123         int result;
1124         /* Keep a global copy of all internal members of d */
1125         static struct custom_getopt_data d;
1126
1127         d.custom_optind = custom_optind;
1128         d.custom_opterr = custom_opterr;
1129         result = getopt_internal_r(argc, argv, optstring, longopts,
1130                 longind, &d);
1131         custom_optind = d.custom_optind;
1132         custom_optarg = d.custom_optarg;
1133         custom_optopt = d.custom_optopt;
1134         return result;
1135 }
1136
1137 static int custom_getopt_long (int argc, char *const *argv, const char *options,
1138         const struct option *long_options, int *opt_index)
1139 {
1140         return custom_getopt_internal(argc, argv, options, long_options,
1141                 opt_index);
1142 }
1143
1144
1145 static char *package_name = 0;
1146
1147 /**
1148  * @brief updates an option
1149  * @param field the generic pointer to the field to update
1150  * @param orig_field the pointer to the orig field
1151  * @param field_given the pointer to the number of occurrence of this option
1152  * @param prev_given the pointer to the number of occurrence already seen
1153  * @param value the argument for this option (if null no arg was specified)
1154  * @param possible_values the possible values for this option (if specified)
1155  * @param default_value the default value (in case the option only accepts fixed values)
1156  * @param arg_type the type of this option
1157  * @param check_ambiguity @see RNAcofold_cmdline_parser_params.check_ambiguity
1158  * @param override @see RNAcofold_cmdline_parser_params.override
1159  * @param no_free whether to free a possible previous value
1160  * @param multiple_option whether this is a multiple option
1161  * @param long_opt the corresponding long option
1162  * @param short_opt the corresponding short option (or '-' if none)
1163  * @param additional_error possible further error specification
1164  */
1165 static
1166 int update_arg(void *field, char **orig_field,
1167                unsigned int *field_given, unsigned int *prev_given, 
1168                char *value, const char *possible_values[],
1169                const char *default_value,
1170                RNAcofold_cmdline_parser_arg_type arg_type,
1171                int check_ambiguity, int override,
1172                int no_free, int multiple_option,
1173                const char *long_opt, char short_opt,
1174                const char *additional_error)
1175 {
1176   char *stop_char = 0;
1177   const char *val = value;
1178   int found;
1179   char **string_field;
1180   FIX_UNUSED (field);
1181
1182   stop_char = 0;
1183   found = 0;
1184
1185   if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
1186     {
1187       if (short_opt != '-')
1188         fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", 
1189                package_name, long_opt, short_opt,
1190                (additional_error ? additional_error : ""));
1191       else
1192         fprintf (stderr, "%s: `--%s' option given more than once%s\n", 
1193                package_name, long_opt,
1194                (additional_error ? additional_error : ""));
1195       return 1; /* failure */
1196     }
1197
1198   FIX_UNUSED (default_value);
1199     
1200   if (field_given && *field_given && ! override)
1201     return 0;
1202   if (prev_given)
1203     (*prev_given)++;
1204   if (field_given)
1205     (*field_given)++;
1206   if (possible_values)
1207     val = possible_values[found];
1208
1209   switch(arg_type) {
1210   case ARG_FLAG:
1211     *((int *)field) = !*((int *)field);
1212     break;
1213   case ARG_INT:
1214     if (val) *((int *)field) = strtol (val, &stop_char, 0);
1215     break;
1216   case ARG_DOUBLE:
1217     if (val) *((double *)field) = strtod (val, &stop_char);
1218     break;
1219   case ARG_STRING:
1220     if (val) {
1221       string_field = (char **)field;
1222       if (!no_free && *string_field)
1223         free (*string_field); /* free previous string */
1224       *string_field = gengetopt_strdup (val);
1225     }
1226     break;
1227   default:
1228     break;
1229   };
1230
1231   /* check numeric conversion */
1232   switch(arg_type) {
1233   case ARG_INT:
1234   case ARG_DOUBLE:
1235     if (val && !(stop_char && *stop_char == '\0')) {
1236       fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
1237       return 1; /* failure */
1238     }
1239     break;
1240   default:
1241     ;
1242   };
1243
1244   /* store the original value */
1245   switch(arg_type) {
1246   case ARG_NO:
1247   case ARG_FLAG:
1248     break;
1249   default:
1250     if (value && orig_field) {
1251       if (no_free) {
1252         *orig_field = value;
1253       } else {
1254         if (*orig_field)
1255           free (*orig_field); /* free previous string */
1256         *orig_field = gengetopt_strdup (value);
1257       }
1258     }
1259   };
1260
1261   return 0; /* OK */
1262 }
1263
1264
1265 int
1266 RNAcofold_cmdline_parser_internal (
1267   int argc, char **argv, struct RNAcofold_args_info *args_info,
1268                         struct RNAcofold_cmdline_parser_params *params, const char *additional_error)
1269 {
1270   int c;        /* Character of the parsed option.  */
1271
1272   int error = 0;
1273   struct RNAcofold_args_info local_args_info;
1274   
1275   int override;
1276   int initialize;
1277   int check_required;
1278   int check_ambiguity;
1279
1280   char *optarg;
1281   int optind;
1282   int opterr;
1283   int optopt;
1284   
1285   package_name = argv[0];
1286   
1287   override = params->override;
1288   initialize = params->initialize;
1289   check_required = params->check_required;
1290   check_ambiguity = params->check_ambiguity;
1291
1292   if (initialize)
1293     RNAcofold_cmdline_parser_init (args_info);
1294
1295   RNAcofold_cmdline_parser_init (&local_args_info);
1296
1297   optarg = 0;
1298   optind = 0;
1299   opterr = params->print_errors;
1300   optopt = '?';
1301
1302   while (1)
1303     {
1304       int option_index = 0;
1305
1306       static struct option long_options[] = {
1307         { "help",       0, NULL, 'h' },
1308         { "detailed-help",      0, NULL, 0 },
1309         { "full-help",  0, NULL, 0 },
1310         { "version",    0, NULL, 'V' },
1311         { "constraint", 0, NULL, 'C' },
1312         { "noconv",     0, NULL, 0 },
1313         { "noPS",       0, NULL, 0 },
1314         { "partfunc",   2, NULL, 'p' },
1315         { "all_pf",     0, NULL, 'a' },
1316         { "concentrations",     0, NULL, 'c' },
1317         { "concfile",   1, NULL, 'f' },
1318         { "pfScale",    1, NULL, 'S' },
1319         { "bppmThreshold",      1, NULL, 0 },
1320         { "gquad",      0, NULL, 'g' },
1321         { "temp",       1, NULL, 'T' },
1322         { "noTetra",    0, NULL, '4' },
1323         { "dangles",    1, NULL, 'd' },
1324         { "noLP",       0, NULL, 0 },
1325         { "noGU",       0, NULL, 0 },
1326         { "noClosingGU",        0, NULL, 0 },
1327         { "paramFile",  1, NULL, 'P' },
1328         { "nsp",        1, NULL, 0 },
1329         { "energyModel",        1, NULL, 'e' },
1330         { "betaScale",  1, NULL, 0 },
1331         { 0,  0, 0, 0 }
1332       };
1333
1334       custom_optarg = optarg;
1335       custom_optind = optind;
1336       custom_opterr = opterr;
1337       custom_optopt = optopt;
1338
1339       c = custom_getopt_long (argc, argv, "hVCp::acf:S:gT:4d:P:e:", long_options, &option_index);
1340
1341       optarg = custom_optarg;
1342       optind = custom_optind;
1343       opterr = custom_opterr;
1344       optopt = custom_optopt;
1345
1346       if (c == -1) break;       /* Exit from `while (1)' loop.  */
1347
1348       switch (c)
1349         {
1350         case 'h':       /* Print help and exit.  */
1351           RNAcofold_cmdline_parser_print_help ();
1352           RNAcofold_cmdline_parser_free (&local_args_info);
1353           exit (EXIT_SUCCESS);
1354
1355         case 'V':       /* Print version and exit.  */
1356           RNAcofold_cmdline_parser_print_version ();
1357           RNAcofold_cmdline_parser_free (&local_args_info);
1358           exit (EXIT_SUCCESS);
1359
1360         case 'C':       /* Calculate structures subject to constraints.
1361 .  */
1362         
1363         
1364           if (update_arg((void *)&(args_info->constraint_flag), 0, &(args_info->constraint_given),
1365               &(local_args_info.constraint_given), optarg, 0, 0, ARG_FLAG,
1366               check_ambiguity, override, 1, 0, "constraint", 'C',
1367               additional_error))
1368             goto failure;
1369         
1370           break;
1371         case 'p':       /* Calculate the partition function and base pairing probability matrix in addition to the mfe structure. Default is calculation of mfe structure only.
1372 .  */
1373         
1374         
1375           if (update_arg( (void *)&(args_info->partfunc_arg), 
1376                &(args_info->partfunc_orig), &(args_info->partfunc_given),
1377               &(local_args_info.partfunc_given), optarg, 0, "1", ARG_INT,
1378               check_ambiguity, override, 0, 0,
1379               "partfunc", 'p',
1380               additional_error))
1381             goto failure;
1382         
1383           break;
1384         case 'a':       /* Compute the partition function and free energies not only of the hetero-dimer consisting of the two input sequences (the \"AB dimer\"), but also of the homo-dimers AA and BB as well as A and B monomers.
1385 .  */
1386         
1387         
1388           if (update_arg((void *)&(args_info->all_pf_flag), 0, &(args_info->all_pf_given),
1389               &(local_args_info.all_pf_given), optarg, 0, 0, ARG_FLAG,
1390               check_ambiguity, override, 1, 0, "all_pf", 'a',
1391               additional_error))
1392             goto failure;
1393         
1394           break;
1395         case 'c':       /* In addition to everything listed under the -a option, read in initial monomer concentrations and compute the expected equilibrium concentrations of the 5 possible species (AB, AA, BB, A, B).
1396 .  */
1397         
1398         
1399           if (update_arg((void *)&(args_info->concentrations_flag), 0, &(args_info->concentrations_given),
1400               &(local_args_info.concentrations_given), optarg, 0, 0, ARG_FLAG,
1401               check_ambiguity, override, 1, 0, "concentrations", 'c',
1402               additional_error))
1403             goto failure;
1404         
1405           break;
1406         case 'f':       /* Specify a file with initial concentrations for the to sequences..  */
1407         
1408         
1409           if (update_arg( (void *)&(args_info->concfile_arg), 
1410                &(args_info->concfile_orig), &(args_info->concfile_given),
1411               &(local_args_info.concfile_given), optarg, 0, 0, ARG_STRING,
1412               check_ambiguity, override, 0, 0,
1413               "concfile", 'f',
1414               additional_error))
1415             goto failure;
1416         
1417           break;
1418         case 'S':       /* In the calculation of the pf use scale*mfe as an estimate for the ensemble free energy (used to avoid overflows).
1419 .  */
1420         
1421         
1422           if (update_arg( (void *)&(args_info->pfScale_arg), 
1423                &(args_info->pfScale_orig), &(args_info->pfScale_given),
1424               &(local_args_info.pfScale_given), optarg, 0, 0, ARG_DOUBLE,
1425               check_ambiguity, override, 0, 0,
1426               "pfScale", 'S',
1427               additional_error))
1428             goto failure;
1429         
1430           break;
1431         case 'g':       /* Incoorporate G-Quadruplex formation into the structure prediction algorithm
1432 .  */
1433         
1434         
1435           if (update_arg((void *)&(args_info->gquad_flag), 0, &(args_info->gquad_given),
1436               &(local_args_info.gquad_given), optarg, 0, 0, ARG_FLAG,
1437               check_ambiguity, override, 1, 0, "gquad", 'g',
1438               additional_error))
1439             goto failure;
1440         
1441           break;
1442         case 'T':       /* Rescale energy parameters to a temperature of temp C. Default is 37C.
1443         
1444 .  */
1445         
1446         
1447           if (update_arg( (void *)&(args_info->temp_arg), 
1448                &(args_info->temp_orig), &(args_info->temp_given),
1449               &(local_args_info.temp_given), optarg, 0, 0, ARG_DOUBLE,
1450               check_ambiguity, override, 0, 0,
1451               "temp", 'T',
1452               additional_error))
1453             goto failure;
1454         
1455           break;
1456         case '4':       /* Do not include special stabilizing energies for certain tetra-loops. Mostly for testing.
1457         
1458 .  */
1459         
1460         
1461           if (update_arg((void *)&(args_info->noTetra_flag), 0, &(args_info->noTetra_given),
1462               &(local_args_info.noTetra_given), optarg, 0, 0, ARG_FLAG,
1463               check_ambiguity, override, 1, 0, "noTetra", '4',
1464               additional_error))
1465             goto failure;
1466         
1467           break;
1468         case 'd':       /* How to treat \"dangling end\" energies for bases adjacent to helices in free ends and multi-loops
1469 .  */
1470         
1471         
1472           if (update_arg( (void *)&(args_info->dangles_arg), 
1473                &(args_info->dangles_orig), &(args_info->dangles_given),
1474               &(local_args_info.dangles_given), optarg, 0, "2", ARG_INT,
1475               check_ambiguity, override, 0, 0,
1476               "dangles", 'd',
1477               additional_error))
1478             goto failure;
1479         
1480           break;
1481         case 'P':       /* Read energy parameters from paramfile, instead of using the default parameter set.
1482 .  */
1483         
1484         
1485           if (update_arg( (void *)&(args_info->paramFile_arg), 
1486                &(args_info->paramFile_orig), &(args_info->paramFile_given),
1487               &(local_args_info.paramFile_given), optarg, 0, 0, ARG_STRING,
1488               check_ambiguity, override, 0, 0,
1489               "paramFile", 'P',
1490               additional_error))
1491             goto failure;
1492         
1493           break;
1494         case 'e':       /* Rarely used option to fold sequences from the artificial ABCD... alphabet, where A pairs B, C-D etc.  Use the energy parameters for GC (-e 1) or AU (-e 2) pairs.
1495         
1496 .  */
1497         
1498         
1499           if (update_arg( (void *)&(args_info->energyModel_arg), 
1500                &(args_info->energyModel_orig), &(args_info->energyModel_given),
1501               &(local_args_info.energyModel_given), optarg, 0, 0, ARG_INT,
1502               check_ambiguity, override, 0, 0,
1503               "energyModel", 'e',
1504               additional_error))
1505             goto failure;
1506         
1507           break;
1508
1509         case 0: /* Long option with no short option */
1510           if (strcmp (long_options[option_index].name, "detailed-help") == 0) {
1511             RNAcofold_cmdline_parser_print_detailed_help ();
1512             RNAcofold_cmdline_parser_free (&local_args_info);
1513             exit (EXIT_SUCCESS);
1514           }
1515
1516           if (strcmp (long_options[option_index].name, "full-help") == 0) {
1517             RNAcofold_cmdline_parser_print_full_help ();
1518             RNAcofold_cmdline_parser_free (&local_args_info);
1519             exit (EXIT_SUCCESS);
1520           }
1521
1522           /* Do not automatically substitude nucleotide \"T\" with \"U\"
1523           
1524 .  */
1525           if (strcmp (long_options[option_index].name, "noconv") == 0)
1526           {
1527           
1528           
1529             if (update_arg((void *)&(args_info->noconv_flag), 0, &(args_info->noconv_given),
1530                 &(local_args_info.noconv_given), optarg, 0, 0, ARG_FLAG,
1531                 check_ambiguity, override, 1, 0, "noconv", '-',
1532                 additional_error))
1533               goto failure;
1534           
1535           }
1536           /* Do not produce postscript output
1537           
1538 .  */
1539           else if (strcmp (long_options[option_index].name, "noPS") == 0)
1540           {
1541           
1542           
1543             if (update_arg((void *)&(args_info->noPS_flag), 0, &(args_info->noPS_given),
1544                 &(local_args_info.noPS_given), optarg, 0, 0, ARG_FLAG,
1545                 check_ambiguity, override, 1, 0, "noPS", '-',
1546                 additional_error))
1547               goto failure;
1548           
1549           }
1550           /* Set the threshold for base pair probabilities included in the postscript output
1551 .  */
1552           else if (strcmp (long_options[option_index].name, "bppmThreshold") == 0)
1553           {
1554           
1555           
1556             if (update_arg( (void *)&(args_info->bppmThreshold_arg), 
1557                  &(args_info->bppmThreshold_orig), &(args_info->bppmThreshold_given),
1558                 &(local_args_info.bppmThreshold_given), optarg, 0, "1e-5", ARG_DOUBLE,
1559                 check_ambiguity, override, 0, 0,
1560                 "bppmThreshold", '-',
1561                 additional_error))
1562               goto failure;
1563           
1564           }
1565           /* Produce structures without lonely pairs (helices of length 1).
1566 .  */
1567           else if (strcmp (long_options[option_index].name, "noLP") == 0)
1568           {
1569           
1570           
1571             if (update_arg((void *)&(args_info->noLP_flag), 0, &(args_info->noLP_given),
1572                 &(local_args_info.noLP_given), optarg, 0, 0, ARG_FLAG,
1573                 check_ambiguity, override, 1, 0, "noLP", '-',
1574                 additional_error))
1575               goto failure;
1576           
1577           }
1578           /* Do not allow GU pairs
1579           
1580 .  */
1581           else if (strcmp (long_options[option_index].name, "noGU") == 0)
1582           {
1583           
1584           
1585             if (update_arg((void *)&(args_info->noGU_flag), 0, &(args_info->noGU_given),
1586                 &(local_args_info.noGU_given), optarg, 0, 0, ARG_FLAG,
1587                 check_ambiguity, override, 1, 0, "noGU", '-',
1588                 additional_error))
1589               goto failure;
1590           
1591           }
1592           /* Do not allow GU pairs at the end of helices
1593           
1594 .  */
1595           else if (strcmp (long_options[option_index].name, "noClosingGU") == 0)
1596           {
1597           
1598           
1599             if (update_arg((void *)&(args_info->noClosingGU_flag), 0, &(args_info->noClosingGU_given),
1600                 &(local_args_info.noClosingGU_given), optarg, 0, 0, ARG_FLAG,
1601                 check_ambiguity, override, 1, 0, "noClosingGU", '-',
1602                 additional_error))
1603               goto failure;
1604           
1605           }
1606           /* Allow other pairs in addition to the usual AU,GC,and GU pairs.
1607 .  */
1608           else if (strcmp (long_options[option_index].name, "nsp") == 0)
1609           {
1610           
1611           
1612             if (update_arg( (void *)&(args_info->nsp_arg), 
1613                  &(args_info->nsp_orig), &(args_info->nsp_given),
1614                 &(local_args_info.nsp_given), optarg, 0, 0, ARG_STRING,
1615                 check_ambiguity, override, 0, 0,
1616                 "nsp", '-',
1617                 additional_error))
1618               goto failure;
1619           
1620           }
1621           /* Set the scaling of the Boltzmann factors
1622 .  */
1623           else if (strcmp (long_options[option_index].name, "betaScale") == 0)
1624           {
1625           
1626           
1627             if (update_arg( (void *)&(args_info->betaScale_arg), 
1628                  &(args_info->betaScale_orig), &(args_info->betaScale_given),
1629                 &(local_args_info.betaScale_given), optarg, 0, "1.", ARG_DOUBLE,
1630                 check_ambiguity, override, 0, 0,
1631                 "betaScale", '-',
1632                 additional_error))
1633               goto failure;
1634           
1635           }
1636           
1637           break;
1638         case '?':       /* Invalid option.  */
1639           /* `getopt_long' already printed an error message.  */
1640           goto failure;
1641
1642         default:        /* bug: option not considered.  */
1643           fprintf (stderr, "%s: option unknown: %c%s\n", RNACOFOLD_CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
1644           abort ();
1645         } /* switch */
1646     } /* while */
1647
1648
1649
1650
1651   RNAcofold_cmdline_parser_release (&local_args_info);
1652
1653   if ( error )
1654     return (EXIT_FAILURE);
1655
1656   return 0;
1657
1658 failure:
1659   
1660   RNAcofold_cmdline_parser_release (&local_args_info);
1661   return (EXIT_FAILURE);
1662 }