-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain_and_predict.py
267 lines (228 loc) · 7.61 KB
/
train_and_predict.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import json
import logging
import numpy as np
from pandas import DataFrame, read_parquet
from mol2ccs.constants import ALL_ATOMS, ALLOWED_ADDUCTS, Parameter
from mol2ccs.graph import (
MyDataset,
convert_to_graph,
generate_coordinates,
get_smiles_atom_set,
)
from mol2ccs.model import (
load_model_from_file,
mol2ccs_model,
predict,
train,
)
from mol2ccs.utils import (
calculate_adduct_descriptors,
calculate_fingeprint,
calculate_metrics,
)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())
def wrapper_predict(
input_file,
parameter_path,
model_file_h5,
output_file,
is_evaluate=1,
coordinates_present=False,
coordinates_col_name="coordinates",
smiles_col_name="smiles",
adduct_col_name="adduct",
ccs_col_name="ccs",
ccs_type_col_name="ccs_type",
mol_type_col_name="mol_type",
dimer_col_name="dimer",
drugtax_col_name="drugtax",
):
file_data = read_parquet(input_file)
smiles_list, adduct, ccs, ccs_type, mol_type, dimer, drugtax = (
file_data[smiles_col_name].values,
file_data[adduct_col_name].values,
file_data[ccs_col_name].values,
file_data[ccs_type_col_name].values,
file_data[mol_type_col_name].values,
file_data[dimer_col_name].values,
file_data[drugtax_col_name].tolist(),
)
if coordinates_present:
coordinates = file_data[coordinates_col_name].values
logger.info(f"Read data: {len(smiles_list)}")
param = None
with open(parameter_path, "r") as file:
parameter_dict = json.load(file)
max_coor = parameter_dict["max_coor"]
min_coor = parameter_dict["min_coor"]
param = Parameter(
max_coor=max_coor,
min_coor=min_coor,
)
if not coordinates_present:
smiles_list, adduct, ccs, Coordinate = generate_coordinates(
smiles=smiles_list, adduct=adduct, ccs=ccs, all_atoms=ALL_ATOMS
)
logger.info("3D coordinates generated successfully ")
else:
Coordinate = coordinates
for i in range(len(Coordinate)):
Coordinate[i] = (np.array(Coordinate[i]) - param.min_coor) / (
param.max_coor - param.min_coor
)
adj, features, edge_features = convert_to_graph(
smi_list=smiles_list,
all_atoms=ALL_ATOMS,
coordinates=Coordinate,
)
dataset = MyDataset(features, adj, edge_features, ccs)
logger.info("Graph & Adduct dataset completed")
ECC_Model = load_model_from_file(model_file_h5)
logger.info("Model loading completed")
fingerprint_pred = [calculate_fingeprint(smiles=smi) for smi in smiles_list]
descriptors_pred = calculate_adduct_descriptors(smiles_list, adduct)
results = predict(
model=ECC_Model,
dataset=dataset,
descriptors=descriptors_pred,
adduct_pred=adduct,
ccs_type_pred=ccs_type,
mol_type_pred=mol_type,
dimer_pred=dimer,
fingerprint_pred=fingerprint_pred,
drugtax_pred=drugtax,
)
data = {
"smiles": smiles_list,
"adduct": adduct,
"ccs": ccs,
"pred_ccs": results,
}
df = DataFrame(data)
df.to_parquet(output_file, index=False)
logger.info("CCS predicted completed")
if is_evaluate == 1:
re_Metrics = calculate_metrics(ccs, results)
return re_Metrics
def wrapper_train(
ifile,
parameter_path,
ofile,
epochs,
batch_size,
verbose,
all_atoms=ALL_ATOMS,
adduct_set=ALLOWED_ADDUCTS,
dropout_rate=0.0,
coordinates_present=False,
coordinates_col_name="coordinates",
smiles_col_name="smiles",
adduct_col_name="adduct",
ccs_col_name="ccs",
ccs_type_col_name="ccs_type",
mol_type_col_name="mol_type",
dimer_col_name="dimer",
drugtax_col_name="drugtax",
):
"""
* Train
*
* Attributes
* ----------
* ifile : File path for storing the data of smiles and adduct
* ParameterPath : Save path of related data parameters
* ofile : File path where the model is stored
"""
# this was being initialized as an empty list if not provided
# but it shouldn't be done in the definition
all_atoms = [] if all_atoms is None else ALL_ATOMS
adduct_set = [] if adduct_set is None else ALLOWED_ADDUCTS
# Read the smiles adduct CCS in the file
file_data = read_parquet(ifile)
smiles_list, adduct, ccs, ccs_type, mol_type, dimer, drugtax = (
file_data[smiles_col_name].values,
file_data[adduct_col_name].values,
file_data[ccs_col_name].values,
file_data[ccs_type_col_name].values,
file_data[mol_type_col_name].values,
file_data[dimer_col_name].values,
file_data[drugtax_col_name].tolist(),
)
logger.info(f"Read data: {len(smiles_list)}")
if coordinates_present:
Coordinate = file_data[coordinates_col_name].values
# If the user does not enter the number of elements, then the default is the set of
# all elements in the training set
if len(all_atoms) == 0:
all_atoms = get_smiles_atom_set(
smiles_list
) # Calculate the set of elements used in the training set
"""1. Graph data generation"""
if not coordinates_present:
# 3D conformation of the input SMILES
smiles_list, adduct, ccs, Coordinate = generate_coordinates(
smiles_list, adduct, ccs, all_atoms
)
logger.info("3D coordinates generated successfully ")
else:
logger.info("3D coordinates read from file")
# Data normalization of the generated coordinate data
ALL_DATA = []
for i in Coordinate:
for ii in i:
ALL_DATA.append(ii[0])
ALL_DATA.append(ii[1])
ALL_DATA.append(ii[2])
max_coor, min_coor = np.max(ALL_DATA), np.min(ALL_DATA)
for i in range(len(Coordinate)):
Coordinate[i] = (np.array(Coordinate[i]) - min_coor) / (max_coor - min_coor)
# Adduct set
if len(adduct_set) == 0:
adduct_set = list(set(list(adduct)))
adduct_set.sort()
logger.info(f"All element types : {all_atoms}")
logger.info(f"All adduct types : {adduct_set}")
# Construct Graph from the input data
adj, features, edge_features = convert_to_graph(
smi_list=smiles_list,
all_atoms=all_atoms,
coordinates=Coordinate,
)
DataSet = MyDataset(features, adj, edge_features, ccs)
logger.info(f"Build graph data successfully. Dataset: {DataSet} len({len(DataSet)})")
"""2: Fingerprints"""
logger.info("Calculating fingerprints")
fingerprint_list = [calculate_fingeprint(smi) for smi in smiles_list]
"""3: Descriptors"""
logger.info("Calculating descriptors")
descriptors = calculate_adduct_descriptors(smiles_list, adduct)
# Storing parameters in objects
rw = Parameter(
max_coor=max_coor,
min_coor=min_coor,
)
# export json with parameters
with open(parameter_path, "w") as file:
json.dump(rw.__dict__, file)
"""4: Model training"""
# Production of models for training
ECC_Model = mol2ccs_model(dataset=DataSet, dropout_rate=dropout_rate)
# Training Model
ECC_Model = train(
Model=ECC_Model,
dataset=DataSet,
descriptors_train=descriptors,
fingerprint_train=fingerprint_list,
ccs_type_train=ccs_type,
mol_type_train=mol_type,
dimer_train=dimer,
drugtax_train=drugtax,
adduct_train=adduct,
epochs=epochs,
ofile=ofile,
batch_size=batch_size,
verbose=verbose,
)
return ECC_Model