forked from nh2tran/DeepNovo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
deepnovo_worker_io.py
286 lines (214 loc) · 8.8 KB
/
deepnovo_worker_io.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
# 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 deepnovo_config
from deepnovo_cython_modules import process_spectrum
class WorkerIO(object):
"""TODO(nh2tran): docstring.
"""
def __init__(self, input_file, output_file=None):
"""TODO(nh2tran): docstring.
The input_file could be input_file or input_file_train/valid/test.
The output_file is None for train/valid/test cases.
During training we use two separate WorkerIO objects for train and valid.
"""
print("".join(["="] * 80)) # section-separating line
print("WorkerIO: __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.batch_size = deepnovo_config.batch_size
self.header_seq = deepnovo_config.FLAGS.header_seq
self.input_file = input_file
self.output_file = output_file
print("input_file = {0:s}".format(self.input_file))
print("output_file = {0:s}".format(self.output_file))
# keep the file handles open throughout the process to read/write batches
self.input_handle = None
self.output_handle = None
# store the file location of all spectra for random access
self.location_list = []
# split data into batches
self.location_batch_list = []
self.location_batch_count = 0
# record the status of spectra that have been read
self.spectrum_count = {"total": 0,
"read": 0,
"skipped": 0,
"skipped_mass": 0}
def close_input(self):
"""TODO(nh2tran): docstring."""
print("".join(["="] * 80)) # section-separating line
print("WorkerIO: close_input()")
self.input_handle.close()
def close_output(self):
"""TODO(nh2tran): docstring."""
print("".join(["="] * 80)) # section-separating line
print("WorkerIO: close_output()")
self.output_handle.close()
def get_spectrum(self, location_batch):
"""TODO(nh2tran): docstring."""
#~ print("".join(["="] * 80)) # section-separating line
#~ print("WorkerIO: get_spectrum()")
spectrum_list = []
for location in location_batch:
# parse a spectrum
(precursor_mz,
charge,
scan,
raw_sequence,
mz_list,
intensity_list) = self._parse_spectrum(location)
# skip if precursor_mass > MZ_MAX
precursor_mass = precursor_mz * charge - deepnovo_config.mass_H * charge
if precursor_mass > self.MZ_MAX:
self.spectrum_count["skipped"] += 1
self.spectrum_count["skipped_mass"] += 1
continue
self.spectrum_count["read"] += 1
# pre-process spectrum
(spectrum_holder,
spectrum_original_forward,
spectrum_original_backward) = process_spectrum(mz_list,
intensity_list,
precursor_mass)
# update dataset
spectrum = {"scan": scan,
"precursor_mass": precursor_mass,
"spectrum_holder": spectrum_holder,
"spectrum_original_forward": spectrum_original_forward,
"spectrum_original_backward": spectrum_original_backward}
spectrum_list.append(spectrum)
return spectrum_list
def get_location(self):
"""TODO(nh2tran): docstring."""
print("".join(["="] * 80)) # section-separating line
print("WorkerIO: get_location()")
location_list = []
keyword = "BEGIN IONS"
line = True
while line:
location = self.input_handle.tell()
line = self.input_handle.readline()
if keyword in line:
location_list.append(location)
self.location_list = location_list
self.spectrum_count["total"] = len(location_list)
def open_input(self):
"""TODO(nh2tran): docstring."""
print("".join(["="] * 80)) # section-separating line
print("WorkerIO: open_input()")
self.input_handle = open(self.input_file, 'r')
def open_output(self):
"""TODO(nh2tran): docstring."""
print("".join(["="] * 80)) # section-separating line
print("WorkerIO: open_output()")
self.output_handle = open(self.output_file, 'w')
self._print_prediction_header()
def split_location(self):
"""TODO(nh2tran): docstring."""
print("".join(["="] * 80)) # section-separating line
print("WorkerIO: split_location()")
location_batch_list = [self.location_list[i:(i+self.batch_size)]
for i in range(0,
self.spectrum_count["total"],
self.batch_size)]
self.location_batch_list = location_batch_list
self.location_batch_count = len(self.location_batch_list)
def write_prediction(self, predicted_batch):
"""TODO(nh2tran): docstring."""
#~ print("".join(["="] * 80)) # section-separating line
#~ print("WorkerIO: write_prediction()")
for predicted in predicted_batch:
scan = predicted["scan"]
if predicted["sequence"]:
predicted_sequence = ",".join(predicted["sequence"])
predicted_score = "{0:.2f}".format(predicted["score"])
predicted_position_score = ",".join([
"{0:.2f}".format(x) for x in predicted["position_score"]])
else: # if no peptide found, write empty sequence to the output file
predicted_sequence = ""
predicted_score = "-inf"
predicted_position_score = ""
predicted_row = "\t".join([scan,
predicted_sequence,
predicted_score,
predicted_position_score])
print(predicted_row, file=self.output_handle, end="\n")
def _parse_spectrum(self, location):
"""TODO(nh2tran): docstring."""
#~ print("".join(["="] * 80)) # section-separating line
#~ print("WorkerIO: _parse_spectrum()")
self.input_handle.seek(location)
# BEGIN IONS
line = self.input_handle.readline()
assert "BEGIN IONS" in line, "Error: wrong input BEGIN IONS"
precursor_mz, charge, scan, raw_sequence = self._parse_spectrum_header()
mz_list, intensity_list = self._parse_spectrum_ion()
return precursor_mz, charge, scan, raw_sequence, mz_list, intensity_list
def _parse_spectrum_header(self):
"""TODO(nh2tran): docstring."""
#~ print("".join(["="] * 80)) # section-separating line
#~ print("WorkerIO: _parse_spectrum_header()")
# header TITLE
line = self.input_handle.readline()
assert "TITLE=" in line, "Error: wrong input TITLE"
# header PEPMASS
line = self.input_handle.readline()
assert "PEPMASS=" in line, "Error: wrong input PEPMASS"
precursor_mz = float(re.split('=|\n', line)[1])
# header CHARGE
line = self.input_handle.readline()
assert "CHARGE=" in line, "Error: wrong input CHARGE"
charge = float(re.split('=|\+', line)[1])
# header SCANS
line = self.input_handle.readline()
assert "SCANS=" in line, "Error: wrong input SCANS"
scan = re.split('=|\n', line)[1]
# header RTINSECONDS
line = self.input_handle.readline()
assert "RTINSECONDS=" in line, "Error: wrong input RTINSECONDS"
# header SEQ
if self.header_seq:
line = self.input_handle.readline()
assert "SEQ=" in line, "Error: wrong input SEQ"
raw_sequence = re.split('=|\n', line)[1]
else:
raw_sequence = ""
return precursor_mz, charge, scan, raw_sequence
def _parse_spectrum_ion(self):
"""TODO(nh2tran): docstring."""
#~ print("".join(["="] * 80)) # section-separating line
#~ print("WorkerIO: _parse_spectrum_ion()")
# ion
mz_list = []
intensity_list = []
line = self.input_handle.readline()
while not "END IONS" in line:
mz, intensity = re.split(' |\n', line)[:2]
mz_float = float(mz)
intensity_float = float(intensity)
# skip an ion if its mass > MZ_MAX
if mz_float > self.MZ_MAX:
line = self.input_handle.readline()
continue
mz_list.append(mz_float)
intensity_list.append(intensity_float)
line = self.input_handle.readline()
return mz_list, intensity_list
def _print_prediction_header(self):
"""TODO(nh2tran): docstring."""
print("".join(["="] * 80)) # section-separating line
print("WorkerIO: _print_prediction_header()")
header_list = ["scan",
"predicted_sequence",
"predicted_score",
"predicted_position_score"]
header_row = "\t".join(header_list)
print(header_row, file=self.output_handle, end="\n")