Package: obonet
obonet: load OBO-formatted ontologies into networkxRead OBO-formatted ontologies in Python.obonetisuser friendlysuccinctpythonicmodernsimple and testedlightweightnetworkxleveragingThis Python package loads OBO serialized ontologies into networks.
The functionobonet.read_obo()takes an.obofile and returns anetworkx.MultiDiGraphrepresentation of the ontology.
The parser was designed for the OBO specification version1.2&1.4.UsageSeepyproject.tomlfor the minimum Python version required and the dependencies.
OBO files can be read from a path, URL, or open file handle.
Compression is inferred from the path's extension.
See example usage below:importnetworkximportobonet# Read the taxrank ontologyurl='https://github.com/dhimmel/obonet/raw/main/tests/data/taxrank.obo'graph=obonet.read_obo(url)# Or read the xz-compressed taxrank ontologyurl='https://github.com/dhimmel/obonet/raw/main/tests/data/taxrank.obo.xz'graph=obonet.read_obo(url)# Number of nodeslen(graph)# Number of edgesgraph.number_of_edges()# Check if the ontology is a DAGnetworkx.is_directed_acyclic_graph(graph)# Mapping from term ID to nameid_to_name={id_:data.get('name')forid_,dataingraph.nodes(data=True)}id_to_name['TAXRANK:0000006']# TAXRANK:0000006 is species# Find all superterms of species. Note that networkx.descendants gets# superterms, while networkx.ancestors returns subterms.networkx.descendants(graph,'TAXRANK:0000006')For a more detailed tutorial, see theGene Ontology example notebook.ComparisonThis package specializes in reading OBO files into anewtorkx.MultiDiGraph.
A more general ontology-to-NetworkX reader is available in the Pythonnxontology packagevia thenxontology.imports.pronto_to_multidigraphfunction.
This function takes apronto.Ontologyobject,
which can be loaded from an OBO file, OBO Graphs JSON file, or Ontology Web Language 2 RDF/XML file (OWL).
Usingpronto_to_multidigraphallows creating a MultiDiGraph similar to the created byobonet,
with some differences in the amount of metadata retained.The primary focus of thenxontologypackage is to provide anNXOntologyclass for representing ontologies based around anetworkx.DiGraph.
NXOntology provides optimized implementations for computing node similarity and other intrinsic ontology metrics.
There are two important differences between a DiGraph for NXOntology and the MultiDiGraph produced by obonet:NXOntology is based on a DiGraph that does not allow multiple edges between the same two nodes.
Multiple edges between the same two nodes must therefore be collapsed.
By default, it only considersis a/rdfs:subClassOfrelationships,
but usingpronto_to_multidigraphto create the NXOntology allows for retaining additional relationship types, likepart ofin the case of the Gene Ontology.NXOntology reverses the direction of relationships so edges go from superterm to subterm.
Traditionally in ontologies, theis arelationships go from subterm to superterm,
but this is confusing.
NXOntology reverses edges so functions such asancestorsrefer to more general concepts anddescendantsrefer to more specific concepts.Thenxontology.imports.multidigraph_to_digraphfunction converts from a MultiDiGraph, like the one produced by obonet, to a DiGraph by filtering to the desired relationship types, reversing edges, and collapsing parallel edges.InstallationThe recommended approach is to install the latest release fromPyPIusing:pipinstallobonetHowever, if you'd like to install the most recent version from GitHub, use:pipinstallgit+https://github.com/dhimmel/obonet.git#egg=obonetContributingWe welcome feature suggestions and community contributions.
Currently, only reading OBO files is supported.DevelopSome development commands:# create virtual environmentpython3-mvenv./env# activate virtual environmentsourceenv/bin/activate# editable installation for developmentpipinstall--editable".[dev]"# install pre-commit hookspre-commitinstall# run all pre-commit checkspre-commitrun--all# run testspytest# generate changelog for release notesgitfetch--tagsoriginmainOLD_TAG=$(gitdescribe--tags--abbrev=0)gitlog--oneline--decorate=no--reverse$OLD_TAG..HEADMaintainers can make a new release athttps://github.com/dhimmel/obonet/releases/new.