{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to get the same atom index scheme between a Mol and its Smiles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T13:49:22.674594Z",
     "start_time": "2018-08-27T13:49:22.568000Z"
    }
   },
   "outputs": [],
   "source": [
    "from rdkit import rdBase\n",
    "from rdkit import Chem\n",
    "from rdkit.Chem import rdinchi\n",
    "from rdkit.Chem import Draw\n",
    "from rdkit.Chem.Draw import IPythonConsole"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T13:49:22.679790Z",
     "start_time": "2018-08-27T13:49:22.676328Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RDKit version: 2018.03.2\n"
     ]
    }
   ],
   "source": [
    "print(\"RDKit version:\", rdBase.rdkitVersion)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T13:49:22.689436Z",
     "start_time": "2018-08-27T13:49:22.681560Z"
    }
   },
   "outputs": [],
   "source": [
    "molblock = \"\"\"\n",
    "  Mrv1813 08271814163D          \n",
    "\n",
    "  6  5  0  0  0  0            999 V2000\n",
    "    0.1984    0.3502   -0.3666 C   0  0  2  0  0  0  0  0  0  0  0  0\n",
    "   -1.0703    0.9919    0.0063 N   0  0  0  0  0  0  0  0  0  0  0  0\n",
    "    0.3592   -0.9715    0.4164 C   0  0  0  0  0  0  0  0  0  0  0  0\n",
    "    1.3220    1.2802   -0.1255 C   0  0  0  0  0  0  0  0  0  0  0  0\n",
    "    2.1154    1.5801   -1.0467 O   0  0  0  0  0  0  0  0  0  0  0  0\n",
    "    1.5164    1.8261    1.0992 O   0  0  0  0  0  0  0  0  0  0  0  0\n",
    "  1  3  1  0  0  0  0\n",
    "  1  4  1  0  0  0  0\n",
    "  4  5  2  0  0  0  0\n",
    "  4  6  1  0  0  0  0\n",
    "  1  2  1  0  0  0  0\n",
    "M  END\n",
    "$$$$\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T13:49:22.703205Z",
     "start_time": "2018-08-27T13:49:22.691372Z"
    }
   },
   "outputs": [],
   "source": [
    "m1 = Chem.MolFromMolBlock(molblock) # same format as SDF\n",
    "smiles = Chem.MolToSmiles(m1, isomericSmiles=True)\n",
    "m2 = Chem.MolFromSmiles(smiles) # m1 comes from a SDF, m2 comes from a Smiles (same molecule)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T13:49:22.713767Z",
     "start_time": "2018-08-27T13:49:22.704863Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Atom index is different in m1 (SDF) and m2 (Smiles):\n",
      "m1: C0\tm2: C0\n",
      "m1: N1\tm2: C1\n",
      "m1: C2\tm2: N2\n",
      "m1: C3\tm2: C3\n",
      "m1: O4\tm2: O4\n",
      "m1: O5\tm2: O5\n"
     ]
    }
   ],
   "source": [
    "print(\"Atom index is different in m1 (SDF) and m2 (Smiles):\")\n",
    "for a1, a2 in zip(m1.GetAtoms(), m2.GetAtoms()):\n",
    "    print(\"m1: {}{}\\tm2: {}{}\".format(a1.GetSymbol(), a1.GetIdx(), a2.GetSymbol(), a2.GetIdx()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We want the same atom indexation between m1 and m2."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using atom properties\n",
    "\n",
    "A suggest by Brian Kelley (1)   \n",
    "(1) https://sourceforge.net/p/rdkit/mailman/message/35898962/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T13:49:22.719833Z",
     "start_time": "2018-08-27T13:49:22.715139Z"
    }
   },
   "outputs": [],
   "source": [
    "# Get atom properties\n",
    "d = m1.GetPropsAsDict(True,True)\n",
    "# Apply Smiles atom ranking to SDF molecule\n",
    "m1_canon = Chem.RenumberAtoms(m1, list(d[\"_smilesAtomOutputOrder\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T13:52:39.426979Z",
     "start_time": "2018-08-27T13:52:39.420752Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Atom index between m1_canon and m2\n",
      "m1_canon: C0\tm2: C0\n",
      "m1_canon: C1\tm2: C1\n",
      "m1_canon: N2\tm2: N2\n",
      "m1_canon: C3\tm2: C3\n",
      "m1_canon: O4\tm2: O4\n",
      "m1_canon: O5\tm2: O5\n"
     ]
    }
   ],
   "source": [
    "print(\"Atom index between m1_canon and m2\")\n",
    "for a1, a2 in zip(m1_canon.GetAtoms(), m2.GetAtoms()):\n",
    "    print(\"m1_canon: {}{}\\tm2: {}{}\".format(a1.GetSymbol(), a1.GetIdx(), a2.GetSymbol(), a2.GetIdx()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This works!"
   ]
  }
 ],
 "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.5"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
