RDKit: RGroupMatch.h Source File

| RDKit Open-source cheminformatics and machine learning.

Loading...
Searching...
No Matches
RGroupMatch.h
Go to the documentation of this file.
1//
2// Copyright (C) 2017 Novartis Institutes for BioMedical Research
3//
4// @@ All Rights Reserved @@
5// This file is part of the RDKit.
6// The contents are covered by the terms of the BSD license
7// which is included in the file license.txt, found at the root
8// of the RDKit source tree.
9//
10#ifndef RGROUP_MATCH_DATA
11#define RGROUP_MATCH_DATA
12#include "RGroupData.h"
13
14namespace RDKit {
15 typedef boost::shared_ptr<RGroupData> RData;
16 typedef std::map<int, RData> R_DECOMP;
17
18//! RGroupMatch is the decomposition for a single molecule
19 struct RGroupMatch {
20 public:
21 size_t core_idx; // index of the matching core
22 size_t numberMissingUserRGroups;
23 R_DECOMP rgroups; // rlabel->RGroupData mapping
24 RWMOL_SPTR matchedCore; // Core with dummy or query atoms and bonds matched
25
26 RGroupMatch(size_t core_index, size_t numberMissingUserRGroups,
27 R_DECOMP input_rgroups, RWMOL_SPTR matchedCore)
28 : core_idx(core_index),
29 numberMissingUserRGroups(numberMissingUserRGroups),
30 rgroups(std::move(input_rgroups)),
31 matchedCore(std::move(matchedCore)) {}
32
33 std::string toString() const {
34 auto rGroupsString = std::accumulate(
35 rgroups.cbegin(), rgroups.cend(), std::string(),
36 [](std::string s, const std::pair<int, RData> &rgroup) {
37 return std::move(s) + "\n\t(" + std::to_string(rgroup.first) + ':' +
38 rgroup.second->toString() + ')';
39 });
40 std::stringstream ss;
41 ss << "Match coreIdx " << core_idx << " missing count "
42 << numberMissingUserRGroups << " " << rGroupsString;
43 return ss.str();
44 }
45
46 //! Set the target molecule to be used for highlighting R groups
47 //! \param targetMol the target molecule
48 void setTargetMoleculeForHighlights(const RWMOL_SPTR &targetMol) {
49 targetMolForHighlights = targetMol;
50 targetMolWasTrimmed = false;
51 }
52
53 //! Get the target molecule to be used for highlighting R groups
54 //! \param trimHs whether explicit hydrogens should be removed,
55 //! except for those corresponding to R groups (if any)
56 //! \return the target molecule (can be null if it was never set)
57 RWMOL_SPTR getTargetMoleculeForHighlights(bool trimHs) {
58 if (!targetMolForHighlights || !trimHs || targetMolWasTrimmed) {
59 return targetMolForHighlights;
60 }
61 // if trimHs is true and this has not been done before, we need
62 // to remove explicit Hs, except for those corresponding to R groups
63 // (if any). Removal of hydrogens will change atom and bond indices,
64 // therefore common_properties::_rgroupTargetAtoms and
65 // common_properties::_rgroupTargetBonds need to be updated
66 int numMolAtoms = targetMolForHighlights->getNumAtoms();
67 std::vector<int> storedAtomMapNums(numMolAtoms);
68 std::vector<std::pair<int, int>> oldBondEnds(
69 targetMolForHighlights->getNumBonds(), std::make_pair(-1, -1));
70 auto atoms = targetMolForHighlights->atoms();
71 // we use atom map numbers to track original atom indices
72 // ahead of removing Hs, so we store existing values to be able
73 // to restore them afterwards
74 std::transform(atoms.begin(), atoms.end(), storedAtomMapNums.begin(),
75 [](auto atom) {
76 auto res = atom->getAtomMapNum();
77 atom->setAtomMapNum(0);
78 return res;
79 });
80 for (const auto &pair : rgroups) {
81 auto &combinedMol = pair.second->combinedMol;
82 std::vector<int> bondIndices;
83 if (combinedMol->getPropIfPresent(common_properties::_rgroupTargetBonds,
84 bondIndices)) {
85 std::for_each(bondIndices.begin(), bondIndices.end(),
86 [this, &oldBondEnds](const auto &bondIdx) {
87 const auto bond =
88 targetMolForHighlights->getBondWithIdx(bondIdx);
89 const auto beginAtom = bond->getBeginAtom();
90 const auto endAtom = bond->getEndAtom();
91 oldBondEnds[bondIdx].first = beginAtom->getIdx();
92 oldBondEnds[bondIdx].second = endAtom->getIdx();
93 beginAtom->setAtomMapNum(beginAtom->getIdx() + 1);
94 endAtom->setAtomMapNum(endAtom->getIdx() + 1);
95 });
96 }
97 }
98 // remove Hs except those involved in R groups
99 std::vector<int> oldToNewAtomIndices(numMolAtoms, -1);
100 MolOps::RemoveHsParameters rhps;
101 rhps.removeMapped = false;
102 MolOps::removeHs(*targetMolForHighlights, rhps);
103 for (auto atom : targetMolForHighlights->atoms()) {
104 auto atomMapNum = atom->getAtomMapNum();
105 if (atomMapNum) {
106 --atomMapNum;
107 oldToNewAtomIndices[atomMapNum] = atom->getIdx();
108 atom->setAtomMapNum(storedAtomMapNums.at(atomMapNum));
109 }
110 }
111 // update atom and bond indices after removing Hs
112 for (const auto &pair : rgroups) {
113 auto &combinedMol = pair.second->combinedMol;
114 std::vector<int> atomIndices;
115 if (combinedMol->getPropIfPresent(common_properties::_rgroupTargetAtoms,
116 atomIndices)) {
117 std::transform(
118 atomIndices.begin(), atomIndices.end(), atomIndices.begin(),
119 [&oldToNewAtomIndices](auto &atomIdx) {
120 auto newAtomIdx = oldToNewAtomIndices.at(atomIdx);
121 CHECK_INVARIANT(newAtomIdx != -1, "newAtomIdx must be >=0");
122 return newAtomIdx;
123 });
124 }
125 combinedMol->setProp(common_properties::_rgroupTargetAtoms, atomIndices);
126 std::vector<int> bondIndices;
127 if (combinedMol->getPropIfPresent(common_properties::_rgroupTargetBonds,
128 bondIndices)) {
129 std::transform(
130 bondIndices.begin(), bondIndices.end(), bondIndices.begin(),
131 [this, &oldBondEnds, &oldToNewAtomIndices](auto &bondIdx) {
132 const auto &oldPair = oldBondEnds.at(bondIdx);
133 CHECK_INVARIANT(oldPair.first != -1 && oldPair.second != -1,
134 "oldPair members must be >=0");
135 const auto newBeginAtomIdx =
136 oldToNewAtomIndices.at(oldPair.first);
137 const auto newEndAtomIdx = oldToNewAtomIndices.at(oldPair.second);
138 CHECK_INVARIANT(newBeginAtomIdx != -1 && newEndAtomIdx != -1,
139 "newBeginAtomIdx and newEndAtomIdx must be >=0");
140 const auto bond = targetMolForHighlights->getBondBetweenAtoms(
141 newBeginAtomIdx, newEndAtomIdx);
142 CHECK_INVARIANT(bond, "bond must not be null");
143 return bond->getIdx();
144 });
145 }
146 combinedMol->setProp(common_properties::_rgroupTargetBonds, bondIndices);
147 }
148 targetMolWasTrimmed = true;
149 return targetMolForHighlights;
150 }
151
152 private:
153 bool targetMolWasTrimmed = false;
154 RWMOL_SPTR targetMolForHighlights;
155};
156
157} // namespace RDKit
158#endif
RGroupData.h
RDKit::MolOps::removeHs
RDKIT_GRAPHMOL_EXPORT ROMol * removeHs(const ROMol &mol, bool implicitOnly, bool updateExplicitCount=false, bool sanitize=true)
returns a copy of a molecule with hydrogens removed
RDKit::common_properties::_rgroupTargetAtoms
RDKIT_RDGENERAL_EXPORT const std::string _rgroupTargetAtoms
RDKit::common_properties::_rgroupTargetBonds
RDKIT_RDGENERAL_EXPORT const std::string _rgroupTargetBonds
RDKit
Std stuff.
Definition Abbreviations.h:19
RDKit::RData
boost::shared_ptr< RGroupData > RData
Definition RGroupMatch.h:15
RDKit::R_DECOMP
std::map< int, RData > R_DECOMP
Definition RGroupMatch.h:16
RDKit::RWMOL_SPTR
boost::shared_ptr< RWMol > RWMOL_SPTR
Definition RWMol.h:222
RDKit::MolOps::RemoveHsParameters
Definition MolOps.h:302
RDKit::MolOps::RemoveHsParameters::removeMapped
bool removeMapped
Definition MolOps.h:318
RDKit::RGroupMatch::rgroups
R_DECOMP rgroups
Definition RGroupMatch.h:23
RDKit::RGroupMatch::numberMissingUserRGroups
size_t numberMissingUserRGroups
Definition RGroupMatch.h:22
RDKit::RGroupMatch::matchedCore
RWMOL_SPTR matchedCore
Definition RGroupMatch.h:24
RDKit::RGroupMatch::core_idx
size_t core_idx
Definition RGroupMatch.h:21
RDKit::RGroupMatch::toString
std::string toString() const
Definition RGroupMatch.h:33
RDKit::RGroupMatch::setTargetMoleculeForHighlights
void setTargetMoleculeForHighlights(const RWMOL_SPTR &targetMol)
Definition RGroupMatch.h:48
RDKit::RGroupMatch::RGroupMatch
RGroupMatch(size_t core_index, size_t numberMissingUserRGroups, R_DECOMP input_rgroups, RWMOL_SPTR matchedCore)
Definition RGroupMatch.h:26
RDKit::RGroupMatch::getTargetMoleculeForHighlights
RWMOL_SPTR getTargetMoleculeForHighlights(bool trimHs)
Definition RGroupMatch.h:57