-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrun_Speed_up.py
175 lines (144 loc) · 5.69 KB
/
run_Speed_up.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
import numpy as np
import pandas as pd
import os
import Bio
from Bio import SeqIO
import pandas as pd
import subprocess
import argparse
import re
#####################################################################
########################## Input Params ###########################
#####################################################################
parser = argparse.ArgumentParser(description='manual to this script')
parser.add_argument('--contigs', type=str, default = 'test_contigs.fa')
parser.add_argument('--len', type=int, default=8000)
parser.add_argument('--gpus', type=int, default = 0)
parser.add_argument('--mode', type=str, default='virus', choices =['virus', 'prokaryote'])
parser.add_argument('--model', type=str, default='pretrain', choices = ['pretrain', 'retrain'])
parser.add_argument('--topk', type=int, default=1)
parser.add_argument('--t', type=float, default=0.98)
inputs = parser.parse_args()
def check_folder(file_name):
if not os.path.exists(file_name):
_ = os.makedirs(file_name)
else:
print("folder {0} exist... cleaning dictionary".format(file_name))
if os.listdir(file_name):
try:
_ = subprocess.check_call(f"rm -rf {file_name}", shell=True)
_ = os.makedirs(file_name)
print("Dictionary cleaned")
except:
print("Cannot clean your folder... permission denied")
exit(1)
check_folder("input")
check_folder("pred")
check_folder("Split_files")
check_folder("tmp_pred")
check_folder("train_phage/")
#####################################################################
######################### processing ###########################
#####################################################################
for record in SeqIO.parse('dataset/nucl.fasta', 'fasta'):
_ = SeqIO.write(record, 'train_phage/'+record.id, 'fasta')
#####################################################################
######################### Start Program ###########################
#####################################################################
# split into sub files
cnt = 0
file_id = 0
records = []
for record in SeqIO.parse(inputs.contigs, 'fasta'):
if cnt !=0 and cnt%2000 == 0:
SeqIO.write(records, f"Split_files/contig_{file_id}.fasta","fasta")
records = []
file_id+=1
cnt = 0
seq = str(record.seq)
seq = seq.upper()
if len(record.seq) > inputs.len:
records.append(record)
cnt+=1
SeqIO.write(records, f"Split_files/contig_{file_id}.fasta","fasta")
file_id+=1
# run sub files
for i in range(file_id):
cmd = f"mv Split_files/contig_{i}.fasta input/"
try:
out = subprocess.check_call(cmd, shell=True)
except:
print(f"Moving file Error for file contig_{i}")
exit()
cmd = "python edge_virus_virus.py"
try:
out = subprocess.check_call(cmd, shell=True)
except:
print(f"phage_phage Error for file contig_{i}")
cmd = "rm input/*"
out = subprocess.check_call(cmd, shell=True)
exit()
cmd = f"python edge_virus_prokaryote.py --mode {inputs.mode}"
try:
out = subprocess.check_call(cmd, shell=True)
except:
print(f"phage_host Error for file contig_{i}")
cmd = "rm input/*"
out = subprocess.check_call(cmd, shell=True)
exit()
cmd = f"python create_feature.py --mode {inputs.mode}"
try:
check_folder("node_feature")
out = subprocess.check_call(cmd, shell=True)
except:
print(f"Pre-trained CNN Error for file contig_{i}")
cmd = "rm input/*"
out = subprocess.check_call(cmd, shell=True)
exit()
cmd = f"python multimodal_graph.py --mode {inputs.mode}"
try:
out = subprocess.check_call(cmd, shell=True)
except:
print(f"multimodal Graph Error for file contig_{i}")
cmd = "rm input/*"
out = subprocess.check_call(cmd, shell=True)
exit()
cmd = f"python run_Cherry.py --mode {inputs.mode} --model {inputs.model} --gpus {inputs.gpus} --topk {inputs.topk} --t {inputs.t}"
try:
out = subprocess.check_call(cmd, shell=True)
except:
print("GCN Error for file contig_{i}")
cmd = "rm input/*"
out = subprocess.check_call(cmd, shell=True)
exit()
# Clean files
cmd = "rm input/*"
out = subprocess.check_call(cmd, shell=True)
# load prediction
if inputs.mode == 'virus':
tmp_pred = pd.read_csv(f'tmp_pred/predict.csv')
name_list = pd.read_csv("name_list.csv")
prediction = tmp_pred.rename(columns={'contig':'idx'})
contig_to_pred = pd.merge(name_list, prediction, on='idx')
contig_to_pred.to_csv(f"pred/file_{i}.csv", index = None)
cmd = "rm name_list.csv"
out = subprocess.check_call(cmd, shell=True)
cmd = "rm tmp_pred/*"
out = subprocess.check_call(cmd, shell=True)
elif inputs.mode == 'prokaryote':
cmd = f"mv tmp_pred/predict.csv pred/file_{i}.csv"
out = subprocess.check_call(cmd, shell=True)
if inputs.mode == 'virus':
prediction_df = []
for i in range(file_id):
prediction_df.append(pd.read_csv(f'pred/file_{i}.csv'))
prediction_df = pd.concat(prediction_df)
prediction_df = prediction_df.drop(columns=['idx'])
prediction_df.drop(prediction_df.columns[len(prediction_df.columns)-1], axis=1, inplace=True)
prediction_df.to_csv(f'final_prediction.csv', index = None)
elif inputs.mode == 'prokaryote':
prediction_df = []
for i in range(file_id):
prediction_df.append(pd.read_csv(f'pred/file_{i}.csv'))
prediction_df = pd.concat(prediction_df)
prediction_df.to_csv(f'final_prediction.csv', index = None)