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
|
//
//
// Copyright (C) 2018-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 "SubstanceGroup.h"
#include "ROMol.h"
#include "RWMol.h"
#include <boost/dynamic_bitset.hpp>
namespace RDKit {
namespace {
template <class T>
void remove_element(std::vector<T> &container, unsigned int element) {
auto pos = std::find(container.begin(), container.end(), element);
if (pos != container.end()) {
container.erase(pos);
}
}
} // namespace
SubstanceGroup::SubstanceGroup(ROMol *owning_mol, const std::string &type)
: RDProps(), dp_mol(owning_mol) {
PRECONDITION(owning_mol, "supplied owning molecule is bad");
// TYPE is required to be set , as other properties will depend on it.
setProp<std::string>("TYPE", type);
}
void SubstanceGroup::setOwningMol(ROMol *mol) {
PRECONDITION(mol, "owning molecule is nullptr");
dp_mol = mol;
}
unsigned int SubstanceGroup::getIndexInMol() const {
PRECONDITION(dp_mol, "SubstanceGroup is not owned by any molecule");
const auto &sgroups = getSubstanceGroups(*dp_mol);
CHECK_INVARIANT(!sgroups.empty(),
"No SubstanceGroups found on owning molecule");
auto match_sgroup = [&](const SubstanceGroup &sg) { return this == &sg; };
auto sgroupItr = std::find_if(sgroups.begin(), sgroups.end(), match_sgroup);
if (sgroupItr == sgroups.end()) {
std::ostringstream errout;
errout << "Unable to find own index in owning mol SubstanceGroup collection"
<< std::endl;
throw SubstanceGroupException(errout.str());
}
return sgroupItr - sgroups.begin();
}
void SubstanceGroup::addAtomWithIdx(unsigned int idx) {
PRECONDITION(dp_mol, "bad mol");
PRECONDITION(dp_mol->getAtomWithIdx(idx), "wrong atom index");
d_atoms.push_back(idx);
}
void SubstanceGroup::addAtomWithBookmark(int mark) {
PRECONDITION(dp_mol, "bad mol");
Atom *atom = dp_mol->getUniqueAtomWithBookmark(mark);
PRECONDITION(atom, "atom not found");
d_atoms.push_back(atom->getIdx());
}
void SubstanceGroup::addParentAtomWithIdx(unsigned int idx) {
PRECONDITION(dp_mol, "bad mol");
if (std::find(d_atoms.begin(), d_atoms.end(), idx) == d_atoms.end()) {
std::ostringstream errout;
errout << "Atom " << idx << " is not a member of current SubstanceGroup";
throw SubstanceGroupException(errout.str());
}
d_patoms.push_back(idx);
}
void SubstanceGroup::addParentAtomWithBookmark(int mark) {
PRECONDITION(dp_mol, "bad mol");
Atom *atom = dp_mol->getUniqueAtomWithBookmark(mark);
unsigned int idx = atom->getIdx();
if (std::find(d_atoms.begin(), d_atoms.end(), idx) == d_atoms.end()) {
std::ostringstream errout;
errout << "Atom with bookmark " << mark
<< " is not a member of current SubstanceGroup ";
throw SubstanceGroupException(errout.str());
}
d_patoms.push_back(idx);
}
void SubstanceGroup::addBondWithIdx(unsigned int idx) {
PRECONDITION(dp_mol, "bad mol");
PRECONDITION(dp_mol->getBondWithIdx(idx), "wrong bond index");
d_bonds.push_back(idx);
}
void SubstanceGroup::addBondWithBookmark(int mark) {
PRECONDITION(dp_mol, "bad mol");
Bond *bond = dp_mol->getUniqueBondWithBookmark(mark);
d_bonds.push_back(bond->getIdx());
}
void SubstanceGroup::removeAtomWithIdx(unsigned int idx) {
PRECONDITION(dp_mol, "bad mol");
remove_element(d_atoms, idx);
}
void SubstanceGroup::removeParentAtomWithIdx(unsigned int idx) {
PRECONDITION(dp_mol, "bad mol");
remove_element(d_patoms, idx);
}
void SubstanceGroup::removeBondWithIdx(unsigned int idx) {
PRECONDITION(dp_mol, "bad mol");
remove_element(d_bonds, idx);
}
void SubstanceGroup::addBracket(const SubstanceGroup::Bracket &bracket) {
d_brackets.push_back(bracket);
}
void SubstanceGroup::addCState(unsigned int bondIdx,
const RDGeom::Point3D &vector) {
PRECONDITION(dp_mol, "bad mol");
PRECONDITION(!d_bonds.empty(), "no bonds");
if (getBondType(bondIdx) != SubstanceGroup::BondType::XBOND) {
std::ostringstream errout;
errout << "Bond with index " << bondIdx
<< " is not an XBOND for current SubstanceGroup";
throw SubstanceGroupException(errout.str());
}
d_cstates.push_back({bondIdx, vector});
}
void SubstanceGroup::addAttachPoint(unsigned int aIdx, int lvIdx,
const std::string &idStr) {
d_saps.push_back({aIdx, lvIdx, idStr});
}
//! check if the bond is SubstanceGroup XBOND or CBOND
SubstanceGroup::BondType SubstanceGroup::getBondType(
unsigned int bondIdx) const {
PRECONDITION(
std::find(d_bonds.begin(), d_bonds.end(), bondIdx) != d_bonds.end(),
"bond is not part of the SubstanceGroup")
auto bond = dp_mol->getBondWithIdx(bondIdx);
bool begin_atom_in_sgroup =
std::find(d_atoms.begin(), d_atoms.end(), bond->getBeginAtomIdx()) !=
d_atoms.end();
bool end_atom_in_sgroup = std::find(d_atoms.begin(), d_atoms.end(),
bond->getEndAtomIdx()) != d_atoms.end();
if (begin_atom_in_sgroup && end_atom_in_sgroup) {
return SubstanceGroup::BondType::CBOND;
} else if (begin_atom_in_sgroup || end_atom_in_sgroup) {
return SubstanceGroup::BondType::XBOND;
} else {
std::ostringstream errout;
errout << "Neither beginning nor ending atoms of bond " << bond->getIdx()
<< " is in this SubstanceGroup.";
throw SubstanceGroupException(errout.str());
}
}
bool SubstanceGroup::adjustToRemovedAtom(unsigned int atomIdx) {
bool res = false;
for (auto &aid : d_atoms) {
if (aid == atomIdx) {
throw SubstanceGroupException(
"adjustToRemovedAtom() called on SubstanceGroup which contains the "
"atom");
}
if (aid > atomIdx) {
res = true;
--aid;
}
}
for (auto &aid : d_patoms) {
if (aid == atomIdx) {
throw SubstanceGroupException(
"adjustToRemovedAtom() called on SubstanceGroup which contains the "
"atom");
}
if (aid > atomIdx) {
res = true;
--aid;
}
}
for (auto &ap : d_saps) {
if (ap.aIdx == atomIdx || ap.lvIdx == rdcast<int>(atomIdx)) {
throw SubstanceGroupException(
"adjustToRemovedAtom() called on SubstanceGroup which contains the "
"atom");
}
if (ap.aIdx > atomIdx) {
res = true;
--ap.aIdx;
}
if (ap.lvIdx > rdcast<int>(atomIdx)) {
res = true;
--ap.lvIdx;
}
}
return res;
}
bool SubstanceGroup::adjustToRemovedBond(unsigned int bondIdx) {
bool res = false;
for (auto &bid : d_bonds) {
if (bid == bondIdx) {
throw SubstanceGroupException(
"adjustToRemovedBond() called on SubstanceGroup which contains the "
"bond");
}
if (bid > bondIdx) {
res = true;
--bid;
}
}
for (auto &cs : d_cstates) {
if (cs.bondIdx == bondIdx) {
throw SubstanceGroupException(
"adjustToRemovedBond() called on SubstanceGroup which contains the "
"bond");
}
if (cs.bondIdx > bondIdx) {
res = true;
--cs.bondIdx;
}
}
return res;
}
bool SubstanceGroup::includesAtom(unsigned int atomIdx) const {
if (std::find(d_atoms.begin(), d_atoms.end(), atomIdx) != d_atoms.end()) {
return true;
}
if (std::find(d_patoms.begin(), d_patoms.end(), atomIdx) != d_patoms.end()) {
return true;
}
for (const auto &ap : d_saps) {
if (ap.aIdx == atomIdx || ap.lvIdx == rdcast<int>(atomIdx)) {
return true;
}
}
return false;
}
bool SubstanceGroup::includesBond(unsigned int bondIdx) const {
if (std::find(d_bonds.begin(), d_bonds.end(), bondIdx) != d_bonds.end()) {
return true;
}
for (const auto &cs : d_cstates) {
if (cs.bondIdx == bondIdx) {
return true;
}
}
return false;
}
bool SubstanceGroupChecks::isValidType(const std::string &type) {
return std::find(SubstanceGroupChecks::sGroupTypes.begin(),
SubstanceGroupChecks::sGroupTypes.end(),
type) != SubstanceGroupChecks::sGroupTypes.end();
}
bool SubstanceGroupChecks::isValidSubType(const std::string &type) {
return std::find(SubstanceGroupChecks::sGroupSubtypes.begin(),
SubstanceGroupChecks::sGroupSubtypes.end(),
type) != SubstanceGroupChecks::sGroupSubtypes.end();
}
bool SubstanceGroupChecks::isValidConnectType(const std::string &type) {
return std::find(SubstanceGroupChecks::sGroupConnectTypes.begin(),
SubstanceGroupChecks::sGroupConnectTypes.end(),
type) != SubstanceGroupChecks::sGroupConnectTypes.end();
}
bool SubstanceGroupChecks::isSubstanceGroupIdFree(const ROMol &mol,
unsigned int id) {
auto match_sgroup = [id](const SubstanceGroup &sg) {
unsigned int storedId;
return sg.getPropIfPresent("ID", storedId) && id == storedId;
};
const auto &sgroups = getSubstanceGroups(mol);
return std::find_if(sgroups.begin(), sgroups.end(), match_sgroup) ==
sgroups.end();
}
std::vector<SubstanceGroup> &getSubstanceGroups(ROMol &mol) {
return mol.d_sgroups;
}
const std::vector<SubstanceGroup> &getSubstanceGroups(const ROMol &mol) {
return mol.d_sgroups;
}
unsigned int addSubstanceGroup(ROMol &mol, SubstanceGroup sgroup) {
sgroup.setOwningMol(&mol);
auto &&sgroups = getSubstanceGroups(mol);
unsigned int id = sgroups.size();
sgroups.push_back(std::move(sgroup));
return id;
}
namespace {
bool includesBond(SubstanceGroup &sg, unsigned int idx) {
return sg.includesBond(idx);
}
bool includesAtom(SubstanceGroup &sg, unsigned int idx) {
return sg.includesAtom(idx);
}
void removedBond(SubstanceGroup &sg, unsigned int idx) {
sg.adjustToRemovedBond(idx);
}
void removedAtom(SubstanceGroup &sg, unsigned int idx) {
sg.adjustToRemovedAtom(idx);
}
bool removedParentInHierarchy(
unsigned int idx, const std::vector<SubstanceGroup> &sgs,
const boost::dynamic_bitset<> &toRemove,
const std::map<unsigned int, unsigned int> &indexLookup) {
PRECONDITION(idx < sgs.size(), "cannot find SubstanceGroup");
if (toRemove[idx]) {
return true;
}
unsigned int parent;
if (sgs[idx].getPropIfPresent("PARENT", parent)) {
auto piter = indexLookup.find(parent);
if (piter != indexLookup.end()) {
return removedParentInHierarchy(piter->second, sgs, toRemove,
indexLookup);
}
}
return false;
}
template <bool INCLUDES_METHOD(SubstanceGroup &, unsigned int),
void ADJUST_METHOD(SubstanceGroup &, unsigned int)>
void removeSubstanceGroupsReferencing(RWMol &mol, unsigned int idx) {
auto &sgs = getSubstanceGroups(mol);
if (!sgs.empty()) {
// first collect the ones that should be removed
boost::dynamic_bitset<> toRemove(sgs.size());
unsigned int nRemoved = 0;
bool parentsPresent = false;
for (unsigned int i = 0; i < sgs.size(); ++i) {
if (!parentsPresent && sgs[i].hasProp("PARENT")) {
parentsPresent = true;
}
if (INCLUDES_METHOD(sgs[i], idx)) {
toRemove.set(i);
++nRemoved;
}
}
// if we're going to be removing anything and there are PARENTS present,
// we need to build a lookup map between index->position in original array
std::map<unsigned int, unsigned int> indexLookup;
if (parentsPresent && nRemoved) {
for (unsigned int i = 0; i < sgs.size(); ++i) {
unsigned int index;
if (sgs[i].getPropIfPresent("index", index)) {
indexLookup[index] = i;
}
}
}
// now go through and keep everything that shouldn't be removed
// and who doesn't have a PARENT that should be removed in their hierarchy
std::vector<SubstanceGroup> newsgs;
newsgs.reserve(sgs.size() - nRemoved);
unsigned int i = 0;
for (auto &&sg : sgs) {
if (!toRemove[i]) {
// we might be keeping it. Check the parent
if (!parentsPresent || !sg.hasProp("PARENT")) {
ADJUST_METHOD(sg, idx);
newsgs.push_back(std::move(sg));
} else if (parentsPresent) {
unsigned int parent;
// has our parent been removed?
if (sg.getPropIfPresent("PARENT", parent)) {
auto piter = indexLookup.find(parent);
bool keepIt = false;
if (piter == indexLookup.end()) {
// our parent isn't around, so it isn't being removed
// note: this is an odd case and probably shouldn't happen, but
// this isn't the place to enforce that
keepIt = true;
} else if (!toRemove[piter->second]) {
// our parent isn't being removed, recursively check up through
// parents to see if we find any that are being removed:
if (!removedParentInHierarchy(piter->second, sgs, toRemove,
indexLookup)) {
keepIt = true;
}
}
if (keepIt) {
ADJUST_METHOD(sg, idx);
newsgs.push_back(std::move(sg));
}
}
}
}
++i;
}
sgs = std::move(newsgs);
}
}
} // namespace
void removeSubstanceGroupsReferencingAtom(RWMol &mol, unsigned int idx) {
// Delete substance groups containing this atom. It could be that it's ok to
// keep it, but we just don't know
removeSubstanceGroupsReferencing<includesAtom, removedAtom>(mol, idx);
}
void removeSubstanceGroupsReferencingBond(RWMol &mol, unsigned int idx) {
// Delete substance groups containing this bond. It could be that it's ok to
// keep it, but we just don't know
removeSubstanceGroupsReferencing<includesBond, removedBond>(mol, idx);
}
} // namespace RDKit
std::ostream &operator<<(std::ostream &target,
const RDKit::SubstanceGroup &sgroup) {
target << sgroup.getIndexInMol() << ' '
<< sgroup.getProp<std::string>("TYPE");
auto brackets = sgroup.getBrackets();
if (!brackets.empty()) {
target << " Brk: " << brackets.size();
}
auto cstates = sgroup.getCStates();
if (!cstates.empty()) {
target << " CSt: " << cstates.size();
}
auto attachpts = sgroup.getAttachPoints();
if (!attachpts.empty()) {
target << " AtPt: " << attachpts.size();
}
auto atoms = sgroup.getAtoms();
if (!atoms.empty()) {
target << " Atoms: { ";
for (auto atom_idx : atoms) {
target << atom_idx << ' ';
}
target << '}';
}
auto patoms = sgroup.getParentAtoms();
if (!patoms.empty()) {
target << " PAtoms: { ";
for (auto atom_idx : patoms) {
target << atom_idx << ' ';
}
target << '}';
}
auto bonds = sgroup.getBonds();
if (!bonds.empty()) {
target << " Bonds: { ";
for (auto bond_idx : bonds) {
target << bond_idx << ' ';
}
target << '}';
}
return target;
}
|