{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# This cell is added by sphinx-gallery\n\n%matplotlib inline\n\nimport mrsimulator\nprint(f'You are using mrsimulator v{mrsimulator.__version__}')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Protein GB1, 13C and 15N (I=1/2)\n\n13C/15N (I=1/2) spinning sideband simulation.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following is the spinning sideband simulation of a macromolecule, protein GB1. The\n$^{13}\\text{C}$ and $^{15}\\text{N}$ CSA tensor parameters were obtained\nfrom Hung `et. al.` [#f1]_, which consists of 42 $^{13}\\text{C}\\alpha$,\n44 $^{13}\\text{CO}$, and 44 $^{15}\\text{NH}$ tensors. In the following\nexample, instead of creating 130 spin systems, we download the spin systems from\na remote file and load it directly to the Simulator object.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib as mpl\nimport matplotlib.pyplot as plt\nimport mrsimulator.signal_processing as sp\nimport mrsimulator.signal_processing.apodization as apo\nfrom mrsimulator import Simulator\nfrom mrsimulator.methods import BlochDecaySpectrum\n\n# global plot configuration\nmpl.rcParams[\"figure.figsize\"] = [9, 4]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create the Simulator object and load the spin systems from an external file.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sim = Simulator()\n\nfile_ = \"https://sandbox.zenodo.org/record/687656/files/protein_GB1_15N_13CA_13CO.mrsys\"\nsim.load_spin_systems(file_)  # load the spin systems.\nprint(f\"number of spin systems = {len(sim.spin_systems)}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create a $^{13}\\text{C}$ Bloch decay spectrum method.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "method_13C = BlochDecaySpectrum(\n    channels=[\"13C\"],\n    magnetic_flux_density=11.7,  # in T\n    rotor_frequency=3000,  # in Hz\n    spectral_dimensions=[\n        {\n            \"count\": 8192,\n            \"spectral_width\": 5e4,  # in Hz\n            \"reference_offset\": 2e4,  # in Hz\n            \"label\": r\"$^{13}$C resonances\",\n        }\n    ],\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Since the spin systems contain both $^{13}\\text{C}$ and $^{15}\\text{N}$\nsites, let's also create a $^{15}\\text{N}$ Bloch decay spectrum method.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "method_15N = BlochDecaySpectrum(\n    channels=[\"15N\"],\n    magnetic_flux_density=11.7,  # in T\n    rotor_frequency=3000,  # in Hz\n    spectral_dimensions=[\n        {\n            \"count\": 8192,\n            \"spectral_width\": 4e4,  # in Hz\n            \"reference_offset\": 7e3,  # in Hz\n            \"label\": r\"$^{15}$N resonances\",\n        }\n    ],\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Add the methods to the Simulator object and run the simulation\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Add the methods.\nsim.methods = [method_13C, method_15N]\n\n# Run the simulation.\nsim.run()\n\n# Get the simulation data from the respective methods.\ndata_13C = sim.methods[0].simulation  # method at index 0 is 13C Bloch decay method.\ndata_15N = sim.methods[1].simulation  # method at index 1 is 15N Bloch decay method."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Add post-simulation signal processing.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "processor = sp.SignalProcessor(\n    operations=[sp.IFFT(), apo.Exponential(FWHM=\"10 Hz\"), sp.FFT()]\n)\n# apply post-simulation processing to data_13C\nprocessed_data_13C = processor.apply_operations(data=data_13C).real\n\n# apply post-simulation processing to data_15N\nprocessed_data_15N = processor.apply_operations(data=data_15N).real"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The plot of the simulation after signal processing.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fig, ax = plt.subplots(1, 2, subplot_kw={\"projection\": \"csdm\"}, sharey=True)\n\nax[0].plot(processed_data_13C, color=\"black\", linewidth=0.5)\nax[0].invert_xaxis()\n\nax[1].plot(processed_data_15N, color=\"black\", linewidth=0.5)\nax[1].set_ylabel(None)\nax[1].invert_xaxis()\n\nplt.tight_layout()\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. [#f1] Hung I., Ge Y., Liu X., Liu M., Li C., Gan Z., Measuring\n    $^{13}\\text{C}$/$^{15}\\text{N}$ chemical shift anisotropy in\n    [$^{13}\\text{C}$, $^{15}\\text{N}$] uniformly enriched proteins using\n    CSA amplification, Solid State Nuclear Magnetic Resonance. 2015, **72**, 96-103.\n    `DOI: 10.1016/j.ssnmr.2015.09.002 <https://doi.org/10.1016/j.ssnmr.2015.09.002>`_\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.8.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}