---
title: "Supplemental Code: Empowering Large Chemical Knowledge Bases for Exposomics: PubChemLite meets MetFrag"
author: Emma L. Schymanski, Steffen Neumann, Todor Kondic, Paul Thiessen, Jian (Jeff) Zhang, Evan E. Bolton
date: "26 Oct 2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Libraries and Helper

The visualisation relies on the `plotly` package and benefits from some advanced data table functions.

```{r libraries, results="hide", message=FALSE}
library(data.table)
library(plotly)
library(knitr)
library(kableExtra)
source_file_url <- "https://git-r3lab.uni.lu/eci/pubchem/-/raw/master/pubchemlite/R/visualise/as_sunburstDF.R"
download.file(source_file_url,"as_sunburstDF.R")
source("as_sunburstDF.R")
```

## Read TOC Tree fingerprints

The file `tier1_bits.tsv.gz` contains the Table of Contents fingerprint (TOC FP) where each bit represents presence or absence of information in that category for a compound, downloaded from the ECI GitLab pages (Jan 14, 2020 tier1 example) or as a local file.

```{r get_data}

fp_url <- "https://git-r3lab.uni.lu/eci/pubchem/-/raw/master/pubchemlite/R/visualise/tier1_bits.tsv.gz"

cidfp <- read.delim(textConnection(readLines(gzcon(url(fp_url)))), 
                    skip=1, stringsAsFactors = FALSE, colClasses="character")

# give this nicer column names
colnames(cidfp) <- c("X","AgroChemInfo","BioPathway", "DrugMedicInfo", 
                     "FoodRelated", "PharmacoInfo", "SafetyInfo", "ToxicityInfo",
                     "KnownUse")

#cidfp <- read.delim("/tmp/pubchem/tier1_bits.tsv.gz", skip=1, stringsAsFactors = #FALSE, colClasses="character")

kable(cidfp[1:11,], row.names = FALSE) %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
```

Due to the layout of the fingerprints, they need some reformatting and counting to produce a nice summary

```{r summarize_data}
toplevel <- t(apply(cidfp[,-1], MARGIN = 1, 
                    FUN=function(r) sapply(r, function(x) grepl("1", x))))

subcatcount <- lapply(colnames(toplevel), function(x) {
  topcat <- cidfp[toplevel[,x], x]
  bitlength <- nchar(topcat[1])
  m <- do.call(rbind, strsplit(topcat, ""))
  storage.mode(m) <- "numeric"
  as.data.frame(list(toplevel=x, 
                     subcat=as.character(seq(1:ncol(m))), counts=colSums(m)))
})

kable(toplevel[1:11,], row.names = FALSE) %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"))

kable(subcatcount[[1]], row.names = FALSE) %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"))

```

With that information it is possible to create a sunburst plot:

```{r plot_sunburst}

df <- do.call(rbind, subcatcount)
#NOTE: to make a plot with readable categories, add 20000 to each by
# uncommenting the next line! This distorts the total counts, however. 
#df [,"counts"] <- df [,"counts"] + 20000
sunburstDF <- as.sunburstDF(data.table(df), valueCol = "counts")

p <- plot_ly(data = sunburstDF, 
        ids = ~ids, labels= ~labels, parents = ~parents, values= ~values, 
        type='sunburst', branchvalues = 'total',
        hovertemplate="%{label}:\n Count: %{value}<extra></extra>") %>%
          style(hoverlabel = list(align = "right"))
p

htmlwidgets::saveWidget(as_widget(p), "sunburst.html")


```

## Session Info

This document was created with the R packages shown below.


```{r sessionInfo, echo=FALSE}
sessionInfo()
```
