1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
//
// Copyright (C) 2004-2021 Greg Landrum and other RDKit contributors
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
//
#include <list>
#include <RDGeneral/RDLog.h>
#include <GraphMol/Chirality.h>
#include <GraphMol/FileParsers/MolFileStereochem.h>
#include <Geometry/point.h>
#include <RDGeneral/BoostStartInclude.h>
#include <boost/dynamic_bitset.hpp>
#include <RDGeneral/BoostEndInclude.h>
#include <algorithm>
#include <RDGeneral/Ranking.h>
#include <RDGeneral/FileParseException.h>
namespace RDKit {
void WedgeBond(Bond *bond, unsigned int fromAtomIdx, const Conformer *conf) {
Chirality::wedgeBond(bond, fromAtomIdx, conf);
}
void WedgeMolBonds(ROMol &mol, const Conformer *conf) {
return Chirality::wedgeMolBonds(mol, conf);
}
std::vector<Bond *> getBondNeighbors(ROMol &mol, const Bond &bond) {
std::vector<Bond *> res;
for (auto nbri :
boost::make_iterator_range(mol.getAtomBonds(bond.getBeginAtom()))) {
auto nbrBond = mol[nbri];
if (nbrBond == &bond) {
continue;
}
res.push_back(nbrBond);
}
for (auto nbri :
boost::make_iterator_range(mol.getAtomBonds(bond.getEndAtom()))) {
auto nbrBond = mol[nbri];
if (nbrBond == &bond) {
continue;
}
res.push_back(nbrBond);
}
return res;
}
const Atom *getNonsharedAtom(const Bond &bond1, const Bond &bond2) {
if (bond1.getBeginAtomIdx() == bond2.getBeginAtomIdx() ||
bond1.getBeginAtomIdx() == bond2.getEndAtomIdx()) {
return bond1.getEndAtom();
} else if (bond1.getEndAtomIdx() == bond2.getBeginAtomIdx() ||
bond1.getEndAtomIdx() == bond2.getEndAtomIdx()) {
return bond1.getBeginAtom();
}
POSTCONDITION(0, "bonds don't share an atom");
}
const unsigned StereoBondThresholds::DBL_BOND_NO_STEREO;
const unsigned StereoBondThresholds::DBL_BOND_SPECIFIED_STEREO;
const unsigned StereoBondThresholds::CHIRAL_ATOM;
const unsigned StereoBondThresholds::DIRECTION_SET;
// a note on the way the StereoBondThresholds are used:
// the penalties are all 1/10th of the corresponding threshold, so
// the penalty for being connected to a chiral atom is
// StereoBondThresholds::CHIRAL_ATOM/10
// This allows us to just add up the penalties for a particular
// single bond and still use one set of thresholds - an individual
// single bond will never have any particular penalty term applied
// more than a couple of times
//
void addWavyBondsForStereoAny(ROMol &mol, bool clearDoubleBondFlags,
unsigned addWhenImpossible) {
std::vector<int> singleBondScores(mol.getNumBonds(), 0);
// used to store the double bond neighbors, if any, of each single bond
std::map<unsigned, std::vector<unsigned>> singleBondNeighbors;
boost::dynamic_bitset<> doubleBondsToSet(mol.getNumBonds());
// mark single bonds adjacent to double bonds
for (const auto dblBond : mol.bonds()) {
if (dblBond->getBondType() != Bond::BondType::DOUBLE) {
continue;
}
if (dblBond->getStereo() == Bond::BondStereo::STEREOANY) {
doubleBondsToSet.set(dblBond->getIdx());
}
for (auto singleBond : getBondNeighbors(mol, *dblBond)) {
if (singleBond->getBondType() != Bond::BondType::SINGLE) {
continue;
}
// NOTE: we could make this canonical by initializing scores to the
// canonical atom ranks
int score = singleBondScores[singleBond->getIdx()];
++score;
// penalty for having a direction already set
if (singleBond->getBondDir() != Bond::BondDir::NONE) {
score += StereoBondThresholds::DIRECTION_SET / 10;
}
// penalties from the double bond itself:
// penalize being adjacent to a double bond with empty stereo:
if (dblBond->getStereo() == Bond::BondStereo::STEREONONE) {
score += StereoBondThresholds::DBL_BOND_NO_STEREO / 10;
} else if (dblBond->getStereo() > Bond::BondStereo::STEREOANY) {
// penalize being adjacent to a double bond with specified stereo:
score += StereoBondThresholds::DBL_BOND_SPECIFIED_STEREO / 10;
}
// atom-related penalties
auto otherAtom = getNonsharedAtom(*singleBond, *dblBond);
// favor atoms with smaller numbers of neighbors:
score += 10 * otherAtom->getDegree();
// penalty for being adjacent to an atom with specified stereo
if (otherAtom->getChiralTag() != Atom::ChiralType::CHI_UNSPECIFIED &&
otherAtom->getChiralTag() != Atom::ChiralType::CHI_OTHER) {
score += StereoBondThresholds::CHIRAL_ATOM / 10;
}
singleBondScores[singleBond->getIdx()] = score;
if (dblBond->getStereo() == Bond::BondStereo::STEREOANY) {
singleBondNeighbors[singleBond->getIdx()].push_back(dblBond->getIdx());
}
}
}
std::vector<std::tuple<int, unsigned int, size_t>> sortedScores;
for (size_t i = 0; i < mol.getNumBonds(); ++i) {
auto score = singleBondScores[i];
if (!score) {
continue;
}
sortedScores.push_back(
std::make_tuple(-1 * singleBondNeighbors[i].size(), score, i));
}
std::sort(sortedScores.begin(), sortedScores.end());
for (const auto &tpl : sortedScores) {
// FIX: check if dir is already set
for (auto dblBondIdx : singleBondNeighbors[std::get<2>(tpl)]) {
if (doubleBondsToSet[dblBondIdx]) {
if (addWhenImpossible) {
if (std::get<1>(tpl) > addWhenImpossible) {
continue;
}
} else if (std::get<1>(tpl) >
StereoBondThresholds::DBL_BOND_NO_STEREO) {
BOOST_LOG(rdWarningLog)
<< "Setting wavy bond flag on bond " << std::get<2>(tpl)
<< " which may make other stereo info ambiguous" << std::endl;
}
mol.getBondWithIdx(std::get<2>(tpl))
->setBondDir(Bond::BondDir::UNKNOWN);
if (clearDoubleBondFlags) {
auto dblBond = mol.getBondWithIdx(dblBondIdx);
if (dblBond->getBondDir() == Bond::BondDir::EITHERDOUBLE) {
dblBond->setBondDir(Bond::BondDir::NONE);
}
dblBond->setStereo(Bond::BondStereo::STEREONONE);
}
doubleBondsToSet.reset(dblBondIdx);
}
}
}
if (addWhenImpossible) {
if (doubleBondsToSet.count()) {
std::stringstream sstr;
sstr << " unable to set wavy bonds for double bonds:";
for (size_t i = 0; i < mol.getNumBonds(); ++i) {
if (doubleBondsToSet[i]) {
sstr << " " << i;
}
}
BOOST_LOG(rdWarningLog) << sstr.str() << std::endl;
}
}
}
//
// Determine bond wedge state
///
Bond::BondDir DetermineBondWedgeState(const Bond *bond,
unsigned int fromAtomIdx,
const Conformer *conf) {
return Chirality::detail::determineBondWedgeState(bond, fromAtomIdx, conf);
}
Bond::BondDir DetermineBondWedgeState(
const Bond *bond,
const std::map<int, std::unique_ptr<RDKit::Chirality::WedgeInfoBase>>
&wedgeBonds,
const Conformer *conf) {
return Chirality::detail::determineBondWedgeState(bond, wedgeBonds, conf);
}
// handles stereochem markers set by the Mol file parser and
// converts them to the RD standard:
void DetectAtomStereoChemistry(RWMol &mol, const Conformer *conf) {
PRECONDITION(conf, "no conformer");
PRECONDITION(&(conf->getOwningMol()) == &mol,
"conformer does not belong to molecule");
MolOps::assignChiralTypesFromBondDirs(mol, conf->getId(), true);
}
void ClearSingleBondDirFlags(ROMol &mol) {
MolOps::clearSingleBondDirFlags(mol);
}
void DetectBondStereoChemistry(ROMol &mol, const Conformer *conf) {
PRECONDITION(conf, "no conformer");
PRECONDITION(&(conf->getOwningMol()) == &mol,
"conformer does not belong to molecule");
MolOps::detectBondStereochemistry(mol, conf->getId());
}
void reapplyMolBlockWedging(RWMol &mol) {
RDKit::Chirality::reapplyMolBlockWedging(mol);
}
void clearMolBlockWedgingInfo(RWMol &mol) {
Chirality::clearMolBlockWedgingInfo(mol);
}
void invertMolBlockWedgingInfo(ROMol &mol) {
Chirality::invertMolBlockWedgingInfo(mol);
}
void markUnspecifiedStereoAsUnknown(ROMol &mol, int confId) {
PRECONDITION(mol.getNumConformers(), "no conformer");
const auto conf = mol.getConformer(confId);
auto wedgeBonds = RDKit::Chirality::pickBondsToWedge(mol, nullptr, &conf);
for (auto b : mol.bonds()) {
if (b->getBondType() == Bond::DOUBLE) {
int dirCode;
bool reverse;
RDKit::Chirality::GetMolFileBondStereoInfo(b, wedgeBonds, &conf, dirCode,
reverse);
if (dirCode == 3) {
b->setStereo(Bond::STEREOANY);
}
}
}
static int noNbrs = 100;
auto si = Chirality::findPotentialStereo(mol);
if (si.size()) {
std::pair<bool, INT_VECT> retVal =
Chirality::detail::countChiralNbrs(mol, noNbrs);
INT_VECT nChiralNbrs = retVal.second;
for (auto i : si) {
if (i.type == Chirality::StereoType::Atom_Tetrahedral &&
i.specified == Chirality::StereoSpecified::Unspecified) {
i.specified = Chirality::StereoSpecified::Unknown;
auto atom = mol.getAtomWithIdx(i.centeredOn);
std::map<int, std::unique_ptr<RDKit::Chirality::WedgeInfoBase>>
resSoFar;
int bndIdx = Chirality::detail::pickBondToWedge(atom, mol, nChiralNbrs,
resSoFar, noNbrs);
auto bond = mol.getBondWithIdx(bndIdx);
bond->setBondDir(Bond::UNKNOWN);
}
}
}
}
void translateChiralFlagToStereoGroups(ROMol &mol,
StereoGroupType zeroFlagGroupType) {
if (!mol.hasProp(common_properties::_MolFileChiralFlag)) {
return;
}
int flagVal = 0;
mol.getProp(common_properties::_MolFileChiralFlag, flagVal);
mol.clearProp(common_properties::_MolFileChiralFlag);
StereoGroupType sgType =
flagVal ? StereoGroupType::STEREO_ABSOLUTE : zeroFlagGroupType;
auto sgs = mol.getStereoGroups();
boost::dynamic_bitset<> sgAtoms(mol.getNumAtoms());
boost::dynamic_bitset<> sgBonds(mol.getNumBonds());
const StereoGroup *absGroup = nullptr;
for (const auto &sg : sgs) {
for (const auto aptr : sg.getAtoms()) {
sgAtoms.set(aptr->getIdx());
}
for (const auto bptr : sg.getBonds()) {
sgBonds.set(bptr->getIdx());
}
// if we already have an ABS group, we'll add to it
if (sgType == StereoGroupType::STEREO_ABSOLUTE && !absGroup &&
sg.getGroupType() == StereoGroupType::STEREO_ABSOLUTE) {
absGroup = &sg;
}
}
ROMol::ATOM_PTR_VECT stereoAts;
ROMol::BOND_PTR_VECT stereoBds;
for (const auto atom : mol.atoms()) {
if (!sgAtoms[atom->getIdx()] &&
(atom->getChiralTag() == Atom::ChiralType::CHI_TETRAHEDRAL_CCW ||
atom->getChiralTag() == Atom::ChiralType::CHI_TETRAHEDRAL_CW)) {
stereoAts.push_back(atom);
}
}
for (const auto bond : mol.bonds()) {
if (!sgBonds[bond->getIdx()] &&
(bond->getStereo() == Bond::BondStereo::STEREOATROPCCW ||
bond->getStereo() == Bond::BondStereo::STEREOATROPCW)) {
stereoBds.push_back(bond);
}
}
if (!stereoAts.empty() || !stereoBds.empty()) {
if (!absGroup) {
sgs.emplace_back(sgType, stereoAts, stereoBds, 0);
mol.setStereoGroups(sgs);
} else {
std::vector<StereoGroup> newSgs;
for (const auto &sg : sgs) {
if (&sg != absGroup) {
newSgs.push_back(sg);
} else {
ROMol::ATOM_PTR_VECT newStereoAtoms = sg.getAtoms();
newStereoAtoms.insert(newStereoAtoms.end(), stereoAts.begin(),
stereoAts.end());
ROMol::BOND_PTR_VECT newStereoBonds = sg.getBonds();
newStereoBonds.insert(newStereoBonds.end(), stereoBds.begin(),
stereoBds.end());
newSgs.emplace_back(StereoGroupType::STEREO_ABSOLUTE, newStereoAtoms,
newStereoBonds, 0);
}
}
mol.setStereoGroups(newSgs);
}
}
}
} // namespace RDKit
|