{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2022.09.1pre\n"
     ]
    }
   ],
   "source": [
    "# NBVAL_IGNORE_OUTPUT\n",
    "from rdkit import Chem\n",
    "from rdkit.Chem import Draw\n",
    "from rdkit.Chem.Draw import IPythonConsole\n",
    "from rdkit.Chem import rdDepictor\n",
    "IPythonConsole.molSize = (350,300)\n",
    "IPythonConsole.drawOptions.centreMoleculesBeforeDrawing=True\n",
    "from rdkit import RDLogger\n",
    "# in case the RGD code is verbose (should only happen pre-release)\n",
    "RDLogger.DisableLog('rdApp.debug')\n",
    "import rdkit\n",
    "print(rdkit.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from rdkit.Chem import rdRGroupDecomposition\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ms = [x for x in Chem.SDMolSupplier('./simple1.sdf')]\n",
    "len(ms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30360 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# NBVAL_SKIP\n",
    "IPythonConsole.drawOptions.addAtomIndices = True\n",
    "Draw.MolsToGridImage(ms,molsPerRow=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  {'Core': 'Cc1cccc([*:1])c1[*:2]', 'R1': '[H][*:1]', 'R2': 'CO[*:2]'}\n",
      "  {'Core': 'Cc1cccc([*:1])c1[*:2]', 'R1': '[H][*:1]', 'R2': 'CO[*:2]'}\n",
      "  {'Core': 'Cc1cccc([*:1])c1[*:2]', 'R1': 'CO[*:1]', 'R2': '[H][*:2]'}\n"
     ]
    }
   ],
   "source": [
    "res,_ = rdRGroupDecomposition.RGroupDecompose([ms[0]],ms[1:],asSmiles=True)\n",
    "for row in res:\n",
    "    print(f'  {row}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# testing against reference data:\n",
    "with open('./simple1.out1.json','rt') as inf:\n",
    "    expected = json.load(inf)\n",
    "if expected != res:\n",
    "    raise ValueError(f\"MISMATCH:\\n{res}\\n   !=\\n{expected}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  {'Core': 'Cc1c([*:3])ccc([*:2])c1[*:1]', 'R1': '[H][*:1]', 'R2': '[H][*:2]', 'R3': 'CO[*:3]'}\n",
      "  {'Core': 'Cc1c([*:3])ccc([*:2])c1[*:1]', 'R1': 'CO[*:1]', 'R2': '[H][*:2]', 'R3': '[H][*:3]'}\n",
      "  {'Core': 'Cc1c([*:3])ccc([*:2])c1[*:1]', 'R1': '[H][*:1]', 'R2': 'CO[*:2]', 'R3': '[H][*:3]'}\n"
     ]
    }
   ],
   "source": [
    "ps = rdRGroupDecomposition.RGroupDecompositionParameters()\n",
    "ps.matchingStrategy = rdRGroupDecomposition.RGroupMatching.NoSymmetrization\n",
    "res,_ = rdRGroupDecomposition.RGroupDecompose([ms[0]],ms[1:],asSmiles=True,options=ps)\n",
    "for row in res:\n",
    "    print(f'  {row}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# testing against reference data:\n",
    "with open('./simple1.out2.json','rt') as inf:\n",
    "    expected = json.load(inf)\n",
    "if expected != res:\n",
    "    raise ValueError(f\"MISMATCH:\\n{res}\\n   !=\\n{expected}\")"
   ]
  },
  {
   "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.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
