Ejemplo n.º 1
0
 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
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
 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
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
0
 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
Ejemplo n.º 7
0
 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
Ejemplo n.º 8
0
 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
Ejemplo n.º 9
0
 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