[b40915]: / unimol / utils / geom.py

Download this file

53 lines (50 with data), 2.0 kB

 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
import random
import numpy as np
import rdkit
import rdkit.Chem as Chem
import rdkit.Chem.AllChem as AllChem
import rdkit.Chem.Descriptors as Descriptors
import tqdm
def change_torsion(pointset, index, angle=None):
# pointset: (N, 3)
#return the new pointset
# angle: rad
# index: (2)
if angle is None:
angle = random.uniform(-np.pi, np.pi)
pointset = np.array(pointset)
#get rotate axis
axis = pointset[index[0][-1]] - pointset[index[1][0]]
axis = axis / np.linalg.norm(axis)
#get rotate matrix
cos = np.cos(angle)
sin = np.sin(angle)
R = np.array([[cos + axis[0] ** 2 * (1 - cos), axis[0] * axis[1] * (1 - cos) - axis[2] * sin, axis[0] * axis[2] * (1 - cos) + axis[1] * sin],
[axis[1] * axis[0] * (1 - cos) + axis[2] * sin, cos + axis[1] ** 2 * (1 - cos), axis[1] * axis[2] * (1 - cos) - axis[0] * sin],
[axis[2] * axis[0] * (1 - cos) - axis[1] * sin, axis[2] * axis[1] * (1 - cos) + axis[0] * sin, cos + axis[2] ** 2 * (1 - cos)]])
#rotate
pointset[index[1]] = np.dot(pointset[index[1]] - pointset[index[1][0]], R) + pointset[index[1][0]]
return pointset, angle
def gen_conformation(mol, num_conf=20, num_worker=8):
try:
mol = Chem.AddHs(mol)
AllChem.EmbedMultipleConfs(mol, numConfs=num_conf, numThreads=num_worker, pruneRmsThresh=0.1, maxAttempts=5, useRandomCoords=False)
AllChem.MMFFOptimizeMoleculeConfs(mol, numThreads=num_worker)
mol = Chem.RemoveHs(mol)
return mol
except:
return None
def RotatableBond(mol):
"""Get the rotatable bond index of a molecule.
Args:
mol: rdkit.Chem.rdchem.Mol
Returns:
rotatable_bond: list of tuple
"""
rotatable_bond = []
for bond in mol.GetBonds():
if bond.IsInRing():
continue
if bond.GetBondType() == Chem.rdchem.BondType.SINGLE:
rotatable_bond.append((bond.GetBeginAtomIdx(), bond.GetEndAtomIdx()))
return rotatable_bond