File: RGroupUtils.cpp

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (251 lines) | stat: -rw-r--r-- 7,278 bytes parent folder | download | duplicates (2)
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
//
//  Copyright (C) 2017 Novartis Institutes for BioMedical Research
//
//   @@ 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 "RGroupUtils.h"
#include <boost/format.hpp>
#include <GraphMol/SmilesParse/SmilesWrite.h>
#include <GraphMol/SmilesParse/SmartsWrite.h>

namespace RDKit {
std::string labellingToString(Labelling type) {
  switch (type) {
    case Labelling::RGROUP_LABELS:
      return "RGroupLabels";
    case Labelling::ISOTOPE_LABELS:
      return "IsotopeLabels";
    case Labelling::ATOMMAP_LABELS:
      return "AtomMapLabels";
    case Labelling::INDEX_LABELS:
      return "IndexLabels";
    case Labelling::DUMMY_LABELS:
      return "DummyLabels";
    case Labelling::INTERNAL_LABELS:
      return "InternalLabels";
  }
  return "unknown";
}

// Return the current set of rlabels for the molecule
//  Negative RLabels are ones not assigned by the user and are
//   either set to the index of the atom OR set to the index of
//   the atom from the core with the best MCS.
//  Positive rlabels are user defined (i.e. the user has specified
//   specific R1, R2 etc...
std::map<int, Atom *> getRlabels(const RWMol &mol) {
  std::map<int, Atom *> atoms;

  for (auto atom : mol.atoms()) {
    if (atom->hasProp(RLABEL)) {
      int rlabel = atom->getProp<int>(RLABEL);  // user label
      CHECK_INVARIANT(atoms.find(rlabel) == atoms.end(),
                      "Duplicate labels in rgroup core!");
      atoms[rlabel] = atom;
    }
  }
  return atoms;
}

void clearInputLabels(Atom *atom) {
  // atom->setIsotope(0); Don't want to clear deuterium and things like that if
  // they aren't labels
  atom->setAtomMapNum(0);
  if (atom->hasProp(common_properties::_MolFileRLabel)) {
    atom->clearProp(common_properties::_MolFileRLabel);
  }
}

bool setLabel(Atom *atom, int label, std::set<int> &labels, int &maxLabel,
              bool relabel, Labelling type) {
  if (type == Labelling::ISOTOPE_LABELS) {
    atom->setIsotope(0);
  } else if (type == Labelling::ATOMMAP_LABELS) {
    atom->setAtomMapNum(0);
  } else if (type == Labelling::RGROUP_LABELS) {
    if (atom->hasProp(common_properties::_MolFileRLabel)) {
      atom->clearProp(common_properties::_MolFileRLabel);
      atom->setIsotope(0);
    }
  }

  if (label) {
    if (labels.find(label) != labels.end()) {
      if (relabel) {
        if (type == Labelling::INTERNAL_LABELS) {
          BOOST_LOG(rdWarningLog) << "Relabelling existing label" << std::endl;
        }
        label = maxLabel;
      } else {
        // XXX FIX me - get label id
        throw ValueErrorException(
            std::string("Duplicate label in input, current type is:") +
            labellingToString(type));
      }
    }

    atom->setProp<int>(RLABEL, label);
    atom->setProp<int>(RLABEL_TYPE, static_cast<int>(type));
    labels.insert(label);
    maxLabel = (std::max)(maxLabel, label + 1);
    return true;
  }
  return false;
}

bool isUserRLabel(const Atom &atom) {
  return atom.hasProp(RLABEL) && atom.hasProp(RLABEL_TYPE) &&
         static_cast<Labelling>(atom.getProp<int>(RLABEL_TYPE)) !=
             Labelling::INDEX_LABELS;
}

bool isDummyRGroupAttachment(const Atom &atom) {
  if (atom.getAtomicNum() != 0 || atom.getDegree() != 1) {
    return false;
  }
  if (isUserRLabel(atom)) {
    return true;
  }
  bool unlabeled_core_attachment = false;
  if (atom.getPropIfPresent(UNLABELED_CORE_ATTACHMENT,
                            unlabeled_core_attachment) &&
      unlabeled_core_attachment) {
    return true;
  }
  return false;
}

bool isAtomWithMultipleNeighborsOrNotDummyRGroupAttachment(const Atom &atom) {
  if (atom.getDegree() > 1) {
    return true;
  }
  return !isDummyRGroupAttachment(atom);
}

bool hasDummy(const RWMol &core) {
  for (RWMol::ConstAtomIterator atIt = core.beginAtoms();
       atIt != core.endAtoms(); ++atIt) {
    if ((*atIt)->getAtomicNum() == 0) {
      return true;
    }
  }
  return false;
}

namespace {
std::string MolToText(const ROMol &mol) {
  bool hasQuery = false;
  for (const auto atom : mol.atoms()) {
    if (atom->hasQuery() && atom->getQuery()->getDescription() != "AtomNull") {
      hasQuery = true;
      break;
    }
  }
  if (!hasQuery) {
    for (const auto bond : mol.bonds()) {
      if (bond->hasQuery()) {
        hasQuery = true;
        break;
      }
    }
  }
  if (!hasQuery) {
    return MolToSmiles(mol);
  } else {
    return MolToSmarts(mol);
  }
}
}  // namespace

std::string toJSON(const RGroupRow &rgr, const std::string &prefix) {
  std::string res = prefix + "{\n";
  for (const auto &elem : rgr) {
    auto fmt = boost::format{"  \"%1%\":\"%2%\""} % (elem.first) %
               (MolToText(*elem.second));
    res += prefix + fmt.str() + ",\n";
  }
  res.erase(res.end() - 2, res.end());
  res += "\n" + prefix + "}";
  return res;
}

std::string toJSON(const RGroupRows &rows, const std::string &prefix) {
  std::string res = prefix + "[\n";
  auto rowPrefix = prefix + "  ";
  for (const auto &row : rows) {
    res += toJSON(row, rowPrefix) + ",\n";
  }
  res.erase(res.end() - 2, res.end());
  res += "\n" + prefix + "]";
  return res;
}

std::string toJSON(const RGroupColumn &rgr, const std::string &prefix) {
  std::string res = "[\n";
  for (const auto &elem : rgr) {
    auto fmt = boost::format{"  \"%1%\""} % (MolToText(*elem));
    res += prefix + fmt.str() + ",\n";
  }
  res.erase(res.end() - 2, res.end());
  res += "\n" + prefix + "]";
  return res;
}

std::string toJSON(const RGroupColumns &cols, const std::string &prefix) {
  std::string res = prefix + "[\n";
  auto colPrefix = prefix + "  ";
  for (const auto &col : cols) {
    auto fmt = boost::format{"  \"%1%\": %2%"} % (col.first) %
               (toJSON(col.second, colPrefix));
    res += prefix + fmt.str() + ",\n";
  }
  res.erase(res.end() - 2, res.end());
  res += "\n" + prefix + "]";
  return res;
}

void relabelMappedDummies(ROMol &mol, unsigned int inputLabels,
                          unsigned int outputLabels) {
  for (auto &atom : mol.atoms()) {
    if (atom->getAtomicNum()) {
      continue;
    }
    unsigned int atomMapNum = 0;
    if (inputLabels & AtomMap) {
      atomMapNum = static_cast<unsigned int>(abs(atom->getAtomMapNum()));
    }
    if (!atomMapNum && (inputLabels & Isotope)) {
      atomMapNum = atom->getIsotope();
    }
    if (!atomMapNum && (inputLabels & MDLRGroup)) {
      atom->getPropIfPresent(common_properties::_MolFileRLabel, atomMapNum);
    }
    if (!atomMapNum) {
      continue;
    }
    auto rLabel = "R" + std::to_string(atomMapNum);
    if (outputLabels & AtomMap) {
      atom->setAtomMapNum(atomMapNum);
    } else {
      atom->setAtomMapNum(0);
    }
    if (outputLabels & Isotope) {
      atom->setIsotope(atomMapNum);
    } else {
      atom->setIsotope(0);
    }
    if (outputLabels & MDLRGroup) {
      atom->setProp(common_properties::_MolFileRLabel, atomMapNum);
      atom->setProp(common_properties::dummyLabel, rLabel);
    } else {
      atom->clearProp(common_properties::_MolFileRLabel);
      atom->clearProp(common_properties::dummyLabel);
    }
  }
}

}  // namespace RDKit