{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fb13c981",
   "metadata": {},
   "source": [
    "### Isotopomer analysis\n",
    "Import necessary packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "82f0ff14",
   "metadata": {},
   "outputs": [],
   "source": [
    "from metabolabpytools import isotopomerAnalysis\n",
    "import os"
   ]
  },
  {
   "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": [
    "Load HSQC multiplet, GC-MS and per carbon 13C percentages data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "346441bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "hsqc_data_file = os.path.join(os.getcwd(), 'hsqcData.xlsx')\n",
    "gcms_data_file = os.path.join(os.getcwd(), 'gcmsData.xlsx')\n",
    "nmr1d_data_file = os.path.join(os.getcwd(), 'nmr1dData.xlsx')\n",
    "ia.read_hsqc_multiplets(hsqc_data_file)\n",
    "ia.read_gcms_data(gcms_data_file)\n",
    "ia.read_nmr1d_data(nmr1d_data_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42de67cc",
   "metadata": {},
   "source": [
    "Print the list of metabolites and number of experiments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cbe0f3e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# experiments: 3, metabolites: ['L-Alanine', 'L-LacticAcid']\n"
     ]
    }
   ],
   "source": [
    "print(f'# experiments: {ia.n_exps}, metabolites: {ia.metabolites}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cda9e969",
   "metadata": {},
   "source": [
    "Define isotopomers to be fitted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ad47e15a",
   "metadata": {},
   "outputs": [],
   "source": [
    "isotopomers = {}\n",
    "isotopomers['L-LacticAcid'] = [[0, 0, 1], [0, 1, 1]]\n",
    "isotopomers['L-Alanine'] = [[0, 0, 1], [0, 1, 1]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "206f837c",
   "metadata": {},
   "source": [
    "Define which data to use during isotopomer analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "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": [
    "loop through all metabolites & all experiments and fit isotopomer data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "23329eae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting all experiments for L-Alanine...\n",
      "Fitting all experiments for L-LacticAcid...\n"
     ]
    }
   ],
   "source": [
    "for k in range(len(ia.metabolites)):\n",
    "        if ia.metabolites[k] in isotopomers.keys():\n",
    "                ia.fit_all_exps(metabolite=ia.metabolites[k], fit_isotopomers=isotopomers[ia.metabolites[k]])\n",
    "        else:\n",
    "                print(f'{ia.metabolites[k]} not in isotopomers list')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49d9ea45",
   "metadata": {},
   "source": [
    "Export data and analysis to Excel spreadsheet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "06b94802",
   "metadata": {},
   "outputs": [],
   "source": [
    "ia.export_data('isotopomerAnalysis.xlsx')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13f97e7f-6ab2-4e14-837a-da5d617d2cc6",
   "metadata": {},
   "source": [
    "Fitted isotopomer distributions are stored in ia.fitted_isotopomers[metabolite][exp_index] and ia.fitted_isotopomer_percentages[metabolite][exp_index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8820a6a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Isotopomers : [[0, 0, 0], [0, 0, 1], [0, 1, 1]]\n",
      "Isotopomer %: [73.8191933266005, 3.1740858167890833, 23.00672085661041]\n"
     ]
    }
   ],
   "source": [
    "metabolite = 'L-LacticAcid'\n",
    "exp_index = 0\n",
    "print(f'Isotopomers : {ia.fitted_isotopomers[metabolite][exp_index]}\\nIsotopomer %: {ia.fitted_isotopomer_percentages[metabolite][exp_index]}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce809368-a0ab-4d6f-8773-392101a572fb",
   "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
}
