---
title: "splots: visualization of data from assays in microtitre plate or slide format"
author: "Wolfgang Huber"
package: splots
date: "2021-01-04"
output: 
    BiocStyle::html_document
vignette: >
  %\VignetteIndexEntry{splots: visualization of data from assays in microtitre plate or slide format}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}  

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = TRUE, autodep = TRUE)
set.seed(0xdada)
```

This package is provided to support legacy code and reverse dependencies, but it should not be used as a dependency for new code development. It provides a single function, `plotScreen`, for visualising data in microtitre plate or slide format. As a better alternative for such functionality, please consider the `r CRANpkg("platetools")` package (see also https://github.com/Swarchal/platetools), or generic `r CRANpkg("ggplot2")` graphics functionality. 
  
Here we give a short demo. First, we load and explore some example data.

```{r exampledata, message=FALSE, warning=FALSE} 
data("featuresPerWell", package = "HD2013SGI")
str(featuresPerWell[[1]])
str(featuresPerWell[[2]])
stopifnot(nrow(featuresPerWell[[1]]) == nrow(featuresPerWell[[2]]))
```

We use a subset of these data and assemble them into the list `xl`. The details of how this is done in the following code chunk are arcane and are likely not to matter much to you; what is important is that you bring your data into the same format.

```{r xl, results = "hide"}
np = 40
nx = 24
ny = 16
plateNames = unique(featuresPerWell[[1]]$plate)
assertthat::assert_that(length(plateNames) >= np)
plateNames = plateNames[seq_len(np)]
xl = lapply(plateNames, function(pl) {
  sel = with(featuresPerWell[[1]], plate == pl & field == "1")
  rv = rep(NA_real_, nx * ny)
  r = match(featuresPerWell[[1]]$row[sel], LETTERS)
  c = match(featuresPerWell[[1]]$col[sel], paste(seq_len(nx)))
  i = (r-1) * nx + c
  assertthat::assert_that(!any(is.na(r)), !any(is.na(c)), !any(duplicated(i)),
                          all(r>=1), all(r<=ny), all(c>=1), all(c<=nx))
  rv[i] = featuresPerWell[[2]][sel, "count"]
  rv
})
names(xl) = plateNames
```

So this the object `xl` that is passed into `plotScreen`:

```{r fig1, fig.wide = TRUE, fig.cap = "Output of `splots::plotScreen`.", , fig.dim=c(10, 20)}
class(xl)
length(xl)
names(xl)[1:4]
unique(vapply(xl, function(x) paste(class(x), length(x)), character(1)))
xl[[1]][1:30]
splots::plotScreen(xl, nx = nx, ny = ny, ncol = 4, 
           fill = c("white", "darkgoldenrod4"), 
           main = "HD2013SGI", legend.label = "cell count",
           zrange = c(0, max(unlist(xl), na.rm = TRUE)))
```


```{r sessionInfo}
sessionInfo()
``` 
