LOOS  v2.3.2
charmm.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 <charmm.hpp>
23 #include <utils.hpp>
24 #include <boost/algorithm/string.hpp>
25 
26 
27 
28 
29 namespace loos {
30 
31 
32  CHARMM* CHARMM::clone(void) const {
33  return(new CHARMM(*this));
34  }
35 
36  CHARMM CHARMM::copy(void) const {
37  AtomicGroup grp = this->AtomicGroup::copy();
38  CHARMM p(grp);
39 
40  return(p);
41  }
42 
43 
44 
51  void CHARMM::read(std::istream& is) {
52  std::string input;
53 
54 
55  getline(is, input);
56  while (input[0] == '*') {
57  getline(is, input);
58  if (!is.good()) {
59  throw(FileReadError(_filename, "Cannot read CHARMM header"));
60  }
61  }
62 
63  // Next line is the number of atoms, and maybe the flag "EXT"
64  unsigned int num_atoms = parseStringAs<unsigned int>(input.c_str(), 0, 10);
65  bool is_ext = false;
66  if (input.size() > 10) {
67  if (input.substr(12,3) == std::string("EXT")) {
68  is_ext = true;
69  }
70  }
71 
72  // now loop and read the coordinates
73  // Note: there are two different formats, depending on the number of atoms
74  int atom_num = -1;
75  int res_num = -1;
76  std::string res_name;
77  std::string atom_name;
78  float x,y,z;
79  std::string segid;
80  float weight;
81 
82  for (unsigned int i=1; i<= num_atoms; ++i) {
83  getline(is, input);
84  if (!is.good()) {
85  throw(FileReadError(_filename, "Cannot read CHARMM coordinates"));
86  }
87  if ((num_atoms < 100000) && !is_ext) {
88  atom_num = parseStringAs<int>(input, 0, 5);
89  res_name = parseStringAs<std::string>(input,11,4);
90  atom_name = parseStringAs<std::string>(input,16,4);
91  x = parseStringAs<float>(input,20,10);
92  y = parseStringAs<float>(input,30,10);
93  z = parseStringAs<float>(input,40,10);
94  segid = parseStringAs<std::string>(input,51,4);
95  res_num = parseStringAs<int>(input,56,4);
96  weight = parseStringAs<float>(input,63,10);
97  }
98  else {
99  atom_num = parseStringAs<int>(input, 0, 10);
100  res_num = parseStringAs<int>(input,10,10);
101  res_name = parseStringAs<std::string>(input,22,8);
102  atom_name = parseStringAs<std::string>(input,32,8);
103  x = parseStringAs<float>(input,40,20);
104  y = parseStringAs<float>(input,60,20);
105  z = parseStringAs<float>(input,80,20);
106  segid = parseStringAs<std::string>(input,100,8);
107  res_num = parseStringAs<int>(input,108,8);
108  weight = parseStringAs<float>(input,116,20);
109 
110  /*
111  segid = parseStringAs<std::string>(input,82,8);
112  res_num = parseStringAs<int>(input,92,8);
113  weight = parseStringAs<float>(input,100,20);
114  */
115  }
116 
117  // Create a new atom and fill in the values
118  pAtom pa(new Atom);
119  pa->index(_max_index++);
120  pa->id(atom_num);
121  pa->resid(res_num);
122  pa->name(atom_name);
123  pa->resname(res_name);
124  pa->segid(segid);
125  pa->coords().x() = x;
126  pa->coords().y() = y;
127  pa->coords().z() = z;
128  pa->occupancy(weight);
129 
130  // add the new atom to the CHARMM list
131  append(pa);
132  }
133  }
134 
135 
136 }
Errors that occur while reading a file.
Definition: exceptions.hpp:180
CHARMM copy(void) const
Creates a deep copy (see AtomicGroup::copy() for more info)
Definition: charmm.cpp:36
Class for reading a CHARMM coordinate file.
Definition: charmm.hpp:48
AtomicGroup & append(pAtom pa)
Append the atom onto the group.
Basic Atom class for handling atom properties.
Definition: Atom.hpp:50
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.
void read(std::istream &is)
Definition: charmm.cpp:51
virtual CHARMM * clone(void) const
Clones an object for polymorphism (see AtomicGroup::clone() for more info)
Definition: charmm.cpp:32