-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
132 lines (114 loc) · 4.31 KB
/
preprocess.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
import os
import datamol as dm
import numpy as np
import pandas as pd
from tqdm import tqdm
import argparse
import json
import selfies as sf
from utils import MolecularFeatureExtractor
from precompute_3d import precompute_3d
from utils.descriptors import can_be_2d_input
from utils.molfeat import get_molfeat_transformer
from molecule.utils.tdc_dataset import get_dataset
parser = argparse.ArgumentParser(
description="Compute ",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--datasets",
type=str,
nargs="+",
default=[
"hERG",
],
)
parser.add_argument(
"--descriptors",
type=str,
nargs="+",
default=["ecfp", "maccs"],
required=False,
help="List of descriptors to compute",
)
parser.add_argument(
"--data-path",
type=str,
default="data",
required=False,
help="Path to the data folder",
)
def main():
args = parser.parse_args()
for dataset in args.datasets:
data_path = os.path.join(args.data_path, dataset)
if not os.path.exists(f"{data_path}/preprocessed.sdf"):
if os.path.exists(f"{data_path}_3d.sdf"):
print(f"Loading 3D conformers from data/{dataset}_3d.sdf")
mols, smiles = precompute_3d(None, dataset)
else:
df = get_dataset(dataset.replace("__", " "))
if "Drug" in df.columns:
smiles = df["Drug"].tolist()
else:
smiles = df["smiles"].tolist()
mols = None
mols, smiles = precompute_3d(smiles, dataset)
# Removing molecules that cannot be featurized
for t_name in ["usr", "electroshape", "usrcat"]:
transformer, thrD = get_molfeat_transformer(t_name)
feat, valid_id = transformer(mols, ignore_errors=True)
smiles = np.array(smiles)[valid_id]
mols = np.array(mols)[valid_id]
valid_smiles = []
valid_mols = []
for i, s in enumerate(tqdm(smiles, desc="Generating graphs")):
mol = mols[i]
# compute molecular weight and limit it under 1000
desc = dm.descriptors.compute_many_descriptors(mol)
if desc["mw"] > 1000:
continue
try:
_ = sf.encoder(s)
if can_be_2d_input(s, mols[i]) and not "." in s:
valid_smiles.append(s)
valid_mols.append(mols[i])
except Exception as e:
print(f"Error processing {s}: {e}")
continue
smiles = valid_smiles
mols = valid_mols
if not os.path.exists(f"{data_path}"):
os.makedirs(f"{data_path}")
pre_processed = pd.DataFrame({"smiles": smiles, "mols": mols})
dm.to_sdf(pre_processed, f"{data_path}/preprocessed.sdf", mol_column="mols")
# save the SMILES in a json file
with open(f"{data_path}/smiles.json", "w") as f:
json.dump(smiles, f)
else:
pre_processed = dm.read_sdf(
f"{data_path}/preprocessed.sdf", as_df=True, mol_column="mols"
)
smiles = pre_processed["smiles"].iloc[:, 0].tolist()
mols = pre_processed["mols"].tolist()
for desc in tqdm(args.descriptors, position=0, desc="Descriptors"):
for length in tqdm([1024], desc="Length", position=1, leave=False):
feature_extractor = MolecularFeatureExtractor(
device="cpu",
length=length,
dataset=dataset,
data_dir=args.data_path,
)
if not os.path.exists(f"{data_path}/{desc}_{length}.npy"):
descriptor = feature_extractor.get_features(
smiles, name=desc, mols=mols, feature_type="descriptor"
).numpy()
np.save(
f"{data_path}/{desc.replace('/','_')}_{length}.npy",
descriptor,
)
del descriptor
else:
print(f"{data_path}/{desc}_{length}.npy already exists")
if __name__ == "__main__":
main()