| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "hydrogentools.h" |
| 7 |
|
| 8 |
#include "../core/mdlvalence_p.h" |
| 9 |
#include <avogadro/core/atomutilities.h> |
| 10 |
|
| 11 |
#include <QtCore/QDebug> |
| 12 |
|
| 13 |
#include <algorithm> |
| 14 |
#include <cmath> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
// C'mon windows.... |
| 18 |
#ifndef M_PI |
| 19 |
#define M_PI 3.14159265358979323846 |
| 20 |
#endif |
| 21 |
|
| 22 |
// Angle of tetrahedron |
| 23 |
#define M_TETRAHED 109.47122063449069389 |
| 24 |
|
| 25 |
using Avogadro::Vector3; |
| 26 |
using Avogadro::Core::Array; |
| 27 |
using Avogadro::Core::atomValence; |
| 28 |
using Avogadro::QtGui::RWAtom; |
| 29 |
using Avogadro::QtGui::RWBond; |
| 30 |
using Avogadro::QtGui::RWMolecule; |
| 31 |
|
| 32 |
namespace { |
| 33 |
|
| 34 |
typedef Avogadro::Core::Array<RWBond> NeighborListType; |
| 35 |
|
| 36 |
inline unsigned int countExistingBonds(const NeighborListType& bonds) |
| 37 |
{ |
| 38 |
unsigned int result(0); |
| 39 |
for (auto bond : bonds) { |
| 40 |
result += static_cast<unsigned int>(bond.order()); |
| 41 |
} |
| 42 |
return result; |
| 43 |
} |
| 44 |
|
| 45 |
inline unsigned int lookupValency(const RWAtom& atom, |
| 46 |
unsigned int numExistingBonds) |
| 47 |
{ |
| 48 |
signed char charge = atom.formalCharge(); |
| 49 |
unsigned int defaultVal = |
| 50 |
atomValence(atom.atomicNumber(), charge, numExistingBonds); |
| 51 |
|
| 52 |
// If hybridization is explicitly set (positive values, not perceived), |
| 53 |
// cap the valence at the number of bonding orbitals implied by it. |
| 54 |
// e.g., an sp2 carbon has max 3 bonds, not the default 4. |
| 55 |
auto hyb = atom.hybridization(); |
| 56 |
if (hyb > 0) { |
| 57 |
unsigned int maxFromHyb = static_cast<unsigned int>(hyb) + 1; |
| 58 |
if (maxFromHyb < defaultVal) |
| 59 |
return maxFromHyb; |
| 60 |
} |
| 61 |
|
| 62 |
return defaultVal; |
| 63 |
} |
| 64 |
|
| 65 |
inline float hydrogenBondDistance(unsigned char otherAtomicNumber) |
| 66 |
{ |
| 67 |
static double hCovRadius(Avogadro::Core::Elements::radiusCovalent(1)); |
| 68 |
double covRadius = |
| 69 |
Avogadro::Core::Elements::radiusCovalent(otherAtomicNumber); |
| 70 |
return static_cast<float>(hCovRadius + covRadius); |
| 71 |
} |
| 72 |
|
| 73 |
} // namespace |
| 74 |
|
| 75 |
namespace Avogadro::QtGui { |
| 76 |
|
| 77 |
void HydrogenTools::removeAllHydrogens(RWMolecule& molecule) |
| 78 |
{ |
| 79 |
const Array<unsigned char> atomicNums(molecule.atomicNumbers()); |
| 80 |
size_t atomIndex = molecule.atomCount() - 1; |
| 81 |
for (auto it = atomicNums.rbegin(), itEnd = atomicNums.rend(); it != itEnd; |
| 82 |
++it, --atomIndex) { |
| 83 |
if (*it == 1) |
| 84 |
molecule.removeAtom(atomIndex); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
void HydrogenTools::adjustHydrogens(RWMolecule& molecule, Adjustment adjustment) |
| 89 |
{ |
| 90 |
// This vector stores indices of hydrogens that need to be removed. Additions |
| 91 |
// are made first, followed by removals to keep indexing sane. |
| 92 |
std::vector<size_t> badHIndices; |
| 93 |
|
| 94 |
// Temporary container for calls to generateNewHydrogenPositions. |
| 95 |
std::vector<Vector3> newHPos; |
| 96 |
|
| 97 |
// Convert the adjustment option to a couple of booleans |
| 98 |
bool doAdd(adjustment == Add || adjustment == AddAndRemove); |
| 99 |
bool doRemove(adjustment == Remove || adjustment == AddAndRemove); |
| 100 |
|
| 101 |
// Limit to only the original atoms: |
| 102 |
const size_t numAtoms = molecule.atomCount(); |
| 103 |
|
| 104 |
// Iterate through all atoms in the molecule, adding hydrogens as needed |
| 105 |
// and building up a list of hydrogens that should be removed. |
| 106 |
for (size_t atomIndex = 0; atomIndex < numAtoms; ++atomIndex) { |
| 107 |
const RWAtom atom(molecule.atom(atomIndex)); |
| 108 |
int hDiff = valencyAdjustment(atom); |
| 109 |
// Add hydrogens: |
| 110 |
if (doAdd && hDiff > 0) { |
| 111 |
newHPos.clear(); |
| 112 |
generateNewHydrogenPositions(atom, hDiff, newHPos); |
| 113 |
for (auto& newHPo : newHPos) { |
| 114 |
RWAtom newH(molecule.addAtom(1)); |
| 115 |
newH.setPosition3d(newHPo); |
| 116 |
molecule.addBond(atom, newH, 1); |
| 117 |
} |
| 118 |
} |
| 119 |
// Add bad hydrogens to our list of hydrogens to remove: |
| 120 |
else if (doRemove && hDiff < 0) { |
| 121 |
extraHydrogenIndices(atom, -hDiff, badHIndices); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// Remove dead hydrogens now. Remove them in reverse-index order to keep |
| 126 |
// indexing sane. |
| 127 |
if (doRemove && !badHIndices.empty()) { |
| 128 |
std::sort(badHIndices.begin(), badHIndices.end()); |
| 129 |
auto newEnd(std::unique(badHIndices.begin(), badHIndices.end())); |
| 130 |
badHIndices.resize(std::distance(badHIndices.begin(), newEnd)); |
| 131 |
for (auto it = badHIndices.rbegin(), itEnd = badHIndices.rend(); |
| 132 |
it != itEnd; ++it) { |
| 133 |
molecule.removeAtom(*it); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void HydrogenTools::adjustHydrogens(RWAtom& atom, Adjustment adjustment) |
| 139 |
{ |
| 140 |
// Convert the adjustment option to a couple of booleans |
| 141 |
bool doAdd(adjustment == Add || adjustment == AddAndRemove); |
| 142 |
bool doRemove(adjustment == Remove || adjustment == AddAndRemove); |
| 143 |
|
| 144 |
// convenience |
| 145 |
RWMolecule* molecule = atom.molecule(); |
| 146 |
|
| 147 |
if (molecule == nullptr) |
| 148 |
return; |
| 149 |
|
| 150 |
if (doRemove) { |
| 151 |
// get the list of hydrogens connected to this |
| 152 |
std::vector<size_t> badHIndices; |
| 153 |
|
| 154 |
const NeighborListType bonds(molecule->bonds(atom)); |
| 155 |
for (auto bond : bonds) { |
| 156 |
const RWAtom otherAtom = bond.getOtherAtom(atom); |
| 157 |
if (otherAtom.atomicNumber() == 1) { |
| 158 |
badHIndices.push_back(otherAtom.index()); |
| 159 |
} |
| 160 |
} // end loop through bonds |
| 161 |
|
| 162 |
std::sort(badHIndices.begin(), badHIndices.end()); |
| 163 |
auto newEnd(std::unique(badHIndices.begin(), badHIndices.end())); |
| 164 |
badHIndices.resize(std::distance(badHIndices.begin(), newEnd)); |
| 165 |
for (auto it = badHIndices.rbegin(), itEnd = badHIndices.rend(); |
| 166 |
it != itEnd; ++it) { |
| 167 |
molecule->removeAtom(*it); |
| 168 |
} |
| 169 |
} // end removing H atoms on this one |
| 170 |
|
| 171 |
int hDiff = valencyAdjustment(atom); |
| 172 |
// Add hydrogens: |
| 173 |
if (doAdd && hDiff > 0) { |
| 174 |
// Temporary container for calls to generateNewHydrogenPositions. |
| 175 |
std::vector<Vector3> newHPos; |
| 176 |
generateNewHydrogenPositions(atom, hDiff, newHPos); |
| 177 |
for (auto& newHPo : newHPos) { |
| 178 |
RWAtom newH(molecule->addAtom(1)); |
| 179 |
newH.setPosition3d(newHPo); |
| 180 |
molecule->addBond(atom, newH, 1); |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
int HydrogenTools::valencyAdjustment(const RWAtom& atom) |
| 186 |
{ |
| 187 |
int result = 0; |
| 188 |
if (atom.isValid()) { |
| 189 |
const NeighborListType bonds(atom.molecule()->bonds(atom)); |
| 190 |
// sum of bond orders |
| 191 |
const unsigned int numberOfBonds(countExistingBonds(bonds)); |
| 192 |
const unsigned int valency(lookupValency(atom, numberOfBonds)); |
| 193 |
result = static_cast<int>(valency) - static_cast<int>(numberOfBonds); |
| 194 |
} |
| 195 |
|
| 196 |
// qDebug() << " valence adjustment " << result; |
| 197 |
return result; |
| 198 |
} |
| 199 |
|
| 200 |
int HydrogenTools::extraHydrogenIndices(const RWAtom& atom, |
| 201 |
int numberOfHydrogens, |
| 202 |
std::vector<size_t>& indices) |
| 203 |
{ |
| 204 |
if (!atom.isValid()) |
| 205 |
return 0; |
| 206 |
|
| 207 |
int result = 0; |
| 208 |
const NeighborListType bonds(atom.molecule()->bonds(atom)); |
| 209 |
for (auto it = bonds.begin(), itEnd = bonds.end(); |
| 210 |
it != itEnd && result < numberOfHydrogens; ++it) { |
| 211 |
const RWAtom otherAtom = it->getOtherAtom(atom); |
| 212 |
if (otherAtom.atomicNumber() == 1) { |
| 213 |
indices.push_back(otherAtom.index()); |
| 214 |
++result; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
return result; |
| 219 |
} |
| 220 |
|
| 221 |
void HydrogenTools::generateNewHydrogenPositions( |
| 222 |
const RWAtom& atom, int numberOfHydrogens, std::vector<Vector3>& positions) |
| 223 |
{ |
| 224 |
if (!atom.isValid()) |
| 225 |
return; |
| 226 |
|
| 227 |
// Get the hybridization |
| 228 |
Core::AtomHybridization hybridization = atom.hybridization(); |
| 229 |
if (hybridization == Core::HybridizationUnknown) { |
| 230 |
// Perceive it |
| 231 |
hybridization = Core::AtomUtilities::perceiveHybridization( |
| 232 |
Core::Atom(dynamic_cast<Core::Molecule*>(&atom.molecule()->molecule()), |
| 233 |
atom.index())); |
| 234 |
} |
| 235 |
|
| 236 |
const Avogadro::Real bondLength = hydrogenBondDistance(atom.atomicNumber()); |
| 237 |
|
| 238 |
// Get a list of all bond vectors (normalized, pointing away from 'atom') |
| 239 |
std::vector<Vector3> allVectors; |
| 240 |
const NeighborListType bonds(atom.molecule()->bonds(atom)); |
| 241 |
allVectors.reserve(bonds.size() + static_cast<size_t>(numberOfHydrogens)); |
| 242 |
for (auto bond : bonds) { |
| 243 |
RWAtom otherAtom = bond.getOtherAtom(atom); |
| 244 |
Vector3 delta = otherAtom.position3d() - atom.position3d(); |
| 245 |
if (!delta.isZero(1e-5)) { |
| 246 |
allVectors.push_back(delta.normalized()); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
for (int impHIndex = 0; impHIndex < numberOfHydrogens; ++impHIndex) { |
| 251 |
// First try to derive the bond vector based on the hybridization |
| 252 |
// Fallback will be to a random vector |
| 253 |
Vector3 newPos = Core::AtomUtilities::generateNewBondVector( |
| 254 |
Core::Atom(dynamic_cast<Core::Molecule*>(&atom.molecule()->molecule()), |
| 255 |
atom.index()), |
| 256 |
allVectors, hybridization); |
| 257 |
allVectors.push_back(newPos); |
| 258 |
positions.emplace_back(atom.position3d() + (newPos * bondLength)); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
} // namespace Avogadro::QtGui |