Esempio n. 1
0
 def _create_ngd_edge(self, ngd_value: float, subject: str, object: str,
                      pmid_list: list) -> Tuple[str, Edge]:
     ngd_edge = Edge()
     ngd_edge.predicate = self.ngd_edge_type
     ngd_edge.subject = subject
     ngd_edge.object = object
     ngd_edge_key = f"NGD:{subject}--{ngd_edge.predicate}--{object}"
     ngd_edge.attributes = [
         Attribute(name=self.ngd_edge_attribute_name,
                   type=self.ngd_edge_attribute_type,
                   value=ngd_value,
                   url=self.ngd_edge_attribute_url)
     ]
     ngd_edge.attributes += [
         Attribute(name="provided_by",
                   value="ARAX",
                   type=eu.get_attribute_type("provided_by")),
         Attribute(name="is_defined_by",
                   value="ARAX",
                   type=eu.get_attribute_type("is_defined_by")),
         Attribute(name="publications",
                   value=pmid_list,
                   type=eu.get_attribute_type("publications"))
     ]
     return ngd_edge_key, ngd_edge
Esempio n. 2
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
Esempio n. 3
0
 def _convert_kg2c_edge_to_swagger_edge(
         self, neo4j_edge: Dict[str, any]) -> Tuple[str, Edge]:
     swagger_edge = Edge()
     swagger_edge_key = f"KG2c:{neo4j_edge.get('id')}"
     swagger_edge.predicate = neo4j_edge.get("simplified_edge_label")
     swagger_edge.subject = neo4j_edge.get("subject")
     swagger_edge.object = neo4j_edge.get("object")
     other_properties = ["provided_by", "publications"]
     swagger_edge.attributes = self._create_swagger_attributes(
         other_properties, neo4j_edge)
     is_defined_by_attribute = Attribute(
         name="is_defined_by",
         value="ARAX/KG2c",
         type=eu.get_attribute_type("is_defined_by"))
     swagger_edge.attributes.append(is_defined_by_attribute)
     return swagger_edge_key, swagger_edge
Esempio n. 4
0
 def _convert_kg1_edge_to_swagger_edge(
         self, neo4j_edge: Dict[str, any],
         node_uuid_to_curie_dict: Dict[str, str]) -> Tuple[str, Edge]:
     swagger_edge = Edge()
     swagger_edge_key = f"KG1:{neo4j_edge.get('id')}"
     swagger_edge.predicate = neo4j_edge.get("predicate")
     swagger_edge.subject = node_uuid_to_curie_dict[neo4j_edge.get(
         "source_node_uuid")]
     swagger_edge.object = node_uuid_to_curie_dict[neo4j_edge.get(
         "target_node_uuid")]
     swagger_edge.relation = neo4j_edge.get("relation")
     other_properties = ["provided_by", "probability"]
     swagger_edge.attributes = self._create_swagger_attributes(
         other_properties, neo4j_edge)
     is_defined_by_attribute = Attribute(
         name="is_defined_by",
         value="ARAX/KG1",
         type=eu.get_attribute_type("is_defined_by"))
     swagger_edge.attributes.append(is_defined_by_attribute)
     return swagger_edge_key, swagger_edge
Esempio n. 5
0
 def _convert_kg2_edge_to_swagger_edge(self, neo4j_edge: Dict[str,
                                                              any]) -> Edge:
     swagger_edge = Edge()
     swagger_edge_key = f"KG2:{neo4j_edge.get('id')}"
     swagger_edge.predicate = neo4j_edge.get("simplified_edge_label")
     swagger_edge.subject = neo4j_edge.get("subject")
     swagger_edge.object = neo4j_edge.get("object")
     swagger_edge.relation = neo4j_edge.get("relation")
     # Add additional properties on KG2 edges as swagger Attribute objects
     other_properties = [
         "provided_by", "publications", "negated", "relation_curie",
         "simplified_relation_curie", "simplified_relation", "edge_label"
     ]
     swagger_edge.attributes = self._create_swagger_attributes(
         other_properties, neo4j_edge)
     is_defined_by_attribute = Attribute(
         name="is_defined_by",
         value="ARAX/KG2",
         type=eu.get_attribute_type("is_defined_by"))
     swagger_edge.attributes.append(is_defined_by_attribute)
     return swagger_edge_key, swagger_edge
Esempio n. 6
0
    def _convert_to_swagger_edge(self, subject: str, object: str, name: str,
                                 value: float) -> Tuple[str, Edge]:
        swagger_edge = Edge()
        swagger_edge.predicate = f"biolink:{name}"
        swagger_edge.subject = subject
        swagger_edge.object = object
        swagger_edge_key = f"CHP:{subject}-{name}-{object}"
        swagger_edge.relation = None

        type = "EDAM:data_0951"
        url = "https://github.com/di2ag/chp_client"

        swagger_edge.attributes = [
            Attribute(type=type, name=name, value=str(value), url=url),
            Attribute(name="provided_by",
                      value=self.kp_name,
                      type=eu.get_attribute_type("provided_by")),
            Attribute(name="is_defined_by",
                      value="ARAX",
                      type=eu.get_attribute_type("is_defined_by"))
        ]
        return swagger_edge_key, swagger_edge
Esempio n. 7
0
 def _create_ngd_edge(self, ngd_value: float, subject: str, object: str,
                      pmid_list: list) -> Tuple[str, Edge]:
     ngd_edge = Edge()
     ngd_edge.predicate = self.ngd_edge_predicate
     ngd_edge.subject = subject
     ngd_edge.object = object
     ngd_edge_key = f"NGD:{subject}--{ngd_edge.predicate}--{object}"
     ngd_edge.attributes = [
         Attribute(original_attribute_name=self.ngd_edge_attribute_name,
                   attribute_type_id=self.ngd_edge_attribute_type,
                   value=ngd_value)
     ]
     kp_description = "ARAX's in-house normalized google distance database."
     ngd_edge.attributes += [
         self.decorator.create_attribute("publications", pmid_list),
         eu.get_kp_source_attribute(
             "infores:arax-normalized-google-distance",
             arax_kp=True,
             description=kp_description),
         eu.get_arax_source_attribute(),
         eu.get_computed_value_attribute()
     ]
     return ngd_edge_key, ngd_edge
Esempio n. 8
0
def flip_edge(edge: Edge, new_predicate: str) -> Edge:
    edge.predicate = new_predicate
    original_subject = edge.subject
    edge.subject = edge.object
    edge.object = original_subject
    return edge
Esempio n. 9
0
 def _remap_edge(edge: Edge, new_curie: str, old_curie: str) -> Edge:
     if edge.subject == new_curie:
         edge.subject = old_curie
     if edge.object == new_curie:
         edge.object = old_curie
     return edge