Merge 'origin/release-4-6' into master
[alexxy/gromacs.git] / src / gromacs / selection / selhelp.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief
33  * Implements functions in selhelp.h.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \ingroup module_selection
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <macros.h>
43 #include <string2.h>
44 #include <wman.h>
45
46 #include "gromacs/selection/selmethod.h"
47
48 #include "selhelp.h"
49 #include "symrec.h"
50
51 /*! \internal \brief
52  * Describes a selection help section.
53  */
54 typedef struct {
55     //! Topic keyword that produces this help.
56     const char  *topic;
57     //! Number of items in the \a text array.
58     int          nl;
59     //! Help text as a list of strings that will be concatenated.
60     const char **text;
61 } t_selection_help_item;
62
63 static const char *help_common[] = {
64     "SELECTION HELP[PAR]",
65
66     "This program supports selections in addition to traditional index files.",
67     "Please read the subtopic pages (available through \"help topic\") for",
68     "more information.",
69     "Explanation of command-line arguments for specifying selections can be",
70     "found under the \"cmdline\" subtopic, and general selection syntax is",
71     "described under \"syntax\". Available keywords can be found under",
72     "\"keywords\", and concrete examples under \"examples\".",
73     "Other subtopics give more details on certain aspects.",
74     "\"help all\" prints the help for all subtopics.",
75 };
76
77 static const char *help_arithmetic[] = {
78     "ARITHMETIC EXPRESSIONS IN SELECTIONS[PAR]",
79
80     "Basic arithmetic evaluation is supported for numeric expressions.",
81     "Supported operations are addition, subtraction, negation, multiplication,",
82     "division, and exponentiation (using ^).",
83     "Result of a division by zero or other illegal operations is undefined.",
84 };
85
86 static const char *help_cmdline[] = {
87     "SELECTION COMMAND-LINE ARGUMENTS[PAR]",
88
89     "There are two alternative command-line arguments for specifying",
90     "selections:[BR]",
91     "1. [TT]-select[tt] can be used to specify the complete selection as a",
92     "string on the command line.[BR]",
93     "2. [TT]-sf[tt] can be used to specify a file name from which the",
94     "selection is read.[BR]",
95     "If both options are specified, [TT]-select[tt] takes precedence.",
96     "If neither of the above is present, the user is prompted to type the",
97     "selection on the standard input (a pipe can also be used to provide",
98     "the selections in this case).",
99     "This is also done if an empty string is passed to [TT]-select[tt].[PAR]",
100
101     "Option [TT]-n[tt] can be used to provide an index file.",
102     "If no index file is provided, default groups are generated.",
103     "In both cases, the user can also select an index group instead of",
104     "writing a full selection.",
105     "The default groups are generated by reading selections from a file",
106     "[TT]defselection.dat[tt]. If such a file is found in the current",
107     "directory, it is used instead of the one provided by default.[PAR]",
108
109     "Depending on the tool, two additional command-line arguments may be",
110     "available to control the behavior:[BR]",
111     "1. [TT]-seltype[tt] can be used to specify the default type of",
112     "positions to calculate for each selection.[BR]",
113     "2. [TT]-selrpos[tt] can be used to specify the default type of",
114     "positions used in selecting atoms by coordinates.[BR]",
115     "See \"help positions\" for more information on these options.",
116 };
117
118 static const char *help_eval[] = {
119     "SELECTION EVALUATION AND OPTIMIZATION[PAR]",
120
121     "Boolean evaluation proceeds from left to right and is short-circuiting",
122     "i.e., as soon as it is known whether an atom will be selected, the",
123     "remaining expressions are not evaluated at all.",
124     "This can be used to optimize the selections: you should write the",
125     "most restrictive and/or the most inexpensive expressions first in",
126     "boolean expressions.",
127     "The relative ordering between dynamic and static expressions does not",
128     "matter: all static expressions are evaluated only once, before the first",
129     "frame, and the result becomes the leftmost expression.[PAR]",
130
131     "Another point for optimization is in common subexpressions: they are not",
132     "automatically recognized, but can be manually optimized by the use of",
133     "variables. This can have a big impact on the performance of complex",
134     "selections, in particular if you define several index groups like this:",
135     "  [TT]rdist = distance from com of resnr 1 to 5;[tt][BR]",
136     "  [TT]resname RES and rdist < 2;[tt][BR]",
137     "  [TT]resname RES and rdist < 4;[tt][BR]",
138     "  [TT]resname RES and rdist < 6;[tt][BR]",
139     "Without the variable assignment, the distances would be evaluated three",
140     "times, although they are exactly the same within each selection.",
141     "Anything assigned into a variable becomes a common subexpression that",
142     "is evaluated only once during a frame.",
143     "Currently, in some cases the use of variables can actually lead to a small",
144     "performance loss because of the checks necessary to determine for which",
145     "atoms the expression has already been evaluated, but this should not be",
146     "a major problem.",
147 };
148
149 static const char *help_examples[] = {
150     "SELECTION EXAMPLES[PAR]",
151
152     "Below, examples of increasingly complex selections are given.[PAR]",
153
154     "Selection of all water oxygens:[BR]",
155     "  resname SOL and name OW",
156     "[PAR]",
157
158     "Centers of mass of residues 1 to 5 and 10:[BR]",
159     "  res_com of resnr 1 to 5 10",
160     "[PAR]",
161
162     "All atoms farther than 1 nm of a fixed position:[BR]",
163     "  not within 1 of (1.2, 3.1, 2.4)",
164     "[PAR]",
165
166     "All atoms of a residue LIG within 0.5 nm of a protein (with a custom name):[BR]",
167     "  \"Close to protein\" resname LIG and within 0.5 of group \"Protein\"",
168     "[PAR]",
169
170     "All protein residues that have at least one atom within 0.5 nm of a residue LIG:[BR]",
171     "  group \"Protein\" and same residue as within 0.5 of resname LIG",
172     "[PAR]",
173
174     "All RES residues whose COM is between 2 and 4 nm from the COM of all of them:[BR]",
175     "  rdist = res_com distance from com of resname RES[BR]",
176     "  resname RES and rdist >= 2 and rdist <= 4",
177     "[PAR]",
178
179     "Selection like C1 C2 C2 C3 C3 C4 ... C8 C9 (e.g., for g_bond):[BR]",
180     "  name \"C[1-8]\" merge name \"C[2-9]\"",
181 };
182
183 static const char *help_keywords[] = {
184     "SELECTION KEYWORDS[PAR]",
185
186     "The following selection keywords are currently available.",
187     "For keywords marked with a star, additional help is available through",
188     "\"help KEYWORD\", where KEYWORD is the name of the keyword.",
189 };
190
191 static const char *help_limits[] = {
192     "SELECTION LIMITATIONS[PAR]",
193
194     "Some analysis programs may require a special structure for the input",
195     "selections (e.g., [TT]g_angle[tt] requires the index group to be made",
196     "of groups of three or four atoms).",
197     "For such programs, it is up to the user to provide a proper selection",
198     "expression that always returns such positions.",
199     "[PAR]",
200
201     "Due to technical reasons, having a negative value as the first value in",
202     "expressions like[BR]",
203     "[TT]charge -1 to -0.7[tt][BR]",
204     "result in a syntax error. A workaround is to write[BR]",
205     "[TT]charge {-1 to -0.7}[tt][BR]",
206     "instead.",
207 };
208
209 static const char *help_positions[] = {
210     "SPECIFYING POSITIONS[PAR]",
211
212     "Possible ways of specifying positions in selections are:[PAR]",
213
214     "1. A constant position can be defined as [TT][XX, YY, ZZ][tt], where",
215     "[TT]XX[tt], [TT]YY[tt] and [TT]ZZ[tt] are real numbers.[PAR]",
216
217     "2. [TT]com of ATOM_EXPR [pbc][tt] or [TT]cog of ATOM_EXPR [pbc][tt]",
218     "calculate the center of mass/geometry of [TT]ATOM_EXPR[tt]. If",
219     "[TT]pbc[tt] is specified, the center is calculated iteratively to try",
220     "to deal with cases where [TT]ATOM_EXPR[tt] wraps around periodic",
221     "boundary conditions.[PAR]",
222
223     "3. [TT]POSTYPE of ATOM_EXPR[tt] calculates the specified positions for",
224     "the atoms in [TT]ATOM_EXPR[tt].",
225     "[TT]POSTYPE[tt] can be [TT]atom[tt], [TT]res_com[tt], [TT]res_cog[tt],",
226     "[TT]mol_com[tt] or [TT]mol_cog[tt], with an optional prefix [TT]whole_[tt]",
227     "[TT]part_[tt] or [TT]dyn_[tt].",
228     "[TT]whole_[tt] calculates the centers for the whole residue/molecule,",
229     "even if only part of it is selected.",
230     "[TT]part_[tt] prefix calculates the centers for the selected atoms, but",
231     "uses always the same atoms for the same residue/molecule. The used atoms",
232     "are determined from the the largest group allowed by the selection.",
233     "[TT]dyn_[tt] calculates the centers strictly only for the selected atoms.",
234     "If no prefix is specified, whole selections default to [TT]part_[tt] and",
235     "other places default to [TT]whole_[tt].",
236     "The latter is often desirable to select the same molecules in different",
237     "tools, while the first is a compromise between speed ([TT]dyn_[tt]",
238     "positions can be slower to evaluate than [TT]part_[tt]) and intuitive",
239     "behavior.[PAR]",
240
241     "4. [TT]ATOM_EXPR[tt], when given for whole selections, is handled as 3.",
242     "above, using the position type from the command-line argument",
243     "[TT]-seltype[tt].[PAR]",
244
245     "Selection keywords that select atoms based on their positions, such as",
246     "[TT]dist from[tt], use by default the positions defined by the",
247     "[TT]-selrpos[tt] command-line option.",
248     "This can be overridden by prepending a [TT]POSTYPE[tt] specifier to the",
249     "keyword. For example, [TT]res_com dist from POS[tt] evaluates the",
250     "residue center of mass distances. In the example, all atoms of a residue",
251     "are either selected or not, based on the single distance calculated.",
252 };
253
254 static const char *help_syntax[] = {
255     "SELECTION SYNTAX[PAR]",
256
257     "A set of selections consists of one or more selections, separated by",
258     "semicolons. Each selection defines a set of positions for the analysis.",
259     "Each selection can also be preceded by a string that gives a name for",
260     "the selection for use in, e.g., graph legends.",
261     "If no name is provided, the string used for the selection is used",
262     "automatically as the name.[PAR]",
263
264     "For interactive input, the syntax is slightly altered: line breaks can",
265     "also be used to separate selections. \\ followed by a line break can",
266     "be used to continue a line if necessary.",
267     "Notice that the above only applies to real interactive input,",
268     "not if you provide the selections, e.g., from a pipe.[PAR]",
269
270     "It is possible to use variables to store selection expressions.",
271     "A variable is defined with the following syntax:[BR]",
272     "[TT]VARNAME = EXPR ;[tt][BR]",
273     "where [TT]EXPR[tt] is any valid selection expression.",
274     "After this, [TT]VARNAME[tt] can be used anywhere where [TT]EXPR[tt]",
275     "would be valid.[PAR]",
276
277     "Selections are composed of three main types of expressions, those that",
278     "define atoms ([TT]ATOM_EXPR[tt]s), those that define positions",
279     "([TT]POS_EXPR[tt]s), and those that evaluate to numeric values",
280     "([TT]NUM_EXPR[tt]s). Each selection should be a [TT]POS_EXPR[tt]",
281     "or a [TT]ATOM_EXPR[tt] (the latter is automatically converted to",
282     "positions). The basic rules are as follows:[BR]",
283     "1. An expression like [TT]NUM_EXPR1 < NUM_EXPR2[tt] evaluates to an",
284     "[TT]ATOM_EXPR[tt] that selects all the atoms for which the comparison",
285     "is true.[BR]",
286     "2. Atom expressions can be combined with boolean operations such as",
287     "[TT]not ATOM_EXPR[tt], [TT]ATOM_EXPR and ATOM_EXPR[tt], or",
288     "[TT]ATOM_EXPR or ATOM_EXPR[tt]. Parentheses can be used to alter the",
289     "evaluation order.[BR]",
290     "3. [TT]ATOM_EXPR[tt] expressions can be converted into [TT]POS_EXPR[tt]",
291     "expressions in various ways, see \"help positions\" for more details.[PAR]",
292
293     "Some keywords select atoms based on string values such as the atom name.",
294     "For these keywords, it is possible to use wildcards ([TT]name \"C*\"[tt])",
295     "or regular expressions (e.g., [TT]resname \"R[AB]\"[tt]).",
296     "The match type is automatically guessed from the string: if it contains",
297     "other characters than letters, numbers, '*', or '?', it is interpreted",
298     "as a regular expression.",
299     "Strings that contain non-alphanumeric characters should be enclosed in",
300     "double quotes as in the examples. For other strings, the quotes are",
301     "optional, but if the value conflicts with a reserved keyword, a syntax",
302     "error will occur. If your strings contain uppercase letters, this should",
303     "not happen.[PAR]",
304
305     "Index groups provided with the [TT]-n[tt] command-line option or",
306     "generated by default can be accessed with [TT]group NR[tt] or",
307     "[TT]group NAME[tt], where [TT]NR[tt] is a zero-based index of the group",
308     "and [TT]NAME[tt] is part of the name of the desired group.",
309     "The keyword [TT]group[tt] is optional if the whole selection is",
310     "provided from an index group.",
311     "To see a list of available groups in the interactive mode, press enter",
312     "in the beginning of a line.",
313 };
314
315 static const t_selection_help_item helpitems[] = {
316     {NULL,          asize(help_common),     help_common},
317     {"cmdline",     asize(help_cmdline),    help_cmdline},
318     {"syntax",      asize(help_syntax),     help_syntax},
319     {"positions",   asize(help_positions),  help_positions},
320     {"arithmetic",  asize(help_arithmetic), help_arithmetic},
321     {"keywords",    asize(help_keywords),   help_keywords},
322     {"evaluation",  asize(help_eval),       help_eval},
323     {"limitations", asize(help_limits),     help_limits},
324     {"examples",    asize(help_examples),   help_examples},
325 };
326
327 /*! \brief
328  * Prints a brief list of keywords (selection methods) available.
329  *
330  * \param[in] fp    Where to write the list.
331  * \param[in] symtab  Symbol table to use to find available keywords.
332  * \param[in] type  Only methods that return this type are printed.
333  * \param[in] bMod  If false, \ref SMETH_MODIFIER methods are excluded, otherwise
334  *     only them are printed.
335  */
336 static void
337 print_keyword_list(FILE *fp, gmx_sel_symtab_t *symtab, e_selvalue_t type,
338                    bool bMod)
339 {
340     gmx_sel_symrec_t *symbol;
341
342     symbol = _gmx_sel_first_symbol(symtab, SYMBOL_METHOD);
343     while (symbol)
344     {
345         gmx_ana_selmethod_t *method = _gmx_sel_sym_value_method(symbol);
346         bool                 bShow;
347         bShow = (method->type == type)
348             && ((bMod && (method->flags & SMETH_MODIFIER))
349                 || (!bMod && !(method->flags & SMETH_MODIFIER)));
350         if (bShow)
351         {
352             fprintf(fp, " %c ",
353                     (method->help.nlhelp > 0 && method->help.help) ? '*' : ' ');
354             if (method->help.syntax)
355             {
356                 fprintf(fp, "%s\n", method->help.syntax);
357             }
358             else
359             {
360                 const char *symname = _gmx_sel_sym_name(symbol);
361
362                 fprintf(fp, "%s", symname);
363                 if (strcmp(symname, method->name) != 0)
364                 {
365                     fprintf(fp, " (synonym for %s)", method->name);
366                 }
367                 fprintf(fp, "\n");
368             }
369         }
370         symbol = _gmx_sel_next_symbol(symbol, SYMBOL_METHOD);
371     }
372 }
373
374 /*!
375  * \param[in]  fp      Where to write the help.
376  * \param[in]  symtab  Symbol table to use to find available keywords.
377  * \param[in]  topic Topic to print help on, or NULL for general help.
378  *
379  * \p symtab is used to get information on which keywords are available in the
380  * present context.
381  */
382 void
383 _gmx_sel_print_help(FILE *fp, gmx_sel_symtab_t *symtab, const char *topic)
384 {
385     const t_selection_help_item *item = NULL;
386     size_t i;
387
388     /* Find the item for the topic */
389     if (!topic)
390     {
391         item = &helpitems[0];
392     }
393     else if (strcmp(topic, "all") == 0)
394     {
395         for (i = 0; i < asize(helpitems); ++i)
396         {
397             item = &helpitems[i];
398             _gmx_sel_print_help(fp, symtab, item->topic);
399             if (i != asize(helpitems) - 1)
400             {
401                 fprintf(fp, "\n\n");
402             }
403         }
404         return;
405     }
406     else
407     {
408         for (i = 1; i < asize(helpitems); ++i)
409         {
410             if (strncmp(helpitems[i].topic, topic, strlen(topic)) == 0)
411             {
412                 item = &helpitems[i];
413                 break;
414             }
415         }
416     }
417     /* If the topic is not found, check the available methods.
418      * If they don't provide any help either, tell the user and exit. */
419     if (!item)
420     {
421         gmx_sel_symrec_t *symbol;
422
423         symbol = _gmx_sel_first_symbol(symtab, SYMBOL_METHOD);
424         while (symbol)
425         {
426             gmx_ana_selmethod_t *method = _gmx_sel_sym_value_method(symbol);
427             if (method->help.nlhelp > 0 && method->help.help
428                 && strncmp(method->name, topic, strlen(topic)) == 0)
429             {
430                 print_tty_formatted(fp, method->help.nlhelp,
431                         method->help.help, 0, NULL, NULL, false);
432                 return;
433             }
434             symbol = _gmx_sel_next_symbol(symbol, SYMBOL_METHOD);
435         }
436
437         fprintf(fp, "No help available for '%s'.\n", topic);
438         return;
439     }
440     /* Print the help */
441     print_tty_formatted(fp, item->nl, item->text, 0, NULL, NULL, false);
442     /* Special handling of certain pages */
443     if (!topic)
444     {
445         int len = 0;
446
447         /* Print the subtopics on the main page */
448         fprintf(fp, "\nAvailable subtopics:\n");
449         for (i = 1; i < asize(helpitems); ++i)
450         {
451             int len1 = strlen(helpitems[i].topic) + 2;
452
453             len += len1;
454             if (len > 79)
455             {
456                 fprintf(fp, "\n");
457                 len = len1;
458             }
459             fprintf(fp, "  %s", helpitems[i].topic);
460         }
461         fprintf(fp, "\n");
462     }
463     else if (strcmp(item->topic, "keywords") == 0)
464     {
465         /* Print the list of keywords */
466         fprintf(fp, "\nKeywords that select atoms by an integer property:\n");
467         fprintf(fp, "(use in expressions or like \"atomnr 1 to 5 7 9\")\n");
468         print_keyword_list(fp, symtab, INT_VALUE, false);
469
470         fprintf(fp, "\nKeywords that select atoms by a numeric property:\n");
471         fprintf(fp, "(use in expressions or like \"occupancy 0.5 to 1\")\n");
472         print_keyword_list(fp, symtab, REAL_VALUE, false);
473
474         fprintf(fp, "\nKeywords that select atoms by a string property:\n");
475         fprintf(fp, "(use like \"name PATTERN [PATTERN] ...\")\n");
476         print_keyword_list(fp, symtab, STR_VALUE, false);
477
478         fprintf(fp, "\nAdditional keywords that directly select atoms:\n");
479         print_keyword_list(fp, symtab, GROUP_VALUE, false);
480
481         fprintf(fp, "\nKeywords that directly evaluate to positions:\n");
482         fprintf(fp, "(see also \"help positions\")\n");
483         print_keyword_list(fp, symtab, POS_VALUE, false);
484
485         fprintf(fp, "\nAdditional keywords:\n");
486         print_keyword_list(fp, symtab, POS_VALUE, true);
487         print_keyword_list(fp, symtab, NO_VALUE, true);
488     }
489 }