import torch
import numpy as np
import random
from data_utils import featurize
from model_utils import ProteinMPNN
restype_1to3 = {'A': 'ALA', 'R': 'ARG', 'N': 'ASN', 'D': 'ASP', 'C': 'CYS', 'Q': 'GLN', 'E': 'GLU', 'G': 'GLY', 'H': 'HIS', 'I': 'ILE', 'L': 'LEU', 'K': 'LYS', 'M': 'MET', 'F': 'PHE', 'P': 'PRO', 'S': 'SER', 'T': 'THR', 'W': 'TRP', 'Y': 'TYR', 'V': 'VAL'}
restype_STRtoINT = {'A': 0, 'C': 1, 'D': 2, 'E': 3, 'F': 4, 'G': 5, 'H': 6, 'I': 7, 'K': 8, 'L': 9, 'M': 10, 'N': 11, 'P': 12, 'Q': 13, 'R': 14, 'S': 15, 'T': 16, 'V': 17, 'W': 18, 'Y': 19, 'X': 20}
restype_INTtoSTR = {0: 'A', 1: 'C', 2: 'D', 3: 'E', 4: 'F', 5: 'G', 6: 'H', 7: 'I', 8: 'K', 9: 'L', 10: 'M', 11: 'N', 12: 'P', 13: 'Q', 14: 'R', 15: 'S', 16: 'T', 17: 'V', 18: 'W', 19: 'Y', 20: 'X'}
alphabet = list(restype_STRtoINT)
element_list = ['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne', 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar', 'K', 'Ca', 'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr', 'Rb', 'Sr', 'Y', 'Zr', 'Nb', 'Mb', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I', 'Xe', 'Cs', 'Ba', 'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn', 'Fr', 'Ra', 'Ac', 'Th', 'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es', 'Fm', 'Md', 'No', 'Lr', 'Rf', 'Db', 'Sg', 'Bh', 'Hs', 'Mt', 'Ds', 'Rg', 'Cn', 'Uut', 'Fl', 'Uup', 'Lv', 'Uus', 'Uuo']
element_list = [item.upper() for item in element_list]
element_dict = dict(zip(element_list, range(1,len(element_list))))
device = torch.device("cuda:0" if (torch.cuda.is_available()) else "cpu")
model_type="antibody_mpnn"
ligand_mpnn_use_side_chain_context = False
batch_size = 2
number_of_batches = 2
temperature=0.1
seed = 1
if seed:
seed=seed
else:
seed=int(np.random.randint(0, high=99999, size=1, dtype=int)[0])
torch.manual_seed(seed)
random.seed(seed)
np.random.seed(seed)
ligand_mpnn_use_atom_context = True
checkpoint_protein_mpnn="/databases/mpnn/vanilla_model_weights/v_48_020.pt"
checkpoint_ligand_mpnn="/databases/mpnn/ligand_mpnn_model_weights/v_32_010.pt"
checkpoint_per_residue_label_membrane_mpnn="/databases/mpnn/tmd_per_residue_weights/tmd_v_48_020.pt"
checkpoint_global_label_membrane_mpnn="/databases/mpnn/tmd_weights/v_48_020.pt"
checkpoint_soluble_mpnn="/databases/mpnn/no_transmembrane/v_48_020.pt"
checkpoint_pssm_mpnn="/databases/mpnn/pssm_model_weights/v_48_020.pt"
checkpoint_antibody_mpnn="/databases/mpnn/antibody_mpnn_model_weights/v_48_020_bias_005.pt"
if model_type == "protein_mpnn":
checkpoint_path = checkpoint_protein_mpnn
elif model_type == "ligand_mpnn":
checkpoint_path = checkpoint_ligand_mpnn
elif model_type == "per_residue_label_membrane_mpnn":
checkpoint_path = checkpoint_per_residue_label_membrane_mpnn
elif model_type == "global_label_membrane_mpnn":
checkpoint_path = checkpoint_global_label_membrane_mpnn
elif model_type == "soluble_mpnn":
checkpoint_path = checkpoint_soluble_mpnn
elif model_type == "pssm_mpnn":
checkpoint_path = checkpoint_pssm_mpnn
elif model_type == "antibody_mpnn":
checkpoint_path = checkpoint_antibody_mpnn
checkpoint = torch.load(checkpoint_path, map_location=device)
if model_type == "ligand_mpnn":
atom_context_num = 16
k_neighbors=32
ligand_mpnn_use_side_chain_context = ligand_mpnn_use_side_chain_context
elif model_type == "antibody_mpnn":
atom_context_num = 1
ligand_mpnn_use_side_chain_context = 0
k_neighbors=48
else:
atom_context_num = 1
ligand_mpnn_use_side_chain_context = 0
k_neighbors=checkpoint["num_edges"]
model = ProteinMPNN(node_features=128,
edge_features=128,
hidden_dim=128,
num_encoder_layers=3,
num_decoder_layers=3,
k_neighbors=k_neighbors,
device=device,
atom_context_num=atom_context_num,
model_type=model_type,
ligand_mpnn_use_side_chain_context=ligand_mpnn_use_side_chain_context)
model.load_state_dict(checkpoint['model_state_dict'])
model.to(device)
model.eval()
L = 66
M = 10
protein_dict = {"X": torch.randn([L,4,3], device=device, dtype=torch.float32),
"R_idx": torch.arange(start=0, end=L, device=device, dtype=torch.int64),
"chain_labels": torch.zeros([L], device=device, dtype=torch.int64),
"S": torch.zeros([L], device=device, dtype=torch.int64),
"mask": torch.ones([L], device=device, dtype=torch.float32),
"chain_mask": torch.ones([L], device=device, dtype=torch.float32),
"side_chain_mask": torch.ones([L], device=device, dtype=torch.float32),
"membrane_per_residue_labels": torch.ones([L], device=device, dtype=torch.int64),
"pssm": torch.ones([L,20], device=device, dtype=torch.float32),
"xyz_37": torch.zeros([L,37,3], device=device, dtype=torch.float32),
"xyz_37_m": torch.zeros([L,37], device=device, dtype=torch.float32),
"Y": torch.zeros([M,3], device=device, dtype=torch.float32),
"Y_t": torch.zeros([M], device=device, dtype=torch.float32),
"Y_m": torch.zeros([M], device=device, dtype=torch.float32),
"bias": torch.zeros([L,21], device=device),
"pair_bias": torch.zeros([L,21,L,21], device=device),
"symmetry_residues": [[]],
"symmetry_weights": [[]],
}
with torch.no_grad():
feature_dict = featurize(protein_dict,
cutoff_for_score=8.0,
use_atom_context=ligand_mpnn_use_atom_context)
feature_dict["batch_size"] = batch_size
B, L, _, _ = feature_dict["X"].shape
feature_dict["temperature"] = temperature
feature_dict["bias"] = protein_dict["bias"][None,]
feature_dict["pair_bias"] = protein_dict["pair_bias"][None,]
feature_dict["symmetry_residues"] = protein_dict["symmetry_residues"]
feature_dict["symmetry_weights"] = protein_dict["symmetry_weights"]
output_list = []
for idx in range(number_of_batches):
feature_dict["randn"] = torch.randn([feature_dict["batch_size"], feature_dict["mask"].shape[1]], device=device)
output_dict = model.sample(feature_dict)
output_list.append(output_dict)
print(seed, model_type, _, output_dict["S"])