ontologyIndex has a function to create an ontology_index object from a given .obo file. The package comes with the such an object, hpo, encapsulating the Human Phenotype Ontology.

suppressPackageStartupMessages(library(ontologyIndex))
data(hpo)

To use an up-to-date version, download the relevant .obo file and read it into R using the function get_ontology, passing it the file name and the correct qualifier (e.g. "HP" for the HPO.

ontology <- get_ontology(file, qualifier)

The object is just a list of vectors and lists term properties, indexed by the IDs of the terms.

##    property     class
## 1        id character
## 2      name character
## 3   parents      list
## 4    alt_id character
## 5  children      list
## 6 ancestors      list
## 7   version character

Thus you can look up properties for a given term using [ and [[ as appropriate.

hpo$name["HP:0001873"]
##         HP:0001873 
## "Thrombocytopenia"
hpo$ancestors[["HP:0001873"]]
## [1] "HP:0000001" "HP:0000118" "HP:0001871" "HP:0001872" "HP:0011873"
## [6] "HP:0001873"
hpo$name[hpo$ancestors[["HP:0001873"]]]
##                                       HP:0000001 
##                                            "All" 
##                                       HP:0000118 
##                         "Phenotypic abnormality" 
##                                       HP:0001871 
## "Abnormality of blood and blood-forming tissues" 
##                                       HP:0001872 
##                    "Abnormality of thrombocytes" 
##                                       HP:0011873 
##                        "Abnormal platelet count" 
##                                       HP:0001873 
##                               "Thrombocytopenia"

Removing redundant terms

A set of terms, i.e. a character vector of term IDs, may contain ancestor-descendant pairs. The function minimal_set removes such pairs so as to leave a minimal set of terms, in the sense of the ontology’s directed acyclic graph.

minimal_set(hpo, c("HP:0001871", "HP:0001873", "HP:0011877"))
## [1] "HP:0001873" "HP:0011877"

Finding all ancestors of a set of terms

get_ancestors(hpo, c("HP:0001873", "HP:0011877"))
## [1] "HP:0000001" "HP:0000118" "HP:0001871" "HP:0001872" "HP:0011873"
## [6] "HP:0001873" "HP:0011876" "HP:0011877"