Bases: Ontology

This class provides functionalities for creating and operating on ontology knowledge graphs. See Ontology documentation for inherited attributes and methods.

Example:

>>> from  metahq_build.ontology import Graph
>>> ontograph = Graph.from_obo("mondo.obo")
>>> ontograph.graph
DiGraph with 23314 nodes and 35351 edges

>>> ontograph.nodes
['MONDO:0002816' 'MONDO:0000004' 'MONDO:0021034' ... 'MONDO:8000019'
 'MONDO:8000023' 'MONDO:8000024']

>>> ontograph.leaves
['MONDO:0000082' 'MONDO:0000138' 'MONDO:0000208' ... 'MONDO:8000019'
 'MONDO:8000023' 'MONDO:8000024']

>>> ontograph.class_dict["MONDO:0021054"]
bone sarcoma

graph property

Return the ontology DiGraph

nodes property

Return the IDs of the graph nodes

leaves property

Return leaf nodes of the ontology

__init__()

Initialize Graph object as a child of Ontology

construct_graph()

Constructs an ontology graph from entries from an ontology file.

A simple cycle occurs between 2 nodes UBERON:8000009 and UBERON:0002354 (cardiac Purkinje fiber network and cardiac Purkinje fiber) They are both parents and children of eachother, so to preserve the directed acyclic structure of the edgelist, we intentionally keep only one edge (fiber network is parent of fiber) on Line 100.

descendants_from(nodes, verbose=False)

Retrieves descendants from an array of parent nodes.

Parameters:
  • nodes (list[str]) –

    IDs in self.nodes for which to find desendants.

  • verbose (bool, default: False ) –

    If True, will print nodes not in the graph.

Returns:
  • _map( dict[str, list[str]] ) –

    Mapping between parents (keys) and their children (values).

Example:

>>> from  metahq_build.ontology import Graph
>>> ontograph = Graph.from_obo("mondo.obo")
>>> ontograph.descendants_from(['MONDO:0005071', 'MONDO:0043543'])
{'MONDO:0005071': ['MONDO:0019438' ... 'MONDO:0100070'],
 'MONDO:0043543': ['MONDO:0043544' ... 'MONDO:0005188']}

ancestors_from(nodes, verbose=False)

Retrieves ancestors from an array of parent nodes.

Parameters:
  • nodes (list[str]) –

    IDs in self.nodes for which to find ancestors.

  • verbose (bool, default: False ) –

    If True, will print nodes not in the graph.

Returns:
  • _map( dict[str, list[str]] ) –

    Mapping between parents (keys) and their children (values).

Example:

>>> from  metahq_build.ontology import Graph
>>> ontograph = Graph.from_obo("mondo.obo")
>>> ontograph.ancestors_from(['MONDO:0008791', 'MONDO:0043209'])
{'MONDO:0008791': ['MONDO:0019042' ... 'MONDO:0021147'],
 'MONDO:0043209': ['MONDO:0700096' ... 'MONDO:0004736']}

relations_matrix(dtype=np.int8)

Construct a term x term matrices defining ancestor and descendant relationships.

You may interpret the output matrix as the following: For any row, column pair, if the value is 1, then the term representing that particular row is an ancestor of the term representing that particular column. If the value is 0, then there is no relationship between the terms.

Parameters:
  • dtype (DTypeLike, default: int8 ) –

    A string representing a dtype that can be coerced into a np.dtype. Can be a numpy dtype (e.g., np.int8, np.int32) a python builtin dtype (e.g., int, float), or a string (e.g., 'int8', ''>i4').

Returns:
  • RelationsMatrix

    A RelationsMatrix object storing the relationships matrix and terms representing the rows and columns.

deepest_node(query)

Find the deepest node using breadth first search from root nodes.

Parameters:
  • query (list[str]) –

    An array of nodes for which to find the deepest node.

Returns:
  • deepest( str ) –

    The deepest node out of all query nodes.

propagate_term(query, ref_term)

Sets label for ontology terms

ancestors(term)

Gets ancestors of a single term

descendants(term)

Gets descendants of a single term