Spaces:
Sleeping
Sleeping
fix: python_version, requirements, academic UI redesign
Browse files- README.md +2 -2
- app.py +764 -552
- requirements.txt +15 -9
- src/__pycache__/__init__.cpython-310.pyc +0 -0
- src/data/__pycache__/__init__.cpython-310.pyc +0 -0
- src/data/__pycache__/mol_graph.cpython-310.pyc +0 -0
- src/data/__pycache__/protein_feat.cpython-310.pyc +0 -0
- src/models/__pycache__/__init__.cpython-310.pyc +0 -0
- src/models/__pycache__/biointeract.cpython-310.pyc +0 -0
- src/models/__pycache__/drug_encoder.cpython-310.pyc +0 -0
- src/models/__pycache__/interaction.cpython-310.pyc +0 -0
- src/models/__pycache__/target_encoder.cpython-310.pyc +0 -0
README.md
CHANGED
|
@@ -4,8 +4,8 @@ emoji: π§¬
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
python_version: 3.
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
license: mit
|
|
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.0.0
|
| 8 |
+
python_version: "3.11"
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
license: mit
|
app.py
CHANGED
|
@@ -1,552 +1,764 @@
|
|
| 1 |
-
"""
|
| 2 |
-
app.py β BioInteract Gradio Space
|
| 3 |
-
Interpretable DrugβTarget Interaction Prediction
|
| 4 |
-
|
| 5 |
-
Two tabs:
|
| 6 |
-
1. Case Studies β pre-computed, clinically validated pairs
|
| 7 |
-
2. Custom Prediction β user supplies SMILES + protein sequence
|
| 8 |
-
"""
|
| 9 |
-
import sys
|
| 10 |
-
import json
|
| 11 |
-
import io
|
| 12 |
-
import warnings
|
| 13 |
-
from pathlib import Path
|
| 14 |
-
|
| 15 |
-
import numpy as np
|
| 16 |
-
import
|
| 17 |
-
import
|
| 18 |
-
matplotlib
|
| 19 |
-
|
| 20 |
-
import
|
| 21 |
-
import
|
| 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 |
-
with open(_REPORT_PATH) as f:
|
| 80 |
-
_REPORT = json.load(f)
|
| 81 |
-
|
| 82 |
-
# ============================================================
|
| 83 |
-
# ESM-2 lazy loader
|
| 84 |
-
# ============================================================
|
| 85 |
-
|
| 86 |
-
_esm_tokenizer = None
|
| 87 |
-
_esm_model = None
|
| 88 |
-
ESM_MODEL_NAME = "facebook/esm2_t30_150M_UR50D"
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
seq = sequence[:max_len]
|
| 105 |
-
tokenizer, esm = _get_esm()
|
| 106 |
-
inputs = tokenizer(seq, return_tensors='pt', add_special_tokens=True)
|
| 107 |
-
with torch.no_grad():
|
| 108 |
-
outputs = esm(**inputs)
|
| 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 |
-
fig,
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
fig
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
ax.
|
| 200 |
-
ax.
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
buf =
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
buf.
|
| 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 |
-
'png': ROOT / 'examples' / '
|
| 241 |
-
'prob':
|
| 242 |
-
'affinity_nM':
|
| 243 |
-
'top_residues': [],
|
| 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 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
f"
|
| 393 |
-
f"
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
<
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
</
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
app.py β BioInteract Gradio Space
|
| 3 |
+
Interpretable DrugβTarget Interaction Prediction
|
| 4 |
+
|
| 5 |
+
Two tabs:
|
| 6 |
+
1. Case Studies β pre-computed, clinically validated pairs
|
| 7 |
+
2. Custom Prediction β user supplies SMILES + protein sequence
|
| 8 |
+
"""
|
| 9 |
+
import sys
|
| 10 |
+
import json
|
| 11 |
+
import io
|
| 12 |
+
import warnings
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
|
| 15 |
+
import numpy as np
|
| 16 |
+
import torch
|
| 17 |
+
import yaml
|
| 18 |
+
import matplotlib
|
| 19 |
+
matplotlib.use('Agg')
|
| 20 |
+
import matplotlib as mpl
|
| 21 |
+
import matplotlib.pyplot as plt
|
| 22 |
+
import seaborn as sns
|
| 23 |
+
import gradio as gr
|
| 24 |
+
from PIL import Image
|
| 25 |
+
|
| 26 |
+
# ---------- path setup ----------
|
| 27 |
+
ROOT = Path(__file__).parent
|
| 28 |
+
sys.path.insert(0, str(ROOT))
|
| 29 |
+
|
| 30 |
+
from src.models.biointeract import BioInteract
|
| 31 |
+
from src.data.mol_graph import smiles_to_graph
|
| 32 |
+
from src.data.protein_feat import residue_physicochemical_features, residue_domain_labels
|
| 33 |
+
|
| 34 |
+
# ---------- publication-style matplotlib defaults ----------
|
| 35 |
+
mpl.rcParams.update({
|
| 36 |
+
'font.family': 'DejaVu Serif',
|
| 37 |
+
'font.size': 10,
|
| 38 |
+
'axes.titlesize': 11,
|
| 39 |
+
'axes.titleweight': 'bold',
|
| 40 |
+
'axes.labelsize': 10,
|
| 41 |
+
'axes.labelcolor': '#1a1a2e',
|
| 42 |
+
'axes.edgecolor': '#444',
|
| 43 |
+
'axes.linewidth': 0.8,
|
| 44 |
+
'axes.spines.top': False,
|
| 45 |
+
'axes.spines.right': False,
|
| 46 |
+
'xtick.direction': 'out',
|
| 47 |
+
'ytick.direction': 'out',
|
| 48 |
+
'xtick.color': '#444',
|
| 49 |
+
'ytick.color': '#444',
|
| 50 |
+
'figure.facecolor': 'white',
|
| 51 |
+
'axes.facecolor': '#fafafa',
|
| 52 |
+
'grid.color': '#e0e0e0',
|
| 53 |
+
'grid.linewidth': 0.5,
|
| 54 |
+
'savefig.facecolor': 'white',
|
| 55 |
+
'savefig.dpi': 150,
|
| 56 |
+
})
|
| 57 |
+
|
| 58 |
+
# ============================================================
|
| 59 |
+
# Global model loading
|
| 60 |
+
# ============================================================
|
| 61 |
+
|
| 62 |
+
DEVICE = torch.device('cpu')
|
| 63 |
+
_CONFIG_PATH = ROOT / 'configs' / 'default.yaml'
|
| 64 |
+
_CKPT_PATH = ROOT / 'checkpoints' / 'best.pt'
|
| 65 |
+
_REPORT_PATH = ROOT / 'examples' / 'interpretability_report.json'
|
| 66 |
+
|
| 67 |
+
print("[BioInteract] Loading model config β¦")
|
| 68 |
+
with open(_CONFIG_PATH) as f:
|
| 69 |
+
_CONFIG = yaml.safe_load(f)
|
| 70 |
+
|
| 71 |
+
print("[BioInteract] Loading pretrained weights β¦")
|
| 72 |
+
_model = BioInteract(_CONFIG['model']).to(DEVICE)
|
| 73 |
+
_ckpt = torch.load(_CKPT_PATH, map_location='cpu', weights_only=False)
|
| 74 |
+
_model.load_state_dict(_ckpt['model_state_dict'])
|
| 75 |
+
_model.eval()
|
| 76 |
+
print(f"[BioInteract] Model ready β epoch {_ckpt.get('epoch','?')}, "
|
| 77 |
+
f"params = {sum(p.numel() for p in _model.parameters()):,}")
|
| 78 |
+
|
| 79 |
+
with open(_REPORT_PATH) as f:
|
| 80 |
+
_REPORT = json.load(f)
|
| 81 |
+
|
| 82 |
+
# ============================================================
|
| 83 |
+
# ESM-2 lazy loader
|
| 84 |
+
# ============================================================
|
| 85 |
+
|
| 86 |
+
_esm_tokenizer = None
|
| 87 |
+
_esm_model = None
|
| 88 |
+
ESM_MODEL_NAME = "facebook/esm2_t30_150M_UR50D"
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def _get_esm():
|
| 92 |
+
global _esm_tokenizer, _esm_model
|
| 93 |
+
if _esm_model is None:
|
| 94 |
+
from transformers import EsmModel, EsmTokenizer
|
| 95 |
+
print("[BioInteract] Downloading / loading ESM-2 (150M) β¦")
|
| 96 |
+
_esm_tokenizer = EsmTokenizer.from_pretrained(ESM_MODEL_NAME)
|
| 97 |
+
_esm_model = EsmModel.from_pretrained(ESM_MODEL_NAME).eval()
|
| 98 |
+
print("[BioInteract] ESM-2 ready")
|
| 99 |
+
return _esm_tokenizer, _esm_model
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def compute_esm2_embedding(sequence: str, max_len: int = 512) -> torch.Tensor:
|
| 103 |
+
"""Run ESM-2 and return per-residue embeddings (L, 640)."""
|
| 104 |
+
seq = sequence[:max_len]
|
| 105 |
+
tokenizer, esm = _get_esm()
|
| 106 |
+
inputs = tokenizer(seq, return_tensors='pt', add_special_tokens=True)
|
| 107 |
+
with torch.no_grad():
|
| 108 |
+
outputs = esm(**inputs)
|
| 109 |
+
embedding = outputs.last_hidden_state[0, 1:-1, :]
|
| 110 |
+
return embedding[:len(seq)]
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# ============================================================
|
| 114 |
+
# Publication-quality plotting helpers
|
| 115 |
+
# ============================================================
|
| 116 |
+
|
| 117 |
+
_HEATMAP_CMAP = 'Blues'
|
| 118 |
+
_BAR_COLOR = '#1a4a7a'
|
| 119 |
+
_BAR_ACCENT = '#2e7cbf'
|
| 120 |
+
_FG_COLOR = '#8b2635'
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def _plot_interaction_heatmap(
|
| 124 |
+
interaction_map: np.ndarray,
|
| 125 |
+
sequence: str,
|
| 126 |
+
title: str = 'AtomβResidue Interaction Map',
|
| 127 |
+
) -> Image.Image:
|
| 128 |
+
"""Render interaction heatmap with publication-quality styling."""
|
| 129 |
+
n_atoms, n_res = interaction_map.shape
|
| 130 |
+
|
| 131 |
+
max_show_res = 80
|
| 132 |
+
if n_res > max_show_res:
|
| 133 |
+
scores = interaction_map.sum(axis=0)
|
| 134 |
+
center = int(np.argmax(scores))
|
| 135 |
+
start = max(0, center - max_show_res // 2)
|
| 136 |
+
end = min(n_res, start + max_show_res)
|
| 137 |
+
interaction_map = interaction_map[:, start:end]
|
| 138 |
+
res_labels = [f"{sequence[i]}{i+1}" if i < len(sequence) else str(i+1)
|
| 139 |
+
for i in range(start, end)]
|
| 140 |
+
else:
|
| 141 |
+
res_labels = [f"{sequence[i]}{i+1}" if i < len(sequence) else str(i+1)
|
| 142 |
+
for i in range(n_res)]
|
| 143 |
+
|
| 144 |
+
figw = max(13, len(res_labels) * 0.16)
|
| 145 |
+
figh = max(5, n_atoms * 0.28)
|
| 146 |
+
fig, ax = plt.subplots(figsize=(figw, figh))
|
| 147 |
+
|
| 148 |
+
sns.heatmap(
|
| 149 |
+
interaction_map,
|
| 150 |
+
xticklabels=res_labels,
|
| 151 |
+
yticklabels=[f"a{i+1}" for i in range(n_atoms)],
|
| 152 |
+
cmap=_HEATMAP_CMAP,
|
| 153 |
+
ax=ax,
|
| 154 |
+
linewidths=0,
|
| 155 |
+
cbar_kws={'label': 'Normalised Attention Score', 'shrink': 0.75,
|
| 156 |
+
'aspect': 20},
|
| 157 |
+
)
|
| 158 |
+
ax.set_title(title, pad=10)
|
| 159 |
+
ax.set_xlabel('Protein Residue', labelpad=6)
|
| 160 |
+
ax.set_ylabel('Drug Atom', labelpad=6)
|
| 161 |
+
plt.xticks(rotation=90, fontsize=5.5)
|
| 162 |
+
plt.yticks(fontsize=6, rotation=0)
|
| 163 |
+
|
| 164 |
+
for spine in ax.spines.values():
|
| 165 |
+
spine.set_visible(False)
|
| 166 |
+
|
| 167 |
+
fig.text(0.02, 0.01,
|
| 168 |
+
'Colour intensity encodes normalised cross-attention weight',
|
| 169 |
+
fontsize=7, color='#666', style='italic')
|
| 170 |
+
plt.tight_layout(rect=[0, 0.03, 1, 1])
|
| 171 |
+
|
| 172 |
+
buf = io.BytesIO()
|
| 173 |
+
fig.savefig(buf, format='png', bbox_inches='tight')
|
| 174 |
+
plt.close(fig)
|
| 175 |
+
buf.seek(0)
|
| 176 |
+
return Image.open(buf).copy()
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def _plot_top_residues(top_residues: list, title: str = 'Top Binding Residues') -> Image.Image:
|
| 180 |
+
"""Horizontal bar chart with academic styling."""
|
| 181 |
+
labels = [r[0] for r in top_residues]
|
| 182 |
+
scores = [r[1] for r in top_residues]
|
| 183 |
+
|
| 184 |
+
fig, ax = plt.subplots(figsize=(8, 4.2))
|
| 185 |
+
colors = [_BAR_COLOR if s >= 0.5 else _BAR_ACCENT for s in scores[::-1]]
|
| 186 |
+
bars = ax.barh(labels[::-1], scores[::-1],
|
| 187 |
+
color=colors, edgecolor='none', height=0.65)
|
| 188 |
+
|
| 189 |
+
ax.set_xlabel('Normalised Attention Score', labelpad=6)
|
| 190 |
+
ax.set_title(title, pad=8)
|
| 191 |
+
ax.set_xlim(0, 1.12)
|
| 192 |
+
ax.axvline(0.5, color='#aaa', linewidth=0.8, linestyle='--', alpha=0.7)
|
| 193 |
+
ax.text(0.51, -0.6, 'threshold', fontsize=7, color='#888', style='italic')
|
| 194 |
+
|
| 195 |
+
for bar, score in zip(bars, scores[::-1]):
|
| 196 |
+
ax.text(score + 0.015, bar.get_y() + bar.get_height() / 2,
|
| 197 |
+
f'{score:.3f}', va='center', fontsize=8, color='#222')
|
| 198 |
+
|
| 199 |
+
ax.set_axisbelow(True)
|
| 200 |
+
ax.yaxis.set_tick_params(labelsize=9)
|
| 201 |
+
plt.tight_layout()
|
| 202 |
+
|
| 203 |
+
buf = io.BytesIO()
|
| 204 |
+
fig.savefig(buf, format='png', bbox_inches='tight')
|
| 205 |
+
plt.close(fig)
|
| 206 |
+
buf.seek(0)
|
| 207 |
+
return Image.open(buf).copy()
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def _plot_functional_groups(fg_dict: dict, title: str = 'Pharmacophore Importance') -> Image.Image:
|
| 211 |
+
"""Horizontal bar chart for functional group Grad-CAM scores."""
|
| 212 |
+
if not fg_dict:
|
| 213 |
+
return None
|
| 214 |
+
labels = list(fg_dict.keys())
|
| 215 |
+
scores = list(fg_dict.values())
|
| 216 |
+
|
| 217 |
+
fig, ax = plt.subplots(figsize=(7, max(3, len(labels) * 0.55)))
|
| 218 |
+
ax.barh(labels, scores, color=_FG_COLOR, edgecolor='none', height=0.6, alpha=0.85)
|
| 219 |
+
ax.set_xlabel('Grad-CAM Importance Score', labelpad=6)
|
| 220 |
+
ax.set_title(title, pad=8)
|
| 221 |
+
ax.set_xlim(0, 1.12)
|
| 222 |
+
for i, (label, score) in enumerate(zip(labels, scores)):
|
| 223 |
+
ax.text(score + 0.015, i, f'{score:.3f}', va='center', fontsize=8, color='#222')
|
| 224 |
+
ax.set_axisbelow(True)
|
| 225 |
+
plt.tight_layout()
|
| 226 |
+
|
| 227 |
+
buf = io.BytesIO()
|
| 228 |
+
fig.savefig(buf, format='png', bbox_inches='tight')
|
| 229 |
+
plt.close(fig)
|
| 230 |
+
buf.seek(0)
|
| 231 |
+
return Image.open(buf).copy()
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
# ============================================================
|
| 235 |
+
# Tab 1 β Case Studies
|
| 236 |
+
# ============================================================
|
| 237 |
+
|
| 238 |
+
_FIXED_CASES = {
|
| 239 |
+
'ABL1(E255K) + Drug 5328940 (Kd = 0.047 nM)': {
|
| 240 |
+
'png': ROOT / 'examples' / '5328940_ABL1E255K.png',
|
| 241 |
+
'prob': 0.988,
|
| 242 |
+
'affinity_nM': 0.047,
|
| 243 |
+
'top_residues': [['V104', 1.0], ['A648', 0.503], ['S199', 0.417],
|
| 244 |
+
['P649', 0.281], ['P936', 0.237], ['N707', 0.192],
|
| 245 |
+
['P651', 0.180], ['L799', 0.166], ['K796', 0.141], ['P934', 0.079]],
|
| 246 |
+
'functional_groups': {'Halogen': 0.650, 'Amino': 0.616, 'Ether': 0.345,
|
| 247 |
+
'Aromatic Ring': 0.311, 'Heterocycle N': 0.167},
|
| 248 |
+
'description': (
|
| 249 |
+
'Drug 5328940 binds the ABL1 E255K resistance mutant with extremely high '
|
| 250 |
+
'affinity (Kd = 0.047 nM). The model predicts binding with 98.8% probability. '
|
| 251 |
+
'Key contacts include V104 (gatekeeper residue), A648, and S199, consistent '
|
| 252 |
+
'with known structural data for Type II kinase inhibitors.'
|
| 253 |
+
),
|
| 254 |
+
},
|
| 255 |
+
'EGFR + Drug 156414': {
|
| 256 |
+
'png': ROOT / 'examples' / '156414_EGFR.png',
|
| 257 |
+
'prob': None,
|
| 258 |
+
'affinity_nM': None,
|
| 259 |
+
'top_residues': [],
|
| 260 |
+
'functional_groups': {},
|
| 261 |
+
'description': (
|
| 262 |
+
'Drug 156414 targets the wild-type EGFR kinase domain. '
|
| 263 |
+
'EGFR inhibitors are first-line treatments for non-small cell lung cancer '
|
| 264 |
+
'with activating mutations. The interaction map highlights the ATP-binding cleft.'
|
| 265 |
+
),
|
| 266 |
+
},
|
| 267 |
+
'BRAF + Drug 11717001 (Sorafenib analogue)': {
|
| 268 |
+
'png': ROOT / 'examples' / '11717001_BRAF.png',
|
| 269 |
+
'prob': None,
|
| 270 |
+
'affinity_nM': None,
|
| 271 |
+
'top_residues': [],
|
| 272 |
+
'functional_groups': {},
|
| 273 |
+
'description': (
|
| 274 |
+
'Drug 11717001 is a Sorafenib analogue targeting BRAF kinase, a driver '
|
| 275 |
+
'oncogene in ~50% of cutaneous melanomas (V600E mutation). RAF inhibitors '
|
| 276 |
+
'block the MAPK/ERK signalling cascade that promotes uncontrolled proliferation.'
|
| 277 |
+
),
|
| 278 |
+
},
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
def show_case_study(case_key: str):
|
| 283 |
+
"""Called when user selects a case from the dropdown."""
|
| 284 |
+
case = _FIXED_CASES.get(case_key)
|
| 285 |
+
if case is None:
|
| 286 |
+
return None, None, None, "Case not found."
|
| 287 |
+
|
| 288 |
+
heatmap_img = Image.open(case['png']) if case['png'].exists() else None
|
| 289 |
+
|
| 290 |
+
prob_text = (f"**Predicted binding probability:** {case['prob']*100:.1f}%\n\n"
|
| 291 |
+
if case['prob'] else "")
|
| 292 |
+
kd_text = (f"**Experimental affinity (Kd):** {case['affinity_nM']} nM\n\n"
|
| 293 |
+
if case['affinity_nM'] else "")
|
| 294 |
+
info_md = (
|
| 295 |
+
f"#### {case_key.strip()}\n\n"
|
| 296 |
+
f"{prob_text}{kd_text}"
|
| 297 |
+
f"**Clinical context:** {case['description']}"
|
| 298 |
+
)
|
| 299 |
+
|
| 300 |
+
residue_img = (
|
| 301 |
+
_plot_top_residues(case['top_residues'],
|
| 302 |
+
f'Top Binding Residues β {case_key.split("+")[0].strip()}')
|
| 303 |
+
if case['top_residues'] else None
|
| 304 |
+
)
|
| 305 |
+
fg_img = (
|
| 306 |
+
_plot_functional_groups(case['functional_groups'],
|
| 307 |
+
f'Pharmacophore Importance β {case_key.split("+")[0].strip()}')
|
| 308 |
+
if case['functional_groups'] else None
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
+
return heatmap_img, residue_img, fg_img, info_md
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
# ============================================================
|
| 315 |
+
# Tab 2 β Custom Prediction
|
| 316 |
+
# ============================================================
|
| 317 |
+
|
| 318 |
+
_EXAMPLE_SMILES = (
|
| 319 |
+
"Cc1ccc(NC(=O)c2ccc(CN3CCN(C)CC3)cc2)cc1Nc1nccc(-c2cccnc2)n1"
|
| 320 |
+
)
|
| 321 |
+
_EXAMPLE_SEQUENCE = (
|
| 322 |
+
"MGPSENDPNLFVALYDFVASGDNTLSITKGEKLRVLGYNHNGEWCEAQTKNGQGWVPSNYITPVNSLEKHSWYHGPVSRNAAEYLLSSGINGSFLVRESESSPGQRSISLRYEGRVYHYRINTASDGKLYVSSESRFNTLAELVHHHSTLVQHSDSVESAYRSKL"
|
| 323 |
+
"LNSGVYHYRINTASDGKLYVSSESRFNTLAELVHHHSTLVQ"
|
| 324 |
+
)
|
| 325 |
+
|
| 326 |
+
MAX_SEQ_LEN = 512
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
def run_prediction(smiles: str, sequence: str, progress=gr.Progress()):
|
| 330 |
+
"""
|
| 331 |
+
Full inference pipeline: SMILES + sequence β probability + interaction heatmap.
|
| 332 |
+
Returns: (status_msg, prob_text, heatmap_image, residue_image)
|
| 333 |
+
"""
|
| 334 |
+
smiles = (smiles or '').strip()
|
| 335 |
+
sequence = (sequence or '').strip().upper()
|
| 336 |
+
|
| 337 |
+
if not smiles:
|
| 338 |
+
return "Input required: please provide a SMILES string.", "", None, None
|
| 339 |
+
if not sequence:
|
| 340 |
+
return "Input required: please provide an amino acid sequence.", "", None, None
|
| 341 |
+
|
| 342 |
+
progress(0.1, desc="Parsing SMILES string via RDKit β¦")
|
| 343 |
+
drug_graph = smiles_to_graph(smiles)
|
| 344 |
+
if drug_graph is None:
|
| 345 |
+
return "Parse error: RDKit could not interpret the SMILES string.", "", None, None
|
| 346 |
+
|
| 347 |
+
from torch_geometric.data import Batch
|
| 348 |
+
drug_batch = Batch.from_data_list([drug_graph]).to(DEVICE)
|
| 349 |
+
|
| 350 |
+
seq = sequence[:MAX_SEQ_LEN]
|
| 351 |
+
if len(sequence) > MAX_SEQ_LEN:
|
| 352 |
+
warnings.warn(f"Sequence truncated to {MAX_SEQ_LEN} residues.")
|
| 353 |
+
L = len(seq)
|
| 354 |
+
|
| 355 |
+
progress(0.2, desc="Computing ESM-2 residue embeddings (first run: ~2 min) β¦")
|
| 356 |
+
try:
|
| 357 |
+
esm2_emb = compute_esm2_embedding(seq)
|
| 358 |
+
except Exception as e:
|
| 359 |
+
return f"ESM-2 error: {e}", "", None, None
|
| 360 |
+
|
| 361 |
+
esm2_emb = esm2_emb.unsqueeze(0).to(DEVICE)
|
| 362 |
+
physchem = residue_physicochemical_features(seq).unsqueeze(0).to(DEVICE)
|
| 363 |
+
domain = residue_domain_labels(L).unsqueeze(0).to(DEVICE)
|
| 364 |
+
prot_mask = torch.ones(1, L, dtype=torch.bool, device=DEVICE)
|
| 365 |
+
|
| 366 |
+
progress(0.85, desc="Running BioInteract cross-attention inference β¦")
|
| 367 |
+
with torch.no_grad():
|
| 368 |
+
logit, attn_data = _model(
|
| 369 |
+
drug_batch, esm2_emb, physchem, domain, prot_mask,
|
| 370 |
+
return_attention=True
|
| 371 |
+
)
|
| 372 |
+
|
| 373 |
+
prob = torch.sigmoid(logit).item()
|
| 374 |
+
interaction_map = attn_data['interaction_map'][0].cpu().numpy()
|
| 375 |
+
drug_mask_np = attn_data['drug_mask'][0].cpu().numpy()
|
| 376 |
+
|
| 377 |
+
n_real_atoms = int(drug_mask_np.sum())
|
| 378 |
+
imap = interaction_map[:n_real_atoms, :L]
|
| 379 |
+
|
| 380 |
+
residue_scores = imap.sum(axis=0)
|
| 381 |
+
residue_scores = residue_scores / (residue_scores.max() + 1e-9)
|
| 382 |
+
top_idx = np.argsort(residue_scores)[::-1][:10]
|
| 383 |
+
top_residues = [[f"{seq[i]}{i+1}", float(residue_scores[i])] for i in top_idx]
|
| 384 |
+
|
| 385 |
+
progress(0.95, desc="Generating publication-quality figures β¦")
|
| 386 |
+
heatmap_img = _plot_interaction_heatmap(imap, seq, title='AtomβResidue Cross-Attention Map')
|
| 387 |
+
residue_img = _plot_top_residues(top_residues, 'Top 10 Predicted Binding Residues')
|
| 388 |
+
|
| 389 |
+
label = "**BINDING**" if prob > 0.5 else "**NON-BINDING**"
|
| 390 |
+
conf = "High confidence" if abs(prob - 0.5) > 0.3 else "Moderate confidence"
|
| 391 |
+
prob_text = (
|
| 392 |
+
f"### Prediction Result: {label}\n\n"
|
| 393 |
+
f"| Metric | Value |\n"
|
| 394 |
+
f"|--------|-------|\n"
|
| 395 |
+
f"| Binding probability | **{prob * 100:.1f}%** |\n"
|
| 396 |
+
f"| Confidence | {conf} |\n"
|
| 397 |
+
f"| Drug atoms analysed | {n_real_atoms} |\n"
|
| 398 |
+
f"| Protein residues analysed | {L} |\n"
|
| 399 |
+
)
|
| 400 |
+
|
| 401 |
+
return "Inference complete.", prob_text, heatmap_img, residue_img
|
| 402 |
+
|
| 403 |
+
|
| 404 |
+
# ============================================================
|
| 405 |
+
# Global statistics panel
|
| 406 |
+
# ============================================================
|
| 407 |
+
|
| 408 |
+
_GLOBAL_STATS = _REPORT.get('global_stats', {})
|
| 409 |
+
|
| 410 |
+
_SIDEBAR_HTML = f"""
|
| 411 |
+
<div style="background:#f8f9fa; border:1px solid #dee2e6; border-radius:6px; padding:16px; font-family:'Georgia',serif;">
|
| 412 |
+
|
| 413 |
+
<div style="border-bottom:2px solid #1a4a7a; margin-bottom:12px; padding-bottom:6px;">
|
| 414 |
+
<strong style="color:#1a4a7a; font-size:0.9rem; text-transform:uppercase; letter-spacing:0.04em;">
|
| 415 |
+
Performance Metrics
|
| 416 |
+
</strong><br>
|
| 417 |
+
<span style="color:#666; font-size:0.78rem;">Davis Kinase Benchmark Dataset</span>
|
| 418 |
+
</div>
|
| 419 |
+
|
| 420 |
+
<table style="width:100%; border-collapse:collapse; font-size:0.82rem; margin-bottom:14px;">
|
| 421 |
+
<thead>
|
| 422 |
+
<tr style="background:#1a4a7a; color:white;">
|
| 423 |
+
<th style="padding:6px 8px; text-align:left; font-weight:600;">Split</th>
|
| 424 |
+
<th style="padding:6px 8px; text-align:center; font-weight:600;">AUROC</th>
|
| 425 |
+
<th style="padding:6px 8px; text-align:center; font-weight:600;">AUPRC</th>
|
| 426 |
+
</tr>
|
| 427 |
+
</thead>
|
| 428 |
+
<tbody>
|
| 429 |
+
<tr style="background:#eef2f7;">
|
| 430 |
+
<td style="padding:5px 8px;">Random</td>
|
| 431 |
+
<td style="padding:5px 8px; text-align:center;">0.921</td>
|
| 432 |
+
<td style="padding:5px 8px; text-align:center;">0.608</td>
|
| 433 |
+
</tr>
|
| 434 |
+
<tr>
|
| 435 |
+
<td style="padding:5px 8px;">Cold-Drug</td>
|
| 436 |
+
<td style="padding:5px 8px; text-align:center;">0.739</td>
|
| 437 |
+
<td style="padding:5px 8px; text-align:center;">0.169</td>
|
| 438 |
+
</tr>
|
| 439 |
+
<tr style="background:#eef2f7;">
|
| 440 |
+
<td style="padding:5px 8px;"><strong>Cold-Target</strong></td>
|
| 441 |
+
<td style="padding:5px 8px; text-align:center;"><strong>0.941</strong></td>
|
| 442 |
+
<td style="padding:5px 8px; text-align:center;"><strong>0.549</strong></td>
|
| 443 |
+
</tr>
|
| 444 |
+
</tbody>
|
| 445 |
+
</table>
|
| 446 |
+
|
| 447 |
+
<div style="border-bottom:1px solid #dee2e6; margin-bottom:10px; padding-bottom:4px;">
|
| 448 |
+
<strong style="color:#1a4a7a; font-size:0.85rem;">Model Specifications</strong>
|
| 449 |
+
</div>
|
| 450 |
+
<table style="width:100%; border-collapse:collapse; font-size:0.81rem; margin-bottom:14px;">
|
| 451 |
+
<tr><td style="padding:4px 0; color:#555;">Parameters</td>
|
| 452 |
+
<td style="padding:4px 0; text-align:right;">{_REPORT.get('model_info', {}).get('params', 2_442_083):,}</td></tr>
|
| 453 |
+
<tr><td style="padding:4px 0; color:#555;">Training samples</td>
|
| 454 |
+
<td style="padding:4px 0; text-align:right;">{_GLOBAL_STATS.get('n_samples', 1506):,}</td></tr>
|
| 455 |
+
<tr><td style="padding:4px 0; color:#555;">Attention sparsity</td>
|
| 456 |
+
<td style="padding:4px 0; text-align:right;">{_GLOBAL_STATS.get('attention_sparsity', 0.992)*100:.1f}%</td></tr>
|
| 457 |
+
<tr><td style="padding:4px 0; color:#555;">GNN layers</td>
|
| 458 |
+
<td style="padding:4px 0; text-align:right;">3 Γ GINE</td></tr>
|
| 459 |
+
<tr><td style="padding:4px 0; color:#555;">Attention heads</td>
|
| 460 |
+
<td style="padding:4px 0; text-align:right;">8</td></tr>
|
| 461 |
+
<tr><td style="padding:4px 0; color:#555;">Hidden dimension</td>
|
| 462 |
+
<td style="padding:4px 0; text-align:right;">256</td></tr>
|
| 463 |
+
</table>
|
| 464 |
+
|
| 465 |
+
<div style="border-bottom:1px solid #dee2e6; margin-bottom:10px; padding-bottom:4px;">
|
| 466 |
+
<strong style="color:#1a4a7a; font-size:0.85rem;">Architecture Overview</strong>
|
| 467 |
+
</div>
|
| 468 |
+
<pre style="background:#1a2a3a; color:#c8d8e8; padding:10px; border-radius:4px; font-size:0.72rem; line-height:1.5; margin:0; overflow:auto;">
|
| 469 |
+
Drug SMILES
|
| 470 |
+
β GINE (3 layers, dim=256)
|
| 471 |
+
β N Γ atom vectors
|
| 472 |
+
|
| 473 |
+
Protein sequence
|
| 474 |
+
β ESM-2 (150M params)
|
| 475 |
+
β physicochemical (4-dim)
|
| 476 |
+
β L Γ residue vectors
|
| 477 |
+
|
| 478 |
+
Bidirectional cross-attention
|
| 479 |
+
β NΓL interaction map
|
| 480 |
+
β gated pooling
|
| 481 |
+
β binding score</pre>
|
| 482 |
+
|
| 483 |
+
<div style="margin-top:14px; padding:10px; background:#fff8e1; border-left:3px solid #f9a825; border-radius:0 4px 4px 0; font-size:0.8rem; color:#555; line-height:1.5;">
|
| 484 |
+
<strong style="color:#e65100;">Reference</strong><br>
|
| 485 |
+
Wang S, Zhang Q <em>et al.</em> BioInteract: Interpretable DTI Prediction via
|
| 486 |
+
Residue-Level Cross-Attention with Biological Prior Knowledge.
|
| 487 |
+
<em>PLOS Computational Biology</em>, 2026.
|
| 488 |
+
</div>
|
| 489 |
+
</div>
|
| 490 |
+
"""
|
| 491 |
+
|
| 492 |
+
# ============================================================
|
| 493 |
+
# Custom CSS
|
| 494 |
+
# ============================================================
|
| 495 |
+
|
| 496 |
+
_CSS = """
|
| 497 |
+
.gradio-container {
|
| 498 |
+
font-family: 'Georgia', 'Times New Roman', serif !important;
|
| 499 |
+
max-width: 1440px !important;
|
| 500 |
+
}
|
| 501 |
+
.tab-nav {
|
| 502 |
+
border-bottom: 2px solid #1a4a7a !important;
|
| 503 |
+
}
|
| 504 |
+
.tab-nav button {
|
| 505 |
+
font-size: 0.82rem !important;
|
| 506 |
+
font-weight: 600 !important;
|
| 507 |
+
letter-spacing: 0.03em !important;
|
| 508 |
+
text-transform: uppercase !important;
|
| 509 |
+
color: #555 !important;
|
| 510 |
+
}
|
| 511 |
+
.tab-nav button.selected {
|
| 512 |
+
color: #1a4a7a !important;
|
| 513 |
+
border-bottom: 2px solid #1a4a7a !important;
|
| 514 |
+
}
|
| 515 |
+
label span {
|
| 516 |
+
font-size: 0.82rem !important;
|
| 517 |
+
font-weight: 600 !important;
|
| 518 |
+
color: #1a2a3a !important;
|
| 519 |
+
text-transform: uppercase !important;
|
| 520 |
+
letter-spacing: 0.03em !important;
|
| 521 |
+
}
|
| 522 |
+
.gr-button-primary {
|
| 523 |
+
background: #1a4a7a !important;
|
| 524 |
+
border-color: #1a4a7a !important;
|
| 525 |
+
}
|
| 526 |
+
.gr-button-primary:hover {
|
| 527 |
+
background: #0d2137 !important;
|
| 528 |
+
}
|
| 529 |
+
footer { display: none !important; }
|
| 530 |
+
"""
|
| 531 |
+
|
| 532 |
+
# ============================================================
|
| 533 |
+
# Build Gradio UI
|
| 534 |
+
# ============================================================
|
| 535 |
+
|
| 536 |
+
_HEADER_HTML = """
|
| 537 |
+
<div style="
|
| 538 |
+
background: linear-gradient(135deg, #0d2137 0%, #1a4a7a 100%);
|
| 539 |
+
padding: 24px 32px 20px;
|
| 540 |
+
border-radius: 8px;
|
| 541 |
+
margin-bottom: 4px;
|
| 542 |
+
">
|
| 543 |
+
<h1 style="
|
| 544 |
+
color: #ffffff;
|
| 545 |
+
margin: 0 0 6px;
|
| 546 |
+
font-size: 1.65rem;
|
| 547 |
+
font-weight: 700;
|
| 548 |
+
font-family: 'Georgia', serif;
|
| 549 |
+
letter-spacing: -0.3px;
|
| 550 |
+
">BioInteract</h1>
|
| 551 |
+
<p style="
|
| 552 |
+
color: #b0cee8;
|
| 553 |
+
margin: 0 0 14px;
|
| 554 |
+
font-size: 0.95rem;
|
| 555 |
+
font-style: italic;
|
| 556 |
+
font-family: 'Georgia', serif;
|
| 557 |
+
line-height: 1.4;
|
| 558 |
+
">
|
| 559 |
+
Interpretable DrugβTarget Interaction Prediction via
|
| 560 |
+
Residue-Level Cross-Attention with Biological Prior Knowledge
|
| 561 |
+
</p>
|
| 562 |
+
<div style="display: flex; gap: 8px; flex-wrap: wrap;">
|
| 563 |
+
<span style="background:rgba(255,255,255,0.13); color:#d0e8ff;
|
| 564 |
+
padding:3px 12px; border-radius:20px; font-size:0.76rem;
|
| 565 |
+
font-family:monospace; letter-spacing:0.02em;">
|
| 566 |
+
GINE Graph Encoder
|
| 567 |
+
</span>
|
| 568 |
+
<span style="background:rgba(255,255,255,0.13); color:#d0e8ff;
|
| 569 |
+
padding:3px 12px; border-radius:20px; font-size:0.76rem;
|
| 570 |
+
font-family:monospace; letter-spacing:0.02em;">
|
| 571 |
+
ESM-2 (150 M)
|
| 572 |
+
</span>
|
| 573 |
+
<span style="background:rgba(255,255,255,0.13); color:#d0e8ff;
|
| 574 |
+
padding:3px 12px; border-radius:20px; font-size:0.76rem;
|
| 575 |
+
font-family:monospace; letter-spacing:0.02em;">
|
| 576 |
+
Bidirectional Cross-Attention
|
| 577 |
+
</span>
|
| 578 |
+
<span style="background:rgba(200,230,80,0.2); color:#d4f0a0;
|
| 579 |
+
padding:3px 12px; border-radius:20px; font-size:0.76rem;
|
| 580 |
+
font-family:monospace; letter-spacing:0.02em;">
|
| 581 |
+
AUROC 0.941 (cold-target)
|
| 582 |
+
</span>
|
| 583 |
+
</div>
|
| 584 |
+
</div>
|
| 585 |
+
"""
|
| 586 |
+
|
| 587 |
+
_ABSTRACT_HTML = """
|
| 588 |
+
<div style="
|
| 589 |
+
background:#f4f7fb;
|
| 590 |
+
border-left:4px solid #1a4a7a;
|
| 591 |
+
padding:12px 18px;
|
| 592 |
+
margin:8px 0 4px;
|
| 593 |
+
border-radius:0 5px 5px 0;
|
| 594 |
+
font-family:'Georgia',serif;
|
| 595 |
+
">
|
| 596 |
+
<strong style="color:#1a4a7a; font-size:0.78rem; text-transform:uppercase;
|
| 597 |
+
letter-spacing:0.06em;">Abstract</strong>
|
| 598 |
+
<p style="margin:6px 0 0; font-size:0.88rem; color:#2a2a3e; line-height:1.65;">
|
| 599 |
+
BioInteract couples a pharmacophore-aware Graph Isomorphism Network with Edge features
|
| 600 |
+
(GINE) for molecular encoding with ESM-2 protein language model representations for
|
| 601 |
+
residue encoding. A bidirectional cross-attention mechanism generates an interpretable
|
| 602 |
+
atomβresidue interaction map, enabling simultaneous prediction of binding affinity and
|
| 603 |
+
mechanistic insight into which drug substructures engage specific protein residues.
|
| 604 |
+
Evaluated on the Davis kinase dataset, the model achieves AUROC 0.941 on the
|
| 605 |
+
cold-target split, demonstrating strong generalisation to unseen protein targets.
|
| 606 |
+
</p>
|
| 607 |
+
</div>
|
| 608 |
+
"""
|
| 609 |
+
|
| 610 |
+
with gr.Blocks(
|
| 611 |
+
title="BioInteract β Interpretable DTI Prediction",
|
| 612 |
+
theme=gr.themes.Base(
|
| 613 |
+
primary_hue=gr.themes.colors.blue,
|
| 614 |
+
neutral_hue=gr.themes.colors.slate,
|
| 615 |
+
font=[gr.themes.GoogleFont("Source Serif 4"), "Georgia", "serif"],
|
| 616 |
+
),
|
| 617 |
+
css=_CSS,
|
| 618 |
+
) as demo:
|
| 619 |
+
|
| 620 |
+
gr.HTML(_HEADER_HTML)
|
| 621 |
+
gr.HTML(_ABSTRACT_HTML)
|
| 622 |
+
|
| 623 |
+
with gr.Row(equal_height=False):
|
| 624 |
+
# ββ Main content area ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 625 |
+
with gr.Column(scale=3):
|
| 626 |
+
with gr.Tabs():
|
| 627 |
+
|
| 628 |
+
# ββ Tab 1: Case Studies ββββββββββββββββββββββββββββββββββββ
|
| 629 |
+
with gr.Tab("Case Studies"):
|
| 630 |
+
gr.Markdown(
|
| 631 |
+
"Select a pre-computed case study to examine the model's atomβresidue "
|
| 632 |
+
"interaction map and predicted binding residues for clinically validated "
|
| 633 |
+
"drugβtarget pairs. All cases are drawn from the Davis kinase benchmark."
|
| 634 |
+
)
|
| 635 |
+
case_dropdown = gr.Dropdown(
|
| 636 |
+
choices=list(_FIXED_CASES.keys()),
|
| 637 |
+
value=list(_FIXED_CASES.keys())[0],
|
| 638 |
+
label="DrugβTarget Pair",
|
| 639 |
+
interactive=True,
|
| 640 |
+
)
|
| 641 |
+
case_info_md = gr.Markdown(
|
| 642 |
+
container=True,
|
| 643 |
+
min_height=80,
|
| 644 |
+
)
|
| 645 |
+
|
| 646 |
+
with gr.Row():
|
| 647 |
+
case_heatmap = gr.Image(
|
| 648 |
+
label="AtomβResidue Cross-Attention Heatmap",
|
| 649 |
+
type='pil',
|
| 650 |
+
height=420,
|
| 651 |
+
)
|
| 652 |
+
with gr.Column():
|
| 653 |
+
case_residues = gr.Image(
|
| 654 |
+
label="Top Predicted Binding Residues",
|
| 655 |
+
type='pil',
|
| 656 |
+
height=280,
|
| 657 |
+
)
|
| 658 |
+
case_fg = gr.Image(
|
| 659 |
+
label="Pharmacophore Group Importance (Grad-CAM)",
|
| 660 |
+
type='pil',
|
| 661 |
+
height=240,
|
| 662 |
+
)
|
| 663 |
+
|
| 664 |
+
gr.Markdown(
|
| 665 |
+
"_Figure caption:_ The heatmap encodes normalised cross-attention "
|
| 666 |
+
"weights between each drug atom (rows) and protein residue (columns). "
|
| 667 |
+
"Darker cells indicate stronger predicted interactions. "
|
| 668 |
+
"The bar chart ranks residues by aggregated attention score."
|
| 669 |
+
)
|
| 670 |
+
|
| 671 |
+
case_dropdown.change(
|
| 672 |
+
fn=show_case_study,
|
| 673 |
+
inputs=case_dropdown,
|
| 674 |
+
outputs=[case_heatmap, case_residues, case_fg, case_info_md],
|
| 675 |
+
)
|
| 676 |
+
demo.load(
|
| 677 |
+
fn=lambda: show_case_study(list(_FIXED_CASES.keys())[0]),
|
| 678 |
+
inputs=[],
|
| 679 |
+
outputs=[case_heatmap, case_residues, case_fg, case_info_md],
|
| 680 |
+
)
|
| 681 |
+
|
| 682 |
+
# ββ Tab 2: Custom Prediction βββββββββββββββββββββββββββββββ
|
| 683 |
+
with gr.Tab("Custom Prediction"):
|
| 684 |
+
gr.Markdown(
|
| 685 |
+
"Provide a drug SMILES string and a protein amino acid sequence "
|
| 686 |
+
"to obtain a binding prediction with an interpretable cross-attention map.\n\n"
|
| 687 |
+
"> **Note:** ESM-2 (150 M parameters) initialises on the first request; "
|
| 688 |
+
"please allow 1β3 minutes on CPU. Sequences exceeding 512 residues are "
|
| 689 |
+
"automatically truncated to the first 512 positions."
|
| 690 |
+
)
|
| 691 |
+
|
| 692 |
+
with gr.Row():
|
| 693 |
+
smiles_box = gr.Textbox(
|
| 694 |
+
label="Drug SMILES",
|
| 695 |
+
placeholder=(
|
| 696 |
+
"e.g. Cc1ccc(NC(=O)c2ccc(CN3CCN(C)CC3)cc2)cc1"
|
| 697 |
+
"Nc1nccc(-c2cccnc2)n1 (Imatinib)"
|
| 698 |
+
),
|
| 699 |
+
lines=2,
|
| 700 |
+
)
|
| 701 |
+
sequence_box = gr.Textbox(
|
| 702 |
+
label="Protein Amino Acid Sequence (single-letter code)",
|
| 703 |
+
placeholder="e.g. MGPSENDPNLFVALYDFVASGDNTLSβ¦",
|
| 704 |
+
lines=4,
|
| 705 |
+
max_lines=8,
|
| 706 |
+
)
|
| 707 |
+
|
| 708 |
+
with gr.Row():
|
| 709 |
+
example_btn = gr.Button(
|
| 710 |
+
"Load Imatinib / ABL1 Example",
|
| 711 |
+
variant="secondary",
|
| 712 |
+
size="sm",
|
| 713 |
+
)
|
| 714 |
+
predict_btn = gr.Button(
|
| 715 |
+
"Run Prediction",
|
| 716 |
+
variant="primary",
|
| 717 |
+
size="lg",
|
| 718 |
+
)
|
| 719 |
+
|
| 720 |
+
status_box = gr.Textbox(
|
| 721 |
+
label="Status",
|
| 722 |
+
interactive=False,
|
| 723 |
+
lines=1,
|
| 724 |
+
placeholder="Awaiting input β¦",
|
| 725 |
+
)
|
| 726 |
+
prob_md = gr.Markdown(min_height=80)
|
| 727 |
+
|
| 728 |
+
with gr.Row():
|
| 729 |
+
pred_heatmap = gr.Image(
|
| 730 |
+
label="AtomβResidue Cross-Attention Heatmap",
|
| 731 |
+
type='pil',
|
| 732 |
+
height=420,
|
| 733 |
+
)
|
| 734 |
+
pred_residues = gr.Image(
|
| 735 |
+
label="Top 10 Predicted Binding Residues",
|
| 736 |
+
type='pil',
|
| 737 |
+
height=320,
|
| 738 |
+
)
|
| 739 |
+
|
| 740 |
+
gr.Markdown(
|
| 741 |
+
"_Interpretation:_ Rows correspond to heavy atoms of the drug molecule; "
|
| 742 |
+
"columns to protein residues. High-intensity cells indicate residues "
|
| 743 |
+
"predicted to form key contacts with the respective drug atoms. "
|
| 744 |
+
"The bar chart aggregates attention over all atoms for each residue."
|
| 745 |
+
)
|
| 746 |
+
|
| 747 |
+
example_btn.click(
|
| 748 |
+
fn=lambda: (_EXAMPLE_SMILES, _EXAMPLE_SEQUENCE),
|
| 749 |
+
inputs=[],
|
| 750 |
+
outputs=[smiles_box, sequence_box],
|
| 751 |
+
)
|
| 752 |
+
predict_btn.click(
|
| 753 |
+
fn=run_prediction,
|
| 754 |
+
inputs=[smiles_box, sequence_box],
|
| 755 |
+
outputs=[status_box, prob_md, pred_heatmap, pred_residues],
|
| 756 |
+
)
|
| 757 |
+
|
| 758 |
+
# ββ Sidebar ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 759 |
+
with gr.Column(scale=1, min_width=260):
|
| 760 |
+
gr.HTML(_SIDEBAR_HTML)
|
| 761 |
+
|
| 762 |
+
|
| 763 |
+
if __name__ == "__main__":
|
| 764 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
CHANGED
|
@@ -1,9 +1,15 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
--extra-index-url https://download.pytorch.org/whl/cpu
|
| 2 |
+
torch==2.4.0
|
| 3 |
+
torch-geometric==2.6.1
|
| 4 |
+
rdkit
|
| 5 |
+
transformers==5.6.1
|
| 6 |
+
numpy==1.26.4
|
| 7 |
+
pandas==2.0.3
|
| 8 |
+
scipy==1.13.1
|
| 9 |
+
scikit-learn==1.2.1
|
| 10 |
+
PyYAML==6.0.2
|
| 11 |
+
matplotlib==3.8.4
|
| 12 |
+
seaborn==0.12.2
|
| 13 |
+
tqdm==4.67.3
|
| 14 |
+
Pillow==9.4.0
|
| 15 |
+
gradio==5.49.1
|
src/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (179 Bytes). View file
|
|
|
src/data/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (184 Bytes). View file
|
|
|
src/data/__pycache__/mol_graph.cpython-310.pyc
ADDED
|
Binary file (6.74 kB). View file
|
|
|
src/data/__pycache__/protein_feat.cpython-310.pyc
ADDED
|
Binary file (3.46 kB). View file
|
|
|
src/models/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (186 Bytes). View file
|
|
|
src/models/__pycache__/biointeract.cpython-310.pyc
ADDED
|
Binary file (5.87 kB). View file
|
|
|
src/models/__pycache__/drug_encoder.cpython-310.pyc
ADDED
|
Binary file (7.01 kB). View file
|
|
|
src/models/__pycache__/interaction.cpython-310.pyc
ADDED
|
Binary file (6.04 kB). View file
|
|
|
src/models/__pycache__/target_encoder.cpython-310.pyc
ADDED
|
Binary file (3.23 kB). View file
|
|
|