| name | coot-rdkit |
| description | RDKit molecular manipulation and visualization within Coot's Python environment. Use when working with Coot and need to (1) Create RDKit molecules from Coot monomers, (2) Modify molecular structures (e.g., atom substitution), (3) Generate 2D chemical structure diagrams, (4) Perform cheminformatics operations on ligands or small molecules loaded in Coot. |
Coot-RDKit Integration
This skill provides guidance for using RDKit within Coot's Python environment for molecular manipulation and visualization.
Key Integration Points
Module Import
Use coot_headless_api (NOT chapi):
import coot_headless_api
from rdkit import Chem
from rdkit.Chem import AllChem
Creating RDKit Molecules from Coot Monomers
import coot_headless_api
import base64
from rdkit import Chem
molecules = coot_headless_api.molecules_container_t(False)
imol = molecules.get_monomer("AMP")
pickle_base64_str = molecules.get_rdkit_mol_pickle_base64("AMP", imol)
pickle_bytes = base64.b64decode(pickle_base64_str)
rdkit_mol = Chem.Mol(pickle_bytes)
Molecular Manipulation
Atom Substitution
from rdkit import Chem
mol_edit = Chem.RWMol(rdkit_mol)
for atom in mol_edit.GetAtoms():
if atom.GetSymbol() == 'P':
atom.SetAtomicNum(16)
break
modified_mol = mol_edit.GetMol()
Chem.SanitizeMol(modified_mol)
2D Structure Visualization
CRITICAL: Always Regenerate 2D Coordinates
When removing hydrogens or modifying structure, ALWAYS regenerate 2D coordinates:
from rdkit.Chem import AllChem
mol_no_h = Chem.RemoveHs(mol)
AllChem.Compute2DCoords(mol_no_h)
Generating SVG Diagrams
from rdkit.Chem.Draw import rdMolDraw2D
drawer = rdMolDraw2D.MolDraw2DSVG(400, 400)
drawer.DrawMolecule(mol_no_h)
drawer.FinishDrawing()
svg_string = drawer.GetDrawingText()
Data Handling Best Practices
NEVER Display Large String Data
Do NOT return or print large strings (SVG, base64, etc.) as this causes slow response times:
BAD:
svg_data
GOOD:
Efficient File Writing
Write files directly without displaying content:
From Coot Python:
svg_content = drawer.GetDrawingText()
Then in bash or file creation:
Coot Python Limitations
Single-Line Return Values Only
Coot's Python environment only returns values from single-line expressions:
Works:
Chem.MolToSmiles(mol)
Doesn't return properly:
x = 5
y = 10
x + y
Workaround - Define function in one call, execute in next:
def my_function():
x = 5
y = 10
return x + y
my_function()
Common Workflows
Modify and Visualize Ligand
- Load monomer from Coot library
- Convert to RDKit molecule
- Make modifications (atom substitution, etc.)
- Remove hydrogens if desired for cleaner diagram
- Regenerate 2D coordinates (critical!)
- Generate SVG diagram
- Save to file without displaying