$npx -y skills add ai4protein/VenusFactory2 --skill rdkitCheminformatics toolkit for fine-grained molecular control. SMILES/SDF parsing, descriptors (MW, LogP, TPSA), fingerprints, substructure search, 2D/3D generation, similarity, reactions. For standard workflows with simpler interface, use datamol (wrapper around RDKit). Use rdkit f
| 1 | # RDKit Cheminformatics Toolkit |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | RDKit is a comprehensive cheminformatics library providing Python APIs for molecular analysis and manipulation. This skill provides guidance for reading/writing molecular structures, calculating descriptors, fingerprinting, substructure searching, chemical reactions, 2D/3D coordinate generation, and molecular visualization. Use this skill for drug discovery, computational chemistry, and cheminformatics research tasks. |
| 6 | |
| 7 | ## Quick Start and Project Scripts |
| 8 | |
| 9 | Scripts live in `src/tools/bioinfo/rdkit/`. Each module is an executable atomic script with `if __name__ == "__main__"` (CLI via argparse). |
| 10 | |
| 11 | | Script | Purpose | Main | |
| 12 | |--------|---------|------| |
| 13 | | `molecular_properties.py` | Properties: MW, LogP, TPSA, Lipinski, QED; single SMILES or batch file → CSV | Yes (argparse) | |
| 14 | | `substructure_filter.py` | Filter by SMARTS/SMILES include/exclude; pattern libs (functional-groups, rings, pains, privileged) | Yes (argparse) | |
| 15 | | `similarity_search.py` | Fingerprint similarity (Morgan, RDKit, MACCS, atompair, torsion); Tanimoto/Dice/cosine | Yes (argparse) | |
| 16 | |
| 17 | Import from package: |
| 18 | ```python |
| 19 | from src.tools.bioinfo.rdkit import ( |
| 20 | calculate_properties, |
| 21 | process_single_molecule, |
| 22 | process_file, |
| 23 | filter_molecules, |
| 24 | create_pattern_query, |
| 25 | PATTERN_LIBRARIES, |
| 26 | generate_fingerprint, |
| 27 | similarity_search, |
| 28 | FINGERPRINT_METHODS, |
| 29 | ) |
| 30 | # Example: single-molecule properties |
| 31 | props = calculate_properties(Chem.MolFromSmiles("CCO")) |
| 32 | # Example: substructure filter (mols from load_molecules_filter) |
| 33 | from src.tools.bioinfo.rdkit.substructure_filter import load_molecules as load_mols |
| 34 | mols = load_mols("molecules.smi") |
| 35 | filtered, info = filter_molecules(mols, include_patterns=[("benzene", Chem.MolFromSmarts("c1ccccc1"))]) |
| 36 | # Example: similarity (query_mol + list of dicts with 'mol', 'smiles', 'name', 'index') |
| 37 | from src.tools.bioinfo.rdkit.similarity_search import load_molecules as load_db |
| 38 | db = load_db("database.smi") |
| 39 | hits = similarity_search(query_mol, db, method="morgan", threshold=0.7) |
| 40 | ``` |
| 41 | |
| 42 | Run as CLI (each has `main()`): |
| 43 | ```bash |
| 44 | python -m src.tools.bioinfo.rdkit.molecular_properties "CCO" |
| 45 | python -m src.tools.bioinfo.rdkit.molecular_properties --file molecules.smi --output props.csv |
| 46 | python -m src.tools.bioinfo.rdkit.substructure_filter molecules.smi --pattern "c1ccccc1" -o filtered.smi |
| 47 | python -m src.tools.bioinfo.rdkit.similarity_search "CCO" database.smi --threshold 0.7 -o hits.csv |
| 48 | ``` |
| 49 | |
| 50 | References: `references/api_reference.md`, `references/descriptors_reference.md`, `references/smarts_patterns.md`. |
| 51 | |
| 52 | ## Core Capabilities |
| 53 | |
| 54 | ### 1. Molecular I/O and Creation |
| 55 | |
| 56 | **Reading Molecules:** |
| 57 | |
| 58 | Read molecular structures from various formats: |
| 59 | |
| 60 | ```python |
| 61 | from rdkit import Chem |
| 62 | |
| 63 | # From SMILES strings |
| 64 | mol = Chem.MolFromSmiles('Cc1ccccc1') # Returns Mol object or None |
| 65 | |
| 66 | # From MOL files |
| 67 | mol = Chem.MolFromMolFile('path/to/file.mol') |
| 68 | |
| 69 | # From MOL blocks (string data) |
| 70 | mol = Chem.MolFromMolBlock(mol_block_string) |
| 71 | |
| 72 | # From InChI |
| 73 | mol = Chem.MolFromInchi('InChI=1S/C6H6/c1-2-4-6-5-3-1/h1-6H') |
| 74 | ``` |
| 75 | |
| 76 | **Writing Molecules:** |
| 77 | |
| 78 | Convert molecules to text representations: |
| 79 | |
| 80 | ```python |
| 81 | # To canonical SMILES |
| 82 | smiles = Chem.MolToSmiles(mol) |
| 83 | |
| 84 | # To MOL block |
| 85 | mol_block = Chem.MolToMolBlock(mol) |
| 86 | |
| 87 | # To InChI |
| 88 | inchi = Chem.MolToInchi(mol) |
| 89 | ``` |
| 90 | |
| 91 | **Batch Processing:** |
| 92 | |
| 93 | For processing multiple molecules, use Supplier/Writer objects: |
| 94 | |
| 95 | ```python |
| 96 | # Read SDF files |
| 97 | suppl = Chem.SDMolSupplier('molecules.sdf') |
| 98 | for mol in suppl: |
| 99 | if mol is not None: # Check for parsing errors |
| 100 | # Process molecule |
| 101 | pass |
| 102 | |
| 103 | # Read SMILES files |
| 104 | suppl = Chem.SmilesMolSupplier('molecules.smi', titleLine=False) |
| 105 | |
| 106 | # For large files or compressed data |
| 107 | with gzip.open('molecules.sdf.gz') as f: |
| 108 | suppl = Chem.ForwardSDMolSupplier(f) |
| 109 | for mol in suppl: |
| 110 | # Process molecule |
| 111 | pass |
| 112 | |
| 113 | # Multithreaded processing for large datasets |
| 114 | suppl = Chem.MultithreadedSDMolSupplier('molecules.sdf') |
| 115 | |
| 116 | # Write molecules to SDF |
| 117 | writer = Chem.SDWriter('output.sdf') |
| 118 | for mol in molecules: |
| 119 | writer.write(mol) |
| 120 | writer.close() |
| 121 | ``` |
| 122 | |
| 123 | **Important Notes:** |
| 124 | - All `MolFrom*` functions return `None` on failure with error messages |
| 125 | - Always check for `None` before processing molecules |
| 126 | - Molecules are automatically sanitized on import (validates valence, perceives aromaticity) |
| 127 | |
| 128 | ### 2. Molecular Sanitization and Validation |
| 129 | |
| 130 | RDKit automatically sanitizes molecules during parsing, executing 13 steps including valence checking, aromaticity perception, and chirality assignment. |
| 131 | |
| 132 | **Sanitization Control:** |
| 133 | |
| 134 | ```python |
| 135 | # Disable automatic |