Repository navigation
Expand file tree
/
Copy pathodin_multi.py
More file actions
executable file
·1503 lines (1366 loc) · 54.9 KB
/
Copy pathodin_multi.py
File metadata and controls
executable file
·1503 lines (1366 loc) · 54.9 KB
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
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
393
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Simple, resumable Odin-Multi design, selection, and AF2/AF3 evaluation."""
from __future__ import annotations
import argparse
import copy
import csv
import fcntl
import json
import math
import os
import pickle
import re
import shutil
import tempfile
from contextlib import contextmanager
from pathlib import Path
from typing import Any, Iterator
import numpy as np
from run_layout import (
CURRENT_LAYOUT_VERSION,
RUN_FILE,
RunLayout,
layout_for_run,
)
REPOSITORY_ROOT = Path(__file__).resolve().parent
AA_ORDER = "ARNDCQEGHILKMFPSTWYV"
# Stages whose saved frames carry a discrete (one-hot) sequence, and are
# therefore selectable. "logits" and "soft" are continuous and never eligible.
# The name varies by design_algorithm: 3stage -> "hard", 3stage_ste -> "ste"
# (ColabDesign af/design.py:1571,1576), greedy -> "greedy", mcmc -> "mcmc".
HARD_STAGES = frozenset({"hard", "ste", "greedy", "mcmc"})
OFFTARGET_I_PAE_CAP = 15.0
SELECTION_METHODS = (
"last",
"best_i_ptm",
"best_i_pae",
"best_clipped_i_pae_ratio",
)
EVALUATORS = ("af2", "af3")
DEFAULT_EVALUATOR = "af3"
def json_safe(value: Any) -> Any:
if value is None or isinstance(value, (str, bool, int)):
return value
if isinstance(value, float):
return value if math.isfinite(value) else None
if isinstance(value, Path):
return str(value)
if isinstance(value, dict):
return {str(key): json_safe(item) for key, item in value.items()}
if isinstance(value, (list, tuple)):
return [json_safe(item) for item in value]
if hasattr(value, "item"):
try:
return json_safe(value.item())
except (TypeError, ValueError):
pass
if hasattr(value, "tolist"):
return json_safe(value.tolist())
raise TypeError(f"Cannot serialize {type(value).__name__} as JSON")
def load_json(path: Path) -> Any:
with path.open("r", encoding="utf-8") as handle:
return json.load(handle)
def atomic_write_json(path: Path, value: Any) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
fd, temporary = tempfile.mkstemp(prefix=f".{path.name}.", dir=path.parent)
try:
with os.fdopen(fd, "w", encoding="utf-8") as handle:
json.dump(json_safe(value), handle, indent=2, sort_keys=True, allow_nan=False)
handle.write("\n")
os.replace(temporary, path)
finally:
if os.path.exists(temporary):
os.unlink(temporary)
def atomic_write_text(path: Path, value: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
fd, temporary = tempfile.mkstemp(prefix=f".{path.name}.", dir=path.parent)
try:
with os.fdopen(fd, "w", encoding="utf-8") as handle:
handle.write(value)
os.replace(temporary, path)
finally:
if os.path.exists(temporary):
os.unlink(temporary)
def _copy_atomic(source: Path, destination: Path) -> None:
destination.parent.mkdir(parents=True, exist_ok=True)
fd, temporary = tempfile.mkstemp(prefix=f".{destination.name}.", dir=destination.parent)
os.close(fd)
try:
shutil.copy2(source, temporary)
os.replace(temporary, destination)
finally:
if os.path.exists(temporary):
os.unlink(temporary)
def _slug(value: str) -> str:
result = re.sub(r"[^A-Za-z0-9_.-]+", "_", str(value).strip()).strip("_.")
if not result:
raise ValueError(f"Cannot make a safe name from {value!r}")
return result
@contextmanager
def file_lock(path: Path, blocking: bool = True) -> Iterator[bool]:
"""Hold a POSIX advisory lock; nonblocking callers receive False when busy."""
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("a+", encoding="utf-8") as handle:
flags = fcntl.LOCK_EX | (0 if blocking else fcntl.LOCK_NB)
try:
fcntl.flock(handle.fileno(), flags)
except BlockingIOError:
yield False
return
try:
yield True
finally:
fcntl.flock(handle.fileno(), fcntl.LOCK_UN)
def _resolve_file(value: Any, owner: Path) -> Path:
candidate = Path(str(value)).expanduser()
candidates = [candidate] if candidate.is_absolute() else [
Path.cwd() / candidate,
owner.parent / candidate,
]
for path in candidates:
resolved = path.resolve()
if resolved.is_file():
return resolved
raise FileNotFoundError(f"Referenced file not found: {value!r}")
def _resolve_directory(value: Any, owner: Path) -> Path:
if value in (None, ""):
return REPOSITORY_ROOT
candidate = Path(str(value)).expanduser()
if candidate.is_absolute():
return candidate.resolve()
for path in (Path.cwd() / candidate, owner.parent / candidate):
if path.is_dir():
return path.resolve()
return (owner.parent / candidate).resolve()
def init_run(
run_dir: Path,
settings_paths: list[Path],
general_path: Path,
loss_paths: list[Path],
base_seed: int,
) -> dict[str, Any]:
"""Create the small saved run configuration used by later commands."""
run_dir = run_dir.resolve()
run_dir.mkdir(parents=True, exist_ok=True)
layout = layout_for_run(run_dir, create=True)
with file_lock(layout.locks / "run-setup.lock"):
if layout.manifest.is_file():
return load_run(run_dir)[1]
for stage in (
layout.inputs,
layout.designs,
layout.selections,
layout.evaluations,
layout.summary,
):
stage.mkdir(parents=True, exist_ok=True)
if not settings_paths or len(settings_paths) != len(loss_paths):
raise ValueError("The first run needs at least one --context SETTINGS LOSS")
if isinstance(base_seed, bool) or base_seed < 0:
raise ValueError("--base-seed must be a non-negative integer")
sources = [*settings_paths, general_path, *loss_paths]
for source in sources:
if not source.resolve().is_file():
raise FileNotFoundError(source)
general = load_json(general_path)
if not isinstance(general, dict):
raise ValueError("--advanced must contain a JSON object")
_copy_atomic(general_path.resolve(), layout.inputs / "general.json")
contexts: list[dict[str, Any]] = []
names: set[str] = set()
for index, (settings_path, loss_path) in enumerate(
zip(settings_paths, loss_paths)
):
settings_path = settings_path.resolve()
loss_path = loss_path.resolve()
settings = load_json(settings_path)
if not isinstance(settings, dict):
raise ValueError(f"Settings must contain a JSON object: {settings_path}")
if "role" in settings:
raise ValueError(
f"{settings_path}: role belongs in the paired "
"--context loss file, not in its target settings"
)
if "lengths" in settings:
raise ValueError(
f"{settings_path}: lengths belongs in the general "
"--advanced file, not in target settings"
)
loss = load_json(loss_path)
if not isinstance(loss, dict):
raise ValueError(f"Loss file must contain a JSON object: {loss_path}")
name = str(settings.get("binder_name", "")).strip()
role = str(loss.get("role", "")).strip().lower()
if not name or _slug(name) != name or name in names:
raise ValueError(f"binder_name must be unique and filesystem-safe: {name!r}")
if role not in {"target", "offtarget"}:
raise ValueError(f"Unsupported context role {role!r} for {name}")
if index == 0 and role != "target":
raise ValueError("The first context must have role 'target'")
names.add(name)
pdb = _resolve_file(settings.get("starting_pdb"), settings_path)
folder = Path(layout.inputs.name) / "contexts" / f"{index:02d}_{name}"
saved = {
"name": name,
"role": role,
"settings": str(folder / "settings.json"),
"loss": str(folder / "loss.json"),
"pdb": str(folder / "target.pdb"),
}
_copy_atomic(settings_path, run_dir / saved["settings"])
_copy_atomic(loss_path, run_dir / saved["loss"])
_copy_atomic(pdb, run_dir / saved["pdb"])
contexts.append(saved)
manifest = {
"layout_version": CURRENT_LAYOUT_VERSION,
"base_seed": int(base_seed),
"requested_designs": 0,
"advanced": str(Path(layout.inputs.name) / "general.json"),
"contexts": contexts,
"af_params_dir": str(
_resolve_directory(general.get("af_params_dir"), general_path)
),
}
atomic_write_json(layout.manifest, manifest)
return manifest
def load_run(run_dir: Path) -> tuple[Path, dict[str, Any]]:
run_dir = run_dir.resolve()
try:
layout = layout_for_run(run_dir)
except FileNotFoundError as error:
raise FileNotFoundError(
f"{error}. Supply --context and --advanced on the first design command."
) from error
value = load_json(layout.manifest)
if not isinstance(value, dict) or not value.get("contexts"):
raise ValueError(f"Invalid run configuration: {layout.manifest}")
if not layout.legacy and value.get("layout_version") != CURRENT_LAYOUT_VERSION:
raise ValueError(
f"Unsupported run layout version in {layout.manifest}: "
f"{value.get('layout_version')!r}"
)
return run_dir, value
def ensure_run(
run_dir: Path,
settings_paths: list[Path] | None = None,
general_path: Path | None = None,
loss_paths: list[Path] | None = None,
base_seed: int = 0,
) -> tuple[Path, dict[str, Any]]:
run_dir = run_dir.resolve()
try:
layout_for_run(run_dir)
except FileNotFoundError:
pass
else:
return load_run(run_dir)
if not settings_paths or general_path is None or not loss_paths:
raise ValueError(
"The first design command needs --context SETTINGS LOSS and --advanced"
)
init_run(run_dir, settings_paths, general_path, loss_paths, base_seed)
return load_run(run_dir)
def update_design_target(
run_dir: Path, manifest: dict[str, Any], requested: int
) -> int:
if requested <= 0:
raise ValueError("--num-designs must be a positive integer")
run_dir = run_dir.resolve()
layout = layout_for_run(run_dir)
with file_lock(layout.locks / "run-total.lock"):
current = load_json(layout.manifest)
effective = max(int(current.get("requested_designs", 0)), requested)
if effective != current.get("requested_designs"):
current["requested_designs"] = effective
atomic_write_json(layout.manifest, current)
manifest["requested_designs"] = effective
return effective
def requested_designs(run_dir: Path, manifest: dict[str, Any] | None = None) -> int:
layout = layout_for_run(run_dir)
return int(load_json(layout.manifest).get("requested_designs", 0))
def trajectory_parameters(
base_seed: int, index: int, minimum: int, maximum: int
) -> tuple[int, int]:
generator = np.random.default_rng(np.random.SeedSequence([base_seed, index]))
return (
int(generator.integers(0, 999999)),
int(generator.integers(minimum, maximum + 1)),
)
def trajectory_helicity(
advanced: dict[str, Any], base_seed: int, index: int
) -> float:
if advanced.get("random_helicity") is True:
generator = np.random.default_rng(np.random.SeedSequence([base_seed, index, 17]))
return round(float(generator.uniform(-3.0, 1.0)), 2)
return float(advanced.get("weights_helicity") or 0.0)
def _run_path(run_dir: Path, value: str) -> Path:
path = Path(value)
return path if path.is_absolute() else run_dir / path
def _binder_length_bounds(
run_dir: Path, manifest: dict[str, Any]
) -> tuple[int, int]:
general = load_json(_run_path(run_dir, manifest["advanced"]))
lengths = general.get("lengths")
if not isinstance(lengths, list) or not lengths:
raise ValueError("General advanced settings needs a non-empty lengths list")
values = [int(value) for value in lengths]
if min(values) <= 0:
raise ValueError("Binder lengths must be positive")
return min(values), max(values)
def _design_directories(
layout: RunLayout, index: int | None = None
) -> dict[str, str]:
if not layout.legacy:
if index is None:
raise ValueError("A design index is required for layout version 2")
base = layout.design_dir(index)
work = base / ".work"
paths = {
"Trajectory": work / "structure",
"Trajectory/Animation": work / "animation",
"Trajectory/Clashing": work / "clashing",
"Trajectory/LowConfidence": work / "low_confidence",
"Trajectory/Pickle": work / "pickle",
"Trajectory/Plots": work / "plots",
}
for path in paths.values():
path.mkdir(parents=True, exist_ok=True)
return {name: str(path) for name, path in paths.items()}
result: dict[str, str] = {}
for name in (
"Trajectory",
"Trajectory/Animation",
"Trajectory/Clashing",
"Trajectory/LowConfidence",
"Trajectory/Pickle",
"Trajectory/Plots",
):
path = layout.root / name
path.mkdir(parents=True, exist_ok=True)
result[name] = str(path)
return result
def _status_path(run_dir: Path, index: int) -> Path:
return layout_for_run(run_dir).design_status(index)
def _design_id(
manifest: dict[str, Any],
index: int,
minimum: int,
maximum: int,
) -> tuple[str, int, int]:
seed, length = trajectory_parameters(
int(manifest["base_seed"]), index, minimum, maximum
)
name = manifest["contexts"][0]["name"]
return f"{name}_t{index:05d}_l{length}_s{seed}", seed, length
def _trajectory_file(run_dir: Path, design_id: str, index: int) -> Path:
return layout_for_run(run_dir).design_trajectory(index, design_id)
def _completed_design(run_dir: Path, index: int) -> dict[str, Any] | None:
path = _status_path(run_dir, index)
if not path.is_file():
return None
try:
status = load_json(path)
artifact = _run_path(run_dir, status["trajectory_pickle"])
except (KeyError, OSError, TypeError, ValueError):
return None
return status if status.get("status") == "complete" and artifact.is_file() else None
def _failure_csv(path: Path) -> None:
atomic_write_text(
path,
"Trajectory_logits_pLDDT,Trajectory_softmax_pLDDT,"
"Trajectory_one-hot_pLDDT,Trajectory_ste_pLDDT,Trajectory_final_pLDDT,"
"Trajectory_Contacts,Trajectory_Clashes,Trajectory_WrongHotspot\n"
"0,0,0,0,0,0,0,0\n",
)
def _clear_design_outputs(
run_dir: Path,
design_id: str,
index: int,
) -> None:
layout = layout_for_run(run_dir)
if not layout.legacy:
directory = layout.design_dir(index)
if directory.is_dir():
shutil.rmtree(directory)
return
design_paths = _design_directories(layout)
files = [
_status_path(run_dir, index),
Path(design_paths["Trajectory/Pickle"]) / f"{design_id}.pickle",
_trajectory_file(run_dir, design_id, index),
Path(design_paths["Trajectory/Animation"]) / f"{design_id}.html",
layout.design_checks(index),
]
for key in ("Trajectory", "Trajectory/LowConfidence", "Trajectory/Clashing"):
files.append(Path(design_paths[key]) / f"{design_id}.pdb")
for path in files:
path.unlink(missing_ok=True)
plots = Path(design_paths["Trajectory/Plots"])
for path in plots.glob(f"{design_id}_*.png"):
path.unlink()
def _trajectory_pdb(
design_paths: dict[str, str], design_id: str
) -> Path | None:
for key in ("Trajectory", "Trajectory/LowConfidence", "Trajectory/Clashing"):
path = Path(design_paths[key]) / f"{design_id}.pdb"
if path.is_file():
return path
return None
def _normalize_design_outputs(
layout: RunLayout,
index: int,
design_id: str,
design_paths: dict[str, str],
) -> tuple[Path, Path | None]:
"""Give version-2 design artifacts short names inside their design folder."""
if layout.legacy:
return (
layout.design_trajectory(index, design_id),
_trajectory_pdb(design_paths, design_id),
)
base = layout.design_dir(index)
moves = [
(
Path(design_paths["Trajectory/Pickle"]) / f"{design_id}_trajectory.pickle",
layout.design_trajectory(index, design_id),
),
(
Path(design_paths["Trajectory/Pickle"]) / f"{design_id}.pickle",
layout.design_auxiliary(index, design_id),
),
(
Path(design_paths["Trajectory/Animation"]) / f"{design_id}.html",
base / "animation.html",
),
]
pdb = _trajectory_pdb(design_paths, design_id)
if pdb is not None:
moves.append((pdb, base / "structure.pdb"))
plots = Path(design_paths["Trajectory/Plots"])
for source in plots.glob(f"{design_id}_*.png"):
moves.append((source, base / "plots" / source.name[len(design_id) + 1:]))
for source, destination in moves:
if source.is_file():
destination.parent.mkdir(parents=True, exist_ok=True)
os.replace(source, destination)
work = base / ".work"
if work.is_dir():
shutil.rmtree(work)
structure = base / "structure.pdb"
return layout.design_trajectory(index, design_id), (
structure if structure.is_file() else None
)
def _relative(path: Path | None, root: Path) -> str | None:
return str(path.relative_to(root)) if path is not None else None
def run_design(
run_dir: Path,
num_designs: int,
*,
shard_index: int = 0,
num_shards: int = 1,
settings_paths: list[Path] | None = None,
general_path: Path | None = None,
loss_paths: list[Path] | None = None,
base_seed: int = 0,
) -> dict[str, int]:
if num_shards < 1 or not 0 <= shard_index < num_shards:
raise ValueError("Require num_shards >= 1 and 0 <= shard_index < num_shards")
run_dir, manifest = ensure_run(
run_dir, settings_paths, general_path, loss_paths, base_seed
)
total = update_design_target(run_dir, manifest, num_designs)
minimum, maximum = _binder_length_bounds(run_dir, manifest)
assigned = [index for index in range(total) if index % num_shards == shard_index]
summary = {
"requested": total,
"assigned": len(assigned),
"completed": 0,
"skipped": 0,
"busy": 0,
"failed": 0,
}
pending = [index for index in assigned if _completed_design(run_dir, index) is None]
summary["skipped"] = len(assigned) - len(pending)
if not pending:
return summary
# GPU dependencies are imported only when unfinished work exists.
try:
import functions.colabdesign_utils as colabdesign_utils
from functions.generic_utils import (
check_jax_gpu,
load_af2_models,
load_json_settings,
perform_advanced_settings_check,
)
except ModuleNotFoundError as error:
if error.name == "colabdesign":
raise RuntimeError(
"ColabDesign is not installed; run install_odin_multi.sh or "
"pip install -e ./ColabDesign --no-deps"
) from error
raise
settings_files = [_run_path(run_dir, item["settings"]) for item in manifest["contexts"]]
loss_files = [_run_path(run_dir, item["loss"]) for item in manifest["contexts"]]
settings_list, advanced_list, _ = load_json_settings(
[str(path) for path in settings_files],
str(_run_path(run_dir, manifest["advanced"])),
[str(path) for path in loss_files],
)
for settings, context in zip(settings_list, manifest["contexts"]):
settings["starting_pdb"] = str(_run_path(run_dir, context["pdb"]))
settings_list[0]["design_path"] = str(run_dir)
advanced_list = [
perform_advanced_settings_check(copy.deepcopy(value), str(REPOSITORY_ROOT))
for value in advanced_list
]
params_dir = manifest.get("af_params_dir")
if params_dir is None:
params_dir = manifest.get("af2_defaults", {}).get("params_dir")
if params_dir is None:
raise ValueError("Run manifest is missing the AF2 design parameter directory")
for advanced in advanced_list:
advanced["af_params_dir"] = params_dir
check_jax_gpu()
design_models, _, _ = load_af2_models(advanced_list[0]["use_multimer_design"])
layout = layout_for_run(run_dir)
failures: list[str] = []
for index in pending:
lock_folder = "Design" if layout.legacy else "design"
lock_path = layout.locks / lock_folder / f"t{index:05d}.lock"
with file_lock(lock_path, blocking=False) as acquired:
if not acquired:
summary["busy"] += 1
continue
if _completed_design(run_dir, index) is not None:
summary["skipped"] += 1
continue
design_id, seed, length = _design_id(manifest, index, minimum, maximum)
_clear_design_outputs(run_dir, design_id, index)
design_paths = _design_directories(layout, index)
failure_path = layout.design_checks(index)
_failure_csv(failure_path)
status = {
"status": "running",
"design_index": index,
"design_id": design_id,
"seed": seed,
"length": length,
"helicity": trajectory_helicity(
advanced_list[0], int(manifest["base_seed"]), index
),
}
atomic_write_json(_status_path(run_dir, index), status)
try:
model = colabdesign_utils.binder_hallucination(
design_id,
copy.deepcopy(settings_list[0]),
copy.deepcopy(advanced_list[0]),
copy.deepcopy(settings_list[1:]),
copy.deepcopy(advanced_list[1:]),
length,
seed,
status["helicity"],
design_paths,
str(failure_path),
design_models,
)
artifact, pdb = _normalize_design_outputs(
layout, index, design_id, design_paths
)
if not artifact.is_file():
raise FileNotFoundError(f"Trajectory pickle was not saved: {artifact}")
if advanced_list[0].get("remove_unrelaxed_trajectory", False):
if pdb is not None:
pdb.unlink()
pdb = None
atomic_write_json(_status_path(run_dir, index), {
**status,
"status": "complete",
"termination": str(model.aux.get("log", {}).get("terminate", "")),
"trajectory_pickle": _relative(artifact, run_dir),
"trajectory_pdb": _relative(pdb, run_dir),
})
summary["completed"] += 1
except Exception as error:
atomic_write_json(_status_path(run_dir, index), {
**status,
"status": "failed",
"error": f"{type(error).__name__}: {error}",
})
summary["failed"] += 1
failures.append(design_id)
if failures:
raise RuntimeError(
f"{len(failures)} trajectory attempt(s) failed: {', '.join(failures[:5])}"
)
return summary
def _finite(value: Any) -> float | None:
try:
number = float(value)
except (TypeError, ValueError):
return None
return number if math.isfinite(number) else None
def _sequence(frame: Any) -> str:
array = np.asarray(frame)
while array.ndim > 2 and array.shape[0] == 1:
array = array[0]
if array.ndim == 2 and array.shape[1] == len(AA_ORDER):
indices = array.argmax(-1)
elif array.ndim == 1 and np.issubdtype(array.dtype, np.integer):
indices = array
else:
raise ValueError(f"Unsupported saved sequence shape {array.shape}")
if np.any(indices < 0) or np.any(indices >= len(AA_ORDER)):
raise ValueError("Saved sequence has an invalid amino-acid index")
return "".join(AA_ORDER[int(index)] for index in indices)
def _trajectory_frames(value: Any) -> dict[str, list[Any]]:
if not isinstance(value, dict):
raise ValueError("Trajectory context is missing")
keys = ("seq", "xyz", "plddt", "pae", "ptm", "i_ptm", "iteration", "stage")
if any(not isinstance(value.get(key), list) for key in keys):
raise ValueError("Trajectory is missing a saved frame list")
lengths = {len(value[key]) for key in keys}
if len(lengths) != 1 or not lengths or next(iter(lengths)) == 0:
raise ValueError("Trajectory frame lists are empty or unaligned")
return value
def _interface_pae(frame: Any, binder_length: int) -> float | None:
array = np.asarray(frame).squeeze()
if array.ndim != 2 or array.shape[0] != array.shape[1]:
return None
target_length = array.shape[0] - binder_length
if target_length <= 0:
return None
value = (
float(array[:target_length, target_length:].mean())
+ float(array[target_length:, :target_length].mean())
) / 2.0
return value if math.isfinite(value) else None
def _aligned_frames(
reference: dict[str, list[Any]],
trajectories: list[tuple[str, dict[str, list[Any]]]],
) -> None:
reference_alignment = list(zip(reference["iteration"], reference["stage"]))
for name, trajectory in trajectories:
alignment = list(zip(trajectory["iteration"], trajectory["stage"]))
if alignment != reference_alignment:
raise ValueError(f"Context {name!r} saved frames are not aligned")
def _hard_frames(trajectory: dict[str, list[Any]]) -> list[int]:
indices = [
index
for index, stage in enumerate(trajectory["stage"])
if str(stage) in HARD_STAGES
]
if not indices:
saved = sorted({str(stage) for stage in trajectory["stage"]})
raise ValueError(
"No saved frame with a discrete sequence; saved stages were "
f"{saved or ['none']}, expected one of {sorted(HARD_STAGES)}"
)
return indices
def _frame_iteration(trajectory: dict[str, list[Any]], index: int) -> int:
value = trajectory["iteration"][index]
return index if value is None else int(value)
def _choose_target_frame(
targets: list[tuple[str, dict[str, list[Any]]]],
method: str,
binder_length: int,
) -> tuple[int, int, str, float | int, float | None, str | None]:
"""Select a hard frame using the worst-performing target context."""
reference = targets[0][1]
_aligned_frames(reference, targets[1:])
eligible = _hard_frames(reference)
def iteration(index: int) -> int:
return _frame_iteration(reference, index)
if method == "last":
selected = max(eligible, key=lambda index: (iteration(index), index))
score: float | int = iteration(selected)
worst_value = None
worst_name = None
elif method == "best_i_pae":
scored: list[tuple[float, int, str]] = []
for index in eligible:
values = [
(_interface_pae(trajectory["pae"][index], binder_length), name)
for name, trajectory in targets
]
if any(value is None for value, _ in values):
continue
worst_value, worst_name = max(
((float(value), name) for value, name in values),
key=lambda item: item[0],
)
scored.append((worst_value, index, worst_name))
if not scored:
raise ValueError("No hard frame has interface PAE for every target")
score, selected, worst_name = min(
scored,
key=lambda item: (item[0], -iteration(item[1]), -item[1]),
)
worst_value = score
elif method == "best_i_ptm":
scored = []
for index in eligible:
values = [
(_finite(trajectory["i_ptm"][index]), name)
for name, trajectory in targets
]
if any(value is None for value, _ in values):
continue
worst_value, worst_name = min(
((float(value), name) for value, name in values),
key=lambda item: item[0],
)
scored.append((worst_value, index, worst_name))
if not scored:
raise ValueError("No hard frame has interface pTM for every target")
score, selected, worst_name = max(
scored,
key=lambda item: (item[0], iteration(item[1]), item[1]),
)
worst_value = score
else:
raise ValueError(f"Unsupported target selection method {method!r}")
return (
selected,
iteration(selected),
str(reference["stage"][selected]),
score,
worst_value,
worst_name,
)
def _choose_specificity_frame(
targets: list[tuple[str, dict[str, list[Any]]]],
offtargets: list[tuple[str, dict[str, list[Any]]]],
binder_length: int,
) -> tuple[int, int, str, float, float, str, float, float, str]:
"""Select the hard frame with the best clipped off-target/target ratio."""
reference = targets[0][1]
_aligned_frames(reference, targets[1:] + offtargets)
eligible = _hard_frames(reference)
def iteration(index: int) -> int:
return _frame_iteration(reference, index)
candidates: list[tuple[float, int, float, str, float, float, str]] = []
for index in eligible:
target_values = [
(_interface_pae(trajectory["pae"][index], binder_length), name)
for name, trajectory in targets
]
offtarget_values = [
(_interface_pae(trajectory["pae"][index], binder_length), name)
for name, trajectory in offtargets
]
if (
any(value is None for value, _ in target_values)
or any(value is None for value, _ in offtarget_values)
):
continue
worst_target_i_pae, worst_target_name = max(
((float(value), name) for value, name in target_values),
key=lambda item: item[0],
)
strongest_offtarget_i_pae, strongest_offtarget_name = min(
((float(value), name) for value, name in offtarget_values),
key=lambda item: item[0],
)
if worst_target_i_pae <= 0:
continue
clipped_offtarget_i_pae = min(
strongest_offtarget_i_pae, OFFTARGET_I_PAE_CAP
)
score = clipped_offtarget_i_pae / worst_target_i_pae
if math.isfinite(score):
candidates.append((
score,
index,
worst_target_i_pae,
worst_target_name,
strongest_offtarget_i_pae,
clipped_offtarget_i_pae,
strongest_offtarget_name,
))
if not candidates:
raise ValueError(
"No hard frame has valid target/off-target interface PAE"
)
chosen = max(
candidates,
key=lambda item: (item[0], iteration(item[1]), item[1]),
)
(
score,
selected,
worst_target_i_pae,
worst_target_name,
strongest_offtarget_i_pae,
clipped_offtarget_i_pae,
strongest_offtarget_name,
) = chosen
return (
selected,
iteration(selected),
str(reference["stage"][selected]),
score,
worst_target_i_pae,
worst_target_name,
strongest_offtarget_i_pae,
clipped_offtarget_i_pae,
strongest_offtarget_name,
)
def _csv_text(rows: list[dict[str, Any]], fields: list[str]) -> str:
import io
buffer = io.StringIO()
writer = csv.DictWriter(buffer, fieldnames=fields, extrasaction="ignore")
writer.writeheader()
writer.writerows(
{key: "" if value is None else value for key, value in row.items()}
for row in rows
)
return buffer.getvalue()
def _save_selection_figure(fig: Any, path: Path, *, dpi: int | None = None) -> None:
"""Atomically save one selection figure."""
path.parent.mkdir(parents=True, exist_ok=True)
fd, temporary = tempfile.mkstemp(
prefix=f".{path.stem}.", suffix=path.suffix, dir=path.parent
)
os.close(fd)
try:
fig.savefig(
temporary,
format=path.suffix.lstrip("."),
dpi=dpi,
facecolor="white",
bbox_inches="tight",
)
os.replace(temporary, path)
finally:
if os.path.exists(temporary):
os.unlink(temporary)
def _plot_selection_overview(
output: Path, method: str, rows: list[dict[str, Any]]
) -> int:
"""Plot the metric that caused one frame to be selected per design."""
figures = output / "figures"
if figures.is_dir():
shutil.rmtree(figures)
selected = [row for row in rows if row["selection_status"] == "selected"]
if not selected:
return 0
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
teal = "#2BBAAC"
charcoal = "#42403F"
mid_grey = "#828080"
light_grey = "#C2C0BF"
style = {
"font.family": "sans-serif",
"font.sans-serif": ["Arial", "Helvetica", "DejaVu Sans"],
"font.size": 6.0,
"axes.titlesize": 7.0,
"axes.labelsize": 6.0,
"xtick.labelsize": 5.5,
"ytick.labelsize": 5.5,
"legend.fontsize": 5.3,
"legend.frameon": False,
"axes.edgecolor": charcoal,
"axes.linewidth": 0.7,
"axes.titlepad": 3.0,
"xtick.direction": "out",
"ytick.direction": "out",
"xtick.major.size": 3.0,
"ytick.major.size": 3.0,
"xtick.major.width": 0.65,
"ytick.major.width": 0.65,
"svg.fonttype": "none",
}
def clean_axis(axis: Any) -> None:
axis.spines["top"].set_visible(False)
axis.spines["right"].set_visible(False)
axis.tick_params(
direction="out", pad=1.5, colors=charcoal,
width=0.65, length=3.0,
)
axis.grid(False)
with plt.rc_context(style):
if method == "best_clipped_i_pae_ratio":
fig, axis = plt.subplots(figsize=(3.5, 2.8))
x = np.asarray(
[float(row["worst_target_i_pae"]) for row in selected]
)
y = np.asarray(
[float(row["strongest_offtarget_i_pae"]) for row in selected]
)
ratio_cutoff = 1.5