| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 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/>. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | /* | 
|---|
| 24 | * analysis_bonds.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Nov 7, 2009 | 
|---|
| 27 | *      Author: heber | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | #include "analysis_bonds.hpp" | 
|---|
| 38 | #include "Atom/atom.hpp" | 
|---|
| 39 | #include "Bond/bond.hpp" | 
|---|
| 40 | #include "Element/element.hpp" | 
|---|
| 41 | #include "CodePatterns/Info.hpp" | 
|---|
| 42 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 43 | #include "CodePatterns/Log.hpp" | 
|---|
| 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; | 
|---|
| 59 | for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) { | 
|---|
| 60 | const BondList& ListOfBonds = (*iter)->getListOfBonds(); | 
|---|
| 61 | const int count = ListOfBonds.size(); | 
|---|
| 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) | 
|---|
| 70 | ELOG(1, "Something is wrong with the bond structure, the number of bonds is not even!"); | 
|---|
| 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 | */ | 
|---|
| 82 | void MinMeanMaxBondDistanceBetweenElements(const molecule *mol, const element *type1, const element *type2, double &Min, double &Mean, double &Max) | 
|---|
| 83 | { | 
|---|
| 84 | Min = 2e+6; | 
|---|
| 85 | Mean = 0.; | 
|---|
| 86 | Max = -2e+6; | 
|---|
| 87 |  | 
|---|
| 88 | int AtomNo = 0; | 
|---|
| 89 | for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) { | 
|---|
| 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++) | 
|---|
| 95 | if ((*BondRunner)->GetOtherAtom((*iter))->getType() == type2) { | 
|---|
| 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 | } | 
|---|
| 104 | } | 
|---|
| 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 | }; | 
|---|
| 114 |  | 
|---|
| 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 | */ | 
|---|
| 121 | double CalculateAngle(const Vector &first, const Vector ¢ral, const Vector &second) | 
|---|
| 122 | { | 
|---|
| 123 | Vector OHBond; | 
|---|
| 124 | Vector OOBond; | 
|---|
| 125 |  | 
|---|
| 126 | OHBond = first - central; | 
|---|
| 127 | OOBond = second - central; | 
|---|
| 128 | const double angle = OHBond.Angle(OOBond); | 
|---|
| 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 | */ | 
|---|
| 139 | bool CheckHydrogenBridgeBondAngle(const atom & Oxygen, const atom & Hydrogen, const atom & OtherOxygen) | 
|---|
| 140 | { | 
|---|
| 141 | Info FunctionInfo(__func__); | 
|---|
| 142 |  | 
|---|
| 143 | // check angle | 
|---|
| 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.)) { | 
|---|
| 150 | return true; | 
|---|
| 151 | } else { | 
|---|
| 152 | return false; | 
|---|
| 153 | } | 
|---|
| 154 | }; | 
|---|
| 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 | 
|---|
| 161 | * \param *Interface2Element or NULL | 
|---|
| 162 | */ | 
|---|
| 163 | int CountHydrogenBridgeBonds(const std::vector<molecule *> &molecules, const element * InterfaceElement = NULL, const element * Interface2Element = NULL) | 
|---|
| 164 | { | 
|---|
| 165 | Info FunctionInfo(__func__); | 
|---|
| 166 |  | 
|---|
| 167 | int count = 0; | 
|---|
| 168 | int OtherHydrogens = 0; | 
|---|
| 169 | double Otherangle = 0.; | 
|---|
| 170 | bool InterfaceFlag = false; | 
|---|
| 171 | bool Interface2Flag = false; | 
|---|
| 172 | bool OtherHydrogenFlag = true; | 
|---|
| 173 | LinkedCell::LinkedCell_View LC = World::getInstance().getLinkedCell(HBRIDGEDISTANCE); | 
|---|
| 174 |  | 
|---|
| 175 | // go through every molecule | 
|---|
| 176 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); | 
|---|
| 177 | MolWalker != molecules.end(); | 
|---|
| 178 | ++MolWalker) { | 
|---|
| 179 | LOG(2, "INFO: Current molecule is " << (*MolWalker)->getName() << "."); | 
|---|
| 180 |  | 
|---|
| 181 | // go through every atom | 
|---|
| 182 | typedef std::set<const molecule *> Moleculeset; | 
|---|
| 183 | for(molecule::const_iterator Walker = const_cast<const molecule *>(*MolWalker)->begin(); | 
|---|
| 184 | Walker != const_cast<const molecule *>(*MolWalker)->end(); | 
|---|
| 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(); | 
|---|
| 233 | for (BondList::const_iterator BondRunner = ListOfBonds.begin(); | 
|---|
| 234 | BondRunner != ListOfBonds.end(); | 
|---|
| 235 | BondRunner++) { | 
|---|
| 236 | atom * const OtherAtom = (*BondRunner)->GetOtherAtom(*Runner); | 
|---|
| 237 | // if hydrogen, check angle to be greater(!) than 30 degrees | 
|---|
| 238 | if (OtherAtom->getType()->getAtomicNumber() == 1) { | 
|---|
| 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 | } | 
|---|
| 272 | } | 
|---|
| 273 | } | 
|---|
| 274 | } | 
|---|
| 275 | } | 
|---|
| 276 | } | 
|---|
| 277 | } // end go through molecules | 
|---|
| 278 | } // end gather molecules | 
|---|
| 279 | } // end go through every oxygen | 
|---|
| 280 | } // end go through every atom | 
|---|
| 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 | */ | 
|---|
| 291 | int CountBondsOfTwo(const std::vector<molecule *> &molecules, const element * const first, const element * const second) | 
|---|
| 292 | { | 
|---|
| 293 | int count = 0; | 
|---|
| 294 |  | 
|---|
| 295 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin();MolWalker != molecules.end(); MolWalker++) { | 
|---|
| 296 | molecule::iterator Walker = (*MolWalker)->begin(); | 
|---|
| 297 | for(;Walker!=(*MolWalker)->end();++Walker){ | 
|---|
| 298 | atom * theAtom = *Walker; | 
|---|
| 299 | if ((theAtom->getType() == first) || (theAtom->getType() == second)) {  // first element matches | 
|---|
| 300 | const BondList& ListOfBonds = theAtom->getListOfBonds(); | 
|---|
| 301 | for (BondList::const_iterator BondRunner = ListOfBonds.begin(); | 
|---|
| 302 | BondRunner != ListOfBonds.end(); | 
|---|
| 303 | BondRunner++) { | 
|---|
| 304 | atom * const OtherAtom = (*BondRunner)->GetOtherAtom(theAtom); | 
|---|
| 305 | if (((OtherAtom->getType() == first) || (OtherAtom->getType() == second)) && (theAtom->getNr() < OtherAtom->getNr())) { | 
|---|
| 306 | count++; | 
|---|
| 307 | LOG(1, *first << "-" << *second << " bond found between " << *Walker << " and " << *OtherAtom << "."); | 
|---|
| 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 | */ | 
|---|
| 325 | int CountBondsOfThree(const std::vector<molecule *> &molecules, const element * const first, const element * const second, const element * const third) | 
|---|
| 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 |  | 
|---|
| 334 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin();MolWalker != molecules.end(); MolWalker++) { | 
|---|
| 335 | molecule::iterator Walker = (*MolWalker)->begin(); | 
|---|
| 336 | for(;Walker!=(*MolWalker)->end();++Walker){ | 
|---|
| 337 | atom *theAtom = *Walker; | 
|---|
| 338 | if (theAtom->getType() == second) {  // first element matches | 
|---|
| 339 | for (int i=0;i<2;i++) | 
|---|
| 340 | MatchFlag[i] = false; | 
|---|
| 341 | const BondList& ListOfBonds = theAtom->getListOfBonds(); | 
|---|
| 342 | for (BondList::const_iterator BondRunner = ListOfBonds.begin(); | 
|---|
| 343 | BondRunner != ListOfBonds.end(); | 
|---|
| 344 | BondRunner++) { | 
|---|
| 345 | atom * const OtherAtom = (*BondRunner)->GetOtherAtom(theAtom); | 
|---|
| 346 | for (int i=0;i<2;i++) | 
|---|
| 347 | if ((!MatchFlag[i]) && (OtherAtom->getType() == ElementArray[i])) { | 
|---|
| 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++; | 
|---|
| 357 | LOG(1, *first << "-" << *second << "-" << *third << " bond found at " << *Walker << "."); | 
|---|
| 358 | } | 
|---|
| 359 | } | 
|---|
| 360 | } | 
|---|
| 361 | } | 
|---|
| 362 | return count; | 
|---|
| 363 | }; | 
|---|