LOOS  v2.3.2
amber.hpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2008, 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 
25 #if !defined(LOOS_AMBER_HPP)
26 #define LOOS_AMBER_HPP
27 
28 
29 
30 #include <fstream>
31 #include <sstream>
32 #include <string>
33 #include <stdexcept>
34 #include <vector>
35 #include <boost/algorithm/string.hpp>
36 #include <boost/tokenizer.hpp>
37 
38 #include <loos_defs.hpp>
39 #include <AtomicGroup.hpp>
40 #include <LineReader.hpp>
41 
42 
43 namespace loos {
44 
46 
60  class Amber : public AtomicGroup {
61  private:
62 
63  typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
64 
65 
66  struct FormatSpec {
67  FormatSpec() : repeat(1), type('?'), width(0), precision(0) { }
68  int repeat;
69  char type;
70  int width;
71  int precision;
72  };
73 
74  struct AmberLineReader : public LineReader {
75  AmberLineReader() : LineReader() { }
76  AmberLineReader(std::istream& is) : LineReader(is) { }
77  AmberLineReader(std::istream& is, std::string& s) : LineReader(is, s) { }
78 
79  virtual void stripComment(std::string& s) const {
80  if (s.compare(0, 8, "%COMMENT") == 0)
81  s = "";
82  }
83  };
84 
85  public:
86 
87  Amber() : natoms(0), nres(0), nbonh(0), mbona(0) { }
88  virtual ~Amber() { }
89 
91  explicit Amber(const std::string& fname)
92  : natoms(0), nres(0), nbonh(0), mbona(0) {
93  std::ifstream ifs(fname.c_str());
94  if (!ifs)
95  throw(FileOpenError(fname));
96  reader.stream(ifs);
97  reader.name(fname);
98  read(ifs);
99  }
100 
101  explicit Amber(std::istream& ifs)
102  : natoms(0), nres(0), nbonh(0), mbona(0), reader(ifs) {
103  read(ifs);
104  }
105 
106  static pAtomicGroup create(const std::string& fname) {
107  return(pAtomicGroup(new Amber(fname)));
108  }
109 
110 
112  virtual Amber* clone(void) const {
113  return(new Amber(*this));
114  }
115 
117  Amber copy(void) const {
118  AtomicGroup grp = this->AtomicGroup::copy();
119  Amber p(grp);
120 
121  return(p);
122  }
123 
125  void read(std::istream& ifs);
126 
128  std::string title() const { return(_title); }
129 
130  private:
131 
132  Amber(const AtomicGroup& grp) : AtomicGroup(grp), natoms(0), nres(0), nbonh(0), mbona(0) { }
133 
134  FormatSpec parseFormat(const std::string& expected_types, const std::string& where);
135 
136  void parseCharges();
137  void parseMasses();
138  void parseResidueLabels();
139  void parseResiduePointers();
140  void assignResidues(void);
141  void parseBonds(const uint);
142  void parsePointers();
143  void parseTitle();
144  void parseAtomNames();
145  void parseAmoebaRegularBondNumList();
146  void parseAmoebaRegularBondList(const uint);
147 
148 
149  // Reads in a "block" of data. Reading terminates on the first
150  // line that begins with a '%'.
151 
152  template<typename T>
153  std::vector<T> readBlock(const int field_width) {
154  std::vector<T> data;
155  while (reader.getNext()) {
156  std::string line = reader.line();
157  if (line[0] == '%') {
158  reader.push_back(line);
159  break;
160  }
161  std::istringstream iss(line);
162  T d;
163  while (iss >> std::setw(field_width) >> d)
164  data.push_back(d);
165  }
166 
167  return(data);
168  }
169 
170  private:
171 
172  std::string _title;
173 
174  // These are internal and are used for parsing the parmtop info...
175  uint natoms, nres, nbonh, mbona, _amoeba_regular_bond_num_list;
176 
177  std::vector<std::string> residue_labels;
178  std::vector<uint> residue_pointers;
179 
180  AmberLineReader reader;
181 
182  };
183 
184 
185 }
186 
187 
188 
189 #endif
190 
191 
Error while opening a file.
Definition: exceptions.hpp:170
std::string title() const
Return the title.
Definition: amber.hpp:128
virtual Amber * clone(void) const
Clones an object for polymorphism...
Definition: amber.hpp:112
Class for reading line-by-line from a file while tracking line numbers and stripping comments...
Definition: LineReader.hpp:39
Amber copy(void) const
Deep copy.
Definition: amber.hpp:117
void read(std::istream &ifs)
Parse the parmtop file.
Definition: amber.cpp:279
AtomicGroup copy(void) const
Creates a deep copy of this group.
Definition: AtomicGroup.cpp:56
Class for reading in AMBER parmtop/coord files...
Definition: amber.hpp:60
Amber(const std::string &fname)
Read in a parmtop file.
Definition: amber.hpp:91
Class for handling groups of Atoms (pAtoms, actually)
Definition: AtomicGroup.hpp:87
Namespace for most things not already encapsulated within a class.