diff --git a/bioapi/Dockerfile b/bioapi/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..23f957a1b3048a85dd64d8590dcdbc098aed0020 --- /dev/null +++ b/bioapi/Dockerfile @@ -0,0 +1,7 @@ +FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7-alpine3.8 + +RUN apk add --no-cache git + +COPY ./requirements.txt requirements.txt +RUN pip install --upgrade pip +RUN pip install -r requirements.txt diff --git a/bioapi/README.md b/bioapi/README.md new file mode 100644 index 0000000000000000000000000000000000000000..06f484a7d206892101c81abed69462b00c667919 --- /dev/null +++ b/bioapi/README.md @@ -0,0 +1,20 @@ +# Bioapi Web service + +This is preliminary documentation for the service. + +This service aims to facilitate aquisition of information from external APIs with clean documentation and a common returned format. + +## Response format + +Each response is comprised two parts: + +```json +{ + 'info': {} + 'response': {} +} +``` + +The first `info` section contains information about the time the request was done on the server and which url has been requested. + +The `response` section contains the response from the API requested. diff --git a/bioapi/app/__init__.py b/bioapi/app/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/bioapi/app/main.py b/bioapi/app/main.py new file mode 100644 index 0000000000000000000000000000000000000000..1c5e4f377d1433dda383901b77d3db2e90e75f94 --- /dev/null +++ b/bioapi/app/main.py @@ -0,0 +1,21 @@ +import logging + +from fastapi import FastAPI + +from routers import kegg_orthology + +app = FastAPI( + title="BioAPI", + description="Access external bioinformatics API", + version="0.0.1", + docs_url="/documentation", + openapi_prefix="/bioapi" +) +logger = logging.getLogger(__name__) + + +app.include_router( + kegg_orthology.router, + prefix="/kegg-orthology", + tags=["KEGG"], +) diff --git a/bioapi/app/routers/__init__.py b/bioapi/app/routers/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/bioapi/app/routers/common/__init__.py b/bioapi/app/routers/common/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/bioapi/app/routers/common/response/__init__.py b/bioapi/app/routers/common/response/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/bioapi/app/routers/common/response/model.py b/bioapi/app/routers/common/response/model.py new file mode 100644 index 0000000000000000000000000000000000000000..cda8c5420011ae408273edb39cddb13e88adafdd --- /dev/null +++ b/bioapi/app/routers/common/response/model.py @@ -0,0 +1,13 @@ +from datetime import datetime + +from pydantic import BaseModel, HttpUrl + + +class InfoModel(BaseModel): + time: datetime + requested_url: HttpUrl + + +class ResponseModel(BaseModel): + info: InfoModel + response: dict diff --git a/bioapi/app/routers/kegg_orthology.py b/bioapi/app/routers/kegg_orthology.py new file mode 100644 index 0000000000000000000000000000000000000000..09e1336d9cad60bffb599a468fae5c43ceba4b63 --- /dev/null +++ b/bioapi/app/routers/kegg_orthology.py @@ -0,0 +1,73 @@ +from datetime import datetime + +from bioapi.togows import TogoWSEntryAPI +from fastapi import APIRouter, HTTPException, Path +from requests.exceptions import HTTPError + +from .common.response.model import ResponseModel + +router = APIRouter() + + +class KeggResponseModel(ResponseModel): + + class Config: + schema_extra = { + "example": { + "info": { + "time": "2020-04-08T10:12:37.815424", + "requested_url": "http://togows.org/entry/kegg-orthology/K06925.json" + }, + "response": { + "entry_id": "K06925", + "name": "tsaE", + "names": [ + "tsaE" + ], + "definition": "tRNA threonylcarbamoyladenosine biosynthesis protein TsaE", + "pathways": {}, + "modules": {}, + "classes": [], + "diseases": {}, + "dblinks": { + "COG": [ + "COG0802" + ] + }, + "genes": {}, + "references": [] + } + } + } + + +@router.get( + "/{kegg_id}", + response_model=KeggResponseModel, + summary="Retrieve Kegg orthology information through togows.org API", + response_description="KEGG entry at the JSON format", +) +async def kegg_orthology( + kegg_id: str = Path( + ..., + description="KEGG orthology ID", + regex="^K[0-9]{5}$" + ) + ): + """ + KEGG orthology ID have the format `KXXXXX`. *example*: `K06925` + + [More information about KEGG orthology](https://www.genome.jp/kegg/ko.html) + """ + kegg_api = TogoWSEntryAPI("kegg-orthology") + try: + response = kegg_api.get(kegg_id) + except HTTPError: + raise HTTPException(status_code=404, detail=f"{kegg_id} not found") + return { + 'info': { + 'time': datetime.now(), + 'requested_url': kegg_api.last_url_requested + }, + 'response': response + } diff --git a/bioapi/requirements.txt b/bioapi/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..990c80e65eb97893a28cba8d429a86573da9c34b --- /dev/null +++ b/bioapi/requirements.txt @@ -0,0 +1 @@ +git+https://github.com/khillion/bioapi.git#egg=bioapi diff --git a/docker-compose.yaml b/docker-compose.yaml index 38fea5fe1fdd0d63a6b80456b6cee88b4a039252..41f1ed4b4c0934327e9217a4f13567a471520ba9 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -106,6 +106,20 @@ services: networks: - main + bioapi: + container_name: bioapi + build: + context: ./bioapi + volumes: + - ./bioapi/app:/app + entrypoint: /start-reload.sh + ports: + - "81:80" + environment: + DEBUG: "true" + networks: + - main + volumes: postgresql-data: django-static: diff --git a/nginx/dev/nginx.conf b/nginx/dev/nginx.conf index 957ca6151c0053dd83468ac263cd4fac0cbf1e43..e1c686e1095a60d0bae1ecf8ea8794947c0d4d9c 100644 --- a/nginx/dev/nginx.conf +++ b/nginx/dev/nginx.conf @@ -13,6 +13,10 @@ http { server backend:8000; } + upstream bioapi { + server bioapi:80; + } + upstream frontend { server frontend:8080; } @@ -22,7 +26,7 @@ http { } upstream portainer { - server portainer:9000; + server portainer:9000; } server { @@ -56,6 +60,13 @@ http { proxy_set_header Host $http_host; } + # bioapi urls + location /bioapi/ { + proxy_pass http://bioapi/; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $http_host; + } + # flower location /flower/ { rewrite ^/flower/(.*)$ /$1 break;