-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.py
49 lines (42 loc) · 1.42 KB
/
utils.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
import rdkit
from rdkit import Chem
from rdkit.Chem import rdmolfiles, rdmolops
import numpy as np
import openbabel as ob
def obsmitosmile(smi):
conv = ob.OBConversion()
conv.SetInAndOutFormats("smi", "can")
conv.SetOptions("K", conv.OUTOPTIONS)
mol = ob.OBMol()
conv.ReadString(mol, smi)
smile = conv.WriteString(mol)
smile = smile.replace('\t\n', '')
return smile
def smiles2adjoin(smiles,explicit_hydrogens=True,canonical_atom_order=False):
mol = Chem.MolFromSmiles(smiles)
if mol is None:
print('error')
mol = Chem.MolFromSmiles(obsmitosmile(smiles))
assert mol is not None, smiles + ' is not valid '
if explicit_hydrogens:
mol = Chem.AddHs(mol)
else:
mol = Chem.RemoveHs(mol)
if canonical_atom_order:
new_order = rdmolfiles.CanonicalRankAtoms(mol)
mol = rdmolops.RenumberAtoms(mol, new_order)
num_atoms = mol.GetNumAtoms()
atoms_list = []
for i in range(num_atoms):
atom = mol.GetAtomWithIdx(i)
atoms_list.append(atom.GetSymbol())
adjoin_matrix = np.eye(num_atoms)
# Add edges
num_bonds = mol.GetNumBonds()
for i in range(num_bonds):
bond = mol.GetBondWithIdx(i)
u = bond.GetBeginAtomIdx()
v = bond.GetEndAtomIdx()
adjoin_matrix[u,v] = 1.0
adjoin_matrix[v,u] = 1.0
return atoms_list,adjoin_matrix