66import logging
77
88from typing import Tuple , Union
9+ from pydantic import parse_obj_as
910from django .db .models import QuerySet
11+ from reasoner_pydantic .utils import HashableMapping
1012from django .core .exceptions import ObjectDoesNotExist
1113from reasoner_pydantic import MetaKnowledgeGraph , Message , KnowledgeGraph
1214from reasoner_pydantic .kgraph import RetrievalSource , Attribute
@@ -25,7 +27,7 @@ def note(self, message, *args, **kwargs):
2527APP_PATH = os .path .dirname (os .path .abspath (__file__ ))
2628
2729class TrapiInterface :
28- def __init__ (self , trapi_version : str = '1.4 ' ):
30+ def __init__ (self , trapi_version : str = '1.5 ' ):
2931 self .trapi_version = trapi_version
3032
3133 def get_meta_knowledge_graph (self ) -> MetaKnowledgeGraph :
@@ -42,7 +44,7 @@ def get_name(self) -> str:
4244 def _get_sources (self ):
4345 source_1 = RetrievalSource (resource_id = "infores:connections-hypothesis" ,
4446 resource_role = "primary_knowledge_source" )
45- return { source_1 }
47+ return [ source_1 ]
4648
4749 def _get_attributes (self , val , algorithm_instance , dataset ):
4850 att_1 = Attribute (
@@ -64,18 +66,14 @@ def _get_attributes(self, val, algorithm_instance, dataset):
6466 description = f'{ dataset .title } : { dataset .description } ' ,
6567 )
6668 att_4 = Attribute (
67- attribute_type_id = 'primary_knowledge_source' ,
68- value = 'infores:connections-hypothesis' ,
69- value_url = 'https://github.com/di2ag/gennifer' ,
70- description = 'The Connections Hypothesis Provider from NCATS Translator.'
69+ attribute_type_id = 'knowledge_level' ,
70+ value = 'statistical_association'
7171 )
72- return { att_1 , att_2 , att_3 , att_4 }
72+ return [ att_1 , att_2 , att_3 , att_4 ]
7373
7474 def _add_results (
7575 self ,
7676 message ,
77- node_bindings ,
78- edge_bindings ,
7977 qg_subject_id ,
8078 subject_curies ,
8179 subject_category ,
@@ -88,13 +86,15 @@ def _add_results(
8886 algorithms ,
8987 datasets ,
9088 ):
89+ node_binding_group = []
90+ edge_binding_group = []
9191 nodes = dict ()
9292 edges = dict ()
9393 val_id = 0
9494 for subject_curie in subject_curies :
9595 for object_curie in object_curies :
96- nodes [subject_curie ] = {"categories" : [subject_category ]}
97- nodes [object_curie ] = {"categories" : [object_category ]}
96+ nodes [subject_curie ] = {"categories" : [subject_category ], "attributes" : [] }
97+ nodes [object_curie ] = {"categories" : [object_category ], "attributes" : [] }
9898 kg_edge_id = str (uuid .uuid4 ())
9999 edges [kg_edge_id ] = {"predicate" : predicate ,
100100 "subject" : subject_curie ,
@@ -106,14 +106,19 @@ def _add_results(
106106 datasets [val_id ],
107107 )}
108108 val_id += 1
109- node_bindings [qg_subject_id ].add (NodeBinding (id = subject_curie ))
110- node_bindings [qg_object_id ].add (NodeBinding (id = object_curie ))
111- edge_bindings [qg_edge_id ].add (EdgeBinding (id = kg_edge_id ))
109+ node_bindings = {qg_subject_id : set (), qg_object_id : set ()}
110+ edge_bindings = {qg_edge_id : set ()}
111+ node_bindings [qg_subject_id ].add (NodeBinding (id = subject_curie , attributes = []))
112+ node_bindings [qg_object_id ].add (NodeBinding (id = object_curie , attributes = []))
113+ edge_bindings [qg_edge_id ].add (EdgeBinding (id = kg_edge_id , attributes = []))
114+ node_binding_group .append (node_bindings )
115+ edge_binding_group .append (edge_bindings )
112116 kgraph = KnowledgeGraph (nodes = nodes , edges = edges )
113117 if message .knowledge_graph is not None :
114118 message .knowledge_graph .update (kgraph )
115119 else :
116120 message .knowledge_graph = kgraph
121+ return node_binding_group , edge_binding_group
117122
118123 def _extract_qnode_info (self , qnode ):
119124 return qnode .ids , qnode .categories [0 ]
@@ -127,8 +132,8 @@ def get_response(self, message: Message, logger):
127132 subject_curies , subject_category = self ._extract_qnode_info (message .query_graph .nodes [qg_subject_id ])
128133 object_curies , object_category = self ._extract_qnode_info (message .query_graph .nodes [qg_object_id ])
129134 # annotation
130- node_bindings = { qg_subject_id : set (), qg_object_id : set ()}
131- edge_bindings = { qg_edge_id : set ()}
135+ node_bindings = []
136+ edge_bindings = []
132137 #TODO: Should probably offer support to return all results
133138 if subject_curies is not None and object_curies is not None :
134139 logger .info ('Annotation edges detected' )
@@ -156,22 +161,23 @@ def get_response(self, message: Message, logger):
156161 vals = [r .edge_weight for r in results ]
157162 algorithms = [r .study .algorithm_instance for r in results ]
158163 datasets = [r .study .dataset for r in results ]
159- self ._add_results (
164+ node_binding_group , edge_binding_group = self ._add_results (
160165 message ,
161- node_bindings ,
162- edge_bindings ,
163166 qg_subject_id ,
164167 subject_curies ,
165168 subject_category ,
166169 predicate ,
167170 qg_edge_id ,
168- qg_object_id ,
169- [curie ],
171+ object_mapping ,
172+ qg_object_id ,
173+ [curie ],
170174 object_category ,
171175 vals ,
172176 algorithms ,
173- datasets ,
177+ datasets
174178 )
179+ node_bindings .extend (node_binding_group )
180+ edge_bindings .extend (edge_binding_group )
175181 elif subject_curies is not None :
176182 logger .info ('Wildcard detected' )
177183 for curie in subject_curies :
@@ -194,10 +200,8 @@ def get_response(self, message: Message, logger):
194200 vals = [r .edge_weight for r in results ]
195201 algorithms = [r .study .algorithm_instance for r in results ]
196202 datasets = [r .study .dataset for r in results ]
197- self ._add_results (
203+ node_binding_group , edge_binding_group = self ._add_results (
198204 message ,
199- node_bindings ,
200- edge_bindings ,
201205 qg_subject_id ,
202206 subject_curies ,
203207 subject_category ,
@@ -210,10 +214,15 @@ def get_response(self, message: Message, logger):
210214 algorithms ,
211215 datasets ,
212216 )
217+ node_bindings .extend (node_binding_group )
218+ edge_bindings .extend (edge_binding_group )
213219 else :
214220 logger .info ('No curies detected. Returning no results' )
215221 return message
216- analysis = Analysis (resource_id = 'infores:connections-hypothesis' , edge_bindings = edge_bindings )
217- result = Result (node_bindings = node_bindings , analyses = [analysis ])
218- message .results = Results (__root__ = {result })
222+ results = Results (__root__ = parse_obj_as (HashableMapping , {}))
223+ for node_binding_dict , edge_binding_dict in zip (node_bindings , edge_bindings ):
224+ analysis = Analysis (resource_id = 'infores:connections-hypothesis' , edge_bindings = edge_binding_dict , attributes = [])
225+ result = Result (node_bindings = node_binding_dict , analyses = [analysis ])
226+ results .add (result )
227+ message .results = results
219228 return message
0 commit comments