Skip to content

Commit

Permalink
Don't throw error when no results #1510
Browse files Browse the repository at this point in the history
  • Loading branch information
amykglen committed Jun 18, 2021
1 parent e37e50b commit 3b82b19
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 368 deletions.
35 changes: 9 additions & 26 deletions code/ARAX/ARAXQuery/ARAX_expander.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def describe_me(self):
"parameters": {
"edge_key": kg2_definition["parameters"]["edge_key"],
"node_key": kg2_definition["parameters"]["node_key"],
"continue_if_no_results": kg2_definition["parameters"]["continue_if_no_results"],
"enforce_directionality": kg2_definition["parameters"]["enforce_directionality"]
}
}
Expand Down Expand Up @@ -123,7 +122,6 @@ def apply(self, response, input_parameters, mode="ARAX"):
log.debug(f"Applying Expand to Message with parameters {parameters}")
input_qedge_keys = eu.convert_to_list(parameters['edge_key'])
input_qnode_keys = eu.convert_to_list(parameters['node_key'])
continue_if_no_results = parameters['continue_if_no_results']
user_specified_kp = True if parameters['kp'] else False

# Convert message knowledge graph to format organized by QG keys, for faster processing
Expand Down Expand Up @@ -221,21 +219,16 @@ def apply(self, response, input_parameters, mode="ARAX"):
if response.status != 'OK':
return response

# Make sure we found at least SOME answers for this edge (unless we're continuing if no results)
# Make sure we found at least SOME answers for this edge
if not eu.qg_is_fulfilled(one_hop_qg, overarching_kg) and not qedge.exclude and not qedge.option_group_id:
if continue_if_no_results:
log.warning(f"No paths were found in {kps_to_query} satisfying qedge {qedge_key}")
else:
log.error(f"No paths were found in {kps_to_query} satisfying qedge {qedge_key}",
error_code="NoResults")
return response
log.warning(f"No paths were found in {kps_to_query} satisfying qedge {qedge_key}")
return response

# Expand any specified nodes
if input_qnode_keys:
kp_to_use = parameters["kp"] if user_specified_kp else "RTX-KG2" # Only KG2 does single-node queries
for qnode_key in input_qnode_keys:
answer_kg = self._expand_node(qnode_key, kp_to_use, continue_if_no_results, query_graph,
mode, user_specified_kp, force_local, log)
answer_kg = self._expand_node(qnode_key, kp_to_use, query_graph, mode, user_specified_kp, force_local, log)
if log.status != 'OK':
return response
self._merge_answer_into_message_kg(answer_kg, overarching_kg, message.query_graph, mode, log)
Expand All @@ -255,12 +248,8 @@ def apply(self, response, input_parameters, mode="ARAX"):

# Return the response and done
kg = message.knowledge_graph
only_kryptonite_qedges_expanded = all([query_graph.edges[qedge_key].exclude for qedge_key in input_qedge_keys])
if not kg.nodes and not continue_if_no_results and not only_kryptonite_qedges_expanded:
log.error(f"No paths were found satisfying this query graph", error_code="NoResults")
else:
log.info(f"After Expand, the KG has {len(kg.nodes)} nodes and {len(kg.edges)} edges "
f"({eu.get_printable_counts_by_qg_id(overarching_kg)})")
log.info(f"After Expand, the KG has {len(kg.nodes)} nodes and {len(kg.edges)} edges "
f"({eu.get_printable_counts_by_qg_id(overarching_kg)})")

return response

Expand Down Expand Up @@ -334,8 +323,8 @@ def _expand_edge(self, edge_qg: QueryGraph, kp_to_use: str, input_parameters: Di

return answer_kg, log

def _expand_node(self, qnode_key: str, kp_to_use: str, continue_if_no_results: bool, query_graph: QueryGraph,
mode: str, user_specified_kp: bool, force_local: bool, log: ARAXResponse) -> QGOrganizedKnowledgeGraph:
def _expand_node(self, qnode_key: str, kp_to_use: str, query_graph: QueryGraph, mode: str, user_specified_kp: bool,
force_local: bool, log: ARAXResponse) -> QGOrganizedKnowledgeGraph:
# This function expands a single node using the specified knowledge provider
log.debug(f"Expanding node {qnode_key} using {kp_to_use}")
qnode = query_graph.nodes[qnode_key]
Expand All @@ -359,15 +348,9 @@ def _expand_node(self, qnode_key: str, kp_to_use: str, continue_if_no_results: b
answer_kg = kp_querier.answer_single_node_query(single_node_qg)
log.info(f"Query for node {qnode_key} returned results ({eu.get_printable_counts_by_qg_id(answer_kg)})")

# Make sure all qnodes have been fulfilled (unless we're continuing if no results)
if log.status == 'OK' and not continue_if_no_results:
if not answer_kg.nodes_by_qg_id.get(qnode_key):
log.error(f"Returned answer KG does not contain any results for QNode {qnode_key}",
error_code="UnfulfilledQGID")
return answer_kg

if kp_to_use != 'RTX-KG2': # KG2c is already deduplicated
answer_kg = self._deduplicate_nodes(answer_kg, kp_to_use, log)

return answer_kg
else:
log.error(f"Invalid knowledge provider: {kp_to_use}. Valid options for single-node queries are "
Expand Down
15 changes: 15 additions & 0 deletions code/ARAX/ARAXQuery/ARAX_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,21 @@ def apply(self, response, input_parameters):
response.debug(
f"Applying Overlay to Message with parameters {parameters}") # TODO: re-write this to be more specific about the actual action

# Don't try to overlay anything if the KG is empty
message = response.envelope.message
if not message.knowledge_graph or not message.knowledge_graph.nodes:
response.debug(f"Nothing to overlay (KG is empty)")
return response
# Don't try to overlay anything if any of the specified qnodes aren't fulfilled in the KG
possible_node_params = {"subject_qnode_key", "object_qnode_key", "start_node_key", "intermediate_node_key",
"end_node_key"}
node_params_to_check = set(self.parameters).intersection(possible_node_params)
qnode_keys_to_check = {self.parameters[node_param] for node_param in node_params_to_check}
if not all(any(node for node in message.knowledge_graph.nodes.values() if qnode_key in node.qnode_keys)
for qnode_key in qnode_keys_to_check):
response.debug(f"Nothing to overlay (one or more of the specified qnodes is not fulfilled in the KG)")
return response

# convert the action string to a function call (so I don't need a ton of if statements
getattr(self, '_' + self.__class__.__name__ + '__' + parameters[
'action'])() # thank you https://stackoverflow.com/questions/11649848/call-methods-by-string
Expand Down
35 changes: 7 additions & 28 deletions code/ARAX/ARAXQuery/Expand/expand_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,14 +456,6 @@ def get_kp_command_definitions() -> dict:
"type": "string",
"description": "A query graph node ID or list of such IDs to expand (default is to expand entire query graph)."
}
continue_if_no_results_parameter_info = {
"is_required": False,
"examples": ["true", "false"],
"enum": ["true", "false", "True", "False", "t", "f", "T", "F"],
"default": "false",
"type": "boolean",
"description": "Whether to continue execution if no paths are found matching the query graph."
}
enforce_directionality_parameter_info = {
"is_required": False,
"examples": ["true", "false"],
Expand All @@ -480,7 +472,6 @@ def get_kp_command_definitions() -> dict:
"parameters": {
"edge_key": edge_key_parameter_info,
"node_key": node_key_parameter_info,
"continue_if_no_results": continue_if_no_results_parameter_info,
"enforce_directionality": enforce_directionality_parameter_info
}
},
Expand All @@ -491,7 +482,6 @@ def get_kp_command_definitions() -> dict:
"parameters": {
"edge_key": edge_key_parameter_info,
"node_key": node_key_parameter_info,
"continue_if_no_results": continue_if_no_results_parameter_info,
"enforce_directionality": enforce_directionality_parameter_info
}
},
Expand All @@ -506,7 +496,6 @@ def get_kp_command_definitions() -> dict:
"parameters": {
"edge_key": edge_key_parameter_info,
"node_key": node_key_parameter_info,
"continue_if_no_results": continue_if_no_results_parameter_info,
"enforce_directionality": enforce_directionality_parameter_info
}
},
Expand All @@ -517,7 +506,6 @@ def get_kp_command_definitions() -> dict:
"parameters": {
"edge_key": edge_key_parameter_info,
"node_key": node_key_parameter_info,
"continue_if_no_results": continue_if_no_results_parameter_info,
"COHD_method": {
"is_required": False,
"examples": ["paired_concept_freq", "chi_square"],
Expand All @@ -543,8 +531,7 @@ def get_kp_command_definitions() -> dict:
"satisfy the query graph.",
"parameters": {
"edge_key": edge_key_parameter_info,
"node_key": node_key_parameter_info,
"continue_if_no_results": continue_if_no_results_parameter_info
"node_key": node_key_parameter_info
}
},
"MolePro": {
Expand All @@ -553,8 +540,7 @@ def get_kp_command_definitions() -> dict:
"subpaths that satisfy the query graph.",
"parameters": {
"edge_key": edge_key_parameter_info,
"node_key": node_key_parameter_info,
"continue_if_no_results": continue_if_no_results_parameter_info
"node_key": node_key_parameter_info
}
},
"ClinicalRiskKP": {
Expand All @@ -563,8 +549,7 @@ def get_kp_command_definitions() -> dict:
"subpaths that satisfy the query graph.",
"parameters": {
"edge_key": edge_key_parameter_info,
"node_key": node_key_parameter_info,
"continue_if_no_results": continue_if_no_results_parameter_info
"node_key": node_key_parameter_info
}
},
"WellnessKP": {
Expand All @@ -573,8 +558,7 @@ def get_kp_command_definitions() -> dict:
"subpaths that satisfy the query graph.",
"parameters": {
"edge_key": edge_key_parameter_info,
"node_key": node_key_parameter_info,
"continue_if_no_results": continue_if_no_results_parameter_info
"node_key": node_key_parameter_info
}
},
"DrugResponseKP": {
Expand All @@ -583,8 +567,7 @@ def get_kp_command_definitions() -> dict:
"bioentity subpaths that satisfy the query graph.",
"parameters": {
"edge_key": edge_key_parameter_info,
"node_key": node_key_parameter_info,
"continue_if_no_results": continue_if_no_results_parameter_info
"node_key": node_key_parameter_info
}
},
"TumorGeneMutationKP": {
Expand All @@ -593,8 +576,7 @@ def get_kp_command_definitions() -> dict:
"all bioentity subpaths that satisfy the query graph.",
"parameters": {
"edge_key": edge_key_parameter_info,
"node_key": node_key_parameter_info,
"continue_if_no_results": continue_if_no_results_parameter_info
"node_key": node_key_parameter_info
}
},
"NGD": {
Expand All @@ -605,8 +587,7 @@ def get_kp_command_definitions() -> dict:
"configurable/smarter in the future.",
"parameters": {
"edge_key": edge_key_parameter_info,
"node_key": node_key_parameter_info,
"continue_if_no_results": continue_if_no_results_parameter_info
"node_key": node_key_parameter_info
}
},
"CHP": {
Expand All @@ -620,7 +601,6 @@ def get_kp_command_definitions() -> dict:
"parameters": {
"edge_key": edge_key_parameter_info,
"node_key": node_key_parameter_info,
"continue_if_no_results": continue_if_no_results_parameter_info,
"CHP_survival_threshold": {
"is_required": False,
"examples": [200, 100],
Expand All @@ -645,7 +625,6 @@ def get_kp_command_definitions() -> dict:
"parameters": {
"edge_key": edge_key_parameter_info,
"node_key": node_key_parameter_info,
"continue_if_no_results": continue_if_no_results_parameter_info,
"DTD_threshold": {
"is_required": False,
"examples": [0.8, 0.5],
Expand Down
2 changes: 1 addition & 1 deletion code/ARAX/ARAXQuery/operation_to_ARAXi.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __translate_fill(parameters):
if 'allowlist' in parameters:
for KP_name in parameters['allowlist']:
# continue if no results, don't enforce directionality, and use synonyms
ARAXi.append(f"expand(kp={KP_name},continue_if_no_results=true,enforce_directionality=false)")
ARAXi.append(f"expand(kp={KP_name},enforce_directionality=false)")
else:
ARAXi.append("expand()")
return ARAXi
Expand Down
Loading

0 comments on commit 3b82b19

Please sign in to comment.