Skip to content

Instantly share code, notes, and snippets.

@greglandrum
Created April 19, 2021 06:42
Show Gist options
  • Save greglandrum/73c488981caa83da66fd4b08b219a64a to your computer and use it in GitHub Desktop.
Save greglandrum/73c488981caa83da66fd4b08b219a64a to your computer and use it in GitHub Desktop.
Github4043.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"id": "a1463cd1",
"cell_type": "code",
"source": "from rdkit import Chem\nfrom rdkit.Chem import Draw\nfrom rdkit.Chem import rdDepictor\nimport rdkit\nprint(rdkit.__version__)",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": "2021.03.1\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"id": "e72d8cea",
"cell_type": "code",
"source": "m = Chem.MolFromSmiles('C(C(N)=O)c(c)c',sanitize=False)\nChem.SanitizeMol(m,sanitizeOps=(Chem.SanitizeFlags.SANITIZE_ALL^Chem.SanitizeFlags.SANITIZE_KEKULIZE^Chem.SanitizeFlags.SANITIZE_SETAROMATICITY))\nrdDepictor.Compute2DCoords(m)",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "0"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"id": "298db1f8",
"cell_type": "code",
"source": "from IPython.display import SVG\nd2d = Draw.MolDraw2DSVG(250,200)\nd2d.drawOptions().prepareMolsBeforeDrawing=False\nd2d.DrawMolecule(m)\nd2d.FinishDrawing()\nSVG(d2d.GetDrawingText())",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "<IPython.core.display.SVG object>",
"image/svg+xml": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:rdkit=\"http://www.rdkit.org/xml\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\" baseProfile=\"full\" xml:space=\"preserve\" width=\"250px\" height=\"200px\" viewBox=\"0 0 250 200\">\n<!-- END OF HEADER -->\n<rect style=\"opacity:1.0;fill:#FFFFFF;stroke:none\" width=\"250\" height=\"200\" x=\"0\" y=\"0\"> </rect>\n<path class=\"bond-0 atom-0 atom-1\" d=\"M 110.614,56.8516 L 160.238,85.5026\" style=\"fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.0px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\"/>\n<path class=\"bond-3 atom-0 atom-4\" d=\"M 110.614,56.8516 L 60.9886,85.5026\" style=\"fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.0px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\"/>\n<path class=\"bond-1 atom-1 atom-2\" d=\"M 160.238,85.5026 L 181.258,73.3672\" style=\"fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.0px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\"/>\n<path class=\"bond-1 atom-1 atom-2\" d=\"M 181.258,73.3672 L 202.277,61.2319\" style=\"fill:none;fill-rule:evenodd;stroke:#0000FF;stroke-width:2.0px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\"/>\n<path class=\"bond-2 atom-1 atom-3\" d=\"M 154.508,85.5026 L 154.508,109.334\" style=\"fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.0px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\"/>\n<path class=\"bond-2 atom-1 atom-3\" d=\"M 154.508,109.334 L 154.508,133.166\" style=\"fill:none;fill-rule:evenodd;stroke:#FF0000;stroke-width:2.0px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\"/>\n<path class=\"bond-2 atom-1 atom-3\" d=\"M 165.969,85.5026 L 165.969,109.334\" style=\"fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.0px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\"/>\n<path class=\"bond-2 atom-1 atom-3\" d=\"M 165.969,109.334 L 165.969,133.166\" style=\"fill:none;fill-rule:evenodd;stroke:#FF0000;stroke-width:2.0px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\"/>\n<path class=\"bond-4 atom-4 atom-5\" d=\"M 63.8537,80.5401 L 14.2287,51.8891\" style=\"fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.0px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\"/>\n<path class=\"bond-4 atom-4 atom-5\" d=\"M 58.1235,90.4651 L 8.49854,61.8141\" style=\"fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.0px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;stroke-dasharray:6,6\"/>\n<path class=\"bond-5 atom-4 atom-6\" d=\"M 55.2584,85.5026 L 55.2584,142.805\" style=\"fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.0px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\"/>\n<path class=\"bond-5 atom-4 atom-6\" d=\"M 66.7188,85.5026 L 66.7188,142.805\" style=\"fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:2.0px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;stroke-dasharray:6,6\"/>\n<path class=\"atom-2\" d=\"M 206.276 48.7377 L 211.594 57.333 Q 212.121 58.181, 212.969 59.7167 Q 213.817 61.2524, 213.863 61.3441 L 213.863 48.7377 L 216.018 48.7377 L 216.018 64.9656 L 213.794 64.9656 L 208.087 55.5681 Q 207.422 54.4679, 206.712 53.2072 Q 206.024 51.9466, 205.818 51.5569 L 205.818 64.9656 L 203.709 64.9656 L 203.709 48.7377 L 206.276 48.7377 \" fill=\"#0000FF\"/>\n<path class=\"atom-2\" d=\"M 217.966 48.7377 L 220.166 48.7377 L 220.166 55.6368 L 228.464 55.6368 L 228.464 48.7377 L 230.664 48.7377 L 230.664 64.9656 L 228.464 64.9656 L 228.464 57.4705 L 220.166 57.4705 L 220.166 64.9656 L 217.966 64.9656 L 217.966 48.7377 \" fill=\"#0000FF\"/>\n<path class=\"atom-2\" d=\"M 231.451 64.3962 Q 231.844 63.3827, 232.782 62.8229 Q 233.72 62.2481, 235.021 62.2481 Q 236.64 62.2481, 237.547 63.1255 Q 238.455 64.0029, 238.455 65.5611 Q 238.455 67.1495, 237.275 68.632 Q 236.11 70.1145, 233.69 71.8693 L 238.636 71.8693 L 238.636 73.0795 L 231.42 73.0795 L 231.42 72.066 Q 233.417 70.644, 234.597 69.585 Q 235.792 68.5261, 236.367 67.5731 Q 236.942 66.62, 236.942 65.6367 Q 236.942 64.608, 236.428 64.0332 Q 235.913 63.4583, 235.021 63.4583 Q 234.159 63.4583, 233.584 63.8062 Q 233.009 64.1542, 232.6 64.9257 L 231.451 64.3962 \" fill=\"#0000FF\"/>\n<path class=\"atom-3\" d=\"M 152.789 142.85 Q 152.789 138.954, 154.715 136.776 Q 156.64 134.599, 160.238 134.599 Q 163.837 134.599, 165.762 136.776 Q 167.688 138.954, 167.688 142.85 Q 167.688 146.793, 165.739 149.039 Q 163.791 151.262, 160.238 151.262 Q 156.663 151.262, 154.715 149.039 Q 152.789 146.816, 152.789 142.85 M 160.238 149.429 Q 162.714 149.429, 164.043 147.778 Q 165.396 146.105, 165.396 142.85 Q 165.396 139.664, 164.043 138.06 Q 162.714 136.433, 160.238 136.433 Q 157.763 136.433, 156.411 138.037 Q 155.081 139.641, 155.081 142.85 Q 155.081 146.128, 156.411 147.778 Q 157.763 149.429, 160.238 149.429 \" fill=\"#FF0000\"/>\n</svg>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"id": "d0d620ff",
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.9.1",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"gist": {
"id": "",
"data": {
"description": "Github4043.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
@mohammad-saber
Copy link

Thank you very much for your time and effort to prepare this notebook.
I tried to do the same with "Draw.MolsToGridImage" (since I have several smiles and I would like to use smiles as legend to the image).

I tried the following code:

from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem.Draw.MolDrawing import DrawingOptions

smile_1 = 'C(C(N)=O)c(c)c'
smile_2 = 'o(cn)c(c)c'

m1 = Chem.MolFromSmiles(smile_1,sanitize=False)
Chem.SanitizeMol(m1, sanitizeOps=(Chem.SanitizeFlags.SANITIZE_ALL^Chem.SanitizeFlags.SANITIZE_KEKULIZE^Chem.SanitizeFlags.SANITIZE_SETAROMATICITY))
m2 = Chem.MolFromSmiles(smile_2,sanitize=False)
Chem.SanitizeMol(m2, sanitizeOps=(Chem.SanitizeFlags.SANITIZE_ALL^Chem.SanitizeFlags.SANITIZE_KEKULIZE^Chem.SanitizeFlags.SANITIZE_SETAROMATICITY))

UpdatedDrawingOptions = DrawingOptions()
UpdatedDrawingOptions.prepareMolsBeforeDrawing=False

Draw.MolsToGridImage([m1, m2], legends = [smile_1, smile_2], drawOptions=UpdatedDrawingOptions)

But, an error came up:

TypeError: MolsToGridImage() got multiple values for keyword argument 'drawOptions'

I looked at the source code of "Draw.MolsToGridImage". There is an if-condition as below:

if drawOptions is not None:
    d2d.SetDrawOptions(drawOptions)

However, I couldn't understand how to pass Draw Options to "Draw.MolsToGridImage".

Also, if I remove UpdatedDrawingOptions.prepareMolsBeforeDrawing=False and use "Draw.MolsToGridImage" without "drawOptions", I get another error:

AtomKekulizeException: non-ring atom 4 marked aromatic

Sorry for the trouble but any help is really appreciated. Thank you.

@greglandrum
Copy link
Author

please ask rdkit-related questions on the discussions tab in the rdkit github repo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment