LOOS  v2.3.2
Atom.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 <Atom.hpp>
23 #include <algorithm>
24 #include <boost/format.hpp>
25 
26 namespace loos {
27 
28  int Atom::id(void) const { return(_id); }
29  void Atom::id(const int i) { _id = i; }
30 
31  uint Atom::index(void) const
32  {
33  return(_index);
34  }
35 
36 
37  void Atom::index(const uint i)
38  {
39  _index = i;
40  setPropertyBit(indexbit);
41  }
42 
43 
44  int Atom::resid(void) const { return(_resid); }
45  void Atom::resid(const int i) { _resid = i; }
46 
47  int Atom::atomic_number(void) const { return(_atomic_number); }
48  void Atom::atomic_number(const int i) {
49  _atomic_number = i;
50  setPropertyBit(anumbit);
51  }
52 
53  std::string Atom::name(void) const { return(_name); }
54  void Atom::name(const std::string s) { _name = s; }
55 
56  std::string Atom::altLoc(void) const { return(_altloc); }
57  void Atom::altLoc(const std::string s) { _altloc = s; }
58 
59  std::string Atom::chainId(void) const { return(_chainid); }
60  void Atom::chainId(const std::string s) { _chainid = s; }
61 
62  std::string Atom::resname(void) const { return(_resname); }
63  void Atom::resname(const std::string s) { _resname = s; }
64 
65  std::string Atom::segid(void) const { return(_segid); }
66  void Atom::segid(const std::string s) { _segid = s; }
67 
68  std::string Atom::iCode(void) const { return(_icode); }
69  void Atom::iCode(const std::string s) { _icode = s; }
70 
71  std::string Atom::PDBelement(void) const { return(_pdbelement); }
72  void Atom::PDBelement(const std::string s) { _pdbelement = s; }
73 
74  const GCoord& Atom::coords(void) const { return(_coords); }
75 
76  GCoord& Atom::coords(void) { return(_coords); }
77 
78  void Atom::coords(const GCoord& c) { _coords = c; setPropertyBit(coordsbit); }
79 
80  double Atom::bfactor(void) const { return(_b); }
81  void Atom::bfactor(const double d) { _b = d; }
82 
83  double Atom::occupancy(void) const { return(_q); }
84  void Atom::occupancy(const double d) { _q = d ; }
85 
86  double Atom::charge(void) const {
87  if (!(mask & chargebit))
88  throw(loos::UnsetProperty("Atom has no charge set"));
89  return(_charge);
90  }
91 
93 
94  void Atom::charge(const double d) { _charge = d ; setPropertyBit(chargebit); }
95 
96  double Atom::mass(void) const { return(_mass); }
97  void Atom::mass(const double d) { _mass = d ; setPropertyBit(massbit); }
98 
102  std::string Atom::recordName(void) const { return(_record); }
103  void Atom::recordName(const std::string s) { _record = s; }
104 
106  void Atom::clearBonds(void) { bonds.clear(); clearPropertyBit(bondsbit); }
108  void Atom::addBond(const pAtom& p) { bonds.push_back(p->id()); setPropertyBit(bondsbit); }
110  void Atom::addBond(const int i) { bonds.push_back(i); setPropertyBit(bondsbit); }
111 
113  void Atom::deleteBond(const int b) {
114  std::vector<int>::iterator i = find(bonds.begin(), bonds.end(), b);
115  if (i == bonds.end())
116  throw(LOOSError(*this, "Attempting to delete a non-existent bond"));
117  bonds.erase(i);
118  if (bonds.size() == 0)
119  clearPropertyBit(bondsbit);
120  }
121 
123  void Atom::deleteBond(const pAtom& p) { deleteBond(p->id()); }
124 
126  std::vector<int> Atom::getBonds(void) const {
127  if (!(mask & bondsbit))
128  throw(loos::UnsetProperty("Atom has no connectivity"));
129  return(bonds);
130  }
131 
132  void Atom::setBonds(const std::vector<int>& list) {
133  bonds = list;
134  setPropertyBit(bondsbit);
135  }
136 
137  bool Atom::hasBonds(void) const { return(bonds.size() != 0); }
138 
140  bool Atom::isBoundTo(const int i) {
141  std::vector<int>::iterator found = find(bonds.begin(), bonds.end(), i);
142  return(found != bonds.end());
143  }
144 
145  bool Atom::isBoundTo(const pAtom& p) { return(isBoundTo(p->id())); }
146 
148 
160  bool Atom::checkProperty(const bits bitmask) { return((mask & bitmask) != 0); }
161 
162 
163  // DEPRECATED: will likely be removed in future versions of LOOS
164  void Atom::checkUserBits(const bits bitmask) {
165  if (! (bitmask & (flagbit|usr1bit|usr2bit|usr3bit)) )
166  throw(LOOSError("Attempting to set a non-user property bit in an Atom"));
167  }
168 
169  void Atom::setProperty(const bits bitmask) {
170  setPropertyBit(bitmask);
171  }
172 
173  void Atom::clearProperty(const bits bitmask) {
174  clearPropertyBit(bitmask);
175  }
176 
177  void Atom::atomType(const int i) {
178  _atom_type = i;
179  }
180 
181  int Atom::atomType() const {
182  return(_atom_type);
183  }
184 
185 
186  void Atom::init() {
187  _id = 1;
188  _resid = 1;
189  _atomic_number = -1;
190  _b = 0.0;
191  _q = 1.0;
192  _charge = 0.0;
193  _mass = 1.0;
194  _name = " ";
195  _altloc = " ";
196  _resname = " ";
197  _chainid = " ";
198  _segid = " ";
199  _pdbelement = "";
200  _record = "ATOM";
201  _atom_type = -1;
202  mask = nullbit; // Nullbit means nothing was set...
203  }
204 
205  void Atom::setPropertyBit(const bits bitmask) { mask |= bitmask; }
207  void Atom::clearPropertyBit(const bits bitmask) { mask &= (~bitmask); }
208 
209 
210  std::ostream& operator<<(std::ostream& os, const loos::Atom& a) {
211  os << "<ATOM INDEX='" << a._index << "' ID='" << a._id << "' NAME='" << a._name << "' ";
212  os << "RESID='" << a._resid << "' RESNAME='" << a._resname << "' ";
213  os << "COORDS='" << a._coords << "' ";
214  os << "ALTLOC='" << a._altloc << "' CHAINID='" << a._chainid << "' ICODE='" << a._icode << "' SEGID='" << a._segid << "' ";
215  os << "B='" << a._b << "' Q='" << a._q << "' CHARGE='" << a._charge << "' MASS='" << a._mass << "'";
216  os << " ATOMICNUMBER='" << a._atomic_number <<"'";
217  os << " MASK='" << boost::format("%x") % a.mask << "'";
218  if (a.hasBonds() > 0) {
219  std::vector<int>::const_iterator i;
220  os << ">\n";
221  for (i=a.bonds.begin(); i != a.bonds.end(); i++)
222  os << " <BOND>" << *i << "</BOND>\n";
223  os << "</ATOM>";
224  } else
225  os << "/>";
226  return(os);
227  }
228 
229 
230  bool AtomEquals::operator()(const pAtom& a, const pAtom& b) const {
231  return(a->name() == b->name()
232  && a->id() == b->id()
233  && a->resname() == b->resname()
234  && a->resid() == b->resid()
235  && a->segid() == b->segid());
236  }
237 
238  bool AtomCoordsEquals::operator()(const pAtom& a, const pAtom& b) const {
239  bool bb = (a->name() == b->name()
240  && a->id() == b->id()
241  && a->resname() == b->resname()
242  && a->resid() == b->resid()
243  && a->segid() == b->segid());
244  if (!bb)
245  return(false);
246 
247  double d = a->coords().distance2(b->coords());
248  return( d <= threshold );
249  }
250 
251 }
252 
std::string recordName(void) const
Definition: Atom.cpp:102
void clearBonds(void)
Clear all stored bonds.
Definition: Atom.cpp:106
void setProperty(const bits bitmask)
Sets user-defined bits.
Definition: Atom.cpp:169
std::vector< int > getBonds(void) const
Returns a copy of the bond list.
Definition: Atom.cpp:126
void addBond(const pAtom &)
Add a bond given a pAtom (extracting the atomid of the bond)
Definition: Atom.cpp:108
Basic Atom class for handling atom properties.
Definition: Atom.hpp:50
Exception when trying to use an unset Atom property.
Definition: exceptions.hpp:71
bits
Bits in the bitmask that flag what properties have actually been set.
Definition: Atom.hpp:65
bool isBoundTo(const int)
Checks to see if this atom is bound to another atom.
Definition: Atom.cpp:140
void setBonds(const std::vector< int > &list)
Sets the bonds list.
Definition: Atom.cpp:132
Generic LOOS exception.
Definition: exceptions.hpp:40
void deleteBond(const int)
Deletes the specified bond.
Definition: Atom.cpp:113
Namespace for most things not already encapsulated within a class.
bool checkProperty(const bits bitmask)
Given a bit-mask, checks to see if those bits are set.
Definition: Atom.cpp:160
const GCoord & coords(void) const
Definition: Atom.cpp:74
void clearProperty(const bits bitmask)
Clears user-defined bits...
Definition: Atom.cpp:173