Platform wheels for RDKit - a comprehensive cheminformatics and machine-learning library with Python bindings
89
Fundamental molecular representation, manipulation, and I/O operations that form the foundation of RDKit's cheminformatics capabilities. These functions handle molecule creation from various chemical formats, basic molecular property access, format conversions, and core molecular operations.
Convert between SMILES strings and molecular objects for basic chemical structure representation.
def MolFromSmiles(smiles: str, sanitize: bool = True) -> Mol:
"""
Create a molecule from a SMILES string.
Parameters:
- smiles: SMILES string representation
- sanitize: Whether to sanitize the molecule (default True)
Returns:
Mol object or None if parsing fails
"""
def MolToSmiles(mol: Mol, canonical: bool = True, isomericSmiles: bool = True,
kekuleSmiles: bool = False, rootedAtAtom: int = -1,
allBondsExplicit: bool = False, allHsExplicit: bool = False) -> str:
"""
Convert a molecule to SMILES string.
Parameters:
- mol: Input molecule
- canonical: Generate canonical SMILES (default True)
- isomericSmiles: Include stereochemistry information (default True)
- kekuleSmiles: Generate Kekule form (default False)
- rootedAtAtom: Root SMILES at specific atom index (default -1)
- allBondsExplicit: Make all bonds explicit (default False)
- allHsExplicit: Make all hydrogens explicit (default False)
Returns:
SMILES string representation
"""Handle MOL block format for detailed molecular structure representation including 3D coordinates.
def MolFromMolBlock(molblock: str, sanitize: bool = True,
removeHs: bool = True, strictParsing: bool = True) -> Mol:
"""
Create a molecule from a MOL block string.
Parameters:
- molblock: MOL format string
- sanitize: Whether to sanitize the molecule (default True)
- removeHs: Remove explicit hydrogens (default True)
- strictParsing: Use strict parsing rules (default True)
Returns:
Mol object or None if parsing fails
"""
def MolToMolBlock(mol: Mol, includeStereo: bool = True,
confId: int = -1, kekulize: bool = True) -> str:
"""
Convert a molecule to MOL block format.
Parameters:
- mol: Input molecule
- includeStereo: Include stereochemistry (default True)
- confId: Conformer ID to use (default -1 for default conformer)
- kekulize: Convert to Kekule form (default True)
Returns:
MOL block string representation
"""Read and write Structure Data Files containing multiple molecules with associated data.
def SDMolSupplier(fileName: str, sanitize: bool = True,
removeHs: bool = True, strictParsing: bool = True):
"""
Create an SDF file supplier for reading multiple molecules.
Parameters:
- fileName: Path to SDF file
- sanitize: Whether to sanitize molecules (default True)
- removeHs: Remove explicit hydrogens (default True)
- strictParsing: Use strict parsing rules (default True)
Returns:
SDMolSupplier iterator object
"""
def SDWriter(fileName: str):
"""
Create an SDF file writer for writing multiple molecules.
Parameters:
- fileName: Output SDF file path
Returns:
SDWriter object
"""Basic molecular editing operations including hydrogen addition/removal and sanitization.
def AddHs(mol: Mol, explicitOnly: bool = False, addCoords: bool = False) -> Mol:
"""
Add explicit hydrogens to a molecule.
Parameters:
- mol: Input molecule
- explicitOnly: Only add hydrogens that are already explicit (default False)
- addCoords: Add coordinates for new hydrogens (default False)
Returns:
New molecule with explicit hydrogens
"""
def RemoveHs(mol: Mol, implicitOnly: bool = False,
updateExplicitCount: bool = False, sanitize: bool = True) -> Mol:
"""
Remove explicit hydrogens from a molecule.
Parameters:
- mol: Input molecule
- implicitOnly: Only remove implicit hydrogens (default False)
- updateExplicitCount: Update implicit hydrogen counts (default False)
- sanitize: Sanitize after removal (default True)
Returns:
New molecule with hydrogens removed
"""
def SanitizeMol(mol: Mol, sanitizeOps: int = None, catchErrors: bool = False) -> None:
"""
Sanitize a molecule by kekulizing, setting aromaticity, etc.
Parameters:
- mol: Molecule to sanitize (modified in place)
- sanitizeOps: Bitmask of operations to perform (default all)
- catchErrors: Catch and ignore errors (default False)
Returns:
None (modifies molecule in place)
"""Access basic molecular properties and structural information.
def GetMolProps(mol: Mol) -> dict:
"""
Get all molecular properties as a dictionary.
Parameters:
- mol: Input molecule
Returns:
Dictionary of property name/value pairs
"""
def SetMolProp(mol: Mol, key: str, val: str) -> None:
"""
Set a molecular property.
Parameters:
- mol: Target molecule
- key: Property name
- val: Property value
Returns:
None (modifies molecule in place)
"""
def GetMolProp(mol: Mol, key: str) -> str:
"""
Get a molecular property value.
Parameters:
- mol: Input molecule
- key: Property name
Returns:
Property value as string
"""from rdkit import Chem
# Parse SMILES and handle errors
smiles = "CCO"
mol = Chem.MolFromSmiles(smiles)
if mol is not None:
canonical_smiles = Chem.MolToSmiles(mol)
print(f"Canonical SMILES: {canonical_smiles}")
else:
print("Invalid SMILES string")from rdkit import Chem
# Read molecules from SDF file
supplier = Chem.SDMolSupplier('compounds.sdf')
for mol in supplier:
if mol is not None:
smiles = Chem.MolToSmiles(mol)
print(f"Molecule: {smiles}")
# Write molecules to SDF file
writer = Chem.SDWriter('output.sdf')
mol1 = Chem.MolFromSmiles('CCO')
mol2 = Chem.MolFromSmiles('CCCO')
writer.write(mol1)
writer.write(mol2)
writer.close()from rdkit import Chem
# Add and remove hydrogens
mol = Chem.MolFromSmiles('CCO')
mol_with_hs = Chem.AddHs(mol)
print(f"Atoms with Hs: {mol_with_hs.GetNumAtoms()}")
mol_no_hs = Chem.RemoveHs(mol_with_hs)
print(f"Atoms without Hs: {mol_no_hs.GetNumAtoms()}")Install with Tessl CLI
npx tessl i tessl/pypi-rdkitevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10