---
title: "Exploring the OMOP CDM vocabulary tables"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{a02_ExploreCDMvocabulary}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

```{r, include = FALSE}
NOT_CRAN <- identical(tolower(Sys.getenv("NOT_CRAN")), "true")
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = NOT_CRAN
)
```

```{r, include = FALSE}
CDMConnector::requireEunomia("synpuf-1k", "5.3")
```

In this vignette, we will explore the functions that help us delve into the vocabularies used in our database. These functions allow us to explore the different vocabularies and concepts characteristics.

First of all, we will load the required packages and a eunomia database.
```{r, warning=FALSE, message=FALSE}
library(DBI)
library(dplyr)
library(CDMConnector)
library(CodelistGenerator)

# Connect to the database and create the cdm object
con <- dbConnect(duckdb::duckdb(), 
                 eunomiaDir("synpuf-1k", "5.3"))
cdm <- cdmFromCon(con = con, 
                  cdmName = "Eunomia Synpuf",
                  cdmSchema   = "main",
                  writeSchema = "main", 
                  achillesSchema = "main")
```

Note that we have included [achilles tables](https://github.com/OHDSI/Achilles) in our cdm reference, which are used for some of the analyses.

## Vocabulary characteristics

We can first start by getting the vocabulary version of our CDM object:
```{r}
vocabularyVersion(cdm)
```

And the available vocabularies, which correspond to the column *vocabulary_id* from the concept table:
```{r}
availableVocabularies(cdm)
```

## Domains

We can also explore the domains that our CDM object has, which is the column *domain_id* from the concept table:
```{r}
availableDomains(cdm)
```

or restrict the search among *standard* concepts:
```{r}
availableDomains(cdm,
                 standardConcept = "Standard")
```

## Concept class

We can further explore the different classes that we have (reported in *concept_class_id* column from the concept table):
```{r}
availableConceptClassIds(cdm)
```

Or restrict the search among *non-standard* concepts with *condition* domain:
```{r}
availableConceptClassIds(cdm, 
                         standardConcept = "Non-standard", 
                         domain = "Condition")
```

## Relationships

We can also explore the different relationships that are present in our CDM:
```{r}
availableRelationshipIds(cdm)
```

Or narrow the search among *standard* concepts with domain *observation*:
```{r}
availableRelationshipIds(cdm,
                         standardConcept1 = "Standard",
                         standardConcept2 = "Standard",
                         domains1 = "Observation",
                         domains2 = "Observation")
```
