OboGraph Interface

class oaklib.interfaces.obograph_interface.OboGraphInterface(resource: ~oaklib.resource.OntologyResource | None = None, strict: bool = False, _multilingual: bool | None = None, autosave: bool = <factory>, exclude_owl_top_and_bottom: bool = <factory>, ontology_metamodel_mapper: ~oaklib.mappers.ontology_metadata_mapper.OntologyMetadataMapper | None = None, _converter: ~curies.api.Converter | None = None, auto_relax_axioms: bool | None = None, cache_lookups: bool = False, property_cache: ~oaklib.utilities.keyval_cache.KeyValCache = <factory>, _edge_index: ~oaklib.indexes.edge_index.EdgeIndex | None = None, _entailed_edge_index: ~oaklib.indexes.edge_index.EdgeIndex | None = None)[source]

an interface that provides an Object Oriented view of an ontology, following the OBO Graph Datamodel

See OBOGraphs

Key datamodel concepts:

  • obograph.Node - any named ontology element

  • obograph.Edge - any relationship between elements; for example between “finger” and “hand”

  • obograph.Graph - a collection of nodes, edges, and other ontology components

This datamodel conceives of an ontology as a graph

enable_transitive_query_cache()[source]

Cache transitive queries

disable_transitive_query_cache()[source]

Do not cache transitive queries (default)

node(curie: str, strict=False, include_metadata=False, expand_curies=False) Node[source]

Look up a node object by CURIE

Parameters:
  • curie – identifier of node

  • strict – raise exception if node not found

  • include_metadata – include detailed metadata

  • expand_curies – if True expand CURIEs to URIs

Returns:

nodes(expand_curies=False) Iterator[Node][source]

Yields all nodes in all graphs

Parameters:

expand_curies – if True expand CURIEs to URIs

Returns:

edges(expand_curies=False) Iterator[Edge][source]

Yields all edges in all graphs.

Parameters:

expand_curies – if True expand CURIEs to URIs

Returns:

synonym_map_for_curies(subject: str | Iterable[str]) Dict[str, List[SynonymPropertyValue]][source]

Get a map of SynonymPropertyValue objects keyed by curie

Parameters:

subject – curie or list of curies

Returns:

direct_graph(curies: str | List[str], **kwargs) Graph[source]

Return a graph object that consists of all the nodes specified in the curies list, extended with all direct relationships

Parameters:

curies

Returns:

direct graph

ancestor_graph(start_curies: str | List[str], predicates: List[str] | None = None) Graph[source]

Return a graph object that consists of all the nodes specified in the start_curies list, extended with an interactive walk up the graph following all relationships (optionally filtered by the predicate list)

Parameters:
  • start_curies

  • predicates – if supplied then only follow edges with these predicates

Returns:

ancestor graph

descendant_graph(start_curies: str | List[str], predicates: List[str] | None = None) Graph[source]

As ancestor graph, but in opposite direction

Parameters:
  • start_curies

  • predicates – if supplied then only follow edges with these predicates

Returns:

ancestor graph

non_redundant_entailed_relationships(predicates: List[str] | None = None, **kwargs) Iterator[Tuple[str, str, str]][source]

Yields all relationships that are directly entailed.

See https://github.com/INCATools/ontology-access-kit/issues/739

Parameters:

kwargs – same as relationships

Returns:

ancestors(start_curies: str | List[str], predicates: List[str] | None = None, reflexive=True, method: GraphTraversalMethod | None = None) Iterable[str][source]

Ancestors obtained from a walk starting from start_curies ending in roots, following only the specified predicates.

Note

This operation is reflexive: self is included

Parameters:
  • start_curies – curie or curies to start the walk from

  • predicates – only traverse over these (traverses over all if this is not set)

  • reflexive – include self

  • method

Returns:

all ancestor CURIEs

descendants(start_curies: str | List[str], predicates: List[str] | None = None, reflexive=True, method: GraphTraversalMethod | None = None) Iterable[str][source]

Descendants obtained from a walk downwards starting from start_curies ending in roots, following only the specified predicates.

Note

This operation is reflexive: self is included

Parameters:
  • start_curies – curie or curies to start the walk from

  • predicates – only traverse over these (traverses over all if this is not set)

  • reflexive – include self

  • method

Returns:

all descendant CURIEs

descendant_count(start_curies: str | List[str], predicates: List[str] | None = None, reflexive=True) int[source]

Count of descendants.

See descendants for more details.

Parameters:
  • start_curies – curie or curies to start the walk from

  • predicates – only traverse over these (traverses over all if this is not set)

  • reflexive – include self

Returns:

count of distinct CURIEs

subgraph_from_traversal(start_curies: str | List[str], predicates: List[str] | None = None, traversal: TraversalConfiguration | None = None) Graph[source]

Combines ancestors and descendants according to a traversal configuration.

>>> from oaklib import get_adapter
>>> from oaklib.interfaces.obograph_interface import TraversalConfiguration, Distance
>>> from oaklib.datamodels.vocabulary import IS_A, PART_OF
>>> # use an adapter to talk to an endpoint (here, sqlite)
>>> adapter = get_adapter("tests/input/go-nucleus.db")
>>> # get a subgraph centered around these nodes
>>> seeds = ["GO:0005634", "GO:0005773"] # nucleus, vacuole
>>> # walk up the graph to get ancestors, and also get direct children
>>> traversal = TraversalConfiguration(up_distance=Distance.TRANSITIVE, down_distance=Distance.DIRECT)
>>> graph = adapter.subgraph_from_traversal(seeds, predicates=[IS_A, PART_OF], traversal=traversal)
>>> len(graph.nodes)
22
>>> len(graph.edges)
27
Parameters:
  • start_curies

  • predicates

  • traversal

Returns:

extract_graph(entities: List[str], predicates: List[str] | None = None, dangling=True, include_metadata=True) Graph[source]

Extract a subgraph from the graph that contains the specified entities and predicates.

Parameters:
  • entities – entities to extract

  • predicates – predicates to extract

  • dangling – if true, include dangling nodes

Returns:

subgraph

relationships_to_graph(relationships: Iterable[Tuple[str, str, str]]) Graph[source]

Generates an OboGraph from a list of relationships

Parameters:

relationships

Returns:

walk_up_relationship_graph(start_curies: str | List[str], predicates: List[str] | None = None) Iterable[Tuple[str, str, str]][source]

Walks up the relation graph from a seed set of curies or individual curie, returning the full ancestry graph

Note: this may be inefficient for remote endpoints, in future a graph walking endpoint will implement this

Parameters:
  • start_curies

  • predicates

Returns:

paths(start_curies: str | List[str], target_curies: str | List[str], predicates: List[str] | None = None, predicate_weights: Dict[str, float] | None = None, shortest=True, directed=False) Iterator[Tuple[str, str, str]][source]

Returns all paths between sources and targets.

>>> from oaklib import get_adapter
>>> adapter = get_adapter("tests/input/go-nucleus.db", implements=OboGraphInterface)
>>> for path in sorted(list(adapter.paths(["GO:0005634"], ["GO:0005773"]))):
...   print(path)
('GO:0005634', 'GO:0005773', 'GO:0005634')
('GO:0005634', 'GO:0005773', 'GO:0005773')
('GO:0005634', 'GO:0005773', 'GO:0043231')
Parameters:
  • start_curies

  • start_curies

  • predicates

  • predicate_weights

  • shortest

Returns:

logical_definitions(subjects: Iterable[str] | None = None, predicates: Iterable[str] | None = None, objects: Iterable[str] | None = None, **kwargs) Iterable[LogicalDefinitionAxiom][source]

Yields all logical definitions for input subjects.

>>> from oaklib import get_adapter
>>> adapter = get_adapter("tests/input/go-nucleus.db", implements=OboGraphInterface)
>>> for ldef in adapter.logical_definitions(["GO:0009892"]):
...     print(f"Genus: {adapter.label(ldef.genusIds[0])}")
...     for r in ldef.restrictions:
...         print(f"  Differentia: {adapter.label(r.propertyId)} SOME {adapter.label(r.fillerId)}")
Genus: biological regulation
  Differentia: negatively regulates SOME metabolic process

Leaving the subjects parameter as None will yield all logical definitions in the ontology.

>>> len(list(adapter.logical_definitions()))
50
Parameters:
  • subjects – If specified, defined class must be in this set

  • predicates – If specified, only yields logical definitions with these predicates

  • objects – If specified, only yields logical definitions with genus or filler in this list

Returns:

disjoint_class_expressions_axioms(subjects: Iterable[str] | None = None, predicates: Iterable[str] | None = None, group=False, **kwargs) Iterable[DisjointClassExpressionsAxiom][source]

Yields all disjoint class expressions.

Parameters:
  • subjects – if present, filter to only those that reference these subjects

  • predicates – if present, filter to only those that reference these predicates

  • group – if True, group into cliques

  • kwargs

Returns:

add_metadata(graph: Graph) None[source]

Decorates the graph with meta objects on all nodes :param graph: :return:

as_obograph(expand_curies=False) Graph[source]

Convert entire resource to an OBO Graph object

Warning

some remote resources may choose to throw a NotImplementedError if it is impractical to download the entire ontology as a graph

Parameters:

expand_curies

Returns:

load_graph(graph: Graph, replace: True) None[source]

Loads a graph into the repository

Parameters:
  • graph

  • replace

Returns:

class oaklib.interfaces.obograph_interface.TraversalConfiguration(predicates: ~typing.List[str] | None = None, up_distance: ~oaklib.interfaces.obograph_interface.Distance = <factory>, down_distance: ~oaklib.interfaces.obograph_interface.Distance = <factory>)[source]

Specifies how to walk up and down a graph