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. 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.

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.

## 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.

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".

## 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.

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.

plot(sort(res), fills = gui$fills)
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.

## Hierarchical Clustering
hc <- hclust(dist(txpSliceScores(res)), method = 'complete')
plot(hc, hang = -1, labels = txpIDs(res), xlab = 'Name', sub = '')
## 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)


ToxPi/toxpiR documentation built on Sept. 4, 2024, 5:55 p.m.