LOOS  v2.3.2
tinkerxyz.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 #include <tinkerxyz.hpp>
23 #include <Atom.hpp>
24 
25 namespace loos {
26 
27 
28  TinkerXYZ* TinkerXYZ::clone(void) const {
29  return(new TinkerXYZ(*this));
30  }
31 
32  TinkerXYZ TinkerXYZ::copy(void) const {
33  AtomicGroup grp = this->AtomicGroup::copy();
34  TinkerXYZ p(grp);
35 
36  // Add TinkerXYZ specific member data copies here...
37  return(p);
38  }
39 
40 
41  void TinkerXYZ::read(std::istream& is) {
42  std::string input;
43 
44  // first line is the header, first field is number of atoms
45  if (!getline(is, input))
46  throw(FileReadError(_filename, "Failed reading first line of xyz"));
47  int num_atoms = 0;
48  if (!(std::stringstream(input) >> num_atoms))
49  throw(FileReadError(_filename, "TinkerXYZ has malformed header"));
50 
51  if (!getline(is, input))
52  throw(FileReadError(_filename, "Failed reading 2nd line of Tinker file"));
53  if (parseBoxRecord(input)) {
54  if (!getline(is, input)) {
55  throw(FileReadError(_filename, "Failed reading first atom line of TinkerXYZ"));
56  }
57  }
58 
59 
60 
61  // Read the lines
62  for (int i=0; i<num_atoms; i++) {
63  parseAtomRecord(input);
64  if ((i < (num_atoms-1)) && (!getline(is, input)))
65  throw(FileReadError(_filename, "Failed reading TinkerXYZ atom line "));
66  }
67 
68  // Assume all XYZ files have connectivity
69  setGroupConnectivity();
70  }
71 
72 
73  bool TinkerXYZ::parseBoxRecord(const std::string& s) {
74  greal x,y,z;
75  std::string tmp;
76  std::stringstream ss(s);
77  ss >> x;
78  ss >> tmp;
79  if (isalpha(tmp[0])) {
80  // this is an atom record, not a box record
81  return false;
82  }
83  else {
84  y = atof(tmp.c_str());
85  }
86  ss >> z;
87 
88  periodicBox(x,y,z);
89  return true;
90  }
91 
92 
93  void TinkerXYZ::parseAtomRecord(const std::string& s) {
94 
95 
96  gint index;
97  //std::string segname(""); // Tinker doesn't have segments
98  //gint resid=1; // Tinker doesn't have residues
99  //std::string resname(""); // Tinker doesn't have residues
100  std::string atomname;
101  //greal charge=0.0;
102  //greal mass=1.0;
103  //gint atomic_number = 1;
104 
105  pAtom pa(new Atom);
106  pa->index(_max_index++);
107 
108  std::stringstream ss(s);
109 
110  ss >> index;
111  pa->id(index);
112 
113  ss >> atomname;
114  pa->name(atomname);
115 
116  greal x,y,z;
117  ss >> x;
118  ss >> y;
119  ss >> z;
120  pa->coords(GCoord(x,y,z));
121 
122  int atomtype;
123  ss >> atomtype;
124  pa->atomType(atomtype);
125 
126  // Now read in the atoms to which this atom is bonded
127  int bonded_atom;
128  while (ss >> bonded_atom)
129  {
130  // Probably should verify this value is sane
131  pa->addBond(bonded_atom);
132  }
133 
134  // Tinker XYZ files don't have segments or residues, so these
135  // properties get set to the class defaults by the constructor
136  //pa->segid(segname);
137  //pa->resid(resid);
138  //pa->resname(resname);
139 
140  // TODO: once we support reading Tinker param files, we should set these
141  // to their correct values
142  //pa->charge(charge);
143  //pa->mass(mass);
144  //pa->atomic_number(atomic_number);
145 
146 
147  append(pa);
148  }
149 
150 }
Errors that occur while reading a file.
Definition: exceptions.hpp:180
TinkerXYZ copy(void) const
Creates a deep copy (see AtomicGroup::copy() for more info)
Definition: tinkerxyz.cpp:32
AtomicGroup & append(pAtom pa)
Append the atom onto the group.
Class for reading a subset of the TinkerXYZ format.
Definition: tinkerxyz.hpp:47
virtual TinkerXYZ * clone(void) const
Clones an object for polymorphism (see AtomicGroup::clone() for more info)
Definition: tinkerxyz.cpp:28
AtomicGroup copy(void) const
Creates a deep copy of this group.
Definition: AtomicGroup.cpp:56
Class for handling groups of Atoms (pAtoms, actually)
Definition: AtomicGroup.hpp:87
Namespace for most things not already encapsulated within a class.
GCoord periodicBox(void) const
Fetch the periodic boundary conditions.