File: AtropisomerBond.cpp

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: 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 (154 lines) | stat: -rw-r--r-- 4,586 bytes parent folder | download | duplicates (4)
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
//
//
//  Copyright (C) 2020 Schrödinger, LLC
//
//   @@ 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 <GraphMol/Chirality.h>
#include <GraphMol/Atropisomers.h>

#include "AtropisomerBond.h"
#include "../Sort.h"
#include "../rules/Rules.h"

namespace RDKit {
namespace CIPLabeler {
AtropisomerBond::AtropisomerBond(const CIPMol &mol, Bond *bond, Atom *startAtom,
                                 Atom *endAtom, Bond::BondStereo cfg)
    : Configuration(mol, {startAtom, endAtom}, true),
      dp_bond{bond},
      d_cfg{cfg} {
  CHECK_INVARIANT(startAtom && endAtom, "bad foci")
  CHECK_INVARIANT(d_cfg == Bond::STEREOATROPCW || d_cfg == Bond::STEREOATROPCCW,
                  "bad config")

  Atropisomers::AtropAtomAndBondVec atomAndBondVecs[2];
  if (!Atropisomers::getAtropisomerAtomsAndBonds(bond, atomAndBondVecs,
                                                 bond->getOwningMol())) {
    return;  // not an atropisomer
  }
  auto atom1 = mol.getAtom(atomAndBondVecs[0].second[0]->getOtherAtomIdx(
      atomAndBondVecs[0].first->getIdx()));
  auto atom2 = mol.getAtom(atomAndBondVecs[1].second[0]->getOtherAtomIdx(
      atomAndBondVecs[1].first->getIdx()));

  std::vector<Atom *> anchors{atom1, atom2};

  setCarriers(std::move(anchors));
}

void AtropisomerBond::setPrimaryLabel(Descriptor desc) {
  switch (desc) {
    case Descriptor::M:
    case Descriptor::P:
    case Descriptor::m:
    case Descriptor::p: {
      auto carriers = getCarriers();
      // dp_bond->setStereoAtoms(carriers[0]->getIdx(), carriers[1]->getIdx());
      // dp_bond->setStereo(d_cfg);
      dp_bond->setProp(common_properties::_CIPCode, to_string(desc));
      return;
    }
    case Descriptor::R:
    case Descriptor::S:
    case Descriptor::r:
    case Descriptor::s:
    case Descriptor::SP_4:
    case Descriptor::TBPY_5:
    case Descriptor::OC_6:
    case Descriptor::seqTrans:
    case Descriptor::E:
    case Descriptor::seqCis:
    case Descriptor::Z:
      throw std::runtime_error(
          "Received a Descriptor that is not supported for atropisomer bonds");
    default:
      throw std::runtime_error("Received an invalid Bond Descriptor");
  }
}

Descriptor AtropisomerBond::label(const Rules &comp) {
  auto &digraph = getDigraph();
  auto root1 = digraph.getOriginalRoot();
  if (digraph.getCurrentRoot() != root1) {
    digraph.changeRoot(root1);
  }

  return label(root1, digraph, comp);
}

Descriptor AtropisomerBond::label(Node *root1, Digraph &digraph,
                                  const Rules &comp) {
  const auto &focus1 = getFoci()[0];
  const auto &focus2 = getFoci()[1];

  const auto &internal = findInternalEdge(root1->getEdges(), focus1, focus2);
  if (internal == nullptr) {
    return Descriptor::UNKNOWN;
  }
  const auto &root2 = internal->getOther(root1);

  auto edges1 = root1->getEdges();
  auto edges2 = root2->getEdges();
  removeInternalEdges(edges1, focus1, focus2);
  removeInternalEdges(edges2, focus1, focus2);

  removeDuplicatesAndHs(edges1);
  removeDuplicatesAndHs(edges2);

  auto carriers = std::vector<Atom *>(getCarriers());
  auto config = d_cfg;

  if (root1->getAtom() == focus2) {
    std::swap(carriers[0], carriers[1]);
  }

  digraph.changeRoot(root1);
  const auto &priority1 = comp.sort(root1, edges1);
  if (!priority1.isUnique()) {
    return Descriptor::UNKNOWN;
  }
  // swap
  if (edges1.size() > 1 && carriers[0] == edges1[1]->getEnd()->getAtom()) {
    if (config == Bond::STEREOATROPCCW) {
      config = Bond::STEREOATROPCW;
    } else {
      config = Bond::STEREOATROPCCW;
    }
  }
  digraph.changeRoot(root2);
  const auto &priority2 = comp.sort(root2, edges2);
  if (!priority2.isUnique()) {
    return Descriptor::UNKNOWN;
  }
  // swap
  if (edges2.size() > 1 && carriers[1] == edges2[1]->getEnd()->getAtom()) {
    if (config == Bond::STEREOATROPCCW) {
      config = Bond::STEREOATROPCW;
    } else {
      config = Bond::STEREOATROPCCW;
    }
  }

  if (config == Bond::STEREOATROPCCW) {
    if (priority1.isPseudoAsymetric() || priority2.isPseudoAsymetric()) {
      return Descriptor::m;
    } else {
      return Descriptor::M;
    }
  } else if (config == Bond::STEREOATROPCW) {
    if (priority1.isPseudoAsymetric() || priority2.isPseudoAsymetric()) {
      return Descriptor::p;
    } else {
      return Descriptor::P;
    }
  }
  return Descriptor::UNKNOWN;
}

}  // namespace CIPLabeler
}  // namespace RDKit