-
Notifications
You must be signed in to change notification settings - Fork 1
[feat] Pubchempy #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[feat] Pubchempy #23
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0c17f53
[feat] pubchempy name_to_smiles
tdadela a93cf12
[feat] pubchempy get_compound_info
tdadela 0d3bab8
[feat] pubchempy get_name_from_smiles
tdadela bc3980a
Merge remote-tracking branch 'origin' into pubchempy
tdadela 715f433
[feat] pubchem return input if valid SMILES is passed to get_smiles_f…
tdadela e7b3367
[fix] apply ruff rules
tdadela 49778f6
Merge remote-tracking branch 'origin' into pubchempy
tdadela ee44087
[fix] re-add pubchempy info to instructions.txt
tdadela 95552b8
[fix] minor typing
tdadela 1bb7ae2
Merge remote-tracking branch 'origin' into pubchempy
tdadela 6702ea0
[fix] raise ModelRetry inside pubchempy tools instead of others
tdadela b5bdae4
[feat] pubchempy example prompts
tdadela 4658fa6
[refactor] swap examples and usage history
tdadela fca6fcf
[chore] add pubchempy info to readme
tdadela File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| import logging | ||
|
|
||
| import pubchempy as pcp | ||
| from pydantic_ai.exceptions import ModelRetry | ||
|
|
||
| from src.reagentai.tools.smiles import is_valid_smiles | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def get_smiles_from_name(compound_name: str) -> str: | ||
| """ | ||
| Retrieve the SMILES string for a chemical compound using its common name via PubChem. | ||
|
|
||
| This function searches the PubChem database to find the canonical SMILES representation | ||
| of a chemical compound based on its common name, IUPAC name, or other identifiers. | ||
| PubChem is a comprehensive chemical database maintained by the NIH that contains | ||
| millions of chemical structures and their properties. | ||
|
|
||
| Args: | ||
| compound_name (str): The name of the chemical compound to search for. | ||
| This can be a common name (e.g., "aspirin", "caffeine"), | ||
| IUPAC name, trade name, or other chemical identifier. | ||
|
|
||
| Returns: | ||
| str: The canonical SMILES string of the compound as found in PubChem. | ||
|
|
||
| Raises: | ||
| ModelRetry: If the compound name is not found in PubChem, if no valid | ||
| SMILES string could be retrieved, or if there's a network issue. | ||
|
|
||
| Example: | ||
| >>> smiles = get_smiles_from_name("aspirin") | ||
| >>> print(smiles) | ||
| "CC(=O)OC1=CC=CC=C1C(=O)O" | ||
|
|
||
| >>> smiles = get_smiles_from_name("caffeine") | ||
| >>> print(smiles) | ||
| "CN1C=NC2=C1C(=O)N(C(=O)N2C)C" | ||
| """ | ||
| logger.info(f"[TASK] [GET_SMILES_FROM_NAME] Arguments: compound_name: {compound_name}") | ||
|
|
||
| if not compound_name or not compound_name.strip(): | ||
| logger.error("Empty or invalid compound name provided") | ||
| raise ModelRetry("Compound name cannot be empty") | ||
|
|
||
| compound_name = compound_name.strip() | ||
|
|
||
| # Check if the input is already a valid SMILES string | ||
| if is_valid_smiles(compound_name): | ||
| logger.info(f"Input appears to be a valid SMILES, returning as is: {compound_name}") | ||
| return compound_name | ||
|
|
||
| try: | ||
| # Search for the compound by name | ||
| compounds = pcp.get_compounds(compound_name, "name") | ||
|
|
||
| if not compounds: | ||
| logger.warning(f"No compounds found for name: {compound_name}") | ||
| raise ModelRetry(f"No compound found in PubChem for name: '{compound_name}'") | ||
|
|
||
| # Get the first (most relevant) compound | ||
| compound = compounds[0] | ||
|
|
||
| # Retrieve the canonical SMILES | ||
| smiles = compound.canonical_smiles | ||
|
|
||
| if not smiles: | ||
| logger.error(f"No SMILES found for compound: {compound_name}") | ||
| raise ModelRetry(f"No SMILES string available for compound: '{compound_name}'") | ||
|
|
||
| logger.info(f"Successfully retrieved SMILES for {compound_name}: {smiles}") | ||
| logger.debug(f"PubChem CID: {compound.cid}") | ||
|
|
||
| return smiles | ||
|
|
||
| except Exception as e: | ||
| if isinstance(e, ModelRetry): | ||
| raise # Re-raise ModelRetry as-is | ||
|
|
||
| logger.error(f"Error retrieving SMILES for {compound_name}: {str(e)}") | ||
|
|
||
| # For all exceptions, wrap in ModelRetry | ||
| error_msg = f"Failed to retrieve SMILES for '{compound_name}': {str(e)}" | ||
| if "connection" in str(e).lower() or "network" in str(e).lower(): | ||
| error_msg = f"Failed to connect to PubChem: {str(e)}" | ||
|
|
||
| raise ModelRetry(error_msg) from e | ||
|
|
||
|
|
||
| def get_compound_info(compound_name: str) -> dict[str, str | list | None]: | ||
| """ | ||
| Retrieve comprehensive information about a chemical compound from PubChem. | ||
|
|
||
| This function provides additional chemical information beyond just the SMILES string, | ||
| including molecular formula, molecular weight, IUPAC name, and other identifiers. | ||
|
|
||
| Args: | ||
| compound_name (str): The name of the chemical compound to search for. | ||
|
|
||
| Returns: | ||
| dict[str, Optional[str]]: A dictionary containing compound information with keys: | ||
| - 'smiles': Canonical SMILES string | ||
| - 'molecular_formula': Molecular formula | ||
| - 'molecular_weight': Molecular weight in g/mol | ||
| - 'iupac_name': IUPAC systematic name | ||
| - 'cid': PubChem Compound ID | ||
| - 'synonyms': List of alternative names (first 5) | ||
|
|
||
| Raises: | ||
| ModelRetry: If the compound name is not found in PubChem or if there's a network issue. | ||
|
|
||
| Example: | ||
| >>> info = get_compound_info("aspirin") | ||
| >>> print(info['smiles']) | ||
| "CC(=O)OC1=CC=CC=C1C(=O)O" | ||
| >>> print(info['molecular_formula']) | ||
| "C9H8O4" | ||
| """ | ||
| logger.info(f"[TASK] [GET_COMPOUND_INFO] Arguments: compound_name: {compound_name}") | ||
|
|
||
| if not compound_name or not compound_name.strip(): | ||
| logger.error("Empty or invalid compound name provided") | ||
| raise ModelRetry("Compound name cannot be empty") | ||
|
|
||
| compound_name = compound_name.strip() | ||
|
|
||
| try: | ||
| # Search for the compound by name | ||
| compounds = pcp.get_compounds(compound_name, "name") | ||
|
|
||
| if not compounds: | ||
| logger.warning(f"No compounds found for name: {compound_name}") | ||
| raise ModelRetry(f"No compound found in PubChem for name: '{compound_name}'") | ||
|
|
||
| # Get the first (most relevant) compound | ||
| compound = compounds[0] | ||
|
|
||
| # Extract comprehensive information | ||
| info = { | ||
| "smiles": getattr(compound, "canonical_smiles", None), | ||
| "molecular_formula": getattr(compound, "molecular_formula", None), | ||
| "molecular_weight": str(getattr(compound, "molecular_weight", None)) | ||
tdadela marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if hasattr(compound, "molecular_weight") | ||
| else None, | ||
| "iupac_name": getattr(compound, "iupac_name", None), | ||
| "cid": str(getattr(compound, "cid", None)) if hasattr(compound, "cid") else None, | ||
| "synonyms": getattr(compound, "synonyms", [])[:5] | ||
| if hasattr(compound, "synonyms") | ||
| else [], | ||
| } | ||
|
|
||
| logger.info(f"Successfully retrieved compound info for {compound_name}") | ||
| logger.debug(f"Compound info: {info}") | ||
|
|
||
| return info | ||
|
|
||
| except Exception as e: | ||
| if isinstance(e, ModelRetry): | ||
| raise # Re-raise ModelRetry as-is | ||
|
|
||
| logger.error(f"Error retrieving compound info for {compound_name}: {str(e)}") | ||
|
|
||
| # For all exceptions, wrap in ModelRetry | ||
| error_msg = f"Failed to retrieve compound info for '{compound_name}': {str(e)}" | ||
| if "connection" in str(e).lower() or "network" in str(e).lower(): | ||
| error_msg = f"Failed to connect to PubChem: {str(e)}" | ||
|
|
||
| raise ModelRetry(error_msg) from e | ||
|
|
||
|
|
||
| def get_name_from_smiles(smiles: str) -> str: | ||
| """ | ||
| Retrieve the best-matching chemical name for a given SMILES string using PubChem. | ||
|
|
||
| Args: | ||
| smiles (str): The SMILES string of the compound. | ||
|
|
||
| Returns: | ||
| str: The best-matching chemical name (IUPAC or synonym) from PubChem. | ||
|
|
||
| Raises: | ||
| ModelRetry: If no compound is found for the SMILES, if no name is available, | ||
| or if there's a network issue connecting to PubChem servers. | ||
| """ | ||
| logger.info(f"[TASK] [GET_NAME_FROM_SMILES] Arguments: smiles: {smiles}") | ||
|
|
||
| if not smiles or not smiles.strip(): | ||
| logger.error("Empty or invalid SMILES provided") | ||
| raise ModelRetry("SMILES string cannot be empty") | ||
|
|
||
| smiles = smiles.strip() | ||
|
|
||
| try: | ||
| compounds = pcp.get_compounds(smiles, "smiles") | ||
| if not compounds: | ||
| logger.warning(f"No compounds found for SMILES: {smiles}") | ||
| raise ModelRetry(f"No compound found in PubChem for SMILES: '{smiles}'") | ||
| compound = compounds[0] | ||
| # Prefer IUPAC name, fall back to first synonym | ||
| name = getattr(compound, "iupac_name", None) | ||
| if not name: | ||
| synonyms = getattr(compound, "synonyms", []) | ||
| if synonyms: | ||
| name = synonyms[0] | ||
| if not name: | ||
| logger.error(f"No name found for SMILES: {smiles}") | ||
| raise ModelRetry(f"No name available for SMILES: '{smiles}'") | ||
| logger.info(f"Successfully retrieved name for SMILES {smiles}: {name}") | ||
| return name | ||
| except Exception as e: | ||
| if isinstance(e, ModelRetry): | ||
| raise # Re-raise ModelRetry as-is | ||
|
|
||
| logger.error(f"Error retrieving name for SMILES {smiles}: {str(e)}") | ||
|
|
||
| # For all exceptions, wrap in ModelRetry | ||
| error_msg = f"Failed to retrieve name for SMILES '{smiles}': {str(e)}" | ||
| if "connection" in str(e).lower() or "network" in str(e).lower(): | ||
| error_msg = f"Failed to connect to PubChem: {str(e)}" | ||
|
|
||
| raise ModelRetry(error_msg) from e | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.