Skip to content

Commit

Permalink
Merge pull request #1310 from bgyori/ido
Browse files Browse the repository at this point in the history
Improved OWL/IDO processing and handling of external ontology relations
  • Loading branch information
bgyori committed Jul 8, 2021
2 parents b84b37f + a5fbf89 commit 68abb57
Show file tree
Hide file tree
Showing 5 changed files with 785 additions and 431 deletions.
20 changes: 14 additions & 6 deletions indra/databases/owl_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pickle
from collections import defaultdict
from operator import itemgetter
from typing import Any, Mapping, TYPE_CHECKING
from typing import Any, Collection, Mapping, TYPE_CHECKING

from tqdm import tqdm

Expand All @@ -24,6 +24,7 @@ def entry_from_term(
term: "pronto.Term",
prefix: str,
remove_prefix: bool = False,
allowed_external_ns: Collection = None,
) -> Mapping[str, Any]:
"""Create a data dictionary from a Pronto term."""
rels_dict = defaultdict(list)
Expand All @@ -35,12 +36,18 @@ def entry_from_term(
continue
else:
xrefs.append(dict(namespace=xref_db, id=xref_id))
for child in term.subclasses(distance=1, with_self=False):
child_db, child_id = child.id.split(':', maxsplit=1)
if remove_prefix and child_db.lower() == prefix.lower():
rels_dict["is_a"].append(child_id)
for parent in term.superclasses(distance=1, with_self=False):
parent_db, parent_id = parent.id.split(':', maxsplit=1)
# If the parent here is not from this namespace and not one of the
# allowed external namespaces then we skip the parent
if parent_db.lower() != prefix.lower() and \
(not allowed_external_ns or
parent_db not in allowed_external_ns):
continue
if remove_prefix and parent_db.lower() == prefix.lower():
rels_dict["is_a"].append(parent_id)
else:
rels_dict["is_a"].append(child.id)
rels_dict["is_a"].append(parent.id)

term_ns, term_id = term.id.split(':', maxsplit=1)
term_ns = term_ns.lower()
Expand All @@ -62,6 +69,7 @@ def entries_from_ontology(
*,
skip_obsolete: bool = True,
remove_prefix: bool = False,
allowed_external_ns: Collection = None,
):
prefix = prefix.upper()
rv = []
Expand Down
21 changes: 12 additions & 9 deletions indra/ontology/bio/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BioOntology(IndraOntology):
# should be incremented to "force" rebuilding the ontology to be consistent
# with the underlying resource files.
name = 'bio'
version = '1.13'
version = '1.14'

def __init__(self):
super().__init__()
Expand Down Expand Up @@ -187,10 +187,11 @@ def add_obo_nodes(self):
oc = obo_client.OntologyClient(prefix=ns)
for db_id, entry in oc.entries.items():
label = self.label(ns.upper(), db_id)
# There may be more general ways of doing this but this
# handles the special case of BFO nodes in the EFO
# ontology.
if ns == 'efo' and db_id.startswith('BFO'):
# Here we handle and isa relationships that point
# to an entry outside this ontology. The logic for recognizing
# these here is: if there is a : in the ID but the prefix is
# not for this namespace then we assume it's another namespace
if ':' in db_id and not db_id.startswith(ns.upper()):
label = db_id
nodes.append((label,
{'name': entry['name']}))
Expand Down Expand Up @@ -227,13 +228,15 @@ def add_obo_hierarchies(self):
mapped_rel = rel_mappings.get(rel)
if not mapped_rel:
continue
source_label = self.label(ns.upper(), db_id)
if ns == 'efo' and db_id.startswith('BFO'):
if ':' in db_id and not db_id.startswith(ns.upper()):
source_label = db_id
else:
source_label = self.label(ns.upper(), db_id)
for target in targets:
target_label = self.label(ns.upper(), target)
if ns == 'efo' and target.startswith('BFO'):
if ':' in target and not target.startswith(ns.upper()):
target_label = target
else:
target_label = self.label(ns.upper(), target)
if rel in reverse_rel:
av = (target_label,
source_label,
Expand Down

0 comments on commit 68abb57

Please sign in to comment.