Give me a python snippet to calculate the molecular weight of a SMILES string.
- User
Give me a python snippet to calculate the molecular weight of a SMILES string.
Assistant
Here's a Python snippet to calculate the molecular weight of a SMILES string using RDKit:
from rdkit import Chem from rdkit.Chem import Descriptors def calculate_molecular_weight(smiles): """ Calculate the molecular weight of a molecule from its SMILES string. Args: smiles (str): SMILES string representation of the molecule Returns: float: Molecular weight in g/mol, or None if SMILES is invalid """ try: mol = Chem.MolFromSmiles(smiles) if mol is None: print(f"Invalid SMILES string: {smiles}") return None mw = Descriptors.MolWt(mol) return mw except Exception as e: print(f"Error processing SMILES '{smiles}': {e}") return None # Example usage smiles_examples = [ "CCO", # Ethanol "CC(=O)O", # Acetic acid "c1ccccc1", # Benzene "CC(C)C(=O)O" # Isobutyric acid ] for smiles in smiles_examples: mw = calculate_molecular_weight(smiles) if mw: print(f"SMILES: {smiles} -> Molecular Weight: {mw:.2f} g/mol")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.