-
Notifications
You must be signed in to change notification settings - Fork 5
/
run_Speed_up.py
155 lines (130 loc) · 5.08 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
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
parser = argparse.ArgumentParser(description='manual to this script')
parser.add_argument('--contigs', type=str, default = 'contigs.fa')
parser.add_argument('--len', type=int, default=8000)
parser.add_argument('--threads', type=int, default=8)
args = parser.parse_args()
if not os.path.exists("input"):
_ = os.makedirs("input")
else:
print("folder {0} exist... cleaning dictionary".format("input"))
if os.listdir("input"):
try:
_ = subprocess.check_call("rm -rf {0}".format("input"), shell=True)
_ = os.makedirs("input")
print("Dictionary cleaned")
except:
print("Cannot clean your folder... permission denied")
exit(1)
if not os.path.exists("pred"):
_ = os.makedirs("pred")
else:
print("folder {0} exist... cleaning dictionary".format("pred"))
if os.listdir("pred"):
try:
_ = subprocess.check_call("rm -rf {0}".format("pred"), shell=True)
_ = os.makedirs("pred")
print("Dictionary cleaned")
except:
print("Cannot clean your folder... permission denied")
exit(1)
if not os.path.exists("Split_files"):
_ = os.makedirs("Split_files")
else:
print("folder {0} exist... cleaning dictionary".format("Split_files"))
if os.listdir("Split_files"):
try:
_ = subprocess.check_call("rm -rf {0}".format("Split_files"), shell=True)
_ = os.makedirs("Split_files")
except:
print("Cannot clean your folder... permission denied")
exit(1)
try:
if os.path.exists('database/database.self-diamond.tab'):
print(f'Using preformatted DIAMOND database ({diamond_db}) ...')
else:
make_diamond_cmd = 'diamond makedb --threads 8 --in database/Caudovirales_protein.fasta -d database/database.dmnd'
print("Creating Diamond database...")
_ = subprocess.check_call(make_diamond_cmd, shell=True)
diamond_cmd = 'diamond blastp --threads 8 --sensitive -d database/database.dmnd -q database/Caudovirales_protein.fasta -o database/database.self-diamond.tab'
print("Running Diamond...")
_ = subprocess.check_call(diamond_cmd, shell=True)
diamond_out_fp = "database/database.self-diamond.tab"
database_abc_fp = "database/database.self-diamond.tab.abc"
_ = subprocess.check_call("awk '$1!=$2 {{print $1,$2,$11}}' {0} > {1}".format(diamond_out_fp, database_abc_fp), shell=True)
except:
print("create database failed")
exit(1)
#####################################################################
########################## Start Program ########################
#####################################################################
def special_match(strg, search=re.compile(r'[^ACGT]').search):
return not bool(search(strg))
cnt = 0
file_id = 0
records = []
for record in SeqIO.parse(args.contigs, 'fasta'):
if cnt !=0 and cnt%1000 == 0:
SeqIO.write(records, "Split_files/contig_"+str(file_id)+".fasta","fasta")
records = []
file_id+=1
cnt = 0
seq = str(record.seq)
seq = seq.upper()
if special_match(seq):
if len(record.seq) > args.len:
records.append(record)
cnt+=1
SeqIO.write(records, "Split_files/contig_"+str(file_id)+".fasta","fasta")
file_id+=1
for i in range(file_id):
cmd = "mv Split_files/contig_"+str(i)+".fasta input/"
try:
out = subprocess.check_call(cmd, shell=True)
except:
print("Moving file Error for file {0}".format("contig_"+str(i)))
continue
cmd = "python run_CNN.py"
try:
out = subprocess.check_call(cmd, shell=True)
except:
print("Pre-trained CNN Error for file {0}".format("contig_"+str(i)))
cmd = "rm input/*"
out = subprocess.check_call(cmd, shell=True)
continue
cmd = f"python run_KnowledgeGraph.py --threads {args.threads}"
try:
out = subprocess.check_call(cmd, shell=True)
except:
print("Knowledge Graph Error for file {0}".format("contig_"+str(i)))
cmd = "rm input/*"
out = subprocess.check_call(cmd, shell=True)
continue
cmd = "python run_GCN.py"
try:
out = subprocess.check_call(cmd, shell=True)
except:
print("GCN Error for file {0}".format("contig_"+str(i)))
cmd = "rm input/*"
out = subprocess.check_call(cmd, shell=True)
continue
# Clean files
cmd = "rm input/*"
out = subprocess.check_call(cmd, shell=True)
name_list = pd.read_csv("name_list.csv")
prediction = pd.read_csv("prediction.csv")
prediction = prediction.rename(columns={'contig_names':'idx'})
contig_to_pred = pd.merge(name_list, prediction, on='idx')
contig_to_pred.to_csv("pred/contig_"+str(i)+".csv", index = None)
cmd = "rm name_list.csv prediction.csv"
out = subprocess.check_call(cmd, shell=True)
cmd = "cat pred/* > final_prediction.csv"
out = subprocess.check_call(cmd, shell=True)