-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalivhu.py
517 lines (488 loc) · 20.9 KB
/
malivhu.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import argparse
parser = argparse.ArgumentParser(add_help=False,
usage="%(prog)s [-h] -iv INPUT_VIRUS -o OUTPUT -p PHASES [-ih INPUT_HUMAN] [-p4] [-v VIRUS]",
description="Malivhu - MAchine LearnIng for Virus classification and virus-HUman interaction prediction",
)
required = parser.add_argument_group("Required arguments")
required.add_argument("-iv", "--input_virus", required=True, type=str,
help="path to the input virus file")
required.add_argument("-o", "--output", required=True, type=str,
help="path to the output directory")
required.add_argument("-p", "--phases", required=True, type=int,
help="a value from 1 to 4. This number is the last phase to be executed. For example, if 3 is selected, Malivhu will execute phases 1, 2 and 3.")
mandatory4 = parser.add_argument_group("Required arguments if phase 4 is set to be executed")
mandatory4.add_argument("-ih", "--input_human", type=str,
help="path to the input human file. Only applies if phase 4 is set to be executed.")
mandatory4.add_argument("-v", "--virus", help="'cov1', 'cov2', 'mers'. Applies if the --phase4only (or -p4) flag is added.")
optional = parser.add_argument_group("Optional arguments")
optional.add_argument("-p4", "--phase4only", action="store_true",
help="flag for executing only phase 4. Only applies if phase 4 is set to be executed.")
other = parser.add_argument_group("Other arguments")
other.add_argument("-h", "--help", action="help", help="show this help message and exit")
args = parser.parse_args()
phase1 = True if args.phases >= 1 else False
phase2 = True if args.phases >= 2 else False
phase3 = True if args.phases >= 3 else False
phase4 = True if args.phases == 4 else False
if phase4:
if args.input_human == None:
parser.error("--input_human (-ih) is required when phase 4 is set to be executed.")
if args.phase4only and phase4:
phase1 = False
phase2 = False
phase3 = False
if args.virus == None:
parser.error("--virus (-v) is required when phase 4 is set to be executed alone.")
if args.virus not in ['cov1', 'cov2', 'mers']:
parser.error("--virus (-v) accepted values are 'cov1', 'cov2' and 'mers'.")
import os
import subprocess
import joblib
import re
import numpy as np
import tensorflow as tf
from Bio import SeqIO
def execCmd(cmd, type):
cmdList = cmd.split(" ")
res = subprocess.run(cmdList, capture_output=True)
resString = res.stdout.decode("utf-8")
if "Error" in resString:
print(resString.replace("Error", f"Error converting your {type} file"))
os._exit(0)
pattern = re.compile("[\W_]+")
folder = args.output
if not os.path.exists(folder):
os.makedirs(folder)
iFeaturePath = "./iFeature/"
virusSeqs = []
fastas = SeqIO.parse(args.input_virus, "fasta")
for rec in fastas:
fasta = [rec.name, str(rec.seq)]
virusSeqs.append(fasta)
if not args.phase4only:
with open(folder + "/VIRUS_" + pattern.sub("", rec.name) + ".fasta", "w") as f:
f.write(">" + rec.name + "\n" + str(rec.seq))
if phase4:
humanSeqs = []
fastas = SeqIO.parse(args.input_human, "fasta")
for rec in fastas:
fasta = [rec.name, str(rec.seq)]
humanSeqs.append(fasta)
if not args.phase4only:
with open(folder + "/HUMAN_" + pattern.sub("", rec.name) + ".fasta", "w") as f:
f.write(">" + rec.name + "\n" + str(rec.seq))
if phase1:
execCmd(f"python3 {iFeaturePath}/iFeature.py --file {args.input_virus} --out {folder}/DDEVirus.tsv --type DDE", "virus")
if phase2:
execCmd(f"python3 {iFeaturePath}/iFeature.py --file {args.input_virus} --out {folder}/DPCVirus.tsv --type DPC", "virus")
if phase3:
execCmd(f"python3 {iFeaturePath}/iFeature.py --file {args.input_virus} --out {folder}/CKSAAPVirus.tsv --type CKSAAP", "virus")
if phase4:
execCmd(f"python3 {iFeaturePath}/iFeature.py --file {args.input_virus} --out {folder}/CTDDVirus.tsv --type CTDD", "virus")
execCmd(f"python3 {iFeaturePath}/iFeature.py --file {args.input_virus} --out {folder}/PAACVirus.tsv --type PAAC", "virus")
execCmd(f"python3 {iFeaturePath}/iFeature.py --file {args.input_human} --out {folder}/CTDDHuman.tsv --type CTDD", "human")
execCmd(f"python3 {iFeaturePath}/iFeature.py --file {args.input_human} --out {folder}/CKSAAPHuman.tsv --type CKSAAP", "human")
execCmd(f"python3 {iFeaturePath}/iFeature.py --file {args.input_human} --out {folder}/PAACHuman.tsv --type PAAC", "human")
if phase1:
# Run phase 1
X = []
labels = []
with open(folder + "/DDEVirus.tsv", "r") as f:
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
labels.append(splits[0])
splits = np.array(splits[1:]).astype(float)
X.append(splits)
line = f.readline()
with open(folder + "/DPCVirus.tsv", "r") as f:
count = 0
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()[1:]
splits = np.array(splits).astype(float)
X[count] = np.append(X[count], splits)
line = f.readline()
count += 1
X = np.array(X).astype(np.float32)
scaler = joblib.load("models/scaler1.pkl")
X = scaler.transform(X)
model = tf.keras.models.load_model("models/Phase1.h5")
X = tf.convert_to_tensor(X)
X = tf.expand_dims(X, axis=-1)
y_pred = model.predict(X)
with open(folder + "/phase1.out", "w") as f:
f.write("PROTEIN\tnon-ssRNA(-)\tssRNA(-)\n")
for i in range(len(y_pred)):
f.write(labels[i] + "\t")
pred = y_pred[i]
for score in pred:
f.write(str(score) + "\t")
f.write("\n")
if phase2:
# Run phase 2
labelsAux = []
for i in range(len(y_pred)):
if y_pred.argmax(axis=1)[i] == 1:
labelsAux.append(labels[i])
X = []
labels = []
if len(labelsAux) == 0:
with open(folder + "/phase2.out", "w") as f:
#Terminate script if no sequences passed the previous phase
f.write("No sequences passed phase 1.")
os._exit(0)
with open(folder + "/CKSAAPVirus.tsv", "r") as f:
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
if splits[0] in labelsAux:
labels.append(splits[0])
splits = np.array(splits[1:]).astype(float)
X.append(splits)
line = f.readline()
X = np.array(X).astype(np.float32)
scaler = joblib.load("models/scaler2.pkl")
X = scaler.transform(X)
model = tf.keras.models.load_model("models/Phase2.h5")
X = tf.convert_to_tensor(X)
X = tf.expand_dims(X, axis=-1)
y_pred = model.predict(X)
with open(folder + "/phase2.out", "w") as f:
f.write("PROTEIN\tnon-Coronaviridae\tCoronaviridae\n")
for i in range(len(y_pred)):
f.write(labels[i] + "\t")
pred = y_pred[i]
for score in pred:
f.write(str(score) + "\t")
f.write("\n")
if phase3:
# Run phase 3
labelsAux = []
for i in range(len(y_pred)):
if y_pred.argmax(axis=1)[i] == 1:
labelsAux.append(labels[i])
X = []
labels = []
if len(labelsAux) == 0:
with open(folder + "/phase3.out", "w") as f:
#Terminate script if no sequences passed the previous phase
f.write("No sequences passed phase 2.")
os._exit(0)
with open(folder + "/CKSAAPVirus.tsv", "r") as f:
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
if splits[0] in labelsAux:
labels.append(splits[0])
splits = np.array(splits[1:]).astype(float)
X.append(splits)
line = f.readline()
X = np.array(X).astype(np.float32)
scaler = joblib.load("models/scaler3.pkl")
X = scaler.transform(X)
model = tf.keras.models.load_model("models/Phase3.h5")
X = tf.convert_to_tensor(X)
X = tf.expand_dims(X, axis=-1)
y_pred = model.predict(X)
with open(folder + "/phase3.out", "w") as f:
f.write("PROTEIN\tnon-SARS/MERS\tSARS\tMERS\n")
for i in range(len(y_pred)):
f.write(labels[i] + "\t")
pred = y_pred[i]
for score in pred:
f.write(str(score) + "\t")
f.write("\n")
# Run phase 4
if phase4:
X = []
labelsAuxSARS = []
labelsSARS1 = []
labelsSARS2 = []
labelsMERS = []
if phase3:
for i in range(len(y_pred)):
if y_pred.argmax(axis=1)[i] == 1:
labelsAuxSARS.append(labels[i])
for label in labelsAuxSARS:
virusClass = "sars1"
modLabel = pattern.sub("", label)
os.system("blastp -db ./db/SARS1DB -query " + folder + "/VIRUS_" + modLabel + ".fasta -out " + folder + "/VIRUS_" + modLabel + ".sars1.blast -outfmt 6 -num_threads 4")
os.system("blastp -db ./db/SARS2DB -query " + folder + "/VIRUS_" + modLabel + ".fasta -out " + folder + "/VIRUS_" + modLabel + ".sars2.blast -outfmt 6 -num_threads 4")
with open(folder + "/VIRUS_" + modLabel + ".sars1.blast") as f:
greatestScore = 0
greatestPct = 0.0
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
pct = float(splits[2])
score = int(splits[11])
if greatestScore < score:
greatestScore = score
greatestPct = pct
line = f.readline()
with open(folder + "/VIRUS_" + modLabel + ".sars2.blast") as f:
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
pct = float(splits[2])
score = int(splits[11])
if greatestScore < score or (greatestScore == score and greatestPct <= pct):
virusClass = "sars2"
break
line = f.readline()
if virusClass == "sars1":
labelsSARS1.append(label)
else:
labelsSARS2.append(label)
for i in range(len(y_pred)):
if y_pred.argmax(axis=1)[i] == 2:
labelsMERS.append(labels[i])
if len(labelsSARS1) == 0 and len(labelsSARS2) == 0 and len(labelsMERS) == 0:
with open(folder + "/phase3.out", "w") as f:
#Terminate script if no sequences passed the previous phase
f.write("No sequences passed phase 2.")
os._exit(0)
#SARS1
if len(labelsSARS1) > 0:
labelsVirus = []
XVirus = []
labels = []
X = []
with open(folder + "/CTDDVirus.tsv", "r") as f:
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
label = splits[0]
if label in labelsSARS1:
labelsVirus.append(label)
splits = np.array(splits[1:]).astype(float)
XVirus.append(splits)
line = f.readline()
for i in range(len(labelsVirus)):
with open(folder + "/CTDDHuman.tsv", "r") as f:
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
labels.append(labelsVirus[i] + "___" + splits[0])
splits = np.array(splits[1:]).astype(float)
Xpair = np.append(XVirus[i], splits)
X.append(Xpair)
line = f.readline()
X = np.array(X).astype(np.float32)
model = joblib.load("models/CoV1.pkl")
y_pred = model.predict_proba(X)
with open(folder + "/phase4.out", "a") as f:
f.write("PROTEIN\tNEGATIVE\tPOSITIVE\n")
for i in range(len(y_pred)):
f.write(labels[i] + "\t")
f.write("SARS1 \t")
pred = y_pred[i]
for score in pred:
f.write(str(score) + "\t")
f.write("\n")
#SARS2
if len(labelsSARS2) > 0:
labelsVirus = []
XVirus = []
labels = []
X = []
with open(folder + "/CKSAAPVirus.tsv", "r") as f:
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
label = splits[0]
if label in labelsSARS2:
labelsVirus.append(label)
splits = np.array(splits[1:]).astype(float)
XVirus.append(splits)
line = f.readline()
for i in range(len(labelsVirus)):
with open(folder + "/CKSAAPHuman.tsv", "r") as f:
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
labels.append(labelsVirus[i] + "___" + splits[0])
splits = np.array(splits[1:]).astype(float)
Xpair = np.append(XVirus[i], splits)
X.append(Xpair)
line = f.readline()
X = np.array(X).astype(np.float32)
scaler = joblib.load("models/scalerCoV2.pkl")
X = scaler.transform(X)
model = tf.keras.models.load_model("models/CoV2.h5")
X = tf.convert_to_tensor(X)
X = tf.expand_dims(X, axis=-1)
y_pred = model.predict(X)
with open(folder + "/phase4.out", "a") as f:
f.write("PROTEIN\tNEGATIVE\tPOSITIVE\n")
for i in range(len(y_pred)):
f.write(labels[i] + "\t")
f.write("SARS2 \t")
pred = y_pred[i]
for score in pred:
f.write(str(score) + "\t")
f.write("\n")
#MERS
if len(labelsMERS) > 0:
labelsVirus = []
XVirus = []
labels = []
X = []
with open(folder + "/PAACVirus.tsv", "r") as f:
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
label = splits[0]
if label in labelsMERS:
labelsVirus.append(label)
splits = np.array(splits[1:]).astype(float)
XVirus.append(splits)
line = f.readline()
for i in range(len(labelsVirus)):
with open(folder + "/PAACHuman.tsv", "r") as f:
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
labels.append(labelsVirus[i] + "___" + splits[0])
splits = np.array(splits[1:]).astype(float)
Xpair = np.append(XVirus[i], splits)
X.append(Xpair)
line = f.readline()
X = np.array(X).astype(np.float32)
model = joblib.load("models/MERS.pkl")
y_pred = model.predict_proba(X)
with open(folder + "/phase4.out", "a") as f:
f.write("PROTEIN\tNEGATIVE\tPOSITIVE\n")
for i in range(len(y_pred)):
f.write(labels[i] + "\t")
f.write("MERS \t")
pred = y_pred[i]
for score in pred:
f.write(str(score) + "\t")
f.write("\n")
elif args.virus == "cov1":
labelsVirus = []
XVirus = []
labels = []
X = []
with open(folder + "/CTDDVirus.tsv", "r") as f:
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
labelsVirus.append(splits[0])
splits = np.array(splits[1:]).astype(float)
XVirus.append(splits)
line = f.readline()
for i in range(len(labelsVirus)):
with open(folder + "/CTDDHuman.tsv", "r") as f:
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
labels.append(labelsVirus[i] + "___" + splits[0])
splits = np.array(splits[1:]).astype(float)
Xpair = np.append(XVirus[i], splits)
X.append(Xpair)
line = f.readline()
X = np.array(X).astype(np.float32)
model = joblib.load("models/CoV1.pkl")
y_pred = model.predict_proba(X)
with open(folder + "/phase4.out", "a") as f:
f.write("PROTEIN\tNEGATIVE\tPOSITIVE\n")
for i in range(len(y_pred)):
f.write(labels[i] + "\t")
f.write("SARS1 \t")
pred = y_pred[i]
for score in pred:
f.write(str(score) + "\t")
f.write("\n")
elif args.virus == "cov2":
labelsVirus = []
XVirus = []
labels = []
X = []
with open(folder + "/CKSAAPVirus.tsv", "r") as f:
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
labelsVirus.append(splits[0])
splits = np.array(splits[1:]).astype(float)
XVirus.append(splits)
line = f.readline()
for i in range(len(labelsVirus)):
with open(folder + "/CKSAAPHuman.tsv", "r") as f:
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
labels.append(labelsVirus[i] + "___" + splits[0])
splits = np.array(splits[1:]).astype(float)
Xpair = np.append(XVirus[i], splits)
X.append(Xpair)
line = f.readline()
X = np.array(X).astype(np.float32)
scaler = joblib.load("models/scalerCoV2.pkl")
X = scaler.transform(X)
model = tf.keras.models.load_model("models/CoV2.h5")
X = tf.convert_to_tensor(X)
X = tf.expand_dims(X, axis=-1)
y_pred = model.predict(X)
with open(folder + "/phase4.out", "a") as f:
f.write("PROTEIN\tNEGATIVE\tPOSITIVE\n")
for i in range(len(y_pred)):
f.write(labels[i] + "\t")
f.write("SARS2 \t")
pred = y_pred[i]
for score in pred:
f.write(str(score) + "\t")
f.write("\n")
elif predictVirus == "MERS":
labelsVirus = []
XVirus = []
labels = []
X = []
with open(folder + "/PAACVirus.tsv", "r") as f:
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
labelsVirus.append(splits[0])
splits = np.array(splits[1:]).astype(float)
XVirus.append(splits)
line = f.readline()
for i in range(len(labelsVirus)):
with open(folder + "/PAACHuman.tsv", "r") as f:
line = f.readline()
line = f.readline()
while line != None and line.strip() != "":
splits = line.split()
labels.append(labelsVirus[i] + "___" + splits[0])
splits = np.array(splits[1:]).astype(float)
Xpair = np.append(XVirus[i], splits)
X.append(Xpair)
line = f.readline()
X = np.array(X).astype(np.float32)
model = joblib.load("models/MERS.pkl")
y_pred = model.predict_proba(X)
with open(folder + "phase4.out", "a") as f:
f.write("PROTEIN\tNEGATIVE\tPOSITIVE\n")
for i in range(len(y_pred)):
f.write(labels[i] + "\t")
f.write("MERS \t")
pred = y_pred[i]
for score in pred:
f.write(str(score) + "\t")
f.write("\n")