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
6 changes: 6 additions & 0 deletions src/pyobo/resources/so.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

def get_so_name(so_id: str) -> str | None:
"""Get the name from the identifier."""
if so_id == "0003002":
# see https://github.com/The-Sequence-Ontology/SO-Ontologies/pull/668
return "viral integration site"
if so_id == "0003001":
# see https://github.com/The-Sequence-Ontology/SO-Ontologies/pull/667
return "cluster RNA gene"
return load_so().get(so_id)


Expand Down
39 changes: 19 additions & 20 deletions src/pyobo/sources/hgnc/hgnc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,25 @@

from pyobo.api.utils import get_version
from pyobo.resources.so import get_so_name
from pyobo.struct import (
Annotation,
Obo,
OBOLiteral,
Reference,
Term,
from_species,
has_gene_product,
is_mentioned_by,
member_of,
orthologous,
transcribes_to,
)
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.typedef import (
comment,
enables,
ends,
exact_match,
from_species,
gene_product_enables,
gene_product_of,
has_gene_product,
has_member,
is_mentioned_by,
located_in,
member_of,
orthologous,
starts,
transcribes_to,
)
from pyobo.utils.path import ensure_path

Expand Down Expand Up @@ -137,6 +135,7 @@
"homeodb", # TODO add to bioregistry, though this is defunct
"mamit-trnadb", # TODO add to bioregistry, though this is defunct
"mane_select", # TODO
"gene_group", # gene_group_id is needed, but this just has label
}

#: A mapping from HGNC's locus_type annotations to sequence ontology identifiers
Expand Down Expand Up @@ -195,8 +194,10 @@ class HGNCGetter(Obo):
typedefs = [
from_species,
has_gene_product,
gene_product_of,
gene_product_enables,
transcribes_to,
has_member,
orthologous,
member_of,
exact_match,
Expand All @@ -205,6 +206,7 @@ class HGNCGetter(Obo):
starts,
ends,
comment,
enables,
]
synonym_typedefs = [
previous_name,
Expand Down Expand Up @@ -262,6 +264,8 @@ def get_terms(version: str | None = None, force: bool = False) -> Iterable[Term]
for so_id in sorted(_so_ids)
]

yield from get_gene_family_terms(version=version, force=force)

statuses = set()
for entry in tqdm(entries, desc=f"Mapping {PREFIX}", unit="gene", unit_scale=True):
name, symbol, identifier = (
Expand Down Expand Up @@ -393,15 +397,10 @@ def get_terms(version: str | None = None, force: bool = False) -> Iterable[Term]
term.append_mentioned_by(Reference(prefix="pubmed", identifier=str(pubmed_id)))

gene_group_ids = entry.pop("gene_group_id", [])
gene_groups = entry.pop("gene_group", [])
for gene_group_id, gene_group_label in zip(gene_group_ids, gene_groups, strict=False):
for gene_group_id in gene_group_ids:
term.append_relationship(
member_of,
Reference(
prefix="hgnc.genegroup",
identifier=str(gene_group_id),
name=gene_group_label,
),
Reference(prefix=GENE_GROUP_PREFIX, identifier=str(gene_group_id)),
)

for alias_symbol in entry.pop("alias_symbol", []):
Expand Down
66 changes: 36 additions & 30 deletions src/pyobo/sources/hgnc/hgncgenefamily.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@

import pandas as pd

from ...struct import Obo, Reference, Term, is_mentioned_by
from ...api.utils import get_version
from ...struct.struct import Obo, Reference, Term
from ...struct.struct import abbreviation as symbol_type
from ...struct.typedef import enables, exact_match, from_species
from ...struct.typedef import enables, exact_match, from_species, is_mentioned_by
from ...utils.path import ensure_df

__all__ = [
"GENE_GROUP_REFERENCE",
"GENE_GROUP_TERM",
"HGNCGroupGetter",
"get_gene_family_terms",
]

PREFIX = "hgnc.genegroup"
GENE_GROUP_PREFIX = "hgnc.genegroup"
FAMILIES_URL = "https://storage.googleapis.com/public-download-files/hgnc/csv/csv/genefamily_db_tables/family.csv"
FAMILIES_ALIAS_URL = "https://storage.googleapis.com/public-download-files/hgnc/csv/csv/genefamily_db_tables/family_alias.csv"
HIERARCHY_URL = "https://storage.googleapis.com/public-download-files/hgnc/csv/csv/genefamily_db_tables/hierarchy.csv"
Expand All @@ -23,19 +27,21 @@
class HGNCGroupGetter(Obo):
"""An ontology representation of HGNC's gene group nomenclature."""

ontology = PREFIX
ontology = GENE_GROUP_PREFIX
bioversions_key = "hgnc"
synonym_typedefs = [symbol_type]
typedefs = [from_species, enables, exact_match, is_mentioned_by]

def iter_terms(self, force: bool = False) -> Iterable[Term]:
"""Iterate over terms in the ontology."""
return get_terms(force=force)
return get_gene_family_terms(force=force)


def get_hierarchy(force: bool = False) -> Mapping[str, list[str]]:
def get_hierarchy(*, version: str | None = None, force: bool = False) -> Mapping[str, list[str]]:
"""Get the HGNC Gene Families hierarchy as a dictionary."""
df = ensure_df(PREFIX, url=HIERARCHY_URL, force=force, sep=",")
if version is None:
version = get_version("hgnc")
df = ensure_df(GENE_GROUP_PREFIX, url=HIERARCHY_URL, force=force, sep=",", version=version)
d = defaultdict(list)
for parent_id, child_id in df.values:
d[child_id].append(parent_id)
Expand All @@ -44,51 +50,51 @@ def get_hierarchy(force: bool = False) -> Mapping[str, list[str]]:

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_terms(force: bool = False) -> Iterable[Term]:

def get_gene_family_terms(*, version: str | None = None, force: bool = False) -> Iterable[Term]:
"""Get the HGNC Gene Group terms."""
terms = list(_get_terms_helper(force=force))
hierarchy = get_hierarchy(force=force)
if version is None:
version = get_version("hgnc")

terms = list(_get_terms_helper(force=force, version=version))
hierarchy = get_hierarchy(force=force, version=version)

id_to_term = {term.reference.identifier: term for term in terms}
id_to_term = {term.identifier: term for term in terms}
for child_id, parent_ids in hierarchy.items():
child: Term = id_to_term[child_id]
for parent_id in parent_ids:
parent: Term = id_to_term[parent_id]
child.append_parent(
Reference(
prefix=PREFIX,
identifier=parent_id,
name=parent.name,
)
)
gene_group = Reference(prefix="SO", identifier="0005855", name="gene group")
yield Term(reference=gene_group)
child.append_parent(id_to_term[parent_id])
yield GENE_GROUP_TERM
for term in terms:
if not term.parents:
term.append_parent(gene_group)
term.append_parent(GENE_GROUP_REFERENCE)
yield from terms


def _get_terms_helper(force: bool = False) -> Iterable[Term]:
alias_df = ensure_df(PREFIX, url=FAMILIES_ALIAS_URL, force=force, sep=",")
def _get_terms_helper(version: str, force: bool = False) -> Iterable[Term]:
alias_df = ensure_df(
GENE_GROUP_PREFIX, url=FAMILIES_ALIAS_URL, force=force, sep=",", version=version
)
aliases = defaultdict(set)
for _id, family_id, alias in alias_df.values:
aliases[family_id].add(alias)

df = ensure_df(PREFIX, url=FAMILIES_URL, force=force, sep=",")
df = ensure_df(GENE_GROUP_PREFIX, url=FAMILIES_URL, force=force, sep=",", version=version)
for gene_group_id, symbol, name, pubmed_ids, definition, desc_go in df[COLUMNS].values:
if not definition or pd.isna(definition):
definition = None
term = Term(
reference=Reference(prefix=PREFIX, identifier=gene_group_id, name=name),
reference=Reference(prefix=GENE_GROUP_PREFIX, identifier=gene_group_id, name=name),
definition=definition,
)
if pubmed_ids and pd.notna(pubmed_ids):
for s in pubmed_ids.replace(" ", ",").split(","):
s = s.strip()
if s:
term.append_mentioned_by(Reference(prefix="pubmed", identifier=s))
for pubmed_id in pubmed_ids.replace(" ", ",").split(","):
pubmed_id = pubmed_id.strip()
if pubmed_id:
term.append_mentioned_by(Reference(prefix="pubmed", identifier=pubmed_id))
if desc_go and pd.notna(desc_go):
go_id = desc_go[len("http://purl.uniprot.org/go/") :]
term.append_relationship(enables, Reference(prefix="GO", identifier=go_id))
Expand Down
1 change: 1 addition & 0 deletions src/pyobo/struct/typedef.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"is_antagonist_of",
"is_defined_by",
"is_inverse_agonist_of",
"is_mentioned_by",
"located_in",
"mapping_has_confidence",
"mapping_has_justification",
Expand Down
Loading