Skip to content

Instantly share code, notes, and snippets.

@iwatobipen
Last active May 11, 2019 08:50
Show Gist options
  • Save iwatobipen/606c9a167d7b488d943668fbaad3eec4 to your computer and use it in GitHub Desktop.
Save iwatobipen/606c9a167d7b488d943668fbaad3eec4 to your computer and use it in GitHub Desktop.
comparison of rdChemReactions and editable mol
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from rdkit import Chem\n",
"from rdkit.Chem import AllChem\n",
"from rdkit.Chem import rdChemReactions\n",
"# from rdkit.Chem import EditableMol\n",
"# Greg suggested to use RWMol instead of EditableMol\n",
"from rdkit.Chem import RWMol"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"mol = Chem.MolFromSmiles('c1ccncc1')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"mol = Chem.AddHs(mol)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"AllChem.EmbedMolecule(mol)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"rxn = rdChemReactions.ReactionFromSmarts('[cH:1]>>[c:1](F)')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"ps = rxn.RunReactants([mol,])\n",
"mols = [p[0] for p in ps]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"m1 = mols[0]\n",
"atoms = m1.GetAtoms()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"c1 = m1.GetConformer()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"C -0.10442110178100782 1.1575512339607825 0.04302466892403103\n",
"F 0.0 0.0 0.0\n",
"C -1.2030075044841062 0.34465105595859574 0.03016638855796142\n",
"C 1.1361907036364194 0.5520704798363746 0.0029571500829689975\n",
"H -0.1931426564236893 2.230796065998375 0.08277829197701424\n",
"C -1.1017065279900353 -1.0368098339316463 -0.020831317255544882\n",
"H -2.1800029609544134 0.8221660760383411 0.06174518137034971\n",
"C 1.2528495602123897 -0.8282336133624136 -0.048225689649723444\n",
"H 2.03804265885753 1.1830130796873037 0.012207819007894026\n",
"N 0.12367737693039976 -1.5610454231756343 -0.057760303715081285\n",
"H -2.01570281978104 -1.608727752662867 -0.027796083317350433\n",
"H 2.247223271777564 -1.2554313683472025 -0.07826610598251835\n"
]
}
],
"source": [
"for i in range(c1.GetNumAtoms()):\n",
" pos = c1.GetAtomPosition(i)\n",
" print(atoms[i].GetSymbol(), pos.x, pos.y, pos.z)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"q = Chem.MolFromSmarts('[H](c)')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"maches = mol.GetSubstructMatches(q)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"atoms = mol.GetAtoms()\n",
"mols2 = []\n",
"for m in maches:\n",
" emol = RWMol(mol)\n",
" emol.ReplaceAtom(m[0], Chem.Atom(9))\n",
" res = emol.GetMol()\n",
" mols2.append(res)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"mol2 = mols2[0]\n",
"c2 = mol2.GetConformer()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"C -0.10442110178100782 1.1575512339607825 0.04302466892403103\n",
"C -1.2030075044841062 0.34465105595859574 0.03016638855796142\n",
"C -1.1017065279900353 -1.0368098339316463 -0.020831317255544882\n",
"N 0.12367737693039976 -1.5610454231756343 -0.057760303715081285\n",
"C 1.2528495602123897 -0.8282336133624136 -0.048225689649723444\n",
"C 1.1361907036364194 0.5520704798363746 0.0029571500829689975\n",
"F -0.1931426564236893 2.230796065998375 0.08277829197701424\n",
"H -2.1800029609544134 0.8221660760383411 0.06174518137034971\n",
"H -2.01570281978104 -1.608727752662867 -0.027796083317350433\n",
"H 2.247223271777564 -1.2554313683472025 -0.07826610598251835\n",
"H 2.03804265885753 1.1830130796873037 0.012207819007894026\n"
]
}
],
"source": [
"atoms2 = mol2.GetAtoms()\n",
"for i in range(c2.GetNumAtoms()):\n",
" pos = c2.GetAtomPosition(i)\n",
" print(atoms2[i].GetSymbol(), pos.x, pos.y, pos.z)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment