Spaces:
Sleeping
Sleeping
File size: 8,711 Bytes
bebad14 d7f69ca dffaf30 bebad14 d7f69ca bebad14 d7f69ca bebad14 d7f69ca 28cb117 d7f69ca 28cb117 d7f69ca bebad14 d7f69ca 8b16c9c d7f69ca bebad14 d7f69ca bebad14 f624b87 bebad14 d7f69ca bebad14 fd32c9f 1891093 fd32c9f 809b460 1891093 fd32c9f d7f69ca bebad14 28cb117 bebad14 44470f9 28cb117 fd32c9f 28cb117 d7f69ca 28cb117 d7f69ca 28cb117 8b16c9c bebad14 d7f69ca dffaf30 bebad14 |
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
import time
import json
import gradio as gr
from gradio_molecule3d import Molecule3D
import torch
from torch_geometric.data import HeteroData
import numpy as np
from loguru import logger
from Bio import PDB
from Bio.PDB.PDBIO import PDBIO
from pinder.core.loader.geodata import structure2tensor
from pinder.core.loader.structure import Structure
from src.models.pinder_module import PinderLitModule
try:
from torch_cluster import knn_graph
torch_cluster_installed = True
except ImportError:
logger.warning(
"torch-cluster is not installed!"
"Please install the appropriate library for your pytorch installation."
"See https://github.com/rusty1s/pytorch_cluster/issues/185 for background."
)
torch_cluster_installed = False
def get_props_pdb(pdb_file):
structure = Structure.read_pdb(pdb_file)
atom_mask = np.isin(getattr(structure, "atom_name"), list(["CA"]))
calpha = structure[atom_mask].copy()
props = structure2tensor(
atom_coordinates=structure.coord,
atom_types=structure.atom_name,
element_types=structure.element,
residue_coordinates=calpha.coord,
residue_types=calpha.res_name,
residue_ids=calpha.res_id,
)
return props
def create_graph(pdb_1, pdb_2, k=5, device: torch.device = torch.device("cpu")):
props_ligand = get_props_pdb(pdb_1)
props_receptor = get_props_pdb(pdb_2)
data = HeteroData()
data["ligand"].x = props_ligand["atom_types"]
data["ligand"].pos = props_ligand["atom_coordinates"]
data["ligand", "ligand"].edge_index = knn_graph(data["ligand"].pos, k=k)
data["receptor"].x = props_receptor["atom_types"]
data["receptor"].pos = props_receptor["atom_coordinates"]
data["receptor", "receptor"].edge_index = knn_graph(data["receptor"].pos, k=k)
data = data.to(device)
return data
def update_pdb_coordinates_from_tensor(
input_filename, output_filename, coordinates_tensor
):
r"""
Updates atom coordinates in a PDB file with new transformed coordinates provided in a tensor.
Parameters:
- input_filename (str): Path to the original PDB file.
- output_filename (str): Path to the new PDB file to save updated coordinates.
- coordinates_tensor (torch.Tensor): Tensor of shape (1, N, 3) with transformed coordinates.
"""
# Convert the tensor to a list of tuples
new_coordinates = coordinates_tensor.squeeze(0).tolist()
# Create a parser and parse the structure
parser = PDB.PDBParser(QUIET=True)
structure = parser.get_structure("structure", input_filename)
# Flattened iterator for atoms to update coordinates
atom_iterator = (
atom
for model in structure
for chain in model
for residue in chain
for atom in residue
)
# Update each atom's coordinates
for atom, (new_x, new_y, new_z) in zip(atom_iterator, new_coordinates):
original_anisou = atom.get_anisou()
original_uij = atom.get_siguij()
original_tm = atom.get_sigatm()
original_occupancy = atom.get_occupancy()
original_bfactor = atom.get_bfactor()
original_altloc = atom.get_altloc()
original_serial_number = atom.get_serial_number()
original_element = atom.get_charge()
original_parent = atom.get_parent()
original_radius = atom.get_radius()
# Update only the atom coordinates, keep other fields intact
atom.coord = np.array([new_x, new_y, new_z])
# Reapply the preserved properties
atom.set_anisou(original_anisou)
atom.set_siguij(original_uij)
atom.set_sigatm(original_tm)
atom.set_occupancy(original_occupancy)
atom.set_bfactor(original_bfactor)
atom.set_altloc(original_altloc)
# atom.set_fullname(original_fullname)
atom.set_serial_number(original_serial_number)
atom.set_charge(original_element)
atom.set_radius(original_radius)
atom.set_parent(original_parent)
# atom.set_name(original_name)
# atom.set_leve
# Save the updated structure to a new PDB file
io = PDBIO()
io.set_structure(structure)
io.save(output_filename)
# Return the path to the updated PDB file
return output_filename
def merge_pdb_files(file1, file2, output_file):
r"""
Merges two PDB files by concatenating them without altering their contents.
Parameters:
- file1 (str): Path to the first PDB file (e.g., receptor).
- file2 (str): Path to the second PDB file (e.g., ligand).
- output_file (str): Path to the output file where the merged structure will be saved.
"""
with open(output_file, "w") as outfile:
# Copy the contents of the first file
with open(file1, "r") as f1:
lines = f1.readlines()
# Write all lines except the last 'END' line
outfile.writelines(lines[:-1])
# Copy the contents of the second file
with open(file2, "r") as f2:
outfile.write(f2.read())
print(f"Merged PDB saved to {output_file}")
return output_file
def predict(
input_seq_1, input_msa_1, input_protein_1, input_seq_2, input_msa_2, input_protein_2
):
start_time = time.time()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
logger.info(f"Using device: {device}")
data = create_graph(input_protein_1, input_protein_2, k=10, device=device)
logger.info("Created graph data")
model = PinderLitModule.load_from_checkpoint("./checkpoints/epoch_010.ckpt")
model = model.to(device)
model.eval()
logger.info("Loaded model")
with torch.no_grad():
receptor_coords, ligand_coords = model(data)
file1 = update_pdb_coordinates_from_tensor(
input_protein_1, "holo_ligand.pdb", ligand_coords
)
file2 = update_pdb_coordinates_from_tensor(
input_protein_2, "holo_receptor.pdb", receptor_coords
)
out_pdb = merge_pdb_files(file1, file2, "output.pdb")
# return an output pdb file with the protein and two chains A and B.
# also return a JSON with any metrics you want to report
metrics = {"mean_plddt": 80, "binding_affinity": 2}
end_time = time.time()
run_time = end_time - start_time
return out_pdb, json.dumps(metrics), run_time
with gr.Blocks() as app:
gr.Markdown("# Template for inference")
gr.Markdown("EquiMPNN MOdel")
with gr.Row():
with gr.Column():
input_seq_1 = gr.Textbox(lines=3, label="Input Protein 1 sequence (FASTA)")
input_msa_1 = gr.File(label="Input MSA Protein 1 (A3M)")
input_protein_1 = gr.File(label="Input Protein 2 monomer (PDB)")
with gr.Column():
input_seq_2 = gr.Textbox(lines=3, label="Input Protein 2 sequence (FASTA)")
input_msa_2 = gr.File(label="Input MSA Protein 2 (A3M)")
input_protein_2 = gr.File(label="Input Protein 2 structure (PDB)")
# define any options here
# for automated inference the default options are used
# slider_option = gr.Slider(0,10, label="Slider Option")
# checkbox_option = gr.Checkbox(label="Checkbox Option")
# dropdown_option = gr.Dropdown(["Option 1", "Option 2", "Option 3"], label="Radio Option")
btn = gr.Button("Run Inference")
gr.Examples(
[
[
"GSGSPLAQQIKNIHSFIHQAKAAGRMDEVRTLQENLHQLMHEYFQQSD",
"3v1c_A.pdb",
"GSGSPLAQQIKNIHSFIHQAKAAGRMDEVRTLQENLHQLMHEYFQQSD",
"3v1c_B.pdb",
],
],
[input_seq_1, input_protein_1, input_seq_2, input_protein_2],
)
reps = [
{
"model": 0,
"style": "cartoon",
"chain": "A",
"color": "whiteCarbon",
},
{
"model": 0,
"style": "cartoon",
"chain": "B",
"color": "greenCarbon",
},
{
"model": 0,
"chain": "A",
"style": "stick",
"sidechain": True,
"color": "whiteCarbon",
},
{
"model": 0,
"chain": "B",
"style": "stick",
"sidechain": True,
"color": "greenCarbon",
},
]
# outputs
out = Molecule3D(reps=reps)
metrics = gr.JSON(label="Metrics")
run_time = gr.Textbox(label="Runtime")
btn.click(
predict,
inputs=[
input_seq_1,
input_msa_1,
input_protein_1,
input_seq_2,
input_msa_2,
input_protein_2,
],
outputs=[out, metrics, run_time],
)
app.launch()
|