-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsmiles_parser.py
277 lines (251 loc) · 7.9 KB
/
smiles_parser.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
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
import modified_smiles_parser as pysmiles
import networkx
from data import NodeInfo, EdgeInfo, lookup_atom_type, lookup_bond_type
def parse_smiles(
input: str,
apply_paths=False,
parse_cis_trans=False,
only_biggest=False,
split_ions=False,
unknown_atom_is_dummy=False
) -> (
list[map], list[list[int]], list[map], list[tuple]): # x, a, e, paths
# Substitutions
#for replace_from, replace_to in metallole_substitutions.items():
# input = input.replace(replace_from, replace_to)
# networkx graph
network: networkx.Graph = pysmiles.read_smiles(
input,
explicit_hydrogen=True,
zero_order_bonds=False,
reinterpret_aromatic=True,
)
assert not(only_biggest and split_ions)
if only_biggest:
network = network.subgraph(max(networkx.connected_components(network), key=len)).copy()
if split_ions:
return [parse_smiles_internal(
network.subgraph(sub_network).copy(),
apply_paths=apply_paths,
parse_cis_trans=parse_cis_trans,
unknown_atom_is_dummy=unknown_atom_is_dummy
) for sub_network in networkx.connected_components(network)]
else:
return parse_smiles_internal(
network,
apply_paths=apply_paths,
parse_cis_trans=parse_cis_trans,
unknown_atom_is_dummy=unknown_atom_is_dummy
)
def parse_smiles_internal(
network: networkx.Graph,
apply_paths,
parse_cis_trans,
unknown_atom_is_dummy
):
nodes = []
neighbour_lists = {}
node_map = {}
for x, (i, node) in enumerate(network.nodes(data=True)):
nodes.append(node)
neighbour_lists[i] = []
node_map[i] = x
# print(node)
for x, y, data in network.edges(data=True):
stereo = data["ez_stereo"] if "ez_stereo" in data else None
if stereo is None:
neighbour_lists[x].append((y, data["order"], 0))
neighbour_lists[y].append((x, data["order"], 0))
elif stereo[0] == x:
neighbour_lists[x].append((y, data["order"], 1))
neighbour_lists[y].append((x, data["order"], -1))
else:
neighbour_lists[x].append((y, data["order"], -1))
neighbour_lists[y].append((x, data["order"], 1))
# find stereo paths
stereo_paths = []
if parse_cis_trans:
for i, neighbours in neighbour_lists.items():
if any(stereo != 0 for _, _, stereo in neighbours) and \
any(order == 2 for _, order, _ in neighbours):
starts = [(neighbour, stereo) for neighbour, order, stereo in neighbours if order == 1]
if nodes[node_map[i]]["element"] == "C":
if len(starts) != 2: continue
elif nodes[node_map[i]]["element"] == "N":
if len(starts) != 1: continue
starts.append((-1, 0))
else:
continue
starts.sort(key=lambda item: item[1])
internal = [i]
end_double = [neighbour for neighbour, order, stereo in neighbours if order == 2][0]
while len(neighbour_lists[end_double]) == 2 and \
all(order == 2 for _, order, _ in neighbour_lists[end_double]):
internal.append(end_double)
end_double = [neighbour for neighbour, order, stereo in neighbour_lists[end_double]
if neighbour != internal[-2]][0]
internal.append(end_double)
ends = [(neighbour, stereo) for neighbour, order, stereo in neighbour_lists[end_double] if order == 1]
if nodes[node_map[end_double]]["element"] == "C":
if len(ends) != 2: continue
elif nodes[node_map[end_double]]["element"] == "N":
if len(ends) != 1: continue
ends.append((-1, 0))
else:
continue
ends.sort(key=lambda item: item[1])
if starts[0][1] == starts[1][1]:
raise ValueError("Invalid stereochemistry: " + input)
if ends[0][1] == ends[1][1]:
if ends[0][1] == 0:
continue
raise ValueError("Invalid stereochemistry: " + input)
if 0 <= starts[0][0] < ends[0][0] >= 0:
stereo_paths.append((starts[0][0], *internal, ends[0][0]))
if 0 <= starts[1][0] < ends[1][0] >= 0:
stereo_paths.append((starts[1][0], *internal, ends[1][0]))
adjacency_matrix = [[0] * len(nodes) for _ in nodes]
edges: list[(int, int)] = []
if parse_cis_trans and apply_paths:
for path in stereo_paths:
start = path[0]
end = path[-1]
if any(x == end for x, _, _ in neighbour_lists[start]):
raise ValueError("Invalid stereochemistry: " + input)
neighbour_lists[start].append((end, -1, 0))
neighbour_lists[end].append((start, -1, 0))
for i, neighbours in neighbour_lists.items():
neighbours.sort(key=lambda item: item[0])
for j, (neighbour, order, stereo) in enumerate(neighbours):
adjacency_matrix[node_map[i]][node_map[neighbour]] = 1
edges.append((order, stereo))
return [{
"type": lookup_atom_type(node["element"], is_aromatic=node["aromatic"], unknown_is_dummy=unknown_atom_is_dummy),
} for node in nodes], adjacency_matrix, [{
"type": lookup_bond_type(order),
} for order, _ in edges], stereo_paths
test_inputs = """
N=C(N)NN=C(/C=C\c1ccc([N+](=O)[O-])o1)/C=C\c1ccc([N+](=O)[O-])o1
[As]
[Al+3].[Al+3].[Mo].[Mo].[O-2].[O-2].[O-2].[O-2].[O-2].[O-2].[O-2].[O-2].[O-2]
C[n+]1c(/C=C\c2ccc(O)c3ncccc23)[se]c2ccccc21
N#N
CN=C=O
Cu2+SO2−
[Cu+2].[O-]S(=O)(=O)[O-]
O=Cc1ccc(O)c(OC)c1
COc1cc(C=O)ccc1O
CC(=O)NCCC1=CNc2c1cc(OC)cc2
CC(=O)NCCc1c[nH]c2ccc(OC)cc12
CCc(c1)ccc2[n+]1ccc3c2[nH]c4c3cccc4
CCc1c[n+]2ccc3c4ccccc4[nH]c3c2cc1
CN1CCC[C@H]1c2ccc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
O=C=O
COC
C#N
CCO
[H][H]
C=CCC=CCO
C=C-C-C=C-C-O
OCC=CCC=C
CCN(CC)CC
CC(C)C(=O)O
C=CC(CCC)C(C(C)C)CCC
C12C3C4C1C5C4C3C25
O1CCCCC1N1CCCCC1
C1.C1
[12C]
[13C]
[C]
[13CH4]
F/C=C/C=C/C
F/C=C/C=CC
NC(C)(F)C(=O)O
NC(F)(C)C(=O)O
N[C@](C)(F)C(=O)O
N[C@@](F)(C)C(=O)O
N[C@@]([H])(C)C(=O)O
N[C@]([H])(C)C(=O)O
N[C@@H](C)C(=O)O
N[C@H](C)C(=O)O
N[C@H](C(=O)O)C
N[C@@H](C(=O)O)C
[H][C@](N)(C)C(=O)O
[H][C@@](N)(C)C(=O)O
[C@H](N)(C)C(=O)O
[C@@H](N)(C)C(=O)O
C[C@H]1CCCCO1
O1CCCC[C@@H]1C
C1=COC=C1
C1=CN=C[NH]C(=O)1
C1=CC=CC=C1
c1cocc1
c1cnc[nH]c(=O)1
c1ccccc1
n1ccccc1
O=n1ccccc1
[O-][n+]1ccccc1
Cn1cccc1
[nH]1cccc1
"""
if __name__ == '__main__':
for x, i in enumerate(items := test_inputs.split("\n")):
i = i.strip()
if i:
parse_smiles(i, only_biggest=True, unknown_atom_is_dummy=True)
print(x, "/", len(items), i)