2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements functions in selhelp.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
46 #include <boost/scoped_ptr.hpp>
48 #include "gromacs/onlinehelp/helptopic.h"
49 #include "gromacs/onlinehelp/helpwritercontext.h"
50 #include "gromacs/utility/exceptions.h"
51 #include "gromacs/utility/file.h"
52 #include "gromacs/utility/stringutil.h"
55 #include "selmethod.h"
63 static const char name[];
64 static const char title[];
65 static const char *const text[];
68 const char CommonHelpText::name[] = "selections";
69 const char CommonHelpText::title[] =
70 "Selection syntax and usage";
71 const char *const CommonHelpText::text[] = {
72 "Selections are used to select atoms/molecules/residues for analysis.",
73 "In contrast to traditional index files, selections can be dynamic, i.e.,",
74 "select different atoms for different trajectory frames.[PAR]",
76 "Each analysis tool requires a different number of selections and the",
77 "selections are interpreted differently. The general idea is still the",
78 "same: each selection evaluates to a set of positions, where a position",
79 "can be an atom position or center-of-mass or center-of-geometry of",
80 "a set of atoms. The tool then uses these positions for its analysis to",
81 "allow very flexible processing. Some analysis tools may have limitations",
82 "on the types of selections allowed.[PAR]",
84 "To get started with selections, run, e.g., [TT][PROGRAM] select[tt]",
85 "without specifying selections on the command-line and use the interactive",
86 "prompt to try out different selections.",
87 "This tool provides output options that allow one to see what is actually",
88 "selected by the given selections, and the interactive prompt reports",
89 "syntax errors immediately, allowing one to try again.",
90 "The subtopics listed below give more details on different aspects of",
94 struct ArithmeticHelpText
96 static const char name[];
97 static const char title[];
98 static const char *const text[];
101 const char ArithmeticHelpText::name[] = "arithmetic";
102 const char ArithmeticHelpText::title[] =
103 "Arithmetic expressions in selections";
104 const char *const ArithmeticHelpText::text[] = {
105 "Basic arithmetic evaluation is supported for numeric expressions.",
106 "Supported operations are addition, subtraction, negation, multiplication,",
107 "division, and exponentiation (using ^).",
108 "Result of a division by zero or other illegal operations is undefined.",
111 struct CmdLineHelpText
113 static const char name[];
114 static const char title[];
115 static const char *const text[];
118 const char CmdLineHelpText::name[] = "cmdline";
119 const char CmdLineHelpText::title[] =
120 "Specifying selections from command line";
121 const char *const CmdLineHelpText::text[] = {
122 "If no selections are provided on the command line, you are prompted to",
123 "type the selections interactively (a pipe can also be used to provide",
124 "the selections in this case for most tools). While this works well for",
125 "testing, it is easier to provide the selections from the command line",
126 "if they are complex or for scripting.[PAR]",
128 "Each tool has different command-line arguments for specifying selections",
129 "(listed by [TT][PROGRAM] help <tool>[tt]).",
130 "You can either pass a single string containing all selections (separated",
131 "by semicolons), or multiple strings, each containing one selection.",
132 "Note that you need to quote the selections to protect them from the",
135 "If you set a selection command-line argument, but do not provide any",
136 "selections, you are prompted to type the selections for that argument",
137 "interactively. This is useful if that selection argument is optional,",
138 "in which case it is not normally prompted for.[PAR]",
140 "To provide selections from a file, use [TT]-sf file.dat[tt] in the place",
141 "of the selection for a selection argument (e.g.,",
142 "[TT]-select -sf file.dat[tt]). In general, the [TT]-sf[tt] argument reads",
143 "selections from the provided file and assigns them to selection arguments",
144 "that have been specified up to that point, but for which no selections",
145 "have been provided.",
146 "As a special case, [TT]-sf[tt] provided on its own, without preceding",
147 "selection arguments, assigns the selections to all (yet unset) required",
148 "selections (i.e., those that would be promted interactively if no",
149 "selections are provided on the command line).[PAR]",
151 "To use groups from a traditional index file, use argument [TT]-n[tt]",
152 "to provide a file. See the \"syntax\" subtopic for how to use them.",
153 "If this option is not provided, default groups are generated.",
154 "The default groups are generated by reading selections from a file",
155 "[TT]defselection.dat[tt]. If such a file is found in the current",
156 "directory, it is used instead of the one provided by default.[PAR]",
158 "Depending on the tool, two additional command-line arguments may be",
159 "available to control the behavior:[BR]",
160 "1. [TT]-seltype[tt] can be used to specify the default type of",
161 "positions to calculate for each selection.[BR]",
162 "2. [TT]-selrpos[tt] can be used to specify the default type of",
163 "positions used in selecting atoms by coordinates.[BR]",
164 "See the \"positions\" subtopic for more information on these options.",
167 struct EvaluationHelpText
169 static const char name[];
170 static const char title[];
171 static const char *const text[];
174 const char EvaluationHelpText::name[] = "evaluation";
175 const char EvaluationHelpText::title[] =
176 "Selection evaluation and optimization";
177 const char *const EvaluationHelpText::text[] = {
178 "Boolean evaluation proceeds from left to right and is short-circuiting",
179 "i.e., as soon as it is known whether an atom will be selected, the",
180 "remaining expressions are not evaluated at all.",
181 "This can be used to optimize the selections: you should write the",
182 "most restrictive and/or the most inexpensive expressions first in",
183 "boolean expressions.",
184 "The relative ordering between dynamic and static expressions does not",
185 "matter: all static expressions are evaluated only once, before the first",
186 "frame, and the result becomes the leftmost expression.[PAR]",
188 "Another point for optimization is in common subexpressions: they are not",
189 "automatically recognized, but can be manually optimized by the use of",
190 "variables. This can have a big impact on the performance of complex",
191 "selections, in particular if you define several index groups like this:",
192 " [TT]rdist = distance from com of resnr 1 to 5;[tt][BR]",
193 " [TT]resname RES and rdist < 2;[tt][BR]",
194 " [TT]resname RES and rdist < 4;[tt][BR]",
195 " [TT]resname RES and rdist < 6;[tt][BR]",
196 "Without the variable assignment, the distances would be evaluated three",
197 "times, although they are exactly the same within each selection.",
198 "Anything assigned into a variable becomes a common subexpression that",
199 "is evaluated only once during a frame.",
200 "Currently, in some cases the use of variables can actually lead to a small",
201 "performance loss because of the checks necessary to determine for which",
202 "atoms the expression has already been evaluated, but this should not be",
206 struct ExamplesHelpText
208 static const char name[];
209 static const char title[];
210 static const char *const text[];
213 const char ExamplesHelpText::name[] = "examples";
214 const char ExamplesHelpText::title[] =
215 "Selection examples";
216 const char *const ExamplesHelpText::text[] = {
217 // TODO: Once there are more tools available, use examples that invoke
218 // tools and explain what the selections do in those tools.
219 "Below, examples of increasingly complex selections are given.[PAR]",
221 "Selection of all water oxygens:[BR]",
222 " resname SOL and name OW",
225 "Centers of mass of residues 1 to 5 and 10:[BR]",
226 " res_com of resnr 1 to 5 10",
229 "All atoms farther than 1 nm of a fixed position:[BR]",
230 " not within 1 of [1.2, 3.1, 2.4]",
233 "All atoms of a residue LIG within 0.5 nm of a protein (with a custom name):[BR]",
234 " \"Close to protein\" resname LIG and within 0.5 of group \"Protein\"",
237 "All protein residues that have at least one atom within 0.5 nm of a residue LIG:[BR]",
238 " group \"Protein\" and same residue as within 0.5 of resname LIG",
241 "All RES residues whose COM is between 2 and 4 nm from the COM of all of them:[BR]",
242 " rdist = res_com distance from com of resname RES[BR]",
243 " resname RES and rdist >= 2 and rdist <= 4",
246 "Selection like C1 C2 C2 C3 C3 C4 ... C8 C9 (e.g., for g_bond):[BR]",
247 " name \"C[1-8]\" merge name \"C[2-9]\"",
250 struct KeywordsHelpText
252 static const char name[];
253 static const char title[];
254 static const char *const text[];
257 const char KeywordsHelpText::name[] = "keywords";
258 const char KeywordsHelpText::title[] =
259 "Selection keywords";
260 const char *const KeywordsHelpText::text[] = {
261 "The following selection keywords are currently available.",
262 "For keywords marked with a star, additional help is available through",
263 "a subtopic KEYWORD, where KEYWORD is the name of the keyword.",
266 struct LimitationsHelpText
268 static const char name[];
269 static const char title[];
270 static const char *const text[];
273 const char LimitationsHelpText::name[] = "limitations";
274 const char LimitationsHelpText::title[] =
275 "Selection limitations";
276 const char *const LimitationsHelpText::text[] = {
277 "Some analysis programs may require a special structure for the input",
278 "selections (e.g., [TT]gmx angle[tt] requires the index group to be made",
279 "of groups of three or four atoms).",
280 "For such programs, it is up to the user to provide a proper selection",
281 "expression that always returns such positions.",
284 "Due to technical reasons, having a negative value as the first value in",
285 "expressions like[BR]",
286 "[TT]charge -1 to -0.7[tt][BR]",
287 "result in a syntax error. A workaround is to write[BR]",
288 "[TT]charge {-1 to -0.7}[tt][BR]",
291 "When [TT]name[tt] selection keyword is used together with PDB input",
292 "files, the behavior may be unintuitive. When Gromacs reads in a PDB",
293 "file, 4 character atom names that start with a digit are transformed",
294 "such that, e.g., 1HG2 becomes HG21, and the latter is what is matched",
295 "by the [TT]name[tt] keyword. Use [TT]pdbname[tt] to match the atom name",
296 "as it appears in the input PDB file.",
299 struct PositionsHelpText
301 static const char name[];
302 static const char title[];
303 static const char *const text[];
306 const char PositionsHelpText::name[] = "positions";
307 const char PositionsHelpText::title[] =
308 "Specifying positions in selections";
309 const char *const PositionsHelpText::text[] = {
310 "Possible ways of specifying positions in selections are:[PAR]",
312 "1. A constant position can be defined as [TT][XX, YY, ZZ][tt], where",
313 "[TT]XX[tt], [TT]YY[tt] and [TT]ZZ[tt] are real numbers.[PAR]",
315 "2. [TT]com of ATOM_EXPR [pbc][tt] or [TT]cog of ATOM_EXPR [pbc][tt]",
316 "calculate the center of mass/geometry of [TT]ATOM_EXPR[tt]. If",
317 "[TT]pbc[tt] is specified, the center is calculated iteratively to try",
318 "to deal with cases where [TT]ATOM_EXPR[tt] wraps around periodic",
319 "boundary conditions.[PAR]",
321 "3. [TT]POSTYPE of ATOM_EXPR[tt] calculates the specified positions for",
322 "the atoms in [TT]ATOM_EXPR[tt].",
323 "[TT]POSTYPE[tt] can be [TT]atom[tt], [TT]res_com[tt], [TT]res_cog[tt],",
324 "[TT]mol_com[tt] or [TT]mol_cog[tt], with an optional prefix [TT]whole_[tt]",
325 "[TT]part_[tt] or [TT]dyn_[tt].",
326 "[TT]whole_[tt] calculates the centers for the whole residue/molecule,",
327 "even if only part of it is selected.",
328 "[TT]part_[tt] prefix calculates the centers for the selected atoms, but",
329 "uses always the same atoms for the same residue/molecule. The used atoms",
330 "are determined from the the largest group allowed by the selection.",
331 "[TT]dyn_[tt] calculates the centers strictly only for the selected atoms.",
332 "If no prefix is specified, whole selections default to [TT]part_[tt] and",
333 "other places default to [TT]whole_[tt].",
334 "The latter is often desirable to select the same molecules in different",
335 "tools, while the first is a compromise between speed ([TT]dyn_[tt]",
336 "positions can be slower to evaluate than [TT]part_[tt]) and intuitive",
339 "4. [TT]ATOM_EXPR[tt], when given for whole selections, is handled as 3.",
340 "above, using the position type from the command-line argument",
341 "[TT]-seltype[tt].[PAR]",
343 "Selection keywords that select atoms based on their positions, such as",
344 "[TT]dist from[tt], use by default the positions defined by the",
345 "[TT]-selrpos[tt] command-line option.",
346 "This can be overridden by prepending a [TT]POSTYPE[tt] specifier to the",
347 "keyword. For example, [TT]res_com dist from POS[tt] evaluates the",
348 "residue center of mass distances. In the example, all atoms of a residue",
349 "are either selected or not, based on the single distance calculated.",
352 struct SyntaxHelpText
354 static const char name[];
355 static const char title[];
356 static const char *const text[];
359 const char SyntaxHelpText::name[] = "syntax";
360 const char SyntaxHelpText::title[] =
362 const char *const SyntaxHelpText::text[] = {
363 "A set of selections consists of one or more selections, separated by",
364 "semicolons. Each selection defines a set of positions for the analysis.",
365 "Each selection can also be preceded by a string that gives a name for",
366 "the selection for use in, e.g., graph legends.",
367 "If no name is provided, the string used for the selection is used",
368 "automatically as the name.[PAR]",
370 "For interactive input, the syntax is slightly altered: line breaks can",
371 "also be used to separate selections. \\ followed by a line break can",
372 "be used to continue a line if necessary.",
373 "Notice that the above only applies to real interactive input,",
374 "not if you provide the selections, e.g., from a pipe.[PAR]",
376 "It is possible to use variables to store selection expressions.",
377 "A variable is defined with the following syntax:[BR]",
378 "[TT]VARNAME = EXPR ;[tt][BR]",
379 "where [TT]EXPR[tt] is any valid selection expression.",
380 "After this, [TT]VARNAME[tt] can be used anywhere where [TT]EXPR[tt]",
381 "would be valid.[PAR]",
383 "Selections are composed of three main types of expressions, those that",
384 "define atoms ([TT]ATOM_EXPR[tt]s), those that define positions",
385 "([TT]POS_EXPR[tt]s), and those that evaluate to numeric values",
386 "([TT]NUM_EXPR[tt]s). Each selection should be a [TT]POS_EXPR[tt]",
387 "or a [TT]ATOM_EXPR[tt] (the latter is automatically converted to",
388 "positions). The basic rules are as follows:[BR]",
389 "1. An expression like [TT]NUM_EXPR1 < NUM_EXPR2[tt] evaluates to an",
390 "[TT]ATOM_EXPR[tt] that selects all the atoms for which the comparison",
392 "2. Atom expressions can be combined with boolean operations such as",
393 "[TT]not ATOM_EXPR[tt], [TT]ATOM_EXPR and ATOM_EXPR[tt], or",
394 "[TT]ATOM_EXPR or ATOM_EXPR[tt]. Parentheses can be used to alter the",
395 "evaluation order.[BR]",
396 "3. [TT]ATOM_EXPR[tt] expressions can be converted into [TT]POS_EXPR[tt]",
397 "expressions in various ways, see the \"positions\" subtopic for more",
400 "Some keywords select atoms based on string values such as the atom name.",
401 "For these keywords, it is possible to use wildcards ([TT]name \"C*\"[tt])",
402 "or regular expressions (e.g., [TT]resname \"R[AB]\"[tt]).",
403 "The match type is automatically guessed from the string: if it contains",
404 "other characters than letters, numbers, '*', or '?', it is interpreted",
405 "as a regular expression.",
406 "To force the matching to use literal string matching, use",
407 "[TT]name = \"C*\"[tt] to match a literal C*.",
408 "To force other type of matching, use '?' or '~' in place of '=' to force",
409 "wildcard or regular expression matching, respectively.[PAR]",
411 "Strings that contain non-alphanumeric characters should be enclosed in",
412 "double quotes as in the examples. For other strings, the quotes are",
413 "optional, but if the value conflicts with a reserved keyword, a syntax",
414 "error will occur. If your strings contain uppercase letters, this should",
417 "Index groups provided with the [TT]-n[tt] command-line option or",
418 "generated by default can be accessed with [TT]group NR[tt] or",
419 "[TT]group NAME[tt], where [TT]NR[tt] is a zero-based index of the group",
420 "and [TT]NAME[tt] is part of the name of the desired group.",
421 "The keyword [TT]group[tt] is optional if the whole selection is",
422 "provided from an index group.",
423 "To see a list of available groups in the interactive mode, press enter",
424 "in the beginning of a line.",
436 * Help topic implementation for an individual selection method.
438 * \ingroup module_selection
440 class KeywordDetailsHelpTopic : public AbstractSimpleHelpTopic
443 //! Initialize help topic for the given selection method.
444 KeywordDetailsHelpTopic(const std::string &name,
445 const gmx_ana_selmethod_t &method)
446 : name_(name), method_(method)
450 virtual const char *name() const
452 return name_.c_str();
454 virtual const char *title() const
460 virtual std::string helpText() const
462 return concatenateStrings(method_.help.help, method_.help.nlhelp);
467 const gmx_ana_selmethod_t &method_;
469 GMX_DISALLOW_COPY_AND_ASSIGN(KeywordDetailsHelpTopic);
473 * Custom help topic for printing a list of selection keywords.
475 * \ingroup module_selection
477 class KeywordsHelpTopic : public CompositeHelpTopic<KeywordsHelpText>
482 virtual void writeHelp(const HelpWriterContext &context) const;
486 * Container for known selection methods.
488 * The first item in the pair is the name of the selection method, and
489 * the second points to the static data structure that describes the
491 * The name in the first item may differ from the name of the static
492 * data structure if an alias is defined for that method.
494 typedef std::vector<std::pair<std::string,
495 const gmx_ana_selmethod_t *> >
499 * Prints a brief list of keywords (selection methods) available.
501 * \param[in] context Context for printing the help.
502 * \param[in] type Only methods that return this type are printed.
503 * \param[in] bModifiers If false, \ref SMETH_MODIFIER methods are
504 * excluded, otherwise only them are printed.
506 void printKeywordList(const HelpWriterContext &context,
507 e_selvalue_t type, bool bModifiers) const;
512 KeywordsHelpTopic::KeywordsHelpTopic()
514 // TODO: This is not a very elegant way of getting the list of selection
515 // methods, but this needs to be rewritten in any case if/when #652 is
517 boost::scoped_ptr<SelectionParserSymbolTable> symtab(
518 new SelectionParserSymbolTable);
519 gmx_ana_selmethod_register_defaults(symtab.get());
521 SelectionParserSymbolIterator symbol
522 = symtab->beginIterator(SelectionParserSymbol::MethodSymbol);
523 while (symbol != symtab->endIterator())
525 const std::string &symname = symbol->name();
526 const gmx_ana_selmethod_t *method = symbol->methodValue();
527 methods_.push_back(std::make_pair(std::string(symname), method));
528 if (method->help.nlhelp > 0 && method->help.help != NULL)
530 addSubTopic(HelpTopicPointer(
531 new KeywordDetailsHelpTopic(symname, *method)));
537 void KeywordsHelpTopic::writeHelp(const HelpWriterContext &context) const
539 if (context.outputFormat() != eHelpOutputFormat_Console)
541 GMX_THROW(NotImplementedError(
542 "Selection help is not implemented for this output format"));
544 // TODO: The markup here is not really appropriate, and printKeywordList()
545 // still prints raw text, but these are waiting for discussion of the
546 // markup format in #969.
547 writeBasicHelpTopic(context, *this, helpText());
548 context.writeTextBlock("[BR]");
550 // Print the list of keywords
551 context.writeTextBlock(
552 "Keywords that select atoms by an integer property:[BR]"
553 "(use in expressions or like \"atomnr 1 to 5 7 9\")[BR]");
554 printKeywordList(context, INT_VALUE, false);
555 context.writeTextBlock("[BR]");
557 context.writeTextBlock(
558 "Keywords that select atoms by a numeric property:[BR]"
559 "(use in expressions or like \"occupancy 0.5 to 1\")[BR]");
560 printKeywordList(context, REAL_VALUE, false);
561 context.writeTextBlock("[BR]");
563 context.writeTextBlock(
564 "Keywords that select atoms by a string property:[BR]"
565 "(use like \"name PATTERN [PATTERN] ...\")[BR]");
566 printKeywordList(context, STR_VALUE, false);
567 context.writeTextBlock("[BR]");
569 context.writeTextBlock(
570 "Additional keywords that directly select atoms:[BR]");
571 printKeywordList(context, GROUP_VALUE, false);
572 context.writeTextBlock("[BR]");
574 context.writeTextBlock(
575 "Keywords that directly evaluate to positions:[BR]"
576 "(see also \"positions\" subtopic)[BR]");
577 printKeywordList(context, POS_VALUE, false);
578 context.writeTextBlock("[BR]");
580 context.writeTextBlock("Additional keywords:[BR]");
581 printKeywordList(context, POS_VALUE, true);
582 printKeywordList(context, NO_VALUE, true);
585 void KeywordsHelpTopic::printKeywordList(const HelpWriterContext &context,
587 bool bModifiers) const
589 File &file = context.outputFile();
590 MethodList::const_iterator iter;
591 for (iter = methods_.begin(); iter != methods_.end(); ++iter)
593 const gmx_ana_selmethod_t &method = *iter->second;
594 bool bIsModifier = (method.flags & SMETH_MODIFIER) != 0;
595 if (method.type == type && bModifiers == bIsModifier)
597 bool bHasHelp = (method.help.nlhelp > 0 && method.help.help != NULL);
598 file.writeString(formatString(" %c ", bHasHelp ? '*' : ' '));
599 if (method.help.syntax != NULL)
601 file.writeLine(method.help.syntax);
605 std::string symname = iter->first;
606 if (symname != method.name)
608 symname.append(formatString(" (synonym for %s)", method.name));
610 file.writeLine(symname);
619 HelpTopicPointer createSelectionHelpTopic()
621 CompositeHelpTopicPointer root(new CompositeHelpTopic<CommonHelpText>);
622 root->registerSubTopic<SimpleHelpTopic<ArithmeticHelpText> >();
623 root->registerSubTopic<SimpleHelpTopic<CmdLineHelpText> >();
624 root->registerSubTopic<SimpleHelpTopic<EvaluationHelpText> >();
625 root->registerSubTopic<SimpleHelpTopic<ExamplesHelpText> >();
626 root->registerSubTopic<KeywordsHelpTopic>();
627 root->registerSubTopic<SimpleHelpTopic<LimitationsHelpText> >();
628 root->registerSubTopic<SimpleHelpTopic<PositionsHelpText> >();
629 root->registerSubTopic<SimpleHelpTopic<SyntaxHelpText> >();