-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.py
190 lines (174 loc) · 7.8 KB
/
test.py
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
import unittest, os
from molecular_rectifier import Rectifier
from rdkit import Chem, Geometry, RDLogger
from rdkit.Chem import AllChem
RDLogger.DisableLog('rdApp.*')
import requests
from urllib.parse import quote
def name2mol(name) -> Chem.Mol:
"""
Using Cactus get a molecule from a name
"""
response: requests.Response = requests.get(
f'https://cactus.nci.nih.gov/chemical/structure/{quote(name)}/smiles')
response.raise_for_status()
smiles: str = response.text
mol: Chem.Mol = Chem.MolFromSmiles(smiles)
mol.SetProp('_Name', name)
AllChem.EmbedMolecule(mol)
return mol
class RectifierTester(unittest.TestCase):
# name: [before, after]
chemdex = {'phenylnaphthalene': ('c1ccc2ccccc2c1(c3ccccc3)', 'c1ccc(-c2cccc3ccccc23)cc1'),
'benzoazetine': ('C12CCCCC1CC2', 'C1CCC2CCCC2C1'),
#'conjoined': ('C1C2CCC2C1', 'C1CCCCC1'), # bridged hexane
'allene': ('C=C=C', 'C=CC'),
'benzocyclopronane': ('C12CCCCC1C2', 'C1CCC2CCCC2C1'),
'benzoindolium': ('c12cccc(nc3)c1c3ccc2', 'C1=Nc2cccc3cccc1c23'),
# 'norbornane': ('C1CC2CCC1C2', 'C1CC2CCC1C2'),
'tetralin': ('c1ccc2c(c1)CCCC2', 'c1ccc2c(c1)CCCC2'),
'mixed_ring': ('c1cccc2c1CCCC2', 'c1ccc2c(c1)CCCC2'),
'mixe_ring2': ('C1CCCc2c1cccc2', 'c1ccc2c(c1)CCCC2'),
'acenaphthylene': ('c1cc2c3c1cccc3ccc2', 'C1=Cc2cccc3cccc1c23'),
'pentalene': ('C1=CC2=CC=CC2=C1', 'C1=CC2=CC=CC2=C1'),
}
def test_cyclopentadiene(self):
"""
A pyrrole is altered to have a carbon as opposed to a nitrogen
"""
# aromatic cyclopent-ine -> cyclopentadiene
name = 'cyclopentadiene'
mol = Chem.MolFromSmiles('[nH]1cccc1')
mol.SetProp('_Name', name)
AllChem.EmbedMolecule(mol)
mod = Chem.RWMol(mol)
mod.GetAtomWithIdx(0).SetAtomicNum(6)
mol = mod.GetMol()
recto = Rectifier(mol, atoms_in_bridge_cutoff=3).fix()
gotten = Chem.MolToSmiles(AllChem.RemoveHs(recto.mol))
after = 'C1=CCC=C1'
#self.assertEqual(after, gotten, f'{name} failed {gotten} (expected {after})')
# it no longer gives cyclopentadiene, but a pyrrole
def test_bad_ring(self):
name = 'bad ring'
after = 'c1ccc2c(c1)CCCC2'
mol = Chem.MolFromSmiles(after)
mol.SetProp('_Name', name)
mol.GetBondBetweenAtoms(0, 1).SetBondType(Chem.BondType.SINGLE)
AllChem.EmbedMolecule(mol)
before = Chem.MolToSmiles(mol)
recto = Rectifier(mol).fix()
gotten = Chem.MolToSmiles(AllChem.RemoveHs(recto.mol))
self.assertEqual(gotten, after, f'{name} failed {gotten} (expected {after})')
def test_bad_ring2(self):
name = 'bad ring2'
before = 'c1ccc2c(c1)CCCC2'
after = 'c1ccc2ccccc2c1'
mol = Chem.MolFromSmiles(before)
mol.SetProp('_Name', name)
mol.GetBondBetweenAtoms(0, 1).SetBondType(Chem.BondType.SINGLE)
mol.GetBondBetweenAtoms(6, 7).SetBondType(Chem.BondType.AROMATIC)
before = Chem.MolToSmiles(mol)
AllChem.EmbedMolecule(mol)
recto = Rectifier(mol).fix()
gotten = Chem.MolToSmiles(AllChem.RemoveHs(recto.mol))
self.assertEqual(after, gotten, f'{name} failed {gotten} (expected {after})')
def test_emergency_overclose(self):
mol: Chem.Mol = Chem.MolFromSmiles('CC(C)(C)')
conf = Chem.Conformer()
for i in range(4):
conf.SetAtomPosition(i, Geometry.Point3D(i/4, 0, 0))
mol.AddConformer(conf)
recto = Rectifier(mol).fix()
self.assertEqual(AllChem.RemoveHs(recto.mol).GetNumAtoms(), 1)
def test_sub_downgrade(self):
# none of these should be touched
xanthine = Chem.MolFromSmiles('c1[nH]c2c(n1)nc(nc2O)O')
caffeine = Chem.MolFromSmiles('CN1C=NC2=C1C(=O)N(C(=O)N2C)C')
naphthalene = Chem.MolFromSmiles('c1c2ccccc2ccc1')
naphthoquinone = Chem.MolFromSmiles('O=C1c2ccccc2C(=O)cc1')
anthracene = Chem.MolFromSmiles('c1ccc2cc3ccccc3cc2c1')
primulin = Chem.MolFromSmiles(
'COc1cc(cc(c1O)OC)c2c(cc3c(cc(cc3[o+]2)O)O)O[C@H]4[C@@H]([C@H]([C@H]([C@H](O4)CO)O)O)O')
for mol in (xanthine, caffeine, naphthalene, naphthoquinone, anthracene, primulin):
self.assertTrue(Rectifier(mol).fix().mol.HasSubstructMatch(mol),
'failed to fix molecule')
# this will
phenol = Chem.MolFromSmiles('Oc1ccccc1')
phenmonoone = Chem.Mol(phenol) # not a real thing...
phenmonoone.GetBondWithIdx(0).SetBondType(Chem.BondType.DOUBLE)
self.assertTrue(Rectifier(phenmonoone).fix().mol.HasSubstructMatch(phenol),
'failed to fix substituent!')
def test_real_merger(self):
molblock = '''spiro-ditoluene
RDKit 2D
13 14 0 0 0 0 0 0 0 0999 V2000
1.5000 2.5981 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-5.9196 -0.8437 0.0724 C 0 0 0 0 0 0 0 0 0 0 0 0
1.5000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.7500 1.2990 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.7500 1.2990 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-1.5000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-0.7500 -1.2990 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
0.7500 -1.2990 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
-3.8002 0.1514 1.1984 C 0 0 0 0 0 0 0 0 0 0 0 0
-4.4463 -0.5624 0.0483 C 0 0 0 0 0 0 0 0 0 0 0 0
-3.6193 -0.9951 -1.1259 C 0 0 0 0 0 0 0 0 0 0 0 0
-2.1462 -0.7139 -1.1501 C 0 0 0 0 0 0 0 0 0 0 0 0
-2.3270 0.4327 1.1743 C 0 0 0 0 0 0 0 0 0 0 0 0
3 8 4 0
3 4 4 0
4 5 4 0
4 1 1 0
5 6 4 0
6 7 4 0
7 8 4 0
9 13 4 0
9 10 4 0
10 11 4 0
10 2 1 0
11 12 4 0
6 12 4 0
6 13 4 0
M END'''
mol = Chem.MolFromMolBlock(molblock, sanitize=False, removeHs=False)
recto = Rectifier(mol)
recto.fix()
self.assertEqual('CC1CCC2(CCCC(C)C2)CC1', Chem.MolToSmiles(AllChem.RemoveHs(recto.mol)))
def test_unproto_indole(self):
correct = Chem.MolFromSmiles('c1ccc2[nH]ccc2c1')
mol = Chem.MolFromSmiles('c1ccc2nccc2c1', sanitize=False)
recto = Rectifier(mol)
self.assertIn(4, recto._get_nitrogens(Chem.DetectChemistryProblems(mol)[0].GetAtomIndices()))
# recto.log.setLevel(logging.DEBUG)
# recto.log.handlers[0].setLevel(logging.DEBUG)
recto.fix()
self.assertEqual(Chem.MolToInchi(correct), Chem.MolToInchi(recto.mol))
def test_methylpyridine(self):
# N-methylpyridine
correct = Chem.MolFromSmiles('c1cc[n+](C)cc1')
mol = Chem.MolFromSmiles('c1ccn(C)cc1', sanitize=False)
recto = Rectifier(mol)
recto.valence_correction = 'charge' # element
recto.fix()
self.assertEqual(Chem.MolToInchi(correct), Chem.MolToInchi(recto.mol))
correct = Chem.MolFromSmiles('c1ccc(C)cc1')
recto = Rectifier(mol)
recto.valence_correction = 'element'
recto.fix()
self.assertEqual(Chem.MolToInchi(correct), Chem.MolToInchi(recto.mol))
from functools import partial
def test_factory(name):
def test(self):
before, after = self.chemdex[name]
mol = Chem.MolFromSmiles(before)
mol.SetProp('_Name', name)
AllChem.EmbedMolecule(mol)
recto = Rectifier(mol).fix()
gotten = Chem.MolToSmiles(AllChem.RemoveHs(recto.mol))
self.assertEqual(after, gotten, f'{name} failed {gotten} (expected {after}) from {before}')
return test
for name in RectifierTester.chemdex:
setattr(RectifierTester,
f'test_{name}',
test_factory(name))