|
|
|
|
|
""" |
|
|
Test script for SMILES error handling and validation. |
|
|
""" |
|
|
|
|
|
from rdkit import Chem |
|
|
from rdkit.Chem import Draw |
|
|
|
|
|
def validate_smiles(smiles): |
|
|
"""Validate SMILES string and return error message if invalid.""" |
|
|
if not smiles or not smiles.strip(): |
|
|
return "Please enter a SMILES string" |
|
|
|
|
|
mol = Chem.MolFromSmiles(smiles.strip()) |
|
|
if not mol: |
|
|
return f"Invalid SMILES string: '{smiles}'\n\n**Common issues:**\n- Unclosed parentheses or brackets\n- Invalid characters\n- Malformed ring notation\n- Incorrect stereochemistry notation\n\n**Try:**\n- Simple molecules first (e.g., 'CO' for methanol)\n- Check parentheses balance\n- Use standard SMILES notation" |
|
|
|
|
|
return None |
|
|
|
|
|
def test_smiles_validation(): |
|
|
"""Test various SMILES strings for validation.""" |
|
|
|
|
|
test_cases = [ |
|
|
|
|
|
("CO", "Methanol - should work"), |
|
|
("CCO", "Ethanol - should work"), |
|
|
("c1ccccc1", "Benzene - should work"), |
|
|
("C[C@H](N)C(=O)O", "Alanine - should work"), |
|
|
|
|
|
|
|
|
("", "Empty string - should fail"), |
|
|
("invalid", "Invalid text - should fail"), |
|
|
("C([C@@H]1[C@H]([C@@H]([C@H]([C@H](O1)O)O)O)O", "Complex glucose - should fail"), |
|
|
("C(C", "Unclosed parentheses - should fail"), |
|
|
("C1C1", "Invalid ring - should fail"), |
|
|
("C[invalid]", "Invalid bracket - should fail"), |
|
|
] |
|
|
|
|
|
print("π§ͺ Testing SMILES Validation") |
|
|
print("=" * 50) |
|
|
|
|
|
for smiles, description in test_cases: |
|
|
print(f"\nπ Testing: {description}") |
|
|
print(f"SMILES: '{smiles}'") |
|
|
|
|
|
error = validate_smiles(smiles) |
|
|
if error: |
|
|
print(f"β Validation failed: {error[:100]}...") |
|
|
else: |
|
|
print("β
Validation passed") |
|
|
|
|
|
print("-" * 30) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
test_smiles_validation() |
|
|
|