-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_checking_functions.py
More file actions
304 lines (224 loc) · 11.6 KB
/
Copy patherror_checking_functions.py
File metadata and controls
304 lines (224 loc) · 11.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
293
294
295
296
297
298
299
300
301
302
303
304
'''Error Classes and Error Checking Functions for Predictor'''
import numpy as np
# ERROR CLASSES
# Error classes can be added here to easily keep track of error status codes
class APIError(Exception):
"""
Base class for all custom API errors.
This allows us to catch all our custom errors with a single handler.
"""
def __init__(self, message, status_code, error_key):
super().__init__(message)
self.message = message
self.status_code = status_code
self.error_key = error_key
class BadRequestError(APIError):
"""
For errors where the request is unacceptable (e.g. malformed JSON, missing keys).
Corresponds to the 'bad_prediction_request' key.
"""
def __init__(self, message="The request was unacceptable."):
super().__init__(message, status_code=400, error_key='bad_prediction_request')
class PredictionFailedError(APIError):
"""
For errors where the request was valid, but the model could not complete the prediction.
Corresponds to the 'prediction_request_failed' key.
"""
def __init__(self, message="The model prediction was incomplete."):
super().__init__(message, status_code=422, error_key='prediction_request_failed')
class ServerError(APIError):
"""
For backend issues (e.g. memory errors, unexpected crashes).
Corresponds to the 'server_error' key.
"""
def __init__(self, message="An unexpected issue occurred on the server."):
super().__init__(message, status_code=500, error_key='server_error')
# ------------------------
# ERROR CHECKING FUNCTIONS
# Model-specific check: "prediction_request_failed" error
def check_seqs_specifications(sequences, json_return_error_model):
"""
Check that sequences conform to model specs.
- Valid bases: "A", "T", "C", "G", "N"
- No empty sequences
"""
# max_length = int(5e9)
valid_bases = {"A", "T", "C", "G", "N"}
for seq_id, seq in sequences.items():
# if len(value) > max_length:
# json_return_error_model['prediction_request_failed'].append(f"length of a sequence in {key} is greater than {max_length}")
if not seq:
json_return_error_model["prediction_request_failed"].append(f"sequence '{seq_id}' is empty")
invalid_chars = set(seq.upper()) - valid_bases
if invalid_chars:
json_return_error_model['prediction_request_failed'].append(f"sequence '{seq_id}' has invalid character(s): {invalid_chars}")
return(json_return_error_model)
# check the the mandatory_keys exist in the .json files
def check_mandatory_keys(evaluator_keys, json_return_error):
mandatory_keys = ["readout", "prediction_tasks", "sequences"] # NOTE: "request" removed
evaluator_keys_set = set(evaluator_keys)
missing = list(sorted(set(mandatory_keys) - evaluator_keys_set))
if missing:
json_return_error['bad_prediction_request'].append(
f"The following mandatory top-level keys are missing from the JSON: {', '.join(missing)}"
)
return json_return_error
def check_key_values_readout(readout_value, json_return_error):
readout_options = ["point","track", "interaction_matrix"]
if readout_value not in readout_options:
json_return_error['bad_prediction_request'].append("readout requested is not recognized. Please choose from ['point', 'track', 'interaction_matrix']")
else:
pass
if isinstance(readout_value, str) == True:
pass
else:
json_return_error['bad_prediction_request'].append("'readout' value should be a string")
if type(readout_value) == list:
json_return_error['bad_prediction_request'].append("'readout' should only have 1 value")
else:
pass
return(json_return_error)
def check_prediction_task_mandatory_keys(prediction_tasks, json_return_error):
for index, prediction_task in enumerate(prediction_tasks):
mandatory_keys = ["name", "type", "cell_type", "species"]
# print(index, prediction_task)
task_keys = set(prediction_task.keys())
# print(task_keys)
missing = list(sorted(set(mandatory_keys) - task_keys))
if missing:
# Get the name, using index as fallback
task_identifier = prediction_task.get("name", f"at index {index}")
error_msg = (f"Mandatory keys missing from prediction_task '{task_identifier}': "
f"{', '.join(missing)}")
print(error_msg)
json_return_error['bad_prediction_request'].append(error_msg)
return json_return_error
def check_prediction_task_name(prediction_tasks, json_return_error):
#loop through object to check each array
for prediction_task in prediction_tasks:
if type(prediction_task['name']) == list:
json_return_error['bad_prediction_request'].append("'name' should only have 1 value")
else:
pass
if isinstance(prediction_task['name'], str) == True:
pass
else:
json_return_error['bad_prediction_request'].append("'name' value should be a string")
return(json_return_error)
def check_prediction_task_type(prediction_tasks, json_return_error):
#loop through object to check each array
for prediction_task in prediction_tasks:
print(prediction_task)
prediction_task_options = ["accessibility", "expression", "chromatin_confirmation"]
if type(prediction_task['type']) == list:
json_return_error['bad_prediction_request'].append("'type' should only have 1 value")
else:
if isinstance(prediction_task['type'], str) == True:
if prediction_task['type'] in prediction_task_options or prediction_task['type'].startswith('binding_'):
pass
else:
json_return_error['bad_prediction_request'].append("prediction type " + str(prediction_task['type']) + " is not recognized")
pass
else:
json_return_error['bad_prediction_request'].append("'type' value should be a string")
return(json_return_error)
def check_prediction_task_cell_type(prediction_tasks, json_return_error):
#loop through object to check each array
for prediction_task in prediction_tasks:
if type(prediction_task['cell_type']) == list:
json_return_error['bad_prediction_request'].append("'cell_type' should only have 1 value")
else:
if isinstance(prediction_task['cell_type'], str) == True:
pass
else:
json_return_error['bad_prediction_request'].append("'cell_type' value should be a string")
return(json_return_error)
def check_prediction_task_species(prediction_tasks, json_return_error):
#loop through object to check each array
for prediction_task in prediction_tasks:
if type(prediction_task['species']) == list:
json_return_error['bad_prediction_request'].append("'species' should only have 1 value")
else:
if isinstance(prediction_task['species'], str) == True:
pass
else:
json_return_error['bad_prediction_request'].append("'species' value should be a string")
return(json_return_error)
def check_prediction_task_scale(prediction_tasks, json_return_error):
#loop through object to check each array
for prediction_task in prediction_tasks:
if 'scale' in prediction_task:
if type(prediction_task['scale']) == list:
json_return_error['bad_prediction_request'].append("'scale' should only have 1 value")
else:
prediction_scale_options = ["linear", "log"]
if prediction_task['scale'] not in prediction_scale_options:
json_return_error['bad_prediction_request'].append("scale requested is not recognized. Please choose from ['log', 'linear']")
else:
pass
if isinstance(prediction_task['scale'], str) == True:
pass
else:
json_return_error['bad_prediction_request'].append("'scale' value should be a string")
else:
pass
return(json_return_error)
def check_prediction_ranges(prediction_ranges, sequences, json_return_error):
"""
Checks that prediction_ranges are formatted correctly.
Now includes checks for positive integers and start <= end.
"""
for key, value in prediction_ranges.items():
if not isinstance(value, list):
json_return_error['bad_prediction_request'].append(f"Values for '{key}' in 'prediction_ranges' must be in a list")
if not value:
continue
if len(value) != 2:
json_return_error['bad_prediction_request'].append(f"Range array for '{key}' in 'prediction_ranges' must have 2 elements")
if not all(isinstance(num, int) for num in value):
json_return_error['bad_prediction_request'].append(f"Values in '{key}' in 'prediction_ranges' must be integers")
start = value[0]
end = value[1]
if start < 0 or end < 0:
json_return_error['bad_prediction_request'].append(f"Invalid range for '{key}' in 'prediction_ranges': indices must be positive. Received [{start}, {end}]")
if start > end:
json_return_error['bad_prediction_request'].append(f"Invalid range for '{key}' in 'prediction_ranges': start index ({start}) cannot be greater than end index ({end}). Received [{start}, {end}]")
# UPDATED: Out-of-bounds check with a clearer message
seq_len = len(sequences.get(key, ''))
# Check start index bounds as well, just in case
if start >= seq_len or end >= seq_len:
# Handle the empty sequence case for a clean message
if seq_len == 0:
err_msg = f"Invalid range for '{key}': cannot specify a range for a non-existent or empty sequence."
else:
err_msg = f"Invalid range for '{key}': index is out of bounds. The maximum valid index for a sequence of length {seq_len} is {seq_len - 1}."
json_return_error['bad_prediction_request'].append(err_msg)
return json_return_error
##check that seqids have valid characters
## apparently this is done by default in .json loads
#it works for some but not all
#check that keys in sequences match those in prediction ranges
def check_seq_ids(prediction_ranges, sequences, json_return_error):
if prediction_ranges.keys() == sequences.keys():
pass
else:
json_return_error['bad_prediction_request'].append("sequence ids in prediction_ranges do not match those in sequences")
return(json_return_error)
def check_key_values_upstream_flank(upstream_seq, json_return_error):
if type(upstream_seq) == list:
json_return_error['bad_prediction_request'].append("'upstream_seq' should only have 1 value")
else:
if isinstance(upstream_seq, str) == True:
pass
else:
json_return_error['bad_prediction_request'].append("'upstream_seq' value should be a string")
return(json_return_error)
def check_key_values_downstream_flank(downstream_seq, json_return_error):
if type(downstream_seq) == list:
json_return_error['bad_prediction_request'].append("'downstream_seq' should only have 1 value")
else:
if isinstance(downstream_seq, str) == True:
pass
else:
json_return_error['bad_prediction_request'].append("'downstream_seq' value should be a string")
return(json_return_error)