From 544cc6f1935ca50a542d0421ab87dce4181d0b6b Mon Sep 17 00:00:00 2001 From: uhbrar Date: Wed, 2 Sep 2026 10:28:00 -0400 Subject: [PATCH 1/2] refactor pathfinder functions --- TCT/TCT_pathfinder.py | 115 +++++++++++++----------------------------- 1 file changed, 35 insertions(+), 80 deletions(-) diff --git a/TCT/TCT_pathfinder.py b/TCT/TCT_pathfinder.py index 435b245..375ccaf 100644 --- a/TCT/TCT_pathfinder.py +++ b/TCT/TCT_pathfinder.py @@ -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 @@ -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( + ara: str, + node1_id: str, + node1_category: str, + node2_id: str, + node2_category: str, + constraints: Optional[Sequence[str]] = None, + timeout: Optional[float] = None, +) -> 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. + 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, From 34b5fb08510bcf094633ea6f71897115e989f0a7 Mon Sep 17 00:00:00 2001 From: uhbrar Date: Wed, 2 Sep 2026 10:28:09 -0400 Subject: [PATCH 2/2] use new functions --- notebooks/Path_finder.ipynb | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/notebooks/Path_finder.ipynb b/notebooks/Path_finder.ipynb index eb36d35..f9fe433 100644 --- a/notebooks/Path_finder.ipynb +++ b/notebooks/Path_finder.ipynb @@ -372,7 +372,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -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", @@ -410,7 +406,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -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", @@ -452,7 +446,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -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", @@ -489,7 +481,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -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",