-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathverifyBenchmark.py
More file actions
executable file
·276 lines (246 loc) · 11.3 KB
/
verifyBenchmark.py
File metadata and controls
executable file
·276 lines (246 loc) · 11.3 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
#!/usr/bin/env python
"""
This script verifies the Limited-Knowledge(LK) and No-Knowledge(NK)
benchmark sets. The entry points for such verification are the
following two methods:
verify_LK_benchmark:
This method need to be invoked to verify LK-benchmark files.
The arguments for calling this method are the following:
t1_iea_handle: file handle to a t1_ea file
t1_exp_handle: file handle to a t1_exp file
t2_exp_handle: file handle to a t2_exp file
output_filename_LK_bpo: file name to a LK-bpo benchmark set
output_filename_LK_cco: file name to a LK-cco benchmark set
output_filename_LK_mfo: file name to a LK-mfo benchmark set
verify_NK_benchmark:
This method need to be invoked to verify NK-benchmark files. The
arguments for calling this method are the following:
t1_iea_handle: file handle to a t1_ea file
t1_exp_handle: file handle to a t1_exp file
t2_exp_handle: file handle to a t2_exp file
output_filename_LK_bpo: file name to a NK-bpo benchmark set
output_filename_LK_cco: file name to a NK-cco benchmark set
output_filename_LK_mfo: file name to a NK-mfo benchmark set
The following methods are invoked by the above two methods to create
required data structures and perform the verification:
create_iea_ann_dict:
This method builds a dictionary for <protein, GO ID> tuples
from t1_iea file.
create_exp_ann_dict:
This method builds three dictionaries in BPO, CCO, and MFO
categories for <protein, GO ID> tuples from a
t1_exp or t2_exp file.
check_LK_benchmark_creation(t1_iea_dict,
t1_xxo_dict,
t2_xxo_dict,
benchmark_fh):
This method verifies the benchmark entries in the benchmark file
passed by the file handle benchmark_fh.
Meaning of xxo: xxo is replaced runtime by bpo, cco, or mfo to
make this method specific to a certain type of benchmarks.
check_NK_benchmark_creation(t1_iea_dict,
t1_bpo_dict,
t1_cco_dict,
t1_mfo_dict,
t2_xxo_dict,
benchmark_fh):
This method verifies the benchmark entries in the benchmark file
passed by the file handle benchmark_fh.
Meaning of xxo: xxo is replaced runtime by bpo, cco, or mfo to
make this method specific to a certain type of benchmarks.
"""
import os.path
import sys
from collections import defaultdict
import FormatChecker as fc
def create_iea_ann_dict(goa_iea_handle):
"""
This method builds a dictionary for <protein, GO ID> tuples
from t1_iea file.
"""
# Initialize a dictionar which will later be populated with
# <protein, GO ID> tuples from t1_iea file:
dict_iea = defaultdict(lambda:set())
# Populate the dictionary for t1_iea with <protein, GO terms> as
# <key, values> pairs from the entries with NOn-Experimental Evidence
# at time t1:
for lines in goa_iea_handle:
cols = lines.strip().split('\t')
if len(cols) < 15:
continue
dict_iea[cols[1]].add(cols[4])
# Column 1: protein name, Column 4: GO ID
return dict_iea
def create_exp_ann_dict(goa_exp_handle):
"""
This method builds three dictionaries in BPO, CCO, and MFO categories
for <protein, GO ID> tuples from UniProt-GOA file without the header
string.
"""
# Initialize three dictionaries in BPO, CCO, and MFO ontology groups
# which will later be populated with <protein, GO ID> tuples from
# t1_exp file ontology groups:
dict_bpo = defaultdict(lambda:set())
dict_cco = defaultdict(lambda:set())
dict_mfo = defaultdict(lambda:set())
# Populate the three dictionaries for t1_exp with <protein, GO terms>
# as <key, values> pairs from the entries with Non-Experimental Evidence
# at time t1:
for lines in goa_exp_handle:
cols = lines.strip().split('\t')
if len(cols) < 15:
continue
if cols[8] == 'F': # Column 8: Ontology group
dict_mfo[cols[1]].add(cols[4])
# Column 1: protein name, Column 4: GO ID
elif cols[8] == 'P':
dict_bpo[cols[1]].add(cols[4])
elif cols[8] == 'C':
dict_cco[cols[1]].add(cols[4])
return (dict_bpo, dict_cco, dict_mfo)
def check_LK_benchmark_creation(t1_iea_dict,
t1_xxo_dict,
t2_xxo_dict,
benchmark_fh):
"""
This method verifies the benchmark entries in the benchmark file
passed by the file handle benchmark_fh.
Meaning of xxo: xxo is replaced runtime by bpo, cco, or mfo to make
this method specific to a certain type of benchmarks.
"""
err_msg = ''
for lines in benchmark_fh:
cols = lines.strip().split('\t')
if cols[0] not in t1_iea_dict:
err_msg = '\t\tan undesired protein ' + cols[0] + ' got selected ' + \
'in the benchmark file.'
break
elif cols[0] in t1_xxo_dict:
err_msg = '\t\tselected protein ' + cols[0] + ' in the ' + \
'benchmark file already had\n' + \
'\t\texperimental evidence at time t1.'
break
elif cols[0] not in t2_xxo_dict or cols[1] not in t2_xxo_dict[cols[0]]:
err_msg = '\t\tselected protein ' + cols[0] + ' in the ' + \
' benchmark file has not gainedi\n' + \
'\t\texperimental evidence at time t2.'
break
return err_msg
def verify_LK_benchmark(t1_iea_handle,
t1_exp_handle,
t2_exp_handle,
benchmark_fh,
ontType):
"""
This method verifies Limited-Knowledge benchmark sets.
"""
# Create a dictionary for the <protein, GO ID> tuples from t1_iea file:
t1_iea_dict = create_iea_ann_dict(t1_iea_handle)
# Create BPO, CCO and MFO dictionaries for the
# <protein, GO ID> tuples from t1_exp file:
t1_bpo_dict, t1_cco_dict, t1_mfo_dict = create_exp_ann_dict(t1_exp_handle)
# Create BPO, CCO and MFO dictionaries for the
# <protein, GO ID> tuples from t2_exp file:
t2_bpo_dict, t2_cco_dict, t2_mfo_dict = create_exp_ann_dict(t2_exp_handle)
# Check file format for LK_bpo benchmark file.
# LK_bpo is True when the filename is non-empty, file exists, file size
# is non-zero and file in correct format:
err_msg = '' # Error message holders
if ontType == 'BPO':
# Verify LK-BPO benchmarks
err_msg = check_LK_benchmark_creation(t1_iea_dict,
t1_bpo_dict,
t2_bpo_dict,
benchmark_fh)
elif ontType == 'CCO':
# Verify LK-CCO benchmarks
err_msg = check_LK_benchmark_creation(t1_iea_dict,
t1_cco_dict,
t2_cco_dict,
benchmark_fh)
elif ontType == 'MFO':
# Verify LK-MFO benchmarks
err_msg = check_LK_benchmark_creation(t1_iea_dict,
t1_mfo_dict,
t2_mfo_dict,
benchmark_fh)
return err_msg
def check_NK_benchmark_creation(t1_iea_dict,
t1_bpo_dict,
t1_cco_dict,
t1_mfo_dict,
t2_xxo_dict,
benchmark_fh):
"""
This method verifies the benchmark entries in the benchmark file
passed by the file handle benchmark_fh.
Meaning of xxo: xxo is replaced runtime by bpo, cco, or mfo to make
this method specific to a certain type of benchmarks.
"""
err_msg = ''
for lines in benchmark_fh:
cols = lines.strip().split('\t')
if cols[0] not in t1_iea_dict:
err_msg = '\t\tan undesired protein ' + cols[0] + \
' got selected in the benchmark file.'
break
elif cols[0] in t1_bpo_dict or \
cols[0] in t1_cco_dict or \
cols[0] in t1_mfo_dict:
err_msg = '\t\tselected protein ' + cols[0] + ' in the ' + \
'benchmark file already had\n' +\
'\t\texperimental evidence at t1.'
break
elif cols[0] not in t2_xxo_dict:
err_msg = '\t\tselected protein ' + cols[0] + ' in the '+ \
'benchmark file has not gained\n' + \
'\t\texperimental evidence at time t2.'
break
return err_msg
def verify_NK_benchmark(t1_iea_handle,
t1_exp_handle,
t2_exp_handle,
benchmark_fh,
ontType):
"""
This method verifies No-Knowledge benchmark sets.
"""
# Create a dictionary for the
# <protein, GO ID> tuples from t1_iea file:
t1_iea_dict = create_iea_ann_dict(t1_iea_handle)
# Create BPO, CCO and MFO dictionaries for the
# <protein, GO ID> tuples from t1_exp file:
t1_bpo_dict, t1_cco_dict, t1_mfo_dict = create_exp_ann_dict(t1_exp_handle)
# Create BPO, CCO and MFO dictionaries for the
# <protein, GO ID> tuples from t2_exp file:
t2_bpo_dict, t2_cco_dict, t2_mfo_dict = create_exp_ann_dict(t2_exp_handle)
err_msg = '' # Error message holders
if (ontType == 'BPO'):
# Verify NK-BPO benchmarks:
err_msg = check_NK_benchmark_creation(t1_iea_dict,
t1_bpo_dict,
t1_cco_dict,
t1_mfo_dict,
t2_bpo_dict,
benchmark_fh)
elif (ontType == 'CCO'):
# Verify NK-CCO benchmarks:
err_msg = check_NK_benchmark_creation(t1_iea_dict,
t1_bpo_dict,
t1_cco_dict,
t1_mfo_dict,
t2_cco_dict,
benchmark_fh)
elif (ontType == 'MFO'):
# Verify NK-MFO benchmarks:
err_msg = check_NK_benchmark_creation(t1_iea_dict,
t1_bpo_dict,
t1_cco_dict,
t1_mfo_dict,
t2_mfo_dict,
benchmark_fh)
return err_msg
if __name__ == "__main__":
print (sys.argv[0] + ':')
print (__doc__)
sys.exit(0)