Skip to content

Instantly share code, notes, and snippets.

@ptosco
Last active September 11, 2018 17:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptosco/baf6168c48c4271bf3c096d53adaaf82 to your computer and use it in GitHub Desktop.
Save ptosco/baf6168c48c4271bf3c096d53adaaf82 to your computer and use it in GitHub Desktop.
LargestNumberOfContiguousRotatableBonds
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.Lipinski import RotatableBondSmarts\n",
"from rdkit.Chem.Draw import IPythonConsole"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"mol = Chem.MolFromSmiles('NCC1CCC1C(=O)O')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAcIAAACWCAIAAADCEh9HAAAEcUlEQVR4nO3d3ZKaShiGUTuV+79l9gEJYSs4ystPN71WzcFkTM2ghU99LYhlGIYHAFv9unoDANomowARGQWIyChAREYBIjIKEJFRgIiMAkRkFCAiowARGQWIyChAREYBIjIKEJFRgIiMAkRkFCAiowARGQWIyChAREYBIjIKEJFRgIiMAkR+X70BsEUpZfp+GIYLtwRklAbMozma0vl6E5xMRqnOm2hChWSUKy3OkqJJW4pdltMcNGaWYjfmSqZRjmJtTidklD05gE6HZJSdqSe9cfo9QERGASIyyp6GYTj/fPhL/ihMZBQgIqMAERkFiMgoQERGASIyys4crKc3MgoQkVGAiIwCRGQUICKjABEZZX+Om9MVGeUmtJuryChAREYBIjIKEJFRgIiMAkRklEO4QAn9kFGAiIwCRGQUICKjABEZBYjIKEdxsJ5OyChA5PfVGwDMzEfpYbhuO/iCjHITd1jLl/K/dD79k1rJKE16jeYwK85469BWg16jOQxK2gQZpQHvo/lqvLWU0lhJaZOMcqDxuPlXLVtcm2+r4Ya/DhvIKFf6dsz81nT+k5hyHBnlPEdHc5EFPkezb3GseTqv3dkaKKkj9W2qfseiZbWVq4EFvvNGG2RRT0dqWeCPrVzcBuls0NX7E/d1fa3WXbZtbwJKs+rd0WlazQ0dnbrAn5bqdT8mbFP7vk6L6m/o5NhNVc8+eG2Urh11ir7Fe0+amRpoRUOj6GSvBX4p5c+vaO0RINHeHk/NWmzoJNn4jSE2tN5Cwzs9tWm6oaMNd0FA8doo/PPVe/AFlFHz4wOVuMEoOvf+7ggoc7fa9bnKzRo6WrxTAsqrG+79nOyWDR3NoymgrPHaKKyavwf/24tPO/OpH7edIzjHjUfRuc/vZgMXkWJvXTwHOEgnDR392EcB7VZHTwP21VVDJ3sed+IuvDYK2wkoj8fj19UbcEOvH205/0kpf74+/P916nMUnSulbDj0xC2ZRk81/3Cdte/r13lDTaA8kdFTrT31hqGZkvbc0J7vO29Y1F+jlWgCPzKNHuL9i5uLDW1iIDWOwSsZPcRTap4OKDW6tNdQWGRRf6qaK/mehsIaGa3LOJACDbGoP9u8kovjXYUlNYrCG54ed/BjmrNfbid5PDwOrDON3sH82b02yW4rgHbAj2T0btaiN8vrQmi1EjaT0V7MOrlQzLIyxMrrxAPBGhnl8ZBLCDjhCSAiowARGQWIyChAREYBIjIKH2j3ojIcT0YBIjIKEJFRgIiMAkRkFCAiowARGQWIyCh8wEmjrJNROvN6ZdWnn5Ty7ws+4HqjMPP0biVvXuIDplH46zWaFX5MK/WRUYCIRT39MWCyKxmlP08rd1UlY1EPEJFR+Ov1gJIj9XzAoh5mnkqqoXyg+IBygIRFPUBERgEiMgoQkVGAiIwCRGQUICKjABEZBYjIKEBERgEiMgoQkVGAiIwCRGQUICKjABEZBYjIKEBERgEiMgoQkVGAiIwCRGQUICKjABEZBYj8B2311hxb64HnAAAAAElFTkSuQmCC\n",
"text/plain": [
"<rdkit.Chem.rdchem.Mol at 0x1f165012a80>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mol"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def find_bond_groups(mol):\n",
" \"\"\"Find groups of contiguous rotatable bonds and return them sorted by decreasing size\"\"\"\n",
" rot_atom_pairs = mol.GetSubstructMatches(RotatableBondSmarts)\n",
" rot_bond_set = set([mol.GetBondBetweenAtoms(*ap).GetIdx() for ap in rot_atom_pairs])\n",
" rot_bond_groups = []\n",
" while (rot_bond_set):\n",
" i = rot_bond_set.pop()\n",
" connected_bond_set = set([i])\n",
" stack = [i]\n",
" while (stack):\n",
" i = stack.pop()\n",
" b = mol.GetBondWithIdx(i)\n",
" bonds = []\n",
" for a in (b.GetBeginAtom(), b.GetEndAtom()):\n",
" bonds.extend([b.GetIdx() for b in a.GetBonds() if (\n",
" (b.GetIdx() in rot_bond_set) and (not (b.GetIdx() in connected_bond_set)))])\n",
" connected_bond_set.update(bonds)\n",
" stack.extend(bonds)\n",
" rot_bond_set.difference_update(connected_bond_set)\n",
" rot_bond_groups.append(tuple(connected_bond_set))\n",
" return tuple(sorted(rot_bond_groups, reverse = True, key = lambda x: len(x)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Find groups of contiguous rotatable bonds in mol"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"bond_groups = find_bond_groups(mol)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As bond groups are sorted by decreasing size, the size of the first group (if any) is the largest number of contiguous rotatable bonds in mol"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"largest_n_cont_rot_bonds = len(bond_groups[0]) if bond_groups else 0"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"largest_n_cont_rot_bonds"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"((1,), (5,))"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bond_groups"
]
}
],
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@ghutchis
Copy link

This is a great idea - probably will use this or a similar concept in Open Babel for a project. 👍

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