{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "pypka.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3",
      "language": "python"
    }
  },
  "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",
          "name": "stdout",
          "text": [
            "E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)\n",
            "E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?\n",
            "WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.\n",
            "Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.\n",
            "To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "/usr/bin/python3: No module named instal\n"
          ]
        }
      ],
      "source": [
        "!python3 -m instal --user pypka biopython"
      ]
    },
    {
      "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": 8,
      "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": 9,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Downloading PDB structure '4lzt'...\n"
          ]
        }
      ]
    },
    {
      "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": 10,
      "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": 11,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Start Preprocessing\n"
          ]
        },
        {
          "output_type": "error",
          "ename": "Exception",
          "evalue": "The following errors have been found on LOG_addHtaut: \naddHtaut: Error: Wrong number of arguments.\n",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mException\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-11-483013c48e35>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtit\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mTitration\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msites\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msites\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;32m~/.local/lib/python3.8/site-packages/pypka/pypka.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, parameters, sites, debug, run)\u001b[0m\n\u001b[1;32m    103\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    104\u001b[0m         \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Start Preprocessing\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 105\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpreprocessing\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msites\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    106\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprocessDelPhiParams\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    107\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m~/.local/lib/python3.8/site-packages/pypka/pypka.py\u001b[0m in \u001b[0;36mpreprocessing\u001b[0;34m(self, sites)\u001b[0m\n\u001b[1;32m    194\u001b[0m                 \u001b[0;31m#        site_numb_n_ref[chain][site] = tau_ref_name\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    195\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 196\u001b[0;31m                 \u001b[0mcleanPDB\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmolecules\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mchains_res\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputpqr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutputpqr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    197\u001b[0m                 \u001b[0mcreate_tit_sites\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mchains_res\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTMPpdb\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    198\u001b[0m                 \u001b[0mf_in\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"TMP.pdb\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m~/.local/lib/python3.8/site-packages/pypka/cleaning.py\u001b[0m in \u001b[0;36mcleanPDB\u001b[0;34m(molecules, chains_res, inputpqr, outputpqr)\u001b[0m\n\u001b[1;32m    362\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    363\u001b[0m     \u001b[0;31m# log.redirectErr(\"stop\", logfile)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 364\u001b[0;31m     \u001b[0mlog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcheckDelPhiErrors\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlogfile\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"addHtaut\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    365\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    366\u001b[0m     \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutputpqr\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m~/.local/lib/python3.8/site-packages/pypka/log.py\u001b[0m in \u001b[0;36mcheckDelPhiErrors\u001b[0;34m(filename, mode)\u001b[0m\n\u001b[1;32m     84\u001b[0m                     \u001b[0merrors\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0mline\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     85\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mexit_trigger\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 86\u001b[0;31m         raise Exception(\n\u001b[0m\u001b[1;32m     87\u001b[0m             \"The following errors have been found on {0}: \\n{1}\".format(\n\u001b[1;32m     88\u001b[0m                 \u001b[0mfilename\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0merrors\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;31mException\u001b[0m: The following errors have been found on LOG_addHtaut: \naddHtaut: Error: Wrong number of arguments.\n"
          ]
        }
      ]
    },
    {
      "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"
          }
        }
      ]
    }
  ]
}