-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconvert_chemprop_ftune_predict.py
251 lines (195 loc) · 8.42 KB
/
convert_chemprop_ftune_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
import argparse
import subprocess
import warnings
import os
warnings.filterwarnings(action='ignore')
parser = argparse.ArgumentParser(description='chemprop train convert arguments')
parser.add_argument(
'--trainisc',
type=str,
default="input/train.csv",
metavar='TRAINISC',
help='the name of the train id smiles and compound file (default: input/train.csv)')
parser.add_argument(
'--name',
type=str,
default="new_family",
metavar='NAME',
help='the name of the your protein or protein family (default: new_family)')
parser.add_argument(
'--testisf',
type=str,
default="input/test.csv",
metavar='TESTISF',
help='the name of the test id and smiles file (default: input/test.csv)')
parser.add_argument(
'--sc',
type=str,
default="kinase",
metavar='SC',
help='the name of the source checkpoint (default: kinase)')
cwd = os.getcwd()
project_file_path = "{}/TransferLearning4DTI".format(cwd.split("TransferLearning4DTI")[0])
training_files_path = "{}/TransferLearning4DTI/training_files".format(cwd.split("TransferLearning4DTI")[0])
def get_compound_ids(FileName, smiles_lst):
compound_lst = []
with open(FileName) as f:
lines = f.readlines()
for line in lines:
feature_lst = line.rstrip('\n').split(",")
if "compound" in line:
continue
if feature_lst[1] in smiles_lst:
compound_lst.append(feature_lst[0])
f.close()
return compound_lst
#get trained smiles from the csv file
def get_smiles_chemprop(FileName):
compound_lst = []
with open(FileName) as f:
lines = f.readlines()
for line in lines:
feature_lst = line.rstrip('\n').split(",")
if "smiles" in line:
continue
compound_lst.append(feature_lst[0])
f.close()
return compound_lst
def read_smiles(FileName):
smilesCompoundDict = {}
compoundSmileDict = {}
smilesClassDict = {}
c = 0
with open(FileName) as f:
lines = f.readlines()
for line in lines:
if c == 0:
c += 1
continue
id_smiles_class = line.rstrip('\n').split(",")
smilesCompoundDict[id_smiles_class[1]] = id_smiles_class[0]
compoundSmileDict[id_smiles_class[0]] = id_smiles_class[1]
smilesClassDict[id_smiles_class[1]] = id_smiles_class[2]
return smilesCompoundDict, compoundSmileDict, smilesClassDict
def read_smiles_predict(FileName):
smilesCompoundDict = {}
compoundSmileDict = {}
c = 0
with open(FileName) as f:
lines = f.readlines()
for line in lines:
if c == 0:
c += 1
continue
id_smiles_class = line.rstrip('\n').split(",")
smilesCompoundDict[id_smiles_class[1]] = id_smiles_class[0]
compoundSmileDict[id_smiles_class[0]] = id_smiles_class[1]
return smilesCompoundDict, compoundSmileDict
def read_csv_convert_to_tsv(csv_file_name, tsv_file_name, compoundSmileDict, compound_lst):
with open(tsv_file_name, 'w') as wf:
with open(csv_file_name) as f:
lines = f.readlines()
for line in lines:
if "smiles" in line:
continue
else:
csv_line = line.rstrip('\n').split(",")
comp_id = compoundSmileDict[csv_line[0]]
if comp_id in compound_lst:
wf.write(comp_id + "\t" + "\t".join([str(float(dim)) for dim in csv_line[1:]]) + "\n")
f.close()
wf.close()
def write_comp_target_features_combined_binary(new_family_path, compoundSmilesDict, smilesClassDict):
wf = open(new_family_path + "/comp_targ_binary.tsv", "w", encoding='utf-8')
for key, value in compoundSmilesDict.items():
if smilesClassDict[value] == "1":
wf.write("dummy_protein" + "\t" + key + "\t1\n")
elif smilesClassDict[value] == "0":
wf.write("dummy_protein" + "\t" + key + "\t0\n")
wf.close()
def create_folds(length):
import random
dtiList = []
for i in range(0, length):
dtiList.append(i)
random.seed(69)
random.shuffle(dtiList)
return dtiList
def write_folds(FileName, dti_list):
f = open(FileName, "w")
f.write(str(dti_list))
f.close()
def write_id_smiles(file_path, compoundSmilesDict):
wf = open(file_path, "w", encoding='utf-8')
wf.write("compound_id,smiles\n")
for key, value in compoundSmilesDict.items():
wf.write(key + "," + value + "\n")
wf.close()
def write_smiles_class(file_path, smilesClassDict):
wf = open(file_path, "w", encoding='utf-8')
wf.write("smiles,class\n")
for key, value in smilesClassDict.items():
wf.write(key + "," + value + "\n")
wf.close()
def write_smiles(file_path, compoundSmilesDict):
wf = open(file_path, "w", encoding='utf-8')
wf.write("smiles\n")
for key, value in compoundSmilesDict.items():
wf.write(value + "\n")
wf.close()
if __name__ == '__main__':
args = parser.parse_args()
train_file = args.trainisc
name = args.name
source_checkpoint = args.sc
new_family_path = "{}/{}".format(training_files_path, name)
if not os.path.exists(new_family_path):
os.makedirs(new_family_path)
smilesCompoundDict, compoundSmilesDict, smilesClassDict = read_smiles(train_file)
id_smiles_file = train_file.split(".")[0] + "_id_smiles.csv"
write_id_smiles(id_smiles_file, compoundSmilesDict)
smiles_class_file = train_file.split(".")[0] + "_smiles_class.csv"
write_smiles_class(smiles_class_file, smilesClassDict)
if not os.path.exists(project_file_path + "/output/"):
os.makedirs(project_file_path + "/output/")
train_chemprop_file = project_file_path + "/output/" + train_file.split(".")[0].split("/")[1] + "_chemprop.csv"
print("chemprop_fingerprint is running")
cmdCommand = "chemprop_fingerprint --test_path " + smiles_class_file + " --checkpoint_path chemprop/" + source_checkpoint + "_checkpoints/model.pt " \
"--preds_path " + train_chemprop_file # specify your cmd command
# print(cmdCommand)
process = subprocess.Popen(cmdCommand.split())
output, error = process.communicate()
#create compound feature vector for the new family
if not os.path.exists(new_family_path + "/compound_feature_vectors/"):
os.makedirs(new_family_path + "/compound_feature_vectors/")
tsv_file_name = new_family_path + "/compound_feature_vectors/chemprop.tsv"
smiles_lst = get_smiles_chemprop(train_chemprop_file)
compound_lst = get_compound_ids(train_file, smiles_lst)
read_csv_convert_to_tsv(train_chemprop_file, tsv_file_name, smilesCompoundDict, compound_lst)
print("Training chemprop file is converted")
#create comp_targ_binary file for the new family
write_comp_target_features_combined_binary(new_family_path, compoundSmilesDict, smilesClassDict)
print("Compound target binary file is created")
#create train fold file for the new family
fold_list = create_folds(len(compound_lst))
if not os.path.exists(new_family_path + "/data/folds/"):
os.makedirs(new_family_path + "/data/folds/")
write_folds(new_family_path + "/data/folds/train_fold_setting1.txt", fold_list)
print("Training fold is created")
##############################################PREDICT#########################################
test_file = args.testisf
smilesCompoundDict, compoundSmilesDict = read_smiles_predict(test_file)
smiles_file = test_file.split(".")[0] + "_smiles.csv"
write_smiles(smiles_file, compoundSmilesDict)
test_chemprop_file = project_file_path + "/output/" + test_file.split(".")[0].split("/")[-1] + "_chemprop.csv"
print("chemprop_fingerprint is running")
cmdCommand = "chemprop_fingerprint --test_path " + smiles_file + " --checkpoint_path chemprop/" + source_checkpoint + "_checkpoints/model.pt " \
"--preds_path " + test_chemprop_file # specify your cmd command
# print(cmdCommand)
process = subprocess.Popen(cmdCommand.split())
output, error = process.communicate()
smiles_lst = get_smiles_chemprop(test_chemprop_file)
compound_lst = get_compound_ids(test_file, smiles_lst)
tsv_file_name = test_chemprop_file.split(".")[0] + ".tsv"
read_csv_convert_to_tsv(test_chemprop_file, tsv_file_name, smilesCompoundDict, compound_lst)
print("Test chemprop file is converted")