-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprecompute_3d.py
98 lines (83 loc) · 2.57 KB
/
precompute_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
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
import os
import datamol as dm
from tqdm import tqdm
import argparse
from typing import List, Tuple
from molecule.utils.tdc_dataset import get_dataset
from utils.descriptors import can_be_2d_input
import pathos.multiprocessing as mp
def compute_3d(smiles: str):
if "." in smiles:
return None
mol = dm.to_mol(smiles)
if not can_be_2d_input(smiles, mol):
return None
desc = dm.descriptors.compute_many_descriptors(mol)
if desc["mw"] > 1000:
return None
try:
mol = dm.conformers.generate(
dm.to_mol(smiles),
align_conformers=True,
ignore_failure=True,
num_threads=8,
n_confs=5,
)
return mol
except Exception as e:
print(e)
return None
def precompute_3d(
smiles: List[str],
dataset_name: str = "tox21",
n_jobs: int = 4,
data_path: str = "data",
) -> Tuple[List[dm.Mol], List[str]]:
if dataset_name.endswith(".csv"):
dataset_name = dataset_name.replace(".csv", "")
if os.path.exists(f"{data_path}/{dataset_name}_3d.sdf"):
mols = dm.read_sdf(f"{data_path}/{dataset_name}_3d.sdf")
smiles = [dm.to_smiles(m, True, False) for m in mols]
return mols, smiles
mols = []
with mp.ProcessingPool(n_jobs) as pool:
for mol in tqdm(pool.uimap(compute_3d, smiles), total=len(smiles)):
if mol is not None:
mols.append(mol)
smiles = [dm.to_smiles(m, True, False) for m in mols]
dm.to_sdf(mols, f"{data_path}/{dataset_name}_3d.sdf")
return mols, smiles
parser = argparse.ArgumentParser(
description="Compute 3d conformers for a given dataset, and save them in data/<dataset_name>_3d.sdf",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--dataset",
type=str,
default="BindingDB_Kd",
required=False,
help="Dataset to use",
)
parser.add_argument(
"--n-jobs",
type=int,
default=4,
required=False,
help="Number of jobs to use",
)
parser.add_argument("--data-path", type=str, default="data")
if __name__ == "__main__":
args = parser.parse_args()
DATA_PATH = args.data_path
if args.dataset.endswith(".csv"):
import pandas as pd
df = pd.read_csv(f"{DATA_PATH}/{args.dataset}")
else:
df = get_dataset(args.dataset)
keys = df.keys()
if "Drug" in keys:
smiles = df["Drug"].drop_duplicates().tolist()
else:
smiles = df["smiles"].drop_duplicates().tolist()
mols = None
_ = precompute_3d(smiles, args.dataset, data_path=DATA_PATH)