A Python toolkit for editing molecular structures by manipulating functional groups using RDKit.
- Replace functional groups in molecules
- Remove functional groups from molecules
- Add functional groups to molecules at specific positions
- Support for 34+ common functional groups
- User-friendly interface with functional group names (no SMARTS knowledge required)
Requires RDKit:
pip install rdkit
# or
conda install -c conda-forge rdkitfrom example import ReplaceFunctionalGroup, RemoveFunctionalGroup, AddFunctionalGroup
# Initialize tools
replace_tool = ReplaceFunctionalGroup()
remove_tool = RemoveFunctionalGroup()
add_tool = AddFunctionalGroup()
# Replace hydroxyl with amine in ethanol
result = replace_tool._run_base('CCO', 'hydroxyl', 'primary_amine')
print(result) # Output: CCN
# Remove hydroxyl from ethanol
result = remove_tool._run_base('CCO', 'hydroxyl')
print(result) # Output: CC
# Add carboxyl group to propane at position 0
result = add_tool._run_base('CCC', 'carboxyl', 0)
print(result) # Output: C(C(=O)O)CC- Aromatic:
benzene_ring,pyridine,pyrrole,furan - Oxygen:
hydroxyl,aldehyde,ketone,carboxyl,ester,ether - Nitrogen:
primary_amine,secondary_amine,tertiary_amine,amide,nitro,nitrile - Halogens:
halo,chloro,bromo,fluoro,iodo - Sulfur:
thiol,thioether,disulfide,sulfoxide,sulfone - Phosphorus:
phosphine,phosphate,phosphonate - Others:
imine,oxime,hydrazone,isocyanate,anhydride,borane
- Alkyl chains:
methyl,ethyl,propyl,isopropyl,butyl,tert_butyl
Replace one functional group with another.
result = replace_tool._run_base(base_smiles, old_group_name, new_group_name)Parameters:
base_smiles(str): Input molecule SMILESold_group_name(str): Name of functional group to replacenew_group_name(str): Name of replacement functional group
Remove a functional group from molecule.
result = remove_tool._run_base(base_smiles, group_name)Parameters:
base_smiles(str): Input molecule SMILESgroup_name(str): Name of functional group to remove
Add a functional group at specific atom position.
result = add_tool._run_base(base_smiles, group_name, atom_index)Parameters:
base_smiles(str): Input molecule SMILESgroup_name(str): Name of functional group to addatom_index(int): 0-based index of atom to attach group to
# Convert alcohol to amine
replace_tool._run_base('CCO', 'hydroxyl', 'primary_amine') # → CCN
# Remove halogen from alkyl halide
remove_tool._run_base('CCCl', 'halo') # → CC
# Add benzene ring to alkane
add_tool._run_base('CCC', 'benzene_ring', 1) # → C[CH]C (with benzene attached)
# Convert aldehyde to carboxylic acid
replace_tool._run_base('CC=O', 'aldehyde', 'carboxyl') # → CC(=O)O
# Add nitro group
add_tool._run_base('CCC', 'nitro', 0) # → C(CC)[N+](=O)[O-]The tools provide informative error messages:
- Invalid SMILES strings
- Unknown functional group names
- Out-of-range atom indices
- Failed molecular transformations
constants.py- Functional group definitions (SMARTS patterns and SMILES)example.py- Main tool classes implementing the BaseTool interfacetest.py- Original functional implementations for referenceCLAUDE.md- Development documentation
- All functions return SMILES strings or error messages
- Atom indices are 0-based (first atom is index 0)
- Some complex transformations may fail due to chemical constraints
- The tools attempt to sanitize molecules after modifications