File size: 13,340 Bytes
db32e07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# Commands to run (Spec-RAG retrieval + sanity check + oracle)

Paths below use your env: `Spec-RAG`, `SpecBridge`, `De-SpecBridge`, `MassSpecGym_test.mgf`, and `out/retrieval/`.

---

## Improving retrieval performance (candidates_A / candidates_B)

- **`--ef-search 512`** (default): Higher FAISS HNSW `ef_search` at query time improves ANN recall. Increase to 1024 if you need better recall and can afford slower search.
- **`--formula-filter`**: When MGF has `FORMULA`/`formula`, restricts candidates to same-formula molecules (over-fetches from FAISS then reranks). Use this so Recall/Tanimoto improve when the library contains same-formula molecules.
- **`--chemberta-model`**: Must match the model used to build the library (`build_library.py --chemberta-model`). Default `Derify/ChemBERTa_augmented_pubchem_13m`. If your SpecBridge checkpoint was trained with a different ChemBERTa, set this and rebuild the library with that model.
- **Formula over-fetch**: `--formula-min-fetch`, `--formula-max-fetch`, `--formula-fetch-multiplier` control how many vectors are fetched when using `--formula-filter`; higher values can improve recall when formula buckets are large.
- **Rebuild FAISS with higher quality**: `python scripts/build_faiss.py --library-dir ... --ef-search 256` (default is now 256) before retrieving.

---

## 1) Build library (once)

```bash
cd /cluster/tufts/liulab/yiwan01/Spec-RAG

python scripts/build_library.py \
  --smiles-path /cluster/tufts/liulab/yiwan01/De-SpecBridge/data/pubchem_clean.smi \
  --out-dir /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/library \
  --despecbridge-path /cluster/tufts/liulab/yiwan01/De-SpecBridge
```

---

## 2) Build FAISS indices

```bash
python scripts/build_faiss.py \
  --library-dir /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/library
```

---

## 3) Train mappers (only if using Variant A without --smited-mapper-ckpt)

```bash
python scripts/train_mapper.py \
  --mgf-path /cluster/tufts/liulab/yiwan01/SpecBridge/data/MassSpecGym_train.mgf \
  --specbridge-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/runs/specbridge_align_chemberta_pub_v3g_msgym_mapper_spec/ckpt_001200.pt \
  --dreams-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/data/ssl_model.ckpt \
  --out-dir /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/mappers \
  --despecbridge-path /cluster/tufts/liulab/yiwan01/De-SpecBridge
```

---

## 4) Retrieve (Variants A, B, C)

**Variant A (SMI-TED only; pretrained mapper):**

```bash
python scripts/retrieve_generate.py \
  --mgf-path /cluster/tufts/liulab/yiwan01/SpecBridge/data/MassSpecGym_test.mgf \
  --library-dir /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/library \
  --specbridge-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/runs/specbridge_align_chemberta_pub_v3g_msgym_mapper_spec/ckpt_001200.pt \
  --dreams-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/data/ssl_model.ckpt \
  --smited-mapper-ckpt /cluster/tufts/liulab/yiwan01/De-SpecBridge/runs/smited_mapper_final/mapper_best.pt \
  --despecbridge-path /cluster/tufts/liulab/yiwan01/De-SpecBridge \
  --variant A \
  --K 100 \
  --ef-search 512 \
  --formula-filter \
  --out-jsonl /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/candidates_A.jsonl
```

**Variant B (ChemBERTa retrieval; no mapper-dir needed):**

```bash
python scripts/retrieve_generate.py \
  --mgf-path /cluster/tufts/liulab/yiwan01/SpecBridge/data/MassSpecGym_test.mgf \
  --library-dir /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/library \
  --specbridge-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/runs/specbridge_align_chemberta_pub_v3g_msgym_mapper_spec/ckpt_001200.pt \
  --dreams-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/data/ssl_model.ckpt \
  --variant B \
  --K 100 \
  --ef-search 512 \
  --formula-filter \
  --chemberta-model Derify/ChemBERTa_augmented_pubchem_13m \
  --out-jsonl /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/candidates_B.jsonl
```

**Variant B formula-keyed candidate-pool mode (direct exact rerank inside the provided candidate pool; no dependency on library overlap, and no FAISS needed unless a query falls back):**

```bash
python scripts/retrieve_generate.py \
  --mgf-path /cluster/tufts/liulab/yiwan01/SpecBridge/data/MassSpecGym_test.mgf \
  --library-dir /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/library \
  --specbridge-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/runs/specbridge_align_chemberta_pub_v3g_msgym_mapper_spec/ckpt_001200.pt \
  --dreams-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/data/ssl_model.ckpt \
  --variant B \
  --K 100 \
  --formula-filter \
  --candidate-json /cluster/tufts/liulab/yiwan01/SpecBridge/data/MassSpecGym_retrieval_candidates_formula.json \
  --candidate-key-field formula \
  --out-jsonl /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/candidates_B_benchmark.jsonl
```

`--candidate-key-field smiles_gt` is still supported, but it is oracle-only benchmarking because it selects the candidate pool using the ground-truth molecule.

**Variant C:**

```bash
python scripts/retrieve_generate.py \
  --mgf-path /cluster/tufts/liulab/yiwan01/SpecBridge/data/MassSpecGym_test.mgf \
  --library-dir /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/library \
  --specbridge-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/runs/specbridge_align_chemberta_pub_v3g_msgym_mapper_spec/ckpt_001200.pt \
  --dreams-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/data/ssl_model.ckpt \
  --variant C \
  --K 100 \
  --out-jsonl /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/candidates_C.jsonl
```

---

## 5) Evaluate spectrum-based retrieval (Recall + Tanimoto)

```bash
python scripts/evaluate_massspecgym.py \
  --pred-jsonl /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/candidates_A.jsonl \
  --report /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/metrics_A.json \
  --tanimoto

python scripts/evaluate_massspecgym.py \
  --pred-jsonl /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/candidates_B.jsonl \
  --report /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/metrics_B.json \
  --tanimoto

python scripts/evaluate_massspecgym.py \
  --pred-jsonl /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/candidates_C.jsonl \
  --report /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/metrics_C.json \
  --tanimoto
```

---

## 6) Self-retrieval sanity check (Priority 2) — true mol embeddings

Index = test molecules only; query = same true molecule embeddings. **Expected Recall@1 ≈ 1.0** for both.

- If either fails → that embedding/index pipeline is broken.
- If ChemBERTa passes and SMI-TED fails → SMI-TED embedding issue.
- If both pass → issue is likely library coverage / eval mismatch.

```bash
python scripts/self_retrieval_sanity_check.py \
  --pred-jsonl /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/candidates_B.jsonl \
  --despecbridge-path /cluster/tufts/liulab/yiwan01/De-SpecBridge \
  --report /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/self_retrieval_report.json
```

(You can use any of `candidates_A.jsonl`, `candidates_B.jsonl`, or `candidates_C.jsonl`; they share the same `smiles_gt` per row.)

---

## 6b) Mapped-embedding self-retrieval sanity check

Index = **spectrum→mapper** embeddings of test set (same MGF); query = same mapped embeddings. **Expected Recall@1 ≈ 1.0.**

- Tests spectrum→ChemBERTa-mapped (SpecBridge) and spectrum→SMI-TED-mapped (DreamsToSmiTed or M_smi).
- If either fails → spectrum→mapped-embedding pipeline is broken.

```bash
# With De-SpecBridge SMI-TED mapper (recommended)
python scripts/self_retrieval_mapped_sanity_check.py \
  --mgf-path /cluster/tufts/liulab/yiwan01/SpecBridge/data/MassSpecGym_test.mgf \
  --specbridge-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/runs/specbridge_align_chemberta_pub_v3g_msgym_mapper_spec/ckpt_001200.pt \
  --dreams-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/data/ssl_model.ckpt \
  --smited-mapper-ckpt /cluster/tufts/liulab/yiwan01/De-SpecBridge/runs/smited_mapper_final/mapper_best.pt \
  --despecbridge-path /cluster/tufts/liulab/yiwan01/De-SpecBridge \
  --report /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/self_retrieval_mapped_report.json
```

If you use Spec-RAG mappers instead of the pretrained SMI-TED mapper:

```bash
python scripts/self_retrieval_mapped_sanity_check.py \
  --mgf-path /cluster/tufts/liulab/yiwan01/SpecBridge/data/MassSpecGym_test.mgf \
  --specbridge-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/runs/specbridge_align_chemberta_pub_v3g_msgym_mapper_spec/ckpt_001200.pt \
  --dreams-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/data/ssl_model.ckpt \
  --mapper-dir /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/mappers \
  --report /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/self_retrieval_mapped_report.json
```

---

## 6c) True-index + Mapped-query sanity check

**Index** = true molecule embeddings (ChemBERTa/SMI-TED of test SMILES). **Query** = mapped embeddings (spectrum → mapper). So we build a tiny index from the test set’s true mol embeddings, then query with the spectrum→mapper embeddings. This tests how well the mapper aligns spectrum to molecule space (Recall@1/10/50).

```bash
python scripts/sanity_check_true_index_mapped_query.py \
  --mgf-path /cluster/tufts/liulab/yiwan01/SpecBridge/data/MassSpecGym_test.mgf \
  --specbridge-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/runs/specbridge_align_chemberta_pub_v3g_msgym_mapper_spec/ckpt_001200.pt \
  --dreams-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/data/ssl_model.ckpt \
  --smited-mapper-ckpt /cluster/tufts/liulab/yiwan01/De-SpecBridge/runs/smited_mapper_final/mapper_best.pt \
  --despecbridge-path /cluster/tufts/liulab/yiwan01/De-SpecBridge \
  --report /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/true_index_mapped_query_report.json
```

---

## 7) Oracle retrieval (true mol embedding vs library)

```bash
python scripts/evaluate_oracle_retrieval.py \
  --pred-jsonl /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/candidates_B.jsonl \
  --library-dir /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/library \
  --despecbridge-path /cluster/tufts/liulab/yiwan01/De-SpecBridge \
  --K 100 \
  --report /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/metrics_oracle.json
```

---

## Minimal “all commands” copy-paste (after library + FAISS exist)

```bash
cd /cluster/tufts/liulab/yiwan01/Spec-RAG

# Self-retrieval sanity check — true mol embeddings (expect Recall@1 ≈ 1.0)
python scripts/self_retrieval_sanity_check.py \
  --pred-jsonl /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/candidates_B.jsonl \
  --despecbridge-path /cluster/tufts/liulab/yiwan01/De-SpecBridge \
  --report /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/self_retrieval_report.json

# Mapped-embedding self-retrieval (spectrum→mapper; expect Recall@1 ≈ 1.0)
python scripts/self_retrieval_mapped_sanity_check.py \
  --mgf-path /cluster/tufts/liulab/yiwan01/SpecBridge/data/MassSpecGym_test.mgf \
  --specbridge-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/runs/specbridge_align_chemberta_pub_v3g_msgym_mapper_spec/ckpt_001200.pt \
  --dreams-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/data/ssl_model.ckpt \
  --smited-mapper-ckpt /cluster/tufts/liulab/yiwan01/De-SpecBridge/runs/smited_mapper_final/mapper_best.pt \
  --despecbridge-path /cluster/tufts/liulab/yiwan01/De-SpecBridge \
  --report /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/self_retrieval_mapped_report.json

# True-index + Mapped-query (index = true mol emb, query = spectrum→mapper)
python scripts/sanity_check_true_index_mapped_query.py \
  --mgf-path /cluster/tufts/liulab/yiwan01/SpecBridge/data/MassSpecGym_test.mgf \
  --specbridge-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/runs/specbridge_align_chemberta_pub_v3g_msgym_mapper_spec/ckpt_001200.pt \
  --dreams-ckpt /cluster/tufts/liulab/yiwan01/SpecBridge/data/ssl_model.ckpt \
  --smited-mapper-ckpt /cluster/tufts/liulab/yiwan01/De-SpecBridge/runs/smited_mapper_final/mapper_best.pt \
  --despecbridge-path /cluster/tufts/liulab/yiwan01/De-SpecBridge \
  --report /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/true_index_mapped_query_report.json

# Oracle retrieval (true mol embedding vs library)
python scripts/evaluate_oracle_retrieval.py \
  --pred-jsonl /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/candidates_B.jsonl \
  --library-dir /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/library \
  --despecbridge-path /cluster/tufts/liulab/yiwan01/De-SpecBridge \
  --K 100 \
  --report /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/metrics_oracle.json

# Spectrum-based eval (if you have candidates_*.jsonl)
python scripts/evaluate_massspecgym.py --pred-jsonl /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/candidates_A.jsonl --report /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/metrics_A.json --tanimoto
python scripts/evaluate_massspecgym.py --pred-jsonl /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/candidates_B.jsonl --report /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/metrics_B.json --tanimoto
python scripts/evaluate_massspecgym.py --pred-jsonl /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/candidates_C.jsonl --report /cluster/tufts/liulab/yiwan01/Spec-RAG/out/retrieval/metrics_C.json --tanimoto
```