fabsssss's picture
Upload scripts/bioseed.py with huggingface_hub
b89b733 verified
Raw
History Blame Contribute Delete
4.5 kB
"""Shared resources for the biology fine-tune: real, verified entity pools + prefix map +
the deterministic (correct-by-construction) RDF/YAML emitters. Every CURIE emitted here
comes from a downloaded OBO release, the Biolink schema (bmt), or the mygene.info-verified
gene table - never hand-typed."""
import json, pathlib, random
ROOT = pathlib.Path("/Users/fabio/projects/qwen-bio-ft")
D = ROOT/"data"
OBO_VALID = json.loads((D/"obo_valid.json").read_text()) # {prefix: {id: label}}
OBO_POOL = json.loads((D/"obo_pool.json").read_text()) # {prefix: [[id,label,leaf]]}
GO_SPLIT = json.loads((D/"go_split.json").read_text()) # {MF/BP/CC: [[id,label]]}
GENES = json.loads((D/"genes_verified.json").read_text()) # {symbol: {ncbigene,uniprot,name}}
BLV = json.loads((D/"biolink_vocab.json").read_text())
# IRI namespaces (CURIE -> base IRI). Used by both emitter and validator; single source of truth.
PREFIX = {
"GO":"http://purl.obolibrary.org/obo/GO_", "CL":"http://purl.obolibrary.org/obo/CL_",
"MONDO":"http://purl.obolibrary.org/obo/MONDO_", "HP":"http://purl.obolibrary.org/obo/HP_",
"CHEBI":"http://purl.obolibrary.org/obo/CHEBI_", "NCBITaxon":"http://purl.obolibrary.org/obo/NCBITaxon_",
"RO":"http://purl.obolibrary.org/obo/RO_",
"NCBIGene":"http://identifiers.org/ncbigene/", "UniProtKB":"http://purl.uniprot.org/uniprot/",
"biolink":"https://w3id.org/biolink/vocab/",
}
IRI2PFX = sorted(((iri, pfx) for pfx, iri in PREFIX.items()), key=lambda x: -len(x[0]))
def iri_to_curie(iri):
for base, pfx in IRI2PFX:
if iri.startswith(base): return pfx+":"+iri[len(base):]
return None
# --- verified label lookups (membership) ---
def label_of(curie):
if ":" not in curie: return None
pfx, local = curie.split(":", 1)
if pfx in OBO_VALID: return OBO_VALID[pfx].get(curie)
if pfx == "NCBIGene":
for s, g in GENES.items():
if g["ncbigene"] == local: return g["name"]
if pfx == "UniProtKB":
for s, g in GENES.items():
if g["uniprot"] == local: return g["name"]
if curie in TAXA: return TAXA[curie]
return None
def is_real(curie): return label_of(curie) is not None
TAXA = {"NCBITaxon:9606":"Homo sapiens", "NCBITaxon:10090":"Mus musculus",
"NCBITaxon:10116":"Rattus norvegicus", "NCBITaxon:7227":"Drosophila melanogaster",
"NCBITaxon:6239":"Caenorhabditis elegans", "NCBITaxon:7955":"Danio rerio",
"NCBITaxon:4932":"Saccharomyces cerevisiae"}
# GO-CAM causal predicates (Relation Ontology) - the standard GO-CAM activity-flow set.
CAUSAL_RO = {
"RO:0002413":"provides input for", "RO:0002629":"directly positively regulates",
"RO:0002630":"directly negatively regulates", "RO:0002406":"directly activates",
"RO:0002305":"causally upstream of, negative effect", "RO:0002304":"causally upstream of, positive effect",
"RO:0002411":"causally upstream of", "RO:0002418":"causally upstream of or within",
}
def pool(pfx, n):
p = OBO_POOL[pfx]; return random.sample(p, min(n, len(p)))
# ---------- Biolink deterministic RDF emitter (guaranteed-valid Turtle) ----------
def _camel(label): return "".join(w.capitalize() for w in label.split())
def _snake(label): return label.replace(" ", "_")
def biolink_turtle(assoc_label, subj_curie, subj_cat, pred_label, obj_curie, obj_cat,
aid="urn:uuid:a", extra=None):
"""Emit a Biolink-shaped graph: category typing + direct edge + reified association.
Uses only IRIs expanded from PREFIX. Returns a Turtle string."""
def iri(c):
p, l = c.split(":", 1); return PREFIX[p]+l
BL = PREFIX["biolink"]; RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
s, o = iri(subj_curie), iri(obj_curie)
lines = [f"@prefix biolink: <{BL}> .", f"@prefix rdf: <{RDF}> .", ""]
lines.append(f"<{s}> a biolink:{_camel(subj_cat)} ;")
lines.append(f" biolink:{_snake(pred_label)} <{o}> .")
lines.append(f"<{o}> a biolink:{_camel(obj_cat)} .")
lines.append(f"<{aid}> a biolink:{_camel(assoc_label)} ;")
lines.append(f" rdf:subject <{s}> ;")
lines.append(f" biolink:predicate biolink:{_snake(pred_label)} ;")
lines.append(f" rdf:object <{o}> ;")
kl = (extra or {}).get("knowledge_level", "knowledge_assertion")
at = (extra or {}).get("agent_type", "manual_agent")
lines.append(f' biolink:knowledge_level "{kl}" ;')
lines.append(f' biolink:agent_type "{at}" .')
return "\n".join(lines)