Parse OBO formatted ontologies into NetworkX MultiDiGraph data structures
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Parse OBO (Open Biomedical Ontologies) formatted files into NetworkX MultiDiGraph data structures. obonet provides a simple, pythonic interface for loading and manipulating ontological data used in bioinformatics and scientific research.
pip install obonetimport obonetimport obonet
import networkx
# Read from a local file
graph = obonet.read_obo('path/to/ontology.obo')
# Read from a URL
url = 'https://example.com/ontology.obo'
graph = obonet.read_obo(url)
# Read compressed file (automatic detection)
graph = obonet.read_obo('ontology.obo.gz')
# Basic graph operations
print(f"Number of terms: {len(graph)}")
print(f"Number of relationships: {graph.number_of_edges()}")
# Check if ontology is a directed acyclic graph
is_dag = networkx.is_directed_acyclic_graph(graph)
# Get term information
for term_id, data in graph.nodes(data=True):
name = data.get('name', 'Unknown')
print(f"{term_id}: {name}")
break
# Find all parent terms (superterms) of a specific term
parents = networkx.descendants(graph, 'TERM:0000001')
# Find all child terms (subterms) of a specific term
children = networkx.ancestors(graph, 'TERM:0000001')Parse OBO formatted ontology files into NetworkX MultiDiGraph structures.
def read_obo(
path_or_file: PathType,
ignore_obsolete: bool = True,
encoding: str | None = "utf-8"
) -> networkx.MultiDiGraph[str]:
"""
Parse an OBO formatted ontology file into a NetworkX MultiDiGraph.
Parameters:
- path_or_file: Path, URL, or open file object for the OBO file
- ignore_obsolete: When True (default), excludes obsolete terms from the graph
- encoding: Character encoding for file reading (default: "utf-8")
Returns:
NetworkX MultiDiGraph representing the ontology with:
- Nodes: ontology terms with metadata as attributes
- Edges: relationships with relationship type as edge key
- Graph attributes: header information from OBO file
"""Access the package version information.
__version__: str | NoneThe returned NetworkX MultiDiGraph contains:
from typing import Union, TextIO, Any
import os
PathType = Union[str, "os.PathLike[Any]", TextIO]ignore_obsolete parameter