---
title: "Gallery of mascarade-generaded masks"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Gallery of mascarade-generaded masks}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 7, fig.height=5)

if (!isTRUE(as.logical(Sys.getenv("NOT_CRAN")))) {
    # on CRAN: return NULL with message
    readRDS <- function(file) {
        origin <- if (is.character(file)) {
            file
        } else if (inherits(file, "connection")) {
            tryCatch(summary(file)$description, error = function(e) "connection object")
        } else {
            paste(deparse(substitute(file)), collapse = " ")
        }
        
        handler <- function(e) {
            message("Could not download example data from ", origin,
                    " (", conditionMessage(e), "). Skipping this example.")
            NULL
        }
        tryCatch({
            base::readRDS(file)
        }, error = handler, warning = handler)
    }
}
```

### Loading necessary libraries

```{r message=FALSE}
library(mascarade)
library(data.table)
library(ggplot2)
library(colorrepel)
```

### PBMC-3K UMAP

```{r}
example <- readRDS(url("https://alserglab.wustl.edu/files/mascarade/examples/pbmc3k_umap.rds"))
```

```{r eval=!is.null(example)}
data <- data.table(example$dims, 
                   cluster=example$clusters)

maskTable <- generateMask(dims=example$dims, 
                          clusters=example$clusters)

ggplot(data, aes(x=UMAP_1, y=UMAP_2)) + 
    geom_point(aes(color=cluster)) + 
    geom_path(data=maskTable, aes(group=group)) +
    coord_fixed() + 
    theme_classic()
                               
```

With labels:

```{r eval=!is.null(example)}
ggplot(data, aes(x=UMAP_1, y=UMAP_2)) + 
    geom_point(aes(color=cluster)) + 
    fancyMask(maskTable, ratio=1, linewidth = 0) +
    theme_classic() + theme(legend.position = "none")
```

### PBMC-3K t-SNE

```{r}
example <- readRDS(url("https://alserglab.wustl.edu/files/mascarade/examples/pbmc3k_tsne.rds"))
```

```{r eval=!is.null(example)}
data <- data.table(example$dims, 
                   cluster=example$clusters)

maskTable <- generateMask(dims=example$dims, 
                          clusters=example$clusters)

ggplot(data, aes(x=tSNE_1, y=tSNE_2)) + 
    geom_point(aes(color=cluster)) + 
    geom_path(data=maskTable, aes(group=group)) +
    coord_fixed() + 
    theme_classic()
                               
```


With labels:

```{r eval=!is.null(example)}
ggplot(data, aes(x=tSNE_1, y=tSNE_2)) + 
    geom_point(aes(color=cluster)) + 
    fancyMask(maskTable, ratio=1, linewidth = 0) +
    theme_classic() + theme(legend.position = "none")
```


### Aya

```{r}
example <- readRDS(url("https://alserglab.wustl.edu/files/mascarade/examples/aya.rds"))
```

```{r eval=!is.null(example), message=FALSE}
data <- data.table(example$dims, 
                   cluster=example$clusters)

maskTable <- generateMask(dims=example$dims, 
                          clusters=example$clusters)

ggplot(data, aes(x=UMAP_1, y=UMAP_2)) + 
    geom_point(aes(color=cluster), size=0.5) + 
    scale_color_repel() +
    geom_path(data=maskTable, aes(group=group)) +
    coord_fixed() + 
    theme_classic()
                               
```

### Chia-Jung

```{r}
example <- readRDS(url("https://alserglab.wustl.edu/files/mascarade/examples/chiajung1.rds"))
```

```{r eval=!is.null(example), message=FALSE}
data <- data.table(example$dims, 
                   cluster=example$clusters)

maskTable <- generateMask(dims=example$dims, 
                          clusters=example$clusters)

ggplot(data, aes(x=UMAP_1, y=UMAP_2)) + 
    geom_point(aes(color=cluster), size=0.1) + 
    scale_color_repel() +
    geom_path(data=maskTable, aes(group=group)) +
    coord_fixed() + 
    theme_classic()
                               
```

```{r}
example <- readRDS(url("https://alserglab.wustl.edu/files/mascarade/examples/chiajung2.rds"))
```

```{r eval=!is.null(example), message=FALSE}
data <- data.table(example$dims, 
                   cluster=example$clusters)

maskTable <- generateMask(dims=example$dims, 
                          clusters=example$clusters)

ggplot(data, aes(x=UMAP_1, y=UMAP_2)) + 
    geom_point(aes(color=cluster)) + 
    scale_color_repel() +
    geom_path(data=maskTable, aes(group=group)) +
    coord_fixed() + 
    theme_classic()
                               
```

### Vladimir Shitov

Really hard case, so playing with parameters a bit to make more space.

```{r}
example <- readRDS(url("https://alserglab.wustl.edu/files/mascarade/examples/vshitov.rds"))
```

```{r eval=!is.null(example), fig.height=7, fig.width=8, message=FALSE}
data <- data.table(example$dims, 
                   cluster=example$clusters)

maskTable <- generateMask(dims=example$dims, 
                          clusters=example$clusters)

ggplot(data, aes(x=UMAP1, y=UMAP2)) + 
    geom_point(aes(color=cluster), size=0.1) + 
    scale_color_repel() +
    fancyMask(maskTable, 
              con.type = "line", # ledges clutter the image visually here
              ratio=1, 
              linewidth = 0, 
              limits.expand = c(0.2, 0.1), # more space to the left and right of the plot
              label.buffer = unit(1, "mm"), # smaller buffer zone around clusters
              label.width = unit(25, "mm"), # wrap long lines
              label.margin = margin(1, 1, 1, 1, "pt"), # tighter label margins
              label.fontsize = 10) +
    theme_classic() + theme(legend.position = "none")
                               
```

### Session info

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