def _create_ngd_node(kg2_node_key: str, kg2_node: Node) -> Tuple[str, Node]: ngd_node = Node() ngd_node_key = kg2_node_key ngd_node.name = kg2_node.name ngd_node.category = kg2_node.category return ngd_node_key, ngd_node
def _convert_to_swagger_node(self, node_key: str) -> Tuple[str, Node]: swagger_node = Node() swagger_node_key = node_key swagger_node.name = self.synonymizer.get_canonical_curies( node_key)[node_key]['preferred_name'] swagger_node.description = None swagger_node.category = self.synonymizer.get_canonical_curies( node_key)[node_key]['preferred_category'] return swagger_node_key, swagger_node
def _add_answers_to_kg(self, answer_kg: QGOrganizedKnowledgeGraph, reasoner_std_response: Dict[str, any], input_qnode_key: str, output_qnode_key: str, qedge_key: str, log: ARAXResponse) -> QGOrganizedKnowledgeGraph: kg_to_qg_ids_dict = self._build_kg_to_qg_id_dict(reasoner_std_response['results']) if reasoner_std_response['knowledge_graph']['edges']: remapped_node_keys = dict() log.debug(f"Got results back from BTE for this query " f"({len(reasoner_std_response['knowledge_graph']['edges'])} edges)") for node in reasoner_std_response['knowledge_graph']['nodes']: swagger_node = Node() bte_node_key = node.get('id') swagger_node.name = node.get('name') swagger_node.category = eu.convert_to_list(eu.convert_string_to_snake_case(node.get('type'))) # Map the returned BTE qg_ids back to the original qnode_keys in our query graph bte_qg_id = kg_to_qg_ids_dict['nodes'].get(bte_node_key) if bte_qg_id == "n0": qnode_key = input_qnode_key elif bte_qg_id == "n1": qnode_key = output_qnode_key else: log.error("Could not map BTE qg_id to ARAX qnode_key", error_code="UnknownQGID") return answer_kg # Find and use the preferred equivalent identifier for this node (if it's an output node) if qnode_key == output_qnode_key: if bte_node_key in remapped_node_keys: swagger_node_key = remapped_node_keys.get(bte_node_key) else: equivalent_curies = [f"{prefix}:{eu.get_curie_local_id(local_id)}" for prefix, local_ids in node.get('equivalent_identifiers').items() for local_id in local_ids] swagger_node_key = self._get_best_equivalent_bte_curie(equivalent_curies, swagger_node.category[0]) remapped_node_keys[bte_node_key] = swagger_node_key else: swagger_node_key = bte_node_key answer_kg.add_node(swagger_node_key, swagger_node, qnode_key) for edge in reasoner_std_response['knowledge_graph']['edges']: swagger_edge = Edge() swagger_edge_key = edge.get("id") swagger_edge.predicate = edge.get('type') swagger_edge.subject = remapped_node_keys.get(edge.get('source_id'), edge.get('source_id')) swagger_edge.object = remapped_node_keys.get(edge.get('target_id'), edge.get('target_id')) swagger_edge.attributes = [Attribute(name="provided_by", value=edge.get('edge_source'), type=eu.get_attribute_type("provided_by")), Attribute(name="is_defined_by", value="BTE", type=eu.get_attribute_type("is_defined_by"))] # Map the returned BTE qg_id back to the original qedge_key in our query graph bte_qg_id = kg_to_qg_ids_dict['edges'].get(swagger_edge_key) if bte_qg_id != "e1": log.error("Could not map BTE qg_id to ARAX qedge_key", error_code="UnknownQGID") return answer_kg answer_kg.add_edge(swagger_edge_key, swagger_edge, qedge_key) return answer_kg
def _create_ngd_node(kg2_node_key: str, kg2_node: Node) -> Tuple[str, Node]: ngd_node = Node() ngd_node_key = kg2_node_key ngd_node.name = kg2_node.name ngd_node.category = kg2_node.category ngd_node.attributes = [ attribute for attribute in kg2_node.attributes if attribute.name in {"iri", "description"} ] return ngd_node_key, ngd_node
def _convert_kg1_node_to_swagger_node( self, neo4j_node: Dict[str, any]) -> Tuple[str, Node]: swagger_node = Node() swagger_node_key = neo4j_node.get('id') swagger_node.name = neo4j_node.get('name') node_category = neo4j_node.get('category') swagger_node.category = eu.convert_to_list(node_category) other_properties = ["symbol", "description", "uri"] swagger_node.attributes = self._create_swagger_attributes( other_properties, neo4j_node) return swagger_node_key, swagger_node
def _convert_kg2c_node_to_swagger_node( self, neo4j_node: Dict[str, any]) -> Tuple[str, Node]: swagger_node = Node() swagger_node_key = neo4j_node.get('id') swagger_node.name = neo4j_node.get('name') swagger_node.category = neo4j_node.get('types') # Add all additional properties on KG2c nodes as swagger Attribute objects other_properties = [ "description", "iri", "equivalent_curies", "publications", "all_names" ] swagger_node.attributes = self._create_swagger_attributes( other_properties, neo4j_node) return swagger_node_key, swagger_node
def _convert_kg2c_node_to_trapi_node( self, neo4j_node: Dict[str, any]) -> Tuple[str, Node]: node = Node() node_key = neo4j_node.get('id') node.name = neo4j_node.get('name') node.category = eu.convert_to_list(neo4j_node.get('category')) # Add all additional properties on KG2c nodes as TRAPI Attribute objects other_properties = [ "iri", "description", "all_names", "all_categories", "expanded_categories", "equivalent_curies", "publications" ] node.attributes = self._create_trapi_attributes( other_properties, neo4j_node) return node_key, node
def _convert_kg2_node_to_trapi_node( self, neo4j_node: Dict[str, any]) -> Tuple[str, Node]: node = Node() node_key = neo4j_node.get('id') node.name = neo4j_node.get('name') node.category = eu.convert_to_list(neo4j_node.get('category')) # Add all additional properties on KG2 nodes as TRAPI Attribute objects other_properties = [ "iri", "full_name", "description", "publications", "synonym", "provided_by", "deprecated", "update_date" ] node.attributes = self._create_trapi_attributes( other_properties, neo4j_node) return node_key, node
def _convert_kg2_node_to_swagger_node( self, neo4j_node: Dict[str, any]) -> Tuple[str, Node]: swagger_node = Node() swagger_node_key = neo4j_node.get('id') swagger_node.name = neo4j_node.get('name') node_category = neo4j_node.get('category_label') swagger_node.category = eu.convert_to_list(node_category) # Add all additional properties on KG2 nodes as swagger Attribute objects other_properties = [ "full_name", "description", "iri", "publications", "synonym", "category", "provided_by", "deprecated", "update_date" ] swagger_node.attributes = self._create_swagger_attributes( other_properties, neo4j_node) return swagger_node_key, swagger_node
def resultGraph(graph): nodes = [] for node in graph.nodes(data=True): node_attributes = [ NodeAttribute(name=key, value=str(value)) for key, value in node[1].items() if key not in ['labels', 'name'] ] nodes.append( Node(id=str(node[0]), type=[str(label) for label in node[1]['labels']], name=str(node[1]['name']), node_attributes=node_attributes)) edges = [] for edge in graph.edges(data=True): edge_attributes = [ EdgeAttribute(name=key, value=str(value)) for key, value in edge[2].items() if key not in ['type', 'source', 'id'] ] if 'source' not in edge[2]: edge[2]['source'] = "NA" edges.append( Edge(type=edge[2]['type'], source_id=str(edge[0]), target_id=str(edge[1]), provided_by=str(edge[2]['source']), id=str(edge[2]['id']), edge_attributes=edge_attributes)) rg = KnowledgeGraph(nodes=nodes, edges=edges) return (rg)
def add_node(self, id, name, type, attributes=None): """ adds a node to the knwledge graph of the response updated for trapi v1.0.0 """ # map type category=translate_type(type, False) # translate the curie from molepro to biolink translated_curie = translate_curie(id, category, False) if translated_curie not in self.nodes: # node = Node(id=id, name=name, type=type) node = Node(name=name, category=category, attributes=attributes) node.id = translated_curie # added for trapi v1.0.0 self.nodes[translated_curie] = node self.knowledge_graph.nodes[translated_curie] = node return node return self.nodes[translated_curie]
def _create_swagger_node_from_kp_node( kp_node_key: str, kp_node: Dict[str, any]) -> Tuple[str, Node]: return kp_node_key, Node(category=kp_node['category'], name=kp_node.get('name'))
def _convert_kg2c_plover_node_to_trapi_node(node_tuple: list) -> Node: node = Node(name=node_tuple[0], categories=eu.convert_to_list(node_tuple[1])) return node