Skip to content

Instantly share code, notes, and snippets.

@greglandrum
Created August 10, 2017 06:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save greglandrum/7f546d1e35c2df537c68a64d887793b8 to your computer and use it in GitHub Desktop.
Save greglandrum/7f546d1e35c2df537c68a64d887793b8 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from rdkit import Chem\n",
"from rdkit.Chem import Draw\n",
"from rdkit.Chem.Draw import IPythonConsole"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def add_formal_charges(m):\n",
" m.UpdatePropertyCache(strict=False)\n",
" for at in m.GetAtoms():\n",
" if at.GetAtomicNum() == 7 and at.GetExplicitValence()==4 and at.GetFormalCharge()==0:\n",
" at.SetFormalCharge(1)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# bogus example:\n",
"mb = \"\"\"\n",
" Mrv1642508101708072D \n",
"\n",
" 23 24 0 0 0 0 999 V2000\n",
" -3.3185 1.1689 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -3.8377 1.8100 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -3.3883 2.5019 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -2.5914 2.2884 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -2.5483 1.4645 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -1.9503 2.8076 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -2.4695 3.4487 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -1.3092 3.3267 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -1.4311 2.1664 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -1.8338 1.0520 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -3.5734 0.3842 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -4.6525 1.6809 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -3.6840 3.2721 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -1.1193 1.4645 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -1.8338 0.2270 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -1.1193 -0.1855 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -0.4049 0.2270 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -0.4049 1.0520 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -2.5483 -0.1855 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -1.0762 -1.0094 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0\n",
" 0.2626 -0.2579 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0\n",
" 0.3096 1.4645 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0\n",
" -1.0331 2.2850 0.0000 H 0 0 0 0 0 0 0 0 0 0 0 0\n",
" 1 2 1 0 0 0 0\n",
" 2 3 2 0 0 0 0\n",
" 4 5 2 0 0 0 0\n",
" 1 5 1 0 0 0 0\n",
" 3 4 1 0 0 0 0\n",
" 4 6 1 0 0 0 0\n",
" 6 7 1 0 0 0 0\n",
" 6 8 1 0 0 0 0\n",
" 6 9 1 0 0 0 0\n",
" 5 10 1 0 0 0 0\n",
" 1 11 1 0 0 0 0\n",
" 2 12 1 0 0 0 0\n",
" 3 13 1 0 0 0 0\n",
" 15 16 1 0 0 0 0\n",
" 16 17 2 0 0 0 0\n",
" 17 18 1 0 0 0 0\n",
" 14 18 2 0 0 0 0\n",
" 14 10 1 0 0 0 0\n",
" 10 15 2 0 0 0 0\n",
" 15 19 1 0 0 0 0\n",
" 16 20 1 0 0 0 0\n",
" 17 21 1 0 0 0 0\n",
" 18 22 1 0 0 0 0\n",
" 14 23 1 0 0 0 0\n",
"M END\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"m = Chem.MolFromMolBlock(mb,sanitize=False)\n",
"m.UpdatePropertyCache(strict=False)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'[NH3+]c1cc[nH]c1-c1ccc[nH+]c1'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add_formal_charges(m)\n",
"Chem.MolToSmiles(Chem.RemoveHs(m),isomericSmiles=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment