Rdkit 技能编写
- 作者仓库星标 39
- 作者仓库 awesome-omni-skill
RDKit Skill
This skill provides up-to-date RDKit patterns. LLMs often have outdated RDKit syntax — always follow the patterns here.
Installation
uv pip install rdkit
Core Imports
from rdkit import Chem
from rdkit.Chem import AllChem, Descriptors, Draw, rdFingerprintGenerator
from rdkit.Chem import rdDepictor
from rdkit import DataStructs
Molecule I/O
# From SMILES
mol = Chem.MolFromSmiles('CCO')
# From file
mol = Chem.MolFromMolFile('molecule.mol')
mols = [m for m in Chem.SDMolSupplier('molecules.sdf') if m is not None]
# To SMILES
smiles = Chem.MolToSmiles(mol)
Fingerprints — USE THE NEW API
CRITICAL: Use rdFingerprintGenerator module, NOT the old functions like GetMorganFingerprint().
See references/fingerprints.md for complete documentation.
Quick Reference
from rdkit.Chem import rdFingerprintGenerator
# Morgan/ECFP fingerprints (ECFP4 = radius 2)
mfpgen = rdFingerprintGenerator.GetMorganGenerator(radius=2, fpSize=2048)
# RDKit fingerprint
rdkgen = rdFingerprintGenerator.GetRDKitFPGenerator(fpSize=2048)
# Atom pairs
apgen = rdFingerprintGenerator.GetAtomPairGenerator(fpSize=2048)
# Topological torsions
ttgen = rdFingerprintGenerator.GetTopologicalTorsionGenerator(fpSize=2048)
# Generate fingerprints (same API for all types)
fp = mfpgen.GetFingerprint(mol) # bit vector
cfp = mfpgen.GetCountFingerprint(mol) # count vector
np_fp = mfpgen.GetFingerprintAsNumPy(mol) # numpy array
Drawing Molecules
Use MolDraw2DCairo or MolDraw2DSVG with drawOptions() for customization.
See references/drawing.md for complete documentation.
Quick Reference
from rdkit.Chem import Draw, rdDepictor
# Generate 2D coordinates
rdDepictor.Compute2DCoords(mol)
rdDepictor.StraightenDepiction(mol)
# Simple drawing
img = Draw.MolToImage(mol, size=(300, 300))
img.save('molecule.png')
# Customized drawing
d2d = Draw.MolDraw2DCairo(350, 300)
dopts = d2d.drawOptions()
dopts.addAtomIndices = True # show atom indices
d2d.DrawMolecule(mol)
d2d.FinishDrawing()
with open('molecule.png', 'wb') as f:
f.write(d2d.GetDrawingText())
# Grid of molecules
img = Draw.MolsToGridImage(mols, molsPerRow=4, subImgSize=(200, 200))
Molecular Properties
from rdkit.Chem import Descriptors
mw = Descriptors.MolWt(mol)
logp = Descriptors.MolLogP(mol)
hbd = Descriptors.NumHDonors(mol)
hba = Descriptors.NumHAcceptors(mol)
tpsa = Descriptors.TPSA(mol)
rotatable = Descriptors.NumRotatableBonds(mol)
Substructure Search
# SMARTS pattern matching
pattern = Chem.MolFromSmarts('[OH]')
matches = mol.GetSubstructMatches(pattern) # returns tuple of tuples
# Check if has substructure
has_oh = mol.HasSubstructMatch(pattern)
Similarity
Always specify which fingerprint when reporting similarity — different fingerprints give very different values for the same molecule pair (e.g., RDKit FP: 0.79 vs Morgan2: 0.43).
from rdkit import DataStructs
# Tanimoto similarity between two fingerprints
sim = DataStructs.TanimotoSimilarity(fp1, fp2)
# Bulk similarity (one vs many)
sims = DataStructs.BulkTanimotoSimilarity(fp1, [fp2, fp3, fp4])
Molecule Properties
# Get/set/check properties on molecules, atoms, bonds
mol.SetProp('name', 'aspirin')
mol.GetProp('name')
mol.HasProp('name')
mol.ClearProp('name')
# Typed getters/setters
mol.SetDoubleProp('score', 3.14)
mol.GetDoubleProp('score')
mol.SetIntProp('count', 42)
# Get all properties
mol.GetPropsAsDict() # {key: value, ...}
# Private props (start with _) hidden by default
mol.GetPropsAsDict(includePrivate=True)
Pickling Gotcha
Properties are lost by default when pickling/serializing:
from rdkit import Chem
# Enable property preservation
Chem.SetDefaultPickleProperties(Chem.PropertyPickleOptions.AllProps)
# Or per-molecule
binary = mol.ToBinary(Chem.PropertyPickleOptions.AllProps)
Parsing & Sanitization
Control chemistry perception during parsing. See references/parsing.md for details.
# Custom parsing without sanitization
params = Chem.SmilesParserParams()
params.sanitize = False
params.removeHs = False
mol = Chem.MolFromSmiles(smiles, params=params)
# Manual sanitization after preprocessing
Chem.SanitizeMol(mol)
Chem.AssignStereochemistry(mol, cleanIt=True, force=True)- 流狐分类
- 通用
- 作者声明 Agent
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 88 / 100 · 启发式扫描,不代表运行安全
- 作者 / 版本 / 许可
- @diegosouzapw · 未声明 license
- 流狐 Token 估算
- 低消耗
- 流狐接入估算
- 即装即用
- 是否需要外部 API Key
- 未发现要求
- 检测到的系统要求
- 未声明
- 底层运行要求
- Python
- 检测到的文件与系统行为
-
- 只读
- 允许写入 / 修改
- 检测到的网络行为
- 仅限本地
- 安装命令数
- 无(仅作为资料)
档案由构建时根据 SKILL.md 与安装命令自动衍生,可能与作者实际意图存在差异。
需要注意: 未限定 allowed-tools,默认拥有全部工具权限。
作者没有在当前 SKILL.md 中定义固定输出样例。 Installation
Core Imports
Molecule I/O
CRITICAL: Use rdFingerprintGenerator module, NOT the old functions like GetMorganFingerprint(). See references/fingerprints.md for complete documentation.
Quick Reference
Use MolDraw2DCairo or MolDraw2DSVG with drawOptions() for customization. See references/drawing.md for complete documentation.
# RDKit Skill
This skill provides up-to-date RDKit patterns. **LLMs often have outdated RDKit syntax** — always follow the patterns here.
## Installation
```bash
uv pip install rdkit
```
## Core Imports
```python
from rdkit import Chem
from rdkit.Chem import AllChem, Descriptors, Draw, rdFingerprintGenerator
from rdkit.Chem import rdDepictor
from rdkit import DataStructs
```
## Molecule I/O
```python
# From SMILES
mol = Chem.MolFromSmiles('CCO')
# From file
mol = Chem.MolFromMolFile('molecule.mol')
mols = [m for m in Chem.SDMolSupplier('molecules.sdf') if m is not None]
# To SMILES
smiles = Chem.MolToSmiles(mol)
```
## Fingerprints — USE THE NEW API
**CRITICAL**: Use `rdFingerprintGenerator` module, NOT the old functions like `GetMorganFingerprint()`.
See `references/fingerprints.md` for complete documentation.
### Quick Reference
```python
from rdkit.Chem import rdFingerprintGenerator
# Morgan/ECFP fingerprints (ECFP4 = radius 2)
mfpgen = rdFingerprintGenerator.GetMorganGenerator(radius=2, fpSize=2048)
# RDKit fingerprint
rdkgen = rdFingerprintGenerator.GetRDKitFPGenerator(fpSize=2048)
# Atom pairs
apgen = rdFingerprintGenerator.GetAtomPairGenerator(fpSize=2048)
# Topological torsions
ttgen = rdFingerprintGenerator.GetTopologicalTorsionGenerator(fpSize=2048)
# Generate fingerprints (same API for all types)
fp = mfpgen.GetFingerprint(mol) # bit vector
cfp = mfpgen.GetCountFingerprint(mol) # count vector
np_fp = mfpgen.GetFingerprintAsNumPy(mol) # numpy array
```
## Drawing Molecules
**Use `MolDraw2DCairo` or `MolDraw2DSVG`** with `drawOptions()` for customization.
See `references/drawing.md` for complete documentation.
### Quick Reference
```python
from rdkit.Chem import Draw, rdDepictor
# Generate 2D coordinates
… 作者原文负责流程事实;流狐只索引当前章节、要点、文件与命令。
章节 -> Installation → Core Imports → Molecule I/O → Fingerprints — USE THE NEW API → Quick Reference → Drawing Molecules
要点 -> LLMs often have outdated RDKit syntax · CRITICAL · Use MolDraw2DCairo or MolDraw2DSVG · Always specify which fingerprint · lost by default
文件/命令 -> rdFingerprintGenerator · GetMorganFingerprint() · references/fingerprints.md · MolDraw2DCairo · MolDraw2DSVG · drawOptions() · references/drawing.md · references/parsing.md
内容 SHA-256 -> 273d9de2be1e
原文结构
适用与边界
原文中的明确线索
rdFingerprintGenerator、GetMorganFingerprint()、references/fingerprints.md、MolDraw2DCairo、MolDraw2DSVG、drawOptions()、references/drawing.md、references/parsing.md