diff --git a/Dockerfile b/Dockerfile index 6518e5d..be2d9f6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -43,4 +43,10 @@ RUN python3 -m pip install uvicorn[standard] COPY ./app /code/app +RUN curl -sL https://deb.nodesource.com/setup_current.x | bash - +RUN apt-get install -y nodejs +RUN npm install -g npm@latest + +RUN npm install -g /code/app/scripts/nmr-cli + CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80", "--workers", "4", "--reload"] \ No newline at end of file diff --git a/README.md b/README.md index 69884ba..d23b77b 100644 --- a/README.md +++ b/README.md @@ -49,5 +49,5 @@ NMRKit is developed and maintained by the [NFDI4Chem partners](https://www.nfdi4 The code for this web application is released under the [MIT license](https://opensource.org/licenses/MIT). -
+ diff --git a/app/main.py b/app/main.py index 0de3ab0..c74c68c 100644 --- a/app/main.py +++ b/app/main.py @@ -5,6 +5,7 @@ from .routers import registration from .routers import chem +from .routers import spectra from fastapi.middleware.cors import CORSMiddleware from app.core import config, tasks @@ -29,6 +30,7 @@ app.include_router(registration.router) app.include_router(chem.router) +app.include_router(spectra.router) app.add_event_handler("startup", tasks.create_start_app_handler(app)) app.add_event_handler("shutdown", tasks.create_stop_app_handler(app)) diff --git a/app/routers/chem.py b/app/routers/chem.py index 20fd730..29dbff8 100644 --- a/app/routers/chem.py +++ b/app/routers/chem.py @@ -38,7 +38,7 @@ def get_health() -> HealthCheck: @router.get( "/hosecode", - tags=["registration"], + tags=["chem"], summary="Generates HOSE codes of molecule", response_model=list[str], response_description="Returns an array of hose codes generated", diff --git a/app/routers/spectra.py b/app/routers/spectra.py new file mode 100644 index 0000000..fa2fd9b --- /dev/null +++ b/app/routers/spectra.py @@ -0,0 +1,70 @@ +from typing import Annotated +from fastapi import APIRouter, HTTPException, status, FastAPI, File, UploadFile +from fastapi.responses import Response +from app.schemas import HealthCheck +import subprocess +import json + +router = APIRouter( + prefix="/spectra", + tags=["spectra"], + dependencies=[], + responses={404: {"description": "Not found"}}, +) + +@router.get("/", include_in_schema=False) +@router.get( + "/health", + tags=["healthcheck"], + summary="Perform a Health Check on Chem Module", + response_description="Return HTTP Status Code 200 (OK)", + status_code=status.HTTP_200_OK, + include_in_schema=False, + response_model=HealthCheck, +) +def get_health() -> HealthCheck: + """ + ## Perform a Health Check + Endpoint to perform a healthcheck on. This endpoint can primarily be used Docker + to ensure a robust container orchestration and management is in place. Other + services which rely on proper functioning of the API service will not deploy if this + endpoint returns any other HTTP status code except 200 (OK). + Returns: + HealthCheck: Returns a JSON response with the health status + """ + return HealthCheck(status="OK") + + +@router.post( + "/parse", + tags=["spectra"], + summary="Parse the input spectra format and extract metadata", + response_description="", + status_code=status.HTTP_200_OK, + +) +async def parse_spectra(file: UploadFile): + """ + ## Parse the spectra file and extract meta-data + Endpoint to uses nmr-load-save to read the input spectra file (.jdx,.nmredata,.dx) and extracts metadata + + Returns: + data: spectra data in json format + """ + try: + contents = file.file.read() + file_path = "/tmp/" + file.filename + with open(file_path, 'wb') as f: + f.write(contents) + p = subprocess.Popen("npx nmr-cli -p " + file_path, stdout=subprocess.PIPE, shell=True) + (output, err) = p.communicate() + p_status = p.wait() + return output + except Exception as e: + raise HTTPException( + status_code=422, + detail="Error paring the structure " + e.message + ". Error: " + err + ". Status:" + p_status, + headers={"X-Error": "RDKit molecule input parse error"}, + ) + finally: + file.file.close() \ No newline at end of file diff --git a/app/scripts/nmr-cli/bin/index.js b/app/scripts/nmr-cli/bin/index.js new file mode 100755 index 0000000..7556d97 --- /dev/null +++ b/app/scripts/nmr-cli/bin/index.js @@ -0,0 +1,69 @@ +#!/usr/bin/env node +const {join,isAbsolute}= require("path"); +const yargs = require("yargs"); +const loader = require("nmr-load-save"); +const fileUtils = require("filelist-utils"); + +const usageMessage ="Usage: nmr-cli -u