e2972d9d091d95d02bb0067e962e071931022abf
[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     "[PAR]",
190
191     "Due to technical reasons, having a negative value as the first value in",
192     "expressions like[BR]",
193     "[TT]charge -1 to -0.7[tt][BR]",
194     "result in a syntax error. A workaround is to write[BR]",
195     "[TT]charge {-1 to -0.7}[tt][BR]",
196     "instead.[PAR]",
197
198     "When [TT]name[tt] selection keyword is used together with PDB input",
199     "files, the behavior may be unintuitive. When Gromacs reads in a PDB",
200     "file, 4 character atom names that start with a digit are transformed",
201     "such that, e.g., 1HG2 becomes HG21, and the latter is what is matched",
202     "by the [TT]name[tt] keyword. Use [TT]pdbname[tt] to match the atom name",
203     "as it appears in the input PDB file.",
204 };
205
206 static const char *help_positions[] = {
207     "SPECIFYING POSITIONS[PAR]",
208
209     "Possible ways of specifying positions in selections are:[PAR]",
210
211     "1. A constant position can be defined as [TT][XX, YY, ZZ][tt], where",
212     "[TT]XX[tt], [TT]YY[tt] and [TT]ZZ[tt] are real numbers.[PAR]",
213
214     "2. [TT]com of ATOM_EXPR [pbc][tt] or [TT]cog of ATOM_EXPR [pbc][tt]",
215     "calculate the center of mass/geometry of [TT]ATOM_EXPR[tt]. If",
216     "[TT]pbc[tt] is specified, the center is calculated iteratively to try",
217     "to deal with cases where [TT]ATOM_EXPR[tt] wraps around periodic",
218     "boundary conditions.[PAR]",
219
220     "3. [TT]POSTYPE of ATOM_EXPR[tt] calculates the specified positions for",
221     "the atoms in [TT]ATOM_EXPR[tt].",
222     "[TT]POSTYPE[tt] can be [TT]atom[tt], [TT]res_com[tt], [TT]res_cog[tt],",
223     "[TT]mol_com[tt] or [TT]mol_cog[tt], with an optional prefix [TT]whole_[tt]",
224     "[TT]part_[tt] or [TT]dyn_[tt].",
225     "[TT]whole_[tt] calculates the centers for the whole residue/molecule,",
226     "even if only part of it is selected.",
227     "[TT]part_[tt] prefix calculates the centers for the selected atoms, but",
228     "uses always the same atoms for the same residue/molecule. The used atoms",
229     "are determined from the the largest group allowed by the selection.",
230     "[TT]dyn_[tt] calculates the centers strictly only for the selected atoms.",
231     "If no prefix is specified, whole selections default to [TT]part_[tt] and",
232     "other places default to [TT]whole_[tt].",
233     "The latter is often desirable to select the same molecules in different",
234     "tools, while the first is a compromise between speed ([TT]dyn_[tt]",
235     "positions can be slower to evaluate than [TT]part_[tt]) and intuitive",
236     "behavior.[PAR]",
237
238     "4. [TT]ATOM_EXPR[tt], when given for whole selections, is handled as 3.",
239     "above, using the position type from the command-line argument",
240     "[TT]-seltype[tt].[PAR]",
241
242     "Selection keywords that select atoms based on their positions, such as",
243     "[TT]dist from[tt], use by default the positions defined by the",
244     "[TT]-selrpos[tt] command-line option.",
245     "This can be overridden by prepending a [TT]POSTYPE[tt] specifier to the",
246     "keyword. For example, [TT]res_com dist from POS[tt] evaluates the",
247     "residue center of mass distances. In the example, all atoms of a residue",
248     "are either selected or not, based on the single distance calculated.",
249 };
250
251 static const char *help_syntax[] = {
252     "SELECTION SYNTAX[PAR]",
253
254     "A set of selections consists of one or more selections, separated by",
255     "semicolons. Each selection defines a set of positions for the analysis.",
256     "Each selection can also be preceded by a string that gives a name for",
257     "the selection for use in, e.g., graph legends.",
258     "If no name is provided, the string used for the selection is used",
259     "automatically as the name.[PAR]",
260
261     "For interactive input, the syntax is slightly altered: line breaks can",
262     "also be used to separate selections. \\ followed by a line break can",
263     "be used to continue a line if necessary.",
264     "Notice that the above only applies to real interactive input,",
265     "not if you provide the selections, e.g., from a pipe.[PAR]",
266
267     "It is possible to use variables to store selection expressions.",
268     "A variable is defined with the following syntax:[BR]",
269     "[TT]VARNAME = EXPR ;[tt][BR]",
270     "where [TT]EXPR[tt] is any valid selection expression.",
271     "After this, [TT]VARNAME[tt] can be used anywhere where [TT]EXPR[tt]",
272     "would be valid.[PAR]",
273
274     "Selections are composed of three main types of expressions, those that",
275     "define atoms ([TT]ATOM_EXPR[tt]s), those that define positions",
276     "([TT]POS_EXPR[tt]s), and those that evaluate to numeric values",
277     "([TT]NUM_EXPR[tt]s). Each selection should be a [TT]POS_EXPR[tt]",
278     "or a [TT]ATOM_EXPR[tt] (the latter is automatically converted to",
279     "positions). The basic rules are as follows:[BR]",
280     "1. An expression like [TT]NUM_EXPR1 < NUM_EXPR2[tt] evaluates to an",
281     "[TT]ATOM_EXPR[tt] that selects all the atoms for which the comparison",
282     "is true.[BR]",
283     "2. Atom expressions can be combined with gmx_boolean operations such as",
284     "[TT]not ATOM_EXPR[tt], [TT]ATOM_EXPR and ATOM_EXPR[tt], or",
285     "[TT]ATOM_EXPR or ATOM_EXPR[tt]. Parentheses can be used to alter the",
286     "evaluation order.[BR]",
287     "3. [TT]ATOM_EXPR[tt] expressions can be converted into [TT]POS_EXPR[tt]",
288     "expressions in various ways, see \"help positions\" for more details.[PAR]",
289
290     "Some keywords select atoms based on string values such as the atom name.",
291     "For these keywords, it is possible to use wildcards ([TT]name \"C*\"[tt])",
292     "or regular expressions (e.g., [TT]resname \"R[AB]\"[tt]).",
293     "The match type is automatically guessed from the string: if it contains",
294     "other characters than letters, numbers, '*', or '?', it is interpreted",
295     "as a regular expression.",
296     "Strings that contain non-alphanumeric characters should be enclosed in",
297     "double quotes as in the examples. For other strings, the quotes are",
298     "optional, but if the value conflicts with a reserved keyword, a syntax",
299     "error will occur. If your strings contain uppercase letters, this should",
300     "not happen.[PAR]",
301
302     "Index groups provided with the [TT]-n[tt] command-line option or",
303     "generated by default can be accessed with [TT]group NR[tt] or",
304     "[TT]group NAME[tt], where [TT]NR[tt] is a zero-based index of the group",
305     "and [TT]NAME[tt] is part of the name of the desired group.",
306     "The keyword [TT]group[tt] is optional if the whole selection is",
307     "provided from an index group.",
308     "To see a list of available groups in the interactive mode, press enter",
309     "in the beginning of a line.",
310 };
311
312 static const t_selection_help_item helpitems[] = {
313     {NULL,          asize(help_common),     help_common},
314     {"cmdline",     asize(help_cmdline),    help_cmdline},
315     {"syntax",      asize(help_syntax),     help_syntax},
316     {"positions",   asize(help_positions),  help_positions},
317     {"arithmetic",  asize(help_arithmetic), help_arithmetic},
318     {"keywords",    asize(help_keywords),   help_keywords},
319     {"evaluation",  asize(help_eval),       help_eval},
320     {"limitations", asize(help_limits),     help_limits},
321     {"examples",    asize(help_examples),   help_examples},
322 };
323
324 /*! \brief
325  * Prints a brief list of keywords (selection methods) available.
326  *
327  * \param[in] sc    Selection collection for which the list should be printed.
328  * \param[in] type  Only methods that return this type are printed.
329  * \param[in] bMod  If FALSE, \ref SMETH_MODIFIER methods are excluded, otherwise
330  *     only them are printed.
331  */
332 static void
333 print_keyword_list(struct gmx_ana_selcollection_t *sc, e_selvalue_t type,
334                    gmx_bool bMod)
335 {
336     gmx_sel_symrec_t *symbol;
337
338     symbol = _gmx_sel_first_symbol(sc->symtab, SYMBOL_METHOD);
339     while (symbol)
340     {
341         gmx_ana_selmethod_t *method = _gmx_sel_sym_value_method(symbol);
342         gmx_bool                 bShow;
343         bShow = (method->type == type)
344             && ((bMod && (method->flags & SMETH_MODIFIER))
345                 || (!bMod && !(method->flags & SMETH_MODIFIER)));
346         if (bShow)
347         {
348             fprintf(stderr, " %c ",
349                     (method->help.nlhelp > 0 && method->help.help) ? '*' : ' ');
350             if (method->help.syntax)
351             {
352                 fprintf(stderr, "%s\n", method->help.syntax);
353             }
354             else
355             {
356                 const char *symname = _gmx_sel_sym_name(symbol);
357
358                 fprintf(stderr, "%s", symname);
359                 if (strcmp(symname, method->name) != 0)
360                 {
361                     fprintf(stderr, " (synonym for %s)", method->name);
362                 }
363                 fprintf(stderr, "\n");
364             }
365         }
366         symbol = _gmx_sel_next_symbol(symbol, SYMBOL_METHOD);
367     }
368 }
369
370 /*!
371  * \param[in]  sc    Selection collection for which help should be printed.
372  * \param[in]  topic Topic to print help on, or NULL for general help.
373  *
374  * \p sc is used to get information on which keywords are available in the
375  * present context.
376  */
377 void
378 _gmx_sel_print_help(struct gmx_ana_selcollection_t *sc, const char *topic)
379 {
380     const t_selection_help_item *item = NULL;
381     size_t i;
382
383     /* Find the item for the topic */
384     if (!topic)
385     {
386         item = &helpitems[0];
387     }
388     else if (strcmp(topic, "all") == 0)
389     {
390         for (i = 0; i < asize(helpitems); ++i)
391         {
392             item = &helpitems[i];
393             _gmx_sel_print_help(sc, item->topic);
394             if (i != asize(helpitems) - 1)
395             {
396                 fprintf(stderr, "\n\n");
397             }
398         }
399         return;
400     }
401     else
402     {
403         for (i = 1; i < asize(helpitems); ++i)
404         {
405             if (strncmp(helpitems[i].topic, topic, strlen(topic)) == 0)
406             {
407                 item = &helpitems[i];
408                 break;
409             }
410         }
411     }
412     /* If the topic is not found, check the available methods.
413      * If they don't provide any help either, tell the user and exit. */
414     if (!item)
415     {
416         gmx_sel_symrec_t *symbol;
417
418         symbol = _gmx_sel_first_symbol(sc->symtab, SYMBOL_METHOD);
419         while (symbol)
420         {
421             gmx_ana_selmethod_t *method = _gmx_sel_sym_value_method(symbol);
422             if (method->help.nlhelp > 0 && method->help.help
423                 && strncmp(method->name, topic, strlen(topic)) == 0)
424             {
425                 print_tty_formatted(stderr, method->help.nlhelp,
426                         method->help.help, 0, NULL, NULL, FALSE);
427                 return;
428             }
429             symbol = _gmx_sel_next_symbol(symbol, SYMBOL_METHOD);
430         }
431
432         fprintf(stderr, "No help available for '%s'.\n", topic);
433         return;
434     }
435     /* Print the help */
436     print_tty_formatted(stderr, item->nl, item->text, 0, NULL, NULL, FALSE);
437     /* Special handling of certain pages */
438     if (!topic)
439     {
440         int len = 0;
441
442         /* Print the subtopics on the main page */
443         fprintf(stderr, "\nAvailable subtopics:\n");
444         for (i = 1; i < asize(helpitems); ++i)
445         {
446             int len1 = strlen(helpitems[i].topic) + 2;
447
448             len += len1;
449             if (len > 79)
450             {
451                 fprintf(stderr, "\n");
452                 len = len1;
453             }
454             fprintf(stderr, "  %s", helpitems[i].topic);
455         }
456         fprintf(stderr, "\n");
457     }
458     else if (strcmp(item->topic, "keywords") == 0)
459     {
460         /* Print the list of keywords */
461         fprintf(stderr, "\nKeywords that select atoms by an integer property:\n");
462         fprintf(stderr, "(use in expressions or like \"atomnr 1 to 5 7 9\")\n");
463         print_keyword_list(sc, INT_VALUE, FALSE);
464
465         fprintf(stderr, "\nKeywords that select atoms by a numeric property:\n");
466         fprintf(stderr, "(use in expressions or like \"occupancy 0.5 to 1\")\n");
467         print_keyword_list(sc, REAL_VALUE, FALSE);
468
469         fprintf(stderr, "\nKeywords that select atoms by a string property:\n");
470         fprintf(stderr, "(use like \"name PATTERN [PATTERN] ...\")\n");
471         print_keyword_list(sc, STR_VALUE, FALSE);
472
473         fprintf(stderr, "\nAdditional keywords that directly select atoms:\n");
474         print_keyword_list(sc, GROUP_VALUE, FALSE);
475
476         fprintf(stderr, "\nKeywords that directly evaluate to positions:\n");
477         fprintf(stderr, "(see also \"help positions\")\n");
478         print_keyword_list(sc, POS_VALUE, FALSE);
479
480         fprintf(stderr, "\nAdditional keywords:\n");
481         print_keyword_list(sc, POS_VALUE, TRUE);
482         print_keyword_list(sc, NO_VALUE, TRUE);
483     }
484 }