Datasets:
File size: 4,416 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 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
"""
Parse PDB files to extract the coordinates of the 4 key atoms from AAs to
generate json records compatible to the LM-GVP model.
This script is intended for Fluorescence and Protease datasets from TAPE.
"""
import json
import os
import argparse
from collections import defaultdict
import pandas as pd
import numpy as np
from tqdm import tqdm
from joblib import Parallel, delayed
from Bio.PDB import PDBParser
from Bio.PDB.Polypeptide import three_to_one
import xpdb
from contact_map_utils import parse_pdb_structure
def parse_args():
"""Prepare argument parser.
Args:
Return:
"""
parser = argparse.ArgumentParser(
description="Generate json records for GVP from pdb files"
)
parser.add_argument(
"--data_file",
help="Path to csv file containing pdbpaths",
required=True,
)
parser.add_argument(
"--target_col",
help="target column in data",
required=True,
)
parser.add_argument(
"--smiles_col",
help="smiles column in data",
required=True,
)
parser.add_argument("--out_prefix", help="output prefix for json records")
args = parser.parse_args()
return args
def get_atom_coords(residue, target_atoms=["N", "CA", "C", "O"]):
"""Extract the coordinates of the target_atoms from an AA residue.
Args:
residue: a Bio.PDB.Residue object representing the residue.
target_atoms: Target atoms which residues will be returned.
Retruns:
Array of residue's target atoms (in the same order as target atoms).
"""
return np.asarray([residue[atom].coord for atom in target_atoms])
def structure_to_coords(struct, target_atoms=["N", "CA", "C", "O"], name=""):
"""Convert a PDB structure in to coordinates of target atoms from all AAs
Args:
struct: a Bio.PDB.Structure object representing the protein structure
target_atoms: Target atoms which residues will be returned.
name: String. Name of the structure
Return:
Dictionary with the pdb sequence, atom 3D coordinates and name.
"""
output = {}
# get AA sequence in the pdb structure
pdb_seq = "".join(
[three_to_one(res.get_resname()) for res in struct.get_residues()]
)
output["seq"] = pdb_seq
# get the atom coords
coords = np.asarray(
[
get_atom_coords(res, target_atoms=target_atoms)
for res in struct.get_residues()
]
)
output["coords"] = coords.tolist()
output["name"] = name
return output
def parse_pdb_gz_to_json_record(parser, sequence, pdb_file_path, name=""):
"""
Reads and reformats a pdb strcuture into a dictionary.
Args:
parser: a Bio.PDB.PDBParser or Bio.PDB.MMCIFParser instance.
sequence: String. Sequence of the structure.
pdb_file_path: String. Path to the pdb file.
name: String. Name of the protein.
Return:
Dictionary with the pdb sequence, atom 3D coordinates and name.
"""
struct = parse_pdb_structure(parser, sequence, pdb_file_path)
record = structure_to_coords(struct, name=name)
return record
if __name__=='__main__':
args = parse_args()
df = pd.read_csv(args.data_file)
# PDB parser
sloppyparser = PDBParser(
QUIET=True,
PERMISSIVE=True,
structure_builder=xpdb.SloppyStructureBuilder(),
)
# 2. Parallel parsing structures and converting to protein records
records = Parallel(n_jobs=-1)(
delayed(parse_pdb_gz_to_json_record)(
sloppyparser,
df.iloc[i]["sequence"],
df.iloc[i]["pdbpath"],
df.iloc[i]["pdbpath"].split("/")[-1],
)
for i in tqdm(range(df.shape[0]))
)
target_col = args.target_col
smiles_col = args.smiles_col
for i, rec in enumerate(records):
row = df.iloc[i]
target = row[target_col]
smiles = row[smiles_col]
rec["target"] = target
rec["ec"] = row["ec"]
rec["taxonomy_id"] = row["taxonomy_id"]
outprefix = args.out_prefix
# 4. write to disk
print("number of records:", len(records))
outfile = os.path.join(f"{outprefix}_pdbrecords.json")
json.dump(records, open(outfile, "w"))
print('Success!') |