-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_3D.py
63 lines (50 loc) · 1.69 KB
/
extract_3D.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
import h5py
import os
import numpy as np
import pandas as pd
from rdkit import Chem
from rdkit.Chem import AllChem
import subprocess
import typing
def conf_gen(smi, conf_number, smi_charge):
m = Chem.MolFromSmiles(smi)
m = Chem.AddHs(m)
print(smi)
params = AllChem.ETKDGv3()
cids = AllChem.EmbedMultipleConfs(m, numConfs=conf_number, params=params)
res = AllChem.MMFFOptimizeMoleculeConfs(m,
numThreads=40,
mmffVariant="MMFF94")
energy_low = min(res)
label = res.index(energy_low)
conf_mol = Chem.MolToXYZBlock(m, confId=label)
del cids
return conf_mol
def convert_pdb2xyz(filename: str, pdbfolder: str, xyzfolder: str) -> str:
cmd = [
'obabel', '-ixyz -opdb', f'{pdbfolder}/{filename}.pdb', '-O',
f'{xyzfolder}/{filename}.xyz'
] #obabel -ixyz -opdb input.pdb -O input.xyz
db = h5py.File("testdb.hdf5", "r")
IDs = list(db.keys())
for id in IDs:
group = db[id]
pdb_block = ""
xyz_block = ""
if "XTB optimized structure" in group.keys():
pdb_block = group["XTB optimized structure"][0].decode()
else:
if "MMFF94 optimized structure" in group.keys():
pdb_block = group["MMFF94 optimized structure"][0].decode()
else:
smi = group["SMILES"][0].decode()
xyz_block = conf_gen(smi, 500, 0)
if xyz_block != "":
with open("xyzs/" + id + ".xyz", "w") as f:
f.write(xyz_block)
else:
with open("pdbs/" + id + ".pdb", "w") as f:
f.write(pdb_block)
convert_pdb2xyz(filename=id, pdbfolder="pdbs", xyzfolder="xyzs")
del group
db.close()