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