[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[94d5ac6] | 5 | *
|
---|
| 6 | *
|
---|
| 7 | * This file is part of MoleCuilder.
|
---|
| 8 | *
|
---|
| 9 | * MoleCuilder 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, either version 2 of the License, or
|
---|
| 12 | * (at your option) any later version.
|
---|
| 13 | *
|
---|
| 14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 17 | * GNU General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * You should have received a copy of the GNU General Public License
|
---|
| 20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
[bcf653] | 21 | */
|
---|
| 22 |
|
---|
[96c961] | 23 | /*
|
---|
| 24 | * analysis_bonds.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: Nov 7, 2009
|
---|
| 27 | * Author: heber
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[bf3817] | 30 | // include config.h
|
---|
| 31 | #ifdef HAVE_CONFIG_H
|
---|
| 32 | #include <config.h>
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
[ad011c] | 35 | #include "CodePatterns/MemDebug.hpp"
|
---|
[112b09] | 36 |
|
---|
[220cf37] | 37 | #include "analysis_bonds.hpp"
|
---|
[6f0841] | 38 | #include "Atom/atom.hpp"
|
---|
[129204] | 39 | #include "Bond/bond.hpp"
|
---|
[3bdb6d] | 40 | #include "Element/element.hpp"
|
---|
[ad011c] | 41 | #include "CodePatterns/Info.hpp"
|
---|
| 42 | #include "CodePatterns/Verbose.hpp"
|
---|
| 43 | #include "CodePatterns/Log.hpp"
|
---|
[220cf37] | 44 | #include "molecule.hpp"
|
---|
| 45 |
|
---|
| 46 | /** Calculates the min, mean and maximum bond counts for the given molecule.
|
---|
| 47 | * \param *mol molecule with atoms and atom::ListOfBonds
|
---|
| 48 | * \param &Min minimum count on return
|
---|
| 49 | * \param &Mean mean count on return
|
---|
| 50 | * \param &Max maximum count on return
|
---|
| 51 | */
|
---|
| 52 | void GetMaxMinMeanBondCount(const molecule * const mol, double &Min, double &Mean, double &Max)
|
---|
| 53 | {
|
---|
| 54 | Min = 2e+6;
|
---|
| 55 | Max = -2e+5;
|
---|
| 56 | Mean = 0.;
|
---|
| 57 |
|
---|
| 58 | int AtomCount = 0;
|
---|
[9879f6] | 59 | for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
|
---|
[9d83b6] | 60 | const BondList& ListOfBonds = (*iter)->getListOfBonds();
|
---|
| 61 | const int count = ListOfBonds.size();
|
---|
[220cf37] | 62 | if (Max < count)
|
---|
| 63 | Max = count;
|
---|
| 64 | if (Min > count)
|
---|
| 65 | Min = count;
|
---|
| 66 | Mean += count;
|
---|
| 67 | AtomCount++;
|
---|
| 68 | }
|
---|
| 69 | if (((int)Mean % 2) != 0)
|
---|
[47d041] | 70 | ELOG(1, "Something is wrong with the bond structure, the number of bonds is not even!");
|
---|
[220cf37] | 71 | Mean /= (double)AtomCount;
|
---|
| 72 | };
|
---|
| 73 |
|
---|
| 74 | /** Calculates the min and max bond distance of all atoms of two given elements.
|
---|
| 75 | * \param *mol molecule with atoms
|
---|
| 76 | * \param *type1 one element
|
---|
| 77 | * \param *type2 other element
|
---|
| 78 | * \param &Min minimum distance on return, 0 if no bond between the two elements
|
---|
| 79 | * \param &Mean mean distance (i.e. sum of distance for matching element pairs, divided by number) on return, 0 if no bond between the two elements
|
---|
| 80 | * \param &Max maximum distance on return, 0 if no bond between the two elements
|
---|
| 81 | */
|
---|
[4eb4fe] | 82 | void MinMeanMaxBondDistanceBetweenElements(const molecule *mol, const element *type1, const element *type2, double &Min, double &Mean, double &Max)
|
---|
[220cf37] | 83 | {
|
---|
| 84 | Min = 2e+6;
|
---|
| 85 | Mean = 0.;
|
---|
| 86 | Max = -2e+6;
|
---|
| 87 |
|
---|
| 88 | int AtomNo = 0;
|
---|
[9879f6] | 89 | for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
|
---|
[9d83b6] | 90 | if ((*iter)->getType() == type1) {
|
---|
| 91 | const BondList& ListOfBonds = (*iter)->getListOfBonds();
|
---|
| 92 | for (BondList::const_iterator BondRunner = ListOfBonds.begin();
|
---|
| 93 | BondRunner != ListOfBonds.end();
|
---|
| 94 | BondRunner++)
|
---|
[d74077] | 95 | if ((*BondRunner)->GetOtherAtom((*iter))->getType() == type2) {
|
---|
[220cf37] | 96 | const double distance = (*BondRunner)->GetDistanceSquared();
|
---|
| 97 | if (Min > distance)
|
---|
| 98 | Min = distance;
|
---|
| 99 | if (Max < distance)
|
---|
| 100 | Max = distance;
|
---|
| 101 | Mean += sqrt(distance);
|
---|
| 102 | AtomNo++;
|
---|
| 103 | }
|
---|
[9d83b6] | 104 | }
|
---|
[220cf37] | 105 | }
|
---|
| 106 | if (Max < 0) {
|
---|
| 107 | Max = Min = 0.;
|
---|
| 108 | } else {
|
---|
| 109 | Max = sqrt(Max);
|
---|
| 110 | Min = sqrt(Min);
|
---|
| 111 | Mean = Mean/(double)AtomNo;
|
---|
| 112 | }
|
---|
| 113 | };
|
---|
[388049] | 114 |
|
---|
[fe238c] | 115 | /** Calculate the angle between \a *first and \a *origin and \a *second and \a *origin.
|
---|
| 116 | * \param *first first Vector
|
---|
| 117 | * \param *origin origin of angle taking
|
---|
| 118 | * \param *second second Vector
|
---|
| 119 | * \return angle between \a *first and \a *second, both relative to origin at \a *origin.
|
---|
| 120 | */
|
---|
[d74077] | 121 | double CalculateAngle(const Vector &first, const Vector ¢ral, const Vector &second)
|
---|
[fe238c] | 122 | {
|
---|
| 123 | Vector OHBond;
|
---|
| 124 | Vector OOBond;
|
---|
| 125 |
|
---|
[d74077] | 126 | OHBond = first - central;
|
---|
| 127 | OOBond = second - central;
|
---|
[8cbb97] | 128 | const double angle = OHBond.Angle(OOBond);
|
---|
[fe238c] | 129 | return angle;
|
---|
| 130 | };
|
---|
| 131 |
|
---|
| 132 | /** Checks whether the angle between \a *Oxygen and \a *Hydrogen and \a *Oxygen and \a *OtherOxygen is less than 30 degrees.
|
---|
| 133 | * Note that distance criterion is not checked.
|
---|
| 134 | * \param *Oxygen first oxygen atom, bonded to \a *Hydrogen
|
---|
| 135 | * \param *Hydrogen hydrogen bonded to \a *Oxygen
|
---|
| 136 | * \param *OtherOxygen other oxygen atom
|
---|
| 137 | * \return true - angle criteria fulfilled, false - criteria not fulfilled, angle greater than 30 degrees.
|
---|
| 138 | */
|
---|
[153985] | 139 | bool CheckHydrogenBridgeBondAngle(const atom & Oxygen, const atom & Hydrogen, const atom & OtherOxygen)
|
---|
[fe238c] | 140 | {
|
---|
| 141 | Info FunctionInfo(__func__);
|
---|
| 142 |
|
---|
| 143 | // check angle
|
---|
[153985] | 144 | const double angle = CalculateAngle(
|
---|
| 145 | Hydrogen.getPosition(),
|
---|
| 146 | Oxygen.getPosition(),
|
---|
| 147 | OtherOxygen.getPosition());
|
---|
| 148 | LOG(3, "INFO: Hydrogen bridge bond angle is " << angle << ", < " << M_PI*(30./180.) << "?");
|
---|
| 149 | if (angle < M_PI*(30./180.)) {
|
---|
[fe238c] | 150 | return true;
|
---|
| 151 | } else {
|
---|
| 152 | return false;
|
---|
| 153 | }
|
---|
| 154 | };
|
---|
[388049] | 155 |
|
---|
| 156 | /** Counts the number of hydrogen bridge bonds.
|
---|
| 157 | * With \a *InterfaceElement an extra element can be specified that identifies some boundary.
|
---|
| 158 | * Then, counting is for the h-bridges that connect to interface only.
|
---|
| 159 | * \param *molecules molecules to count bonds
|
---|
| 160 | * \param *InterfaceElement or NULL
|
---|
[bfd839] | 161 | * \param *Interface2Element or NULL
|
---|
[388049] | 162 | */
|
---|
[2bfc5b] | 163 | int CountHydrogenBridgeBonds(const std::vector<molecule *> &molecules, const element * InterfaceElement = NULL, const element * Interface2Element = NULL)
|
---|
[388049] | 164 | {
|
---|
[153985] | 165 | Info FunctionInfo(__func__);
|
---|
| 166 |
|
---|
[388049] | 167 | int count = 0;
|
---|
[fe238c] | 168 | int OtherHydrogens = 0;
|
---|
| 169 | double Otherangle = 0.;
|
---|
[388049] | 170 | bool InterfaceFlag = false;
|
---|
[bfd839] | 171 | bool Interface2Flag = false;
|
---|
[fe238c] | 172 | bool OtherHydrogenFlag = true;
|
---|
[153985] | 173 | LinkedCell::LinkedCell_View LC = World::getInstance().getLinkedCell(HBRIDGEDISTANCE);
|
---|
| 174 |
|
---|
| 175 | // go through every molecule
|
---|
[2bfc5b] | 176 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin();
|
---|
| 177 | MolWalker != molecules.end();
|
---|
[153985] | 178 | ++MolWalker) {
|
---|
| 179 | LOG(2, "INFO: Current molecule is " << (*MolWalker)->getName() << ".");
|
---|
| 180 |
|
---|
| 181 | // go through every atom
|
---|
| 182 | typedef std::set<const molecule *> Moleculeset;
|
---|
[f01769] | 183 | for(molecule::const_iterator Walker = const_cast<const molecule *>(*MolWalker)->begin();
|
---|
| 184 | Walker != const_cast<const molecule *>(*MolWalker)->end();
|
---|
[153985] | 185 | ++Walker) {
|
---|
| 186 | // go through every oxygen
|
---|
| 187 | if ((*Walker)->getType()->getAtomicNumber() == 8) {
|
---|
| 188 | LOG(2, "INFO: Current oxygen atom is " << *(*Walker) << ".");
|
---|
| 189 |
|
---|
| 190 | // get all its neighbors
|
---|
| 191 | LinkedCell::LinkedList NeighborList = LC.getAllNeighbors(HBRIDGEDISTANCE, (*Walker)->getPosition());
|
---|
| 192 | // go through each candidate and gather the molecules of all other oxygens
|
---|
| 193 | Moleculeset MoleculeNeighbors;
|
---|
| 194 | for(LinkedCell::LinkedList::const_iterator Runner = NeighborList.begin();
|
---|
| 195 | Runner != NeighborList.end(); ++Runner) {
|
---|
| 196 | const atom * const OtherAtom = dynamic_cast<const atom *>(*Runner);
|
---|
| 197 | if ((OtherAtom->getType()->getAtomicNumber() == 8) &&
|
---|
| 198 | (OtherAtom->getMolecule() != (*MolWalker))) {
|
---|
| 199 | LOG(3, "INFO: Possible neighboring molecule is " << OtherAtom->getMolecule()->getName() << ".");
|
---|
| 200 | MoleculeNeighbors.insert(OtherAtom->getMolecule());
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | // now go through the molecules
|
---|
| 205 | for (Moleculeset::const_iterator moliter = MoleculeNeighbors.begin();
|
---|
| 206 | moliter != MoleculeNeighbors.end();
|
---|
| 207 | ++moliter) {
|
---|
| 208 | LOG(2, "INFO: Current other molecule is " << (*moliter)->getName() << ".");
|
---|
| 209 |
|
---|
| 210 | // go through every other atom
|
---|
| 211 | for(molecule::const_iterator Runner = (*moliter)->begin();
|
---|
| 212 | Runner != (*moliter)->end();
|
---|
| 213 | ++Runner) {
|
---|
| 214 | // go through each oxygen
|
---|
| 215 | if ((*Runner)->getType()->getAtomicNumber() == 8) {
|
---|
| 216 |
|
---|
| 217 | // check distance
|
---|
| 218 | const double distance = (*Runner)->DistanceSquared(*(*Walker));
|
---|
| 219 | if ((distance > MYEPSILON) && (distance < HBRIDGEDISTANCE*HBRIDGEDISTANCE)) {
|
---|
| 220 | LOG(2, "INFO: Distance between oxygen atom "
|
---|
| 221 | << (*Walker)->getName() << " and "
|
---|
| 222 | << (*Runner)->getName() << " is "
|
---|
| 223 | << sqrt(distance) << ".");
|
---|
| 224 | // distance >0 means different atoms
|
---|
| 225 | // on other atom(Runner) we check for bond to interface element and
|
---|
| 226 | // check that O-O line is not in between the shanks of the two connected hydrogens (Otherangle > 104.5)
|
---|
| 227 | OtherHydrogenFlag = true;
|
---|
| 228 | Otherangle = 0.;
|
---|
| 229 | OtherHydrogens = 0;
|
---|
| 230 | InterfaceFlag = (InterfaceElement == NULL);
|
---|
| 231 | Interface2Flag = (Interface2Element == NULL);
|
---|
| 232 | const BondList& ListOfBonds = (*Runner)->getListOfBonds();
|
---|
[9d83b6] | 233 | for (BondList::const_iterator BondRunner = ListOfBonds.begin();
|
---|
| 234 | BondRunner != ListOfBonds.end();
|
---|
| 235 | BondRunner++) {
|
---|
[153985] | 236 | atom * const OtherAtom = (*BondRunner)->GetOtherAtom(*Runner);
|
---|
| 237 | // if hydrogen, check angle to be greater(!) than 30 degrees
|
---|
[83f176] | 238 | if (OtherAtom->getType()->getAtomicNumber() == 1) {
|
---|
[153985] | 239 | const double angle = CalculateAngle(OtherAtom->getPosition(), (*Runner)->getPosition(), (*Walker)->getPosition());
|
---|
| 240 | OtherHydrogenFlag = OtherHydrogenFlag && (angle > M_PI*(30./180.) + MYEPSILON);
|
---|
| 241 | Otherangle += angle;
|
---|
| 242 | OtherHydrogens++;
|
---|
| 243 | }
|
---|
| 244 | InterfaceFlag = InterfaceFlag || (OtherAtom->getType() == InterfaceElement);
|
---|
| 245 | Interface2Flag = Interface2Flag || (OtherAtom->getType() == Interface2Element);
|
---|
| 246 | }
|
---|
| 247 | LOG(1, "Otherangle is " << Otherangle << " for " << OtherHydrogens << " hydrogens.");
|
---|
| 248 | switch (OtherHydrogens) {
|
---|
| 249 | case 0:
|
---|
| 250 | case 1:
|
---|
| 251 | break;
|
---|
| 252 | case 2:
|
---|
| 253 | OtherHydrogenFlag = OtherHydrogenFlag && (Otherangle > M_PI*(104.5/180.) + MYEPSILON);
|
---|
| 254 | break;
|
---|
| 255 | default: // 3 or more hydrogens ...
|
---|
| 256 | OtherHydrogenFlag = false;
|
---|
| 257 | break;
|
---|
| 258 | }
|
---|
| 259 | if (InterfaceFlag && Interface2Flag && OtherHydrogenFlag) {
|
---|
| 260 | // on this element (Walker) we check for bond to hydrogen, i.e. part of water molecule
|
---|
| 261 | const BondList& ListOfBonds = (*Walker)->getListOfBonds();
|
---|
| 262 | for (BondList::const_iterator BondRunner = ListOfBonds.begin();
|
---|
| 263 | BondRunner != ListOfBonds.end();
|
---|
| 264 | BondRunner++) {
|
---|
| 265 | atom * const OtherAtom = (*BondRunner)->GetOtherAtom(*Walker);
|
---|
| 266 | if (OtherAtom->getType()->getAtomicNumber() == 1) {
|
---|
| 267 | // check angle
|
---|
| 268 | if (CheckHydrogenBridgeBondAngle(*(*Walker), *OtherAtom, *(*Runner))) {
|
---|
| 269 | count++;
|
---|
| 270 | break;
|
---|
| 271 | }
|
---|
[388049] | 272 | }
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
[153985] | 277 | } // end go through molecules
|
---|
| 278 | } // end gather molecules
|
---|
| 279 | } // end go through every oxygen
|
---|
| 280 | } // end go through every atom
|
---|
[388049] | 281 | }
|
---|
| 282 | return count;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | /** Counts the number of bonds between two given elements.
|
---|
| 286 | * \param *molecules list of molecules with all atoms
|
---|
| 287 | * \param *first pointer to first element
|
---|
| 288 | * \param *second pointer to second element
|
---|
| 289 | * \return number of found bonds (\a *first-\a *second)
|
---|
| 290 | */
|
---|
[2bfc5b] | 291 | int CountBondsOfTwo(const std::vector<molecule *> &molecules, const element * const first, const element * const second)
|
---|
[388049] | 292 | {
|
---|
| 293 | int count = 0;
|
---|
| 294 |
|
---|
[2bfc5b] | 295 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin();MolWalker != molecules.end(); MolWalker++) {
|
---|
[a7b761b] | 296 | molecule::iterator Walker = (*MolWalker)->begin();
|
---|
| 297 | for(;Walker!=(*MolWalker)->end();++Walker){
|
---|
| 298 | atom * theAtom = *Walker;
|
---|
[d74077] | 299 | if ((theAtom->getType() == first) || (theAtom->getType() == second)) { // first element matches
|
---|
[9d83b6] | 300 | const BondList& ListOfBonds = theAtom->getListOfBonds();
|
---|
| 301 | for (BondList::const_iterator BondRunner = ListOfBonds.begin();
|
---|
| 302 | BondRunner != ListOfBonds.end();
|
---|
| 303 | BondRunner++) {
|
---|
[a7b761b] | 304 | atom * const OtherAtom = (*BondRunner)->GetOtherAtom(theAtom);
|
---|
[735b1c] | 305 | if (((OtherAtom->getType() == first) || (OtherAtom->getType() == second)) && (theAtom->getNr() < OtherAtom->getNr())) {
|
---|
[388049] | 306 | count++;
|
---|
[47d041] | 307 | LOG(1, *first << "-" << *second << " bond found between " << *Walker << " and " << *OtherAtom << ".");
|
---|
[388049] | 308 | }
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
| 313 | return count;
|
---|
| 314 | };
|
---|
| 315 |
|
---|
| 316 | /** Counts the number of bonds between three given elements.
|
---|
| 317 | * Note that we do not look for arbitrary sequence of given bonds, but \a *second will be the central atom and we check
|
---|
| 318 | * whether it has bonds to both \a *first and \a *third.
|
---|
| 319 | * \param *molecules list of molecules with all atoms
|
---|
| 320 | * \param *first pointer to first element
|
---|
| 321 | * \param *second pointer to second element
|
---|
| 322 | * \param *third pointer to third element
|
---|
| 323 | * \return number of found bonds (\a *first-\a *second-\a *third, \a *third-\a *second-\a *first, respectively)
|
---|
| 324 | */
|
---|
[2bfc5b] | 325 | int CountBondsOfThree(const std::vector<molecule *> &molecules, const element * const first, const element * const second, const element * const third)
|
---|
[388049] | 326 | {
|
---|
| 327 | int count = 0;
|
---|
| 328 | bool MatchFlag[2];
|
---|
| 329 | bool result = false;
|
---|
| 330 | const element * ElementArray[2];
|
---|
| 331 | ElementArray[0] = first;
|
---|
| 332 | ElementArray[1] = third;
|
---|
| 333 |
|
---|
[2bfc5b] | 334 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin();MolWalker != molecules.end(); MolWalker++) {
|
---|
[a7b761b] | 335 | molecule::iterator Walker = (*MolWalker)->begin();
|
---|
| 336 | for(;Walker!=(*MolWalker)->end();++Walker){
|
---|
| 337 | atom *theAtom = *Walker;
|
---|
[d74077] | 338 | if (theAtom->getType() == second) { // first element matches
|
---|
[388049] | 339 | for (int i=0;i<2;i++)
|
---|
| 340 | MatchFlag[i] = false;
|
---|
[9d83b6] | 341 | const BondList& ListOfBonds = theAtom->getListOfBonds();
|
---|
| 342 | for (BondList::const_iterator BondRunner = ListOfBonds.begin();
|
---|
| 343 | BondRunner != ListOfBonds.end();
|
---|
| 344 | BondRunner++) {
|
---|
[a7b761b] | 345 | atom * const OtherAtom = (*BondRunner)->GetOtherAtom(theAtom);
|
---|
[388049] | 346 | for (int i=0;i<2;i++)
|
---|
[d74077] | 347 | if ((!MatchFlag[i]) && (OtherAtom->getType() == ElementArray[i])) {
|
---|
[388049] | 348 | MatchFlag[i] = true;
|
---|
| 349 | break; // each bonding atom can match at most one element we are looking for
|
---|
| 350 | }
|
---|
| 351 | }
|
---|
| 352 | result = true;
|
---|
| 353 | for (int i=0;i<2;i++) // gather results
|
---|
| 354 | result = result && MatchFlag[i];
|
---|
| 355 | if (result) { // check results
|
---|
| 356 | count++;
|
---|
[47d041] | 357 | LOG(1, *first << "-" << *second << "-" << *third << " bond found at " << *Walker << ".");
|
---|
[388049] | 358 | }
|
---|
| 359 | }
|
---|
| 360 | }
|
---|
| 361 | }
|
---|
| 362 | return count;
|
---|
| 363 | };
|
---|