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

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.

# 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

# 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

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

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

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

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

masks <- masks[mcols(masks)$ImageName %in% mcols(images)$ImageName]
identical(mcols(masks)$ImageName, mcols(images)$ImageName)

Add protein short names as 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

saveRDS(images, "pancreas_images.rds")
saveRDS(masks, "pancreas_masks.rds")

Delete unneeded CSV files from the extdata directory

file.remove("panel.csv", "ChannelMass.csv")


BodenmillerGroup/SingleCellMapper documentation built on July 2, 2024, 4:31 p.m.