{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BAGO\n",
    "\n",
    "Preparations for the BAGO project:\n",
    "\n",
    "Make sure you have the following installed:\n",
    "\n",
    "* Python 3.8 or higher\n",
    "* scikit-learn package (https://scikit-learn.org/stable/install.html#installation-instructions)\n",
    "* numpy package (https://numpy.org/install/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Imports\n",
    "from bago import bagoMain\n",
    "import numpy as np"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first step is to set parameters for the BAGO algorithm in next cell.\n",
    "\n",
    "We also provide a template for the parameters in `bago` package. To view the template, run the following command:\n",
    "```python\n",
    "    from bago import bagoMain\n",
    "    print(parametersTemplate)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set the parameters\n",
    "parameters = {\n",
    "\n",
    "    # Time points of the gradient (in minutes)\n",
    "    'timePoints': np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=float),\n",
    "\n",
    "    # A value specifying if the gradient can be changed at the second time point. \n",
    "    # Set it to False if the gradient is fixed at the second time point to load the sample.\n",
    "    'isChangable': False,\n",
    "\n",
    "    # Range of strong mobile phase percetange (in percentage)\n",
    "    'gradRange': (5.0, 95.0),\n",
    "\n",
    "    # Step size of strong mobile phase percetange (in percentage)\n",
    "    'gradStep': 5,\n",
    "\n",
    "    # Range of total strong mobile phase being used (in percentage)\n",
    "    'mpBound': (35.0, 50.0),\n",
    "\n",
    "    # A dictionary of the gradient settings. Make sure the initial gradient setting is named starting with \"Init_\" (e.g. Init_1).\n",
    "    'grads': {\n",
    "        'Init_1': np.array([5, 5, 25, 35, 45, 55, 65, 75, 85, 95], dtype=float),\n",
    "    },\n",
    "\n",
    "    # Directory of the raw data. The new raw data should be loaded in this directory.\n",
    "    'rawDatadir': \"RawData\",\n",
    "\n",
    "    # Directory of the gradient method. The new gradient method will be saved in this directory.\n",
    "    'methodDir': \"GradientMethod\",\n",
    "\n",
    "    # Number of top signals used to calculate the separation efficiency.\n",
    "    'sNum': 500,\n",
    "\n",
    "    # Retention time tolerance (in minutes)\n",
    "    'rtTol': 1.0,\n",
    "\n",
    "    # Intensity tolerance (in arbitrary units)\n",
    "    'intTol': 1000,\n",
    "\n",
    "    # Precursor m/z tolerance (in m/z)\n",
    "    'precsMzTol': 0.01,\n",
    "\n",
    "    # Product m/z tolerance (in m/z)\n",
    "    'prodMzTol': 0.02,\n",
    "\n",
    "    # Acquisition function used to calculate the next gradient setting\n",
    "    'acqFunc': 'ei',\n",
    "\n",
    "    # DO NOT CHANGE THE FOLLOWING PARAMETERS\n",
    "    'gradPoints': None, # Valid strong mobile phase percetanges calculated from gradRange and gradStep\n",
    "    'gradIdx': 0, # Index of the current gradient setting generated by BAGO\n",
    "    'sepEffs': None, # Separation efficiencies of the gradient settings calculated by BAGO\n",
    "    'rtRange': None, # Retention time range (in minutes) of the gradient (e.g. (0, 10)) calculated by BAGO\n",
    "}\n",
    "\n",
    "# Create a dictionary to store the LC-MS data\n",
    "exp = {}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initilization\n",
    "mainModel = bagoMain.modelInitilization(parameters=parameters, exp=exp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Calculate the next gradient by taking new MS data\n",
    "# Run this block whenever you have a new MS data\n",
    "bagoMain.getNextGradient(exp=exp, parameters=parameters, mainModel=mainModel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Finish optimization and generate results\n",
    "bagoMain.runEvaluation(exp=exp, parameters=parameters)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Save parameters\n",
    "# Run this block whenever you want to save the current parameters\n",
    "bagoMain.saveParameters(parameters=parameters)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.9 64-bit",
   "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.8.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "570feb405e2e27c949193ac68f46852414290d515b0ba6e5d90d076ed2284471"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
