File: Tetrahedral.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 (181 lines) | stat: -rw-r--r-- 5,074 bytes parent folder | download | duplicates (5)
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
//
//
//  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 "Tetrahedral.h"
#include "../rules/Rules.h"

namespace RDKit {
namespace CIPLabeler {

Tetrahedral::Tetrahedral(const CIPMol &mol, Atom *focus)
    : Configuration(mol, focus) {
  CHECK_INVARIANT(focus, "bad atom")
  CHECK_INVARIANT(focus->getChiralTag() == Atom::CHI_TETRAHEDRAL_CCW ||
                      focus->getChiralTag() == Atom::CHI_TETRAHEDRAL_CW,
                  "bad config")

  std::vector<Atom *> carriers;
  carriers.reserve(4);
  for (auto &nbr : mol.getNeighbors(focus)) {
    carriers.push_back(nbr);
  }
  if (carriers.size() < 4) {
    // Implicit H -- use the central atom instead of a dummy H
    carriers.push_back(focus);
  }
  if (carriers.size() < 4) {
    // Trigonal pyramid centers with an implicit H need a phantom
    // atom as fourth carrier. This one must be represented differently
    // than the implicit H.
    carriers.push_back(nullptr);
  }
  POSTCONDITION(carriers.size() == 4, "configurtion must have 4 carriers");

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

void Tetrahedral::setPrimaryLabel(Descriptor desc) {
  switch (desc) {
    case Descriptor::R:
    case Descriptor::S:
    case Descriptor::r:
    case Descriptor::s:
      getFocus()->setProp(common_properties::_CIPCode, to_string(desc));
      return;
    case Descriptor::seqTrans:
    case Descriptor::seqCis:
    case Descriptor::E:
    case Descriptor::Z:
    case Descriptor::M:
    case Descriptor::P:
    case Descriptor::m:
    case Descriptor::p:
    case Descriptor::SP_4:
    case Descriptor::TBPY_5:
    case Descriptor::OC_6:
      throw std::runtime_error(
          "Received a Descriptor that is not supported for atoms");
    default:
      throw std::runtime_error("Received an invalid Atom Descriptor");
  }
}

Descriptor Tetrahedral::label(const Rules &comp) {
  auto &digraph = getDigraph();

  auto root = digraph.getOriginalRoot();
  if (digraph.getCurrentRoot() != root) {
    digraph.changeRoot(root);
  }

  return label(root, comp);
}

Descriptor Tetrahedral::label(Node *node, Digraph &digraph, const Rules &comp) {
  digraph.changeRoot(node);
  return label(node, comp);
}

Descriptor Tetrahedral::label(Node *node, const Rules &comp) const {
  auto focus = getFocus();
  auto edges = node->getEdges();

  // something not right!?! bad creation
  if (edges.size() < 3) {
    return Descriptor::ns;
  }

  auto priority = comp.sort(node, edges);

  bool isUnique = priority.isUnique();
  if (!isUnique && edges.size() == 4) {
    if (comp.getNumSubRules() == 3) {
      return Descriptor::UNKNOWN;
    }
    auto partition = comp.getSorter()->getGroups(edges);
    if (partition.size() == 2) {
      node->getDigraph()->setRule6Ref(edges[1]->getEnd()->getAtom());
      priority = comp.sort(node, edges);
      node->getDigraph()->setRule6Ref(nullptr);
    } else if (partition.size() == 1) {
      // S4 symmetric case
      node->getDigraph()->setRule6Ref(edges[0]->getEnd()->getAtom());
      comp.sort(node, edges);
      auto nbrs1 = std::vector<Edge *>(edges.begin(), edges.end());

      node->getDigraph()->setRule6Ref(edges[1]->getEnd()->getAtom());
      priority = comp.sort(node, edges);

      node->getDigraph()->setRule6Ref(nullptr);

      if (parity4(nbrs1, edges) == 1) {
        return Descriptor::UNKNOWN;
      }
    }
    if (!priority.isUnique()) {
      return Descriptor::UNKNOWN;
    }
  } else if (!isUnique) {
    return Descriptor::UNKNOWN;
  }

  // if we are resolving a trigonal pyramid with an implicit H,
  // the 4th carrier will be a nullptr: we need to add a phantom
  // atom, which will always have the lowest priority, so that
  // it must be different than the representation of the implicit H.
  auto ordered = std::vector<Atom *>(4, nullptr);
  int idx = 0;
  for (const auto &edge : edges) {
    if (edge->getEnd()->isSet(Node::BOND_DUPLICATE) ||
        edge->getEnd()->isSet(Node::IMPL_HYDROGEN)) {
      continue;
    }
    ordered[idx] = edge->getEnd()->getAtom();
    ++idx;
  }
  if (idx < 4) {
    ordered[idx] = focus;
  }

  int parity = parity4(ordered, getCarriers());

  if (parity == 0) {
    throw std::runtime_error("Could not calculate parity! Carrier mismatch");
  }

  auto config = focus->getChiralTag();
  if (parity == 1) {
    if (config == Atom::CHI_TETRAHEDRAL_CCW) {
      config = Atom::CHI_TETRAHEDRAL_CW;
    } else {
      config = Atom::CHI_TETRAHEDRAL_CCW;
    }
  }

  if (config == Atom::CHI_TETRAHEDRAL_CCW) {
    if (priority.isPseudoAsymetric()) {
      return Descriptor::s;
    } else {
      return Descriptor::S;
    }
  } else if (config == Atom::CHI_TETRAHEDRAL_CW) {
    if (priority.isPseudoAsymetric()) {
      return Descriptor::r;
    } else {
      return Descriptor::R;
    }
  }

  return Descriptor::UNKNOWN;
}

}  // namespace CIPLabeler
}  // namespace RDKit