Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/pyobo/sources/hgnc/hgnc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
from pyobo.resources.so import get_so_name
from pyobo.sources.hgnc.hgncgenefamily import GENE_GROUP_PREFIX, get_gene_family_terms
from pyobo.struct import Annotation, Obo, OBOLiteral, Reference, Term
from pyobo.struct.struct import gene_symbol_synonym, previous_gene_symbol, previous_name
from pyobo.struct import vocabulary as v
from pyobo.struct.struct import (
cleanup_terms,
gene_symbol_synonym,
previous_gene_symbol,
previous_name,
)
from pyobo.struct.typedef import (
comment,
enables,
Expand Down Expand Up @@ -222,7 +228,22 @@ class HGNCGetter(Obo):

def iter_terms(self, force: bool = False) -> Iterable[Term]:
"""Iterate over terms in the ontology."""
return get_terms(force=force, version=self.data_version)
yield from cleanup_terms(
get_terms(force=force, version=self.data_version),
prefix=PREFIX,
prefix_allowlist={
"chr": v.chromosomal_region,
"uniprot": v.protein,
"mgi": v.gene,
"rgd": v.gene,
"go": v.molecular_function,
"ec": v.molecular_function,
"ncbigene": v.gene,
"rnacentral": v.rna,
"mirbase": v.rna,
"snornabase": v.rna,
},
)


def _get_location_to_chr() -> dict[str, Reference]:
Expand Down
11 changes: 4 additions & 7 deletions src/pyobo/sources/hgnc/hgncgenefamily.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
from ...struct.struct import Obo, Reference, Term
from ...struct.struct import abbreviation as symbol_type
from ...struct.typedef import enables, exact_match, from_species, is_mentioned_by
from ...struct.vocabulary import GENE_GROUP
from ...utils.path import ensure_df

__all__ = [
"GENE_GROUP_REFERENCE",
"GENE_GROUP_TERM",
"HGNCGroupGetter",
"get_gene_family_terms",
]
Expand All @@ -30,6 +29,7 @@ class HGNCGroupGetter(Obo):
ontology = GENE_GROUP_PREFIX
bioversions_key = "hgnc"
synonym_typedefs = [symbol_type]
root_terms = [GENE_GROUP]
typedefs = [from_species, enables, exact_match, is_mentioned_by]

def iter_terms(self, force: bool = False) -> Iterable[Term]:
Expand All @@ -50,9 +50,6 @@ def get_hierarchy(*, version: str | None = None, force: bool = False) -> Mapping

COLUMNS = ["id", "abbreviation", "name", "pubmed_ids", "desc_comment", "desc_go"]

GENE_GROUP_REFERENCE = Reference(prefix="SO", identifier="0005855", name="gene group")
GENE_GROUP_TERM = Term(reference=GENE_GROUP_REFERENCE)


def get_gene_family_terms(*, version: str | None = None, force: bool = False) -> Iterable[Term]:
"""Get the HGNC Gene Group terms."""
Expand All @@ -67,10 +64,10 @@ def get_gene_family_terms(*, version: str | None = None, force: bool = False) ->
child: Term = id_to_term[child_id]
for parent_id in parent_ids:
child.append_parent(id_to_term[parent_id])
yield GENE_GROUP_TERM
yield Term(reference=GENE_GROUP)
for term in terms:
if not term.parents:
term.append_parent(GENE_GROUP_REFERENCE)
term.append_parent(GENE_GROUP)
yield from terms


Expand Down
40 changes: 40 additions & 0 deletions src/pyobo/struct/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -2527,3 +2527,43 @@ def iter_terms(self, force: bool = False) -> Iterable[Term]:
HUMAN_TERM = Term(reference=v.HUMAN)
CHARLIE_TERM = Term(reference=v.CHARLIE, type="Instance").append_parent(HUMAN_TERM)
PYOBO_INJECTED = "Injected by PyOBO"


def cleanup_terms(
terms: Iterable[Term], *, prefix: str, prefix_allowlist: dict[str, Reference]
) -> set[Term]:
"""Define missing references as classes."""
aux_term = Term(reference=default_reference(prefix, "aux", "auxiliary terms"))

terms = set(terms)
term_references: dict[Reference, Term] = {term.reference: term for term in terms}

prefix_to_parent_term: dict[str, Term] = {}
for allowlist_prefix, allowlist_reference in prefix_allowlist.items():
if allowlist_reference not in term_references:
term = Term(reference=allowlist_reference)
term_references[allowlist_reference] = term
prefix_to_parent_term[allowlist_prefix] = term
terms.add(term)

prefix_to_parent_term[allowlist_prefix] = (
Term.default(
allowlist_prefix,
f"aux-{allowlist_prefix}",
name=f"auxiliary terms from {allowlist_prefix}",
)
.append_parent(aux_term)
.append_parent(allowlist_reference)
)

undefined: dict[Reference, Term] = {}
for term in terms:
for references in term._get_references().values():
for reference in references:
if reference in term_references or reference in undefined:
continue
if parent := prefix_to_parent_term.get(reference.prefix):
undefined[reference] = Term(reference=reference).append_parent(parent)

rv = terms | {aux_term} | set(prefix_to_parent_term.values()) | set(undefined.values())
return rv
9 changes: 9 additions & 0 deletions src/pyobo/struct/vocabulary.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,12 @@ def _c(c: curies.Reference) -> Reference:
comment, # maps to "comment:" line with strings
obo_creation_date,
]

mouse = Reference(prefix="NCBITaxon", identifier="10090", name="Mus musculus")
rat = Reference(prefix="NCBITaxon", identifier="10116", name="Rattus norvegicus")
gene = Reference(prefix="SO", identifier="0000704", name="gene")
GENE_GROUP = Reference(prefix="SO", identifier="0005855", name="gene group")
protein = Reference(prefix="PR", identifier="000000001", name="protein")
molecular_function = Reference(prefix="GO", identifier="0003674", name="molecular function")
rna = Reference(prefix="uniprot.core", identifier="RNA", name="RNA")
chromosomal_region = Reference(prefix="GO", identifier="0098687", name="chromosomal region")
Loading