Skip to content

Commit

Permalink
fix: flake8 errors fix
Browse files Browse the repository at this point in the history
  • Loading branch information
CS76 committed Aug 8, 2023
1 parent ff8a87b commit 927cc1c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
37 changes: 36 additions & 1 deletion app/routers/chem.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import Annotated
from psycopg2.errors import UniqueViolation
from app.modules.cdkmodules import getCDKHOSECodes
from fastapi import APIRouter, HTTPException, status, Query
from fastapi import APIRouter, HTTPException, status, Query, Body
from app.modules.rdkitmodules import getRDKitHOSECodes
from app.schemas import HealthCheck
from app.schemas.alatis import AlatisModel
import requests

router = APIRouter(
prefix="/chem",
Expand Down Expand Up @@ -76,3 +78,36 @@ async def HOSE_Codes(
detail="Error paring the structure " + e.message,
headers={"X-Error": "RDKit molecule input parse error"},
)


@router.post(
"/label-atoms",
tags=["chem"],
summary="Label atoms using ALATIS naming system",
response_model=AlatisModel,
response_description="",
status_code=status.HTTP_200_OK,
)
async def label_atoms(
data: Annotated[
str,
Body(embed=False, media_type="text/plain"),
]
):
"""
## Generates atom labels for a given molecule
Returns:
JSON with various representations
"""
try:
url = "http://alatis.nmrfam.wisc.edu/upload"
payload = {"input_text": data, "format": "format_", "response_type": "json"}
response = requests.request("POST", url, data=payload)
return response.json()
except Exception as e:
raise HTTPException(
status_code=422,
detail="Error paring the structure " + e.message,
headers={"X-Error": "RDKit molecule input parse error"},
)
22 changes: 13 additions & 9 deletions app/routers/spectra.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from typing import Annotated
from fastapi import APIRouter, HTTPException, status, FastAPI, File, UploadFile
from fastapi.responses import Response
from fastapi import APIRouter, HTTPException, status, UploadFile
from app.schemas import HealthCheck
import subprocess
import json

router = APIRouter(
prefix="/spectra",
Expand All @@ -12,6 +9,7 @@
responses={404: {"description": "Not found"}},
)


@router.get("/", include_in_schema=False)
@router.get(
"/health",
Expand Down Expand Up @@ -41,7 +39,6 @@ def get_health() -> HealthCheck:
summary="Parse the input spectra format and extract metadata",
response_description="",
status_code=status.HTTP_200_OK,
)
async def parse_spectra(file: UploadFile):
"""
Expand All @@ -54,17 +51,24 @@ async def parse_spectra(file: UploadFile):
try:
contents = file.file.read()
file_path = "/tmp/" + file.filename
with open(file_path, 'wb') as f:
with open(file_path, "wb") as f:
f.write(contents)
p = subprocess.Popen("npx nmr-cli -p " + file_path, stdout=subprocess.PIPE, shell=True)
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,
detail="Error paring the structure "
+ e.message
+ ". Error: "
+ err
+ ". Status:"
+ p_status,
headers={"X-Error": "RDKit molecule input parse error"},
)
finally:
file.file.close()
file.file.close()
11 changes: 11 additions & 0 deletions app/schemas/alatis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from __future__ import annotations

from pydantic import BaseModel


class AlatisModel(BaseModel):
html_url: str
inchi: str
key: str
status: str
structure: str

0 comments on commit 927cc1c

Please sign in to comment.