2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 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 gmx::SelectionCollection.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
42 #include "selectioncollection.h"
50 #include <boost/shared_ptr.hpp>
52 #include "gromacs/legacyheaders/oenv.h"
53 #include "gromacs/legacyheaders/xvgr.h"
55 #include "gromacs/onlinehelp/helpmanager.h"
56 #include "gromacs/onlinehelp/helpwritercontext.h"
57 #include "gromacs/options/basicoptions.h"
58 #include "gromacs/options/options.h"
59 #include "gromacs/selection/selection.h"
60 #include "gromacs/utility/exceptions.h"
61 #include "gromacs/utility/file.h"
62 #include "gromacs/utility/gmxassert.h"
63 #include "gromacs/utility/messagestringcollector.h"
64 #include "gromacs/utility/smalloc.h"
65 #include "gromacs/utility/stringutil.h"
72 #include "selection.h"
73 #include "selectioncollection-impl.h"
76 #include "selmethod.h"
82 /********************************************************************
83 * SelectionCollection::Impl
86 SelectionCollection::Impl::Impl()
87 : debugLevel_(0), bExternalGroupsSet_(false), grps_(NULL)
92 gmx_ana_index_clear(&sc_.gall);
94 sc_.symtab.reset(new SelectionParserSymbolTable);
95 gmx_ana_selmethod_register_defaults(sc_.symtab.get());
99 SelectionCollection::Impl::~Impl()
102 // The tree must be freed before the SelectionData objects, since the
103 // tree may hold references to the position data in SelectionData.
106 for (int i = 0; i < sc_.nvars; ++i)
108 sfree(sc_.varstrs[i]);
111 gmx_ana_index_deinit(&sc_.gall);
114 _gmx_sel_mempool_destroy(sc_.mempool);
120 SelectionCollection::Impl::clearSymbolTable()
130 * Reads a single selection line from stdin.
132 * \param[in] infile File to read from (typically File::standardInput()).
133 * \param[in] bInteractive Whether to print interactive prompts.
134 * \param[out] line The read line in stored here.
135 * \returns true if something was read, false if at end of input.
137 * Handles line continuation, reading also the continuing line(s) in one call.
139 bool promptLine(File *infile, bool bInteractive, std::string *line)
143 fprintf(stderr, "> ");
145 if (!infile->readLineWithTrailingSpace(line))
149 while (endsWith(*line, "\\\n"))
151 line->resize(line->length() - 2);
154 fprintf(stderr, "... ");
157 // Return value ignored, buffer remains empty and works correctly
158 // if there is nothing to read.
159 infile->readLineWithTrailingSpace(&buffer);
160 line->append(buffer);
162 if (endsWith(*line, "\n"))
164 line->resize(line->length() - 1);
166 else if (bInteractive)
168 fprintf(stderr, "\n");
174 * Helper function for tokenizing the input and pushing them to the parser.
176 * \param scanner Tokenizer data structure.
177 * \param parserState Parser data structure.
178 * \param[in] bInteractive Whether to operate in interactive mode.
180 * Repeatedly reads tokens using \p scanner and pushes them to the parser with
181 * \p parserState until there is no more input, or until enough input is given
182 * (only in interactive mode).
184 int runParserLoop(yyscan_t scanner, _gmx_sel_yypstate *parserState,
187 int status = YYPUSH_MORE;
192 int token = _gmx_sel_yylex(&value, scanner);
199 // Empty commands cause the interactive parser to print out
200 // status information. This avoids producing those unnecessarily,
201 // e.g., from "resname RA;;".
202 if (prevToken == CMD_SEP && token == CMD_SEP)
208 status = _gmx_sel_yypush_parse(parserState, token, &value, scanner);
210 while (status == YYPUSH_MORE);
211 _gmx_sel_lexer_rethrow_exception_if_occurred(scanner);
216 * Print current status in response to empty line in interactive input.
218 * \param[in] sc Selection collection data structure.
219 * \param[in] grps Available index groups.
220 * \param[in] firstSelection Index of first selection from this interactive
222 * \param[in] maxCount Maximum number of selections.
223 * \param[in] context Context to print for what the selections are for.
224 * \param[in] bFirst Whether this is the header that is printed before
227 * Prints the available index groups and currently provided selections.
229 void printCurrentStatus(gmx_ana_selcollection_t *sc, gmx_ana_indexgrps_t *grps,
230 size_t firstSelection, int maxCount,
231 const std::string &context, bool bFirst)
235 std::fprintf(stderr, "Available static index groups:\n");
236 gmx_ana_indexgrps_print(stderr, grps, 0);
238 std::fprintf(stderr, "Specify ");
241 std::fprintf(stderr, "any number of selections");
243 else if (maxCount == 1)
245 std::fprintf(stderr, "a selection");
249 std::fprintf(stderr, "%d selections", maxCount);
251 std::fprintf(stderr, "%s%s:\n",
252 context.empty() ? "" : " ", context.c_str());
254 "(one per line, <enter> for status/groups, 'help' for help%s)\n",
255 maxCount < 0 ? ", Ctrl-D to end" : "");
256 if (!bFirst && (sc->nvars > 0 || sc->sel.size() > firstSelection))
258 std::fprintf(stderr, "Currently provided selections:\n");
259 for (int i = 0; i < sc->nvars; ++i)
261 std::fprintf(stderr, " %s\n", sc->varstrs[i]);
263 for (size_t i = firstSelection; i < sc->sel.size(); ++i)
265 std::fprintf(stderr, " %2d. %s\n",
266 static_cast<int>(i - firstSelection + 1),
267 sc->sel[i]->selectionText());
272 = maxCount - static_cast<int>(sc->sel.size() - firstSelection);
273 std::fprintf(stderr, "(%d more selection%s required)\n",
274 remaining, remaining > 1 ? "s" : "");
280 * Prints selection help in interactive selection input.
282 * \param[in] sc Selection collection data structure.
283 * \param[in] line Line of user input requesting help (starting with `help`).
285 * Initializes the selection help if not yet initialized, and finds the help
286 * topic based on words on the input line.
288 void printHelp(gmx_ana_selcollection_t *sc, const std::string &line)
290 if (sc->rootHelp.get() == NULL)
292 sc->rootHelp = createSelectionHelpTopic();
294 HelpWriterContext context(&File::standardError(),
295 eHelpOutputFormat_Console);
296 HelpManager manager(*sc->rootHelp, context);
299 std::vector<std::string> topic = splitString(line);
300 std::vector<std::string>::const_iterator value;
301 // First item in the list is the 'help' token.
302 for (value = topic.begin() + 1; value != topic.end(); ++value)
304 manager.enterTopic(*value);
307 catch (const InvalidInputError &ex)
309 fprintf(stderr, "%s\n", ex.what());
312 manager.writeCurrentTopic();
316 * Helper function that runs the parser once the tokenizer has been
319 * \param[in,out] scanner Scanner data structure.
320 * \param[in] bStdIn Whether to use a line-based reading
321 * algorithm designed for interactive input.
322 * \param[in] maxnr Maximum number of selections to parse
323 * (if -1, parse as many as provided by the user).
324 * \param[in] context Context to print for what the selections are for.
325 * \returns Vector of parsed selections.
326 * \throws std::bad_alloc if out of memory.
327 * \throws InvalidInputError if there is a parsing error.
329 * Used internally to implement parseFromStdin(), parseFromFile() and
332 SelectionList runParser(yyscan_t scanner, bool bStdIn, int maxnr,
333 const std::string &context)
335 boost::shared_ptr<void> scannerGuard(scanner, &_gmx_sel_free_lexer);
336 gmx_ana_selcollection_t *sc = _gmx_sel_lexer_selcollection(scanner);
337 gmx_ana_indexgrps_t *grps = _gmx_sel_lexer_indexgrps(scanner);
339 MessageStringCollector errors;
340 _gmx_sel_set_lexer_error_reporter(scanner, &errors);
342 size_t oldCount = sc->sel.size();
345 boost::shared_ptr<_gmx_sel_yypstate> parserState(
346 _gmx_sel_yypstate_new(), &_gmx_sel_yypstate_delete);
349 File &stdinFile(File::standardInput());
350 const bool bInteractive = _gmx_sel_is_lexer_interactive(scanner);
353 printCurrentStatus(sc, grps, oldCount, maxnr, context, true);
357 while (promptLine(&stdinFile, bInteractive, &line))
361 line = stripString(line);
364 printCurrentStatus(sc, grps, oldCount, maxnr, context, false);
367 if (startsWith(line, "help")
368 && (line[4] == 0 || std::isspace(line[4])))
375 _gmx_sel_set_lex_input_str(scanner, line.c_str());
376 status = runParserLoop(scanner, parserState.get(), true);
377 if (status != YYPUSH_MORE)
379 // TODO: Check if there is more input, and issue an
380 // error/warning if some input was ignored.
381 goto early_termination;
383 if (!errors.isEmpty() && bInteractive)
385 fprintf(stderr, "%s", errors.toString().c_str());
389 status = _gmx_sel_yypush_parse(parserState.get(), 0, NULL,
391 _gmx_sel_lexer_rethrow_exception_if_occurred(scanner);
397 int status = runParserLoop(scanner, parserState.get(), false);
401 scannerGuard.reset();
402 int nr = sc->sel.size() - oldCount;
403 if (maxnr > 0 && nr != maxnr)
406 errors.append("Too few selections provided");
409 // TODO: Remove added selections from the collection if parsing failed?
410 if (!bOk || !errors.isEmpty())
412 GMX_ASSERT(!bOk && !errors.isEmpty(), "Inconsistent error reporting");
413 GMX_THROW(InvalidInputError(errors.toString()));
416 SelectionList result;
417 SelectionDataList::const_iterator i;
419 for (i = sc->sel.begin() + oldCount; i != sc->sel.end(); ++i)
421 result.push_back(Selection(i->get()));
429 void SelectionCollection::Impl::resolveExternalGroups(
430 const SelectionTreeElementPointer &root,
431 ExceptionInitializer *errors)
434 if (root->type == SEL_GROUPREF)
438 root->resolveIndexGroupReference(grps_);
440 catch (const UserInputError &)
442 errors->addCurrentExceptionAsNested();
446 SelectionTreeElementPointer child = root->child;
449 resolveExternalGroups(child, errors);
455 /********************************************************************
456 * SelectionCollection
459 SelectionCollection::SelectionCollection()
465 SelectionCollection::~SelectionCollection()
471 SelectionCollection::initOptions(Options *options)
473 const char * const debug_levels[]
474 = { "no", "basic", "compile", "eval", "full" };
476 bool bAllowNonAtomOutput = false;
477 SelectionDataList::const_iterator iter;
478 for (iter = impl_->sc_.sel.begin(); iter != impl_->sc_.sel.end(); ++iter)
480 const internal::SelectionData &sel = **iter;
481 if (!sel.hasFlag(efSelection_OnlyAtoms))
483 bAllowNonAtomOutput = true;
487 const char *const *postypes = PositionCalculationCollection::typeEnumValues;
488 options->addOption(StringOption("selrpos")
489 .enumValueFromNullTerminatedArray(postypes)
490 .store(&impl_->rpost_).defaultValue(postypes[0])
491 .description("Selection reference positions"));
492 if (bAllowNonAtomOutput)
494 options->addOption(StringOption("seltype")
495 .enumValueFromNullTerminatedArray(postypes)
496 .store(&impl_->spost_).defaultValue(postypes[0])
497 .description("Default selection output positions"));
501 impl_->spost_ = postypes[0];
503 GMX_RELEASE_ASSERT(impl_->debugLevel_ >= 0 && impl_->debugLevel_ <= 4,
504 "Debug level out of range");
505 options->addOption(StringOption("seldebug").hidden(impl_->debugLevel_ == 0)
506 .enumValue(debug_levels)
507 .defaultValue(debug_levels[impl_->debugLevel_])
508 .storeEnumIndex(&impl_->debugLevel_)
509 .description("Print out selection trees for debugging"));
514 SelectionCollection::setReferencePosType(const char *type)
516 GMX_RELEASE_ASSERT(type != NULL, "Cannot assign NULL position type");
517 // Check that the type is valid, throw if it is not.
518 e_poscalc_t dummytype;
520 PositionCalculationCollection::typeFromEnum(type, &dummytype, &dummyflags);
521 impl_->rpost_ = type;
526 SelectionCollection::setOutputPosType(const char *type)
528 GMX_RELEASE_ASSERT(type != NULL, "Cannot assign NULL position type");
529 // Check that the type is valid, throw if it is not.
530 e_poscalc_t dummytype;
532 PositionCalculationCollection::typeFromEnum(type, &dummytype, &dummyflags);
533 impl_->spost_ = type;
538 SelectionCollection::setDebugLevel(int debugLevel)
540 impl_->debugLevel_ = debugLevel;
545 SelectionCollection::setTopology(t_topology *top, int natoms)
547 GMX_RELEASE_ASSERT(natoms > 0 || top != NULL,
548 "The number of atoms must be given if there is no topology");
549 // Get the number of atoms from the topology if it is not given.
552 natoms = top->atoms.nr;
554 gmx_ana_selcollection_t *sc = &impl_->sc_;
555 // Do this first, as it allocates memory, while the others don't throw.
556 gmx_ana_index_init_simple(&sc->gall, natoms);
557 sc->pcc.setTopology(top);
563 SelectionCollection::setIndexGroups(gmx_ana_indexgrps_t *grps)
565 GMX_RELEASE_ASSERT(grps == NULL || !impl_->bExternalGroupsSet_,
566 "Can only set external groups once or clear them afterwards");
568 impl_->bExternalGroupsSet_ = true;
570 ExceptionInitializer errors("Unknown index group references encountered");
571 SelectionTreeElementPointer root = impl_->sc_.root;
574 impl_->resolveExternalGroups(root, &errors);
577 if (errors.hasNestedExceptions())
579 GMX_THROW(InconsistentInputError(errors));
581 for (size_t i = 0; i < impl_->sc_.sel.size(); ++i)
583 impl_->sc_.sel[i]->refreshName();
589 SelectionCollection::requiresTopology() const
594 if (!impl_->rpost_.empty())
597 // Should not throw, because has been checked earlier.
598 PositionCalculationCollection::typeFromEnum(impl_->rpost_.c_str(),
600 if (type != POS_ATOM)
605 if (!impl_->spost_.empty())
608 // Should not throw, because has been checked earlier.
609 PositionCalculationCollection::typeFromEnum(impl_->spost_.c_str(),
611 if (type != POS_ATOM)
617 SelectionTreeElementPointer sel = impl_->sc_.root;
620 if (_gmx_selelem_requires_top(*sel))
631 SelectionCollection::parseFromStdin(int nr, bool bInteractive,
632 const std::string &context)
636 _gmx_sel_init_lexer(&scanner, &impl_->sc_, bInteractive, nr,
637 impl_->bExternalGroupsSet_,
639 return runParser(scanner, true, nr, context);
644 SelectionCollection::parseFromFile(const std::string &filename)
650 File file(filename, "r");
651 // TODO: Exception-safe way of using the lexer.
652 _gmx_sel_init_lexer(&scanner, &impl_->sc_, false, -1,
653 impl_->bExternalGroupsSet_,
655 _gmx_sel_set_lex_input_file(scanner, file.handle());
656 return runParser(scanner, false, -1, std::string());
658 catch (GromacsException &ex)
660 ex.prependContext(formatString(
661 "Error in parsing selections from file '%s'",
669 SelectionCollection::parseFromString(const std::string &str)
673 _gmx_sel_init_lexer(&scanner, &impl_->sc_, false, -1,
674 impl_->bExternalGroupsSet_,
676 _gmx_sel_set_lex_input_str(scanner, str.c_str());
677 return runParser(scanner, false, -1, std::string());
682 SelectionCollection::compile()
684 if (impl_->sc_.top == NULL && requiresTopology())
686 GMX_THROW(InconsistentInputError("Selection requires topology information, but none provided"));
688 if (!impl_->bExternalGroupsSet_)
690 setIndexGroups(NULL);
692 if (impl_->debugLevel_ >= 1)
694 printTree(stderr, false);
697 SelectionCompiler compiler;
698 compiler.compile(this);
700 if (impl_->debugLevel_ >= 1)
702 std::fprintf(stderr, "\n");
703 printTree(stderr, false);
704 std::fprintf(stderr, "\n");
705 impl_->sc_.pcc.printTree(stderr);
706 std::fprintf(stderr, "\n");
708 impl_->sc_.pcc.initEvaluation();
709 if (impl_->debugLevel_ >= 1)
711 impl_->sc_.pcc.printTree(stderr);
712 std::fprintf(stderr, "\n");
715 // TODO: It would be nicer to associate the name of the selection option
716 // (if available) to the error message.
717 SelectionDataList::const_iterator iter;
718 for (iter = impl_->sc_.sel.begin(); iter != impl_->sc_.sel.end(); ++iter)
720 const internal::SelectionData &sel = **iter;
721 if (sel.hasFlag(efSelection_OnlyAtoms))
723 if (sel.type() != INDEX_ATOM)
725 std::string message = formatString(
726 "Selection '%s' does not evaluate to individual atoms. "
727 "This is not allowed in this context.",
728 sel.selectionText());
729 GMX_THROW(InvalidInputError(message));
732 if (sel.hasFlag(efSelection_DisallowEmpty))
734 if (sel.posCount() == 0)
736 std::string message = formatString(
737 "Selection '%s' never matches any atoms.",
738 sel.selectionText());
739 GMX_THROW(InvalidInputError(message));
747 SelectionCollection::evaluate(t_trxframe *fr, t_pbc *pbc)
749 impl_->sc_.pcc.initFrame();
751 SelectionEvaluator evaluator;
752 evaluator.evaluate(this, fr, pbc);
754 if (impl_->debugLevel_ >= 3)
756 std::fprintf(stderr, "\n");
757 printTree(stderr, true);
763 SelectionCollection::evaluateFinal(int nframes)
765 SelectionEvaluator evaluator;
766 evaluator.evaluateFinal(this, nframes);
771 SelectionCollection::printTree(FILE *fp, bool bValues) const
773 SelectionTreeElementPointer sel = impl_->sc_.root;
776 _gmx_selelem_print_tree(fp, *sel, bValues, 0);
783 SelectionCollection::printXvgrInfo(FILE *out, output_env_t oenv) const
785 if (output_env_get_xvg_format(oenv) != exvgNONE)
787 const gmx_ana_selcollection_t &sc = impl_->sc_;
788 std::fprintf(out, "# Selections:\n");
789 for (int i = 0; i < sc.nvars; ++i)
791 std::fprintf(out, "# %s\n", sc.varstrs[i]);
793 for (size_t i = 0; i < sc.sel.size(); ++i)
795 std::fprintf(out, "# %s\n", sc.sel[i]->selectionText());
797 std::fprintf(out, "#\n");
803 SelectionCollection::createDefaultHelpTopic()
805 return createSelectionHelpTopic();