---
title: "LoadPancreasImages"
author: "Nicolas Damond"
date: "`r Sys.Date()`"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = file.path("..", "extdata"))
```


# Script to download images and masks from the pancreas IMC dataset and format them as CytoImageList objects

- Publication: [Damond et al. A Map of Human Type 1 Diabetes Progression by Imaging Mass Cytometry. Cell Metab. 2019 Mar 5;29(3):755-768](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6821395)
- Dataset: [accessible from Mendeley Data](http://dx.doi.org/10.17632/cydmwsfztj.2)

```{r load-packages, message=FALSE}
library(EBImage)
library(cytomapper)
```

# Download the images and load them as a CytoImageList object

Here, a subset of 100 images from the pancreas IMC dataset is downloaded.

```{r load-images}
# Download the zipped folder image and unzip it
url.images <- ("https://data.mendeley.com/public-files/datasets/cydmwsfztj/files/b37054d2-d5d0-4c48-a001-81ff77136f41/file_downloaded")
download.file(url.images, destfile = "ImageSubset.zip")
unzip("ImageSubset.zip")
file.remove("ImageSubset.zip")

# Load the images as a CytoImageList object
images <- loadImages("./", pattern="_full_clean.tiff")
images
```

# Download the cell masks and load them as a CytoImageList object

```{r load-masks}
# Download the zipped folder image and unzip it
url.masks <- ("https://data.mendeley.com/public-files/datasets/cydmwsfztj/files/13679a61-e9b4-4820-9f09-a5bbc697647c/file_downloaded")
download.file(url.masks, destfile = "Masks.zip")
unzip("Masks.zip")
file.remove("Masks.zip")

# Load the images as a CytoImageList object
masks <- loadImages("./", pattern="_full_mask.tiff")
masks
```

# Remove tiff files from the root directory

```{r clean-up-2, message = FALSE}
# Remove image stacks
images.del <- list.files("./", pattern="_full_clean.tiff")
file.remove(images.del)

# Remove masks
masks.del <- list.files("./", pattern="_full_mask.tiff")
file.remove(masks.del)
```

# Load panel data

The panel contains antibody-related metadata.
The channel-mass file is used to match panel information and image stack slices.

```{r load-panel}
# Import panel
url.panel <- ("https://data.mendeley.com/public-files/datasets/cydmwsfztj/files/2f9fecfc-b98f-4937-bc38-ae1b959bd74d/file_downloaded")
download.file(url.panel, destfile = "panel.csv")
panel <- read.csv("panel.csv")

# Import channel-mass file
url.channelmass <- ("https://data.mendeley.com/public-files/datasets/cydmwsfztj/files/704312eb-377c-42e2-8227-44bb9aca0fb3/file_downloaded")
download.file(url.channelmass, destfile = "ChannelMass.csv")
channel.mass <- read.csv("ChannelMass.csv", header = FALSE)
```

# Scale masks

The masks are 16-bit images and need to be re-scaled.
We then convert the mask values to integer.

```{r scale-masks}
masks <- scaleImages(masks, (2 ^ 16) - 1)
```

# Add image names for images and masks

This information is stored in the metadata columns of the CytoImageList objects
and is used by SingleCellMapper to match single cell data, images and mask

```{r add-image-names}
mcols(images)$ImageName <- gsub("_a0_full_clean", "", names(images))
mcols(masks)$ImageName <- gsub("_a0_full_mask", "", names(masks))
```

# Subset the masks to keep only masks matching an image in `images`

```{r subset-masks}
masks <- masks[mcols(masks)$ImageName %in% mcols(images)$ImageName]
identical(mcols(masks)$ImageName, mcols(images)$ImageName)
```

# Add protein short names as channel names

```{r add-channel-names}
# Match panel and stack slice information
panel <- panel[panel$full == 1,]
panel <- panel[match(channel.mass[,1], panel$MetalTag),]

# Add channel names to the  image stacks CytoImageList object
channelNames(images) <- panel$shortname
```

# Save the CytoImageList objects

```{r save}
saveRDS(images, "pancreas_images.rds")
saveRDS(masks, "pancreas_masks.rds")
```

# Delete unneeded CSV files from the extdata directory

```{r clean-up, message = FALSE}
file.remove("panel.csv", "ChannelMass.csv")
```
