forked from DIFACQUIM/HANNA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph_to_smiles.py
140 lines (120 loc) · 3.44 KB
/
Graph_to_smiles.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
from typing import Any
import torch
import torch_geometric
import numpy as np
x_map = {
'atomic_num':
list(range(0, 119)),
'chirality': [
'CHI_UNSPECIFIED',
'CHI_TETRAHEDRAL_CW',
'CHI_TETRAHEDRAL_CCW',
'CHI_OTHER',
'CHI_TETRAHEDRAL',
'CHI_ALLENE',
'CHI_SQUAREPLANAR',
'CHI_TRIGONALBIPYRAMIDAL',
'CHI_OCTAHEDRAL',
],
'degree':
list(range(0, 11)),
'formal_charge':
list(range(-5, 7)),
'num_hs':
list(range(0, 9)),
'num_radical_electrons':
list(range(0, 5)),
'hybridization': [
'UNSPECIFIED',
'S',
'SP',
'SP2',
'SP3',
'SP3D',
'SP3D2',
'OTHER',
],
'is_aromatic': [False, True],
'is_in_ring': [False, True],
}
e_map = {
'bond_type': [
'UNSPECIFIED',
'SINGLE',
'DOUBLE',
'TRIPLE',
'QUADRUPLE',
'QUINTUPLE',
'HEXTUPLE',
'ONEANDAHALF',
'TWOANDAHALF',
'THREEANDAHALF',
'FOURANDAHALF',
'FIVEANDAHALF',
'AROMATIC',
'IONIC',
'HYDROGEN',
'THREECENTER',
'DATIVEONE',
'DATIVE',
'DATIVEL',
'DATIVER',
'OTHER',
'ZERO',
],
'stereo': [
'STEREONONE',
'STEREOANY',
'STEREOZ',
'STEREOE',
'STEREOCIS',
'STEREOTRANS',
],
'is_conjugated': [False, True],
}
def to_smiles(data: 'torch_geometric.data.Data',
kekulize: bool = False) -> Any:
"""Converts a :class:`torch_geometric.data.Data` instance to a SMILES
string.
Args:
data (torch_geometric.data.Data): The molecular graph.
kekulize (bool, optional): If set to :obj:`True`, converts aromatic
bonds to single/double bonds. (default: :obj:`False`)
"""
from rdkit import Chem
mol = Chem.RWMol()
for i in range(data.num_nodes):
atom = Chem.Atom(data.x[i, 0].item())
atom.SetChiralTag(Chem.rdchem.ChiralType.values[data.x[i, 1].item()])
atom.SetFormalCharge(x_map['formal_charge'][data.x[i, 3].item()])
atom.SetNumExplicitHs(x_map['num_hs'][data.x[i, 4].item()])
atom.SetNumRadicalElectrons(
x_map['num_radical_electrons'][data.x[i, 5].item()])
atom.SetHybridization(
Chem.rdchem.HybridizationType.values[data.x[i, 6].item()])
atom.SetIsAromatic(data.x[i, 7].item())
mol.AddAtom(atom)
edges = [tuple(i) for i in data.edge_index.t().tolist()]
visited = set()
for i in range(len(edges)):
src, dst = edges[i]
if tuple(sorted(edges[i])) in visited:
continue
bond_type = Chem.BondType.values[data.edge_attr[i, 0].item()]
mol.AddBond(src, dst, bond_type)
# Set stereochemistry:
stereo = Chem.rdchem.BondStereo.values[data.edge_attr[i, 1].item()]
if stereo != Chem.rdchem.BondStereo.STEREONONE:
db = mol.GetBondBetweenAtoms(src, dst)
db.SetStereoAtoms(dst, src)
db.SetStereo(stereo)
# Set conjugation:
is_conjugated = bool(data.edge_attr[i, 2].item())
mol.GetBondBetweenAtoms(src, dst).SetIsConjugated(is_conjugated)
visited.add(tuple(sorted(edges[i])))
mol = mol.GetMol()
if kekulize:
Chem.Kekulize(mol)
Chem.SanitizeMol(mol)
Chem.AssignStereochemistry(mol)
return Chem.MolToSmiles(mol, isomericSmiles=True)