Skip to main content

Parse OBO formatted ontologies into networkx

Project description

obonet: load OBO-formatted ontologies into networkx

GitHub Actions CI Build Status
Software License
PyPI

Read OBO-formatted ontologies in Python. obonet is

  • user friendly
  • succinct
  • pythonic
  • modern
  • simple and tested
  • lightweight
  • networkx leveraging

This Python package loads OBO serialized ontologies into networks. The function obonet.read_obo() takes an .obo file and returns a networkx.MultiDiGraph representation of the ontology. The parser was designed for the OBO specification version 1.2 & 1.4.

Usage

See pyproject.toml for 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:

import networkx
import obonet

# Read the taxrank ontology
url = 'https://github.com/dhimmel/obonet/raw/main/tests/data/taxrank.obo'
graph = obonet.read_obo(url)

# Or read the xz-compressed taxrank ontology
url = 'https://github.com/dhimmel/obonet/raw/main/tests/data/taxrank.obo.xz'
graph = obonet.read_obo(url)

# Number of nodes
len(graph)

# Number of edges
graph.number_of_edges()

# Check if the ontology is a DAG
networkx.is_directed_acyclic_graph(graph)

# Mapping from term ID to name
id_to_name = {id_: data.get('name') for id_, data in graph.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 the Gene Ontology example notebook.

Comparison

This package specializes in reading OBO files into a newtorkx.MultiDiGraph. A more general ontology-to-NetworkX reader is available in the Python nxontology package via the nxontology.imports.pronto_to_multidigraph function. This function takes a pronto.Ontology object, which can be loaded from an OBO file, OBO Graphs JSON file, or Ontology Web Language 2 RDF/XML file (OWL). Using pronto_to_multidigraph allows creating a MultiDiGraph similar to the created by obonet, with some differences in the amount of metadata retained.

The primary focus of the nxontology package is to provide an NXOntology class for representing ontologies based around a networkx.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:

  1. 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 considers is a / rdfs:subClassOf relationships, but using pronto_to_multidigraph to create the NXOntology allows for retaining additional relationship types, like part of in the case of the Gene Ontology.

  2. NXOntology reverses the direction of relationships so edges go from superterm to subterm. Traditionally in ontologies, the is a relationships go from subterm to superterm, but this is confusing. NXOntology reverses edges so functions such as ancestors refer to more general concepts and descendants refer to more specific concepts.

The nxontology.imports.multidigraph_to_digraph function 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.

Installation

The recommended approach is to install the latest release from PyPI using:

pip install obonet

However, if you'd like to install the most recent version from GitHub, use:

pip install git+https://github.com/dhimmel/obonet.git#egg=obonet

Contributing

GitHub issues

We welcome feature suggestions and community contributions. Currently, only reading OBO files is supported.

Develop

Some development commands:

# create virtual environment
python3 -m venv ./env

# activate virtual environment
source env/bin/activate

# editable installation for development
pip install --editable ".[dev]"

# install pre-commit hooks
pre-commit install

# run all pre-commit checks
pre-commit run --all

# run tests
pytest

# generate changelog for release notes
git fetch --tags origin main
OLD_TAG=$(git describe --tags --abbrev=0)
git log --oneline --decorate=no --reverse $OLD_TAG..HEAD

Maintainers can make a new release at https://github.com/dhimmel/obonet/releases/new.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

obonet-1.1.1.tar.gz (25.7 kB view details)

Uploaded Source

Built Distribution

obonet-1.1.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file obonet-1.1.1.tar.gz.

File metadata

  • Download URL: obonet-1.1.1.tar.gz
  • Upload date:
  • Size: 25.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for obonet-1.1.1.tar.gz
Algorithm Hash digest
SHA256 0dbc742f078d5a9bb89c7ae8dc66535e944de08dd4ead24854d340d882cfa275
MD5 e7ac44f43b8c91e88cb7ea2b1eadf8d2
BLAKE2b-256 a92ad2129fbb8f8015df90f9755bd8cd3cf63ab53ca21a07b6371875716b31a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for obonet-1.1.1.tar.gz:

Publisher: release.yaml on dhimmel/obonet

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file obonet-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: obonet-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for obonet-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0513a589e27fd29856a3e6d4660f53cbdecf601319aaae4635b59a9795e68bc7
MD5 5906671000910835b90f886361f86047
BLAKE2b-256 a386ae23b7033126c05b7a75b06796b3627a8dc866df2de2f4a30b8796863675

See more details on using hashes here.

Provenance

The following attestation bundles were made for obonet-1.1.1-py3-none-any.whl:

Publisher: release.yaml on dhimmel/obonet

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page