{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "pypka.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "uBkU-8Oy7g6n",
        "colab_type": "code",
        "outputId": "1089993a-0c4e-4a9d-ee1d-5f7fe774ab22",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 289
        }
      },
      "source": [
        "!apt-get -qq install -y libgfortran4 gawk\n",
        "!pip install -q pypka biopython"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Selecting previously unselected package libsigsegv2:amd64.\n",
            "(Reading database ... 144433 files and directories currently installed.)\n",
            "Preparing to unpack .../libsigsegv2_2.12-1_amd64.deb ...\n",
            "Unpacking libsigsegv2:amd64 (2.12-1) ...\n",
            "Setting up libsigsegv2:amd64 (2.12-1) ...\n",
            "Selecting previously unselected package gawk.\n",
            "(Reading database ... 144440 files and directories currently installed.)\n",
            "Preparing to unpack .../gawk_1%3a4.1.4+dfsg-1build1_amd64.deb ...\n",
            "Unpacking gawk (1:4.1.4+dfsg-1build1) ...\n",
            "Setting up gawk (1:4.1.4+dfsg-1build1) ...\n",
            "Processing triggers for man-db (2.8.3-2ubuntu0.1) ...\n",
            "Processing triggers for libc-bin (2.27-3ubuntu1) ...\n",
            "/sbin/ldconfig.real: /usr/local/lib/python3.6/dist-packages/ideep4py/lib/libmkldnn.so.0 is not a symbolic link\n",
            "\n",
            "\u001b[K     |████████████████████████████████| 4.9MB 2.8MB/s \n",
            "\u001b[K     |████████████████████████████████| 2.3MB 36.2MB/s \n",
            "\u001b[?25h"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AhnkwzN79Lbu",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from pypka.pypka import Titration\n",
        "import Bio\n",
        "from Bio.PDB import PDBList\n",
        "import os"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "z3y_8vQi9RqB",
        "colab_type": "code",
        "outputId": "d5408454-ab44-429a-8d32-8f2e583f94f1",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "pdbl = PDBList()\n",
        "pdbl.retrieve_pdb_file('4lzt', file_format=\"pdb\", pdir='.')\n",
        "os.rename('pdb4lzt.ent', '4lzt.pdb')"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Downloading PDB structure '4lzt'...\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6bUM38D9LApJ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "params = {\n",
        "    'structure'     : '4lzt.pdb',    \n",
        "    'pH'            : \"0,14\",\n",
        "    'ncpus'         : 1,\n",
        "    'epsin'         : 15,\n",
        "    'ionicstr'      : 0.1,\n",
        "    'pbc_dimensions': 0,\n",
        "    'convergence'   : 0.1\n",
        "}\n",
        "sites = {\n",
        "    'A': ['1N', '1', '7', '129C']\n",
        "}"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QxyAdLEKLlEU",
        "colab_type": "code",
        "outputId": "c34a2158-86e7-4e48-e359-c65325c1212c",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 238
        }
      },
      "source": [
        "tit = Titration(params, sites=sites)  "
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Start Preprocessing\n",
            "Start PB Calculations\n",
            "PB Runs Ended                                                                                \n",
            "\n",
            "MC Runs Ended                                                                                \n",
            "\n",
            "Results\n",
            "Chain  Site   Name      pK\n",
            "   A   2001    NTR    7.0\n",
            "   A      1    LYS    10.45\n",
            "   A      7    GLU    3.59\n",
            "   A   2129    CTR    2.6\n",
            "API exited successfully\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vsFxypE9FdLZ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        },
        "outputId": "ae7f5d69-b8ad-414f-eaca-ffbd043897dd"
      },
      "source": [
        "pH = 7.0\n",
        "for site in tit:\n",
        "    pK = round(site.pK, 1)\n",
        "    state = site.getProtState(pH)[0]    \n",
        "    print(f'{site.res_name} {site.res_number:10} {pK:5.1f} {state}')"
      ],
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "NTR       2001   7.0 undefined\n",
            "LYS          1  10.5 protonated\n",
            "GLU          7   3.6 deprotonated\n",
            "CTR       2129   2.6 deprotonated\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nF24CkxjGXaA",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 295
        },
        "outputId": "cdcf0387-04e9-4dee-bba4-47a211b1ec59"
      },
      "source": [
        "import matplotlib.pyplot as plt\n",
        "tit_curve = tit.getTitrationCurve()\n",
        "x = sorted(list(tit_curve.keys()))\n",
        "y = [tit_curve[pH] for pH in x]\n",
        "plt.title('Total Titration Curve')\n",
        "plt.xlabel('pH')\n",
        "plt.xlabel('Protonation')\n",
        "plt.plot(x, y)\n",
        "plt.show()"
      ],
      "execution_count": 33,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 14384 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    }
  ]
}