File size: 14,049 Bytes
e611d1f |
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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
"""
Simplified graph utilities for SphereGraphDataset.
Contains only the essential functions needed without external dependencies.
"""
import numpy as np
from pathlib import Path
from typing import List, Optional, Tuple
import torch
from torch_geometric.data import Data
from torch_geometric.nn import radius_graph
def parse_binding_site_txt(txt_path: Path) -> Tuple[List[str], List[str], List[torch.Tensor]]:
"""
Parse a fasta-like txt file with 3 lines per entry: >id, sequence, label
Returns: (rna_ids, rna_seqs, labels)
"""
rna_ids, rna_seqs, labels = [], [], []
with open(txt_path, 'r') as f:
lines = [line.strip() for line in f if line.strip()]
for i in range(0, len(lines), 3):
rna_id = lines[i][1:] if lines[i].startswith('>') else lines[i]
seq = lines[i+1]
label_str = lines[i+2]
label = torch.tensor([int(x) for x in label_str], dtype=torch.float32)
rna_ids.append(rna_id)
rna_seqs.append(seq)
labels.append(label)
return rna_ids, rna_seqs, labels
def create_graph_data_full(
embeddings: np.ndarray,
backbone_atoms: np.ndarray,
rsa_values: np.ndarray,
epitope_indices: List[int],
pdb_id: str,
chain_id: str,
num_rbf: int = 16,
num_posenc: int = 16,
radius: float = 18.0,
verbose: bool = True
) -> Optional[Data]:
"""
Create a PyTorch Geometric Data object for a full protein graph.
Args:
embeddings: Full protein embeddings [seq_len, embed_dim]
backbone_atoms: Full protein backbone atoms [seq_len, 3, 3] (N, CA, C)
rsa_values: Full protein RSA values [seq_len]
epitope_indices: List of epitope residue indices
pdb_id: PDB ID
chain_id: Chain ID
num_rbf: Number of RBF features
num_posenc: Number of positional encoding features
radius: Distance threshold for edge creation (default: 18.0 Å)
verbose: Whether to print debug information
Returns:
PyTorch Geometric Data object or None if creation fails
"""
try:
# Validate input dimensions
seq_len = len(embeddings)
if len(backbone_atoms) != seq_len or len(rsa_values) != seq_len:
if verbose:
print(f"[WARNING] Dimension mismatch for {pdb_id}_{chain_id}: "
f"embeddings={len(embeddings)}, backbone={len(backbone_atoms)}, "
f"rsa={len(rsa_values)}")
return None
if seq_len == 0:
if verbose:
print(f"[WARNING] Empty protein {pdb_id}_{chain_id}")
return None
# Create node labels (binary epitope classification)
node_labels = np.zeros(seq_len, dtype=np.float32)
if epitope_indices:
# Filter epitope_indices to ensure they are within bounds
valid_epitope_indices = [idx for idx in epitope_indices if 0 <= idx < seq_len]
if valid_epitope_indices:
node_labels[valid_epitope_indices] = 1.0
if verbose and len(valid_epitope_indices) != len(epitope_indices):
print(f"[WARNING] Some epitope indices out of bounds for {pdb_id}_{chain_id}: "
f"filtered {len(epitope_indices)} -> {len(valid_epitope_indices)}")
# Extract CA coordinates for distance calculation
ca_coords = backbone_atoms[:, 1, :] # CA is the second atom [seq_len, 3]
# Validate CA coordinates
if ca_coords.shape[0] == 0:
if verbose:
print(f"[WARNING] Empty CA coordinates for {pdb_id}_{chain_id}")
return None
# Check for NaN or infinite values
if np.any(np.isnan(ca_coords)) or np.any(np.isinf(ca_coords)):
if verbose:
print(f"[WARNING] Invalid CA coordinates (NaN/Inf) for {pdb_id}_{chain_id}")
return None
# Create edges based on distance threshold using radius_graph
ca_coords_tensor = torch.tensor(ca_coords, dtype=torch.float32)
# Additional safety check for tensor
if ca_coords_tensor.numel() == 0:
if verbose:
print(f"[WARNING] Empty CA coordinates tensor for {pdb_id}_{chain_id}")
return None
edge_index = radius_graph(ca_coords_tensor, r=radius, loop=False, max_num_neighbors=32)
if edge_index.shape[1] == 0:
if verbose:
print(f"[WARNING] No edges found for {pdb_id}_{chain_id} with radius {radius}")
# Create a minimal graph with self-loops to avoid empty graph
edge_index = torch.stack([torch.arange(seq_len), torch.arange(seq_len)], dim=0)
# Compute edge features
edge_features = compute_edge_features(ca_coords, edge_index, num_rbf=num_rbf, num_posenc=num_posenc)
# Convert to tensors
x = torch.tensor(embeddings, dtype=torch.float32) # [seq_len, embed_dim]
pos = torch.tensor(backbone_atoms, dtype=torch.float32) # [seq_len, 3, 3]
rsa = torch.tensor(rsa_values, dtype=torch.float32) # [seq_len]
# Node-level labels
y_node = torch.tensor(node_labels, dtype=torch.float32) # [seq_len]
# Additional protein-level statistics
num_epitopes = int(node_labels.sum())
epitope_ratio = num_epitopes / seq_len if seq_len > 0 else 0.0
# Create Data object
data = Data(
x=x, # Node embeddings [seq_len, embed_dim]
pos=pos, # Backbone coordinates [seq_len, 3, 3]
rsa=rsa, # RSA values [seq_len]
edge_index=edge_index, # Edge connectivity [2, n_edges]
edge_attr=edge_features, # Edge features [n_edges, edge_dim]
y_node=y_node, # Node-level labels [seq_len]
epitope_indices=epitope_indices, # Original epitope indices
pdb_id=pdb_id, # PDB ID
chain_id=chain_id, # Chain ID
num_nodes=seq_len, # Number of nodes (residues)
num_epitopes=num_epitopes, # Number of epitope residues
epitope_ratio=epitope_ratio, # Ratio of epitope residues
radius=radius # Distance threshold used for edges
)
if verbose:
print(f"[INFO] Created full protein graph for {pdb_id}_{chain_id}: "
f"{seq_len} nodes, {edge_index.shape[1]} edges, {num_epitopes} epitopes")
return data
except Exception as e:
if verbose:
print(f"[ERROR] Failed to create full protein graph for {pdb_id}_{chain_id}: {str(e)}")
return None
def create_graph_data(
center_idx: int,
covered_indices: List[int],
covered_epitope_indices: List[int],
embeddings: np.ndarray,
backbone_atoms: np.ndarray,
rsa_values: np.ndarray,
epitope_indices: List[int],
recall: float,
precision: float,
pdb_id: str,
chain_id: str,
embeddings2: np.ndarray = None,
num_rbf: int = 16,
num_posenc: int = 16,
verbose: bool = True
) -> Optional[Data]:
"""
Create a PyTorch Geometric Data object for a spherical region.
Args:
center_idx: Index of the center residue
covered_indices: List of residue indices in the region
covered_epitope_indices: List of epitope residue indices in the region
embeddings: Full protein embeddings
backbone_atoms: Full protein backbone atoms [seq_len, 3, 3]
rsa_values: Full protein RSA values
epitope_indices: List of all epitope indices in the protein (if available)
recall: Region recall value (if available)
precision: Region precision value (if available)
pdb_id: PDB ID
chain_id: Chain ID
Returns:
PyTorch Geometric Data object or None if creation fails
"""
try:
# Validate indices first
if not covered_indices:
if verbose:
print(f"[WARNING] Empty covered_indices for center {center_idx}")
return None
# Check if indices are within bounds
max_idx = max(covered_indices)
if max_idx >= len(embeddings) or max_idx >= len(backbone_atoms) or max_idx >= len(rsa_values):
if verbose:
print(f"[WARNING] Index out of bounds: max_idx={max_idx}, "
f"embeddings_len={len(embeddings)}, backbone_len={len(backbone_atoms)}, "
f"rsa_len={len(rsa_values)}")
return None
# Extract node features for covered residues
node_embeddings = embeddings[covered_indices] # [n_nodes, embed_dim]
node_backbone = backbone_atoms[covered_indices] # [n_nodes, 3, 3]
node_rsa = rsa_values[covered_indices] # [n_nodes]
if embeddings2 is not None:
node_embeddings2 = embeddings2[covered_indices] # [n_nodes, embed_dim]
else:
node_embeddings2 = None
# Create node labels (binary epitope classification)
node_labels = np.zeros(len(covered_indices), dtype=np.float32)
# Use the epitope_indices from the loaded data if available
epitope_mask = np.isin(covered_indices, epitope_indices)
node_labels[epitope_mask] = 1.0
# Create fully connected edge index (no self-loops)
n_nodes = len(covered_indices)
edge_index = get_edges(n_nodes)
edge_index = torch.tensor(edge_index, dtype=torch.long)
# Compute edge features using CA coordinates
ca_coords = node_backbone[:, 1, :] # Extract CA coordinates [n_nodes, 3]
edge_features = compute_edge_features(ca_coords, edge_index, num_rbf=num_rbf, num_posenc=num_posenc)
# Convert to tensors
x = torch.tensor(node_embeddings, dtype=torch.float32)
pos = torch.tensor(node_backbone, dtype=torch.float32) # [n_nodes, 3, 3]
rsa = torch.tensor(node_rsa, dtype=torch.float32)
# Graph-level label (recall)
y_graph = torch.tensor([recall], dtype=torch.float32)
# Node-level labels
y_node = torch.tensor(node_labels, dtype=torch.float32)
# Create Data object
data = Data(
x=x, # Node embeddings [n_nodes, embed_dim]
pos=pos, # Backbone coordinates [n_nodes, 3, 3]
rsa=rsa, # RSA values [n_nodes]
edge_index=edge_index, # Edge connectivity [2, n_edges]
edge_attr=edge_features, # Edge features [n_edges, edge_dim]
y=y_graph, # Graph-level label (recall)
y_node=y_node, # Node-level labels [n_nodes]
center_idx=center_idx, # Center residue index
covered_indices=covered_indices, # All covered residue indices
precision=precision, # Region precision
pdb_id=pdb_id, # PDB ID
chain_id=chain_id, # Chain ID
num_nodes=n_nodes, # Number of nodes
embeddings2=node_embeddings2, # other embeddings [n_nodes, embed_dim] - region-specific
)
return data
except Exception as e:
if verbose:
print(f"Error creating graph data for {pdb_id}_{chain_id} center {center_idx}: {str(e)}")
return None
def compute_edge_features(coords: np.ndarray, edge_index: torch.Tensor, num_rbf: int = 16, num_posenc: int = 16) -> torch.Tensor:
"""
Compute edge features including RBF and positional encoding.
Args:
coords: Node coordinates [n_nodes, 3]
edge_index: Edge connectivity [2, n_edges]
num_rbf: Number of RBF features
num_posenc: Number of positional encoding features
Returns:
Edge features [n_edges, edge_dim]
"""
# Convert to torch tensors
coords_tensor = torch.tensor(coords, dtype=torch.float32)
# Compute edge vectors and distances
edge_vectors = coords_tensor[edge_index[0]] - coords_tensor[edge_index[1]] # [n_edges, 3]
edge_distances = torch.norm(edge_vectors, dim=-1) # [n_edges]
# RBF features
edge_rbf = rbf(edge_distances, D_count=num_rbf) # [n_edges, num_rbf]
# Positional encoding
edge_posenc = get_posenc(edge_index, num_posenc=num_posenc) # [n_edges, num_posenc]
# Concatenate edge features
edge_features = torch.cat([edge_rbf, edge_posenc], dim=-1) # [n_edges, num_rbf + num_posenc]
return edge_features
def get_edges(n_nodes):
"""Generate fully connected edge indices (no self-loops)"""
rows, cols = [], []
for i in range(n_nodes):
for j in range(n_nodes):
if i != j:
rows.append(i)
cols.append(j)
return [rows, cols]
def get_posenc(edge_index, num_posenc=16):
"""
Generate positional encoding for edges.
From https://github.com/jingraham/neurips19-graph-protein-design
"""
d = edge_index[0] - edge_index[1]
frequency = torch.exp(
torch.arange(0, num_posenc, 2, dtype=torch.float32, device=d.device)
* -(np.log(10000.0) / num_posenc)
)
angles = d.unsqueeze(-1) * frequency
E = torch.cat((torch.cos(angles), torch.sin(angles)), -1)
return E
def rbf(D, D_min=0., D_max=20., D_count=16):
"""
Radial Basis Function (RBF) encoding for distances.
From https://github.com/jingraham/neurips19-graph-protein-design
Returns an RBF embedding of `torch.Tensor` `D` along a new axis=-1.
That is, if `D` has shape [...dims], then the returned tensor will have
shape [...dims, D_count].
"""
D_mu = torch.linspace(D_min, D_max, D_count, device=D.device)
D_mu = D_mu.view([1, -1])
D_sigma = (D_max - D_min) / D_count
D_expand = torch.unsqueeze(D, -1)
RBF = torch.exp(-((D_expand - D_mu) / D_sigma) ** 2)
return RBF |