LOOS  v2.3.2
amber_traj.cpp
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 #include <amber_traj.hpp>
25 #include <AtomicGroup.hpp>
26 #include <iomanip>
27 #include <sstream>
28 
29 namespace loos {
30 
31  // Scan the trajectory file to determine frame sizes and box
32  void AmberTraj::init(void) {
33  char buf[1024];
34 
35  ifs()->getline(buf, 1024);
36  frame_offset = ifs()->tellg();
37  greal x, y, z;
38 
39  for (uint i=0; i<_natoms; i++) {
40  *(ifs()) >> std::setw(8) >> x >> std::setw(8) >> y >> std::setw(8) >> z;
41  frame.push_back(GCoord(x,y,z));
42  }
43 
44 
45  // This is probably not a good way of doing this???
46  ifs()->getline(buf, 1024);
47  unsigned long fpos = ifs()->tellg();
48 
49  ifs()->getline(buf, 1024);
50  if (ifs()->fail())
51  throw(FileOpenError(_filename, "Problem scanning Amber Trajectory"));
52 
53  std::stringstream ss(buf);
54  double a= -1, b= -1, c= -1;
55  ss >> std::setw(8) >> a >> std::setw(8) >> b >> std::setw(8) >> c;
56  if (ss.eof()) {
57  fpos = ifs()->tellg();
58  periodic = true;
59  box = GCoord(a, b, c);
60  }
61 
62  frame_size = fpos - frame_offset;
63 
64  // Now try to count the number of frames...
65  _nframes = 1;
66  double dummy;
67  while (!ifs()->fail()) {
68  ++_nframes;
69  fpos = _nframes * frame_size + frame_offset;
70  ifs()->seekg(fpos);
71  *(ifs()) >> dummy;
72  }
73 
74 
75  ifs()->clear();
76  ifs()->seekg(frame_offset + frame_size);
77 
78  // Punt our failure check to the end...for now...
79  if (ifs()->fail())
80  throw(FileOpenError(_filename, "Cannot determine frame information for Amber trajectory"));
81 
82  cached_first = true;
83  }
84 
85 
86  bool AmberTraj::parseFrame(void) {
87  greal x, y, z;
88 
89  if (ifs()->eof())
90  return(false);
91 
92  // It seems that it's possible that, at the end of the trajectory,
93  // we may have read the last datum from the last frame but will not
94  // be physically at the EOF. So, we also check inside the coord
95  // read loop to see if there is an EOF. Rather than flag this as an
96  // error condition, just return a false indicating we've read past
97  // the end...
98 
99  for (uint i=0; i<_natoms && !(ifs()->eof()); i++) {
100  *(ifs()) >> std::setw(8) >> x >> std::setw(8) >> y >> std::setw(8) >> z;
101  frame[i] = GCoord(x, y, z);
102  }
103 
104  if (ifs()->eof())
105  return(false);
106 
107  if (periodic) {
108  greal a, b, c;
109  *(ifs()) >> std::setw(8) >> a >> std::setw(8) >> b >> std::setw(8) >> c;
110  box = GCoord(a, b, c);
111  }
112 
113  if (ifs()->fail())
114  throw(FileReadError(_filename, "Problem reading from Amber trajectory"));
115 
116  return(true);
117  }
118 
119 
120  void AmberTraj::seekFrameImpl(const uint i) {
121 
122  cached_first = false;
123  unsigned long fpos = i * frame_size + frame_offset;
124  if (i >= _nframes)
125  throw(FileError(_filename, "Attempting seek frame beyond end of trajectory"));
126 
127 
128  ifs()->seekg(fpos);
129  if (ifs()->fail())
130  throw(FileError(_filename, "Cannot seek to frame"));
131  }
132 
133 
134  void AmberTraj::updateGroupCoordsImpl(AtomicGroup& g) {
135 
136  for (AtomicGroup::iterator i = g.begin(); i != g.end(); ++i) {
137  uint idx = (*i)->index();
138  if (idx >= _natoms)
139  throw(LOOSError(**i, "Atom index into trajectory is out of bounds"));
140  (*i)->coords(frame[idx]);
141  }
142 
143  if (periodic)
144  g.periodicBox(box);
145  }
146 }
Errors related to File I/O.
Definition: exceptions.hpp:110
Errors that occur while reading a file.
Definition: exceptions.hpp:180
virtual bool parseFrame(void)
Parse an actual frame.
Definition: amber_traj.cpp:86
Namespace for most things not already encapsulated within a class.