File size: 8,557 Bytes
f2ad668
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from itertools import groupby
from typing import Any

import numpy as np
import torch

from .esmfold2_constants import ELEMENT_NUMBER_TO_SYMBOL, MOL_TYPE_NONPOLYMER
from .esmfold2_molecular_complex import (
    MolecularComplex,
    MolecularComplexMetadata,
)


def get_element_symbol(atomic_num: int) -> str:
    return ELEMENT_NUMBER_TO_SYMBOL.get(atomic_num, "X")


def build_molecular_complex_from_features(

    coords: torch.Tensor,

    plddt: torch.Tensor,

    atom_mask: torch.Tensor,

    ref_element: torch.Tensor,

    ref_atom_name_chars: torch.Tensor,

    chain_infos: list,

    complex_id: str,

) -> MolecularComplex:
    """Construct a MolecularComplex from feature-dict tensors and chain metadata.



    Non-polymer chains (ligands) collapse all per-atom tokens into a single

    residue token whose pLDDT is the per-token average and whose hetero flag

    is True.

    """
    mask_np = atom_mask.bool().cpu().numpy()
    coords_np = coords.float().cpu().numpy()
    name_chars_np = ref_atom_name_chars.cpu().numpy()
    elements_np = ref_element.cpu().numpy()
    plddt_np = plddt.float().cpu().numpy()

    sequence_tokens: list[str] = []
    chain_ids_per_token: list[int] = []
    token_to_atoms: list[list[int]] = []
    confidence: list[float] = []
    flat_positions: list[list[float]] = []
    flat_elements: list[str] = []
    flat_names: list[str] = []
    flat_hetero: list[bool] = []

    chain_lookup: dict[int, str] = {}
    entity_info: dict[int, str] = {}
    out_atom_cursor = 0

    for ci in chain_infos:
        chain_lookup[ci.asym_id] = ci.chain_id
        is_nonpolymer = ci.mol_type == MOL_TYPE_NONPOLYMER
        entity_info[ci.entity_id] = "non-polymer" if is_nonpolymer else "polymer"

        if is_nonpolymer:
            residue_name = ci.tokens[0].residue_name if ci.tokens else "LIG"
            sequence_tokens.append(residue_name)
            chain_ids_per_token.append(ci.asym_id)
            avg_plddt = (
                float(np.mean([plddt_np[ti.token_index] for ti in ci.tokens]))
                if ci.tokens
                else 0.0
            )
            confidence.append(avg_plddt)
            token_atom_start = out_atom_cursor
            for ti in ci.tokens:
                for atom_idx in range(ti.atom_start, ti.atom_start + ti.atom_count):
                    if not mask_np[atom_idx]:
                        continue
                    flat_positions.append(coords_np[atom_idx].tolist())
                    flat_elements.append(get_element_symbol(int(elements_np[atom_idx])))
                    chars = name_chars_np[atom_idx]
                    name = "".join(
                        chr(int(c) + 32) for c in chars if int(c) != 0
                    ).strip()
                    flat_names.append(name)
                    flat_hetero.append(True)
                    out_atom_cursor += 1
            token_to_atoms.append([token_atom_start, out_atom_cursor])
            continue

        # Atom-tokenized modified residues (HYP, MSE, ...) span multiple
        # tokens per residue; collapse them back to one mmCIF residue.
        for _residue_index, ti_iter in groupby(
            ci.tokens, key=lambda t: t.residue_index
        ):
            ti_group = list(ti_iter)
            sequence_tokens.append(ti_group[0].residue_name)
            chain_ids_per_token.append(ci.asym_id)
            confidence.append(
                float(np.mean([plddt_np[ti.token_index] for ti in ti_group]))
            )
            token_atom_start = out_atom_cursor
            for ti in ti_group:
                for atom_idx in range(ti.atom_start, ti.atom_start + ti.atom_count):
                    if not mask_np[atom_idx]:
                        continue
                    flat_positions.append(coords_np[atom_idx].tolist())
                    flat_elements.append(get_element_symbol(int(elements_np[atom_idx])))
                    chars = name_chars_np[atom_idx]
                    name = "".join(
                        chr(int(c) + 32) for c in chars if int(c) != 0
                    ).strip()
                    flat_names.append(name)
                    flat_hetero.append(False)
                    out_atom_cursor += 1
            token_to_atoms.append([token_atom_start, out_atom_cursor])

    return MolecularComplex(
        id=complex_id,
        sequence=sequence_tokens,
        atom_positions=np.array(flat_positions, dtype=np.float32).reshape(-1, 3),
        atom_elements=np.array(flat_elements, dtype=object),
        token_to_atoms=np.array(token_to_atoms, dtype=np.int32).reshape(-1, 2),
        chain_id=np.array(chain_ids_per_token, dtype=np.int64),
        plddt=np.array(confidence, dtype=np.float32),
        atom_names=np.array(flat_names, dtype=object),
        atom_hetero=np.array(flat_hetero, dtype=bool),
        metadata=MolecularComplexMetadata(
            entity_lookup=entity_info,
            chain_lookup=chain_lookup,
            assembly_composition=None,
        ),
    )


def build_molecular_complex(

    structure: Any, coords: torch.Tensor, plddt: torch.Tensor, complex_id: str

) -> MolecularComplex:
    """Directly constructs a MolecularComplex from model outputs without intermediate files.



    Args:

        structure: Object with .chains, .residues, .atoms numpy structured arrays.

        coords: [N_atoms, 3] predicted atom coordinates.

        plddt: [N_residues] per-residue confidence scores.

        complex_id: Identifier string for the resulting complex.

    """
    flat_positions = []
    flat_elements = []
    flat_names = []
    flat_hetero = []

    sequence_tokens = []
    token_to_atoms = []
    chain_ids_per_token = []
    confidence_scores = []

    chain_lookup = {}
    entity_info = {}

    global_atom_cursor = 0
    global_res_cursor = 0
    atom_array_idx = 0

    for chain in structure.chains:
        chain_idx_numeric = chain["asym_id"]
        chain_name_str = str(chain["name"])
        mol_type = chain["mol_type"]

        chain_lookup[chain_idx_numeric] = chain_name_str
        entity_info[chain["entity_id"]] = (
            "polymer" if mol_type != MOL_TYPE_NONPOLYMER else "non-polymer"
        )

        res_start = chain["res_idx"]
        res_end = chain["res_idx"] + chain["res_num"]
        residues = structure.residues[res_start:res_end]

        for residue in residues:
            res_name = str(residue["name"])

            sequence_tokens.append(res_name)
            chain_ids_per_token.append(chain_idx_numeric)

            score = plddt[global_res_cursor].item()
            confidence_scores.append(score)
            token_start_idx = atom_array_idx

            atom_start = residue["atom_idx"]
            atom_end = residue["atom_idx"] + residue["atom_num"]
            atoms = structure.atoms[atom_start:atom_end]

            for atom in atoms:
                if not atom["is_present"]:
                    continue

                pos = coords[global_atom_cursor].tolist()
                flat_positions.append(pos)

                elem = get_element_symbol(atom["element"].item())
                flat_elements.append(elem)

                raw_name = atom["name"]
                if hasattr(raw_name, "tolist"):
                    raw_name = raw_name.tolist()
                name_str = "".join([chr(c + 32) for c in raw_name if c != 0])
                flat_names.append(name_str)

                flat_hetero.append(mol_type == MOL_TYPE_NONPOLYMER)

                global_atom_cursor += 1
                atom_array_idx += 1

            token_to_atoms.append([token_start_idx, atom_array_idx])
            global_res_cursor += 1

    return MolecularComplex(
        id=complex_id,
        sequence=sequence_tokens,
        atom_positions=np.array(flat_positions, dtype=np.float32),
        atom_elements=np.array(flat_elements, dtype=object),
        token_to_atoms=np.array(token_to_atoms, dtype=np.int32),
        chain_id=np.array(chain_ids_per_token, dtype=np.int64),
        plddt=np.array(confidence_scores, dtype=np.float32),
        atom_names=np.array(flat_names, dtype=object),
        atom_hetero=np.array(flat_hetero, dtype=bool),
        metadata=MolecularComplexMetadata(
            entity_lookup=entity_info,
            chain_lookup=chain_lookup,
            assembly_composition=None,
        ),
    )