| from __future__ import annotations |
|
|
| import json |
|
|
| import httpx |
|
|
|
|
| async def classify(smiles: str) -> dict: |
| """This function queries the ClassyFire API to classify a chemical. |
| |
| compound. |
| |
| represented by a SMILES string. |
| |
| Args: |
| smiles (str): A SMILES string representing the chemical compound. |
| |
| Returns: |
| dict: A dictionary containing the response from the ClassyFire API. |
| |
| Raises: |
| requests.RequestException: If there's an issue with the API request. |
| """ |
|
|
| |
| url = "http://classyfire.wishartlab.com/queries/?format=json" |
|
|
| |
| payload = json.dumps( |
| {"label": "query", "query_input": smiles, "query_type": "STRUCTURE"}, |
| ) |
|
|
| |
| headers = {"Content-Type": "application/json"} |
|
|
| try: |
| |
| |
| timeout = httpx.Timeout(300.0, connect=60.0) |
| async with httpx.AsyncClient(timeout=timeout) as client: |
| response = await client.post(url, headers=headers, content=payload) |
| response.raise_for_status() |
| return response.json() |
| except httpx.HTTPError as e: |
| |
| raise e |
|
|
|
|
| async def result(id: str) -> dict: |
| """Fetches JSON response from the ClassyFire API for a given ID. |
| |
| This function takes an ID and retrieves the corresponding chemical classification |
| information from the ClassyFire API in JSON format. |
| |
| Args: |
| id (int): The ID associated with the chemical compound. |
| |
| Returns: |
| dict: A dictionary containing ClassyFire classification results. |
| The structure of the dictionary includes various classification |
| details of the chemical compound, such as class, superclass, direct |
| parent, etc. |
| |
| Raises: |
| requests.exceptions.RequestException: If there is an issue with the HTTP request |
| to the ClassyFire API. |
| """ |
| url = f"http://classyfire.wishartlab.com/queries/{id}?format=json" |
|
|
| headers = {"Content-Type": "application/json"} |
|
|
| try: |
| |
| timeout = httpx.Timeout(300.0, connect=60.0) |
| async with httpx.AsyncClient(timeout=timeout) as client: |
| response = await client.get(url, headers=headers) |
| response.raise_for_status() |
| return response.json() |
| except httpx.HTTPError as e: |
| |
| raise e |
|
|