Skip to content

Commit 11921a9

Browse files
steps-reclaude
andcommitted
Translate bond types once per distinct type in set_structure()
`_set_intra_residue_bonds()` looped over every bond to map its `BondType` to the `chem_comp_bond` representation, although a structure contains only a handful of distinct bond types. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent e64b90b commit 11921a9

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

src/biotite/structure/io/pdbx/convert.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,15 +1295,22 @@ def _set_intra_residue_bonds(
12951295
bond_array = _filter_bonds(array, "intra")
12961296
if len(bond_array) == 0:
12971297
return None
1298-
value_order = np.zeros(len(bond_array), dtype="U4")
1299-
aromatic_flag = np.zeros(len(bond_array), dtype="U1")
1300-
for i, bond_type in enumerate(bond_array[:, 2]):
1298+
# A structure contains far fewer distinct bond types than bonds,
1299+
# hence the bond type is translated once per distinct type
1300+
unique_bond_types, type_indices = np.unique(bond_array[:, 2], return_inverse=True)
1301+
unique_value_order = np.zeros(len(unique_bond_types), dtype="U4")
1302+
unique_aromatic_flag = np.zeros(len(unique_bond_types), dtype="U1")
1303+
for i, bond_type in enumerate(unique_bond_types):
13011304
if bond_type == BondType.ANY:
13021305
# ANY bonds will be masked anyway, no need to set the value
13031306
continue
1304-
order, aromatic = _get_chem_comp_bond_type(bond_type)
1305-
value_order[i] = order
1306-
aromatic_flag[i] = aromatic
1307+
unique_value_order[i], unique_aromatic_flag[i] = _get_chem_comp_bond_type(
1308+
bond_type
1309+
)
1310+
# The shape of the inverse indices depends on the NumPy version
1311+
type_indices = type_indices.reshape(-1)
1312+
value_order = unique_value_order[type_indices]
1313+
aromatic_flag = unique_aromatic_flag[type_indices]
13071314
any_mask = bond_array[:, 2] == BondType.ANY
13081315

13091316
# Remove already existing residue and atom name combinations

0 commit comments

Comments
 (0)