{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fdd92793",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "所有模块导入成功!\n"
     ]
    }
   ],
   "source": [
    "# 配置matplotlib在Jupyter中内联显示\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import sys\n",
    "sys.path.append('../')\n",
    "\n",
    "from rdkit import Chem\n",
    "from rdkit.Chem import Draw\n",
    "from rdkit.Chem.Draw import IPythonConsole\n",
    "from IPython.display import display\n",
    "import pandas as pd\n",
    "import json\n",
    "from pathlib import Path\n",
    "import numpy as np\n",
    "\n",
    "# 导入我们的模块\n",
    "from src.ring_numbering import (\n",
    "    assign_ring_numbering,\n",
    "    find_lactone_carbon,\n",
    "    get_ring_atoms,\n",
    "    validate_numbering\n",
    ")\n",
    "from src.fragment_dataclass import Fragment, MoleculeFragments\n",
    "from src.fragment_cleaver import (\n",
    "    identify_side_chains,\n",
    "    extract_side_chain_fragment,\n",
    "    cleave_side_chains,\n",
    "    process_molecule\n",
    ")\n",
    "from src.visualizer import draw_molecule_with_numbering\n",
    "\n",
    "print(\"所有模块导入成功!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "10e7b1b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"../data/ring12_20/temp.csv\")\n",
    "\n",
    "def showmol(ids: str):\n",
    "    \"\"\"\n",
    "    根据IDs筛选并显示分子，同时统计酯基数目\n",
    "    \n",
    "    Parameters:\n",
    "    -----------\n",
    "    ids : str\n",
    "        分子的IDs，用于在DataFrame中筛选\n",
    "        \n",
    "    Returns:\n",
    "    --------\n",
    "    mol : rdkit.Chem.Mol\n",
    "        RDKit分子对象\n",
    "    \"\"\"\n",
    "    # 根据IDs筛选DataFrame\n",
    "    row = df[df['IDs'] == ids]\n",
    "    \n",
    "    if row.empty:\n",
    "        print(f\"未找到IDs为 '{ids}' 的分子\")\n",
    "        return None\n",
    "    \n",
    "    # 获取分子信息\n",
    "    smiles = row.iloc[0]['smiles']\n",
    "    molecule_name = row.iloc[0]['molecule_pref_name']\n",
    "    molecule_id = row.iloc[0]['IDs']\n",
    "    \n",
    "    print(f\"分子名称: {molecule_name}\")\n",
    "    print(f\"分子IDs: {molecule_id}\")\n",
    "    print(f\"SMILES: {smiles}\")\n",
    "    \n",
    "    # 解析分子\n",
    "    mol = Chem.MolFromSmiles(smiles)\n",
    "    \n",
    "    if mol is None:\n",
    "        print(\"无法解析SMILES字符串\")\n",
    "        return None\n",
    "    \n",
    "    # 使用SMARTS匹配酯基\n",
    "    # 酯基模式: C(=O)O，匹配羧酸酯键\n",
    "    ester_smarts = \"[C](=O)[O]\"\n",
    "    ester_pattern = Chem.MolFromSmarts(ester_smarts)\n",
    "    \n",
    "    if ester_pattern is not None:\n",
    "        matches = mol.GetSubstructMatches(ester_pattern)\n",
    "        ester_count = len(matches)\n",
    "        print(f\"酯基数目: {ester_count}\")\n",
    "        \n",
    "        # 高亮显示匹配的酯基\n",
    "        if ester_count > 0:\n",
    "            highlight_atoms = set()\n",
    "            highlight_bonds = set()\n",
    "            for match in matches:\n",
    "                highlight_atoms.update(match)\n",
    "                # 获取匹配中的键\n",
    "                for i in range(len(match) - 1):\n",
    "                    bond = mol.GetBondBetweenAtoms(match[i], match[i+1])\n",
    "                    if bond:\n",
    "                        highlight_bonds.add(bond.GetIdx())\n",
    "            \n",
    "            # 显示高亮的分子\n",
    "            img = Draw.MolToImage(mol, highlightAtoms=list(highlight_atoms), \n",
    "                                 highlightBonds=list(highlight_bonds), \n",
    "                                 size=(800, 600))\n",
    "            display(img)\n",
    "        else:\n",
    "            # 如果没有匹配，正常显示\n",
    "            display(mol)\n",
    "    else:\n",
    "        print(\"无法创建SMARTS模式\")\n",
    "        display(mol)\n",
    "    \n",
    "    return mol"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e0516728",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分子名称: nan\n",
      "分子IDs: CHEMBL4061758\n",
      "SMILES: CC[C@@H]1OC(=O)C[C@@H](C/C=C/C=C(\\C)[C@H]2OC(=O)CC/C=C(\\C)[C@@H](C)[C@H](O)[C@H](C)[C@@H](OC(N)=O)/C=C\\C=C/[C@@H]2C)[C@H]1C\n",
      "酯基数目: 3\n"
     ]
    },
    {
     "data": {
      "image/jpeg": "[encoded data, 27916 characters]",
      "image/png": "[encoded data, 46056 characters]",
      "text/plain": [
       "<PIL.PngImagePlugin.PngImageFile image mode=RGB size=800x600>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 20056 characters]",
      "text/plain": [
       "<rdkit.Chem.rdchem.Mol at 0x7b81c762df50>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "showmol(\"CHEMBL4061758\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "31882513",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分子名称: nan\n",
      "分子IDs: CHEMBL103389\n",
      "SMILES: COc1ccc(C[C@H]2NC(=O)/C=C/C[C@@H]([C@H](C)[C@@H](O)[C@@H](Cl)c3ccc(C)c(C)c3)OC(=O)[C@H](CC(C)C)OC(=O)C(C)(C)CNC2=O)cc1Cl\n",
      "酯基数目: 2\n"
     ]
    },
    {
     "data": {
      "image/jpeg": "[encoded data, 31828 characters]",
      "image/png": "[encoded data, 45968 characters]",
      "text/plain": [
       "<PIL.PngImagePlugin.PngImageFile image mode=RGB size=800x600>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 18128 characters]",
      "text/plain": [
       "<rdkit.Chem.rdchem.Mol at 0x7b81c762ff40>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "showmol(\"CHEMBL103389\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0203155e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分子名称: nan\n",
      "分子IDs: CHEMBL99640\n",
      "SMILES: COc1ccc(C[C@H]2NC(=O)/C=C/C[C@@H]([C@H](C)[C@@H](O)[C@@H](Cl)c3ccc(CNC(=O)CN(C)C(=O)OC(C)(C)C)cc3)OC(=O)[C@H](CC(C)C)OC(=O)C(C)(C)CNC2=O)cc1Cl\n",
      "酯基数目: 3\n"
     ]
    },
    {
     "data": {
      "image/jpeg": "[encoded data, 28648 characters]",
      "image/png": "[encoded data, 42496 characters]",
      "text/plain": [
       "<PIL.PngImagePlugin.PngImageFile image mode=RGB size=800x600>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 21728 characters]",
      "text/plain": [
       "<rdkit.Chem.rdchem.Mol at 0x7b81c762fe60>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "showmol(\"CHEMBL99640\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6ef481af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "以 'ring16_mol_' 开头的文件夹数量: 795\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "# 统计以 ring16_mol_ 开头的文件夹数量\n",
    "dir_list = [d for d in Path(\"../output/ring16_fragments\").glob(\"ring16_mol_*\") if d.is_dir()]\n",
    "count = len(dir_list)\n",
    "print(f\"以 'ring16_mol_' 开头的文件夹数量: {count}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0c81d24",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "\n"
   ]
  }
 ],
 "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.14.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
