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
//! Molecular quantum number descriptor implementation boundary.
//!
//! This module exclusively owns the single fixed-order 42-component MQN core.
//! It reuses shared ring, rotatable-bond, hydrogen, and graph state while
//! retaining the source's MQN-specific N/O polarity counts in this one core.
use super::{DescriptorError, DescriptorResult, NumRotatableBondsOptions};
use crate::{BondOrder, Molecule};
pub(super) fn calc_mqns_core(molecule: &Molecule) -> DescriptorResult<[u32; 42]> {
// BEGIN RDKIT CPP FUNCTION: RDKit::Descriptors::calcMQNs
// RDKit✔️✔️: std::vector<unsigned int> calcMQNs(const ROMol &mol, bool) {
// RDKit✔️✔️: // FIX: use force value to enable caching
// RDKit✔️✔️: std::vector<unsigned int> res(42, 0);
let mut result = [0_u32; 42];
let rings =
super::descriptor_ring_info(molecule, false).map_err(|_| DescriptorError::Unsupported {
function: "calc_mqns",
rdkit_function: "MolOps::symmetrizeSSSR/RingInfo",
})?;
// RDKit✔️✔️: // ---------------------------------------------------
// RDKit✔️✔️: // atom-centered things
// RDKit✔️✔️: // Note: We're not doing exactly the same thing
// RDKit✔️✔️: // as the original paper on polarity counts
// RDKit✔️✔️: // since we're using different donor and acceptor
// RDKit✔️✔️: // definitions.
// RDKit✔️✔️: ROMol::VERTEX_ITER atBegin, atEnd;
// RDKit✔️✔️: boost::tie(atBegin, atEnd) = mol.getVertices();
// RDKit✔️✔️: while (atBegin != atEnd) {
for atom in molecule.atoms() {
// RDKit✔️✔️: const Atom *at = mol[*atBegin];
// RDKit✔️✔️: ++atBegin;
// RDKit✔️✔️: unsigned int nHs = at->getTotalNumHs();
let hydrogen_count = crate::valence::total_num_hydrogens(molecule, atom.id(), false)
.map_err(|_| DescriptorError::Unsupported {
function: "calc_mqns",
rdkit_function: "Atom::getTotalNumHs",
})?;
// RDKit✔️✔️: unsigned int nRings = mol.getRingInfo()->numAtomRings(at->getIdx());
let ring_count = rings.num_atom_rings(atom.id());
let degree = molecule.adjacency().neighbors_of(atom.id().index()).len();
// RDKit✔️✔️: switch (at->getAtomicNum()) {
match atom.atomic_number() {
// RDKit✔️✔️: case 0:
// RDKit✔️✔️: case 1:
// RDKit✔️✔️: break;
0 | 1 => {}
// RDKit✔️✔️: case 6:
// RDKit✔️✔️: res[0]++;
// RDKit✔️✔️: break;
6 => result[0] += 1,
// RDKit✔️✔️: case 9:
// RDKit✔️✔️: res[1]++;
// RDKit✔️✔️: break;
9 => result[1] += 1,
// RDKit✔️✔️: case 17:
// RDKit✔️✔️: res[2]++;
// RDKit✔️✔️: break;
17 => result[2] += 1,
// RDKit✔️✔️: case 35:
// RDKit✔️✔️: res[3]++;
// RDKit✔️✔️: break;
35 => result[3] += 1,
// RDKit✔️✔️: case 53:
// RDKit✔️✔️: res[4]++;
// RDKit✔️✔️: break;
53 => result[4] += 1,
// RDKit✔️✔️: case 16:
// RDKit✔️✔️: res[5]++;
// RDKit✔️✔️: break;
16 => result[5] += 1,
// RDKit✔️✔️: case 15:
// RDKit✔️✔️: res[6]++;
// RDKit✔️✔️: break;
15 => result[6] += 1,
// RDKit✔️✔️: case 7:
7 => {
// RDKit✔️✔️: if (!nRings) {
// RDKit✔️✔️: res[7]++;
// RDKit✔️✔️: } else {
// RDKit✔️✔️: res[8]++;
// RDKit✔️✔️: }
if ring_count == 0 {
result[7] += 1;
} else {
result[8] += 1;
}
// RDKit✔️✔️: if (at->getDegree() != 4) {
// RDKit✔️✔️: res[19]++; // number of acceptor sites
// RDKit✔️✔️: res[20]++; // number of acceptor atoms
// RDKit✔️✔️: }
if degree != 4 {
result[19] += 1;
result[20] += 1;
}
// RDKit✔️✔️: if (nHs) {
// RDKit✔️✔️: res[21] += nHs; // number of donor sites
// RDKit✔️✔️: res[22]++; // number of donor atoms
// RDKit✔️✔️: }
if hydrogen_count != 0 {
result[21] += hydrogen_count;
result[22] += 1;
}
// RDKit✔️✔️: break;
}
// RDKit✔️✔️: case 8:
8 => {
// RDKit✔️✔️: if (!nRings) {
// RDKit✔️✔️: res[9]++;
// RDKit✔️✔️: } else {
// RDKit✔️✔️: res[10]++;
// RDKit✔️✔️: }
if ring_count == 0 {
result[9] += 1;
} else {
result[10] += 1;
}
// RDKit✔️✔️: res[20]++; // number of acceptor atoms
result[20] += 1;
// RDKit✔️✔️: if (at->getFormalCharge() != -1) {
// RDKit✔️✔️: res[19] += 2; // number of acceptor sites
// RDKit✔️✔️: } else {
// RDKit✔️✔️: res[19] += 3; // number of acceptor sites
// RDKit✔️✔️: }
if atom.formal_charge() != -1 {
result[19] += 2;
} else {
result[19] += 3;
}
// RDKit✔️✔️: if (nHs) {
// RDKit✔️✔️: res[21] += nHs; // number of donor sites
// RDKit✔️✔️: res[22]++; // number of donor atoms
// RDKit✔️✔️: }
if hydrogen_count != 0 {
result[21] += hydrogen_count;
result[22] += 1;
}
// RDKit✔️✔️: break;
}
// RDKit✔️✔️: default:
// RDKit✔️✔️: break;
_ => {} // RDKit✔️✔️: }
}
// RDKit✔️✔️: if (at->getFormalCharge() > 0) {
// RDKit✔️✔️: res[24]++; // positive charges
// RDKit✔️✔️: } else if (at->getFormalCharge() < 0) {
// RDKit✔️✔️: res[23]++; // negative charges
// RDKit✔️✔️: }
if atom.formal_charge() > 0 {
result[24] += 1;
} else if atom.formal_charge() < 0 {
result[23] += 1;
}
// RDKit✔️✔️: if (at->getAtomicNum() != 1) {
if atom.atomic_number() != 1 {
// RDKit✔️✔️: switch (at->getDegree()) {
match degree {
// RDKit✔️✔️: case 1:
// RDKit✔️✔️: res[25]++;
// RDKit✔️✔️: break;
1 => result[25] += 1,
// RDKit✔️✔️: case 2:
2 => {
// RDKit✔️✔️: if (!nRings) {
// RDKit✔️✔️: res[26]++;
// RDKit✔️✔️: } else {
// RDKit✔️✔️: res[29]++;
// RDKit✔️✔️: }
if ring_count == 0 {
result[26] += 1;
} else {
result[29] += 1;
}
// RDKit✔️✔️: break;
}
// RDKit✔️✔️: case 3:
3 => {
// RDKit✔️✔️: if (!nRings) {
// RDKit✔️✔️: res[27]++;
// RDKit✔️✔️: } else {
// RDKit✔️✔️: res[30]++;
// RDKit✔️✔️: }
if ring_count == 0 {
result[27] += 1;
} else {
result[30] += 1;
}
// RDKit✔️✔️: break;
}
// RDKit✔️✔️: case 4:
4 => {
// RDKit✔️✔️: if (!nRings) {
// RDKit✔️✔️: res[28]++;
// RDKit✔️✔️: } else {
// RDKit✔️✔️: res[31]++;
// RDKit✔️✔️: }
if ring_count == 0 {
result[28] += 1;
} else {
result[31] += 1;
}
// RDKit✔️✔️: break;
}
// RDKit✔️✔️: }
_ => {}
}
// RDKit✔️✔️: if (nRings >= 2) {
// RDKit✔️✔️: res[40]++;
// RDKit✔️✔️: }
if ring_count >= 2 {
result[40] += 1;
}
// RDKit✔️✔️: }
}
// RDKit✔️✔️: }
}
// RDKit✔️✔️: res[11] = mol.getNumHeavyAtoms();
result[11] = super::lipinski::calc_num_heavy_atoms(molecule)?;
// RDKit✔️✔️: // ---------------------------------------------------
// RDKit✔️✔️: // bond counts:
// RDKit✔️✔️: unsigned int nAromatic = 0;
let mut aromatic_count = 0_u32;
// RDKit✔️✔️: ROMol::EDGE_ITER firstB, lastB;
// RDKit✔️✔️: boost::tie(firstB, lastB) = mol.getEdges();
// RDKit✔️✔️: while (firstB != lastB) {
for bond in molecule.bonds() {
// RDKit✔️✔️: const Bond *bond = mol[*firstB];
// RDKit✔️✔️: if (bond->getIsAromatic()) {
// RDKit✔️✔️: ++nAromatic;
// RDKit✔️✔️: }
if bond.is_aromatic() {
aromatic_count += 1;
}
// RDKit✔️✔️: unsigned int nRings = mol.getRingInfo()->numBondRings(bond->getIdx());
let ring_count = rings.num_bond_rings(bond.id());
// RDKit✔️✔️: switch (bond->getBondType()) {
match bond.order() {
// RDKit✔️✔️: case Bond::SINGLE:
BondOrder::Single => {
// RDKit✔️✔️: if (!nRings) {
// RDKit✔️✔️: res[12]++;
// RDKit✔️✔️: } else {
// RDKit✔️✔️: res[15]++;
// RDKit✔️✔️: }
if ring_count == 0 {
result[12] += 1;
} else {
result[15] += 1;
}
// RDKit✔️✔️: break;
}
// RDKit✔️✔️: case Bond::DOUBLE:
BondOrder::Double => {
// RDKit✔️✔️: if (!nRings) {
// RDKit✔️✔️: res[13]++;
// RDKit✔️✔️: } else {
// RDKit✔️✔️: res[16]++;
// RDKit✔️✔️: }
if ring_count == 0 {
result[13] += 1;
} else {
result[16] += 1;
}
// RDKit✔️✔️: break;
}
// RDKit✔️✔️: case Bond::TRIPLE:
BondOrder::Triple => {
// RDKit✔️✔️: if (!nRings) {
// RDKit✔️✔️: res[14]++;
// RDKit✔️✔️: } else {
// RDKit✔️✔️: res[17]++;
// RDKit✔️✔️: }
if ring_count == 0 {
result[14] += 1;
} else {
result[17] += 1;
}
// RDKit✔️✔️: break;
}
// RDKit✔️✔️: default:
// RDKit✔️✔️: break;
_ => {} // RDKit✔️✔️: }
}
// RDKit✔️✔️: if (nRings >= 2) {
// RDKit✔️✔️: res[41]++;
// RDKit✔️✔️: }
if ring_count >= 2 {
result[41] += 1;
}
// RDKit✔️✔️: ++firstB;
// RDKit✔️✔️: }
}
// RDKit✔️✔️: // rather than do the work to kekulize the molecule, we cheat
// RDKit✔️✔️: // by just dividing the number of aromatic bonds evenly among the
// RDKit✔️✔️: // cyclic single bond and cyclic double bond bins and give any
// RDKit✔️✔️: // remainder to the single bonds
// RDKit✔️✔️: res[15] += nAromatic / 2;
result[15] += aromatic_count / 2;
// RDKit✔️✔️: res[16] += nAromatic / 2;
result[16] += aromatic_count / 2;
// RDKit✔️✔️: if (nAromatic % 2) {
// RDKit✔️✔️: res[15]++;
// RDKit✔️✔️: }
if aromatic_count % 2 != 0 {
result[15] += 1;
}
// RDKit✔️✔️: res[18] = calcNumRotatableBonds(mol);
result[18] = super::calc_num_rotatable_bonds(molecule, NumRotatableBondsOptions::Default)?;
// RDKit✔️✔️: // ---------------------------------------------------
// RDKit✔️✔️: // ring size counts
// RDKit✔️✔️: for (const auto &iv : mol.getRingInfo()->atomRings()) {
for ring in rings.atom_rings() {
// RDKit✔️✔️: if (iv.size() < 10) {
// RDKit✔️✔️: res[iv.size() + 29]++;
// RDKit✔️✔️: } else {
// RDKit✔️✔️: res[39]++;
// RDKit✔️✔️: }
if ring.len() < 10 {
result[ring.len() + 29] += 1;
} else {
result[39] += 1;
}
// RDKit✔️✔️: }
}
// RDKit✔️✔️: return res;
// RDKit✔️✔️: }
// END RDKIT CPP FUNCTION: RDKit::Descriptors::calcMQNs
Ok(result)
}
#[cfg(test)]
mod tests {
use super::calc_mqns_core;
use crate::Molecule;
#[test]
fn mqn_matches_pinned_rdkit_complete_vectors_for_focused_branches() {
const CASES: [(&str, [u32; 42]); 5] = [
("", [0; 42]),
(
"CCO",
[
2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 3, 2, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, 2,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
],
),
(
"c1ccccc1",
[
6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 6, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
],
),
(
"[Na+].[Cl-]",
[
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
],
),
(
"C1CC2CCC1C2",
[
7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 5, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 3, 2,
],
),
];
for (smiles, expected) in CASES {
let molecule = Molecule::from_smiles(smiles).expect("focused MQN fixture");
assert_eq!(
calc_mqns_core(&molecule),
Ok(expected),
"{smiles:?} MQN vector"
);
}
}
#[test]
fn mqn_complete_branch_fixture_exercises_and_matches_every_index() {
const SMILES: &str = concat!(
"CC(F)(Cl)Br.CPI.N1CCOCC1.[O-]C(=O)[NH3+].c1ncccc1.C#CC=C.",
"C1#CC1.C1CCC2(CC1)CCCCC2.C1C2CC3CC1CC(C2)C3.C1CC1.C1CCC1.",
"C1CCCC1.C1CCCCCC1.C1CCCCCCC1.C1CCCCCCCC1.C1CCCCCCCCCC1.CCCC.CS"
);
const EXPECTED: [u32; 42] = [
93, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 105, 13, 2, 1, 82, 3, 1, 1, 10, 6, 4, 2, 1, 1, 15, 5,
1, 1, 78, 4, 1, 2, 1, 1, 8, 1, 1, 1, 1, 11, 12,
];
assert!(EXPECTED.iter().all(|&value| value != 0));
let molecule = Molecule::from_smiles(SMILES).expect("complete MQN branch fixture");
assert_eq!(calc_mqns_core(&molecule), Ok(EXPECTED));
}
}