---
title: "uberonpeek -- a look at UBERON ontology, etc., with ontoProc2"
author: "Vincent J. Carey, stvjc at channing.harvard.edu"
date: "`r format(Sys.time(), '%B %d, %Y')`"
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{uberonpeek -- a look at UBERON ontology, etc., with ontoProc2}
  %\VignetteEncoding{UTF-8}
output:
  BiocStyle::html_document:
    highlight: pygments
    number_sections: yes
    theme: united
    toc: yes
---

# Introduction

The ontoProc2 package is designed to give convenient
access to the ontologies that are transformed to "semantic
SQL" in the INCAtools project.

We'll start by retrieving the current UBERON ontology and examining
some tables and "statements".

```{r setup,message=FALSE}
library(ontoProc2)
library(DBI)
library(dplyr)
ubss <- semsql_connect(ontology = "uberon")
report(ubss)
ubcon <- ubss@con
head(dbListTables(ubcon))
tbl(ubcon, "statements")
```

# Parent-child relations

CRAN's ontologyIndex package provides a familiar representation that
simplifies visualization.

```{r doconv, cache=FALSE}
uboi <- semsql_to_oi(ubcon)
uboi
uboi$name[10364:10370]
```

A sense of the variety of ontological cross-references
present can be given by tabling the tag prefixes.
```{r lktags}
prefs <- gsub(":.*", "", names(uboi$name))
table(prefs)
```

By using the ancestors component we can obtain
a view of is-a relations (presumably developed from
rdfs:subClassOf predicates).  We've chosen as
terminal tags the tags for heart, kidney, and cortex of kidney.
```{r doplot, fig.width=10}
onto_plot2(
  uboi,
  unlist(uboi$ancestors[c(
    "UBERON:0002189",
    "UBERON:0002113", "UBERON:0000948"
  )])
)
```

# Bridging to MONDO for disease terminology

With our knowledge of the tag for "heart", we can
enumerate formal terms for diseases affecting this organ.

```{r lkmondo}
mon = semsql_connect(ontology="mondo")
tbl(mon@con, "entailed_edge") |> 
   filter(object == "UBERON:0000948") |> 
   filter(subject %like% "MONDO%") |> 
   inner_join( tbl(mon@con, "rdfs_label_statement"), by="subject") |> 
   as.data.frame() |> select(subject, value) |> distinct() |> DT::datatable()
```


# Bridging to CL for cell type enumeration

```{r docross}
cl = semsql_connect(ontology="cl")
tbl(ubcon, "entailed_edge") |> 
   filter(object == "UBERON:0000948") |> 
   filter(subject %like% "CL:%") |> 
   inner_join( tbl(cl@con, "rdfs_label_statement"), by="subject", copy="temp-table") |> 
   as.data.frame() |> select(subject, value) |> distinct() |> DT::datatable()
```

Exercise: create a map from cardiac diseases to associated cardiac cell types.

# Session information

```{r lksess}
sessionInfo()
```

