-
Notifications
You must be signed in to change notification settings - Fork 24
/
predict_all.py
executable file
·196 lines (172 loc) · 6.71 KB
/
predict_all.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
#!/usr/bin/env python
import click as ck
import numpy as np
import pandas as pd
from keras.models import load_model
from subprocess import Popen, PIPE
import time
from aaindex import INVALID_ACIDS
MAXLEN = 1002
models = list()
funcs = ['cc', 'mf', 'bp']
@ck.command()
@ck.option('--in-file', '-i', help='Input FASTA file', required=True)
@ck.option('--chunk-size', '-cs', default=1000, help='Number of sequences to read at a time')
@ck.option('--out-file', '-o', default='results.tsv', help='Output result file')
@ck.option('--mapping-file', '-m', default='', help='Mapping file for embeddings database')
@ck.option('--threshold', '-t', default=0.3, help='Prediction threshold')
@ck.option('--batch-size', '-bs', default=1, help='Batch size for prediction model')
@ck.option('--include-long-seq', '-ils', is_flag=True, help='Include long sequences')
def main(in_file, chunk_size, out_file, mapping_file, threshold, batch_size, include_long_seq):
prot_ids = None
mapping = None
if mapping_file != '':
mapping = read_mapping(mapping_file)
prot_ids = {}
w = open(out_file, 'w')
for ids, sequences in read_fasta(in_file, chunk_size, include_long_seq):
if mapping is not None:
prot_ids = {}
for i, seq_id in enumerate(ids):
if seq_id in mapping:
prot_ids[mapping[seq_id]] = i
results = predict_functions(sequences, prot_ids, batch_size, threshold)
for i in range(len(ids)):
w.write(ids[i])
for res in results[i]:
w.write('\t' + res)
w.write('\n')
w.close()
def read_mapping(mapping_file):
mapping = {}
with open(mapping_file, 'r') as f:
for line in f:
it = line.strip().split()
mapping[it[0]] = it[1]
return mapping
def is_ok(seq):
for c in seq:
if c in INVALID_ACIDS:
return False
return True
def read_fasta(filename, chunk_size, include_long_seq):
seqs = list()
info = list()
seq = ''
inf = ''
with open(filename) as f:
for line in f:
line = line.strip()
if line.startswith('>'):
if seq != '':
if is_ok(seq):
if include_long_seq:
seqs.append(seq)
info.append(inf)
if len(info) == chunk_size:
yield (info, seqs)
seqs = list()
info = list()
elif len(seq) <= MAXLEN:
seqs.append(seq)
info.append(inf)
if len(info) == chunk_size:
yield (info, seqs)
seqs = list()
info = list()
else:
print(('Ignoring sequence {} because its length > 1002'
.format(inf)))
else:
print(('Ignoring sequence {} because of ambigious AA'
.format(inf)))
seq = ''
inf = line[1:].split()[0]
else:
seq += line
seqs.append(seq)
info.append(inf)
yield (info, seqs)
def get_data(sequences, prot_ids):
n = len(sequences)
data = np.zeros((n, 1000), dtype=np.float32)
embeds = np.zeros((n, 256), dtype=np.float32)
if prot_ids is None:
p = Popen(['diamond', 'blastp', '-d', 'data/embeddings',
'--max-target-seqs', '1', '--min-score', '60',
'--outfmt', '6', 'qseqid', 'sseqid'], stdin=PIPE, stdout=PIPE)
for i in range(n):
p.stdin.write(bytes('>' + str(i) + '\n' + sequences[i] + '\n', encoding='utf-8'))
p.stdin.close()
prot_ids = {}
if p.wait() == 0:
for line in p.stdout:
it = line.decode('utf-8').strip().split('\t')
if len(it) == 2:
prot_ids[it[1]] = int(it[0])
prots = embed_df[embed_df['accessions'].isin(list(prot_ids.keys()))]
for i, row in prots.iterrows():
embeds[prot_ids[row['accessions']], :] = row['embeddings']
for i in range(len(sequences)):
seq = sequences[i]
for j in range(min(MAXLEN, len(seq)) - gram_len + 1):
data[i, j] = vocab[seq[j: (j + gram_len)]]
return [data, embeds]
def predict(data, model, model_name, functions, threshold, batch_size):
n = data[0].shape[0]
result = list()
for i in range(n):
result.append(list())
predictions = model.predict(
data, batch_size=batch_size, verbose=1)
for i in range(n):
pred = (predictions[i] >= threshold).astype('int32')
for j in range(len(functions)):
if pred[j] == 1:
result[i].append(model_name + '_' + functions[j] + '|' + '%.2f' % predictions[i][j])
return result
def init_models(conf=None, **kwargs):
print('Init')
global models
ngram_df = pd.read_pickle('data/models/ngrams.pkl')
global embed_df
embed_df = pd.read_pickle('data/graph_new_embeddings.pkl')
global vocab
vocab = {}
global gram_len
for key, gram in enumerate(ngram_df['ngrams']):
vocab[gram] = key + 1
gram_len = len(ngram_df['ngrams'][0])
print(('Gram length:', gram_len))
print(('Vocabulary size:', len(vocab)))
threshold = 0.3
# sequences = ['MKKVLVINGPNLNLLGIREKNIYGSVSYEDVLKSISRKAQELGFEVEFFQSNHEGEIIDKIHRAYFEKVDAIIINPGAYTHYSYAIHDAIKAVNIPTIEVHISNIHAREEFRHKSVIAPACTGQISGFGIKSYIIALYALKEILD']
# data = get_data(sequences)
for onto in funcs:
model = load_model('data/models/model_%s.h5' % onto)
df = pd.read_pickle('data/models/%s.pkl' % onto)
functions = df['functions']
models.append((model, functions))
print('Model %s initialized.' % onto)
# result = predict(data, model, functions, threshold)
# print result
def predict_functions(sequences, prot_ids, batch_size, threshold):
if not models:
init_models()
print('Predictions started')
start_time = time.time()
data = get_data(sequences, prot_ids)
result = list()
n = len(sequences)
for i in range(n):
result.append([])
for i in range(len(models)):
model, functions = models[i]
print('Running predictions for model %s' % funcs[i])
res = predict(data, model, funcs[i], functions, threshold, batch_size)
for j in range(n):
result[j] += res[j]
print(('Predictions time: {}'.format(time.time() - start_time)))
return result
if __name__ == '__main__':
main()