tt-bio 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. tt_bio/__init__.py +6 -0
  2. tt_bio/_vendor/__init__.py +0 -0
  3. tt_bio/_vendor/esm/LICENSE +9 -0
  4. tt_bio/_vendor/esm/__init__.py +0 -0
  5. tt_bio/_vendor/esm/models/__init__.py +0 -0
  6. tt_bio/_vendor/esm/models/esmfold2/__init__.py +44 -0
  7. tt_bio/_vendor/esm/models/esmfold2/conformers.py +294 -0
  8. tt_bio/_vendor/esm/models/esmfold2/constants.py +565 -0
  9. tt_bio/_vendor/esm/models/esmfold2/output.py +227 -0
  10. tt_bio/_vendor/esm/models/esmfold2/paired_msa.py +248 -0
  11. tt_bio/_vendor/esm/models/esmfold2/prepare_input.py +1484 -0
  12. tt_bio/_vendor/esm/models/esmfold2/processor.py +358 -0
  13. tt_bio/_vendor/esm/models/esmfold2/types.py +36 -0
  14. tt_bio/_vendor/esm/utils/__init__.py +0 -0
  15. tt_bio/_vendor/esm/utils/constants/__init__.py +0 -0
  16. tt_bio/_vendor/esm/utils/constants/esm3.py +140 -0
  17. tt_bio/_vendor/esm/utils/misc.py +507 -0
  18. tt_bio/_vendor/esm/utils/msa/__init__.py +6 -0
  19. tt_bio/_vendor/esm/utils/msa/filter_sequences.py +85 -0
  20. tt_bio/_vendor/esm/utils/msa/msa.py +509 -0
  21. tt_bio/_vendor/esm/utils/parsing.py +115 -0
  22. tt_bio/_vendor/esm/utils/residue_constants.py +1226 -0
  23. tt_bio/_vendor/esm/utils/sequential_dataclass.py +160 -0
  24. tt_bio/_vendor/esm/utils/structure/__init__.py +0 -0
  25. tt_bio/_vendor/esm/utils/structure/affine3d.py +563 -0
  26. tt_bio/_vendor/esm/utils/structure/aligner.py +104 -0
  27. tt_bio/_vendor/esm/utils/structure/atom_indexer.py +18 -0
  28. tt_bio/_vendor/esm/utils/structure/input_builder.py +257 -0
  29. tt_bio/_vendor/esm/utils/structure/metrics.py +376 -0
  30. tt_bio/_vendor/esm/utils/structure/mmcif_parsing.py +472 -0
  31. tt_bio/_vendor/esm/utils/structure/molecular_complex.py +1228 -0
  32. tt_bio/_vendor/esm/utils/structure/normalize_coordinates.py +82 -0
  33. tt_bio/_vendor/esm/utils/structure/protein_chain.py +1378 -0
  34. tt_bio/_vendor/esm/utils/structure/protein_complex.py +1243 -0
  35. tt_bio/_vendor/esm/utils/structure/protein_structure.py +309 -0
  36. tt_bio/_vendor/esm/utils/system.py +48 -0
  37. tt_bio/_vendor/esm/utils/types.py +36 -0
  38. tt_bio/_vendor/esmfold2_hf/LICENSE +203 -0
  39. tt_bio/_vendor/esmfold2_hf/__init__.py +0 -0
  40. tt_bio/_vendor/esmfold2_hf/configuration_esmfold2.py +302 -0
  41. tt_bio/_vendor/esmfold2_hf/modeling_esmfold2.py +1167 -0
  42. tt_bio/_vendor/esmfold2_hf/modeling_esmfold2_common.py +2762 -0
  43. tt_bio/boltz2.py +5646 -0
  44. tt_bio/boltzgen/__init__.py +28 -0
  45. tt_bio/boltzgen/_config.py +149 -0
  46. tt_bio/boltzgen/adapter.py +207 -0
  47. tt_bio/boltzgen/cli/__init__.py +0 -0
  48. tt_bio/boltzgen/cli/boltzgen.py +2192 -0
  49. tt_bio/boltzgen/data/__init__.py +0 -0
  50. tt_bio/boltzgen/data/cropper.py +409 -0
  51. tt_bio/boltzgen/data/data.py +2122 -0
  52. tt_bio/boltzgen/data/featurizer.py +2198 -0
  53. tt_bio/boltzgen/data/parse/__init__.py +0 -0
  54. tt_bio/boltzgen/data/parse/a3m.py +136 -0
  55. tt_bio/boltzgen/data/parse/mmcif.py +1469 -0
  56. tt_bio/boltzgen/data/parse/pdb_parser.py +144 -0
  57. tt_bio/boltzgen/data/parse/schema.py +2500 -0
  58. tt_bio/boltzgen/data/rmsd_computation.py +118 -0
  59. tt_bio/boltzgen/data/selector.py +724 -0
  60. tt_bio/boltzgen/data/template.py +37 -0
  61. tt_bio/boltzgen/data/tokenizer.py +437 -0
  62. tt_bio/boltzgen/data/write_mmcif.py +471 -0
  63. tt_bio/boltzgen/data/write_pdb.py +137 -0
  64. tt_bio/boltzgen/model/__init__.py +0 -0
  65. tt_bio/boltzgen/model/geometry.py +380 -0
  66. tt_bio/boltzgen/model/layers/__init__.py +0 -0
  67. tt_bio/boltzgen/model/layers/attention.py +134 -0
  68. tt_bio/boltzgen/model/layers/confidence_utils.py +443 -0
  69. tt_bio/boltzgen/model/layers/dropout.py +95 -0
  70. tt_bio/boltzgen/model/layers/initialize.py +84 -0
  71. tt_bio/boltzgen/model/layers/relative.py +58 -0
  72. tt_bio/boltzgen/model/layers/transition.py +73 -0
  73. tt_bio/boltzgen/model/models/__init__.py +0 -0
  74. tt_bio/boltzgen/model/models/boltz.py +890 -0
  75. tt_bio/boltzgen/model/modules/__init__.py +0 -0
  76. tt_bio/boltzgen/model/modules/affinity.py +222 -0
  77. tt_bio/boltzgen/model/modules/confidence.py +632 -0
  78. tt_bio/boltzgen/model/modules/diffusion.py +418 -0
  79. tt_bio/boltzgen/model/modules/diffusion_conditioning.py +117 -0
  80. tt_bio/boltzgen/model/modules/encoders.py +741 -0
  81. tt_bio/boltzgen/model/modules/inverse_fold.py +751 -0
  82. tt_bio/boltzgen/model/modules/masker.py +253 -0
  83. tt_bio/boltzgen/model/modules/scatter_utils.py +140 -0
  84. tt_bio/boltzgen/model/modules/transformers.py +263 -0
  85. tt_bio/boltzgen/model/modules/trunk.py +567 -0
  86. tt_bio/boltzgen/model/modules/utils.py +228 -0
  87. tt_bio/boltzgen/progress.py +342 -0
  88. tt_bio/boltzgen/task/__init__.py +0 -0
  89. tt_bio/boltzgen/task/analyze/analyze.py +1489 -0
  90. tt_bio/boltzgen/task/analyze/analyze_utils.py +1260 -0
  91. tt_bio/boltzgen/task/filter/__init__.py +0 -0
  92. tt_bio/boltzgen/task/filter/filter.py +1413 -0
  93. tt_bio/boltzgen/task/filter/seqplot_utils.py +507 -0
  94. tt_bio/boltzgen/task/predict/__init__.py +0 -0
  95. tt_bio/boltzgen/task/predict/data_from_generated.py +882 -0
  96. tt_bio/boltzgen/task/predict/data_from_yaml.py +449 -0
  97. tt_bio/boltzgen/task/predict/data_ligands.py +410 -0
  98. tt_bio/boltzgen/task/predict/data_protein_binder.py +588 -0
  99. tt_bio/boltzgen/task/predict/loading_utils.py +45 -0
  100. tt_bio/boltzgen/task/predict/predict.py +117 -0
  101. tt_bio/boltzgen/task/predict/writer.py +526 -0
  102. tt_bio/boltzgen/task/task.py +19 -0
  103. tt_bio/boltzgen/utils/__init__.py +0 -0
  104. tt_bio/boltzgen/utils/quiet.py +18 -0
  105. tt_bio/data/__init__.py +0 -0
  106. tt_bio/data/const.py +3593 -0
  107. tt_bio/data/featurizer.py +2353 -0
  108. tt_bio/data/mol.py +1145 -0
  109. tt_bio/data/msa.py +295 -0
  110. tt_bio/data/pad.py +84 -0
  111. tt_bio/data/parse.py +3647 -0
  112. tt_bio/data/tokenize.py +452 -0
  113. tt_bio/data/types.py +784 -0
  114. tt_bio/data/write.py +474 -0
  115. tt_bio/distributed.py +649 -0
  116. tt_bio/energy.py +337 -0
  117. tt_bio/esmc.py +571 -0
  118. tt_bio/esmfold2.py +1316 -0
  119. tt_bio/esmfold2_runtime.py +488 -0
  120. tt_bio/install_system_deps.py +124 -0
  121. tt_bio/main.py +2207 -0
  122. tt_bio/msa_server.py +173 -0
  123. tt_bio/progress.py +389 -0
  124. tt_bio/protenix.py +1261 -0
  125. tt_bio/protenix_data.py +531 -0
  126. tt_bio/protenix_weights.py +137 -0
  127. tt_bio/reference.py +1542 -0
  128. tt_bio/runtime.py +113 -0
  129. tt_bio/tenstorrent.py +3115 -0
  130. tt_bio/worker.py +835 -0
  131. tt_bio-0.2.0.dist-info/METADATA +41 -0
  132. tt_bio-0.2.0.dist-info/RECORD +137 -0
  133. tt_bio-0.2.0.dist-info/WHEEL +5 -0
  134. tt_bio-0.2.0.dist-info/entry_points.txt +2 -0
  135. tt_bio-0.2.0.dist-info/licenses/LICENSE +22 -0
  136. tt_bio-0.2.0.dist-info/licenses/NOTICE +39 -0
  137. tt_bio-0.2.0.dist-info/top_level.txt +1 -0
tt_bio/__init__.py ADDED
@@ -0,0 +1,6 @@
1
+ from importlib.metadata import PackageNotFoundError, version
2
+
3
+ try:
4
+ __version__ = version("tt-bio")
5
+ except PackageNotFoundError: # running from a source tree, not an installed dist
6
+ __version__ = "0+unknown"
File without changes
@@ -0,0 +1,9 @@
1
+ **License (MIT)**
2
+
3
+ Copyright 2026 Chan Zuckerberg Biohub, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
File without changes
File without changes
@@ -0,0 +1,44 @@
1
+ # Vendored from github.com/Biohub/esm @ b6b0e88 (MIT, Copyright 2026 Chan Zuckerberg
2
+ # Biohub, Inc.; see tt_bio/_vendor/esm/LICENSE). Modified: absolute `esm.` imports
3
+ # rewritten to `tt_bio._vendor.esm.` for in-tree vendoring.
4
+ from tt_bio._vendor.esm.models.esmfold2.conformers import load_ccd
5
+ from tt_bio._vendor.esm.models.esmfold2.constants import ELEMENT_NUMBER_TO_SYMBOL
6
+ from tt_bio._vendor.esm.models.esmfold2.prepare_input import ChainInfo, prepare_esmfold2_input
7
+ from tt_bio._vendor.esm.models.esmfold2.processor import ESMFold2InputBuilder, clean_esmfold2_input
8
+ from tt_bio._vendor.esm.models.esmfold2.types import (
9
+ MSA,
10
+ CovalentBond,
11
+ DistogramConditioning,
12
+ DNAInput,
13
+ LigandInput,
14
+ Modification,
15
+ ProteinInput,
16
+ RNAInput,
17
+ StructurePredictionInput,
18
+ )
19
+ from tt_bio._vendor.esm.utils.structure.molecular_complex import (
20
+ MolecularComplex,
21
+ MolecularComplexMetadata,
22
+ MolecularComplexResult,
23
+ )
24
+
25
+ __all__ = [
26
+ "ChainInfo",
27
+ "CovalentBond",
28
+ "DistogramConditioning",
29
+ "DNAInput",
30
+ "ELEMENT_NUMBER_TO_SYMBOL",
31
+ "ESMFold2InputBuilder",
32
+ "LigandInput",
33
+ "MSA",
34
+ "Modification",
35
+ "MolecularComplex",
36
+ "MolecularComplexMetadata",
37
+ "MolecularComplexResult",
38
+ "ProteinInput",
39
+ "RNAInput",
40
+ "StructurePredictionInput",
41
+ "clean_esmfold2_input",
42
+ "load_ccd",
43
+ "prepare_esmfold2_input",
44
+ ]
@@ -0,0 +1,294 @@
1
+ # Vendored from github.com/Biohub/esm @ b6b0e88 (MIT, Copyright 2026 Chan Zuckerberg
2
+ # Biohub, Inc.; see tt_bio/_vendor/esm/LICENSE). Modified: absolute `esm.` imports
3
+ # rewritten to `tt_bio._vendor.esm.` for in-tree vendoring.
4
+ """CCD conformer loading utilities.
5
+
6
+ Loads idealized conformer coordinates from a CCD pickle file containing RDKit molecules.
7
+ Conformer priority follows AF3 Section 2.8: Computed > Ideal > first available.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import os
13
+ import pickle
14
+ from pathlib import Path
15
+
16
+ import numpy as np
17
+ from huggingface_hub import hf_hub_download
18
+
19
+ from tt_bio._vendor.esm.models.esmfold2.constants import RES_TYPE_TO_CCD
20
+
21
+ if os.environ.get("ESMCFOLD_CCD_PATH"):
22
+ CCD_PICKLE_PATH = Path(os.environ["ESMCFOLD_CCD_PATH"])
23
+ else:
24
+ CCD_PICKLE_PATH = None
25
+
26
+
27
+ # Lazily loaded CCD dictionary
28
+ _CCD_MOLECULES: dict | None = None
29
+
30
+ # Caches
31
+ _CCD_CONFORMERS: dict[str, dict[str, np.ndarray]] = {}
32
+ _CCD_ATOM_CACHE: dict[str, list[tuple[str, str, int]]] = {}
33
+ _CCD_BONDS_CACHE: dict[str, list[tuple[str, str]]] = {}
34
+ _CCD_LEAVING_ATOMS_CACHE: dict[str, set[str]] = {}
35
+ _IDEALIZED_POS_CACHE: dict[tuple[int, str], np.ndarray | None] = {}
36
+ _LIGAND_IDEALIZED_POS_CACHE: dict[tuple[str, str], np.ndarray | None] = {}
37
+
38
+
39
+ def load_ccd(cache_dir: Path | str | None = None) -> dict:
40
+ """Load CCD molecules from pickle file, downloading if needed.
41
+
42
+ Args:
43
+ cache_dir: Directory to cache the downloaded CCD pickle.
44
+ If None, uses CCD_PICKLE_PATH env var or downloads to ~/.cache/esmcfold/.
45
+ """
46
+ global _CCD_MOLECULES
47
+ if _CCD_MOLECULES is not None:
48
+ return _CCD_MOLECULES
49
+
50
+ # Determine pickle path
51
+ if CCD_PICKLE_PATH is not None and CCD_PICKLE_PATH.exists():
52
+ pkl_path = CCD_PICKLE_PATH
53
+ elif cache_dir is not None:
54
+ cache_dir = Path(cache_dir)
55
+ cache_dir.mkdir(parents=True, exist_ok=True)
56
+ pkl_path = cache_dir / "ccd.pkl"
57
+ else:
58
+ try:
59
+ pkl_path = Path(
60
+ hf_hub_download(repo_id="biohub/ESMFold2", filename="ccd.pkl")
61
+ )
62
+ except Exception as e:
63
+ raise FileNotFoundError(
64
+ f"Failed to download CCD pickle file from Hugging Face repository: {e}"
65
+ )
66
+
67
+ if not pkl_path.exists():
68
+ raise FileNotFoundError(
69
+ f"CCD pickle file not found: {pkl_path}. Please set the ESMCFOLD_CCD_PATH environment variable to the path of a valid CCD pickle file or download the file from the Hugging Face repository."
70
+ )
71
+
72
+ print(f"Loading CCD dictionary from {pkl_path}")
73
+ with open(pkl_path, "rb") as f:
74
+ _CCD_MOLECULES = pickle.load(f)
75
+
76
+ if _CCD_MOLECULES is None:
77
+ _CCD_MOLECULES = {}
78
+
79
+ return _CCD_MOLECULES
80
+
81
+
82
+ def _get_ccd_molecules() -> dict:
83
+ """Get CCD molecules, loading lazily on first call."""
84
+ global _CCD_MOLECULES
85
+ if _CCD_MOLECULES is None:
86
+ return load_ccd()
87
+ return _CCD_MOLECULES
88
+
89
+
90
+ def _get_ccd_mol_with_significant_h(comp_id: str):
91
+ """Get CCD molecule with only chemically significant hydrogens.
92
+
93
+ Returns (mol, conformer) tuple or (None, None) if not available.
94
+ """
95
+ ccd = _get_ccd_molecules()
96
+ if comp_id not in ccd:
97
+ return None, None
98
+
99
+ mol = ccd[comp_id]
100
+ if mol.GetNumConformers() == 0:
101
+ return None, None
102
+
103
+ # Find the "Computed" conformer (RDKit ETKDGv3), fall back to "Ideal"
104
+ conf_idx = 0
105
+ for i, c in enumerate(mol.GetConformers()):
106
+ props = c.GetPropsAsDict()
107
+ if props.get("name") == "Computed":
108
+ conf_idx = i
109
+ break
110
+ else:
111
+ for i, c in enumerate(mol.GetConformers()):
112
+ props = c.GetPropsAsDict()
113
+ if props.get("name") == "Ideal":
114
+ conf_idx = i
115
+ break
116
+
117
+ from rdkit import Chem
118
+
119
+ mol_no_h = Chem.RemoveHs(mol, sanitize=False)
120
+
121
+ if mol_no_h.GetNumConformers() == 0:
122
+ return None, None
123
+
124
+ return mol_no_h, mol_no_h.GetConformer(
125
+ min(conf_idx, mol_no_h.GetNumConformers() - 1)
126
+ )
127
+
128
+
129
+ def get_ccd_conformer(comp_id: str) -> dict[str, np.ndarray] | None:
130
+ """Get idealized conformer as dict of atom_name -> position [3].
131
+
132
+ Conformer priority: Computed > Ideal > first available.
133
+ """
134
+ if comp_id in _CCD_CONFORMERS:
135
+ cached = _CCD_CONFORMERS[comp_id]
136
+ return cached if cached else None
137
+
138
+ mol, conf = _get_ccd_mol_with_significant_h(comp_id)
139
+ if mol is None or conf is None:
140
+ _CCD_CONFORMERS[comp_id] = {}
141
+ return None
142
+
143
+ conformer: dict[str, np.ndarray] = {}
144
+ for atom in mol.GetAtoms():
145
+ props = atom.GetPropsAsDict()
146
+ atom_name = props.get("name")
147
+ if not isinstance(atom_name, str) or not atom_name:
148
+ continue
149
+ idx = atom.GetIdx()
150
+ pos = conf.GetAtomPosition(idx)
151
+ conformer[atom_name] = np.array([pos.x, pos.y, pos.z], dtype=np.float32)
152
+
153
+ _CCD_CONFORMERS[comp_id] = conformer
154
+ return conformer if conformer else None
155
+
156
+
157
+ def get_idealized_atom_pos(res_type: int, atom_name: str) -> np.ndarray | None:
158
+ """Get idealized position for a standard residue atom.
159
+
160
+ Uses res_type index to look up CCD component, then returns position.
161
+ Returns None if not found.
162
+ """
163
+ cache_key = (res_type, atom_name)
164
+ if cache_key in _IDEALIZED_POS_CACHE:
165
+ return _IDEALIZED_POS_CACHE[cache_key]
166
+
167
+ comp_id = RES_TYPE_TO_CCD.get(res_type)
168
+ if comp_id:
169
+ ccd_conformer = get_ccd_conformer(comp_id)
170
+ if ccd_conformer and atom_name in ccd_conformer:
171
+ pos = ccd_conformer[atom_name]
172
+ _IDEALIZED_POS_CACHE[cache_key] = pos
173
+ return pos
174
+
175
+ _IDEALIZED_POS_CACHE[cache_key] = None
176
+ return None
177
+
178
+
179
+ def get_ligand_idealized_atom_pos(res_name: str, atom_name: str) -> np.ndarray | None:
180
+ """Get idealized position for a ligand/modified residue atom.
181
+
182
+ Returns None if not found.
183
+ """
184
+ cache_key = (res_name, atom_name)
185
+ if cache_key in _LIGAND_IDEALIZED_POS_CACHE:
186
+ return _LIGAND_IDEALIZED_POS_CACHE[cache_key]
187
+
188
+ ccd_conformer = get_ccd_conformer(res_name)
189
+ if ccd_conformer and atom_name in ccd_conformer:
190
+ pos = ccd_conformer[atom_name]
191
+ _LIGAND_IDEALIZED_POS_CACHE[cache_key] = pos
192
+ return pos
193
+
194
+ _LIGAND_IDEALIZED_POS_CACHE[cache_key] = None
195
+ return None
196
+
197
+
198
+ def get_ligand_ccd_atoms_with_charges(
199
+ comp_id: str,
200
+ ) -> list[tuple[str, str, int]] | None:
201
+ """Get list of (atom_name, element, charge) for a CCD component.
202
+
203
+ Uses RDKit RemoveHs(sanitize=False) to keep chemically significant hydrogens.
204
+ Returns None if CCD data not available.
205
+ """
206
+ if comp_id in _CCD_ATOM_CACHE:
207
+ cached = _CCD_ATOM_CACHE[comp_id]
208
+ return cached if cached else None
209
+
210
+ mol, _ = _get_ccd_mol_with_significant_h(comp_id)
211
+ if mol is None:
212
+ _CCD_ATOM_CACHE[comp_id] = []
213
+ return None
214
+
215
+ atoms: list[tuple[str, str, int]] = []
216
+ for atom in mol.GetAtoms():
217
+ props = atom.GetPropsAsDict()
218
+ atom_name = props.get("name")
219
+ if not isinstance(atom_name, str) or not atom_name:
220
+ continue
221
+ element = atom.GetSymbol()
222
+ charge = atom.GetFormalCharge()
223
+ atoms.append((atom_name, element, charge))
224
+
225
+ _CCD_ATOM_CACHE[comp_id] = atoms
226
+ return atoms if atoms else None
227
+
228
+
229
+ def get_ligand_ccd_bonds(comp_id: str) -> list[tuple[str, str]] | None:
230
+ """Get list of (atom1_name, atom2_name) bonds for a CCD component.
231
+
232
+ Returns None if CCD data not available.
233
+ """
234
+ if comp_id in _CCD_BONDS_CACHE:
235
+ cached = _CCD_BONDS_CACHE[comp_id]
236
+ return cached if cached else None
237
+
238
+ mol, _ = _get_ccd_mol_with_significant_h(comp_id)
239
+ if mol is None:
240
+ _CCD_BONDS_CACHE[comp_id] = []
241
+ return None
242
+
243
+ # Get included atom names
244
+ included_atoms = set()
245
+ for atom in mol.GetAtoms():
246
+ props = atom.GetPropsAsDict()
247
+ atom_name = props.get("name")
248
+ if isinstance(atom_name, str) and atom_name:
249
+ included_atoms.add(atom_name)
250
+
251
+ bonds: list[tuple[str, str]] = []
252
+ for bond in mol.GetBonds():
253
+ a1 = bond.GetBeginAtom()
254
+ a2 = bond.GetEndAtom()
255
+ n1 = a1.GetPropsAsDict().get("name")
256
+ n2 = a2.GetPropsAsDict().get("name")
257
+ if (
258
+ isinstance(n1, str)
259
+ and isinstance(n2, str)
260
+ and n1
261
+ and n2
262
+ and n1 in included_atoms
263
+ and n2 in included_atoms
264
+ ):
265
+ bonds.append((n1, n2))
266
+
267
+ _CCD_BONDS_CACHE[comp_id] = bonds
268
+ return bonds if bonds else None
269
+
270
+
271
+ def get_ccd_leaving_atoms(comp_id: str) -> set[str]:
272
+ """Get set of atom names marked as leaving atoms in CCD.
273
+
274
+ Leaving atoms are removed during polymerization (e.g., OP3 in nucleotides).
275
+ """
276
+ if comp_id in _CCD_LEAVING_ATOMS_CACHE:
277
+ return _CCD_LEAVING_ATOMS_CACHE[comp_id]
278
+
279
+ ccd = _get_ccd_molecules()
280
+ if comp_id not in ccd:
281
+ _CCD_LEAVING_ATOMS_CACHE[comp_id] = set()
282
+ return set()
283
+
284
+ mol = ccd[comp_id]
285
+ leaving_atoms = set()
286
+ for atom in mol.GetAtoms():
287
+ if atom.HasProp("leaving_atom"):
288
+ if atom.GetProp("leaving_atom") == "1":
289
+ name = atom.GetProp("name") if atom.HasProp("name") else ""
290
+ if name:
291
+ leaving_atoms.add(name)
292
+
293
+ _CCD_LEAVING_ATOMS_CACHE[comp_id] = leaving_atoms
294
+ return leaving_atoms