Instructions to use Synthyra/Boltz2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Synthyra/Boltz2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="Synthyra/Boltz2", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Synthyra/Boltz2", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
Upload vb_layers_confidence_utils.py with huggingface_hub
Browse files- vb_layers_confidence_utils.py +231 -231
vb_layers_confidence_utils.py
CHANGED
|
@@ -1,231 +1,231 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from torch import nn
|
| 3 |
-
|
| 4 |
-
from . import vb_const as const
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
def compute_collinear_mask(v1, v2):
|
| 8 |
-
norm1 = torch.norm(v1, dim=1, keepdim=True)
|
| 9 |
-
norm2 = torch.norm(v2, dim=1, keepdim=True)
|
| 10 |
-
v1 = v1 / (norm1 + 1e-6)
|
| 11 |
-
v2 = v2 / (norm2 + 1e-6)
|
| 12 |
-
mask_angle = torch.abs(torch.sum(v1 * v2, dim=1)) < 0.9063
|
| 13 |
-
mask_overlap1 = norm1.reshape(-1) > 1e-2
|
| 14 |
-
mask_overlap2 = norm2.reshape(-1) > 1e-2
|
| 15 |
-
return mask_angle & mask_overlap1 & mask_overlap2
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def compute_frame_pred(
|
| 19 |
-
pred_atom_coords,
|
| 20 |
-
frames_idx_true,
|
| 21 |
-
feats,
|
| 22 |
-
multiplicity,
|
| 23 |
-
resolved_mask=None,
|
| 24 |
-
inference=False,
|
| 25 |
-
):
|
| 26 |
-
with torch.amp.autocast("cuda", enabled=False):
|
| 27 |
-
asym_id_token = feats["asym_id"]
|
| 28 |
-
asym_id_atom = torch.bmm(
|
| 29 |
-
feats["atom_to_token"].float(), asym_id_token.unsqueeze(-1).float()
|
| 30 |
-
).squeeze(-1)
|
| 31 |
-
|
| 32 |
-
B, N, _ = pred_atom_coords.shape
|
| 33 |
-
pred_atom_coords = pred_atom_coords.reshape(B // multiplicity, multiplicity, -1, 3)
|
| 34 |
-
frames_idx_pred = (
|
| 35 |
-
frames_idx_true.clone()
|
| 36 |
-
.repeat_interleave(multiplicity, 0)
|
| 37 |
-
.reshape(B // multiplicity, multiplicity, -1, 3)
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
# Iterate through the batch and modify the frames for nonpolymers
|
| 41 |
-
for i, pred_atom_coord in enumerate(pred_atom_coords):
|
| 42 |
-
token_idx = 0
|
| 43 |
-
atom_idx = 0
|
| 44 |
-
for id in torch.unique(asym_id_token[i]):
|
| 45 |
-
mask_chain_token = (asym_id_token[i] == id) * feats["token_pad_mask"][i]
|
| 46 |
-
mask_chain_atom = (asym_id_atom[i] == id) * feats["atom_pad_mask"][i]
|
| 47 |
-
num_tokens = int(mask_chain_token.sum().item())
|
| 48 |
-
num_atoms = int(mask_chain_atom.sum().item())
|
| 49 |
-
if (
|
| 50 |
-
feats["mol_type"][i, token_idx] != const.chain_type_ids["NONPOLYMER"]
|
| 51 |
-
or num_atoms < 3
|
| 52 |
-
):
|
| 53 |
-
token_idx += num_tokens
|
| 54 |
-
atom_idx += num_atoms
|
| 55 |
-
continue
|
| 56 |
-
dist_mat = (
|
| 57 |
-
(
|
| 58 |
-
pred_atom_coord[:, mask_chain_atom.bool()][:, None, :, :]
|
| 59 |
-
- pred_atom_coord[:, mask_chain_atom.bool()][:, :, None, :]
|
| 60 |
-
)
|
| 61 |
-
** 2
|
| 62 |
-
).sum(-1) ** 0.5
|
| 63 |
-
if inference:
|
| 64 |
-
resolved_pair = 1 - (
|
| 65 |
-
feats["atom_pad_mask"][i][mask_chain_atom.bool()][None, :]
|
| 66 |
-
* feats["atom_pad_mask"][i][mask_chain_atom.bool()][:, None]
|
| 67 |
-
).to(torch.float32)
|
| 68 |
-
resolved_pair[resolved_pair == 1] = torch.inf
|
| 69 |
-
indices = torch.sort(dist_mat + resolved_pair, axis=2).indices
|
| 70 |
-
else:
|
| 71 |
-
if resolved_mask is None:
|
| 72 |
-
resolved_mask = feats["atom_resolved_mask"]
|
| 73 |
-
resolved_pair = 1 - (
|
| 74 |
-
resolved_mask[i][mask_chain_atom.bool()][None, :]
|
| 75 |
-
* resolved_mask[i][mask_chain_atom.bool()][:, None]
|
| 76 |
-
).to(torch.float32)
|
| 77 |
-
resolved_pair[resolved_pair == 1] = torch.inf
|
| 78 |
-
indices = torch.sort(dist_mat + resolved_pair, axis=2).indices
|
| 79 |
-
frames = (
|
| 80 |
-
torch.cat(
|
| 81 |
-
[
|
| 82 |
-
indices[:, :, 1:2],
|
| 83 |
-
indices[:, :, 0:1],
|
| 84 |
-
indices[:, :, 2:3],
|
| 85 |
-
],
|
| 86 |
-
dim=2,
|
| 87 |
-
)
|
| 88 |
-
+ atom_idx
|
| 89 |
-
)
|
| 90 |
-
try:
|
| 91 |
-
frames_idx_pred[i, :, token_idx : token_idx + num_atoms, :] = frames
|
| 92 |
-
except Exception as e:
|
| 93 |
-
print(f"Failed to process {feats['pdb_id']} due to {e}")
|
| 94 |
-
token_idx += num_tokens
|
| 95 |
-
atom_idx += num_atoms
|
| 96 |
-
|
| 97 |
-
frames_expanded = pred_atom_coords[
|
| 98 |
-
torch.arange(0, B // multiplicity, 1)[:, None, None, None].to(
|
| 99 |
-
frames_idx_pred.device
|
| 100 |
-
),
|
| 101 |
-
torch.arange(0, multiplicity, 1)[None, :, None, None].to(
|
| 102 |
-
frames_idx_pred.device
|
| 103 |
-
),
|
| 104 |
-
frames_idx_pred,
|
| 105 |
-
].reshape(-1, 3, 3)
|
| 106 |
-
|
| 107 |
-
# Compute masks for collinearity / overlap
|
| 108 |
-
mask_collinear_pred = compute_collinear_mask(
|
| 109 |
-
frames_expanded[:, 1] - frames_expanded[:, 0],
|
| 110 |
-
frames_expanded[:, 1] - frames_expanded[:, 2],
|
| 111 |
-
).reshape(B // multiplicity, multiplicity, -1)
|
| 112 |
-
return frames_idx_pred, mask_collinear_pred * feats["token_pad_mask"][:, None, :]
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
def compute_aggregated_metric(logits, end=1.0):
|
| 116 |
-
# Compute aggregated metric from logits
|
| 117 |
-
num_bins = logits.shape[-1]
|
| 118 |
-
bin_width = end / num_bins
|
| 119 |
-
bounds = torch.arange(
|
| 120 |
-
start=0.5 * bin_width, end=end, step=bin_width, device=logits.device
|
| 121 |
-
)
|
| 122 |
-
probs = nn.functional.softmax(logits, dim=-1)
|
| 123 |
-
plddt = torch.sum(
|
| 124 |
-
probs * bounds.view(*((1,) * len(probs.shape[:-1])), *bounds.shape),
|
| 125 |
-
dim=-1,
|
| 126 |
-
)
|
| 127 |
-
return plddt
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
def tm_function(d, Nres):
|
| 131 |
-
d0 = 1.24 * (torch.clip(Nres, min=19) - 15) ** (1 / 3) - 1.8
|
| 132 |
-
return 1 / (1 + (d / d0) ** 2)
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
def compute_ptms(logits, x_preds, feats, multiplicity):
|
| 136 |
-
# It needs to take as input the mask of the frames as they are not used to compute the PTM
|
| 137 |
-
_, mask_collinear_pred = compute_frame_pred(
|
| 138 |
-
x_preds, feats["frames_idx"], feats, multiplicity, inference=True
|
| 139 |
-
)
|
| 140 |
-
# mask overlapping, collinear tokens and ions (invalid frames)
|
| 141 |
-
mask_pad = feats["token_pad_mask"].repeat_interleave(multiplicity, 0)
|
| 142 |
-
maski = mask_collinear_pred.reshape(-1, mask_collinear_pred.shape[-1])
|
| 143 |
-
pair_mask_ptm = maski[:, :, None] * mask_pad[:, None, :] * mask_pad[:, :, None]
|
| 144 |
-
asym_id = feats["asym_id"].repeat_interleave(multiplicity, 0)
|
| 145 |
-
pair_mask_iptm = (
|
| 146 |
-
maski[:, :, None]
|
| 147 |
-
* (asym_id[:, None, :] != asym_id[:, :, None])
|
| 148 |
-
* mask_pad[:, None, :]
|
| 149 |
-
* mask_pad[:, :, None]
|
| 150 |
-
)
|
| 151 |
-
num_bins = logits.shape[-1]
|
| 152 |
-
bin_width = 32.0 / num_bins
|
| 153 |
-
end = 32.0
|
| 154 |
-
pae_value = torch.arange(
|
| 155 |
-
start=0.5 * bin_width, end=end, step=bin_width, device=logits.device
|
| 156 |
-
).unsqueeze(0)
|
| 157 |
-
N_res = mask_pad.sum(dim=-1, keepdim=True)
|
| 158 |
-
tm_value = tm_function(pae_value, N_res).unsqueeze(1).unsqueeze(2)
|
| 159 |
-
probs = nn.functional.softmax(logits, dim=-1)
|
| 160 |
-
tm_expected_value = torch.sum(
|
| 161 |
-
probs * tm_value,
|
| 162 |
-
dim=-1,
|
| 163 |
-
) # shape (B, N, N)
|
| 164 |
-
ptm = torch.max(
|
| 165 |
-
torch.sum(tm_expected_value * pair_mask_ptm, dim=-1)
|
| 166 |
-
/ (torch.sum(pair_mask_ptm, dim=-1) + 1e-5),
|
| 167 |
-
dim=1,
|
| 168 |
-
).values
|
| 169 |
-
iptm = torch.max(
|
| 170 |
-
torch.sum(tm_expected_value * pair_mask_iptm, dim=-1)
|
| 171 |
-
/ (torch.sum(pair_mask_iptm, dim=-1) + 1e-5),
|
| 172 |
-
dim=1,
|
| 173 |
-
).values
|
| 174 |
-
|
| 175 |
-
# compute ligand and protein iPTM
|
| 176 |
-
token_type = feats["mol_type"]
|
| 177 |
-
token_type = token_type.repeat_interleave(multiplicity, 0)
|
| 178 |
-
is_ligand_token = (token_type == const.chain_type_ids["NONPOLYMER"]).float()
|
| 179 |
-
is_protein_token = (token_type == const.chain_type_ids["PROTEIN"]).float()
|
| 180 |
-
|
| 181 |
-
ligand_iptm_mask = (
|
| 182 |
-
maski[:, :, None]
|
| 183 |
-
* (asym_id[:, None, :] != asym_id[:, :, None])
|
| 184 |
-
* mask_pad[:, None, :]
|
| 185 |
-
* mask_pad[:, :, None]
|
| 186 |
-
* (
|
| 187 |
-
(is_ligand_token[:, :, None] * is_protein_token[:, None, :])
|
| 188 |
-
+ (is_protein_token[:, :, None] * is_ligand_token[:, None, :])
|
| 189 |
-
)
|
| 190 |
-
)
|
| 191 |
-
protein_ipmt_mask = (
|
| 192 |
-
maski[:, :, None]
|
| 193 |
-
* (asym_id[:, None, :] != asym_id[:, :, None])
|
| 194 |
-
* mask_pad[:, None, :]
|
| 195 |
-
* mask_pad[:, :, None]
|
| 196 |
-
* (is_protein_token[:, :, None] * is_protein_token[:, None, :])
|
| 197 |
-
)
|
| 198 |
-
|
| 199 |
-
ligand_iptm = torch.max(
|
| 200 |
-
torch.sum(tm_expected_value * ligand_iptm_mask, dim=-1)
|
| 201 |
-
/ (torch.sum(ligand_iptm_mask, dim=-1) + 1e-5),
|
| 202 |
-
dim=1,
|
| 203 |
-
).values
|
| 204 |
-
protein_iptm = torch.max(
|
| 205 |
-
torch.sum(tm_expected_value * protein_ipmt_mask, dim=-1)
|
| 206 |
-
/ (torch.sum(protein_ipmt_mask, dim=-1) + 1e-5),
|
| 207 |
-
dim=1,
|
| 208 |
-
).values
|
| 209 |
-
|
| 210 |
-
# Compute pair chain ipTM
|
| 211 |
-
chain_pair_iptm = {}
|
| 212 |
-
asym_ids_list = torch.unique(asym_id).tolist()
|
| 213 |
-
for idx1 in asym_ids_list:
|
| 214 |
-
chain_iptm = {}
|
| 215 |
-
for idx2 in asym_ids_list:
|
| 216 |
-
mask_pair_chain = (
|
| 217 |
-
maski[:, :, None]
|
| 218 |
-
* (asym_id[:, None, :] == idx1)
|
| 219 |
-
* (asym_id[:, :, None] == idx2)
|
| 220 |
-
* mask_pad[:, None, :]
|
| 221 |
-
* mask_pad[:, :, None]
|
| 222 |
-
)
|
| 223 |
-
|
| 224 |
-
chain_iptm[idx2] = torch.max(
|
| 225 |
-
torch.sum(tm_expected_value * mask_pair_chain, dim=-1)
|
| 226 |
-
/ (torch.sum(mask_pair_chain, dim=-1) + 1e-5),
|
| 227 |
-
dim=1,
|
| 228 |
-
).values
|
| 229 |
-
chain_pair_iptm[idx1] = chain_iptm
|
| 230 |
-
|
| 231 |
-
return ptm, iptm, ligand_iptm, protein_iptm, chain_pair_iptm
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torch import nn
|
| 3 |
+
|
| 4 |
+
from . import vb_const as const
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def compute_collinear_mask(v1, v2):
|
| 8 |
+
norm1 = torch.norm(v1, dim=1, keepdim=True)
|
| 9 |
+
norm2 = torch.norm(v2, dim=1, keepdim=True)
|
| 10 |
+
v1 = v1 / (norm1 + 1e-6)
|
| 11 |
+
v2 = v2 / (norm2 + 1e-6)
|
| 12 |
+
mask_angle = torch.abs(torch.sum(v1 * v2, dim=1)) < 0.9063
|
| 13 |
+
mask_overlap1 = norm1.reshape(-1) > 1e-2
|
| 14 |
+
mask_overlap2 = norm2.reshape(-1) > 1e-2
|
| 15 |
+
return mask_angle & mask_overlap1 & mask_overlap2
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def compute_frame_pred(
|
| 19 |
+
pred_atom_coords,
|
| 20 |
+
frames_idx_true,
|
| 21 |
+
feats,
|
| 22 |
+
multiplicity,
|
| 23 |
+
resolved_mask=None,
|
| 24 |
+
inference=False,
|
| 25 |
+
):
|
| 26 |
+
with torch.amp.autocast("cuda", enabled=False):
|
| 27 |
+
asym_id_token = feats["asym_id"]
|
| 28 |
+
asym_id_atom = torch.bmm(
|
| 29 |
+
feats["atom_to_token"].float(), asym_id_token.unsqueeze(-1).float()
|
| 30 |
+
).squeeze(-1)
|
| 31 |
+
|
| 32 |
+
B, N, _ = pred_atom_coords.shape
|
| 33 |
+
pred_atom_coords = pred_atom_coords.reshape(B // multiplicity, multiplicity, -1, 3)
|
| 34 |
+
frames_idx_pred = (
|
| 35 |
+
frames_idx_true.clone()
|
| 36 |
+
.repeat_interleave(multiplicity, 0)
|
| 37 |
+
.reshape(B // multiplicity, multiplicity, -1, 3)
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Iterate through the batch and modify the frames for nonpolymers
|
| 41 |
+
for i, pred_atom_coord in enumerate(pred_atom_coords):
|
| 42 |
+
token_idx = 0
|
| 43 |
+
atom_idx = 0
|
| 44 |
+
for id in torch.unique(asym_id_token[i]):
|
| 45 |
+
mask_chain_token = (asym_id_token[i] == id) * feats["token_pad_mask"][i]
|
| 46 |
+
mask_chain_atom = (asym_id_atom[i] == id) * feats["atom_pad_mask"][i]
|
| 47 |
+
num_tokens = int(mask_chain_token.sum().item())
|
| 48 |
+
num_atoms = int(mask_chain_atom.sum().item())
|
| 49 |
+
if (
|
| 50 |
+
feats["mol_type"][i, token_idx] != const.chain_type_ids["NONPOLYMER"]
|
| 51 |
+
or num_atoms < 3
|
| 52 |
+
):
|
| 53 |
+
token_idx += num_tokens
|
| 54 |
+
atom_idx += num_atoms
|
| 55 |
+
continue
|
| 56 |
+
dist_mat = (
|
| 57 |
+
(
|
| 58 |
+
pred_atom_coord[:, mask_chain_atom.bool()][:, None, :, :]
|
| 59 |
+
- pred_atom_coord[:, mask_chain_atom.bool()][:, :, None, :]
|
| 60 |
+
)
|
| 61 |
+
** 2
|
| 62 |
+
).sum(-1) ** 0.5
|
| 63 |
+
if inference:
|
| 64 |
+
resolved_pair = 1 - (
|
| 65 |
+
feats["atom_pad_mask"][i][mask_chain_atom.bool()][None, :]
|
| 66 |
+
* feats["atom_pad_mask"][i][mask_chain_atom.bool()][:, None]
|
| 67 |
+
).to(torch.float32)
|
| 68 |
+
resolved_pair[resolved_pair == 1] = torch.inf
|
| 69 |
+
indices = torch.sort(dist_mat + resolved_pair, axis=2).indices
|
| 70 |
+
else:
|
| 71 |
+
if resolved_mask is None:
|
| 72 |
+
resolved_mask = feats["atom_resolved_mask"]
|
| 73 |
+
resolved_pair = 1 - (
|
| 74 |
+
resolved_mask[i][mask_chain_atom.bool()][None, :]
|
| 75 |
+
* resolved_mask[i][mask_chain_atom.bool()][:, None]
|
| 76 |
+
).to(torch.float32)
|
| 77 |
+
resolved_pair[resolved_pair == 1] = torch.inf
|
| 78 |
+
indices = torch.sort(dist_mat + resolved_pair, axis=2).indices
|
| 79 |
+
frames = (
|
| 80 |
+
torch.cat(
|
| 81 |
+
[
|
| 82 |
+
indices[:, :, 1:2],
|
| 83 |
+
indices[:, :, 0:1],
|
| 84 |
+
indices[:, :, 2:3],
|
| 85 |
+
],
|
| 86 |
+
dim=2,
|
| 87 |
+
)
|
| 88 |
+
+ atom_idx
|
| 89 |
+
)
|
| 90 |
+
try:
|
| 91 |
+
frames_idx_pred[i, :, token_idx : token_idx + num_atoms, :] = frames
|
| 92 |
+
except Exception as e:
|
| 93 |
+
print(f"Failed to process {feats['pdb_id']} due to {e}")
|
| 94 |
+
token_idx += num_tokens
|
| 95 |
+
atom_idx += num_atoms
|
| 96 |
+
|
| 97 |
+
frames_expanded = pred_atom_coords[
|
| 98 |
+
torch.arange(0, B // multiplicity, 1)[:, None, None, None].to(
|
| 99 |
+
frames_idx_pred.device
|
| 100 |
+
),
|
| 101 |
+
torch.arange(0, multiplicity, 1)[None, :, None, None].to(
|
| 102 |
+
frames_idx_pred.device
|
| 103 |
+
),
|
| 104 |
+
frames_idx_pred,
|
| 105 |
+
].reshape(-1, 3, 3)
|
| 106 |
+
|
| 107 |
+
# Compute masks for collinearity / overlap
|
| 108 |
+
mask_collinear_pred = compute_collinear_mask(
|
| 109 |
+
frames_expanded[:, 1] - frames_expanded[:, 0],
|
| 110 |
+
frames_expanded[:, 1] - frames_expanded[:, 2],
|
| 111 |
+
).reshape(B // multiplicity, multiplicity, -1)
|
| 112 |
+
return frames_idx_pred, mask_collinear_pred * feats["token_pad_mask"][:, None, :]
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def compute_aggregated_metric(logits, end=1.0):
|
| 116 |
+
# Compute aggregated metric from logits
|
| 117 |
+
num_bins = logits.shape[-1]
|
| 118 |
+
bin_width = end / num_bins
|
| 119 |
+
bounds = torch.arange(
|
| 120 |
+
start=0.5 * bin_width, end=end, step=bin_width, device=logits.device
|
| 121 |
+
)
|
| 122 |
+
probs = nn.functional.softmax(logits, dim=-1)
|
| 123 |
+
plddt = torch.sum(
|
| 124 |
+
probs * bounds.view(*((1,) * len(probs.shape[:-1])), *bounds.shape),
|
| 125 |
+
dim=-1,
|
| 126 |
+
)
|
| 127 |
+
return plddt
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def tm_function(d, Nres):
|
| 131 |
+
d0 = 1.24 * (torch.clip(Nres, min=19) - 15) ** (1 / 3) - 1.8
|
| 132 |
+
return 1 / (1 + (d / d0) ** 2)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def compute_ptms(logits, x_preds, feats, multiplicity):
|
| 136 |
+
# It needs to take as input the mask of the frames as they are not used to compute the PTM
|
| 137 |
+
_, mask_collinear_pred = compute_frame_pred(
|
| 138 |
+
x_preds, feats["frames_idx"], feats, multiplicity, inference=True
|
| 139 |
+
)
|
| 140 |
+
# mask overlapping, collinear tokens and ions (invalid frames)
|
| 141 |
+
mask_pad = feats["token_pad_mask"].repeat_interleave(multiplicity, 0)
|
| 142 |
+
maski = mask_collinear_pred.reshape(-1, mask_collinear_pred.shape[-1])
|
| 143 |
+
pair_mask_ptm = maski[:, :, None] * mask_pad[:, None, :] * mask_pad[:, :, None]
|
| 144 |
+
asym_id = feats["asym_id"].repeat_interleave(multiplicity, 0)
|
| 145 |
+
pair_mask_iptm = (
|
| 146 |
+
maski[:, :, None]
|
| 147 |
+
* (asym_id[:, None, :] != asym_id[:, :, None])
|
| 148 |
+
* mask_pad[:, None, :]
|
| 149 |
+
* mask_pad[:, :, None]
|
| 150 |
+
)
|
| 151 |
+
num_bins = logits.shape[-1]
|
| 152 |
+
bin_width = 32.0 / num_bins
|
| 153 |
+
end = 32.0
|
| 154 |
+
pae_value = torch.arange(
|
| 155 |
+
start=0.5 * bin_width, end=end, step=bin_width, device=logits.device
|
| 156 |
+
).unsqueeze(0)
|
| 157 |
+
N_res = mask_pad.sum(dim=-1, keepdim=True)
|
| 158 |
+
tm_value = tm_function(pae_value, N_res).unsqueeze(1).unsqueeze(2)
|
| 159 |
+
probs = nn.functional.softmax(logits, dim=-1)
|
| 160 |
+
tm_expected_value = torch.sum(
|
| 161 |
+
probs * tm_value,
|
| 162 |
+
dim=-1,
|
| 163 |
+
) # shape (B, N, N)
|
| 164 |
+
ptm = torch.max(
|
| 165 |
+
torch.sum(tm_expected_value * pair_mask_ptm, dim=-1)
|
| 166 |
+
/ (torch.sum(pair_mask_ptm, dim=-1) + 1e-5),
|
| 167 |
+
dim=1,
|
| 168 |
+
).values
|
| 169 |
+
iptm = torch.max(
|
| 170 |
+
torch.sum(tm_expected_value * pair_mask_iptm, dim=-1)
|
| 171 |
+
/ (torch.sum(pair_mask_iptm, dim=-1) + 1e-5),
|
| 172 |
+
dim=1,
|
| 173 |
+
).values
|
| 174 |
+
|
| 175 |
+
# compute ligand and protein iPTM
|
| 176 |
+
token_type = feats["mol_type"]
|
| 177 |
+
token_type = token_type.repeat_interleave(multiplicity, 0)
|
| 178 |
+
is_ligand_token = (token_type == const.chain_type_ids["NONPOLYMER"]).float()
|
| 179 |
+
is_protein_token = (token_type == const.chain_type_ids["PROTEIN"]).float()
|
| 180 |
+
|
| 181 |
+
ligand_iptm_mask = (
|
| 182 |
+
maski[:, :, None]
|
| 183 |
+
* (asym_id[:, None, :] != asym_id[:, :, None])
|
| 184 |
+
* mask_pad[:, None, :]
|
| 185 |
+
* mask_pad[:, :, None]
|
| 186 |
+
* (
|
| 187 |
+
(is_ligand_token[:, :, None] * is_protein_token[:, None, :])
|
| 188 |
+
+ (is_protein_token[:, :, None] * is_ligand_token[:, None, :])
|
| 189 |
+
)
|
| 190 |
+
)
|
| 191 |
+
protein_ipmt_mask = (
|
| 192 |
+
maski[:, :, None]
|
| 193 |
+
* (asym_id[:, None, :] != asym_id[:, :, None])
|
| 194 |
+
* mask_pad[:, None, :]
|
| 195 |
+
* mask_pad[:, :, None]
|
| 196 |
+
* (is_protein_token[:, :, None] * is_protein_token[:, None, :])
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
ligand_iptm = torch.max(
|
| 200 |
+
torch.sum(tm_expected_value * ligand_iptm_mask, dim=-1)
|
| 201 |
+
/ (torch.sum(ligand_iptm_mask, dim=-1) + 1e-5),
|
| 202 |
+
dim=1,
|
| 203 |
+
).values
|
| 204 |
+
protein_iptm = torch.max(
|
| 205 |
+
torch.sum(tm_expected_value * protein_ipmt_mask, dim=-1)
|
| 206 |
+
/ (torch.sum(protein_ipmt_mask, dim=-1) + 1e-5),
|
| 207 |
+
dim=1,
|
| 208 |
+
).values
|
| 209 |
+
|
| 210 |
+
# Compute pair chain ipTM
|
| 211 |
+
chain_pair_iptm = {}
|
| 212 |
+
asym_ids_list = torch.unique(asym_id).tolist()
|
| 213 |
+
for idx1 in asym_ids_list:
|
| 214 |
+
chain_iptm = {}
|
| 215 |
+
for idx2 in asym_ids_list:
|
| 216 |
+
mask_pair_chain = (
|
| 217 |
+
maski[:, :, None]
|
| 218 |
+
* (asym_id[:, None, :] == idx1)
|
| 219 |
+
* (asym_id[:, :, None] == idx2)
|
| 220 |
+
* mask_pad[:, None, :]
|
| 221 |
+
* mask_pad[:, :, None]
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
chain_iptm[idx2] = torch.max(
|
| 225 |
+
torch.sum(tm_expected_value * mask_pair_chain, dim=-1)
|
| 226 |
+
/ (torch.sum(mask_pair_chain, dim=-1) + 1e-5),
|
| 227 |
+
dim=1,
|
| 228 |
+
).values
|
| 229 |
+
chain_pair_iptm[idx1] = chain_iptm
|
| 230 |
+
|
| 231 |
+
return ptm, iptm, ligand_iptm, protein_iptm, chain_pair_iptm
|