LOOS  v2.3.2
OptionsFramework.hpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2011, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 
23 
24 #if !defined(LOOS_OPTIONS_FRAMEWORK_HPP)
25 #define LOOS_OPTIONS_FRAMEWORK_HPP
26 
27 #include <loos_defs.hpp>
28 #include <boost/program_options.hpp>
29 #include <AtomicGroup.hpp>
30 #include <Trajectory.hpp>
31 #include <sfactories.hpp>
32 #include <boost/algorithm/string.hpp>
33 #include <exceptions.hpp>
34 
35 
36 
37 namespace loos {
38 
40 
239  namespace OptionsFramework {
240 
241  namespace po = boost::program_options;
242 
243 
245 
254  public:
255  virtual ~OptionsPackage() { }
256 
258  virtual void addGeneric(po::options_description& opts) { }
259 
261  virtual void addHidden(po::options_description& opts) { }
262 
264  virtual void addPositional(po::positional_options_description& opts) { }
265 
267  virtual std::string print() const { return(""); }
268 
270 
282  virtual bool check(po::variables_map& map) { return(false); }
283 
285 
297  virtual bool postConditions(po::variables_map& map) {
298  return(true);
299  }
300 
302 
317  virtual std::string help() const { return(""); }
318  };
319 
320 
321  // -------------------------------------------------
322 
324  class BasicOptions : public OptionsPackage {
325  public:
326  BasicOptions() : verbosity(0) { }
327  BasicOptions(const int i) : verbosity(i) { }
328  BasicOptions(const std::string& s) : verbosity(0), full_help(s) { }
329  BasicOptions(const int i, const std::string& s) : verbosity(i), full_help(s) { }
330 
331  void setFullHelp(const std::string& s);
332 
333  int verbosity;
334  std::string full_help;
335 
336  private:
337  void addGeneric(po::options_description& opts);
338  bool check(po::variables_map& map);
339 
340  std::string print() const;
341  };
342 
343  // -------------------------------------------------
344 
346  class OutputPrefix : public OptionsPackage {
347  public:
348  OutputPrefix() : prefix("output"), label("Output prefix") { }
349  OutputPrefix(const std::string& s) : prefix(s), label("Output prefix") { }
350  OutputPrefix(const std::string& s, const std::string& t) : prefix(s), label(t) { }
351 
352 
353  std::string prefix;
354  std::string label;
355 
356  private:
357  void addGeneric(po::options_description& opts);
358  std::string print() const;
359  };
360 
361  // -------------------------------------------------
362 
365  public:
366  BasicSelection() : selection("all"), label("Which atoms to use") { }
367  BasicSelection(const std::string& sel) : selection(sel), label("Which atoms to use") { }
368  BasicSelection(const std::string& sel, const std::string& lbl) :
369  selection(sel),
370  label(lbl)
371  { }
372 
373  std::string selection;
374  std::string label;
375 
376  private:
377  void addGeneric(po::options_description& opts);
378  std::string print() const;
379  };
380 
381 
382  // -------------------------------------------------
383 
385  class BasicSplitBy : public OptionsPackage {
386  public:
387  enum SplitType { NONE, MOLECULE, SEGID, RESIDUE };
388 
389 
390  BasicSplitBy() : split_method("mol"), label("Split selection by (none, mol, segid, res)"), split_type(MOLECULE) { }
391  BasicSplitBy(const std::string& method) : split_method(method), label("Split selection by (none, mol, segid, res)"), split_type(methodToType(method)) { }
392  BasicSplitBy(const std::string& method, const std::string& lbl) :
393  split_method(method),
394  label(lbl),
395  split_type(methodToType(method))
396  { }
397 
398  std::string split_method;
399  std::string label;
400  SplitType split_type;
401 
402  std::vector<AtomicGroup> split(const AtomicGroup& grp);
403 
404  private:
405  SplitType methodToType(const std::string& name);
406  void addGeneric(po::options_description& opts);
407  bool postConditions(po::variables_map& map);
408  std::string print() const;
409  };
410 
411  // -------------------------------------------------
412 
414 
421  public:
422  ModelWithCoords() : coords_name("") { }
423 
424  std::string model_name, coords_name, model_type;
425 
426  AtomicGroup model;
427 
428  private:
429  void addGeneric(po::options_description& opts);
430  void addHidden(po::options_description& opts);
431  void addPositional(po::positional_options_description& pos);
432 
433  bool check(po::variables_map& map);
434 
435  bool postConditions(po::variables_map& map);
436 
437  std::string help() const;
438  std::string print() const;
439  };
440 
441  // -------------------------------------------------
442 
444 
451  public:
452  TwoModelsWithCoords() : coords1_name(""), desc1("model1"),
453  coords2_name(""), desc2("model2") { }
454 
455  TwoModelsWithCoords(const std::string& d1,
456  const std::string& d2) :
457  coords1_name(""), desc1(d1),
458  coords2_name(""), desc2(d2)
459  { }
460 
461  std::string model1_name, model1_type, coords1_name, desc1;
462  std::string model2_name, model2_type, coords2_name, desc2;
463 
464  AtomicGroup model1;
465  AtomicGroup model2;
466 
467  private:
468  void addGeneric(po::options_description& opts);
469  void addHidden(po::options_description& opts);
470  void addPositional(po::positional_options_description& pos);
471 
472  bool check(po::variables_map& map);
473 
474  bool postConditions(po::variables_map& map);
475 
476  std::string help() const;
477  std::string print() const;
478  };
479 
480 
481 
482 
483  // -------------------------------------------------
484 
486 
494  public:
495  BasicTrajectory() : skip(0) { }
496 
497 
498  unsigned int skip;
499  std::string model_name, model_type, traj_name, traj_type;
500 
503 
505  pTraj trajectory;
506 
507  private:
508  void addGeneric(po::options_description& opts);
509  void addHidden(po::options_description& opts);
510 
511  void addPositional(po::positional_options_description& pos);
512 
513  bool check(po::variables_map& map);
514 
515  bool postConditions(po::variables_map& map);
516 
517  std::string help() const;
518  std::string print() const;
519  };
520 
521 
522 
523  // -------------------------------------------------
524 
526 
535  public:
536  TrajectoryWithFrameIndices() : skip(0), stride(1), frame_index_spec("") { }
537 
539  std::vector<uint> frameList() const;
540 
541  unsigned int skip, stride;
542  std::string frame_index_spec;
543  std::string model_name, model_type, traj_name, traj_type;
544 
547 
549  pTraj trajectory;
550 
551  private:
552  void addGeneric(po::options_description& opts);
553  void addHidden(po::options_description& opts);
554 
555  void addPositional(po::positional_options_description& pos);
556 
557  bool check(po::variables_map& map);
558 
559  bool postConditions(po::variables_map& map);
560 
561  std::string help() const;
562  std::string print() const;
563  };
564 
565 
566 
567  // -------------------------------------------------
568 
570 
601  typedef std::pair<std::string, std::string> StringPair;
602  public:
603  RequiredArguments() : vargs_set(false) { }
604 
605  RequiredArguments(const std::string& argname, const std::string& argdesc) : vargs_set(false)
606  {
607  addArgument(argname, argdesc);
608  }
609 
610 
612  void addArgument(const std::string& name, const std::string& description);
613 
615 
630  void addVariableArguments(const std::string& name, const std::string& description);
631 
633  std::string value(const std::string& s) const;
634 
636  std::vector<std::string> variableValues(const std::string& s) const;
637 
638  private:
639  void addHidden(po::options_description& o);
640  void addPositional(po::positional_options_description& pos);
641  bool check(po::variables_map& map);
642  bool postConditions(po::variables_map& map);
643  std::string help() const;
644  std::string print() const;
645 
646  private:
647  bool vargs_set;
648  std::vector<StringPair> arguments;
649  StringPair variable_arguments;
650  po::variables_map held_map;
651  };
652 
653 
654  // ----------------------------------------------------------------------
656  public:
657  OutputTrajectoryOptions() : name("output.dcd"), label("Output Trajectory"), append(false) {}
658  OutputTrajectoryOptions(const std::string& s) : name(s), label("Output Trajectory"), append(false) {}
659  OutputTrajectoryOptions(const std::string& s, const bool appending) : name(s), label("Output Trajectory"), append(appending) {}
660 
661 
662  std::string name;
663  std::string label;
664  bool append;
665  std::string type;
666  std::string basename;
667  pTrajectoryWriter outraj;
668 
669 
670  private:
671  void addGeneric(po::options_description& opts);
672  void addHidden(po::options_description& opts);
673 
674  void addPositional(po::positional_options_description& map);
675 
676  bool check(po::variables_map& map);
677 
678  bool postConditions(po::variables_map& map);
679 
680  std::string help() const;
681  std::string print() const;
682 
683  };
684 
685  // ----------------------------------------------------------------------
686 
688  public:
690  label("Output Trajectory Type"),
691  append(false),
692  type("dcd") {}
693 
694  OutputTrajectoryTypeOptions(const std::string& s) :
695  label("Output Trajectory Type"),
696  append(false),
697  type(s) {}
698 
699  OutputTrajectoryTypeOptions(const std::string& s, const bool appending) :
700  label("Output Trajectory Type"),
701  append(appending), type(s) {}
702 
703  pTrajectoryWriter createTrajectory(const std::string& prefix);
704 
705 
706  std::string label;
707  bool append;
708  std::string type;
709 
710  private:
711  void addGeneric(po::options_description& opts);
712  std::string print() const;
713  };
714 
715 
716 
717  // ----------------------------------------------------------------------
718 
719  typedef std::vector<OptionsPackage *> vOpts;
720 
723  public:
725  AggregateOptions() : program_name(""),
726  generic("Allowed Options"),
727  hidden("Hidden Options")
728  { }
729 
731  AggregateOptions(const std::string& name) : program_name(name),
732  generic("Allowed Options"),
733  hidden("Hidden Options")
734  { }
735 
737 
744 
746  bool parse(int argc, char *argv[]);
747 
750  std::vector<std::string> print() const;
751 
753  void showHelp();
754 
755  private:
756  std::string program_name;
757  std::string config_name;
758 
759  po::options_description generic;
760  po::options_description hidden;
761  po::options_description command_line;
762  po::positional_options_description pos;
763  po::variables_map vm;
764 
765  std::vector<OptionsPackage *> options;
766 
767 
768  void setupOptions();
769  };
770 
771  };
772 };
773 
774 
775 #endif
Gets a string as prefix for output files (–prefix)
Provides a mechanism for controlling how to split an AtomicGroup.
pTraj trajectory
The trajectory, primed by the –skip value (if specified)
Trajectory with either a –range or –skip.
Provides simple way to add command-line arguments (required options)
Namespace for encapsulating options processing.
Provides a single LOOS selection (–selection)
void addArgument(const std::string &name, const std::string &description)
Add a required argument given a name (tag) and a description (currently unused)
virtual bool postConditions(po::variables_map &map)
Post-processing of options returning true if there were no problems, otherwise false.
AtomicGroup model
Model that describes the trajectory.
bool parse(int argc, char *argv[])
Parses a command line, returning true if parsing was ok.
AtomicGroup model
Model that describes the trajectory.
Options common to all tools (including –fullhelp)
std::string value(const std::string &s) const
Retrieve the value for an argument.
Basic trajectory with a –skip option.
std::vector< std::string > print() const
virtual std::string help() const
Returns a slice of the example command-line in the help output.
virtual void addPositional(po::positional_options_description &opts)
Appends positional options.
void addVariableArguments(const std::string &name, const std::string &description)
Add a required argument that can be an arbitrary number of items.
virtual std::string print() const
Returns a string listing the encapsulated options, suitable for logging.
AggregateOptions()
Name is taken from argv[0] when AggregateOptions::parse() is called.
void showHelp()
Displays the help for this tool.
AggregateOptions(const std::string &name)
Explicitly set the program name (for help message and printing)
Request Two models with coordinates.
Request a model with coordinates.
std::vector< uint > frameList() const
Returns the list of frames the user requested.
Class for handling groups of Atoms (pAtoms, actually)
Definition: AtomicGroup.hpp:87
Namespace for most things not already encapsulated within a class.
std::vector< std::string > variableValues(const std::string &s) const
Retrieve the variable-number argument.
virtual bool check(po::variables_map &map)
Validates passed options, returning true if there is a problem or false if not.
virtual void addHidden(po::options_description &opts)
Appends hidden options (these generally match positional)
Combines a set of OptionsPackages.
virtual void addGeneric(po::options_description &opts)
Appends generic options (those that the user can see)
AggregateOptions & add(OptionsPackage *pack)
Add a pointer to an OptionsPackage that will be used for options.