{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fb13c981",
   "metadata": {},
   "source": [
    "### Simulation of pre-defined isotopomer distributions\n",
    "Import necessary packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "82f0ff14",
   "metadata": {},
   "outputs": [],
   "source": [
    "from metabolabpytools import isotopomerAnalysis"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24f7ffdc",
   "metadata": {},
   "source": [
    "Create an isotopomerAnalysis object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9228f2e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "ia = isotopomerAnalysis.IsotopomerAnalysis()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6feda906",
   "metadata": {},
   "source": [
    "Define metabolite parameters (name, HSQC vector, isotopomers & percentages to be simulated, simulate only one dataset <- exp_index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "346441bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "isotopomers = [[0,1,1],[1,0,1],[0,0,1]]\n",
    "percentages = [18.0, 10.0,2.0]\n",
    "hsqc = [0,1,1]\n",
    "metabolite = 'L-LacticAcid'\n",
    "exp_index = 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42de67cc",
   "metadata": {},
   "source": [
    "Initialise isotopomerAnalysis object with selected data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cbe0f3e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "ia.init_metabolite(metabolite, hsqc)\n",
    "ia.set_fit_isotopomers(metabolite=metabolite, isotopomers=isotopomers, percentages=percentages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff4bbd33-e068-4bd3-87d2-ba4a61e855b5",
   "metadata": {},
   "source": [
    "Isotopomer data are filled up to 100% with unlabelled metabolite"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f919f7b3-084a-4291-bcd2-ffd341d1f99a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Isotopomers : [[0, 0, 0], [0, 1, 1], [1, 0, 1], [0, 0, 1]]\n",
      "Isotopomer %: [70.0, 18.0, 10.0, 2.0]\n"
     ]
    }
   ],
   "source": [
    "print(f'Isotopomers : {ia.fit_isotopomers[metabolite]}\\nIsotopomer %: {ia.isotopomer_percentages[metabolite]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cda9e969",
   "metadata": {},
   "source": [
    "Simulate HSQC data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ad47e15a",
   "metadata": {},
   "outputs": [],
   "source": [
    "ia.sim_hsqc_data(metabolite=metabolite, exp_index=exp_index, isotopomers=isotopomers, percentages=percentages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c690719f-fc1d-4f2b-809f-c4d042f2319e",
   "metadata": {},
   "source": [
    "Simulated NMR multiplet data is stored in ia.exp_multiplets and ia.exp_multiplet_percentages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "945afab5-0135-41a8-b155-dbbe0170c192",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HSQC Multiplets: [[2], [2, 3], [2, 1, 3], [3], [3, 2]]\n",
      "Multiplet percentages: [3.9677074173350153, 94.44520961573097, 1.587082966934006, 41.043936388175226, 58.956063611824774]\n"
     ]
    }
   ],
   "source": [
    "print(f'HSQC Multiplets: {ia.exp_multiplets[metabolite][exp_index]}')\n",
    "print(f'Multiplet percentages: {ia.exp_multiplet_percentages[metabolite][exp_index]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9001ba6b-5f04-4800-8895-b437352f30ce",
   "metadata": {},
   "source": [
    "Simulate GC-MS data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3b8c8c33-a020-4fe9-8430-f4a514019baf",
   "metadata": {},
   "outputs": [],
   "source": [
    "ia.sim_gcms_data(metabolite, exp_index)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c93bf99-0509-443d-b630-cc37071a7904",
   "metadata": {},
   "source": [
    "Simulated data is stored in ia.exp_gcms[metabolite][exp_index] (m+0, m+1, m+2...)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d4f0a0e5-ae1b-4dce-b8b4-310ea5650259",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[70.0, 2.0, 28.0, 0.0]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ia.exp_gcms[metabolite][exp_index]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "206f837c",
   "metadata": {},
   "source": [
    "### Derivation of the isotopomer distribution using simulated HSQC and GC-MS data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cc529c6-4906-4f70-bfe6-b934bb5b5a2f",
   "metadata": {},
   "source": [
    "Use only HSQC multiplets and GC-MS data for analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "752fc103",
   "metadata": {},
   "outputs": [],
   "source": [
    "ia.use_hsqc_multiplet_data = True\n",
    "ia.use_gcms_data = True\n",
    "ia.use_nmr1d_data = False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68b352a4",
   "metadata": {},
   "source": [
    "Use the isotopomers previously used to simulate the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "23329eae",
   "metadata": {},
   "outputs": [],
   "source": [
    "ia.fit_data(exp_index=exp_index, metabolite=metabolite, fit_isotopomers=isotopomers)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fee95a38-0f0b-41f3-b035-d8ff31a48bdc",
   "metadata": {},
   "source": [
    "Fitted isotopomer distributions are stored in ia.fitted_isotopomers[metabolite][exp_index] amd ia.fitted_isotopomer_percentages[metabolite][exp_index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e7dc1f93-3423-4e80-bc62-75ce89351cf3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitted Isotoponers : [[0, 0, 0], [0, 1, 1], [1, 0, 1], [0, 0, 1]]\n",
      "Fitted Isotopomer %: [69.99999999999892, 18.00000000000122, 9.999999999996017, 2.000000000003847]\n"
     ]
    }
   ],
   "source": [
    "print(f'Fitted Isotoponers : {ia.fitted_isotopomers[metabolite][exp_index]}\\nFitted Isotopomer %: {ia.fitted_isotopomer_percentages[metabolite][exp_index]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49d9ea45",
   "metadata": {},
   "source": [
    "Export data and analysis to Excel spreadsheet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "06b94802",
   "metadata": {},
   "outputs": [],
   "source": [
    "ia.export_data('fittedIsotopomerAnalysis.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a45c5053-7737-49aa-8dd9-0cbe79e34a95",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
