Skip to content

Commit de8b940

Browse files
committed
Move more TRAPI checks up front. Remove deprecated query_options
1 parent e775618 commit de8b940

1 file changed

Lines changed: 13 additions & 38 deletions

File tree

cohd/cohd_trapi_15.py

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ def __init__(self, request):
7575
self._request = request
7676
self._max_results_per_input = CohdTrapi.default_max_results_per_input
7777
self._max_results = CohdTrapi.default_max_results
78-
self._local_oxo = CohdTrapi.default_local_oxo
7978
self._kg_nodes = {}
8079
self._knowledge_graph = {
8180
'nodes': {},
@@ -155,7 +154,16 @@ def _check_query_input(self):
155154
response = self._trapi_mini_response(TrapiStatusCode.NO_RESULTS, msg)
156155
self._invalid_query_response = response, 200
157156
return self._valid_query, self._invalid_query_response
158-
157+
158+
# Check if QNodes are null
159+
if nodes[0] is None or nodes[1] is None:
160+
self._valid_query = False
161+
msg = f'Null QNode found in query graph'
162+
self.log(msg, level=logging.ERROR)
163+
response = self._trapi_mini_response(TrapiStatusCode.NO_RESULTS, msg)
164+
self._invalid_query_response = response, 200
165+
return self._valid_query, self._invalid_query_response
166+
159167
# If client provided non-empty QNode constraints, respond with error code
160168
if nodes[0].get('constraints') or nodes[1].get('constraints'):
161169
self._valid_query = False
@@ -302,7 +310,7 @@ def _interpret_query(self):
302310
}
303311
self._log_level = log_level_enum.get(log_level, CohdTrapi.default_log_level)
304312

305-
# Check that the query input has the correct structure
313+
# Check that the query input has the correct structure and doesn't request unsupported TRAPI features
306314
input_check = self._check_query_input()
307315
if not input_check[0]:
308316
return input_check
@@ -354,16 +362,6 @@ def _interpret_query(self):
354362
self._confidence_interval = CohdTrapi.default_confidence_interval
355363
self._query_options['confidence_interval'] = CohdTrapi.default_confidence_interval
356364

357-
# Get the query_option for local_oxo
358-
self._local_oxo = self._query_options.get('local_oxo')
359-
if self._local_oxo is None or not isinstance(self._local_oxo, bool):
360-
self._local_oxo = CohdTrapi.default_local_oxo
361-
362-
# Get the query_option for maximum mapping distance
363-
self._mapping_distance = self._query_options.get('mapping_distance')
364-
if self._mapping_distance is None or not isinstance(self._mapping_distance, Number):
365-
self._mapping_distance = CohdTrapi.default_mapping_distance
366-
367365
# Get query_option for including only Biolink nodes
368366
self._biolink_only = self._query_options.get('biolink_only')
369367
if self._biolink_only is None or not isinstance(self._biolink_only, bool):
@@ -377,18 +375,9 @@ def _interpret_query(self):
377375

378376
# Get query information from query_graph
379377
self._query_graph = self._json_data['message']['query_graph']
380-
381-
# Check that the query_graph is supported by the COHD reasoner (1-hop query)
382-
edges = self._query_graph['edges']
383-
if len(edges) != 1:
384-
self._valid_query = False
385-
msg = f'{CohdTrapi._SERVICE_NAME} reasoner only supports 1-hop queries'
386-
self.log(msg, level=logging.WARNING)
387-
response = self._trapi_mini_response(TrapiStatusCode.NO_RESULTS, msg)
388-
self._invalid_query_response = response, 200
389-
return self._valid_query, self._invalid_query_response
390-
378+
391379
# Check if the edge type is supported by COHD Reasoner and how it should be processed
380+
edges = self._query_graph['edges']
392381
self._query_edge_key = list(edges.keys())[0] # Get first and only edge
393382
self._query_edge = edges[self._query_edge_key]
394383
self._query_edge_predicates = self._query_edge.get('predicates')
@@ -456,22 +445,8 @@ def _interpret_query(self):
456445
# Note: qnode_key refers to the key identifier for the qnode in the QueryGraph's nodes property, e.g., "n00"
457446
subject_qnode_key = self._query_edge['subject']
458447
subject_qnode = self._find_query_node(subject_qnode_key)
459-
if subject_qnode is None:
460-
self._valid_query = False
461-
msg = f'QNode id "{subject_qnode_key}" not found in query graph'
462-
self.log(msg, level=logging.ERROR)
463-
response = self._trapi_mini_response(TrapiStatusCode.NO_RESULTS, msg)
464-
self._invalid_query_response = response, 200
465-
return self._valid_query, self._invalid_query_response
466448
object_qnode_key = self._query_edge['object']
467449
object_qnode = self._find_query_node(object_qnode_key)
468-
if object_qnode is None:
469-
self._valid_query = False
470-
msg = f'QNode id "{object_qnode_key}" not found in query graph'
471-
self.log(msg, level=logging.ERROR)
472-
response = self._trapi_mini_response(TrapiStatusCode.NO_RESULTS, msg)
473-
self._invalid_query_response = response, 200
474-
return self._valid_query, self._invalid_query_response
475450

476451
# In COHD queries, concept_id_1 must be specified by ID. Figure out which QNode to use for concept_1
477452
node_ids = set()

0 commit comments

Comments
 (0)