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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
|
//
// Copyright (C) 2020 Greg Landrum and T5 Informatics GmbH
//
// @@ 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 <GraphMol/RDKitBase.h>
#include <RDGeneral/Ranking.h>
#include <GraphMol/new_canon.h>
#include <RDGeneral/types.h>
#include <algorithm>
#include <RDGeneral/utils.h>
#include <RDGeneral/Invariant.h>
#include <RDGeneral/RDLog.h>
#include <boost/dynamic_bitset.hpp>
#include <boost/format.hpp>
#include "Chirality.h"
namespace RDKit {
namespace Chirality {
#ifndef _MSC_VER
const unsigned StereoInfo::NOATOM = std::numeric_limits<unsigned>::max();
#endif
namespace detail {
StereoInfo getStereoInfo(const Bond *bond) {
PRECONDITION(bond, "bond is null");
StereoInfo sinfo;
const auto beginAtom = bond->getBeginAtom();
const auto endAtom = bond->getEndAtom();
if (bond->getBondType() == Bond::BondType::DOUBLE) {
if (beginAtom->getDegree() < 2 || endAtom->getDegree() < 2 ||
beginAtom->getDegree() > 3 || endAtom->getDegree() > 3) {
throw ValueErrorException("invalid atom degree in getStereoInfo(bond)");
}
sinfo.type = StereoType::Bond_Double;
sinfo.centeredOn = bond->getIdx();
sinfo.controllingAtoms.reserve(4);
bool seenSquiggleBond = false;
const auto &mol = bond->getOwningMol();
for (const auto &nbri :
boost::make_iterator_range(mol.getAtomBonds(beginAtom))) {
const auto &nbr = mol[nbri];
if (nbr->getIdx() != bond->getIdx()) {
if (nbr->getBondDir() == Bond::BondDir::UNKNOWN) {
seenSquiggleBond = true;
}
sinfo.controllingAtoms.push_back(
nbr->getOtherAtomIdx(beginAtom->getIdx()));
}
}
if (beginAtom->getDegree() == 2) {
sinfo.controllingAtoms.push_back(StereoInfo::NOATOM);
}
for (const auto &nbri :
boost::make_iterator_range(mol.getAtomBonds(endAtom))) {
const auto &nbr = mol[nbri];
if (nbr->getIdx() != bond->getIdx()) {
if (nbr->getBondDir() == Bond::BondDir::UNKNOWN) {
seenSquiggleBond = true;
}
sinfo.controllingAtoms.push_back(
nbr->getOtherAtomIdx(endAtom->getIdx()));
}
}
if (endAtom->getDegree() == 2) {
sinfo.controllingAtoms.push_back(StereoInfo::NOATOM);
}
Bond::BondStereo stereo = bond->getStereo();
if (stereo == Bond::BondStereo::STEREOANY ||
bond->getBondDir() == Bond::BondDir::EITHERDOUBLE || seenSquiggleBond) {
sinfo.specified = Chirality::StereoSpecified::Unknown;
} else if (stereo != Bond::BondStereo::STEREONONE) {
if (stereo == Bond::BondStereo::STEREOE ||
stereo == Bond::BondStereo::STEREOZ) {
stereo = Chirality::translateEZLabelToCisTrans(stereo);
}
sinfo.specified = Chirality::StereoSpecified::Specified;
const auto satoms = bond->getStereoAtoms();
if (satoms.size() != 2) {
throw ValueErrorException("only can support 2 stereo neighbors");
}
bool firstAtBegin;
if (satoms[0] == static_cast<int>(sinfo.controllingAtoms[0])) {
firstAtBegin = true;
} else if (satoms[0] == static_cast<int>(sinfo.controllingAtoms[1])) {
firstAtBegin = false;
} else {
throw ValueErrorException("controlling atom mismatch at begin");
}
bool firstAtEnd;
if (satoms[1] == static_cast<int>(sinfo.controllingAtoms[2])) {
firstAtEnd = true;
} else if (satoms[1] == static_cast<int>(sinfo.controllingAtoms[3])) {
firstAtEnd = false;
} else {
throw ValueErrorException("controlling atom mismatch at end");
}
auto mismatch = firstAtBegin ^ firstAtEnd;
if (mismatch) {
stereo = (stereo == Bond::BondStereo::STEREOCIS
? Bond::BondStereo::STEREOTRANS
: Bond::BondStereo::STEREOCIS);
}
switch (stereo) {
case Bond::BondStereo::STEREOCIS:
sinfo.descriptor = Chirality::StereoDescriptor::Bond_Cis;
break;
case Bond::BondStereo::STEREOTRANS:
sinfo.descriptor = Chirality::StereoDescriptor::Bond_Trans;
break;
default:
UNDER_CONSTRUCTION("unrecognized bond stereo type");
}
}
} else {
UNDER_CONSTRUCTION("unsupported bond type in getStereoInfo()");
}
return sinfo;
}
StereoInfo getStereoInfo(const Atom *atom) {
PRECONDITION(atom, "atom is null");
StereoInfo sinfo;
sinfo.type = StereoType::Atom_Tetrahedral;
sinfo.centeredOn = atom->getIdx();
sinfo.controllingAtoms.reserve(atom->getDegree());
const auto &mol = atom->getOwningMol();
int explicitUnknownStereo = 0;
for (const auto &nbri : boost::make_iterator_range(mol.getAtomBonds(atom))) {
const auto &bnd = mol[nbri];
if (bnd->getBondDir() == Bond::UNKNOWN) {
explicitUnknownStereo = 1;
} else if (!explicitUnknownStereo) {
bnd->getPropIfPresent<int>(common_properties::_UnknownStereo,
explicitUnknownStereo);
}
sinfo.controllingAtoms.push_back(bnd->getOtherAtomIdx(atom->getIdx()));
}
std::vector<unsigned> origNbrOrder = sinfo.controllingAtoms;
std::sort(sinfo.controllingAtoms.begin(), sinfo.controllingAtoms.end());
if (explicitUnknownStereo) {
sinfo.specified = StereoSpecified::Unknown;
} else {
Atom::ChiralType stereo = atom->getChiralTag();
if (stereo == Atom::ChiralType::CHI_TETRAHEDRAL_CCW ||
stereo == Atom::ChiralType::CHI_TETRAHEDRAL_CW) {
sinfo.specified = StereoSpecified::Specified;
unsigned nSwaps =
countSwapsToInterconvert(origNbrOrder, sinfo.controllingAtoms);
if (nSwaps % 2) {
stereo = (stereo == Atom::ChiralType::CHI_TETRAHEDRAL_CCW
? Atom::ChiralType::CHI_TETRAHEDRAL_CW
: Atom::ChiralType::CHI_TETRAHEDRAL_CCW);
}
switch (stereo) {
case Atom::ChiralType::CHI_TETRAHEDRAL_CCW:
sinfo.descriptor = StereoDescriptor::Tet_CCW;
break;
case Atom::ChiralType::CHI_TETRAHEDRAL_CW:
sinfo.descriptor = StereoDescriptor::Tet_CW;
break;
default:
UNDER_CONSTRUCTION("unrecognized chiral flag");
}
}
}
return sinfo;
}
bool isBondPotentialStereoBond(const Bond *bond) {
PRECONDITION(bond, "bond is null");
if (bond->getBondType() != Bond::BondType::DOUBLE) {
return false;
}
// at the moment the condition for being a potential stereo bond is that
// each of the beginning and end neighbors must have at least 2 heavy atom
// neighbors i.e. C/C=N/[H] is not a possible stereo bond but no more than 3
// total neighbors.
// if it's a ring bond, the smallest ring it's in must have at least 8 members
// (this is common with InChI)
const auto beginAtom = bond->getBeginAtom();
auto begHeavyDegree =
beginAtom->getTotalDegree() - beginAtom->getTotalNumHs(true);
const auto endAtom = bond->getEndAtom();
auto endHeavyDegree =
endAtom->getTotalDegree() - endAtom->getTotalNumHs(true);
if (begHeavyDegree > 1 && beginAtom->getDegree() < 4 && endHeavyDegree > 1 &&
endAtom->getDegree() < 4) {
// check rings
const auto ri = bond->getOwningMol().getRingInfo();
for (const auto &bring : ri->bondRings()) {
if (bring.size() < 8 && std::find(bring.begin(), bring.end(),
bond->getIdx()) != bring.end()) {
return false;
}
}
return true;
} else {
return false;
}
}
bool isAtomPotentialTetrahedralCenter(const Atom *atom) {
PRECONDITION(atom, "atom is null");
if (atom->getTotalDegree() > 4) {
return false;
} else {
const auto &mol = atom->getOwningMol();
auto degree = mol.getAtomDegree(atom);
if (degree == 4) {
// chirality is always possible with 4 nbrs
return true;
} else if (degree == 1) {
// chirality is never possible with 1 nbr
return false;
} else if (degree < 3 &&
(atom->getAtomicNum() != 15 && atom->getAtomicNum() != 33)) {
// less than three neighbors is never stereogenic
// unless it is a phosphine/arsine with implicit H
return false;
} else if (atom->getAtomicNum() == 15 || atom->getAtomicNum() == 33) {
// from logical flow: degree is 2 or 3 (implicit H)
// Since InChI Software v. 1.02-standard (2009), phosphines and arsines
// are always treated as stereogenic even with H atom neighbors.
// Accept automatically.
return true;
} else if (degree == 3) {
// three-coordinate with a single H we'll accept automatically:
if (atom->getTotalNumHs() == 1) {
return true;
} else {
// otherwise we default to not being a legal center
bool legalCenter = false;
// but there are a few special cases we'll accept
// sulfur or selenium with either a positive charge or a double
// bond:
if ((atom->getAtomicNum() == 16 || atom->getAtomicNum() == 34) &&
(atom->getExplicitValence() == 4 ||
(atom->getExplicitValence() == 3 &&
atom->getFormalCharge() == 1))) {
legalCenter = true;
} else if (atom->getAtomicNum() == 7 &&
mol.getRingInfo()->isAtomInRingOfSize(atom->getIdx(), 3)) {
// N in a three-membered ring is another one of the InChI special
// cases
legalCenter = true;
}
return legalCenter;
}
} else {
return false;
}
}
}
bool isAtomPotentialStereoAtom(const Atom *atom) {
return isAtomPotentialTetrahedralCenter(atom);
}
} // namespace detail
std::string getBondSymbol(const Bond *bond) {
// FIX: this is not complete
PRECONDITION(bond, "bad bond");
std::string res;
if (bond->getIsAromatic()) {
res = ":";
} else {
switch (bond->getBondType()) {
case Bond::BondType::SINGLE:
res = "-";
break;
case Bond::BondType::DOUBLE:
res = "=";
break;
case Bond::BondType::TRIPLE:
res = "#";
break;
case Bond::BondType::AROMATIC:
res = ":";
break;
default:
res = "?";
break;
}
}
return res;
}
namespace {
std::string getAtomCompareSymbol(const Atom &atom) {
auto fmt = boost::format("%d%s") % atom.getIsotope() % atom.getSymbol();
return fmt.str();
}
} // namespace
std::vector<StereoInfo> findPotentialStereo(ROMol &mol, bool cleanIt,
bool flagPossible) {
std::map<int, Atom::ChiralType> ochiralTypes;
if (!mol.getRingInfo()->isInitialized()) {
MolOps::symmetrizeSSSR(mol);
}
boost::dynamic_bitset<> knownAtoms(mol.getNumAtoms());
boost::dynamic_bitset<> possibleAtoms(mol.getNumAtoms());
std::vector<std::string> atomSymbols(mol.getNumAtoms());
for (const auto atom : mol.atoms()) {
auto aidx = atom->getIdx();
if (detail::isAtomPotentialStereoAtom(atom)) {
auto sinfo = detail::getStereoInfo(atom);
switch (sinfo.specified) {
case Chirality::StereoSpecified::Unknown:
case Chirality::StereoSpecified::Specified:
knownAtoms.set(aidx);
break;
case Chirality::StereoSpecified::Unspecified:
break;
default:
throw ValueErrorException("bad StereoInfo.specified type");
}
if (flagPossible ||
sinfo.specified != Chirality::StereoSpecified::Unspecified) {
possibleAtoms.set(aidx);
// set "fake stereo"
ochiralTypes[aidx] = atom->getChiralTag();
atom->setChiralTag(Atom::CHI_TETRAHEDRAL_CW);
atomSymbols[aidx] = (boost::format("%d%s_%d") % atom->getIsotope() %
atom->getSymbol() % aidx)
.str();
} else {
atomSymbols[aidx] = getAtomCompareSymbol(*atom);
}
} else {
atomSymbols[aidx] = getAtomCompareSymbol(*atom);
}
}
// flag possible ring stereo cases. The relevant cases here are:
// 1) even-sized rings with possible (or specified) atoms opposite each
// other, like CC1CC(C)C1 or CC1CCC(C)CC1
// 2) atoms sharing a bond which fuses two or more rings, like the central
// bond in C1CCC2CCCCC2C1
// tracks the number of rings with possible ring stereo that the atom is in
// (only set for potential stereoatoms)
std::vector<unsigned int> possibleRingStereoAtoms(mol.getNumAtoms());
// tracks the number of rings with possible ring stereo that the bond is in
// (set for all bonds)
std::vector<unsigned int> possibleRingStereoBonds(mol.getNumBonds());
if (flagPossible) {
boost::dynamic_bitset<> possibleAtomsInRing(mol.getNumAtoms());
for (unsigned int ridx = 0; ridx < mol.getRingInfo()->atomRings().size();
++ridx) {
const auto å = mol.getRingInfo()->atomRings()[ridx];
unsigned int nHere = 0;
auto sz = aring.size();
possibleAtomsInRing.reset();
for (unsigned int ai = 0; ai < aring.size(); ++ai) {
auto aidx = aring[ai];
if (!(aring.size() % 2)) {
// find the index of the atom on the opposite side of the even-sized
// ring
auto oppositeidx = aring[(ai + sz / 2) % sz];
if ((possibleAtoms[aidx] || knownAtoms[aidx]) &&
(possibleAtoms[oppositeidx] || knownAtoms[oppositeidx])) {
++nHere;
possibleAtomsInRing.set(aidx);
continue;
}
}
// if the atom is in more than one bond, see if there's
// a possible neighbor on a fusion bond
if (mol.getRingInfo()->numAtomRings(aidx) > 1) {
auto otheridx = aring[(ai + 1) % aring.size()];
if (possibleAtoms[otheridx] || knownAtoms[otheridx]) {
auto bnd = mol.getBondBetweenAtoms(aidx, otheridx);
CHECK_INVARIANT(bnd, "expected ring bond not found");
if (mol.getRingInfo()->numBondRings(bnd->getIdx()) > 1) {
nHere += 2;
possibleAtomsInRing.set(aidx);
possibleAtomsInRing.set(otheridx);
}
}
}
}
// if the ring contains at least two atoms with possible stereo,
// then each of those possibleAtoms should be included for ring stereo
if (nHere > 1) {
for (auto aidx : aring) {
if (possibleAtomsInRing[aidx]) {
++possibleRingStereoAtoms[aidx];
}
}
for (auto bidx : mol.getRingInfo()->bondRings()[ridx]) {
++possibleRingStereoBonds[bidx];
}
}
}
}
std::vector<std::string> bondSymbols(mol.getNumBonds());
boost::dynamic_bitset<> knownBonds(mol.getNumBonds());
boost::dynamic_bitset<> possibleBonds(mol.getNumBonds());
for (const auto bond : mol.bonds()) {
auto bidx = bond->getIdx();
if (detail::isBondPotentialStereoBond(bond)) {
auto sinfo = detail::getStereoInfo(bond);
switch (sinfo.specified) {
case Chirality::StereoSpecified::Unknown:
case Chirality::StereoSpecified::Specified:
knownBonds.set(bidx);
break;
case Chirality::StereoSpecified::Unspecified:
break;
default:
throw ValueErrorException("bad StereoInfo.specified type");
}
if (flagPossible ||
sinfo.specified != Chirality::StereoSpecified::Unspecified) {
possibleBonds.set(bidx);
bondSymbols[bidx] =
(boost::format("%s-%d") % getBondSymbol(bond) % bond->getIdx())
.str();
} else {
bondSymbols[bidx] = getBondSymbol(bond);
}
} else {
bondSymbols[bidx] = getBondSymbol(bond);
}
}
std::vector<StereoInfo> res;
while (possibleAtoms.count() || possibleBonds.count()) {
res.clear();
bool removedStereo = false;
// we will use the canonicalization code, pretending that each potential
// stereo atom and bond is specified and different from all others. After
// we've done that we can re-examine the potential stereo atoms and bonds
// and remove any where two controlling atoms have the same rank
boost::dynamic_bitset<> atomsInPlay(mol.getNumAtoms());
atomsInPlay.set();
boost::dynamic_bitset<> bondsInPlay(mol.getNumBonds());
bondsInPlay.set();
std::vector<unsigned int> aranks;
const bool breakTies = false;
const bool includeChirality = false;
const bool includeIsotopes = false;
Canon::rankFragmentAtoms(mol, aranks, atomsInPlay, bondsInPlay,
&atomSymbols, &bondSymbols, breakTies,
includeChirality, includeIsotopes);
for (const auto atom : mol.atoms()) {
auto aidx = atom->getIdx();
if (ochiralTypes.find(aidx) != ochiralTypes.end()) {
atom->setChiralTag(ochiralTypes[aidx]);
}
if (possibleAtoms[aidx]) {
auto sinfo = detail::getStereoInfo(atom);
std::vector<unsigned int> nbrs;
nbrs.reserve(sinfo.controllingAtoms.size());
bool haveADupe = false;
for (auto nbrIdx : sinfo.controllingAtoms) {
auto rnk = aranks[nbrIdx];
if (std::find(nbrs.begin(), nbrs.end(), rnk) != nbrs.end()) {
// ok, we just hit a duplicate rank. If the atom we're concerned
// about is a candidate for ring stereo and the bond to the atom
// with the duplicate rank is a ring bond that's not fused between
// rings, we can ignore the duplicate
if (possibleRingStereoAtoms[aidx]) {
auto bnd = mol.getBondBetweenAtoms(aidx, nbrIdx);
if (!bnd || !possibleRingStereoBonds[bnd->getIdx()] ||
possibleRingStereoBonds[bnd->getIdx()] > 1) {
haveADupe = true;
break;
}
} else {
haveADupe = true;
break;
}
} else {
nbrs.push_back(rnk);
}
}
if (!haveADupe) {
res.push_back(std::move(sinfo));
} else {
removedStereo = true;
atomSymbols[aidx] = getAtomCompareSymbol(*atom);
possibleAtoms[aidx] = 0;
if (cleanIt &&
sinfo.specified != Chirality::StereoSpecified::Unspecified) {
atom->setChiralTag(Atom::ChiralType::CHI_UNSPECIFIED);
}
// if this was creating possible ring stereo, update that info now
if (possibleRingStereoAtoms[aidx]) {
--possibleRingStereoAtoms[aidx];
if (!possibleRingStereoAtoms[aidx]) {
// we're no longer in any ring with possible ring stereo. Go
// update all the other atoms/bonds in rings that we're in:
for (unsigned int ridx = 0;
ridx < mol.getRingInfo()->atomRings().size(); ++ridx) {
const auto å = mol.getRingInfo()->atomRings()[ridx];
unsigned int nHere = 0;
for (auto raidx : aring) {
if (possibleRingStereoAtoms[raidx]) {
--possibleRingStereoAtoms[raidx];
if (possibleRingStereoAtoms[raidx]) {
++nHere;
}
}
}
if (nHere <= 1) {
// update the bondstereo counts too
for (auto rbidx : mol.getRingInfo()->bondRings()[ridx]) {
if (possibleRingStereoBonds[rbidx]) {
--possibleRingStereoBonds[rbidx];
}
}
}
}
}
}
}
}
}
for (const auto bond : mol.bonds()) {
auto bidx = bond->getIdx();
if (possibleBonds[bidx]) {
auto sinfo = detail::getStereoInfo(bond);
ASSERT_INVARIANT(sinfo.controllingAtoms.size() == 4,
"bad controlling atoms size");
bool haveADupe = false;
if (sinfo.controllingAtoms[0] != Chirality::StereoInfo::NOATOM &&
sinfo.controllingAtoms[1] != Chirality::StereoInfo::NOATOM &&
aranks[sinfo.controllingAtoms[0]] ==
aranks[sinfo.controllingAtoms[1]]) {
haveADupe = true;
}
if (sinfo.controllingAtoms[2] != Chirality::StereoInfo::NOATOM &&
sinfo.controllingAtoms[3] != Chirality::StereoInfo::NOATOM &&
aranks[sinfo.controllingAtoms[2]] ==
aranks[sinfo.controllingAtoms[3]]) {
haveADupe = true;
}
if (!haveADupe) {
res.push_back(std::move(sinfo));
} else {
removedStereo = true;
bondSymbols[bidx] = getBondSymbol(bond);
possibleBonds[bidx] = 0;
if (cleanIt &&
sinfo.specified != Chirality::StereoSpecified::Unspecified) {
bond->setStereo(Bond::BondStereo::STEREONONE);
}
}
}
}
if (!removedStereo) {
break;
}
}
return res;
} // namespace Chirality
// const_casts are always ugly, but we know that findPotentialStereo() doesn't
// modify the molecule if cleanIt is false:
std::vector<StereoInfo> findPotentialStereo(const ROMol &mol) {
bool cleanIt = false;
return findPotentialStereo(const_cast<ROMol &>(mol), cleanIt);
}
} // namespace Chirality
} // namespace RDKit
|