RDKit
Open-source cheminformatics and machine learning.
Toggle main menu visibility
Loading...
Searching...
No Matches
BoundsMatrixBuilderDetails.h
Go to the documentation of this file.
1
//
2
// Copyright (C) 2026 ETH Zurich
3
// Created by: Katharina Buchthal
4
//
5
// @@ All Rights Reserved @@
6
// This file is part of the RDKit.
7
// The contents are covered by the terms of the BSD license
8
// which is included in the file license.txt, found at the root
9
// of the RDKit source tree.
10
//
11
#include "
DistGeom/ZMatrixUtils.h
"
12
#include "
RDGeneral/Invariant.h
"
13
#include <vector>
14
#include <optional>
15
#include <vector>
16
#include <ranges>
17
#include <algorithm>
18
#include "
RDGeneral/Invariant.h
"
19
20
#ifndef M_PI
21
#define M_PI 3.14159265358979323846
22
#endif
23
24
#ifndef RD_BOUNDS_MATRIX_BUILDER_DETAILS_H
25
#define RD_BOUNDS_MATRIX_BUILDER_DETAILS_H
26
27
namespace
RDKit
{
28
namespace
DGeomHelpers {
29
enum class
TorsionType
{
30
CIS
= 0,
31
TRANS
,
32
FLEXIBLE
,
33
CUSTOM
,
34
CISTRANS
35
};
36
37
enum class
Type14
{
38
IN_CHAIN
,
39
IN_RING
,
40
TWO_IN_SAME_RING
,
41
TWO_IN_DIFF_RING
,
42
SHARE_RING_BOND
,
43
MACROCYCLE_TWO_IN_SAME_RING
,
44
MACROCYCLE_ALL_IN_SAME_RING
45
};
46
47
struct
TorsionValue
{
48
TorsionType
type
=
TorsionType::FLEXIBLE
;
49
std::optional<double>
value
= {};
50
std::optional<double>
extraDist
= {};
51
bool
isForced
=
false
;
52
};
53
54
inline
DistGeom::TorsionRange
ringTorsion
(
const
std::size_t rSize) {
55
double
torsion =
M_PI
;
56
switch
(rSize) {
57
case
4u:
58
[[fallthrough]];
59
case
5u:
60
torsion =
M_PI
* 45.0 / 180.0;
61
break
;
62
case
6u:
63
torsion =
M_PI
* 60.0 / 180.0;
64
break
;
65
case
7u:
66
torsion =
M_PI
* 90.0 / 180.0;
67
break
;
68
case
8u:
69
torsion =
M_PI
* 100.0 / 180.0;
70
break
;
71
}
72
return
{-torsion, torsion};
73
}
74
75
//! A structure used to store planar 14 paths - cis/trans
76
struct
Path14Configuration
{
77
unsigned
int
bid1
,
bid2
,
bid3
;
78
unsigned
int
aid1
,
aid2
,
aid3
,
aid4
;
79
TorsionValue
value
;
80
Type14
type14
;
81
std::size_t
rSize
= 0;
82
83
DistGeom::TorsionCandidates
toTorsionRange
()
const
{
84
switch
(
value
.type) {
85
case
TorsionType::CIS
:
86
return
DistGeom::TorsionValues
{0.0};
87
case
TorsionType::TRANS
:
88
return
DistGeom::TorsionValues
{
M_PI
};
89
case
TorsionType::FLEXIBLE
:
90
return
ringTorsion
(
rSize
);
91
case
TorsionType::CISTRANS
:
92
return
DistGeom::TorsionValues
{0.0,
M_PI
};
93
case
TorsionType::CUSTOM
:
94
return
DistGeom::TorsionValues
{*
value
.value};
95
default
:
96
break
;
97
}
98
return
ringTorsion
(0);
99
}
100
};
101
102
using
PATH14_VECT
= std::vector<Path14Configuration>;
103
104
inline
std::size_t
getUnifiedId
(
const
unsigned
int
id1,
const
unsigned
int
id2,
105
const
unsigned
int
n) {
106
// returns an id for (id1, id2) independent of order within range (0, 2*n - 1)
107
// assuming id1 < n and id2 < n
108
return
id1 < id2 ? (static_cast<std::size_t>(id1) * n + id2)
109
: (
static_cast<
std::size_t
>
(id2) * n + id1);
110
}
111
112
inline
std::size_t
getUnifiedId
(
const
unsigned
int
id1,
const
unsigned
int
id2,
113
const
unsigned
int
id3,
const
unsigned
int
n) {
114
// returns an id for (id1, id2, id3) independent of order of id1, id3 within
115
// range (0, 3*(n) - 1) assuming id1 < n, id2 < n and id3 < n
116
return
id1 < id3 ? (static_cast<std::size_t>(id1) * n * n + id2 * n + id3)
117
: (
static_cast<
std::size_t
>
(id3) * n * n + id2 * n + id1);
118
}
119
120
template
<
unsigned
int
numBondIds>
121
auto
unifiedIdToBondIds
(std::size_t
id
,
const
unsigned
int
n) {
122
// converts an id (created with getUnifiedId) back into the original bond ids
123
std::array<unsigned int, numBondIds> bondIds;
124
125
for
(
auto
i : std::views::iota(0u, numBondIds) | std::views::reverse) {
126
bondIds[i] =
static_cast<
unsigned
int
>
(
id
% n);
127
id
/= n;
128
}
129
130
return
bondIds;
131
};
132
133
struct
Bounds
{
134
double
lower
{1.0},
upper
{-1.0};
// we start invalid
135
unsigned
int
aid1
{0},
aid4
{0};
136
137
inline
bool
valid
()
const
{
return
lower
<=
upper
; }
138
139
bool
operator==
(
const
Bounds
&)
const
=
default
;
140
141
friend
std::ostream &
operator<<
(std::ostream &os,
const
Bounds
&b) {
142
return
os <<
"Bounds{"
143
<<
"lower="
<< b.
lower
<<
", upper="
<< b.
upper
144
<<
", aid1="
<< b.
aid1
<<
", aid4="
<< b.
aid4
<<
'}'
;
145
}
146
};
147
148
inline
Bounds
merge
(std::vector<Bounds> bounds) {
149
PRECONDITION
(bounds.size(),
"Cannot merge empty list of bounds"
);
150
151
std::ranges::sort(bounds, {}, &
Bounds::lower
);
152
153
Bounds
current = bounds.front();
154
double
componentUpper = current.upper;
155
std::optional<double> resultLower;
156
157
// What we are doing here:
158
// U {i'=intersection(i_j,..,i_k) | {i_j, ..., i_k}\subset(I) ^ i` !=
159
// \emptyset ^ !\exists(i_l): intersection(i`, i_l) != \emptyset}
160
// or in other words:
161
// we aim to find the union of all intersections that are maximal in a sense
162
// that adding another arbitrary bounds to it, would lead into an empty set
163
164
// we solve this by traversing the sorted bounds in a sweep manner while
165
// keeping track on the current/active non-empty intersection
166
// (currentIntersection), the largest upperBound that was reached so far
167
// (this is needed since the currentIntersection.upper can be smaller than
168
// that, losing track of potenial overlaps/intersections).
169
// To avoid storing all maximal non-overlapping intersections (only the
170
// first and last one is relevant), we store the lower bound of the first
171
// maximal intersection in resultLower
172
173
for
(
const
auto
&_bound : bounds | std::views::drop(1)) {
174
if
(_bound.lower <= current.upper) {
175
// Case 1: _bounds intersects with currentIntersection => add to current
176
// intersection
177
// we know that bounds are sorted by lower bounds =>
178
// _bound.lower is always greater/equal currentIntersection.lower
179
current.lower = _bound.lower;
180
current.upper = std::min(current.upper, _bound.upper);
181
}
else
{
182
// Case 2: _bound is not overlapping with the current intersection => we
183
// know that currentIntersection is maximal
184
185
if
(!resultLower) {
186
resultLower = current.lower;
187
}
188
189
current.lower = _bound.lower;
190
current.upper =
191
_bound.lower <= componentUpper
192
? std::min(componentUpper,
193
_bound.upper)
// there is this at least former
194
// bounds that is overlapping and
195
// needs to be considered
196
: _bound.upper;
197
}
198
199
componentUpper = std::max(componentUpper, _bound.upper);
200
}
201
return
Bounds
{.lower = resultLower.value_or(current.lower),
202
.upper = current.upper,
203
.aid1 = bounds.front().aid1,
204
.aid4 = bounds.front().aid4};
205
}
206
207
}
// namespace DGeomHelpers
208
}
// namespace RDKit
209
#endif
M_PI
#define M_PI
Definition
BoundsMatrixBuilderDetails.h:21
Invariant.h
PRECONDITION
#define PRECONDITION(expr, mess)
Definition
Invariant.h:108
ZMatrixUtils.h
DistGeom::TorsionCandidates
std::variant< TorsionRange, TorsionValues > TorsionCandidates
Definition
ZMatrixUtils.h:170
RDKit::DGeomHelpers::unifiedIdToBondIds
auto unifiedIdToBondIds(std::size_t id, const unsigned int n)
Definition
BoundsMatrixBuilderDetails.h:121
RDKit::DGeomHelpers::getUnifiedId
std::size_t getUnifiedId(const unsigned int id1, const unsigned int id2, const unsigned int n)
Definition
BoundsMatrixBuilderDetails.h:104
RDKit::DGeomHelpers::Type14
Type14
Definition
BoundsMatrixBuilderDetails.h:37
RDKit::DGeomHelpers::Type14::IN_RING
@ IN_RING
Definition
BoundsMatrixBuilderDetails.h:39
RDKit::DGeomHelpers::Type14::TWO_IN_DIFF_RING
@ TWO_IN_DIFF_RING
Definition
BoundsMatrixBuilderDetails.h:41
RDKit::DGeomHelpers::Type14::TWO_IN_SAME_RING
@ TWO_IN_SAME_RING
Definition
BoundsMatrixBuilderDetails.h:40
RDKit::DGeomHelpers::Type14::SHARE_RING_BOND
@ SHARE_RING_BOND
Definition
BoundsMatrixBuilderDetails.h:42
RDKit::DGeomHelpers::Type14::MACROCYCLE_TWO_IN_SAME_RING
@ MACROCYCLE_TWO_IN_SAME_RING
Definition
BoundsMatrixBuilderDetails.h:43
RDKit::DGeomHelpers::Type14::MACROCYCLE_ALL_IN_SAME_RING
@ MACROCYCLE_ALL_IN_SAME_RING
Definition
BoundsMatrixBuilderDetails.h:44
RDKit::DGeomHelpers::Type14::IN_CHAIN
@ IN_CHAIN
Definition
BoundsMatrixBuilderDetails.h:38
RDKit::DGeomHelpers::PATH14_VECT
std::vector< Path14Configuration > PATH14_VECT
Definition
BoundsMatrixBuilderDetails.h:102
RDKit::DGeomHelpers::TorsionType
TorsionType
Definition
BoundsMatrixBuilderDetails.h:29
RDKit::DGeomHelpers::TorsionType::FLEXIBLE
@ FLEXIBLE
Definition
BoundsMatrixBuilderDetails.h:32
RDKit::DGeomHelpers::TorsionType::CUSTOM
@ CUSTOM
Definition
BoundsMatrixBuilderDetails.h:33
RDKit::DGeomHelpers::TorsionType::CISTRANS
@ CISTRANS
Definition
BoundsMatrixBuilderDetails.h:34
RDKit::DGeomHelpers::TorsionType::TRANS
@ TRANS
Definition
BoundsMatrixBuilderDetails.h:31
RDKit::DGeomHelpers::TorsionType::CIS
@ CIS
Definition
BoundsMatrixBuilderDetails.h:30
RDKit::DGeomHelpers::ringTorsion
DistGeom::TorsionRange ringTorsion(const std::size_t rSize)
Definition
BoundsMatrixBuilderDetails.h:54
RDKit::DGeomHelpers::merge
Bounds merge(std::vector< Bounds > bounds)
Definition
BoundsMatrixBuilderDetails.h:148
RDKit
Std stuff.
Definition
Abbreviations.h:19
DistGeom::TorsionRange
Definition
ZMatrixUtils.h:54
DistGeom::TorsionValues
Definition
ZMatrixUtils.h:109
RDKit::DGeomHelpers::Bounds
Definition
BoundsMatrixBuilderDetails.h:133
RDKit::DGeomHelpers::Bounds::valid
bool valid() const
Definition
BoundsMatrixBuilderDetails.h:137
RDKit::DGeomHelpers::Bounds::aid1
unsigned int aid1
Definition
BoundsMatrixBuilderDetails.h:135
RDKit::DGeomHelpers::Bounds::lower
double lower
Definition
BoundsMatrixBuilderDetails.h:134
RDKit::DGeomHelpers::Bounds::aid4
unsigned int aid4
Definition
BoundsMatrixBuilderDetails.h:135
RDKit::DGeomHelpers::Bounds::operator==
bool operator==(const Bounds &) const =default
RDKit::DGeomHelpers::Bounds::operator<<
friend std::ostream & operator<<(std::ostream &os, const Bounds &b)
Definition
BoundsMatrixBuilderDetails.h:141
RDKit::DGeomHelpers::Bounds::upper
double upper
Definition
BoundsMatrixBuilderDetails.h:134
RDKit::DGeomHelpers::Path14Configuration
A structure used to store planar 14 paths - cis/trans.
Definition
BoundsMatrixBuilderDetails.h:76
RDKit::DGeomHelpers::Path14Configuration::aid1
unsigned int aid1
Definition
BoundsMatrixBuilderDetails.h:78
RDKit::DGeomHelpers::Path14Configuration::aid2
unsigned int aid2
Definition
BoundsMatrixBuilderDetails.h:78
RDKit::DGeomHelpers::Path14Configuration::aid4
unsigned int aid4
Definition
BoundsMatrixBuilderDetails.h:78
RDKit::DGeomHelpers::Path14Configuration::rSize
std::size_t rSize
Definition
BoundsMatrixBuilderDetails.h:81
RDKit::DGeomHelpers::Path14Configuration::bid3
unsigned int bid3
Definition
BoundsMatrixBuilderDetails.h:77
RDKit::DGeomHelpers::Path14Configuration::bid1
unsigned int bid1
Definition
BoundsMatrixBuilderDetails.h:77
RDKit::DGeomHelpers::Path14Configuration::toTorsionRange
DistGeom::TorsionCandidates toTorsionRange() const
Definition
BoundsMatrixBuilderDetails.h:83
RDKit::DGeomHelpers::Path14Configuration::type14
Type14 type14
Definition
BoundsMatrixBuilderDetails.h:80
RDKit::DGeomHelpers::Path14Configuration::value
TorsionValue value
Definition
BoundsMatrixBuilderDetails.h:79
RDKit::DGeomHelpers::Path14Configuration::aid3
unsigned int aid3
Definition
BoundsMatrixBuilderDetails.h:78
RDKit::DGeomHelpers::Path14Configuration::bid2
unsigned int bid2
Definition
BoundsMatrixBuilderDetails.h:77
RDKit::DGeomHelpers::TorsionValue
Definition
BoundsMatrixBuilderDetails.h:47
RDKit::DGeomHelpers::TorsionValue::value
std::optional< double > value
Definition
BoundsMatrixBuilderDetails.h:49
RDKit::DGeomHelpers::TorsionValue::isForced
bool isForced
Definition
BoundsMatrixBuilderDetails.h:51
RDKit::DGeomHelpers::TorsionValue::extraDist
std::optional< double > extraDist
Definition
BoundsMatrixBuilderDetails.h:50
RDKit::DGeomHelpers::TorsionValue::type
TorsionType type
Definition
BoundsMatrixBuilderDetails.h:48
GraphMol
DistGeomHelpers
BoundsMatrixBuilderDetails.h
Generated on
for RDKit by
1.17.0