File: RepeatUnit.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 (464 lines) | stat: -rw-r--r-- 16,183 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
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
//
//  Copyright (C) 2021 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 "MolEnumerator.h"
#include <RDGeneral/Exceptions.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>
#include <GraphMol/SmilesParse/SmartsWrite.h>
#include <GraphMol/ChemReactions/Reaction.h>
#include <GraphMol/ChemReactions/ReactionParser.h>

#include <boost/lexical_cast.hpp>
#include <boost/tokenizer.hpp>
#include <boost/format.hpp>
#include <algorithm>
#include <charconv>

typedef boost::tokenizer<boost::char_separator<char>> tokenizer;

namespace RDKit {
namespace MolEnumerator {

const std::string polymarker = "_polymeratom";
const std::string headmarker = "_headatom";
const std::string headmarker_frame = "_headatom_frame";
const std::string tailmarker = "_tailatom";
const std::string tailmarker_frame = "_tailatom_frame";
const std::string headheadmarker = "_headhead";
const unsigned ladderoffset = 100000;

namespace {
void tagAtoms(std::shared_ptr<ROMol> mol, const Bond *bond,
              const boost::dynamic_bitset<> &sgatoms, unsigned int index,
              const std::string &marker, const std::string &framemarker,
              const std::string &connect) {
  PRECONDITION(mol, "bad molecule");
  PRECONDITION(bond, "bad bond");
  if (sgatoms[bond->getBeginAtomIdx()]) {
    bond->getBeginAtom()->setProp(marker, index);
    if (connect == "HH") {
      bond->getBeginAtom()->setProp(headheadmarker, 1);
    }
    auto frameAtom = mol->getAtomWithIdx(bond->getEndAtomIdx());
    std::vector<unsigned int> vs;
    frameAtom->getPropIfPresent(framemarker, vs);
    vs.push_back(index);
    frameAtom->setProp(framemarker, vs);
  } else if (sgatoms[bond->getEndAtomIdx()]) {
    bond->getEndAtom()->setProp(marker, index);
    if (connect == "HH") {
      bond->getEndAtom()->setProp(headheadmarker, 1);
    }
    auto frameAtom = mol->getAtomWithIdx(bond->getBeginAtomIdx());
    std::vector<unsigned int> vs;
    frameAtom->getPropIfPresent(framemarker, vs);
    vs.push_back(index);
    frameAtom->setProp(framemarker, vs);
  } else {
    throw ValueErrorException("neither atom in an SRU bond is in the polymer");
  }
}
}  // namespace

const size_t RepeatUnitOp::DEFAULT_REPEAT_COUNT = 4;

void RepeatUnitOp::initFromMol(const ROMol &mol) {
  dp_mol.reset(new ROMol(mol));
  initFromMol();
}

// if the SRU label has information about the repetition range, we should use
// that information for the enumerator. There are these cases:
//      * if label is a number, N, assume desired range is 0-N
//      * if label is M-N, where M and N are non-negative integers
//        and M <= N, we'll assume the desired range is {M, N}.
//      * well assume the desired range is {0,
//      RepeatUnitOp::DEFAULT_REPEAT_COUNT}
//        for anything else
[[nodiscard]] static std::pair<size_t, size_t> parse_repeat_counts(
    const std::string &sru_label) {
  static constexpr std::pair<size_t, size_t> default_repeat_counts{
      0, RepeatUnitOp::DEFAULT_REPEAT_COUNT};

  if (sru_label.empty()) {
    return default_repeat_counts;
  }

  auto end = sru_label.data() + sru_label.size();

  size_t min_repeats = 0;
  auto status = std::from_chars(sru_label.data(), end, min_repeats);

  // if label is something like 'N', max count should be N
  if (status.ec == std::errc() && status.ptr == end) {
    // offset by one since range is inclusive
    return std::make_pair(0u, min_repeats + 1);
  }

  // if we failed to parse the first number for some reason
  if (status.ec != std::errc() || (status.ptr != end && status.ptr[0] != '-')) {
    return default_repeat_counts;
  }

  size_t max_repeats = 0;
  status = std::from_chars(status.ptr + 1, end, max_repeats);
  if (status.ec != std::errc() || status.ptr != end) {
    return default_repeat_counts;
  }

  // assume it's nonsense
  if (max_repeats < min_repeats) {
    return default_repeat_counts;
  }

  // offset by one since range is inclusive
  return std::make_pair(min_repeats, max_repeats + 1);
}

void RepeatUnitOp::initFromMol() {
  // we're making an assumption here that each atom has at most one bond to an
  // atom not in the repeat unit
  if (!dp_mol) {
    return;
  }

  d_repeats.clear();
  d_countAtEachPoint.clear();

  // start by figuring out which atoms are in sgroups which will be enumerated
  boost::dynamic_bitset<> atomsInSRUs(dp_mol->getNumAtoms());
  std::vector<boost::dynamic_bitset<>> atomsPerSRU;
  std::vector<const SubstanceGroup *> enumerated_SGroups;
  for (auto &sg : getSubstanceGroups(*dp_mol)) {
    std::string typ;
    if (!sg.getPropIfPresent("TYPE", typ) || typ != "SRU") {
      continue;
    }

    std::string connect;
    sg.getPropIfPresent("CONNECT", connect);
    if (!connect.empty()) {
      if (connect != "HT" && connect != "HH") {
        BOOST_LOG(rdWarningLog)
            << "can only enumerate SRUs with CONNECT=HT or CONNECT=HH"
            << std::endl;
        continue;
      }
    }

    std::string label;
    sg.getPropIfPresent("LABEL", label);
    auto [min_repeats, max_repeats] = parse_repeat_counts(label);
    d_minRepeatCounts.push_back(min_repeats);
    d_countAtEachPoint.push_back(
        std::min(max_repeats - min_repeats, d_maxNumRounds));

    // tag the atoms in the repeat unit:
    boost::dynamic_bitset<> sgatoms(dp_mol->getNumAtoms());
    for (auto aidx : sg.getAtoms()) {
      if (atomsInSRUs[aidx]) {
        throw ValueErrorException("cannot enumerate overlapping SRU groups");
      }
      sgatoms.set(aidx);
      atomsInSRUs.set(aidx);
    }
    atomsPerSRU.push_back(sgatoms);

    auto sgIdx = sg.getProp<unsigned>("index");

    // tag the head and tail atoms
    const auto &bnds = sg.getBonds();
    if (bnds.size() == 2) {
      // simple case with only two bonds: here we just have head and tail
      tagAtoms(dp_mol, dp_mol->getBondWithIdx(bnds[0]), sgatoms, sgIdx,
               headmarker, tailmarker_frame, connect);
      tagAtoms(dp_mol, dp_mol->getBondWithIdx(bnds[1]), sgatoms, sgIdx,
               tailmarker, headmarker_frame, connect);
    } else if (bnds.size() == 4) {
      // four bonds are what we see for a ladder polymer, here we need two
      // different heads and two different tails. We mark the second set with
      // a large offset so that they don't get confused, but otherwise the
      // rest of the code handles this automatically.

      // NOTE: theoretically we could support larger numbers of head/tail
      // pairs, but I don't believe these show up in reality

      // We may have XBCORR to indicate which bonds correspond to which
      std::vector<unsigned int> xbcorr;
      sg.getPropIfPresent("XBCORR", xbcorr);
      // if it's not there, or if it wasn't the right size, just use
      // the bonds:
      if (xbcorr.size() != 4) {
        xbcorr = {bnds[0], bnds[2], bnds[1], bnds[3]};
      }
      tagAtoms(dp_mol, dp_mol->getBondWithIdx(xbcorr[0]), sgatoms, sgIdx,
               headmarker, tailmarker_frame, connect);
      tagAtoms(dp_mol, dp_mol->getBondWithIdx(xbcorr[2]), sgatoms,
               sgIdx + ladderoffset, headmarker, tailmarker_frame, connect);
      tagAtoms(dp_mol, dp_mol->getBondWithIdx(xbcorr[1]), sgatoms, sgIdx,
               tailmarker, headmarker_frame, connect);
      tagAtoms(dp_mol, dp_mol->getBondWithIdx(xbcorr[3]), sgatoms,
               sgIdx + ladderoffset, tailmarker, headmarker_frame, connect);
    } else {
      throw ValueErrorException("can only handle SRUs with two or four bonds");
    }

    enumerated_SGroups.push_back(&sg);
  }

  if (!enumerated_SGroups.empty() &&
      dp_mol->hasProp(common_properties::molFileLinkNodes)) {
    throw ValueErrorException(
        "cannot enumerate molecules which include both SRUs and LINKNODEs");
  }

  // copy the molecule over as the frame. We'll remove atoms in SRUs from this
  // below
  dp_frame.reset(new RWMol(*dp_mol));
  dp_frame->beginBatchEdit();

  // now set up the repeat units for each of the SRUs
  for (const auto *sgp : enumerated_SGroups) {
    const auto &sg = *sgp;
    std::shared_ptr<RWMol> repeat(new RWMol(*dp_mol));

    // remove the atoms in the repeat unit from the frame:
    boost::dynamic_bitset<> sgatoms(dp_mol->getNumAtoms());
    for (auto aidx : sg.getAtoms()) {
      sgatoms.set(aidx);
      // remove it from the frame
      dp_frame->removeAtom(aidx);
    }

    repeat->beginBatchEdit();
    for (auto aidx = 0u; aidx < repeat->getNumAtoms(); ++aidx) {
      if (!sgatoms[aidx]) {
        repeat->removeAtom(aidx);
      } else {
        repeat->getAtomWithIdx(aidx)->setProp(polymarker, 1);
      }
    }
    repeat->commitBatchEdit();
    d_repeats.push_back(repeat);
  }
  dp_frame->commitBatchEdit();

}  // namespace MolEnumerator

std::vector<size_t> RepeatUnitOp::getVariationCounts() const {
  return d_countAtEachPoint;
}

namespace {
void flipHeadHeadGroups(unsigned int origAtomCount, RWMol &mol) {
  for (auto aidx = origAtomCount; aidx < mol.getNumAtoms(); ++aidx) {
    auto atom = mol.getAtomWithIdx(aidx);
    if (atom->hasProp(headheadmarker)) {
      if (atom->hasProp(headmarker)) {
        atom->setProp(tailmarker, atom->getProp<unsigned>(headmarker));
        atom->clearProp(headmarker);
      } else if (atom->hasProp(tailmarker)) {
        atom->setProp(headmarker, atom->getProp<unsigned>(tailmarker));
        atom->clearProp(tailmarker);
      }
      if (atom->hasProp(headmarker_frame)) {
        atom->setProp(
            tailmarker_frame,
            atom->getProp<std::vector<unsigned int>>(headmarker_frame));
        atom->clearProp(headmarker_frame);
      } else if (atom->hasProp(tailmarker_frame)) {
        atom->setProp(
            headmarker_frame,
            atom->getProp<std::vector<unsigned int>>(tailmarker_frame));
        atom->clearProp(tailmarker_frame);
      }
    }
  }
}

void connectRepeatAtomsAndRemoveExtras(unsigned int origAtomCount, RWMol &mol) {
  mol.beginBatchEdit();
  for (auto aidx1 = 0u; aidx1 < origAtomCount; ++aidx1) {
    auto at1 = mol.getAtomWithIdx(aidx1);
    unsigned tailIdx;
    if (at1->getPropIfPresent(tailmarker, tailIdx)) {
      bool connected = false;
      for (auto aidx2 = origAtomCount; aidx2 < mol.getNumAtoms(); ++aidx2) {
        auto at2 = mol.getAtomWithIdx(aidx2);
        unsigned int headIdx;
        if (at2->getPropIfPresent(headmarker, headIdx) && tailIdx == headIdx) {
          connected = true;
          at2->clearProp(headmarker);
          // remove any atom connected to the head which isn't in the
          // repeat unit
          for (const auto &nbri :
               boost::make_iterator_range(mol.getAtomNeighbors(at2))) {
            auto nbr = mol[nbri];
            if (!nbr->hasProp(polymarker)) {
              mol.removeAtom(nbr);
            }
          }
          // FIX: not dealing with multiple bonds from the repeat unit
          mol.addBond(at1, at2, Bond::BondType::SINGLE);
          break;
        }
      }
      if (connected) {
        at1->clearProp(tailmarker);
        // remove any atom connected to the tail which isn't in the
        // repeat unit
        for (const auto &nbri :
             boost::make_iterator_range(mol.getAtomNeighbors(at1))) {
          auto nbr = mol[nbri];
          if (!nbr->hasProp(polymarker)) {
            mol.removeAtom(nbr);
          }
        }
      } else {
        throw ValueErrorException("no head found for tail");
      }
    }
  }
  mol.commitBatchEdit();
}

void connectRepeatToFrame(unsigned int nOrigAtoms, RWMol &mol,
                          std::map<unsigned, Atom *> &headMap,
                          std::map<unsigned, Atom *> &tailMap) {
  for (auto aidx = nOrigAtoms; aidx < mol.getNumAtoms(); ++aidx) {
    auto sruAtom = mol.getAtomWithIdx(aidx);
    unsigned int val;
    if (sruAtom->getPropIfPresent(headmarker, val)) {
      if (tailMap.find(val) != tailMap.end()) {
        // there's an atom in the frame to connect to:
        mol.addBond(sruAtom, tailMap[val], Bond::BondType::SINGLE);
        sruAtom->clearProp(headmarker);
        tailMap.erase(val);
      }
    }
    if (sruAtom->getPropIfPresent(tailmarker, val)) {
      if (headMap.find(val) != headMap.end()) {
        // there's an atom in the frame to connect to:
        mol.addBond(sruAtom, headMap[val], Bond::BondType::SINGLE);
        sruAtom->clearProp(tailmarker);
        headMap.erase(val);
      }
    }
    std::vector<unsigned int> vals;
    if (sruAtom->getPropIfPresent(headmarker_frame, vals)) {
      for (const auto val : vals) {
        headMap[val] = sruAtom;
      }
      sruAtom->clearProp(headmarker_frame);
    }
    if (sruAtom->getPropIfPresent(tailmarker_frame, vals)) {
      for (const auto val : vals) {
        tailMap[val] = sruAtom;
      }
      sruAtom->clearProp(tailmarker_frame);
    }
  }
}

void constructHeadAndTailMaps(RWMol &mol, std::map<unsigned, Atom *> &headMap,
                              std::map<unsigned, Atom *> &tailMap) {
  for (auto atom : mol.atoms()) {
    std::vector<unsigned int> vals;
    if (atom->getPropIfPresent(headmarker_frame, vals)) {
      for (auto val : vals) {
        if (headMap.find(val) != headMap.end()) {
          throw ValueErrorException(
              "SRU group present with multiple head atoms with the same index");
        }
        headMap[val] = atom;
      }
    }
    if (atom->getPropIfPresent(tailmarker_frame, vals)) {
      for (auto val : vals) {
        if (tailMap.find(val) != tailMap.end()) {
          throw ValueErrorException(
              "SRU group present with multiple tail atoms with the same index");
        }
        tailMap[val] = atom;
      }
    }
  }
}
}  // namespace

std::unique_ptr<ROMol> RepeatUnitOp::operator()(
    const std::vector<size_t> &which) const {
  PRECONDITION(dp_mol, "no molecule");
  PRECONDITION(dp_frame, "not initialized");
  if (which.size() != d_countAtEachPoint.size()) {
    throw ValueErrorException("bad element choice in enumeration");
  }
  // quick error checking before we do any work:
  for (size_t i = 0; i < which.size(); ++i) {
    if (which[i] >= d_countAtEachPoint[i]) {
      throw ValueErrorException("bad element value in enumeration");
    }
  }

  std::unique_ptr<RWMol> res{new RWMol()};
  res->insertMol(*dp_frame);

  // ---------------------
  // we will use these maps from head/tail markerss to atoms in the frame
  // later
  std::map<unsigned, Atom *> headMap;
  std::map<unsigned, Atom *> tailMap;
  constructHeadAndTailMaps(*res, headMap, tailMap);

  for (size_t i = 0; i < which.size(); ++i) {
    RWMol filling;
    // if there SRU sgroups has a repetition range like 3-5, we should make
    // sure each output has at least 3 repetitions
    auto offset = d_minRepeatCounts[i];
    for (size_t iter = 0; iter < offset + which[i]; ++iter) {
      auto origAtomCount = filling.getNumAtoms();
      filling.insertMol(*d_repeats[i]);

      if (iter % 2) {
        // check for any HH groups on odd iterations and invert them
        flipHeadHeadGroups(origAtomCount, filling);
      }
      // if we aren't adding the first repeat unit, then we need to connect
      // things
      if (iter) {
        connectRepeatAtomsAndRemoveExtras(origAtomCount, filling);
      }
    }  // end of loop over iteration

    // ok, add the fragment generated by enumerating that SRU to the result:
    unsigned int nOrigAtoms = res->getNumAtoms();
    res->insertMol(filling);

    // and connect it to the frame:
    connectRepeatToFrame(nOrigAtoms, *res, headMap, tailMap);

  }  // end of loop over SRU

  // connect any remaining dangling heads and tails
  for (auto &tpl : headMap) {
    auto iter = tailMap.find(tpl.first);
    if (iter != tailMap.end()) {
      res->addBond(tpl.second, iter->second, Bond::BondType::SINGLE);
      tpl.second->clearProp(headmarker_frame);
      iter->second->clearProp(tailmarker_frame);
    }
  }
  res->commitBatchEdit();

  return std::unique_ptr<ROMol>(new ROMol(*res));
}

}  // namespace MolEnumerator

}  // namespace RDKit