| 1 | /*
|
|---|
| 2 | * bondgraph.hpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: Oct 29, 2009
|
|---|
| 5 | * Author: heber
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #ifndef BONDGRAPH_HPP_
|
|---|
| 9 | #define BONDGRAPH_HPP_
|
|---|
| 10 |
|
|---|
| 11 | using namespace std;
|
|---|
| 12 |
|
|---|
| 13 | /*********************************************** includes ***********************************/
|
|---|
| 14 |
|
|---|
| 15 | // include config.h
|
|---|
| 16 | #ifdef HAVE_CONFIG_H
|
|---|
| 17 | #include <config.h>
|
|---|
| 18 | #endif
|
|---|
| 19 |
|
|---|
| 20 | #include <iosfwd>
|
|---|
| 21 |
|
|---|
| 22 | #include "AtomSet.hpp"
|
|---|
| 23 | #include "bond.hpp"
|
|---|
| 24 | #include "CodePatterns/Assert.hpp"
|
|---|
| 25 | #include "CodePatterns/Log.hpp"
|
|---|
| 26 | #include "CodePatterns/Range.hpp"
|
|---|
| 27 | #include "CodePatterns/Verbose.hpp"
|
|---|
| 28 | #include "element.hpp"
|
|---|
| 29 | #include "linkedcell.hpp"
|
|---|
| 30 | #include "IPointCloud.hpp"
|
|---|
| 31 | #include "PointCloudAdaptor.hpp"
|
|---|
| 32 | #include "WorldTime.hpp"
|
|---|
| 33 |
|
|---|
| 34 | /****************************************** forward declarations *****************************/
|
|---|
| 35 |
|
|---|
| 36 | class molecule;
|
|---|
| 37 | class BondedParticle;
|
|---|
| 38 | class MatrixContainer;
|
|---|
| 39 |
|
|---|
| 40 | /********************************************** definitions *********************************/
|
|---|
| 41 |
|
|---|
| 42 | /********************************************** declarations *******************************/
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | class BondGraph {
|
|---|
| 46 | public:
|
|---|
| 47 | /** Constructor of class BondGraph.
|
|---|
| 48 | * This classes contains typical bond lengths and thus may be used to construct a bond graph for a given molecule.
|
|---|
| 49 | */
|
|---|
| 50 | BondGraph(bool IsA);
|
|---|
| 51 |
|
|---|
| 52 | /** Destructor of class BondGraph.
|
|---|
| 53 | */
|
|---|
| 54 | ~BondGraph();
|
|---|
| 55 |
|
|---|
| 56 | /** Parses the bond lengths in a given file and puts them int a matrix form.
|
|---|
| 57 | * Allocates \a MatrixContainer for BondGraph::BondLengthMatrix, using MatrixContainer::ParseMatrix(),
|
|---|
| 58 | * but only if parsing is successful. Otherwise variable is left as NULL.
|
|---|
| 59 | * \param &input input stream to parse table from
|
|---|
| 60 | * \return true - success in parsing file, false - failed to parse the file
|
|---|
| 61 | */
|
|---|
| 62 | bool LoadBondLengthTable(std::istream &input);
|
|---|
| 63 |
|
|---|
| 64 | /** Returns the entry for a given index pair.
|
|---|
| 65 | * \param firstelement index/atom number of first element (row index)
|
|---|
| 66 | * \param secondelement index/atom number of second element (column index)
|
|---|
| 67 | * \note matrix is of course symmetric.
|
|---|
| 68 | */
|
|---|
| 69 | double GetBondLength(int firstelement, int secondelement);
|
|---|
| 70 |
|
|---|
| 71 | /** Determines the maximum of all element::CovalentRadius for elements present in \a &Set.
|
|---|
| 72 | *
|
|---|
| 73 | * Sets BondGraph::max_distance
|
|---|
| 74 | *
|
|---|
| 75 | * \param &Set PointCloud with all particles
|
|---|
| 76 | */
|
|---|
| 77 | template <class container_type,
|
|---|
| 78 | class iterator_type,
|
|---|
| 79 | class const_iterator_type>
|
|---|
| 80 | double SetMaxDistanceToMaxOfCovalentRadii(const AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set)
|
|---|
| 81 | {
|
|---|
| 82 | max_distance = 0.;
|
|---|
| 83 |
|
|---|
| 84 | for(const_iterator_type AtomRunner = Set.begin(); AtomRunner != Set.end(); ++AtomRunner) {
|
|---|
| 85 | const double radius = (*AtomRunner)->getType()->getCovalentRadius();
|
|---|
| 86 | if (radius > max_distance)
|
|---|
| 87 | max_distance = radius;
|
|---|
| 88 | }
|
|---|
| 89 | max_distance *= 2.;
|
|---|
| 90 | max_distance += BondThreshold;
|
|---|
| 91 |
|
|---|
| 92 | return max_distance;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /** Returns the upper limit on possible bond distances.
|
|---|
| 96 | *
|
|---|
| 97 | * Is set by SetMaxDistanceToMaxOfCovalentRadii(), is needed e.g. as cutoff
|
|---|
| 98 | * for linked-cell.
|
|---|
| 99 | *
|
|---|
| 100 | * \return maximum possible bond distance
|
|---|
| 101 | */
|
|---|
| 102 | double getMaxDistance() const;
|
|---|
| 103 |
|
|---|
| 104 | /** Returns bond criterion for given pair based on a bond length matrix.
|
|---|
| 105 | * This calls either the covalent or the bond matrix criterion.
|
|---|
| 106 | * \param *Walker first BondedParticle
|
|---|
| 107 | * \param *OtherWalker second BondedParticle
|
|---|
| 108 | * \param &MinDistance lower bond bound on return
|
|---|
| 109 | * \param &MaxDistance upper bond bound on return
|
|---|
| 110 | * \param IsAngstroem whether units are in angstroem or bohr radii
|
|---|
| 111 | */
|
|---|
| 112 | void getMinMaxDistance(const BondedParticle * const Walker, const BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem);
|
|---|
| 113 |
|
|---|
| 114 | /** Creates the adjacency list for a given \a Range of iterable atoms.
|
|---|
| 115 | *
|
|---|
| 116 | * @param Set Range with begin and end iterator
|
|---|
| 117 | */
|
|---|
| 118 | template <class container_type,
|
|---|
| 119 | class iterator_type,
|
|---|
| 120 | class const_iterator_type>
|
|---|
| 121 | void CreateAdjacency(AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set)
|
|---|
| 122 | {
|
|---|
| 123 | LOG(1, "STATUS: Removing all present bonds.");
|
|---|
| 124 | cleanAdjacencyList(Set);
|
|---|
| 125 |
|
|---|
| 126 | // count atoms in molecule = dimension of matrix (also give each unique name and continuous numbering)
|
|---|
| 127 | const unsigned int counter = Set.size();
|
|---|
| 128 | if (counter > 1) {
|
|---|
| 129 | LOG(1, "STATUS: Setting max bond distance.");
|
|---|
| 130 | SetMaxDistanceToMaxOfCovalentRadii(Set);
|
|---|
| 131 |
|
|---|
| 132 | LOG(1, "STATUS: Creating LinkedCell structure for given " << counter << " atoms.");
|
|---|
| 133 | PointCloudAdaptor< AtomSetMixin<container_type,iterator_type> > cloud(&Set, "SetOfAtoms");
|
|---|
| 134 | LinkedCell *LC = new LinkedCell(cloud, max_distance);
|
|---|
| 135 |
|
|---|
| 136 | CreateAdjacency(*LC);
|
|---|
| 137 | delete (LC);
|
|---|
| 138 |
|
|---|
| 139 | // correct bond degree by comparing valence and bond degree
|
|---|
| 140 | LOG(1, "STATUS: Correcting bond degree.");
|
|---|
| 141 | CorrectBondDegree(Set);
|
|---|
| 142 |
|
|---|
| 143 | // output bonds for debugging (if bond chain list was correctly installed)
|
|---|
| 144 | LOG(2, "STATUS: Printing list of created bonds.");
|
|---|
| 145 | std::stringstream output;
|
|---|
| 146 | for(const_iterator_type AtomRunner = Set.begin(); AtomRunner != Set.end(); ++AtomRunner) {
|
|---|
| 147 | (*AtomRunner)->OutputBondOfAtom(output);
|
|---|
| 148 | output << std::endl << "\t\t";
|
|---|
| 149 | }
|
|---|
| 150 | LOG(2, output.str());
|
|---|
| 151 | } else {
|
|---|
| 152 | LOG(1, "REJECT: AtomCount is " << counter << ", thus no bonds, no connections.");
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /** Creates an adjacency list of the given \a Set of atoms.
|
|---|
| 157 | *
|
|---|
| 158 | * Note that the input stream is required to refer to the same number of
|
|---|
| 159 | * atoms also contained in \a Set.
|
|---|
| 160 | *
|
|---|
| 161 | * \param &Set container with atoms
|
|---|
| 162 | * \param *input input stream to parse
|
|---|
| 163 | * \param skiplines how many header lines to skip
|
|---|
| 164 | * \param id_offset is base id compared to World startin at 0
|
|---|
| 165 | */
|
|---|
| 166 | template <class container_type,
|
|---|
| 167 | class iterator_type,
|
|---|
| 168 | class const_iterator_type>
|
|---|
| 169 | void CreateAdjacencyListFromDbondFile(
|
|---|
| 170 | AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set,
|
|---|
| 171 | ifstream *input,
|
|---|
| 172 | unsigned int skiplines,
|
|---|
| 173 | int id_offset) const
|
|---|
| 174 | {
|
|---|
| 175 | char line[MAXSTRINGSIZE];
|
|---|
| 176 |
|
|---|
| 177 | // check input stream
|
|---|
| 178 | if (input->fail()) {
|
|---|
| 179 | ELOG(0, "Opening of bond file failed \n");
|
|---|
| 180 | return;
|
|---|
| 181 | };
|
|---|
| 182 | // skip headers
|
|---|
| 183 | unsigned int bondcount = 0;
|
|---|
| 184 | for (unsigned int i=0;i<skiplines;i++)
|
|---|
| 185 | input->getline(line,MAXSTRINGSIZE);
|
|---|
| 186 |
|
|---|
| 187 | // create lookup map
|
|---|
| 188 | LOG(1, "STATUS: Creating lookup map.");
|
|---|
| 189 | std::map< unsigned int, atom *> AtomLookup;
|
|---|
| 190 | unsigned int counter = id_offset; // if ids do not start at 0
|
|---|
| 191 | for (iterator_type iter = Set.begin(); iter != Set.end(); ++iter) {
|
|---|
| 192 | AtomLookup.insert( make_pair( counter++, *iter) );
|
|---|
| 193 | }
|
|---|
| 194 | LOG(2, "INFO: There are " << counter << " atoms in the given set.");
|
|---|
| 195 |
|
|---|
| 196 | LOG(1, "STATUS: Scanning file.");
|
|---|
| 197 | unsigned int atom1, atom2;
|
|---|
| 198 | unsigned int bondcounter = 0;
|
|---|
| 199 | while (!input->eof()) // Check whether we read everything already
|
|---|
| 200 | {
|
|---|
| 201 | input->getline(line,MAXSTRINGSIZE);
|
|---|
| 202 | stringstream zeile(line);
|
|---|
| 203 | if (zeile.str().empty())
|
|---|
| 204 | continue;
|
|---|
| 205 | zeile >> atom1;
|
|---|
| 206 | zeile >> atom2;
|
|---|
| 207 |
|
|---|
| 208 | LOG(4, "INFO: Looking for atoms " << atom1 << " and " << atom2 << ".");
|
|---|
| 209 | if (atom2 < atom1) //Sort indices of atoms in order
|
|---|
| 210 | std::swap(atom1, atom2);
|
|---|
| 211 | ASSERT(atom2 < counter,
|
|---|
| 212 | "BondGraph::CreateAdjacencyListFromDbondFile() - ID "
|
|---|
| 213 | +toString(atom2)+" exceeds number of present atoms "+toString(counter)+".");
|
|---|
| 214 | ASSERT(AtomLookup.count(atom1),
|
|---|
| 215 | "BondGraph::CreateAdjacencyListFromDbondFile() - Could not find an atom with the ID given in dbond file");
|
|---|
| 216 | ASSERT(AtomLookup.count(atom2),
|
|---|
| 217 | "BondGraph::CreateAdjacencyListFromDbondFile() - Could not find an atom with the ID given in dbond file");
|
|---|
| 218 | atom * const Walker = AtomLookup[atom1];
|
|---|
| 219 | atom * const OtherWalker = AtomLookup[atom2];
|
|---|
| 220 |
|
|---|
| 221 | LOG(3, "INFO: Creating bond between atoms " << atom1 << " and " << atom2 << ".");
|
|---|
| 222 | bond * const Binder = new bond(Walker, OtherWalker, 1, -1);
|
|---|
| 223 | Walker->RegisterBond(WorldTime::getTime(), Binder);
|
|---|
| 224 | OtherWalker->RegisterBond(WorldTime::getTime(), Binder);
|
|---|
| 225 | bondcounter++;
|
|---|
| 226 | }
|
|---|
| 227 | LOG(1, "STATUS: "<< bondcounter << " bonds have been parsed.");
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | /** Creates an adjacency list of the molecule.
|
|---|
| 231 | * Generally, we use the CSD approach to bond recognition, that is the the distance
|
|---|
| 232 | * between two atoms A and B must be within [Rcov(A)+Rcov(B)-t,Rcov(A)+Rcov(B)+t] with
|
|---|
| 233 | * a threshold t = 0.4 Angstroem.
|
|---|
| 234 | * To make it O(N log N) the function uses the linked-cell technique as follows:
|
|---|
| 235 | * The procedure is step-wise:
|
|---|
| 236 | * -# Remove every bond in list
|
|---|
| 237 | * -# Count the atoms in the molecule with CountAtoms()
|
|---|
| 238 | * -# partition cell into smaller linked cells of size \a bonddistance
|
|---|
| 239 | * -# put each atom into its corresponding cell
|
|---|
| 240 | * -# go through every cell, check the atoms therein against all possible bond partners in the 27 adjacent cells, add bond if true
|
|---|
| 241 | * -# correct the bond degree iteratively (single->double->triple bond)
|
|---|
| 242 | * -# finally print the bond list to \a *out if desired
|
|---|
| 243 | * \param &LC Linked Cell Container with all atoms
|
|---|
| 244 | */
|
|---|
| 245 | void CreateAdjacency(LinkedCell &LC);
|
|---|
| 246 |
|
|---|
| 247 | /** Removes all bonds within the given set of iterable atoms.
|
|---|
| 248 | *
|
|---|
| 249 | * @param Set Range with atoms
|
|---|
| 250 | */
|
|---|
| 251 | template <class container_type,
|
|---|
| 252 | class iterator_type,
|
|---|
| 253 | class const_iterator_type>
|
|---|
| 254 | void cleanAdjacencyList(AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set)
|
|---|
| 255 | {
|
|---|
| 256 | // remove every bond from the list
|
|---|
| 257 | for(iterator_type AtomRunner = Set.begin(); AtomRunner != Set.end(); ++AtomRunner) {
|
|---|
| 258 | BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
|
|---|
| 259 | for(BondList::iterator BondRunner = ListOfBonds.begin();
|
|---|
| 260 | !ListOfBonds.empty();
|
|---|
| 261 | BondRunner = ListOfBonds.begin()) {
|
|---|
| 262 | ASSERT((*BondRunner)->Contains(*AtomRunner),
|
|---|
| 263 | "BondGraph::cleanAdjacencyList() - "+
|
|---|
| 264 | toString(*BondRunner)+" does not contain "+
|
|---|
| 265 | toString(*AtomRunner)+".");
|
|---|
| 266 | delete((*BondRunner));
|
|---|
| 267 | }
|
|---|
| 268 | }
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | /** correct bond degree by comparing valence and bond degree.
|
|---|
| 272 | * correct Bond degree of each bond by checking both bond partners for a mismatch between valence and current sum of bond degrees,
|
|---|
| 273 | * iteratively increase the one first where the other bond partner has the fewest number of bonds (i.e. in general bonds oxygene
|
|---|
| 274 | * preferred over carbon bonds). Beforehand, we had picked the first mismatching partner, which lead to oxygenes with single instead of
|
|---|
| 275 | * double bonds as was expected.
|
|---|
| 276 | * @param Set Range with atoms
|
|---|
| 277 | * \return number of bonds that could not be corrected
|
|---|
| 278 | */
|
|---|
| 279 | template <class container_type,
|
|---|
| 280 | class iterator_type,
|
|---|
| 281 | class const_iterator_type>
|
|---|
| 282 | int CorrectBondDegree(AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set) const
|
|---|
| 283 | {
|
|---|
| 284 | // reset
|
|---|
| 285 | resetBondDegree(Set);
|
|---|
| 286 | // re-calculate
|
|---|
| 287 | return calculateBondDegree(Set);
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | private:
|
|---|
| 291 | static const double BondThreshold;
|
|---|
| 292 |
|
|---|
| 293 | /** Returns bond criterion for given pair based on a bond length matrix.
|
|---|
| 294 | * The matrix should be contained in \a this BondGraph and contain an element-
|
|---|
| 295 | * to-element length.
|
|---|
| 296 | * \param *Walker first BondedParticle
|
|---|
| 297 | * \param *OtherWalker second BondedParticle
|
|---|
| 298 | * \param &MinDistance lower bond bound on return
|
|---|
| 299 | * \param &MaxDistance upper bond bound on return
|
|---|
| 300 | * \param IsAngstroem whether units are in angstroem or bohr radii
|
|---|
| 301 | */
|
|---|
| 302 | void BondLengthMatrixMinMaxDistance(const BondedParticle * const Walker, const BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem);
|
|---|
| 303 |
|
|---|
| 304 | /** Returns bond criterion for given pair based on covalent radius.
|
|---|
| 305 | * \param *Walker first BondedParticle
|
|---|
| 306 | * \param *OtherWalker second BondedParticle
|
|---|
| 307 | * \param &MinDistance lower bond bound on return
|
|---|
| 308 | * \param &MaxDistance upper bond bound on return
|
|---|
| 309 | * \param IsAngstroem whether units are in angstroem or bohr radii
|
|---|
| 310 | */
|
|---|
| 311 | void CovalentMinMaxDistance(const BondedParticle * const Walker, const BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem);
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 | /** Resets the bond::BondDegree of all atoms in the set to 1.
|
|---|
| 315 | *
|
|---|
| 316 | * @param Set Range with atoms
|
|---|
| 317 | */
|
|---|
| 318 | template <class container_type,
|
|---|
| 319 | class iterator_type,
|
|---|
| 320 | class const_iterator_type>
|
|---|
| 321 | void resetBondDegree(AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set) const
|
|---|
| 322 | {
|
|---|
| 323 | // reset bond degrees
|
|---|
| 324 | for(iterator_type AtomRunner = Set.begin(); AtomRunner != Set.end(); ++AtomRunner) {
|
|---|
| 325 | BondList &ListOfBonds = (*AtomRunner)->getListOfBonds();
|
|---|
| 326 | for (BondList::iterator BondIter = ListOfBonds.begin();
|
|---|
| 327 | BondIter != ListOfBonds.end();
|
|---|
| 328 | ++BondIter)
|
|---|
| 329 | (*BondIter)->BondDegree = 1;
|
|---|
| 330 | }
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | /** Calculates the bond degree for each atom on the set.
|
|---|
| 334 | *
|
|---|
| 335 | * @param Set Range with atoms
|
|---|
| 336 | * @return number of non-matching bonds
|
|---|
| 337 | */
|
|---|
| 338 | template <class container_type,
|
|---|
| 339 | class iterator_type,
|
|---|
| 340 | class const_iterator_type>
|
|---|
| 341 | int calculateBondDegree(AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set) const
|
|---|
| 342 | {
|
|---|
| 343 | //DoLog(1) && (Log() << Verbose(1) << "Correcting Bond degree of each bond ... " << endl);
|
|---|
| 344 | int No = 0, OldNo = -1;
|
|---|
| 345 | do {
|
|---|
| 346 | OldNo = No;
|
|---|
| 347 | No=0;
|
|---|
| 348 | for(iterator_type AtomRunner = Set.begin(); AtomRunner != Set.end(); ++AtomRunner) {
|
|---|
| 349 | No+=(*AtomRunner)->CorrectBondDegree();
|
|---|
| 350 | }
|
|---|
| 351 | } while (OldNo != No);
|
|---|
| 352 | //DoLog(0) && (Log() << Verbose(0) << " done." << endl);
|
|---|
| 353 | return No;
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | //!> Matrix with bond lenth per two elements
|
|---|
| 357 | MatrixContainer *BondLengthMatrix;
|
|---|
| 358 | //!> maximum distance over all bonds possible
|
|---|
| 359 | double max_distance;
|
|---|
| 360 | //!> distance units are angstroem (true), bohr radii (false)
|
|---|
| 361 | bool IsAngstroem;
|
|---|
| 362 | };
|
|---|
| 363 |
|
|---|
| 364 | #endif /* BONDGRAPH_HPP_ */
|
|---|