LOOS  v2.3.2
gro.cpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2009, 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 <gro.hpp>
25 #include <utils.hpp>
26 #include <Fmt.hpp>
27 
28 extern std::string revision_label;
29 
30 
31 
32 namespace loos {
33 
34  void Gromacs::read(std::istream& ifs) {
35  std::string buf;
36 
37  getline(ifs, title_);
38 
39  // Get the # of atoms;;;
40  getline(ifs, buf);
41  int natoms = parseStringAs<int>(buf);
42  while (natoms-- > 0) {
43  getline(ifs, buf);
44 
45  int resid = parseStringAs<int>(buf, 0, 5);
46  std::string resname = parseStringAs<std::string>(buf, 5, 5);
47  std::string name = parseStringAs<std::string>(buf, 10, 5);
48  int atomid = parseStringAs<int>(buf, 15, 5);
49  float x = parseStringAs<float>(buf, 20, 8) * 10.0;
50  float y = parseStringAs<float>(buf, 28, 8) * 10.0;
51  float z = parseStringAs<float>(buf, 36, 8) * 10.0;
52 
53  // We ignore velocities...
54  pAtom pa(new Atom);
55  pa->index(_max_index++);
56  pa->resid(resid);
57  pa->id(atomid);
58  pa->resname(resname);
59  pa->name(name);
60  pa->coords(GCoord(x,y,z));
61 
62  append(pa);
63  }
64 
65  // Now process box...
66  getline(ifs, buf);
67  std::istringstream iss(buf);
68  GCoord box;
69  if (!(iss >> box[0] >> box[1] >> box[2]))
70  throw(FileReadError(_filename, "Cannot parse box '" + buf + "'"));
71  periodicBox(box * 10.0);
72 
73  // Since the atomic field in .gro files is only 5-chars wide, it can
74  // overflow. if there are enough atoms to cause an overflow, manually
75  // renumber everything...
76  if (atoms.size() >= 100000)
77  renumber();
78  }
79 
80  std::string Gromacs::atomAsString(const pAtom p) const {
81  std::ostringstream s;
82 
83  // Float formatter for coords
84  Fmt crdfmt(3);
85  crdfmt.width(8);
86  crdfmt.right();
87  crdfmt.trailingZeros(true);
88  crdfmt.fixed();
89 
90  // Float formatter for velocities
91  Fmt velfmt(4);
92  velfmt.width(8);
93  velfmt.right();
94  velfmt.trailingZeros(true);
95  velfmt.fixed();
96 
97  s << std::setw(5) << p->resid();
98  s << std::left << std::setw(5) << p->resname();
99  s << std::right << std::setw(5) << p->name();
100  s << std::setw(5) << p->id();
101  s << crdfmt(p->coords().x()/10.);
102  s << crdfmt(p->coords().y()/10.);
103  s << crdfmt(p->coords().z()/10.);
104  s << velfmt(0.0);
105  s << velfmt(0.0);
106  s << velfmt(0.0);
107 
108  return(s.str());
109 
110  }
111 
113  //*
114  // Note: if the AtomicGroup doesn't have periodicity information,
115  // the GRO file gets a large generic periodic box. This is
116  // to facilitate writing GRO files, where the box line seems
117  // to be mandatory.
118  //
120  Gromacs p(g);
121 
122  p.title_ = std::string("Generated by LOOS: ") + revision_label;
123 
124  return(p);
125  }
126 
127 
128 
130 
131  std::ostream& operator<<(std::ostream& os, const Gromacs& g) {
132  AtomicGroup::const_iterator i;
133 
134  os << g.title() << std::endl;
135  os << g.size() << std::endl;
136  for (i=g.atoms.begin(); i != g.atoms.end(); ++i) {
137  os << g.atomAsString(*i) << std::endl;
138  }
139 
140  GCoord box = g.periodicBox();
141  box /= 10.0;
142  os << box.x() << " " << box.y() << " " << box.z() << std::endl;
143 
144 
145  return(os);
146  }
147 }
Basic 3-D coordinates class.
Definition: Coord.hpp:36
AtomicGroup & append(pAtom pa)
Append the atom onto the group.
void renumber(const int start=1, const int stride=1)
Renumber the atomid's of the contained atoms...
Implements a GROMACS model file (.gro)
Definition: gro.hpp:39
Class for handling groups of Atoms (pAtoms, actually)
Definition: AtomicGroup.hpp:87
Namespace for most things not already encapsulated within a class.
static Gromacs fromAtomicGroup(const AtomicGroup &)
Class method for creating a GRO from an AtomicGroup.
Definition: gro.cpp:119
GCoord periodicBox(void) const
Fetch the periodic boundary conditions.