3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
29 * For more info, check our website at http://www.gromacs.org
32 * \brief Implementation of functions in selhelp.c.
42 #include "selcollection.h"
43 #include "selmethod.h"
51 } t_selection_help_item;
53 static const char *help_common[] = {
54 "SELECTION HELP[PAR]",
56 "This program supports selections in addition to traditional index files.",
57 "Please read the subtopic pages (available through \"help topic\") for",
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.",
67 static const char *help_arithmetic[] = {
68 "ARITHMETIC EXPRESSIONS IN SELECTIONS[PAR]",
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.",
76 static const char *help_cmdline[] = {
77 "SELECTION COMMAND-LINE ARGUMENTS[PAR]",
79 "There are two alternative command-line arguments for specifying",
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]",
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]",
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.",
108 static const char *help_eval[] = {
109 "SELECTION EVALUATION AND OPTIMIZATION[PAR]",
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 "gmx_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]",
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",
139 static const char *help_examples[] = {
140 "SELECTION EXAMPLES[PAR]",
142 "Below, examples of increasingly complex selections are given.[PAR]",
144 "Selection of all water oxygens:[BR]",
145 " resname SOL and name OW",
148 "Centers of mass of residues 1 to 5 and 10:[BR]",
149 " res_com of resnr 1 to 5 10",
152 "All atoms farther than 1 nm of a fixed position:[BR]",
153 " not within 1 of (1.2, 3.1, 2.4)",
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\"",
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",
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",
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]\"",
173 static const char *help_keywords[] = {
174 "SELECTION KEYWORDS[PAR]",
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.",
181 static const char *help_limits[] = {
182 "SELECTION LIMITATIONS[PAR]",
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.",
191 static const char *help_positions[] = {
192 "SPECIFYING POSITIONS[PAR]",
194 "Possible ways of specifying positions in selections are:[PAR]",
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]",
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]",
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",
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]",
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.",
236 static const char *help_syntax[] = {
237 "SELECTION SYNTAX[PAR]",
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]",
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]",
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]",
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",
268 "2. Atom expressions can be combined with gmx_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]",
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",
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.",
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},
310 * Prints a brief list of keywords (selection methods) available.
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.
318 print_keyword_list(struct gmx_ana_selcollection_t *sc, e_selvalue_t type,
321 gmx_sel_symrec_t *symbol;
323 symbol = _gmx_sel_first_symbol(sc->symtab, SYMBOL_METHOD);
326 gmx_ana_selmethod_t *method = _gmx_sel_sym_value_method(symbol);
328 bShow = (method->type == type)
329 && ((bMod && (method->flags & SMETH_MODIFIER))
330 || (!bMod && !(method->flags & SMETH_MODIFIER)));
333 fprintf(stderr, " %c ",
334 (method->help.nlhelp > 0 && method->help.help) ? '*' : ' ');
335 if (method->help.syntax)
337 fprintf(stderr, "%s\n", method->help.syntax);
341 const char *symname = _gmx_sel_sym_name(symbol);
343 fprintf(stderr, "%s", symname);
344 if (strcmp(symname, method->name) != 0)
346 fprintf(stderr, " (synonym for %s)", method->name);
348 fprintf(stderr, "\n");
351 symbol = _gmx_sel_next_symbol(symbol, SYMBOL_METHOD);
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.
359 * \p sc is used to get information on which keywords are available in the
363 _gmx_sel_print_help(struct gmx_ana_selcollection_t *sc, const char *topic)
365 const t_selection_help_item *item = NULL;
368 /* Find the item for the topic */
371 item = &helpitems[0];
373 else if (strcmp(topic, "all") == 0)
375 for (i = 0; i < asize(helpitems); ++i)
377 item = &helpitems[i];
378 _gmx_sel_print_help(sc, item->topic);
379 if (i != asize(helpitems) - 1)
381 fprintf(stderr, "\n\n");
388 for (i = 1; i < asize(helpitems); ++i)
390 if (strncmp(helpitems[i].topic, topic, strlen(topic)) == 0)
392 item = &helpitems[i];
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. */
401 gmx_sel_symrec_t *symbol;
403 symbol = _gmx_sel_first_symbol(sc->symtab, SYMBOL_METHOD);
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)
410 print_tty_formatted(stderr, method->help.nlhelp,
411 method->help.help, 0, NULL, NULL, FALSE);
414 symbol = _gmx_sel_next_symbol(symbol, SYMBOL_METHOD);
417 fprintf(stderr, "No help available for '%s'.\n", topic);
421 print_tty_formatted(stderr, item->nl, item->text, 0, NULL, NULL, FALSE);
422 /* Special handling of certain pages */
427 /* Print the subtopics on the main page */
428 fprintf(stderr, "\nAvailable subtopics:\n");
429 for (i = 1; i < asize(helpitems); ++i)
431 int len1 = strlen(helpitems[i].topic) + 2;
436 fprintf(stderr, "\n");
439 fprintf(stderr, " %s", helpitems[i].topic);
441 fprintf(stderr, "\n");
443 else if (strcmp(item->topic, "keywords") == 0)
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);
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);
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);
458 fprintf(stderr, "\nAdditional keywords that directly select atoms:\n");
459 print_keyword_list(sc, GROUP_VALUE, FALSE);
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);
465 fprintf(stderr, "\nAdditional keywords:\n");
466 print_keyword_list(sc, POS_VALUE, TRUE);
467 print_keyword_list(sc, NO_VALUE, TRUE);