Skip to content

Commit

Permalink
Working trapi 1.4 verision with legacy trapi_model.
Browse files Browse the repository at this point in the history
  • Loading branch information
anonymous-271828 committed May 1, 2023
1 parent 3090bc8 commit 466a4c2
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WORKDIR /usr/src/chp_api
# && apt-get install -y git python3-pip python3-dev
#dos2unix

RUN git clone --single-branch --branch master https://github.com/di2ag/trapi_model.git
RUN git clone --single-branch --branch pydantic-integration-yakaboskic https://github.com/di2ag/trapi_model.git
RUN git clone --single-branch --branch master https://github.com/di2ag/chp_utils.git
RUN git clone --single-branch --branch master https://github.com/di2ag/chp_look_up.git
RUN git clone --single-branch --branch master https://github.com/di2ag/gene-specificity.git
Expand Down
4 changes: 1 addition & 3 deletions chp_api/chp_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
]

INSTALLED_CHP_APPS = [
'chp_look_up',
# 'chp_learn',
'gene_specificity',
]

Expand All @@ -55,7 +53,7 @@
]

# CHP Versions
#VERSIONS = {app_name: app.__version__ for app_name, app in [(app_name, import_module(app_name)) for app_name in INSTALLED_CHP_APPS + OTHER_APPS]}
VERSIONS = {app_name: app.__version__ for app_name, app in [(app_name, import_module(app_name)) for app_name in INSTALLED_CHP_APPS + OTHER_APPS]}

# Sets up installed apps relevent to django
INSTALLED_APPS = INSTALLED_BASE_APPS + INSTALLED_CHP_APPS
Expand Down
3 changes: 2 additions & 1 deletion chp_api/dispatcher/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib import admin

from .models import App, ZenodoFile
from .models import App, ZenodoFile, DispatcherSettings

admin.site.register(App)
admin.site.register(ZenodoFile)
admin.site.register(DispatcherSettings)
11 changes: 7 additions & 4 deletions chp_api/dispatcher/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from collections import defaultdict

from .curie_database import merge_curies_databases, CurieDatabase
from .models import Transaction, App
from .models import Transaction, App, DispatcherSettings

from chp_utils.trapi_query_processor import BaseQueryProcessor
from trapi_model.meta_knowledge_graph import MetaKnowledgeGraph, merge_meta_knowledge_graphs
Expand Down Expand Up @@ -65,13 +65,15 @@ def get_curies(self):
return merge_curies_databases(curies_dbs)

def get_meta_knowledge_graph(self):
# Get current trapi and biolink versions
dispatcher_settings = DispatcherSettings.load()
meta_kgs = []
for app, app_name in zip(APPS, settings.INSTALLED_CHP_APPS):
app_db_obj = App.objects.get(name=app_name)
# Load location from uploaded Zenodo files
if app_db_obj.meta_knowledge_graph_zenodo_file:
meta_kg = app_db_obj.meta_knowledge_graph_zenodo_file.load_file(base_url="https://sandbox.zenodo.org/api/records")
meta_kg = MetaKnowledgeGraph.load('1.3', None, meta_knowledge_graph=meta_kg)
meta_kg = MetaKnowledgeGraph.load(dispatcher_settings.trapi_version, None, meta_knowledge_graph=meta_kg)
# Load default location
else:
get_app_meta_kg_fn = getattr(app, 'get_meta_knowledge_graph')
Expand Down Expand Up @@ -275,14 +277,15 @@ def add_logs_from_query_list(self, target_query, query_list):
target_query.logger.add_logs(query.logger.to_dict())
return target_query

def add_transaction(self, response, chp_app='dispatcher'):
def add_transaction(self, response, app_name='dispatcher'):
app_db_obj = App.objects.get(name=app_name)
# Save the transaction
transaction = Transaction(
id = response.id,
status = response.status,
query = response.to_dict(),
versions = settings.VERSIONS,
chp_app = chp_app,
chp_app = app_db_obj,
)
transaction.save()

Expand Down
23 changes: 23 additions & 0 deletions chp_api/dispatcher/migrations/0006_dispatchersettings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2 on 2023-05-01 16:47

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dispatcher', '0005_zenodofile_remove_app_curies_file_and_more'),
]

operations = [
migrations.CreateModel(
name='DispatcherSettings',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('trapi_version', models.CharField(default='1.4', max_length=28)),
],
options={
'abstract': False,
},
),
]
23 changes: 23 additions & 0 deletions chp_api/dispatcher/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,26 @@ class Transaction(models.Model):
versions = models.JSONField(default=dict)
chp_app = models.ForeignKey(App, on_delete=models.CASCADE, null=True, blank=True)


class Singleton(models.Model):

class Meta:
abstract = True

def save(self, *args, **kwargs):
self.pk = 1
super(Singleton, self).save(*args, **kwargs)

def delete(self, *args, **kwargs):
pass

@classmethod
def load(cls):
obj, _ = cls.objects.get_or_create(pk=1)
return obj

class DispatcherSettings(Singleton):
trapi_version = models.CharField(max_length=28, default='1.4')

def __str__(self):
return 'settings'
5 changes: 5 additions & 0 deletions chp_api/dispatcher/scripts/load_db_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ def run():
app_db_obj, created = App.objects.get_or_create(name=app_name)
if created:
app_db_obj.save()

# Create a dummy app for the dispatcher
app_db_obj, created = App.objects.get_or_create(name='dispatcher')
if created:
app_db_obj.save()
8 changes: 0 additions & 8 deletions chp_api/dispatcher/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@
path('meta_knowledge_graph/', views.meta_knowledge_graph.as_view()),
path('curies/', views.curies.as_view()),
path('versions/', views.versions.as_view()),
path('v1.1/query/', views.query.as_view(trapi_version='1.1')),
path('v1.1/meta_knowledge_graph/', views.meta_knowledge_graph.as_view()),
path('v1.1/curies/', views.curies.as_view(trapi_version='1.1')),
path('v1.1/versions/', views.versions.as_view(trapi_version='1.1')),
path('v1.2/query/', views.query.as_view(trapi_version='1.2')),
path('v1.2/meta_knowledge_graph/', views.meta_knowledge_graph.as_view(trapi_version='1.2')),
path('v1.2/curies/', views.curies.as_view(trapi_version='1.2')),
path('v1.2/versions/', views.versions.as_view(trapi_version='1.2')),
path('transactions/', views.TransactionList.as_view(), name='transaction-list'),
path('recent/', views.RecentTransactionList.as_view(), name='recent-transaction-list'),
path('transactions/<str:pk>/', views.TransactionDetail.as_view(), name='transactions-detail')
Expand Down
59 changes: 35 additions & 24 deletions chp_api/dispatcher/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from datetime import datetime, timedelta

from .base import Dispatcher
from .models import Transaction
from .models import Transaction, DispatcherSettings
from .serializers import TransactionListSerializer, TransactionDetailSerializer

from django.http import HttpResponse, JsonResponse
Expand All @@ -18,19 +18,24 @@


class query(APIView):
trapi_version = '1.3'
def __init__(self, trapi_version='1.3', **kwargs):
self.trapi_version = trapi_version
super(query, self).__init__(**kwargs)


def post(self, request):
# Get current trapi and biolink versions
dispatcher_settings = DispatcherSettings.load()

if request.method == 'POST':
# Initialize Dispatcher
dispatcher = Dispatcher(request, self.trapi_version)
dispatcher = Dispatcher(
request,
dispatcher_settings.trapi_version,
)
# Process Query
query = None
try:
query = dispatcher.process_request(request, trapi_version=self.trapi_version)
query = dispatcher.process_request(
request,
trapi_version=dispatcher_settings.trapi_version,
)
except Exception as e:
if 'Workflow Error' in str(e):
return dispatcher.process_invalid_workflow(request, str(e))
Expand All @@ -40,45 +45,51 @@ def post(self, request):
return dispatcher.get_response(query)

class curies(APIView):
trapi_version = '1.3'
def __init__(self, trapi_version='1.3', **kwargs):
self.trapi_version = trapi_version
super(curies, self).__init__(**kwargs)

def get(self, request):
# Get current trapi and biolink versions
dispatcher_settings = DispatcherSettings.load()

if request.method == 'GET':
# Initialize dispatcher
dispatcher = Dispatcher(request, self.trapi_version)
dispatcher = Dispatcher(
request,
dispatcher_settings.trapi_version,
)

# Get all chp app curies
curies_db = dispatcher.get_curies()
return JsonResponse(curies_db)

class meta_knowledge_graph(APIView):
trapi_version = '1.3'
def __init__(self, trapi_version='1.3', **kwargs):
self.trapi_version = trapi_version
super(meta_knowledge_graph, self).__init__(**kwargs)


def get(self, request):
# Get current trapi and biolink versions
dispatcher_settings = DispatcherSettings.load()

if request.method == 'GET':
# Initialize Dispatcher
dispatcher = Dispatcher(request, self.trapi_version)
dispatcher = Dispatcher(
request,
dispatcher_settings.trapi_version,
)

# Get merged meta KG
meta_knowledge_graph = dispatcher.get_meta_knowledge_graph()
return JsonResponse(meta_knowledge_graph.to_dict())

class versions(APIView):
trapi_version = '1.3'
def __init__(self, trapi_version='1.3', **kwargs):
self.trapi_version = trapi_version
super(version, self).__init__(**kwargs)

def get(self, request):
# Get current trapi and biolink versions
dispatcher_settings = DispatcherSettings.load()

if request.method == 'GET':
# Initialize Dispatcher
dispatcher = Dispatcher(request, self.trapi_version)
dispatcher = Dispatcher(
request,
dispatcher_settings.trapi_version,
)
return JsonResponse(dispatcher.get_versions())

class TransactionList(mixins.ListModelMixin, generics.GenericAPIView):
Expand Down
3 changes: 3 additions & 0 deletions dev-deployment-script
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ docker compose up -d

docker compose run chp-api python3 manage.py migrate

# Load apps
docker compose run chp-api python3 manage.py runscript load_db_apps

docker compose run --user root chp-api python3 manage.py collectstatic --noinput


Expand Down
31 changes: 31 additions & 0 deletions gs-sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"message": {
"query_graph": {
"edges": {
"e0": {
"object": "n1",
"predicates": [
"biolink:expressed_in"
],
"subject": "n0"
}
},
"nodes": {
"n0": {
"categories": [
"biolink:Gene",
"biolink:Protein"
]
},
"n1": {
"categories": [
"biolink:GrossAnatomicalStructure"
],
"ids": [
"UBERON:0000458"
]
}
}
}
}
}

0 comments on commit 466a4c2

Please sign in to comment.