LOOS  v2.3.2
Trajectory.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 #if !defined(LOOS_TRAJECTORY_HPP)
23 #define LOOS_TRAJECTORY_HPP
24 
25 #include <istream>
26 #include <string>
27 #include <stdexcept>
28 #include <vector>
29 
30 #include <boost/utility.hpp>
31 
32 #include <loos_defs.hpp>
33 #include <StreamWrapper.hpp>
34 #include <AtomicGroup.hpp>
35 
36 #include <AtomicGroup.hpp>
37 
38 namespace loos {
39 
40 
42 
64  class Trajectory : public boost::noncopyable {
65  public:
66  Trajectory() : cached_first(false) { }
67 
69  Trajectory(const std::string& s) : ifs(s), cached_first(false), _filename(s) { }
70 
72  Trajectory(std::istream& fs) : ifs(fs), cached_first(false), _filename("istream") { }
73 
74  virtual ~Trajectory() { }
75 
76 
78  virtual std::string description() const { return("No Description Available"); }
79 
81  virtual uint natoms(void) const =0;
83  virtual float timestep(void) const =0;
85  virtual uint nframes(void) const =0;
86 
88  bool rewind(void) {
89  cached_first = true;
90  rewindImpl();
91  return(parseFrame());
92  }
93 
96 
102  virtual bool hasPeriodicBox(void) const =0;
104  virtual GCoord periodicBox(void) const =0;
105 
107 
111  virtual std::vector<GCoord> coords(void) =0;
112 
114 
136  {
137 #if defined(DEBUG)
138  if (! g.allHaveProperty(Atom::indexbit))
139  throw(LOOSError("Atoms in AtomicGroup have unset index properties and cannot be used to read a trajectory."));
140 #endif
141 
142  updateGroupCoordsImpl(g);
143  }
144 
145 
148  void seekNextFrame(void) {
149  cached_first = false;
150  seekNextFrameImpl();
151  }
152 
155  void seekFrame(const uint i) {
156  cached_first = false;
157  seekFrameImpl(i);
158  }
159 
161 
166  virtual bool parseFrame(void) =0;
167 
169  bool readFrame(void) {
170  bool b = true;
171 
172  if (!cached_first) {
173  seekNextFrame();
174  b = parseFrame();
175  } else
176  cached_first = false;
177 
178  return(b);
179  }
180 
182 
185  bool readFrame(const int i) {
186  bool b = true;
187 
188  if (!(i == 0 && cached_first)) {
189  seekFrame(i);
190  b = parseFrame();
191  }
192  cached_first = false;
193  return(b);
194  }
195 
196  protected:
197  StreamWrapper ifs;
198  bool cached_first; // Indicates that the first frame is cached by
199  // the subclass...
200 
201  std::string _filename; // Remember filename (if passed)
202 
203  private:
204 
206  virtual void seekNextFrameImpl() =0;
207 
209  virtual void seekFrameImpl(const uint) =0;
210 
212  virtual void rewindImpl(void) =0;
213 
215  virtual void updateGroupCoordsImpl(AtomicGroup& g) =0;
216 
217  };
218 
219 }
220 
221 #endif
bool allHaveProperty(const Atom::bits &property) const
True if all atoms in the group have the passed property(ies)
virtual uint nframes(void) const =0
Number of frames in the trajectory.
bool rewind(void)
Rewinds the readFrame() iterator.
Definition: Trajectory.hpp:88
Basic 3-D coordinates class.
Definition: Coord.hpp:36
Simple wrapper class for caching stream pointers.
virtual GCoord periodicBox(void) const =0
Returns the periodic box for the current frame/trajectory.
virtual uint natoms(void) const =0
of atoms per frame
void seekNextFrame(void)
Definition: Trajectory.hpp:148
virtual float timestep(void) const =0
Timestep per frame.
virtual bool hasPeriodicBox(void) const =0
void seekFrame(const uint i)
Definition: Trajectory.hpp:155
virtual bool parseFrame(void)=0
Parse an actual frame.
Generic LOOS exception.
Definition: exceptions.hpp:40
virtual std::string description() const
Return a string describing trajectory format.
Definition: Trajectory.hpp:78
Trajectory(const std::string &s)
Automatically open the file named s.
Definition: Trajectory.hpp:69
Class for handling groups of Atoms (pAtoms, actually)
Definition: AtomicGroup.hpp:87
Namespace for most things not already encapsulated within a class.
bool readFrame(const int i)
Reads a specific frame in a trajectory.
Definition: Trajectory.hpp:185
virtual std::vector< GCoord > coords(void)=0
Returns the current frames coordinates as a vector of GCoords.
Trajectory(std::istream &fs)
Open using the given stream...
Definition: Trajectory.hpp:72
Base-class for polymorphic trajectories.
Definition: Trajectory.hpp:64
void updateGroupCoords(AtomicGroup &g)
Update the coordinates in an AtomicGroup with the current frame.
Definition: Trajectory.hpp:135
bool readFrame(void)
Reads the next frame in a trajectory, returning false if at the end.
Definition: Trajectory.hpp:169