forked from nh2tran/DeepNovo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deepnovo_worker_test.py
300 lines (241 loc) · 10.5 KB
/
deepnovo_worker_test.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
# Copyright 2017 Hieu Tran. All Rights Reserved.
#
# DeepNovo is publicly available for non-commercial uses.
# ==============================================================================
"""TODO(nh2tran): docstring."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import re
import sys
import numpy as np
import deepnovo_config
class WorkerTest(object):
"""TODO(nh2tran): docstring.
The WorkerTest should be stand-alone and separated from other workers.
"""
def __init__(self):
"""TODO(nh2tran): docstring."""
print("".join(["="] * 80)) # section-separating line
print("WorkerTest.__init__()")
# we currently use deepnovo_config to store both const & settings
# the settings should be shown in __init__() to keep track carefully
self.MZ_MAX = deepnovo_config.MZ_MAX
self.target_file = deepnovo_config.target_file
self.predicted_file = deepnovo_config.predicted_file
self.predicted_format = deepnovo_config.predicted_format
self.accuracy_file = deepnovo_config.accuracy_file
print("input_file = {0:s}".format(self.target_file))
print("predicted_file = {0:s}".format(self.predicted_file))
print("predicted_format = {0:s}".format(self.predicted_format))
print("accuracy_file = {0:s}".format(self.accuracy_file))
self.target_dict = {}
self.predicted_list = []
def test_accuracy(self, db_peptide_list=None):
"""TODO(nh2tran): docstring."""
print("".join(["="] * 80)) # section-separating line
print("WorkerTest.test_accuracy()")
# write the accuracy of predicted peptides
accuracy_file_handle = open(self.accuracy_file, 'w')
header_list = ["scan",
"target_sequence",
"predicted_sequence",
"predicted_score",
"recall_AA",
"predicted_len",
"target_len"]
header_row = "\t".join(header_list)
print(header_row, file=accuracy_file_handle, end="\n")
self._get_target()
target_count_total = len(self.target_dict)
target_len_total = sum([len(x) for x in self.target_dict.itervalues()])
# this part is tricky!
# some target peptides are reported by PEAKS DB but not found in
# db_peptide_list due to mistakes in cleavage rules.
# if db_peptide_list is given, we only consider those target peptides,
# otherwise, use all target peptides
target_dict_db = {}
if db_peptide_list is not None:
for scan, target in self.target_dict.iteritems():
target_simplied = target
# remove the extension 'mod' from variable modifications
target_simplied = ['M' if x=='Mmod' else x for x in target_simplied]
target_simplied = ['N' if x=='Nmod' else x for x in target_simplied]
target_simplied = ['Q' if x=='Qmod' else x for x in target_simplied]
if target_simplied in db_peptide_list:
target_dict_db[scan] = target
else:
print("target not found: ", target_simplied)
else:
target_dict_db = self.target_dict
target_count_db = len(target_dict_db)
target_len_db = sum([len(x) for x in target_dict_db.itervalues()])
# we also skip target peptides with precursor_mass > MZ_MAX
target_dict_db_mass = {}
for scan, peptide in target_dict_db.iteritems():
if self._compute_peptide_mass(peptide) <= self.MZ_MAX:
target_dict_db_mass[scan] = peptide
target_count_db_mass = len(target_dict_db_mass)
target_len_db_mass = sum([len(x) for x in target_dict_db_mass.itervalues()])
# note that the prediction has already skipped precursor_mass > MZ_MAX
self._get_predicted()
predicted_count_mass = len(self.predicted_list)
predicted_len_mass = sum([len(x["sequence"]) for x in self.predicted_list])
# we also skip predicted peptides whose scans are not in target_dict_db_mass
predicted_count_mass_db = 0
predicted_len_mass_db = 0
# the recall is calculated on remaining peptides
recall_AA_total = 0.0
recall_peptide_total = 0.0
for index, predicted in enumerate(self.predicted_list):
scan = predicted["scan"]
if scan in target_dict_db_mass:
target = target_dict_db_mass[scan]
target_len= len(target)
predicted_count_mass_db += 1
predicted_len= len(predicted["sequence"])
predicted_len_mass_db += predicted_len
predicted_AA_id = [deepnovo_config.vocab[x] for x in predicted["sequence"]]
target_AA_id = [deepnovo_config.vocab[x] for x in target]
recall_AA = self._match_AA_novor(target_AA_id, predicted_AA_id)
recall_AA_total += recall_AA
if recall_AA == target_len:
recall_peptide_total += 1
target_sequence = ",".join(target)
predicted_sequence = ",".join(predicted["sequence"])
predicted_score = "{0:.2f}".format(predicted["score"])
recall_AA = "{0:d}".format(recall_AA)
predicted_len = "{0:d}".format(predicted_len)
target_len = "{0:d}".format(target_len)
print_list = [scan,
target_sequence,
predicted_sequence,
predicted_score,
recall_AA,
predicted_len,
target_len]
print_row = "\t".join(print_list)
print(print_row, file=accuracy_file_handle, end="\n")
accuracy_file_handle.close()
print("target_count_total = {0:d}".format(target_count_total))
print("target_len_total = {0:d}".format(target_len_total))
print("target_count_db = {0:d}".format(target_count_db))
print("target_len_db = {0:d}".format(target_len_db))
print("target_count_db_mass: {0:d}".format(target_count_db_mass))
print("target_len_db_mass: {0:d}".format(target_len_db_mass))
print()
print("predicted_count_mass: {0:d}".format(predicted_count_mass))
print("predicted_len_mass: {0:d}".format(predicted_len_mass))
print("predicted_count_mass_db: {0:d}".format(predicted_count_mass_db))
print("predicted_len_mass_db: {0:d}".format(predicted_len_mass_db))
print()
print("recall_AA_total = {0:.4f}".format(recall_AA_total / target_len_total))
print("recall_AA_db = {0:.4f}".format(recall_AA_total / target_len_db))
print("recall_AA_db_mass = {0:.4f}".format(recall_AA_total / target_len_db_mass))
print("recall_peptide_total = {0:.4f}".format(recall_peptide_total / target_count_total))
print("recall_peptide_db = {0:.4f}".format(recall_peptide_total / target_count_db))
print("recall_peptide_db_mass = {0:.4f}".format(recall_peptide_total / target_count_db_mass))
print("precision_AA_mass_db = {0:.4f}".format(recall_AA_total / predicted_len_mass_db))
def _compute_peptide_mass(self, peptide):
"""TODO(nh2tran): docstring.
"""
#~ print("".join(["="] * 80)) # section-separating line ===
#~ print("WorkerDB: _compute_peptide_mass()")
peptide_mass = (deepnovo_config.mass_N_terminus
+ sum(deepnovo_config.mass_AA[aa] for aa in peptide)
+ deepnovo_config.mass_C_terminus)
return peptide_mass
def _get_predicted(self):
"""TODO(nh2tran): docstring."""
print("".join(["="] * 80)) # section-separating line
print("WorkerTest._get_predicted()")
predicted_list = []
with open(self.predicted_file, 'r') as handle:
# header
handle.readline()
for line in handle:
line_split = re.split('\t|\n', line)
predicted = {}
predicted["scan"] = line_split[0]
if line_split[1]:
predicted["sequence"] = re.split(',', line_split[1])
predicted["score"] = float(line_split[2])
predicted["position_score"] = [float(x)
for x in re.split(',', line_split[3])]
else: # empty sequence
predicted["sequence"] = []
predicted["score"] = -float("inf")
predicted["position_score"] = []
predicted_list.append(predicted)
self.predicted_list = predicted_list
def _get_target(self):
"""TODO(nh2tran): docstring."""
print("".join(["="] * 80)) # section-separating line
print("WorkerTest._get_target()")
target_dict = {}
with open(self.target_file, 'r') as handle:
for line in handle:
if "SCANS=" in line:
scan = re.split('=|\n', line)[1]
elif "SEQ=" in line:
raw_sequence = re.split('=|\n', line)[1]
peptide = self._parse_sequence(raw_sequence)
target_dict[scan] = peptide
else:
print("Error: wrong target format.")
sys.exit()
self.target_dict = target_dict
def _parse_sequence(self, raw_sequence):
"""TODO(nh2tran): docstring."""
#~ print("".join(["="] * 80)) # section-separating line
#~ print("WorkerTest._parse_sequence()")
raw_sequence_len = len(raw_sequence)
peptide = []
index = 0
while index < raw_sequence_len:
if raw_sequence[index] == "(":
if peptide[-1] == "C" and raw_sequence[index:index+8] == "(+57.02)":
peptide[-1] = "Cmod"
index += 8
elif peptide[-1] == 'M' and raw_sequence[index:index+8] == "(+15.99)":
peptide[-1] = 'Mmod'
index += 8
elif peptide[-1] == 'N' and raw_sequence[index:index+6] == "(+.98)":
peptide[-1] = 'Nmod'
index += 6
elif peptide[-1] == 'Q' and raw_sequence[index:index+6] == "(+.98)":
peptide[-1] = 'Qmod'
index += 6
else: # unknown modification
print("ERROR: unknown modification!")
print("raw_sequence = ", raw_sequence)
sys.exit()
else:
peptide.append(raw_sequence[index])
index += 1
return peptide
def _match_AA_novor(self, target, predicted):
"""TODO(nh2tran): docstring."""
#~ print("".join(["="] * 80)) # section-separating line
#~ print("WorkerTest._test_AA_match_novor()")
num_match = 0
target_len = len(target)
predicted_len = len(predicted)
target_mass = [deepnovo_config.mass_ID[x] for x in target]
target_mass_cum = np.cumsum(target_mass)
predicted_mass = [deepnovo_config.mass_ID[x] for x in predicted]
predicted_mass_cum = np.cumsum(predicted_mass)
i = 0
j = 0
while i < target_len and j < predicted_len:
if abs(target_mass_cum[i] - predicted_mass_cum[j]) < 0.5:
if abs(target_mass[i] - predicted_mass[j]) < 0.1:
#~ if decoder_input[index_aa] == output[index_aa]:
num_match += 1
i += 1
j += 1
elif target_mass_cum[i] < predicted_mass_cum[j]:
i += 1
else:
j += 1
return num_match