Skip to content

Commit e775618

Browse files
committed
Move checks for unsupported TRAPI queries to beginning of interpretation process
1 parent 5fcaf54 commit e775618

2 files changed

Lines changed: 56 additions & 76 deletions

File tree

cohd/cohd_trapi_15.py

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ def _check_query_input(self):
146146
return self._valid_query, self._invalid_query_response
147147

148148
# Check the structure of the query graph. Should have 2 nodes and 1 edge (one-hop query)
149-
nodes = query_graph.get('nodes')
150-
edges = query_graph.get('edges')
149+
nodes = list(query_graph.get('nodes').values())
150+
edges = list(query_graph.get('edges').values())
151151
if nodes is None or len(nodes) != 2 or edges is None or len(edges) != 1:
152152
self._valid_query = False
153153
msg = 'Unsupported query. Only one-hop queries supported.'
@@ -156,6 +156,55 @@ def _check_query_input(self):
156156
self._invalid_query_response = response, 200
157157
return self._valid_query, self._invalid_query_response
158158

159+
# If client provided non-empty QNode constraints, respond with error code
160+
if nodes[0].get('constraints') or nodes[1].get('constraints'):
161+
self._valid_query = False
162+
description = f'{CohdTrapi._SERVICE_NAME} does not support QNode constraints'
163+
self.log(description, TrapiStatusCode.UNSUPPORTED_CONSTRAINT, logging.ERROR)
164+
response = self._trapi_mini_response(TrapiStatusCode.UNSUPPORTED_CONSTRAINT, description)
165+
self._invalid_query_response = response, 200
166+
return self._valid_query, self._invalid_query_response
167+
if edges[0].get("attribute_constraints"):
168+
self._valid_query = False
169+
description = f'{CohdTrapi._SERVICE_NAME} does not support QEdge attribute constraints'
170+
self.log(description, TrapiStatusCode.UNSUPPORTED_ATTR_CONSTRAINT, logging.ERROR)
171+
response = self._trapi_mini_response(TrapiStatusCode.UNSUPPORTED_ATTR_CONSTRAINT, description)
172+
self._invalid_query_response = response, 200
173+
return self._valid_query, self._invalid_query_response
174+
if edges[0].get("qualifier_constraints"):
175+
self._valid_query = False
176+
description = f'{CohdTrapi._SERVICE_NAME} does not support QEdge qualifier constraints'
177+
self.log(description, TrapiStatusCode.UNSUPPORTED_QUAL_CONSTRAINT, logging.ERROR)
178+
response = self._trapi_mini_response(TrapiStatusCode.UNSUPPORTED_QUAL_CONSTRAINT, description)
179+
self._invalid_query_response = response, 200
180+
return self._valid_query, self._invalid_query_response
181+
182+
# If client specifies unsupported set_interpretation (ALL or MANY), respond with error code
183+
if nodes[0].get('set_interpretation') in CohdTrapi150.unsupported_set_interpretation or \
184+
nodes[1].get('set_interpretation') in CohdTrapi150.unsupported_set_interpretation:
185+
self._valid_query = False
186+
description = f'{CohdTrapi._SERVICE_NAME} only supports QNode set_interpretation of {CohdTrapi150.supported_set_interpretation}'
187+
self.log(description, TrapiStatusCode.UNSUPPORTED_SET_INTERPRETATION, logging.ERROR)
188+
response = self._trapi_mini_response(TrapiStatusCode.UNSUPPORTED_SET_INTERPRETATION, description)
189+
self._invalid_query_response = response, 200
190+
return self._valid_query, self._invalid_query_response
191+
192+
# Check to see if cohd doesn't recognize any properties
193+
qnode_properties = {'ids','categories', 'set_interpretation', 'constraints'}
194+
unrec_properties = (set(nodes[0].keys()) | (set(nodes[1].keys()))) - qnode_properties
195+
if unrec_properties:
196+
description = f'{CohdTrapi._SERVICE_NAME} does not recognize the following node properties: ' \
197+
f'{", ".join(unrec_properties)}. {CohdTrapi._SERVICE_NAME} will ignore these properties.'
198+
self.log(description, level=logging.WARNING)
199+
200+
qedge_properties = {'knowledge_type', 'predicates', 'subject', 'object', 'attribute_constraints',
201+
'qualifier_constraints'}
202+
unrec_properties = set(edges[0].keys()) - qedge_properties
203+
if unrec_properties:
204+
description = f'{CohdTrapi._SERVICE_NAME} does not recognize the following edge properties: ' \
205+
f'{", ".join(unrec_properties)}. {CohdTrapi._SERVICE_NAME} will ignore these properties.'
206+
self.log(description, level=logging.WARNING)
207+
159208
# Check the workflow. Should be at most a single lookup operation
160209
workflow = self._json_data.get('workflow')
161210
if workflow and type(workflow) is list:
@@ -227,7 +276,7 @@ def _interpret_query(self):
227276
True if input is valid, otherwise (False, message)
228277
"""
229278
# Log that TRAPI 1.4 was called because there's no clear indication otherwise
230-
logging.debug('Query issued against TRAPI 1.4')
279+
logging.debug(f'Query issued against TRAPI {CohdTrapi150.schema_version}')
231280

232281
try:
233282
self._json_data = self._request.get_json()
@@ -552,62 +601,6 @@ def _interpret_query(self):
552601
self.log(f'The following categories were not recognized in Biolink {bm_version}: {unrecognized_cats}',
553602
level=logging.WARNING)
554603

555-
# If client provided non-empty QNode constraints, respond with error code
556-
if concept_1_qnode.get('constraints') or concept_2_qnode.get('constraints'):
557-
self._valid_query = False
558-
description = f'{CohdTrapi._SERVICE_NAME} does not support QNode constraints'
559-
self.log(description, TrapiStatusCode.UNSUPPORTED_CONSTRAINT, logging.ERROR)
560-
response = self._trapi_mini_response(TrapiStatusCode.UNSUPPORTED_CONSTRAINT, description)
561-
self._invalid_query_response = response, 200
562-
return self._valid_query, self._invalid_query_response
563-
if self._query_edge.get("attribute_constraints"):
564-
self._valid_query = False
565-
description = f'{CohdTrapi._SERVICE_NAME} does not support QEdge attribute constraints'
566-
self.log(description, TrapiStatusCode.UNSUPPORTED_ATTR_CONSTRAINT, logging.ERROR)
567-
response = self._trapi_mini_response(TrapiStatusCode.UNSUPPORTED_ATTR_CONSTRAINT, description)
568-
self._invalid_query_response = response, 200
569-
return self._valid_query, self._invalid_query_response
570-
if self._query_edge.get("qualifier_constraints"):
571-
self._valid_query = False
572-
description = f'{CohdTrapi._SERVICE_NAME} does not support QEdge qualifier constraints'
573-
self.log(description, TrapiStatusCode.UNSUPPORTED_QUAL_CONSTRAINT, logging.ERROR)
574-
response = self._trapi_mini_response(TrapiStatusCode.UNSUPPORTED_QUAL_CONSTRAINT, description)
575-
self._invalid_query_response = response, 200
576-
return self._valid_query, self._invalid_query_response
577-
578-
# If client specifies unsupported set_interpretation (ALL or MANY), respond with error code
579-
if concept_1_qnode.get('set_interpretation') in CohdTrapi150.unsupported_set_interpretation or \
580-
concept_2_qnode.get('set_interpretation') in CohdTrapi150.unsupported_set_interpretation:
581-
self._valid_query = False
582-
description = f'{CohdTrapi._SERVICE_NAME} only supports QNode set_interpretation of {CohdTrapi150.supported_set_interpretation}'
583-
self.log(description, TrapiStatusCode.UNSUPPORTED_SET_INTERPRETATION, logging.ERROR)
584-
response = self._trapi_mini_response(TrapiStatusCode.UNSUPPORTED_SET_INTERPRETATION, description)
585-
self._invalid_query_response = response, 200
586-
return self._valid_query, self._invalid_query_response
587-
588-
# Check to see if cohd doesn't recognize any properties
589-
qnode_properties = {'ids','categories', 'set_interpretation', 'constraints'}
590-
qedge_properties = {'knowledge_type', 'predicates', 'subject', 'object', 'attribute_constraints',
591-
'qualifier_constraints'}
592-
sep = ', '
593-
unrec_properties = set(concept_1_qnode.keys()) - qnode_properties
594-
if unrec_properties:
595-
description = f'{CohdTrapi._SERVICE_NAME} does not recognize the following properties: ' \
596-
f'{sep.join(unrec_properties)}. {CohdTrapi._SERVICE_NAME} will ignore these properties.'
597-
self.log(description, level=logging.WARNING)
598-
599-
unrec_properties = set(concept_2_qnode.keys()) - qnode_properties
600-
if unrec_properties:
601-
description = f'{CohdTrapi._SERVICE_NAME} does not recognize the following properties: ' \
602-
f'{sep.join(unrec_properties)}. {CohdTrapi._SERVICE_NAME} will ignore these properties.'
603-
self.log(description, level=logging.WARNING)
604-
605-
unrec_properties = set(self._query_edge.keys()) - qedge_properties
606-
if unrec_properties:
607-
description = f'{CohdTrapi._SERVICE_NAME} does not recognize the following properties: ' \
608-
f'{sep.join(unrec_properties)}. {CohdTrapi._SERVICE_NAME} will ignore these properties.'
609-
self.log(description, level=logging.WARNING)
610-
611604
# Get concept_id_1. QNode IDs is a list.
612605
self._concept_1_omop_ids = list()
613606
found = False

cohd/query_cohd_mysql.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def query_db_finalize(conn, cursor, json_return):
9696
conn.close()
9797

9898
json_return = {"results": json_return}
99-
return jsonify(json_return)
99+
return json_return
100100

101101

102102
def query_db_datasets():
@@ -239,20 +239,7 @@ def query_db(service, method, args):
239239
logging.debug(msg=f"Service: {service}; Method: {method}, Query: {query}")
240240

241241
if service == 'omop':
242-
# e.g. /api/v1/query?service=omop&meta=findConceptIDs&q=cancer
243-
if method == 'findConceptIDs':
244-
dataset_id = get_arg_dataset_id(args)
245-
domain_id = args.get('domain')
246-
min_count = args.get('min_count')
247-
json_return = query_db_find_concept_ids(cur, dataset_id, query, domain_id, min_count)
248-
249-
# e.g. /api/v1/query?service=omop&meta=concepts&q=4196636,437643
250-
elif method == 'concepts':
251-
json_return = query_db_concepts(query)
252-
253-
# Looks up ancestors of a given concept
254-
# e.g. /api/query?service=omop&meta=conceptAncestors&concept_id=313217
255-
elif method == 'conceptAncestors':
242+
if method == 'conceptAncestors':
256243
# Get non-required parameters
257244
dataset_id = get_arg_dataset_id(args, DATASET_ID_DEFAULT_HIER)
258245

@@ -2083,9 +2070,9 @@ def omop_concept_definitions(concept_ids):
20832070
if not concept_ids:
20842071
return concept_defs
20852072

2086-
response = query_db(service='omop', method='concepts', args={'q': ','.join(str(c) for c in concept_ids)})
2073+
q = ','.join(str(c) for c in concept_ids)
2074+
concept_results = query_db_concepts(q)
20872075

2088-
concept_results = response.get_json()
20892076
if concept_results is None or 'results' not in concept_results:
20902077
return concept_defs
20912078

0 commit comments

Comments
 (0)