# 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 ```