Skip to content

Commit

Permalink
feat: add rdkit functions + add pubchem spectra and GHS functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MrtinoRG committed Jan 7, 2025
1 parent 4763ee0 commit e7d8611
Show file tree
Hide file tree
Showing 3 changed files with 843 additions and 119 deletions.
174 changes: 146 additions & 28 deletions src/chemenv/modal_app.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
from modal import App, Image
from modal import App
from chemenv.tools.cheminformatics import (
rdkit_image,
mendeleev_image,
get_tanimoto_similarity as _get_tanimoto_similarity,
get_number_of_topologically_distinct_atoms as _get_topologically_distinct_atoms,
get_element_info as _get_element_info,
_get_number_atoms,
_get_number_heavy_atoms,
_get_canonical_smiles,
_get_compound_charge,
_get_number_rings,
_get_number_aromatic_rings,
_get_aromatic_rings,
_get_ring_sizes,
_get_chiral_centers,
_get_number_chiral_centers,
_get_number_cis_bonds,
_get_number_trans_bonds,
_get_molecular_properties,
_has_substructure,
_get_substructure_count,
)
from chemenv.tools.pubchem import (
PubChem,
pubchem_image as _pubchem_image,
)
from chemenv.tools.pubchem import (
Smiles2Name as _Smiles2Name,
converters_image as _converters_image,
)
import os

# Define the images
rdkit_image = (
Image.debian_slim(python_version="3.12").pip_install("rdkit").pip_install("numpy")
)
mendeleev_image = Image.debian_slim().pip_install("mendeleev")

chemenv_name = os.getenv("CHEMENV_NAME", "")
if chemenv_name and not chemenv_name.startswith("-"):
chemenv_name = f"-{chemenv_name}"
Expand All @@ -38,13 +45,88 @@ def get_number_of_topologically_distinct_atoms(*args, **kwargs):
return _get_topologically_distinct_atoms(*args, **kwargs)


@app.function(image=rdkit_image)
def get_number_atoms(*args, **kwargs):
return _get_number_atoms(*args, **kwargs)


@app.function(image=rdkit_image)
def get_number_heavy_atoms(*args, **kwargs):
return _get_number_heavy_atoms(*args, **kwargs)


@app.function(image=rdkit_image)
def get_canonical_smiles(*args, **kwargs):
return _get_canonical_smiles(*args, **kwargs)


@app.function(image=rdkit_image)
def get_compound_charge(*args, **kwargs):
return _get_compound_charge(*args, **kwargs)


@app.function(image=rdkit_image)
def get_number_rings(*args, **kwargs):
return _get_number_rings(*args, **kwargs)


@app.function(image=rdkit_image)
def get_ring_sizes(*args, **kwargs):
return _get_ring_sizes(*args, **kwargs)


@app.function(image=rdkit_image)
def get_number_aromatic_rings(*args, **kwargs):
return _get_number_aromatic_rings(*args, **kwargs)


@app.function(image=rdkit_image)
def get_aromatic_rings(*args, **kwargs):
return _get_aromatic_rings(*args, **kwargs)


@app.function(image=rdkit_image)
def get_chiral_centers(*args, **kwargs):
return _get_chiral_centers(*args, **kwargs)


@app.function(image=rdkit_image)
def get_number_chiral_centers(*args, **kwargs):
return _get_number_chiral_centers(*args, **kwargs)


@app.function(image=rdkit_image)
def get_number_cis_bonds(*args, **kwargs):
return _get_number_cis_bonds(*args, **kwargs)


@app.function(image=rdkit_image)
def get_number_trans_bonds(*args, **kwargs):
return _get_number_trans_bonds(*args, **kwargs)


@app.function(image=rdkit_image)
def get_molecular_properties(*args, **kwargs):
return _get_molecular_properties(*args, **kwargs)


@app.function(image=rdkit_image)
def has_substructure(*args, **kwargs):
return _has_substructure(*args, **kwargs)


@app.function(image=rdkit_image)
def get_substructure_count(*args, **kwargs):
return _get_substructure_count(*args, **kwargs)


@app.function(image=mendeleev_image)
def get_element_info(*args, **kwargs):
return _get_element_info(*args, **kwargs)


@app.function(image=_pubchem_image)
async def get_number_atoms(compound_id: str) -> int:
async def get_number_atoms_pubchem(compound_id: str) -> int:
pubchem = await PubChem.create(compound_id)
number_atoms = await pubchem._get_number_atoms()
if number_atoms is None:
Expand All @@ -53,7 +135,7 @@ async def get_number_atoms(compound_id: str) -> int:


@app.function(image=_pubchem_image)
async def get_isomeric_smiles(compound_id: str) -> str:
async def get_isomeric_smiles_pubchem(compound_id: str) -> str:
pubchem = await PubChem.create(compound_id)
isomeric_smiles = await pubchem._get_isomeric_smiles()
if isomeric_smiles is None:
Expand All @@ -62,7 +144,7 @@ async def get_isomeric_smiles(compound_id: str) -> str:


@app.function(image=_pubchem_image)
async def get_canonical_smiles(compound_id: str) -> str:
async def get_canonical_smiles_pubchem(compound_id: str) -> str:
pubchem = await PubChem.create(compound_id)
smiles = await pubchem._get_canonical_smiles()
if smiles is None:
Expand All @@ -71,7 +153,7 @@ async def get_canonical_smiles(compound_id: str) -> str:


@app.function(image=_pubchem_image)
async def get_compound_mass(compound_id: str) -> float:
async def get_compound_mass_pubchem(compound_id: str) -> float:
pubchem = await PubChem.create(compound_id)
molecular_weight = await pubchem._get_compound_mass()
if molecular_weight is None:
Expand All @@ -80,7 +162,7 @@ async def get_compound_mass(compound_id: str) -> float:


@app.function(image=_pubchem_image)
async def get_compound_charge(compound_id: str) -> int:
async def get_compound_charge_pubchem(compound_id: str) -> int:
pubchem = await PubChem.create(compound_id)
charge = await pubchem._get_compound_charge()
if charge is None:
Expand All @@ -89,7 +171,7 @@ async def get_compound_charge(compound_id: str) -> int:


@app.function(image=_pubchem_image)
async def get_compound_formula(compound_id: str) -> str:
async def get_compound_formula_pubchem(compound_id: str) -> str:
pubchem = await PubChem.create(compound_id)
formula = await pubchem._get_compound_formula()
if formula is None:
Expand All @@ -98,7 +180,7 @@ async def get_compound_formula(compound_id: str) -> str:


@app.function(image=_pubchem_image)
async def get_number_isomers(compound_id: str) -> int:
async def get_number_isomers_pubchem(compound_id: str) -> int:
pubchem = await PubChem.create(compound_id)
number_isomers = await pubchem._get_number_isomers()
if number_isomers is None:
Expand All @@ -107,7 +189,7 @@ async def get_number_isomers(compound_id: str) -> int:


@app.function(image=_pubchem_image, timeout=86399)
async def get_compound_isomers(*args, **kwargs):
async def get_compound_isomers_pubchem(*args, **kwargs):
pubchem = await PubChem.create(*args, **kwargs)
data = await pubchem._get_compound_isomers()
if data is None:
Expand All @@ -116,7 +198,7 @@ async def get_compound_isomers(*args, **kwargs):


@app.function(image=_pubchem_image)
async def get_number_heavy_atoms(compound_id: str) -> int:
async def get_number_heavy_atoms_pubchem(compound_id: str) -> int:
pubchem = await PubChem.create(compound_id)
number_heavy_atoms = await pubchem._get_number_heavy_atoms()
if number_heavy_atoms is None:
Expand All @@ -125,18 +207,54 @@ async def get_number_heavy_atoms(compound_id: str) -> int:


@app.function(image=_pubchem_image)
async def _get_number_chiral_atoms(compound_id: str) -> int:
async def get_number_chiral_atoms_pubchem(compound_id: str) -> int:
pubchem = await PubChem.create(compound_id)
number_chiral_atoms = await pubchem._get_number_chiral_atoms()
if number_chiral_atoms is None:
raise ValueError("No chiral atoms found")
return number_chiral_atoms


@app.function(image=_converters_image)
async def get_iupac_name(smiles: str) -> str:
converter = _Smiles2Name(smiles)
name = await converter.get_name()
if name is None:
return ""
return name
@app.function(image=_pubchem_image)
async def get_c_nmr_spectra_pubchem(compound_id: str) -> dict:
pubchem = await PubChem.create(compound_id)
c_nmr_spectra = await pubchem._get_c_nmr_spectra()
if c_nmr_spectra is None:
raise ValueError("No C-NMR spectra found")
return c_nmr_spectra


@app.function(image=_pubchem_image)
async def get_h_nmr_spectra_pubchem(compound_id: str) -> dict:
pubchem = await PubChem.create(compound_id)
h_nmr_spectra = await pubchem._get_h_nmr_spectra()
if h_nmr_spectra is None:
raise ValueError("No H-NMR spectra found")
return h_nmr_spectra


@app.function(image=_pubchem_image)
async def get_uv_spectra_pubchem(compound_id: str) -> dict:
pubchem = await PubChem.create(compound_id)
uv_spectra = await pubchem._get_uv_spectra()
if uv_spectra is None:
raise ValueError("No UV spectra found")
return uv_spectra


@app.function(image=_pubchem_image)
async def get_ms_spectra_pubchem(compound_id: str) -> dict:
pubchem = await PubChem.create(compound_id)
ms_spectra = await pubchem._get_ms_spectra()
if ms_spectra is None:
raise ValueError("No MS spectra found")
return ms_spectra


@app.function(image=_pubchem_image)
async def get_ghs_classification_pubchem(compound_id: str) -> str:
pubchem = await PubChem.create(compound_id)
ghs_classification = await pubchem._get_ghs_classification()
if ghs_classification is None:
raise ValueError("No GHS classification found")
return ghs_classification
Loading

0 comments on commit e7d8611

Please sign in to comment.