<commit_before>## A python parser for OBO ontology files

[![Build Status](https://travis-ci.org/dhimmel/obo.svg?branch=master)](https://travis-ci.org/dhimmel/obo)

This repository contains python code for handling OBO serialized ontologies.

The function `obo.read_obo()` takes an `.obo` file and returns a [networkx MultiDiGraph](http://networkx.readthedocs.io/en/stable/reference/classes.multidigraph.html) representation of the ontology.
The parser aims to be compatible with OBO versions [1.2](https://owlcollab.github.io/oboformat/doc/GO.format.obo-1_2.html) and [1.4](https://owlcollab.github.io/oboformat/doc/GO.format.obo-1_4.html).
<commit_msg>Improve README. Add example usage<commit_after>## A python parser for OBO ontology files

[![Build Status](https://travis-ci.org/dhimmel/obo.svg?branch=master)](https://travis-ci.org/dhimmel/obo)

This repository contains a python package for handling OBO serialized ontologies.
The function `obo.read_obo()` takes an `.obo` file and returns a [`networkx.MultiDiGraph`](http://networkx.readthedocs.io/en/stable/reference/classes.multidigraph.html) representation of the ontology.

The parser aims to be compatible with OBO versions [1.2](https://owlcollab.github.io/oboformat/doc/GO.format.obo-1_2.html) and [1.4](https://owlcollab.github.io/oboformat/doc/GO.format.obo-1_4.html).

## Usage

This package is designed and tested on python ≥ 3.4.
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:

```python
import networkx
import obo

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

# Or read the xz-compressed taxrank ontology
url = 'https://github.com/dhimmel/obo/raw/master/tests/data/taxrank.obo.xz'
graph = obo.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['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')
```

## Installation

The latest version of the package can be installed using `pip` with:

```sh
pip install git+https://github.com/dhimmel/obo.git#egg=obo
```

## Contributing

{'fraction_non_alphanumeric': 0.12475168851807708, 'fraction_numerical': 0.015494636471990465, 'mean_word_length': 5.038369304556355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 11, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2156550', 'n_tokens_mistral': 910, 'n_tokens_neox': 831, 'n_words': 226}