Download utils/gene_context_retrieval.py from pswap/phlam: direct link, hf CLI and curl.
- Browser
- Download file 1.79 kB
-
https://huggingface.co/spaces/pswap/phlam/resolve/main/utils/gene_context_retrieval.py
- Command line
-
hf download hf://spaces/pswap/phlam/utils/gene_context_retrieval.py
-
curl -L -o gene_context_retrieval.py https://huggingface.co/spaces/pswap/phlam/resolve/main/utils/gene_context_retrieval.py
1.79 kB
| """Gene-level nearest-neighbor search against the imgvr_inphared corpus, | |
| using a query gene's phlam_protein vector (its genomic-context residual, | |
| not just the protein sequence -- the same gene in a different genome lands | |
| at a different point).""" | |
| import os | |
| from utils.vector_store import VectorStore | |
| from utils.lazy import lazy_singleton | |
| IMGVR_DIR = os.environ.get("IMGVR_DATA_DIR", "/data/imgvr_inphared") | |
| # 13.9M vectors but only ~1KB/vector (fp16 scalar quant) -- fits comfortably | |
| # in RAM on an 8vCPU/32GB Space; preloaded so every query (including | |
| # synteny's direct .reconstruct() calls, see utils/synteny.py) is immune to | |
| # the mount's latency/flakiness after boot, not just the initial build. | |
| PRELOAD = os.environ.get("PHLAM_PROTEIN_PRELOAD", "1") == "1" | |
| class GeneContextRetriever: | |
| def __init__(self, data_dir=IMGVR_DIR, top_k=10, preload=PRELOAD): | |
| self.store = VectorStore( | |
| os.path.join(data_dir, "phlam_protein.faiss"), | |
| os.path.join(data_dir, "phlam_genes.parquet"), | |
| id_column="gene_id", top_k=top_k, preload=preload, | |
| ) | |
| def query(self, phlam_protein_vecs): | |
| """phlam_protein_vecs: [N, 480] array-like. Returns a list of N hit | |
| lists, each hit {gene_id, genome_id, gene_order, protein_id, seq_hash, norm, score}.""" | |
| return self.store.search_and_fetch(phlam_protein_vecs) | |
| # Shared across every consumer (app.py's gene_context_neighbors job AND | |
| # synteny.py's neighbor_track) -- one 13.4GB index loaded once, not once per | |
| # consumer. lazy_singleton also fixes a race synteny.py used to have on its | |
| # own unlocked module-global (a concurrent first call from two /synteny | |
| # requests could have built two independent copies). | |
| get_gene_context_retriever = lazy_singleton(GeneContextRetriever) | |