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