---
title: "Using Rdiagnosislist functions with custom hierarchies"
author: "Anoop Shah"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Using Rdiagnosislist functions with custom hierarchies}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

Using Rdiagnosislist functions with custom hierarchies
======================================================

The Rdiagnosislist functions can be used with a 'fake' SNOMED environment to show hierarchical relationships with other coding systems, such as ICD-10.

In order to do this, you can modify the sample SNOMED environment by replacing the following three tables:

- CONCEPT - one row per concept, each with a unique ID.
- DESCRIPTION - descriptions for each concept. At the simplest level, this may be one description per concept.
- RELATIONSHIP - relationship between the concepts. At the simplest level, this may consist solely of 'Is a' relationships (parent/child).

This example models a small section of the ICD-10 hierarchy for heart failure:
- IX Diseases of the circulatory system (icdchapter)
- I50 Heart failure (icd3)
- I50.0 Congestive heart failure (leaf)
- I50.1 Left ventricular failure (leaf)
- I50.9 Heart failure, unspecified (leaf)
- I11.0 Hypertensive heart disease with (congestive) heart failure (leaf)
- Heart failure (phenotype)

The concepts are of the following semantic types:
- leaf - a 4-character ICD-10 code which can be used for coding
- icd3 - a 3-character ICD-10 category
- icdchapter - lettered ICD-10 chapter
- phenotype - a set of leaf codes that constitute a research phenotype

As there is only one description per concept in this example, the data can be specified by two tables: a 'description' table and a 'relations' table.

```{r}
# Load package
require(Rdiagnosislist)
require(data.table)

# Use one thread only for CRAN
data.table::setDTthreads(threads = 1)

CUSTOM <- sampleSNOMED()

NEW_DESCRIPTION <- data.table::data.table(
  conceptId = bit64::as.integer64(1:7),
  term = c(
  'IX Diseases of the circulatory system (icdchapter)',
  'I50 Heart failure (icd3)',
  'I50.0 Congestive heart failure (leaf)',
  'I50.1 Left ventricular failure (leaf)',
  'I50.9 Heart failure, unspecified (leaf)',
  'I11.0 Hypertensive heart disease with (congestive) heart failure (leaf)',
  'Heart failure (phenotype)'
  ))
NEW_RELATIONSHIP <- data.table::data.table(
  parent = bit64::as.integer64(c(1,2,2,2,2,7,7)),
  child =  bit64::as.integer64(c(2,3,4,5,6,2,6)))

CUSTOM$CONCEPT <- NEW_DESCRIPTION[, list(id = conceptId,
  effectiveTime = as.Date(Sys.Date()), active = TRUE,
  moduleId = 0, definitionStatusId = 0)]
CUSTOM$DESCRIPTION <- NEW_DESCRIPTION[, list(id = conceptId,
  effectiveTime = as.Date(Sys.Date()), active = TRUE,
  moduleId = 0, conceptId = conceptId, languageCode = 'en',
  typeId = bit64::as.integer64('900000000000003001'), # fully specified name
  term, caseSignificanceId = 0)]
CUSTOM$STATEDRELATIONSHIP <- NEW_RELATIONSHIP[, list(id = 1:.N,
  effectiveTime = as.Date(Sys.Date()), active = TRUE,
  moduleId = 0, sourceId = child, destinationId = parent,
  relationshipGroup = 0, typeId = bit64::as.integer64('116680003'), # is a
  characteristicTypeId = 0, modifierId = 0)]
CUSTOM$RELATIONSHIP <- CUSTOM$STATEDRELATIONSHIP[0]

# Using the new dictionaries
myconcept <- SNOMEDconcept('Diseases of the circulatory system',
  exact = FALSE, SNOMED = CUSTOM)
myphenotype <- SNOMEDconcept('Heart failure (phenotype)',
  SNOMED = CUSTOM)
  
# Show the concept using the new DESCRIPTION table
description(myconcept, SNOMED = CUSTOM)

# Create and view a codelist based on ICD10 and the phenotype
mycodelist <- SNOMEDcodelist(c(myconcept, myphenotype),
  include_desc = TRUE, SNOMED = CUSTOM)

# Create HTML codelist (not run)
# temp = paste0(tempdir(), 'test.html')
# htmlCodelistHierarchy(mycodelist, file = temp, SNOMED = CUSTOM)
# system(paste0('firefox ', temp, ' &')) # open in firefox (on Linux)
```
