{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using the InteractiveMolecule widget\n",
    "\n",
    "For this example we need to install pandas and RDKit. If you don't have these packages yet, just execute the cell bellow to install. Note that you will need conda to install RDKit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!conda install -c conda-forge pandas rdkit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can import the `trident_chemwidgets` and the `pandas` lib to import our csv dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import trident_chemwidgets as tcw\n",
    "import pandas as pd\n",
    "from rdkit import Chem"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can create a small function to featurize our molecules with basic information per atom. \n",
    "\n",
    "**IMPORTANT:** *the order of the data rows in the pandas DataFrame or dict must match the standard ordering of atoms as returned by the RDKit `.GetAtoms()` function.* You can generate this data any way you see fit (e.g. calculated values from RDKit as in the function below or attention values from a Graph Attention Network. The only constraint is the atom ordering. If you are using RDKit-based featurizers like those from DeepChem, this standard ordering should already be the default. Take care when using cutom featurizers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def featurize_mol(smiles):\n",
    "    # Init feature dict\n",
    "    feature_dict = {\n",
    "        'Chiral Tag': [],\n",
    "        'Formal Charge': [],\n",
    "        'Mass': [],\n",
    "        'Total Hs': [],\n",
    "        'Total Valence': []\n",
    "    }\n",
    "    \n",
    "    # Get atoms from SMILES\n",
    "    atoms = Chem.MolFromSmiles(smiles).GetAtoms()\n",
    "    \n",
    "    # Use RDKit to get all the atom properties\n",
    "    for atom in atoms:\n",
    "        feature_dict['Chiral Tag'].append(atom.GetChiralTag())\n",
    "        feature_dict['Formal Charge'].append(atom.GetFormalCharge())\n",
    "        feature_dict['Mass'].append(atom.GetMass())\n",
    "        feature_dict['Total Hs'].append(atom.GetTotalNumHs())\n",
    "        feature_dict['Total Valence'].append(atom.GetTotalValence())\n",
    "        \n",
    "    return pd.DataFrame.from_dict(feature_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we'll be exploring the atom features from the ibuprofen molecule, smiles string `CC(C)CC1=CC=C(C=C1)C(C)C(=O)O`. We'll use the function we defined above to get some data at the atom level."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "atom_data = featurize_mol('CC(C)CC1=CC=C(C=C1)C(C)C(=O)O')\n",
    "atom_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can use the InteractiveMolecule widget to explore the data attached to each atom."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "w = tcw.InteractiveMolecule('CC(C)CC1=CC=C(C=C1)C(C)C(=O)O', data=atom_data)\n",
    "w"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The value of the widget will match what you typed into the input."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "w.smiles"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
