Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 35 additions & 80 deletions TCT/TCT_pathfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pandas as pd
from collections import Counter

from typing import Any, Optional
from typing import Any, Optional, Sequence

from . import translator_query
from .config import service_url
Expand Down Expand Up @@ -363,100 +363,55 @@ def format_pathfinder_query(node1_id:str, node1_category:str, node2_id:str, node
return query_json


def query_aragorn_pathfinder(node1_id:str, node1_category:str, node2_id:str, node2_category:str) -> str:
"""
This queries the ARAGORN Pathfinder API.

Params
------
node1_id : str
node1_category : str
node2_id : str
node2_category : str

Returns
-------
A string (which should be a JSON) representing the result of an ARAGORN pathfinder query.
"""
aragorn_endpoint = service_url("aragorn")
query_current = format_pathfinder_query(node1_id, node1_category, node2_id, node2_category)
response = requests.post(aragorn_endpoint, json=query_current)
return response


def query_aragorn_pathfinder_with_constraints(node1_id:str, node1_category:str, node2_id:str, node2_category:str, constraints:list) -> str:
"""
This queries the ARAGORN Pathfinder API with a list of constraints.

Params
------
node1_id : str
node1_category : str
node2_id : str
node2_category : str
constraints : list
PATHFINDER_ARAS = ("aragorn", "arax")

Returns
-------
A string (which should be a JSON) representing the result of an ARAGORN pathfinder query.
"""
aragorn_endpoint = service_url("aragorn")
query_current = format_query_json_for_pathfinder_with_constraints(
subject_ids=node1_id,
object_ids=node2_id,
subject_categories=node1_category,
object_categories=node2_category,
constraints=constraints
)
response = requests.post(aragorn_endpoint, json=query_current)
return response

def query_arax_pathfinder(node1_id:str, node1_category:str, node2_id:str, node2_category:str) -> str:
def query_pathfinder(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small offline test would help here. It could check that each ARA resolves to the right endpoint, that an unknown ARA raises ValueError, and that passing constraints selects the constrained query builder.

ara: str,
node1_id: str,
node1_category: str,
node2_id: str,
node2_category: str,
constraints: Optional[Sequence[str]] = None,
timeout: Optional[float] = None,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to have this parameter. With the default of None, a call still waits indefinitely when an ARA stops responding. A default of about 300 seconds would match the consortium's five-minute response target for ARAs, and callers could still override it.

) -> requests.Response:
"""
This queries the ARAX Pathfinder API.
Queries an ARA's Pathfinder API, with or without intermediate constraints.

Params
------
ara : str
'aragorn' or 'arax'
node1_id : str
node1_category : str
node2_id : str
node2_category : str
constraints : sequence of str, optional
Intermediate node categories. Only up to one allowed.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring says only one intermediate category is allowed, and the function passes the list through whatever its length. Either raising on more than one or warning and using the first would make the behaviour match the docstring.

timeout : float, optional

Returns
-------
A string (which should be a JSON) representing the result of an ARAX pathfinder query.
pathfinder response
"""
ARAX_endpoint = service_url("arax")
query_current = format_pathfinder_query(node1_id, node1_category, node2_id, node2_category)
response = requests.post(ARAX_endpoint, json=query_current)
return response

def query_arax_pathfinder_with_constraints(node1_id:str, node1_category:str, node2_id:str, node2_category:str, constraints:list) -> str:
"""
This queries the ARAX Pathfinder API with a list of constraints.

Params
------
node1_id : str
node1_category : str
node2_id : str
node2_category : str
constraints : list
ara_key = ara.strip().lower()
if ara_key not in PATHFINDER_ARAS:
raise ValueError(
f"unknown pathfinder ARA: {ara!r}; "
f"expected one of {', '.join(PATHFINDER_ARAS)}"
)
if constraints:
query_json = format_query_json_for_pathfinder_with_constraints(
subject_ids=node1_id,
object_ids=node2_id,
subject_categories=node1_category,
object_categories=node2_category,
constraints=list(constraints),
)
else:
query_json = format_pathfinder_query(node1_id, node1_category, node2_id, node2_category)
return requests.post(service_url(ara_key), json=query_json, timeout=timeout)

Returns
-------
A string (which should be a JSON) representing the result of an ARAX pathfinder query.
"""
ARAX_endpoint = service_url("arax")
query_current = format_query_json_for_pathfinder_with_constraints(
subject_ids=node1_id,
object_ids=node2_id,
subject_categories=node1_category,
object_categories=node2_category,
constraints=constraints
)
response = requests.post(ARAX_endpoint, json=query_current)
return response

def query_TCT_pathfinder(
start: NodeInput,
Expand Down
37 changes: 15 additions & 22 deletions notebooks/Path_finder.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -388,13 +388,9 @@
"# query arax pathfinder with constraints and write response to a json file\n",
"import time\n",
"start_time = time.time()\n",
"result_arax = TCT_pathfinder.query_arax_pathfinder_with_constraints(subject_node, \n",
" subject_category,\n",
" object_node, \n",
" object_category, \n",
" constraints=intermediate_categories\n",
" \n",
" )\n",
"result_arax = TCT_pathfinder.query_pathfinder(\n",
" \"arax\", subject_node, subject_category, object_node, object_category,\n",
" constraints=intermediate_categories)\n",
"end_time = time.time()\n",
"arax_execution_time = end_time - start_time\n",
"if result_arax.json()['message'].get('auxiliary_graphs') is not None:\n",
Expand All @@ -410,7 +406,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -423,17 +419,15 @@
}
],
"source": [
"# run aragorn pathfinder without constraints and write response to a json file\n",
"# run aragorn pathfinder with constraints and write response to a json file\n",
"import time\n",
"start_time = time.time()\n",
"\n",
"# Note: for ARAGORN or ARAX pathfinder pipeline, it is only allowed to have only one intermediate category in the constraints list. If there are multiple intermediate categories, the query will return an error. Therefore, we will only use one intermediate category in the constraints list. \n",
"\n",
"aragorn_response = TCT_pathfinder.query_aragorn_pathfinder_with_constraints(subject_node, \n",
" subject_category, \n",
" object_node, \n",
" object_category, \n",
" constraints=intermediate_categories)\n",
"result_arax = TCT_pathfinder.query_pathfinder(\n",
" \"arax\", subject_node, subject_category, object_node, object_category,\n",
" constraints=intermediate_categories)\n",
"# write response to a json file\n",
"import json\n",
"if 'message' in aragorn_response.json():\n",
Expand All @@ -452,7 +446,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -467,10 +461,8 @@
"source": [
"# run aragorn pathfinder without constraints and write response to a json file\n",
"start_time = time.time()\n",
"aragorn_response = TCT_pathfinder.query_aragorn_pathfinder(subject_node, \n",
" subject_category, \n",
" object_node, \n",
" object_category)\n",
"aragorn_response = TCT_pathfinder.query_pathfinder(\n",
" \"aragorn\", subject_node, subject_category, object_node, object_category)\n",
"# write response to a json file\n",
"import json\n",
"if 'message' in aragorn_response.json():\n",
Expand All @@ -489,7 +481,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -502,7 +494,8 @@
}
],
"source": [
"arax_response = TCT_pathfinder.query_arax_pathfinder(subject_node, 'biolink:SmallMolecule', object_node, 'biolink:Disease')\n",
"arax_response = TCT_pathfinder.query_pathfinder(\n",
" \"arax\", subject_node, subject_category, object_node, object_category)\n",
"# write response to a json file\n",
"import json\n",
"with open('arax_pathfinder_response_'+subject_node.replace(\":\", \"_\")+'_'+object_node.replace(\":\", \"_\")+'.json', 'w') as f:\n",
Expand Down
Loading