File size: 1,914 Bytes
43e7ae4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#!/usr/bin/env python3
"""
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 = [
# Valid SMILES
("CO", "Methanol - should work"),
("CCO", "Ethanol - should work"),
("c1ccccc1", "Benzene - should work"),
("C[C@H](N)C(=O)O", "Alanine - should work"),
# Invalid SMILES
("", "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()
|