{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "63b89129-eb15-489c-acae-c6255df4d9f0", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/ubuntu/.local/lib/python3.8/site-packages/pandas/core/computation/expressions.py:20: UserWarning: Pandas requires version '2.7.3' or newer of 'numexpr' (version '2.7.1' currently installed).\n", " from pandas.core.computation.check import NUMEXPR_INSTALLED\n", "/home/ubuntu/.local/lib/python3.8/site-packages/Bio/pairwise2.py:278: BiopythonDeprecationWarning: Bio.pairwise2 has been deprecated, and we intend to remove it in a future release of Biopython. As an alternative, please consider using Bio.Align.PairwiseAligner as a replacement, and contact the Biopython developers if you still need the Bio.pairwise2 module.\n", " warnings.warn(\n" ] } ], "source": [ "# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n", "# SPDX-License-Identifier: MIT-0\n", "\n", "\"\"\"\n", "Parse PDB files to extract the coordinates of the 4 key atoms from AAs to\n", "generate json records compatible to the LM-GVP model.\n", "\n", "This script is intended for Fluorescence and Protease datasets from TAPE.\n", "\"\"\"\n", "\n", "import json\n", "import os\n", "import argparse\n", "from collections import defaultdict\n", "import pandas as pd\n", "import numpy as np\n", "\n", "from tqdm import tqdm\n", "from joblib import Parallel, delayed\n", "from Bio.PDB import PDBParser\n", "from Bio.PDB.Polypeptide import three_to_one\n", "\n", "import xpdb\n", "from contact_map_utils import parse_pdb_structure\n", "\n", "\n", "def parse_args():\n", " \"\"\"Prepare argument parser.\n", "\n", " Args:\n", "\n", " Return:\n", "\n", " \"\"\"\n", " parser = argparse.ArgumentParser(\n", " description=\"Generate GVP ProteinGraph datasets representing protein\"\n", " + \"structures.\"\n", " )\n", " parser.add_argument(\n", " \"--data-file\",\n", " help=\"Path to protein data frame, including sequences, paths to\"\n", " + \" structure files and labels\",\n", " required=True,\n", " )\n", " parser.add_argument(\n", " \"-t\",\n", " \"--target-variable\",\n", " help=\"target variable in the protein data frame\",\n", " required=True,\n", " )\n", " parser.add_argument(\"-o\", \"--output\", help=\"output dir for graphs\")\n", "\n", " args = parser.parse_args()\n", " return args\n", "\n", "\n", "def get_atom_coords(residue, target_atoms=[\"N\", \"CA\", \"C\", \"O\"]):\n", " \"\"\"Extract the coordinates of the target_atoms from an AA residue.\n", "\n", " Args:\n", " residue: a Bio.PDB.Residue object representing the residue.\n", " target_atoms: Target atoms which residues will be returned.\n", "\n", " Retruns:\n", " Array of residue's target atoms (in the same order as target atoms).\n", " \"\"\"\n", " return np.asarray([residue[atom].coord for atom in target_atoms])\n", "\n", "\n", "def structure_to_coords(struct, target_atoms=[\"N\", \"CA\", \"C\", \"O\"], name=\"\"):\n", " \"\"\"Convert a PDB structure in to coordinates of target atoms from all AAs\n", "\n", " Args:\n", " struct: a Bio.PDB.Structure object representing the protein structure\n", " target_atoms: Target atoms which residues will be returned.\n", " name: String. Name of the structure\n", "\n", " Return:\n", " Dictionary with the pdb sequence, atom 3D coordinates and name.\n", " \"\"\"\n", " output = {}\n", " # get AA sequence in the pdb structure\n", " pdb_seq = \"\".join(\n", " [three_to_one(res.get_resname()) for res in struct.get_residues()]\n", " )\n", " output[\"seq\"] = pdb_seq\n", " # get the atom coords\n", " coords = np.asarray(\n", " [\n", " get_atom_coords(res, target_atoms=target_atoms)\n", " for res in struct.get_residues()\n", " ]\n", " )\n", " output[\"coords\"] = coords.tolist()\n", " output[\"name\"] = name\n", " return output\n", "\n", "\n", "def parse_pdb_gz_to_json_record(parser, sequence, pdb_file_path, name=\"\"):\n", " \"\"\"\n", " Reads and reformats a pdb strcuture into a dictionary.\n", "\n", " Args:\n", " parser: a Bio.PDB.PDBParser or Bio.PDB.MMCIFParser instance.\n", " sequence: String. Sequence of the structure.\n", " pdb_file_path: String. Path to the pdb file.\n", " name: String. Name of the protein.\n", "\n", " Return:\n", " Dictionary with the pdb sequence, atom 3D coordinates and name.\n", " \"\"\"\n", " struct = parse_pdb_structure(parser, sequence, pdb_file_path)\n", " record = structure_to_coords(struct, name=name)\n", " return record" ] }, { "cell_type": "code", "execution_count": null, "id": "402ad072-edd1-4c04-a4c0-93390862770b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 11, "id": "72469cb0-878a-478b-849d-ee88774f3708", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\n", " 0%| | 0/23197 [00:00