Skip to content
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

feat: add first draft of nomeclature converters #10

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,6 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.vscode
.DS_Store
_version.py
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = {file = "LICENSE"}
readme = "README.md"
requires-python = ">=3.9"
dynamic = ["version"]
dependencies = ["modal", "rdkit", "mendeleev", "fire"]
dependencies = ["modal", "fire"]

[project.optional-dependencies]
dev = ["pytest", "pre-commit", "ruff", "pytest-asyncio"]
Expand Down
185 changes: 185 additions & 0 deletions src/chemenv/modal_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
get_number_of_topologically_distinct_atoms as _get_topologically_distinct_atoms,
get_element_info as _get_element_info,
)
from chemenv.tools.converters import (
_converters_image,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what was the reason for making all of them semantically private (leading _)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is that most of the functions of the app are called the same as the working functions, or they were at some point

_safe_image,
_Smiles2Name,
_Name2Smiles,
_smiles_to_selfies,
_smiles_to_deepsmiles,
_smiles_to_inchi,
_smiles_to_inchikey,
_smiles_to_safe,
_selfies_to_smiles,
_inchi_to_smiles,
_deepsmiles_to_smiles,
)
import os

# Define the images
Expand All @@ -17,6 +31,7 @@
if chemenv_name and not chemenv_name.startswith("-"):
chemenv_name = f"-{chemenv_name}"


# Create the app
app = App(f"chemenv{chemenv_name}")

Expand All @@ -34,3 +49,173 @@ def get_number_of_topologically_distinct_atoms(*args, **kwargs):
@app.function(image=mendeleev_image)
def get_element_info(*args, **kwargs):
return _get_element_info(*args, **kwargs)


@app.function(image=_converters_image)
async def get_iupac_name(smiles: str, timeout: int = 10) -> str:
"""
Get the IUPAC name of a molecule from its SMILES string.

Args:
smiles (str): The SMILES string of the molecule.
timeout (int): The timeout in seconds for the request.

Returns:
str: The IUPAC name of the molecule.

Raises:
ValueError: If the conversion fails.
"""
converter = _Smiles2Name(smiles, timeout)
try:
name = await converter.get_name()
return name
except Exception as e:
raise ValueError(f"Error: {e}") from e


@app.function(image=_converters_image)
async def get_smiles_from_name(name: str, timeout: int = 10) -> str:
"""
Get the SMILES string of a molecule from its IUPAC name.

Args:
name (str): The IUPAC name of the molecule.
timeout (int): The timeout in seconds for the request.

Returns:
str: The SMILES string of the molecule.

Raises:
ValueError: If the conversion fails.
"""
converter = _Name2Smiles(name, timeout)
try:
smiles = await converter.get_smiles()
return smiles
except Exception as e:
raise ValueError(f"Error converting name to SMILES: {e}") from e


@app.function(image=_converters_image)
def convert_to_selfies(smiles: str) -> str:
"""
Convert SMILES to SELFIES encoding.

Args:
smiles (str): The SMILES string to convert.

Returns:
str: The SELFIES encoding of the molecule.
"""
return _smiles_to_selfies(smiles)


@app.function(image=_converters_image)
def convert_to_deepsmiles(smiles: str) -> str:
"""
Convert SMILES to DeepSMILES encoding.

Args:
smiles (str): The SMILES string to convert.

Returns:
str: The DeepSMILES encoding of the molecule.
"""
return _smiles_to_deepsmiles(smiles)


@app.function(image=_converters_image)
def convert_to_inchi(smiles: str) -> str:
"""
Convert SMILES to InChI.

Args:
smiles (str): The SMILES string to convert.

Returns:
str: The InChI encoding of the molecule.
"""
try:
return _smiles_to_inchi(smiles)
except Exception as e:
raise ValueError(f"Error converting SMILES to InChI: {e}") from e


@app.function(image=_converters_image)
def convert_to_inchikey(smiles: str) -> str:
"""
Convert SMILES to InChIKey.

Args:
smiles (str): The SMILES string to convert.

Returns:
str: The InChIKey encoding of the molecule.
"""
try:
return _smiles_to_inchikey(smiles)
except Exception as e:
raise ValueError(f"Error converting SMILES to InChIKey: {e}") from e


@app.function(image=_safe_image)
def convert_to_safe(smiles: str) -> str:
"""
Convert SMILES to SAFE encoding.

Args:
smiles (str): The SMILES string to convert.

Returns:
str: The SAFE encoding of the molecule.
"""
return _smiles_to_safe(smiles)


@app.function(image=_converters_image)
def selfies_to_smiles(selfies: str) -> str:
"""
Convert SELFIES to SMILES.

Args:
selfies (str): The SELFIES encoding to convert.

Returns:
str: The SMILES string of the molecule.
"""
return _selfies_to_smiles(selfies)


@app.function(image=_converters_image)
def inchi_to_smiles(inchi: str) -> str:
"""
Convert InChI to SMILES.

Args:
inchi (str): The InChI encoding to convert.

Returns:
str: The SMILES string of the molecule.
"""
try:
return _inchi_to_smiles(inchi)
except Exception as e:
raise ValueError(f"Error converting InChI to SMILES: {e}") from e


@app.function(image=_converters_image)
def deepsmiles_to_smiles(deepsmiles: str) -> str:
"""
Convert DeepSMILES to SMILES.

Args:
deepsmiles (str): The DeepSMILES encoding to convert.

Returns:
str: The SMILES string of the molecule.
"""
try:
return _deepsmiles_to_smiles(deepsmiles)
except Exception as e:
raise ValueError(f"Error converting DeepSMILES to SMILES: {e}") from e
Loading
Loading