|
24 | 24 | class Complex: |
25 | 25 | def __init__( |
26 | 26 | self, |
27 | | - chains: Mapping[ |
28 | | - str | tuple[str, ...], Protein | DNA | RNA | Ligand |
29 | | - ] |
30 | | - | None = None, |
| 27 | + chains: Mapping[str, Protein | DNA | RNA | Ligand] | None = None, |
31 | 28 | name: bytes | str | None = None, |
32 | 29 | ): |
33 | | - expanded: dict[str, Protein | DNA | RNA | Ligand] = {} |
34 | | - groups: list[tuple[str, ...]] = [] |
| 30 | + collected: dict[str, Protein | DNA | RNA | Ligand] = {} |
35 | 31 | if chains is not None: |
36 | 32 | for key, value in chains.items(): |
37 | | - ids = (key,) if isinstance(key, str) else key |
38 | | - if not isinstance(ids, tuple) or not all( |
39 | | - isinstance(cid, str) for cid in ids |
40 | | - ): |
41 | | - raise TypeError( |
42 | | - f"chain id must be str or tuple[str, ...]; got {key!r}" |
43 | | - ) |
44 | | - if len(ids) == 0: |
45 | | - raise ValueError("tuple chain id must be non-empty") |
46 | | - for cid in ids: |
47 | | - if cid in expanded: |
48 | | - raise ValueError(f"duplicate chain id: {cid!r}") |
49 | | - expanded[cid] = value |
50 | | - groups.append(tuple(ids)) |
51 | | - self._chains = dict(sorted(expanded.items())) |
52 | | - self._id_groups: list[tuple[str, ...]] = sorted(groups, key=lambda g: g[0]) |
| 33 | + if not isinstance(key, str): |
| 34 | + raise TypeError(f"chain id must be str; got {key!r}") |
| 35 | + collected[key] = value |
| 36 | + self._chains = dict(sorted(collected.items())) |
53 | 37 | self._templates: "Sequence[Protein | Complex | Template]" = () |
54 | 38 | self.name = name |
55 | 39 |
|
@@ -89,17 +73,6 @@ def set_templates( |
89 | 73 | def get_chains(self) -> Mapping[str, Protein | DNA | RNA | Ligand]: |
90 | 74 | return MappingProxyType(self._chains) |
91 | 75 |
|
92 | | - def get_id_groups( |
93 | | - self, |
94 | | - ) -> "list[tuple[tuple[str, ...], Protein | DNA | RNA | Ligand]]": |
95 | | - """Return ordered (chain_ids, chain) pairs grouped by entity. |
96 | | -
|
97 | | - Each group's ``chain_ids`` is the tuple originally passed to the |
98 | | - constructor (a 1-tuple for scalar keys), and the chain object is |
99 | | - shared across all ids in the group. |
100 | | - """ |
101 | | - return [(ids, self._chains[ids[0]]) for ids in self._id_groups] |
102 | | - |
103 | 76 | def get_proteins(self) -> Mapping[str, Protein]: |
104 | 77 | return MappingProxyType( |
105 | 78 | {k: v for k, v in self._chains.items() if isinstance(v, Protein)} |
@@ -143,18 +116,8 @@ def get_ligand(self, chain_id: str) -> Ligand: |
143 | 116 | def set_chain( |
144 | 117 | self, chain_id: str, value: Protein | DNA | RNA | Ligand |
145 | 118 | ) -> "Complex": |
146 | | - new_groups: list[tuple[str, ...]] = [] |
147 | | - for ids in self._id_groups: |
148 | | - if chain_id in ids: |
149 | | - remaining = tuple(i for i in ids if i != chain_id) |
150 | | - if remaining: |
151 | | - new_groups.append(remaining) |
152 | | - else: |
153 | | - new_groups.append(ids) |
154 | | - new_groups.append((chain_id,)) |
155 | 119 | self._chains[chain_id] = value |
156 | 120 | self._chains = dict(sorted(self._chains.items())) |
157 | | - self._id_groups = sorted(new_groups, key=lambda g: g[0]) |
158 | 121 | return self |
159 | 122 |
|
160 | 123 | def __rand__(self, left: "Complex | Protein | str") -> "Complex": |
@@ -184,9 +147,6 @@ def __and__(self, right: "Complex | Protein | str") -> "Complex": |
184 | 147 | f"Trying to combine two sets of chains with overlapping chain ids: {overlapping_chain_ids}" |
185 | 148 | ) |
186 | 149 | self._chains = dict(sorted((self._chains | right._chains).items())) |
187 | | - self._id_groups = sorted( |
188 | | - self._id_groups + right._id_groups, key=lambda g: g[0] |
189 | | - ) |
190 | 150 | return self |
191 | 151 |
|
192 | 152 | @overload |
@@ -309,12 +269,9 @@ def from_string( |
309 | 269 | ) |
310 | 270 |
|
311 | 271 | def copy(self) -> "Complex": |
312 | | - chains_copy: dict[ |
313 | | - str | tuple[str, ...], Protein | DNA | RNA | Ligand |
314 | | - ] = {} |
315 | | - for ids in self._id_groups: |
316 | | - value = self._chains[ids[0]].copy() |
317 | | - chains_copy[ids if len(ids) > 1 else ids[0]] = value |
| 272 | + chains_copy: dict[str, Protein | DNA | RNA | Ligand] = { |
| 273 | + chain_id: chain.copy() for chain_id, chain in self._chains.items() |
| 274 | + } |
318 | 275 | return Complex(chains=chains_copy, name=self._name) |
319 | 276 |
|
320 | 277 | def _assert_valid_templates(self): |
|
0 commit comments