Skip to content

Commit

Permalink
fix: SMILES parse error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Kohulan committed Feb 23, 2024
1 parent 3820cb2 commit 3ba35b5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
10 changes: 4 additions & 6 deletions app/modules/coconut/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,16 @@ def get_mol_block(input_text: str) -> str:
Raises:
ValueError: If input_text is not a valid Mol or SMILES.
"""
check = rdkitmodules.is_valid_molecule(input_text)

if check == "smiles":
try:
molecule = parse_input(input_text, "cdk", False)
mol_block = cdk.get_CDK_SDG_mol(
molecule,
V3000=False,
).replace("$$$$\n", "")
return mol_block
elif check == "mol":
return input_text
else:
return "Error!, Check the input text."
except InvalidInputException:
raise InvalidInputException(f"Invalid input SMILES: {input_text}")


def get_molecule_hash(molecule: any) -> dict:
Expand Down Expand Up @@ -109,6 +106,7 @@ def get_COCONUT_preprocessing(

# Original molecule
original_mol = parse_input(input_text, "rdkit", False)

original_mol_block = get_mol_block(input_text)
original_mol_hash = get_molecule_hash(original_mol)
original_representations = get_representations(original_mol)
Expand Down
12 changes: 10 additions & 2 deletions app/modules/toolkits/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def parse_SMILES(smiles: str, framework: str = "rdkit", standardize: bool = Fals
Args:
smiles (str): Input SMILES string.
framework (str): Framework to use for parsing. Default is "rdkit".
standardize (bool): Whether to standardize the molecule. Default is False.
Returns:
Chem.Mol or None: Valid molecule object or None if an error occurs.
Expand All @@ -43,7 +45,7 @@ def parse_SMILES(smiles: str, framework: str = "rdkit", standardize: bool = Fals
try:
smiles = smiles.replace(" ", "+")
if framework == "rdkit":
if smiles.__contains__("R"):
if "R" in smiles:
mol = get_CDK_IAtomContainer(smiles)
mol_str = get_CDK_SDG_mol(mol)
mol = Chem.MolFromMolBlock(mol_str)
Expand All @@ -55,9 +57,15 @@ def parse_SMILES(smiles: str, framework: str = "rdkit", standardize: bool = Fals
mol = get_CDK_IAtomContainer(smiles)
elif framework == "openbabel":
mol = get_ob_mol(smiles)
else:
raise ValueError(f"Invalid framework specified: {framework}")

if mol:
return mol
else:
raise InvalidInputException(name="smiles", value=smiles)
mol = get_CDK_IAtomContainer(smiles)
mol_str = get_CDK_SDG_mol(mol)
return Chem.MolFromMolBlock(mol_str)

except Exception:
raise InvalidInputException(name="smiles", value=smiles)

0 comments on commit 3ba35b5

Please sign in to comment.