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