Skip to content

Commit

Permalink
Merge pull request #344 from nanglo123/add_dkg_resource
Browse files Browse the repository at this point in the history
Add resources to dkg
  • Loading branch information
bgyori authored Jul 23, 2024
2 parents 56267f1 + 1395248 commit 7d98f51
Show file tree
Hide file tree
Showing 9 changed files with 881 additions and 26 deletions.
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ RUN wget -O /sw/nodes.tsv.gz https://askem-mira.s3.amazonaws.com/dkg/$domain/bui

# Python packages
RUN python -m pip install --upgrade pip && \
python -m pip install git+https://github.com/gyorilab/mira.git@main#egg=mira[web,uvicorn,dkg-client] && \
python -m pip install git+https://github.com/gyorilab/mira.git@main#egg=mira[web,uvicorn,dkg-client,dkg-construct] && \
python -m pip uninstall -y flask_bootstrap && \
python -m pip uninstall -y bootstrap_flask && \
python -m pip install bootstrap_flask && \
Expand Down
34 changes: 33 additions & 1 deletion mira/dkg/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import itertools as itt
import os
from typing import Any, List, Mapping, Optional, Union, Dict
from typing import Any, List, Mapping, Optional, Union

import pydantic
from fastapi import APIRouter, Body, HTTPException, Path, Query, Request
Expand All @@ -13,6 +13,7 @@

from mira.dkg.client import AskemEntity, Entity, Relation
from mira.dkg.utils import DKG_REFINER_RELS
from mira.dkg.construct import add_resource_to_dkg

__all__ = [
"api_blueprint",
Expand Down Expand Up @@ -360,6 +361,37 @@ def add_relations(
request.app.state.client.add_relation(relation)


@api_blueprint.post(
"/add_resources",
response_model=None,
tags=["relations"],
)
def add_resources(
request: Request,
resource_prefix_list: List[str] = Body(
...,
description="A of resources to add to the DKG",
title="Resource Prefixes",
example=["probonto", "wikidata", "eiffel", "geonames", "ncit",
"nbcbitaxon"],
)
):
"""From a list of resource prefixes, add a list of nodes and edges
extract from each resource to the DKG"""
for resource_prefix in resource_prefix_list:
# nodes and edges will be a list of dicts
nodes, edges = add_resource_to_dkg(resource_prefix.lower())
# node_info and edge_info are dictionaries that will be
# unpacked when creating instances of entities and relations
entities = [Entity(**node_info) for node_info in nodes]
relations = [Relation(**edge_info) for edge_info in edges]

for entity in entities:
request.app.state.client.add_node(entity)
for relation in relations:
request.app.state.client.add_relation(relation)


class IsOntChildResult(BaseModel):
"""Result of a query to /is_ontological_child"""

Expand Down
75 changes: 59 additions & 16 deletions mira/dkg/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def from_data(cls, data):
-------
A MIRA entity
"""

if isinstance(data, neo4j.graph.Node):
data = dict(data.items())
properties = defaultdict(list)
Expand All @@ -185,6 +186,8 @@ def from_data(cls, data):
):
synonyms.append(Synonym(value=value, type=type))
xrefs = []


for curie, type in zip(
data.pop("xrefs", []),
data.pop("xref_types", []),
Expand Down Expand Up @@ -343,29 +346,70 @@ def add_node(self, entity):
entity:
The node object that will be added to the DKG
"""
curie = entity.id
xrefs, xref_types = [], []
synonyms, synonym_types = [], []
property_predicates, property_values = [], []
for xref in entity.xrefs:
xrefs.append(xref.id)
xref_types.append(xref.type)
for synonym in entity.synonyms:
synonyms.append(synonym.value)
synonym_types.append(synonym.type)
for property_predicate, property_value_list in entity.properties.items():
property_predicates.append(property_predicate)
property_values.extend(property_value_list)

_id = entity.id
name = entity.name
type = entity.type
obsolete = entity.obsolete
description = entity.description
synonyms = entity.synonyms
alts = entity.alts
xrefs = entity.xrefs
labels = entity.labels

create_source_node_query = (
f"MERGE (n {{curie: '{curie}', "
f"name: '{name}', "
f"type: '{type}', "
f"obsolete: {obsolete}, "
f"description: '{description}', "
f"synonyms: {synonyms}, "
f"alts: {alts}, "
f"xrefs: {xrefs}, "
f"labels: {labels} }} )"
"MERGE (n {id: $id, "
"type: $type, "
"obsolete: $obsolete"
)

self.create_tx(create_source_node_query)
if name:
create_source_node_query += ", name: $name"
if description:
create_source_node_query += ", description: $description"
if alts:
create_source_node_query += ", alts: $alts"
if labels:
create_source_node_query += ", labels: $labels"
if xrefs:
create_source_node_query += ", xrefs: $xrefs"
create_source_node_query += ", xref_types: $xref_types"
if synonyms:
create_source_node_query += ", synonyms: $synonyms"
create_source_node_query += ", synonym_types: $synonym_types"
if property_predicates:
create_source_node_query += ", property_predicates: $property_predicates"
create_source_node_query += ", property_values: $property_values"

create_source_node_query += "})"

query_parameters = {
"id": _id,
"name": name,
"type": type,
"obsolete": obsolete,
"description": description,
"synonyms": synonyms,
"synonym_types": synonym_types,
"alts": alts,
"xrefs": xrefs,
"xref_types": xref_types,
"labels": labels,
"property_predicates": property_predicates,
"property_values": property_values
}

self.create_tx(create_source_node_query, **query_parameters)

def add_relation(self, relation):
"""Add a relation to the DKG
Expand All @@ -384,8 +428,8 @@ def add_relation(self, relation):
graph = relation.graph

create_relation_query = (
f"MATCH (source_node {{curie: '{source_curie}'}}), "
f"(target_node {{curie: '{target_curie}'}}) "
f"MATCH (source_node {{id: '{source_curie}'}}), "
f"(target_node {{id: '{target_curie}'}}) "
f"MERGE (source_node)-[rel:{type}]->(target_node)"
f"SET rel.pred = '{pred}'"
f"SET rel.source = '{source}'"
Expand All @@ -395,7 +439,6 @@ def add_relation(self, relation):

self.create_tx(create_relation_query)


def create_single_property_node_index(
self,
index_name: str,
Expand Down
Loading

0 comments on commit 7d98f51

Please sign in to comment.