File: Synthon.cpp

package info (click to toggle)
rdkit 202503.6-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 222,000 kB
  • sloc: cpp: 411,111; python: 78,482; ansic: 26,181; java: 8,285; javascript: 4,404; sql: 2,393; yacc: 1,626; lex: 1,267; cs: 1,090; makefile: 581; xml: 229; fortran: 183; sh: 121
file content (212 lines) | stat: -rw-r--r-- 6,935 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
//
// Copyright (C) David Cosgrove 2024.
//
//   @@ 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 <DataStructs/ExplicitBitVect.h>
#include <GraphMol/Chirality.h>
#include <GraphMol/Descriptors/MolDescriptors.h>
#include <GraphMol/MolOps.h>
#include <GraphMol/MolPickler.h>
#include <GraphMol/Descriptors/MolDescriptors.h>
#include <GraphMol/FileParsers/FileWriters.h>
#include <GraphMol/Fingerprints/Fingerprints.h>
#include <GraphMol/SynthonSpaceSearch/Synthon.h>
#include <GraphMol/SynthonSpaceSearch/SynthonSpaceSearch_details.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/SmilesParse/SmartsWrite.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>

namespace RDKit::SynthonSpaceSearch {

Synthon::Synthon(const std::string &smi) : d_smiles(smi) {
  v2::SmilesParse::SmilesParserParams params;
  params.sanitize = false;
  dp_origMol = v2::SmilesParse::MolFromSmiles(d_smiles, params);
  if (!dp_origMol) {
    // This should be rare, as it should be possible to assume that
    // the people who made the SynthonSpace know what they're doing.
    // Therefore, it's probably a corrupted or incorrect file, so
    // bring it all down.
    throw ValueErrorException("Unparsable synthon SMILES " + d_smiles);
  }
  calcProperties();
}

Synthon::Synthon(const Synthon &other)
    : d_smiles(other.d_smiles),
      dp_origMol(std::make_unique<ROMol>(*other.dp_origMol)),
      dp_searchMol(std::make_unique<ROMol>(*other.dp_searchMol)),
      dp_pattFP(std::make_unique<ExplicitBitVect>(*other.dp_pattFP)),
      dp_FP(std::make_unique<ExplicitBitVect>(*other.dp_FP)),
      d_connRegions(other.d_connRegions),
      d_numDummies(other.d_numDummies),
      d_numHeavyAtoms(other.d_numHeavyAtoms),
      d_numChiralAtoms(other.d_numChiralAtoms),
      d_numChiralAtomsExcDummies(other.d_numChiralAtomsExcDummies),
      d_molWt(other.d_molWt) {}

Synthon &Synthon::operator=(const Synthon &other) {
  if (this == &other) {
    return *this;
  }
  d_smiles = other.d_smiles;
  if (other.dp_origMol) {
    dp_origMol = std::make_unique<ROMol>(*other.dp_origMol);
  } else {
    dp_origMol.reset();
  }
  if (other.dp_searchMol) {
    dp_searchMol = std::make_unique<ROMol>(*other.dp_searchMol);
  } else {
    dp_searchMol.reset();
  }
  if (other.dp_pattFP) {
    dp_pattFP = std::make_unique<ExplicitBitVect>(*other.dp_pattFP);
  } else {
    dp_pattFP.reset();
  }
  if (other.dp_FP) {
    dp_FP = std::make_unique<ExplicitBitVect>(*other.dp_FP);
  } else {
    dp_FP.reset();
  }
  if (!other.d_connRegions.empty()) {
    d_connRegions.clear();
    std::transform(
        other.d_connRegions.begin(), other.d_connRegions.end(),
        std::back_inserter(d_connRegions),
        [](const std::shared_ptr<ROMol> &m) -> std::shared_ptr<ROMol> {
          return std::make_shared<ROMol>(*m);
        });
  } else {
    d_connRegions.clear();
  }
  d_numDummies = other.d_numDummies;
  d_numHeavyAtoms = other.d_numHeavyAtoms;
  d_numChiralAtoms = other.d_numChiralAtoms;
  d_numChiralAtomsExcDummies = other.d_numChiralAtomsExcDummies;
  d_molWt = other.d_molWt;
  return *this;
}
const std::unique_ptr<ROMol> &Synthon::getOrigMol() const { return dp_origMol; }
const std::unique_ptr<ROMol> &Synthon::getSearchMol() const {
  return dp_searchMol;
}

const std::unique_ptr<ExplicitBitVect> &Synthon::getPattFP() const {
  return dp_pattFP;
}
const std::unique_ptr<ExplicitBitVect> &Synthon::getFP() const { return dp_FP; }

const std::vector<std::shared_ptr<ROMol>> &Synthon::getConnRegions() const {
  return d_connRegions;
}

void Synthon::setSearchMol(std::unique_ptr<ROMol> mol) {
  dp_searchMol = std::move(mol);
  // There are probably extraneous props on the atoms and bonds
  for (auto &atom : dp_searchMol->atoms()) {
    atom->clearProp("molNum");
    atom->clearProp("idx");
  }
  for (auto &bond : dp_searchMol->bonds()) {
    bond->clearProp("molNum");
    bond->clearProp("idx");
  }
  finishInitialization();
}

void Synthon::setFP(std::unique_ptr<ExplicitBitVect> fp) {
  dp_FP = std::move(fp);
}

void Synthon::writeToDBStream(std::ostream &os) const {
  streamWrite(os, d_smiles);
  MolPickler::pickleMol(*dp_origMol, os, PicklerOps::AllProps);
  MolPickler::pickleMol(*dp_searchMol, os, PicklerOps::AllProps);
  const auto pattFPstr = getPattFP()->toString();
  streamWrite(os, pattFPstr);
  streamWrite(os, static_cast<std::uint64_t>(getConnRegions().size()));
  for (const auto &cr : getConnRegions()) {
    MolPickler::pickleMol(*cr, os, PicklerOps::AllProps);
  }
  if (dp_FP) {
    streamWrite(os, true);
    streamWrite(os, dp_FP->toString());
  } else {
    streamWrite(os, false);
  }
  streamWrite(os, d_numDummies);
  streamWrite(os, d_numHeavyAtoms);
  streamWrite(os, d_numChiralAtoms);
  streamWrite(os, d_molWt);
}

void Synthon::readFromDBStream(std::istream &is, std::uint32_t version) {
  streamRead(is, d_smiles, 0);
  dp_origMol = std::make_unique<ROMol>();
  MolPickler::molFromPickle(is, *dp_origMol);
  dp_searchMol = std::make_unique<ROMol>();
  MolPickler::molFromPickle(is, *dp_searchMol);
  std::string pickle;
  streamRead(is, pickle, 0);
  dp_pattFP = std::make_unique<ExplicitBitVect>(pickle);
  size_t numConnRegs;
  streamRead(is, numConnRegs);
  d_connRegions.resize(numConnRegs);
  for (size_t i = 0; i < numConnRegs; ++i) {
    d_connRegions[i] = std::make_shared<ROMol>();
    MolPickler::molFromPickle(is, *d_connRegions[i]);
  }
  bool haveFP = false;
  streamRead(is, haveFP);
  if (haveFP) {
    std::string pickle;
    streamRead(is, pickle, 0);
    dp_FP = std::make_unique<ExplicitBitVect>(pickle);
  }
  if (version > 3000) {
    streamRead(is, d_numDummies);
    streamRead(is, d_numHeavyAtoms);
    streamRead(is, d_numChiralAtoms);
    streamRead(is, d_molWt);
  } else {
    calcProperties();
  }
}

void Synthon::finishInitialization() {
  dp_pattFP.reset(PatternFingerprintMol(*dp_searchMol, PATT_FP_NUM_BITS));
  d_connRegions.clear();
  if (const auto cr = details::buildConnRegion(*dp_searchMol); cr) {
    std::vector<std::unique_ptr<ROMol>> tmpFrags;
    MolOps::getMolFrags(*cr, tmpFrags, false);
    for (auto &f : tmpFrags) {
      d_connRegions.push_back(std::shared_ptr<ROMol>(f.release()));
    }
  }
}

void Synthon::calcProperties() {
  d_numDummies = 0;
  d_numHeavyAtoms = 0;
  MolOps::assignStereochemistry(*dp_origMol);
  for (const auto atom : dp_origMol->atoms()) {
    if (atom->getAtomicNum() == 0) {
      d_numDummies++;
    } else if (atom->getAtomicNum() > 1) {
      d_numHeavyAtoms++;
    }
  }
  Chirality::findPotentialStereo(*dp_origMol, true, true);
  d_numChiralAtoms =
      details::countChiralAtoms(*dp_origMol, &d_numChiralAtomsExcDummies);
  d_molWt = Descriptors::calcAMW(*dp_origMol);
}
}  // namespace RDKit::SynthonSpaceSearch