|
1 | | -from typing import List, Annotated, Union |
2 | | -from app.core.config import LWREG_CONFIG |
| 1 | +from typing import Annotated |
3 | 2 | from psycopg2.errors import UniqueViolation |
4 | 3 | from app.modules.cdkmodules import getCDKHOSECodes |
5 | | -from fastapi import APIRouter, HTTPException, Body, status, Query |
| 4 | +from fastapi import APIRouter, HTTPException, status, Query |
6 | 5 | from app.modules.rdkitmodules import getRDKitHOSECodes |
7 | | -from lwreg.utils import ( |
8 | | - initdb, |
9 | | - bulk_register, |
10 | | - query, |
11 | | - retrieve, |
12 | | - RegistrationFailureReasons, |
13 | | -) |
14 | | -from app import schemas |
15 | | -from rdkit import Chem |
16 | | -from io import BytesIO |
| 6 | +from app.schemas import HealthCheck |
17 | 7 |
|
18 | 8 | router = APIRouter( |
19 | 9 | prefix="/chem", |
|
23 | 13 | ) |
24 | 14 |
|
25 | 15 |
|
| 16 | +@router.get("/", include_in_schema=False) |
26 | 17 | @router.get( |
27 | 18 | "/health", |
28 | 19 | tags=["healthcheck"], |
29 | 20 | summary="Perform a Health Check on Chem Module", |
30 | | - response_description="Returns HTTP Status Code 200 (OK)", |
| 21 | + response_description="Return HTTP Status Code 200 (OK)", |
31 | 22 | status_code=status.HTTP_200_OK, |
32 | | - response_model=schemas.HealthCheck, |
33 | 23 | include_in_schema=False, |
| 24 | + response_model=HealthCheck, |
34 | 25 | ) |
35 | | -def get_health() -> schemas.HealthCheck: |
| 26 | +def get_health() -> HealthCheck: |
36 | 27 | """ |
37 | 28 | ## Perform a Health Check |
38 | 29 | Endpoint to perform a healthcheck on. This endpoint can primarily be used Docker |
39 | 30 | to ensure a robust container orchestration and management is in place. Other |
40 | 31 | services which rely on proper functioning of the API service will not deploy if this |
41 | 32 | endpoint returns any other HTTP status code except 200 (OK). |
42 | | -
|
43 | 33 | Returns: |
44 | 34 | HealthCheck: Returns a JSON response with the health status |
45 | 35 | """ |
46 | | - return schemas.HealthCheck(status="OK") |
47 | | - |
48 | | - |
49 | | -@router.post( |
50 | | - "/init", |
51 | | - tags=["chem"], |
52 | | - summary="Initializes the registration database", |
53 | | - response_description="Returns boolean indicating the success of the initialisation", |
54 | | - status_code=status.HTTP_200_OK, |
55 | | - response_model=Union[bool, None], |
56 | | -) |
57 | | -async def initialise_database(confirm: Annotated[bool, Body(embed=True)] = False): |
58 | | - """ |
59 | | - ## Initializes the registration database |
60 | | -
|
61 | | - NOTE: This call destroys any existing information in the registration database |
62 | | -
|
63 | | - Arguments: |
64 | | -
|
65 | | - confirm -- if set to False we immediately return |
66 | | - """ |
67 | | - return initdb(config=LWREG_CONFIG, confirm=confirm) |
68 | | - |
69 | | - |
70 | | -@router.post( |
71 | | - "/register", |
72 | | - tags=["chem"], |
73 | | - summary="Registers new molecules", |
74 | | - response_description="Returns the new registry number(s) (molregno). If all entries are duplicates exception is raised", |
75 | | - status_code=status.HTTP_200_OK, |
76 | | - response_model=List[str | int], |
77 | | -) |
78 | | -async def register_compounds( |
79 | | - data: Annotated[ |
80 | | - str, |
81 | | - Body(embed=False, media_type="text/plain"), |
82 | | - ] = "CCCC" |
83 | | -): |
84 | | - """ |
85 | | - ## Registers new molecules, assuming it doesn't already exist, |
86 | | - and returns the new registry number(s) (molregno). If all entries |
87 | | - are duplicates exception is raised |
88 | | -
|
89 | | - #### Only one of the molecule format objects should be provided |
90 | | -
|
91 | | - molblock -- MOL or SDF block |
92 | | - smiles -- smiles |
93 | | - """ |
94 | | - try: |
95 | | - if "$$$$" in data: |
96 | | - molStream = BytesIO(data.encode("utf8")) |
97 | | - mols = [m for m in Chem.ForwardSDMolSupplier(molStream)] |
98 | | - else: |
99 | | - smiles = data.splitlines() |
100 | | - mols = [Chem.MolFromSmiles(smi) for smi in smiles] |
101 | | - if len(mols) > 0: |
102 | | - db_responses = bulk_register(mols=mols, config=LWREG_CONFIG) |
103 | | - reg_responses = [] |
104 | | - ops_total_failure = True |
105 | | - for res in db_responses: |
106 | | - if res == RegistrationFailureReasons.PARSE_FAILURE: |
107 | | - reg_responses.append("PARSE_FAILURE") |
108 | | - elif res == RegistrationFailureReasons.DUPLICATE: |
109 | | - reg_responses.append("DUPLICATE") |
110 | | - else: |
111 | | - ops_total_failure = False |
112 | | - reg_responses.append(res) |
113 | | - if ops_total_failure: |
114 | | - raise |
115 | | - else: |
116 | | - return reg_responses |
117 | | - else: |
118 | | - raise |
119 | | - except Exception as e: |
120 | | - raise HTTPException( |
121 | | - status_code=422, |
122 | | - detail="Registration failed: ALL_DUPLICATE_ENTRIES " + e.message, |
123 | | - ) |
124 | | - |
125 | | - |
126 | | -@router.get( |
127 | | - "/query", |
128 | | - tags=["chem"], |
129 | | - summary="Queries to see if a molecule has already been registered", |
130 | | - response_model=List[int], |
131 | | - response_description="Returns the corresponding registry numbers (molregnos)", |
132 | | - status_code=status.HTTP_200_OK, |
133 | | -) |
134 | | -async def query_compounds(smi: str): |
135 | | - """ |
136 | | - ## Queries to see if a molecule has already been registered |
137 | | -
|
138 | | - Returns: |
139 | | - Corresponding registry numbers (molregnos) |
140 | | - """ |
141 | | - try: |
142 | | - res = query(smiles=smi, config=LWREG_CONFIG) |
143 | | - return res |
144 | | - except Exception as e: |
145 | | - raise HTTPException( |
146 | | - status_code=500, |
147 | | - detail="Internal Server Error" + e.message, |
148 | | - ) |
149 | | - |
150 | | - |
151 | | -@router.post( |
152 | | - "/retrieve", |
153 | | - tags=["chem"], |
154 | | - summary="Retrieves entries based on the list of ids provided", |
155 | | - response_model=tuple(), |
156 | | - response_description="Returns HTTP Status Code 200 (OK)", |
157 | | - status_code=status.HTTP_200_OK, |
158 | | -) |
159 | | -async def retrieve_compounds(ids: List[int]): |
160 | | - """ |
161 | | - ## Retrieves entries based on the ids provided |
162 | | -
|
163 | | - Returns: |
164 | | - Molecule data for one or more registry ids (molregnos). |
165 | | - The return value is a tuple of (molregno, data, format) 3-tuples |
166 | | - """ |
167 | | - try: |
168 | | - res = retrieve(ids=ids, config=LWREG_CONFIG) |
169 | | - return res |
170 | | - except Exception as e: |
171 | | - raise HTTPException( |
172 | | - status_code=500, |
173 | | - detail="Internal Server Error" + e.message, |
174 | | - ) |
| 36 | + return HealthCheck(status="OK") |
175 | 37 |
|
176 | 38 |
|
177 | 39 | @router.get( |
178 | 40 | "/hosecode", |
179 | | - tags=["chem"], |
| 41 | + tags=["registration"], |
180 | 42 | summary="Generates HOSE codes of molecule", |
181 | 43 | response_model=list[str], |
182 | 44 | response_description="Returns an array of hose codes generated", |
|
0 commit comments