"""Utility operations to perform common chemistry tasks"""fromfunctoolsimportlru_cachefromrdkitimportChem
[docs]defparse_from_molecule_string(mol_string:str)->Chem.Mol:"""Parse an RDKit molecule from either SMILES or InChI Args: mol_string: String representing a molecule Returns: RDKit molecule object """ifmol_string.startswith('InChI='):mol=Chem.MolFromInchi(mol_string,sanitize=False)else:mol=Chem.MolFromSmiles(mol_string,sanitize=False)ifmolisNone:raiseValueError(f'Failed to parse: "{mol_string}"')# Sanitize the molecule, but don't check for bad valencesChem.SanitizeMol(mol,sanitizeOps=(Chem.SANITIZE_ALL-Chem.SANITIZE_PROPERTIES))returnmol
[docs]@lru_cache()defget_inchi_key_from_molecule_string(mol_string:str)->str:"""Get an InChI key from either SMILES or InChI Args: mol_string: String representing a molecule Returns: InChI key """mol=parse_from_molecule_string(mol_string)returnChem.MolToInchiKey(mol)
[docs]defget_baseline_charge(mol_string:str)->int:"""Determine the charge on a molecule from its SMILES string Examples: H<sub>2</sub>O has a baseline charge of 0 NH<sub>4</sub>+ has a baseline charge of +1 Args: mol_string: SMILES string of the molecule Returns: Charge on the molecule """mol=parse_from_molecule_string(mol_string)returnChem.GetFormalCharge(mol)