Skip to content

Commit a7dd61b

Browse files
authored
Merge pull request #260 from gyorilab/fix-web-api
Fix web api
2 parents abedb5c + 804da43 commit a7dd61b

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

src/indra_cogex/apps/queries_web/__init__.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ def get_example_data():
181181
'namespace': literature_metadata_ns,
182182
'functions': [
183183
"get_pmids_for_mesh",
184+
"get_pmids_for_stmt_hash",
184185
"get_mesh_ids_for_pmid",
185186
"get_mesh_ids_for_pmids",
186187
"get_publisher_for_journal",
@@ -546,6 +547,24 @@ def get_example_data():
546547
# Maps function names to the actual functions
547548
func_mapping = {fname: getattr(module, fname) for module, fname in module_functions}
548549

550+
# Validate that all functions in func_mapping are registered in FUNCTION_CATEGORIES.
551+
# This catches the common mistake of adding a function to module_functions but forgetting
552+
# to add it to FUNCTION_CATEGORIES, which would cause a runtime error later.
553+
_registered_functions = set()
554+
for _category, _info in FUNCTION_CATEGORIES.items():
555+
_registered_functions.update(_info['functions'])
556+
557+
_unregistered_functions = set(func_mapping.keys()) - _registered_functions
558+
if _unregistered_functions:
559+
logger.warning(
560+
f"The following functions are in func_mapping but not registered in "
561+
f"FUNCTION_CATEGORIES: {sorted(_unregistered_functions)}. "
562+
f"Add them to FUNCTION_CATEGORIES to expose them via the API."
563+
)
564+
565+
# Clean up temporary variables
566+
del _registered_functions, _unregistered_functions
567+
549568
# Create resource for each query function
550569
for module, func_name in module_functions:
551570
if not isfunction(getattr(module, func_name)) or func_name == "get_schema_graph":
@@ -570,9 +589,14 @@ def get_example_data():
570589
f"it to FUNCTION_CATEGORIES."
571590
)
572591

573-
short_doc, fixed_doc = get_docstring(
574-
func, skip_params=SKIP_GLOBAL | SKIP_ARGUMENTS.get(func_name, set())
575-
)
592+
try:
593+
short_doc, fixed_doc = get_docstring(
594+
func, skip_params=SKIP_GLOBAL | SKIP_ARGUMENTS.get(func_name, set())
595+
)
596+
except ValueError as e:
597+
raise ValueError(
598+
f"Error processing docstring for function '{func_name}': {e}"
599+
) from e
576600

577601
param_names = list(func_sig.parameters.keys())
578602
param_names.remove("client")

src/indra_cogex/apps/queries_web/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def get_web_return_annotation(sig: Signature) -> Type:
132132
# Get the return annotation
133133
return_annotation = sig.return_annotation
134134
if return_annotation is sig.empty:
135-
raise ValueError("Forgot to type annotate function")
135+
raise ValueError("Forgot to type annotate function return value")
136136

137137
# Translate the return annotation:
138138
# Iterable[Node] -> List[Dict[str, Any]]
@@ -233,7 +233,7 @@ def get_docstring(
233233
)
234234
sig = signature(fun)
235235
if sig.return_annotation is sig.empty:
236-
raise ValueError("Forgot to type annotate function")
236+
raise ValueError(f"Forgot to type annotate function return value of {fun.__name__}")
237237

238238
full_docstr = """{title}
239239
{extra_description}

src/indra_cogex/client/queries.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -801,9 +801,9 @@ def get_pmids_for_mesh(
801801

802802
return client.query_nodes(query, **query_param)
803803

804-
@autoclient()
805-
def get_pmids_for_stmt_hash(stmt_hash: int, *, client: Neo4jClient):
806804

805+
@autoclient()
806+
def get_pmids_for_stmt_hash(stmt_hash: int, *, client: Neo4jClient) -> List[str]:
807807
"""Return the PubMed IDs for the given statement hash.
808808
809809
Parameters
@@ -816,7 +816,7 @@ def get_pmids_for_stmt_hash(stmt_hash: int, *, client: Neo4jClient):
816816
Returns
817817
-------
818818
:
819-
The PubMed IDs for the given MESH term
819+
The PubMed IDs for the given statement hash.
820820
"""
821821
query = """
822822
MATCH (e:Evidence {stmt_hash: $hash})-[:has_citation]-(p:Publication)
@@ -2701,7 +2701,7 @@ def get_projects_for_patent(
27012701
)
27022702

27032703

2704-
#interpro
2704+
# interpro
27052705
@autoclient()
27062706
def get_domains_for_gene(
27072707
gene: Tuple[str, str], *, client: Neo4jClient
@@ -2783,7 +2783,7 @@ def gene_has_domain(
27832783
)
27842784

27852785

2786-
#gwas
2786+
# gwas
27872787
@autoclient()
27882788
def get_phenotypes_for_variant_gwas(
27892789
variant: Tuple[str, str], *, client: Neo4jClient
@@ -3533,8 +3533,6 @@ def build_edges_from_graph(graph, statements, input_node_names, include_db_evide
35333533
return edges
35343534

35353535

3536-
3537-
35383536
def _get_edge_styling(stmt_type):
35393537
"""Return RGB base color, dash pattern, and arrow spec for statement type."""
35403538
if 'Activation' in stmt_type:
@@ -3644,7 +3642,6 @@ def get_network(
36443642
return {"nodes": [], "edges": [], "error": str(e)}
36453643

36463644

3647-
36483645
if __name__ == "__main__":
36493646
print(get_prefix_counter())
36503647
print(get_node_counter())

0 commit comments

Comments
 (0)