-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_outputs.py
More file actions
executable file
·293 lines (230 loc) · 10.6 KB
/
Copy pathparse_outputs.py
File metadata and controls
executable file
·293 lines (230 loc) · 10.6 KB
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
#!/usr/bin/env python3
import argparse
import sys
def main():
parser = argparse.ArgumentParser( description = 'Parse output produced by netMHCpan-4.0' )
parser.add_argument( '-f', '--in_file',
help = "Output file produced by netMHCpan-4.0 to parse"
)
parser.add_argument( '-w', '--weak',
help = (
"Floating-point threshold for a binding to be considered "
"weak. Anything less than this value will be considered a weak "
"binding if it is also greater than or equal "
"the strong threshold. (0.5, 0.2] "
)
)
parser.add_argument( '-s', '--strong',
help = (
"Floating-point threshold for a binding to be considered "
"strong. Anything less than this, "
"will be considered a strong binding. "
"(0, 0.2)"
)
)
parser.add_argument( '-o', '--output', default = "output.txt",
help = "File to write tab-delimited output to"
)
parser.add_argument( '-v', '--verbose', default = False,
action = "store_true",
help = "Flag for output to be written to STDOUT as well as output file"
)
args = parser.parse_args()
HEADER = "Pep-len\tAllele\tTotal\t#SB\t#WB\n"
output_parser = BindingOutputParser( args.in_file )
output_parser.read_file()
if not output_parser.file_found():
print( "ERROR: %s was either unable to be found or opened, exiting" % output_parser.get_file_name() )
sys.exit( 1 )
allele_info = output_parser.parse()
for current_allele in allele_info:
current_allele.set_weak_binding_thresh( float( args.weak ) )
current_allele.set_strong_binding_thresh( float( args.strong ) )
out_file = open( args.output, 'w' )
out_file.write( HEADER )
if args.verbose:
# Don't print a newline
print( HEADER[ :-1: ] )
for current_allele in allele_info:
out_file.write( str( current_allele ) + '\n' )
if args.verbose:
print( str( current_allele ) )
out_file.close()
class AlleleInfo:
def __init__( self, name, weak_binding_thresh = 0, strong_binding_thresh = 0 ):
self._name = name
self._weak_binding_thresh = weak_binding_thresh
self._strong_binding_thresh = strong_binding_thresh
self._bindings = list()
self._seq_names = set()
self._lengths = set()
self._header = "Pep-len\tAllele\tTotal\t#SB\t#WB"
class BindingInfo:
def __init__( self, attributes_list ):
self._pos = attributes_list[ 0 ]
self._peptide = attributes_list[ 1 ]
self._peptide_id = attributes_list[ 2 ]
self._core = attributes_list[ 3 ]
self._icore = attributes_list[ 4 ]
self._log = attributes_list[ 5 ]
self._nm = attributes_list[ 6 ]
self._rank = attributes_list[ 7 ]
self._peptide_length = len( attributes_list[ 1 ] )
self._attributes = attributes_list
def is_strong( self, strong_threshold, weak_threshold ):
return float( self._rank ) < strong_threshold
def is_weak( self, strong_threshold, weak_threshold ):
return float( self._rank ) <= weak_threshold and float( self._rank ) > strong_threshold
def __str__( self ):
out_str = "\t".join( self._attributes )
return out_str
def set_weak_binding_thresh( self, new_thresh ):
self._weak_binding_thresh = new_thresh
def set_strong_binding_thresh( self, new_thresh ):
self._strong_binding_thresh = new_thresh
def get_name( self ):
return self._name
def get_num_bindings_of_length( self, length ):
total = 0
for current_binding in self._bindings:
if current_binding._peptide_length == length:
total += 1
return total
def get_strong_bindings( self ):
out_list = list()
for current_binding in self._bindings:
if current_binding.is_strong( self._strong_binding_thresh, self._weak_binding_thresh ):
out_list.append( current_binding )
return out_list
def get_strong_bindings_by_length( self, length ):
strong_bindings = self.get_strong_bindings()
out_list = list()
for current_binding in strong_bindings:
if current_binding._peptide_length == length:
out_list.append( current_binding )
return out_list
def get_weak_bindings_by_length( self, length ):
weak_bindings = self.get_weak_bindings()
out_list = list()
for current_binding in weak_bindings:
if current_binding._peptide_length == length:
out_list.append( current_binding )
return out_list
def get_weak_bindings( self ):
out_list = list()
for current_binding in self._bindings:
if current_binding.is_weak( self._strong_binding_thresh, self._weak_binding_thresh ):
out_list.append( current_binding )
return out_list
def get_strong_bindings_per_seq( self ):
strong_bindings = self.get_strong_bindings()
out_bindings = {}
for current_binding in strong_bindings:
if current_binding not in out_bindings:
out_bindings[ current_binding._peptide_id ] = 0
out_bindings[ current_binding._peptide_id ] += 1
return out_bindings
def get_weak_bindings_per_seq( self ):
weak_bindings = self.get_weak_bindings()
out_bindings = {}
for current_binding in weak_bindings:
if current_binding not in out_bindings:
out_bindings[ current_binding._peptide_id ] = 0
out_bindings[ current_binding._peptide_id ] += 1
return out_bindings
@staticmethod
def _is_header( in_list ):
return in_list[ 0 ] == "Pos"
def add_info( self, string ):
split_string = string.split()
if not AlleleInfo._is_header( split_string ):
binding_info = self.BindingInfo( split_string )
self._seq_names.add( binding_info._peptide_id )
self._bindings.append( binding_info )
self._lengths.add( binding_info._peptide_length )
else:
self._header = "\t".join( split_string )
def __str__( self ):
output_string = ""
sorted_lengths = sorted( self._lengths )
for current_length in sorted_lengths:
num_weak_bindings = len( self.get_weak_bindings_by_length( current_length ) )
num_strong_bindings = len( self.get_strong_bindings_by_length( current_length ) )
total_bindings = num_weak_bindings + num_strong_bindings
output_string += "%d\t%s\t%d\t%s\t%s\n" % ( current_length,
self._name,
total_bindings,
num_strong_bindings,
num_weak_bindings,
)
# No newline at end of string
return output_string[:-1:]
class BindingOutputParser:
def __init__( self, in_file ):
self._file_name = in_file
self._lines_from_file = None
self._in_file_not_found = False
self._alleles_from_file = list()
def read_file( self ):
file_opened = False
try:
open_file = open( self._file_name, 'r' )
self._lines_from_file = open_file.readlines()
sucess = True
except ( IOError, OSError, TypeError ):
self._in_file_not_found = True
return file_opened
def get_file_name( self ):
return self._file_name
def file_found( self ):
return not self._in_file_not_found
def get_file_name( self ):
return self._file_name
def get_alleles( self ):
output_alleles = ""
if self._lines_from_file is not None:
output_alleles = self._lines_from_file[ 0 ]
output_alleles = output_alleles.strip().split()
return output_alleles
def add_allele( self, to_add):
allele_to_add = AlleleInfo( to_add )
self._alleles_from_file.append( allele_to_add )
def create_alleles( self ):
allele_names = self.get_alleles()
if len( allele_names ) > 0:
for current in allele_names:
self.add_allele( current )
def _parse_line( self, line_to_parse ):
line_split = line_to_parse.split()
output = list()
num_alleles = len( self._alleles_from_file )
num_cols_per_allele = 5
for current in range( num_alleles ):
current_list = list()
current_list.append( line_split[ 0 ] )
current_list.append( line_split[ 1 ] )
current_list.append( line_split[ 2 ] )
current_list.append( line_split[ ( current * num_cols_per_allele ) + 3 ] )
current_list.append( line_split[ ( current * num_cols_per_allele ) + 4 ] )
current_list.append( line_split[ ( current * num_cols_per_allele ) + 5 ] )
current_list.append( line_split[ ( current * num_cols_per_allele ) + 6 ] )
current_list.append( line_split[ ( current * num_cols_per_allele ) + 7 ] )
output.append( current_list )
return output
def parse( self ):
allele_info_list = list()
if self.file_found():
self.create_alleles()
allele_info_list = self._alleles_from_file
num_alleles = len( self._alleles_from_file )
for current_line in self._lines_from_file[ 2:: ]:
line_stripped = current_line.strip()
parsed_line = self._parse_line( current_line )
for current in range( len( parsed_line ) ):
allele_info_list[ current ].add_info( '\t'.join( parsed_line[ current ] ) )
return allele_info_list
def get_info( self ):
self.read_file()
return self.parse()
if __name__ == '__main__':
main()