File size: 7,698 Bytes
6019d52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38bce11
6019d52
 
 
 
 
 
 
 
 
 
 
 
 
 
38bce11
 
6019d52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38bce11
6019d52
 
 
 
 
 
 
 
 
 
 
 
38bce11
6019d52
 
 
 
 
 
 
 
 
38bce11
 
6019d52
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Cache pinned frozen-encoder embeddings for a MitoInteract sample."""

from __future__ import annotations

import argparse
import json
import time
from collections import defaultdict
from pathlib import Path

import numpy as np
import torch
from transformers import AutoModel, AutoTokenizer

DEFAULT_PROTEIN_MODEL = "facebook/esm2_t12_35M_UR50D"
DEFAULT_PROTEIN_REVISION = "6fbf070e65b0b7291e7bbcd451118c216cff79d8"
DEFAULT_LIGAND_MODEL = "DeepChem/ChemBERTa-77M-MLM"
DEFAULT_LIGAND_REVISION = "ed8a5374f2024ec8da53760af91a33fb8f6a15ff"


def read_rows(path: Path, limit: int | None) -> list[dict]:
    rows = []
    with path.open() as handle:
        for line in handle:
            if line.strip():
                rows.append(json.loads(line))
            if limit and len(rows) >= limit:
                break
    return rows


def masked_mean(
    last_hidden: torch.Tensor, attention: torch.Tensor, special: torch.Tensor
) -> torch.Tensor:
    mask = attention.bool() & ~special.bool()
    weights = mask.unsqueeze(-1).to(last_hidden.dtype)
    return (last_hidden * weights).sum(dim=1) / weights.sum(dim=1).clamp_min(1)


def encode_texts(
    model, tokenizer, texts: list[str], batch_size: int, device: torch.device
) -> np.ndarray:
    outputs = []
    for start in range(0, len(texts), batch_size):
        batch = texts[start : start + batch_size]
        encoded = tokenizer(
            batch,
            padding=True,
            truncation=True,
            max_length=min(getattr(tokenizer, "model_max_length", 512), 512),
            return_special_tokens_mask=True,
            return_tensors="pt",
        )
        special = encoded.pop("special_tokens_mask")
        encoded = {key: value.to(device) for key, value in encoded.items()}
        with torch.inference_mode():
            hidden = model(**encoded).last_hidden_state
        pooled = masked_mean(hidden, encoded["attention_mask"], special.to(device))
        outputs.append(pooled.float().cpu().numpy())
    return np.concatenate(outputs, axis=0)


def encode_proteins(
    model,
    tokenizer,
    entities: list[tuple[str, str]],
    batch_size: int,
    device: torch.device,
    chunk_residues: int,
) -> dict[str, np.ndarray]:
    chunks: list[str] = []
    owners: list[str] = []
    weights: list[int] = []
    for entity_id, sequence in entities:
        for start in range(0, len(sequence), chunk_residues):
            chunk = sequence[start : start + chunk_residues]
            chunks.append(chunk)
            owners.append(entity_id)
            weights.append(len(chunk))

    chunk_embeddings = []
    for start in range(0, len(chunks), batch_size):
        batch = chunks[start : start + batch_size]
        encoded = tokenizer(
            batch,
            padding=True,
            truncation=True,
            max_length=chunk_residues + 2,
            return_special_tokens_mask=True,
            return_tensors="pt",
        )
        special = encoded.pop("special_tokens_mask")
        encoded = {key: value.to(device) for key, value in encoded.items()}
        with torch.inference_mode():
            hidden = model(**encoded).last_hidden_state
        pooled = masked_mean(hidden, encoded["attention_mask"], special.to(device))
        chunk_embeddings.extend(pooled.float().cpu().numpy())

    accum: dict[str, list[tuple[np.ndarray, int]]] = defaultdict(list)
    for owner, embedding, weight in zip(owners, chunk_embeddings, weights, strict=True):
        accum[owner].append((embedding, weight))
    result = {}
    for owner, values in accum.items():
        matrix = np.stack([value for value, _ in values])
        entity_weights = np.asarray([weight for _, weight in values], dtype=np.float32)
        result[owner] = np.average(matrix, axis=0, weights=entity_weights)
    return result


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--sample", type=Path, default=Path("artifacts/dev-10k/sample.jsonl")
    )
    parser.add_argument("--target-key", default="paffinity")
    parser.add_argument("--target-name", default="pAffinity")
    parser.add_argument("--limit", type=int)
    parser.add_argument("--batch-size", type=int, default=8)
    parser.add_argument("--protein-chunk-residues", type=int, default=1022)
    parser.add_argument("--device", choices=["auto", "cpu", "cuda"], default="auto")
    parser.add_argument("--protein-model", default=DEFAULT_PROTEIN_MODEL)
    parser.add_argument("--protein-revision", default=DEFAULT_PROTEIN_REVISION)
    parser.add_argument("--ligand-model", default=DEFAULT_LIGAND_MODEL)
    parser.add_argument("--ligand-revision", default=DEFAULT_LIGAND_REVISION)
    parser.add_argument("--output", type=Path, default=Path("artifacts/embeddings.npz"))
    args = parser.parse_args()

    device_name = (
        "cuda" if args.device == "auto" and torch.cuda.is_available() else args.device
    )
    if device_name == "auto":
        device_name = "cpu"
    device = torch.device(device_name)
    rows = read_rows(args.sample, args.limit)
    proteins = sorted({row["protein_id"]: row["sequence"] for row in rows}.items())
    ligands = sorted({row["ligand_id"]: row["smiles"] for row in rows}.items())

    started = time.monotonic()
    protein_tokenizer = AutoTokenizer.from_pretrained(
        args.protein_model, revision=args.protein_revision
    )
    protein_model = (
        AutoModel.from_pretrained(args.protein_model, revision=args.protein_revision)
        .eval()
        .to(device)
    )
    protein_embeddings = encode_proteins(
        protein_model,
        protein_tokenizer,
        proteins,
        args.batch_size,
        device,
        args.protein_chunk_residues,
    )
    del protein_model

    ligand_tokenizer = AutoTokenizer.from_pretrained(
        args.ligand_model, revision=args.ligand_revision
    )
    ligand_model = (
        AutoModel.from_pretrained(args.ligand_model, revision=args.ligand_revision)
        .eval()
        .to(device)
    )
    ligand_matrix = encode_texts(
        ligand_model,
        ligand_tokenizer,
        [smiles for _, smiles in ligands],
        args.batch_size,
        device,
    )
    ligand_embeddings = {
        entity_id: embedding
        for (entity_id, _), embedding in zip(ligands, ligand_matrix, strict=True)
    }

    pair_protein = np.stack(
        [protein_embeddings[row["protein_id"]] for row in rows]
    ).astype(np.float32)
    pair_ligand = np.stack(
        [ligand_embeddings[row["ligand_id"]] for row in rows]
    ).astype(np.float32)
    args.output.parent.mkdir(parents=True, exist_ok=True)
    np.savez_compressed(
        args.output,
        pair_ids=np.asarray([row["pair_id"] for row in rows]),
        target=np.asarray([row[args.target_key] for row in rows], dtype=np.float32),
        protein=pair_protein,
        ligand=pair_ligand,
    )
    metadata = {
        "rows": len(rows),
        "unique_proteins": len(proteins),
        "unique_ligands": len(ligands),
        "protein_dim": int(pair_protein.shape[1]),
        "ligand_dim": int(pair_ligand.shape[1]),
        "target_key": args.target_key,
        "target_name": args.target_name,
        "protein_model": args.protein_model,
        "protein_revision": args.protein_revision,
        "ligand_model": args.ligand_model,
        "ligand_revision": args.ligand_revision,
        "protein_chunk_residues": args.protein_chunk_residues,
        "device": str(device),
        "seconds": time.monotonic() - started,
    }
    args.output.with_suffix(".json").write_text(json.dumps(metadata, indent=2) + "\n")
    print(json.dumps(metadata, indent=2))


if __name__ == "__main__":
    main()