Datasets:
File size: 5,518 Bytes
24fff69 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
"""
Helpers for parsing protein structure files and generating contact maps.
"""
import gzip
import numpy as np
import pandas as pd
from io import StringIO
from sklearn.metrics import pairwise_distances
from Bio import pairwise2
from Bio.Seq import Seq
from Bio.PDB.Polypeptide import three_to_one, is_aa
def idx_align(pdb_seq: str, og_seq: str):
"""Maps indices of original and PDB sequences to mutual alignment
Args:
pdb_seq: Sequence of amino acids as extracted from PDB file
og_seq: Original sequence of amino acids
Returns:
Tuple where the first element is a list of integers containing the indices in
the PDB sequence for which aligned information is available and the second element
is a list of integers containing the indices in the original sequence
for which aligned information is available
"""
alignment = pairwise2.align.globalxx(Seq(og_seq), Seq(pdb_seq))[0]
og_seq_align = alignment.seqA
og_idxs = []
og_offset = 0
pdb_seq_align = alignment.seqB
pdb_idxs = []
pdb_offset = 0
for i in range(len(pdb_seq_align)):
valid = True
pdb_res = pdb_seq_align[i]
og_res = og_seq_align[i]
if pdb_res == "-":
pdb_offset += 1
valid = False
if og_res == "-":
og_offset += 1
valid = False
if valid and (pdb_res == og_res):
pdb_idxs.append(i - pdb_offset)
og_idxs.append(i - og_offset)
return pdb_idxs, og_idxs
def calc_adj_matrix(structure, dist_thresh=10.0):
"""Returns an adjacency matrix for a PDB structure
Args:
structure : BioPython PDB Structure object representing a protein containing N residues
dist_thresh : float, optional. Value specifying threshold for classifying contacts between residues
Returns:
Tuple where the first element is a np.ndarray adjacency matrix with shape NxN, where N is the number of residues in `structure` and the second element is np.ndarray NxN matrix specifying distances between residues.
"""
residues = list(structure.get_residues())
coords = np.asarray([residue["CA"].coord for residue in residues])
dist_mat = pairwise_distances(coords, metric="euclidean")
return 1 * (dist_mat < dist_thresh), dist_mat
def gunzip_to_ram(gzip_file_path):
"""
gunzip a gzip file and decode it to a io.StringIO object.
Args:
gzip_file_path: String. Gunzip filepath.
Returns:
io.StringIO object.
"""
content = []
with gzip.open(gzip_file_path, "rb") as f:
for line in f:
content.append(line.decode("utf-8"))
temp_fp = StringIO("".join(content))
return temp_fp
def _parse_structure(parser, name, file_path):
"""Parse a .pdb or .cif file into a structure object.
The file can be gzipped.
Args:
parser: a Bio.PDB.PDBParser or Bio.PDB.MMCIFParser instance.
name: String. name of protein
file_path: String. Filpath of the pdb or cif file to be read.
Retruns:
a Bio.PDB.Structure object representing the protein structure.
"""
if pd.isnull(file_path):
return None
if file_path.endswith(".gz"):
structure = parser.get_structure(name, gunzip_to_ram(file_path))
else: # not gzipped
structure = parser.get_structure(name, file_path)
return structure
parse_pdb_structure = _parse_structure # for backward compatiblity
def parse_structure(pdb_parser, cif_parser, name, file_path):
"""Parse a .pdb file or .cif file into a structure object.
The file can be gzipped.
Args:
pdb_parser: a Bio.PDB.PDBParser instance
cif_parser: Bio.PDB.MMCIFParser instance
name: String. name of protein
file_path: String. Filpath of the pdb or cif file to be read.
Return:
a Bio.PDB.Structure object representing the protein structure.
"""
if file_path.rstrip(".gz").endswith("pdb"):
return _parse_structure(pdb_parser, name, file_path)
else:
return _parse_structure(cif_parser, name, file_path)
def get_energy(pdb_file_path):
"""Get total pose energy from a PDB file.
Args:
pdb_file_path: String. Path to the pdb file.
Return:
Energy score.
"""
if pdb_file_path.endswith(".pdb.gz"):
pdb_file = gunzip_to_ram(pdb_file_path)
else:
pdb_file = open(pdb_file_path, "r")
parse = False
score = None
for line in pdb_file:
if line.startswith("#BEGIN_POSE_ENERGIES_TABLE"):
parse = True
if parse and line.startswith("pose"):
score = float(line.strip().split()[-1])
return score
def three_to_one_standard(res):
"""Encode non-standard AA to X.
Args:
res: a Bio.PDB.Residue object representing the residue.
Return:
String. One letter code of the residue.
"""
if not is_aa(res, standard=True):
return "X"
return three_to_one(res)
def is_aa_by_target_atoms(res):
"""Tell if a Residue object is AA
Args:
res: a Bio.PDB.Residue object representing the residue.
Return:
Bool. Wheather or not the residue is AA.
"""
target_atoms = ["N", "CA", "C", "O"]
for atom in target_atoms:
try:
res[atom]
except KeyError:
return False
return True
|