---
title: "Import ToxPi GUI Files"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Import ToxPi GUI Files}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

This vignette will show how to load a data file that was saved using the ToxPi Java GUI, which can be downloaded from [here](https://toxpi.org/). The ToxPi Java GUI will save data files using file format "C" described in the ToxPi User Manual. This vignette will use the "format_C.csv" file to demonstrate how to import GUI data.

```{r setup}
library(toxpiR)

## Create a tempfile and download 'format_C.csv'
fmtc <- tempfile()
ghuc <- "https://raw.githubusercontent.com"
fmtcUrl <- file.path(ghuc, "ToxPi", "ToxPi-example-files", "main", "format_C.csv")
download.file(url = fmtcUrl, destfile = fmtc, quiet = TRUE)
```

The "format_C.csv" model specification reuses metrics across different slices. 
In general, we do not recommend duplicating inputs across slices, so the user gets a warning when creating a model with duplicate inputs.

```{r}
## Import file into R
gui <- txpImportGui(fmtc)

```

The resulting `list` object contains: `$model`, a `TxpModel` object with the model specifications; `$input`, a `data.frame` containing the data for calculating ToxPi scores; and `$fills`, an array of slice colors for plotting. 

```{r}
gui$model
gui$input
gui$fills
```

We calculate ToxPi scores using the `txpCalculateScores` function, which takes a model and input `data.frame`.
Note that by default the ToxPi GUI does not accept negative values. However, the package keeps them by default. 
To replicate the GUI functionailty, we set `negative.value.handling = "missing"`. 

```{r}
## Calculate ToxPi scores
res <- txpCalculateScores(model = gui$model, input = gui$input, id.var = "Name",negative.value.handling = "missing")

## Overall ToxPi scores
txpScores(res)

## Slice scores
txpSliceScores(res, adjusted = FALSE)
```

A results output similar to that given by the Java GUI can be obtained by combining score components.

```{r}
out <- as.data.frame(res, adjusted = FALSE)
out <- out[order(out$score, decreasing = TRUE), ]
out
```

ToxPi images and overall score rank plot can also be produced.

```{r fig.width = 7}
plot(sort(res), fills = gui$fills)
```

```{r fig.width = 7, fig.height = 4}
plot(res, txpRanks(res))
plot(res, txpRanks(res), labels = 1:10, pch = 16, size = grid::unit(0.75, "char"))
```

The basic clustering methods offered in the Java GUI can also be recreated.

```{r fig.width = 7, fig.height = 5}
## Hierarchical Clustering
hc <- hclust(dist(txpSliceScores(res)), method = 'complete')
plot(hc, hang = -1, labels = txpIDs(res), xlab = 'Name', sub = '')
```

```{r fig.width = 7, fig.height = 5}
## K-Means Clustering, plotted using principal components
nClusters <- 3
km <- kmeans(txpSliceScores(res), nClusters)
pc <- prcomp(txpSliceScores(res))
coord <- predict(pc) * -sum(txpWeights(res))
plot(coord[,1], coord[,2], col = km$cluster, 
     xlab = 'PC1', ylab = 'PC2', pch = 16)
```
