# Retrieval pipeline (Retrival_plan.md) This implements the retrieval plan from De-SpecBridge/Retrival_plan.md in Spec-RAG: molecule library with precomputed embeddings, spectrum→embedding mappers, and three retrieval/generation variants (A: SMI-TED only, B: ChemBERTa retrieval + SMI-TED generation, C: ChemBERTa only). ## Scripts | Script | Purpose | |--------|--------| | `scripts/build_library.py` | Build library: compute v_smi (E_smi), v_chem (E_chem), meta.parquet (SMILES, formula, mass). SMI-TED optional via `--despecbridge-path` or `DESPECBRIDGE_PATH`. | | `scripts/build_faiss.py` | Build FAISS indices from `vectors_smi.npy` and `vectors_chem.npy` → `index_smi.faiss`, `index_chem.faiss`. | | `scripts/train_mapper.py` | Train M_smi and M_chem on MassSpecGym train MGF: E_mist(spec) → t_smi, t_chem. Saves `mappers.pt`. | | `scripts/retrieve_generate.py` | Run Variant A/B/C: query MGF → candidates JSONL. | | `scripts/evaluate_massspecgym.py` | Compute Recall@1/10/50 (and optional Tanimoto@1) from candidates JSONL. | | `scripts/evaluate_oracle_retrieval.py` | Oracle: encode smiles_gt with ChemBERTa/SMI-TED, search library; report Recall@1/10/50, Tanimoto@1. | | `scripts/self_retrieval_sanity_check.py` | Self-retrieval: index test molecules only, query with same embeddings; expect Recall@1 ≈ 1.0 (ChemBERTa and SMI-TED separately). | | `scripts/self_retrieval_mapped_sanity_check.py` | Mapped self-retrieval: index = spectrum→mapper embeddings of test set, query = same; expect Recall@1 ≈ 1.0 (ChemBERTa-mapped and SMI-TED-mapped). | | `scripts/sanity_check_true_index_mapped_query.py` | **True-index + Mapped-query**: index = true mol embeddings (test SMILES), query = spectrum→mapper embeddings; report Recall@1/10/50. Tests mapper alignment of spectrum to molecule space. | ## Artifacts - **Library dir**: `vectors_smi.npy`, `vectors_chem.npy`, `meta.parquet`, `index_smi.faiss`, `index_chem.faiss` - **Mapper dir**: `mappers.pt` (M_smi, M_chem state_dicts + d_spec, d_smi, d_chem) ## Example ```bash # 1) Build library (SMILES list, e.g. from PubChem or MassSpecGym unique SMILES) python scripts/build_library.py --smiles-path /path/to/smiles.txt --out-dir /path/to/library --despecbridge-path /path/to/De-SpecBridge # 2) Build FAISS indices python scripts/build_faiss.py --library-dir /path/to/library # 3) Train mappers (MassSpecGym train MGF) python scripts/train_mapper.py --mgf-path /path/to/MassSpecGym_train.mgf --specbridge-ckpt /path/to/specbridge.pt --out-dir /path/to/mappers --despecbridge-path /path/to/De-SpecBridge # 4) Retrieve (e.g. Variant B, K=100) python scripts/retrieve_generate.py --mgf-path /path/to/MassSpecGym_test.mgf --library-dir /path/to/library --mapper-dir /path/to/mappers --specbridge-ckpt /path/to/specbridge.pt --variant B --K 100 --out-jsonl /path/to/candidates_b.jsonl # 5) Evaluate python scripts/evaluate_massspecgym.py --pred-jsonl /path/to/candidates_b.jsonl --report /path/to/metrics.json --tanimoto ``` ## Self-retrieval sanity check (Priority 2) Build a tiny FAISS index from test molecules only; query with the same true molecule embeddings. Expected **Recall@1 ≈ 1.0** for both ChemBERTa and SMI-TED. - If either fails → that embedding/index pipeline is broken. - If ChemBERTa passes and SMI-TED fails → SMI-TED embedding construction is the issue. - If both pass → main issue is likely library coverage / eval mismatch. **Mapped-embedding self-retrieval** (`self_retrieval_mapped_sanity_check.py`): index = spectrum→mapper embeddings (test MGF), query = same. Tests spectrum→ChemBERTa-mapped and spectrum→SMI-TED-mapped. If either fails, the spectrum→mapped-embedding pipeline is broken. **True-index + Mapped-query** (`sanity_check_true_index_mapped_query.py`): index = **true** molecule embeddings (ChemBERTa/SMI-TED of test SMILES), query = **mapped** embeddings (spectrum→mapper). Measures how well the mapper aligns spectrum to molecule space (Recall@1/10/50). Low recall here with high oracle recall suggests the mapper or spectrum representation is the bottleneck. ## Dependencies - SMI-TED (E_smi): set `DESPECBRIDGE_PATH` or `--despecbridge-path` to De-SpecBridge repo so `despecbridge.models.smited_decoder.load_smited` can be imported. - E_mist: SpecBridge checkpoint (DreaMS adapter) via `--specbridge-ckpt` (and optional `--dreams-ckpt`).