Last updated: 2020-11-18

Checks: 7 0

Knit directory: cytomapper_publication/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20200602) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 6ccdd51. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rproj.user/

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/01-LoadPancreasData.Rmd) and HTML (docs/01-LoadPancreasData.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 1ffd9d9 Nils Eling 2020-11-16 Small changes to text
html 3c9f49b Nils Eling 2020-09-22 Recompiled from new docker
html 05d2b29 Nils Eling 2020-09-14 Recompiled htmls
Rmd 177107f nilseling 2020-09-14 Updated links to files
html 8e6633e Nils Eling 2020-08-21 Recompiled scripts
html 9ddf2b3 Nils Eling 2020-08-20 Recompiled from with docker container
html d47d609 nilseling 2020-07-22 Recompiled from docker
html a100544 nilseling 2020-07-10 Recompiled htmls
html 2e3df83 nilseling 2020-07-09 Added Github icon and recompiled
html 69f1501 nilseling 2020-07-09 Small fixes and recompilation
Rmd 9b3076b nilseling 2020-07-09 Added more comments
html 55b6a0a nilseling 2020-07-08 Recompiled clean .Rmd files
Rmd a47f6fd nilseling 2020-07-08 Cleanup and further comments
html 5476a61 nilseling 2020-07-08 Recompiled files
Rmd cb92024 nilseling 2020-07-03 Finalized Figure 4
html 2fbf302 nilseling 2020-06-02 Re-compiled htmls
Rmd 7960caa nilseling 2020-06-02 Compiled htmls of preprocessing steps
html 7960caa nilseling 2020-06-02 Compiled htmls of preprocessing steps
Rmd cddc968 nilseling 2020-06-02 Chenged header
Rmd c68803c nilseling 2020-06-02 Restructured to workflowr poject

This script downloads the single-cell data from the pancreas IMC dataset available here. The dataset is associated to the following publication:

Damond et al. A Map of Human Type 1 Diabetes Progression by Imaging Mass Cytometry. Cell Metabolism. 2019 Mar 5;29(3):755-768

After obtaining the raw data as produced by the IMC segmentation pipeline, we will further process them to create a SingleCellExperiment object.

Obtain and read-in data

Here, a subset of single-cell data, corresponding to 100 images from the full dataset is downloaded.

Read in single-cell data

We read in the single-cell meta- and expression data and order them based on the image and cell number.

library(S4Vectors)
library(SingleCellExperiment)
library(cytomapper)

# Download the zipped folder image and unzip it
url.cells <- ("https://data.mendeley.com/public-files/datasets/cydmwsfztj/files/f1e3b8dc-56be-4172-bbc4-3a6f9de97563/file_downloaded")
download.file(url.cells, destfile = "data/PancreasData/CellSubset.zip")
unzip("data/PancreasData/CellSubset.zip", exdir = "data/PancreasData")
file.remove("data/PancreasData/CellSubset.zip")
[1] TRUE
# Read-in the data
cells <- read.csv("data/PancreasData/CellSubset.csv", stringsAsFactors = FALSE)

# Order the dataset by ImageNumber and ObjectNumber
cells <- cells[order(cells$ImageNumber, cells$ObjectNumber), ]

Read in image metadata

Next, we will read in the image-specific metadata.

# Download the zipped folder image and unzip it
url.image <- ("https://data.mendeley.com/public-files/datasets/cydmwsfztj/files/0b236273-d21b-4566-84a2-f1c56324a900/file_downloaded")
download.file(url.image, destfile = "data/PancreasData/Image.zip")
unzip("data/PancreasData/Image.zip", exdir = "data/PancreasData")
file.remove("data/PancreasData/Image.zip")
[1] TRUE
# Read-in the data
image <- read.csv("data/PancreasData/All_Image.csv", stringsAsFactors = FALSE)

Read in cell-type information

In the original publication, cells were phenotyped based on informative marker expression. These phenotype labels are supplied in the online repository.

# Download the zipped folder image and unzip it
url.celltypes <- ("https://data.mendeley.com/public-files/datasets/cydmwsfztj/files/59e8da72-5bfe-4289-b95b-28348a6e1222/file_downloaded")
download.file(url.celltypes, destfile = "data/PancreasData/CellTypes.zip")
unzip("data/PancreasData/CellTypes.zip", exdir = "data/PancreasData")
file.remove("data/PancreasData/CellTypes.zip")
[1] TRUE
# Read-in the data
celltypes <- read.csv("data/PancreasData/CellTypes.csv", stringsAsFactors = FALSE)

Read-in donor information

We will furthermore read in the metadata per donor.

# Download the zipped folder image and unzip it
url.donors <- ("https://data.mendeley.com/public-files/datasets/cydmwsfztj/files/9074990e-1b93-4c79-8c49-1db01a66398b/file_downloaded")
download.file(url.donors, destfile = "data/PancreasData/Donors.zip")
unzip("data/PancreasData/Donors.zip", exdir = "data/PancreasData")
file.remove("data/PancreasData/Donors.zip")
[1] TRUE
# Read-in the data
donors <- read.csv("data/PancreasData/Donors.csv", stringsAsFactors = FALSE)

Prepare metadata

In this part of the workflow, we will select cell-specific metadata and collect them in a single DataFrame. This will be the colData entry of the final SingleCellExperiment object.

Generate cell-specific metadata

The cell-specific metadata can be obtained from the cells object.

cell.metadata <- DataFrame(ImageNumber = cells$ImageNumber,
                           CellNumber = cells$ObjectNumber,
                           Pos_X = cells$Location_Center_X,
                           Pos_Y = cells$Location_Center_Y,
                           ParentIslet = cells$Parent_Islets,
                           ClosestIslet = cells$Parent_ExpandedIslets,
                           Area = cells$AreaShape_Area,
                           NbNeighbours = cells$Neighbors_NumberOfNeighbors_3)

Specific image metadata can be obtained from column-entries of the image object.

image.metadata <- DataFrame(ImageNumber = image$ImageNumber,
                            ImageFullName = image$FileName_CleanStack,
                            slide = image$Metadata_Slide,
                            width = image$Width_CleanStack,
                            height = image$Height_CleanStack)

We will next merge the cell- and image-specific metadata.

cell.metadata <- merge(cell.metadata, image.metadata, by="ImageNumber")

We will also add the ImageName entry to the metadata object. This information is used by cytomapper to match single-cell data with images and masks.

cell.metadata$ImageName <- sub("_a0_full_clean.tiff", "", cell.metadata$ImageFullName)

We will now add the cell-type information to the metadata object.

# Add cell ids to cell metadata (format: "ImageName_CellNumber")
cell.metadata$id <- paste(cell.metadata$ImageName, cell.metadata$CellNumber, sep="_")

# Merge cell metadata and cell type information
cell.metadata <- merge(cell.metadata,
                       celltypes[, c("id", "CellCat", "CellType")],
                       by="id")

We will add the donor information to the metadata object.

cell.metadata <- merge(cell.metadata, donors, by="slide")

Finally, we order the cell-metadata object based on ImageNumber and CellNumber and add rownames.

# Rows are ordered by ImageNumber and CellNumber
cell.metadata <- cell.metadata[order(cell.metadata$ImageNumber, cell.metadata$CellNumber), ]

# Cell ids are used as row names
rownames(cell.metadata) <- cell.metadata$id

Generate marker-specific metadata

Here, we will download the panel information, which contains antibody-related metadata. However, for some datasets, the channel-order and the panel order do not match. For this, 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 = "data/PancreasData/panel.csv")
panel <- read.csv("data/PancreasData/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 = "data/PancreasData/ChannelMass.csv")
channel.mass <- read.csv("data/PancreasData/ChannelMass.csv", header = FALSE)

First, we will select the relevant channels used for analysis and order the pannel based on channel mass.

# Match panel and stack slice information
panel <- panel[panel$full == 1,]
panel <- panel[match(channel.mass[,1], panel$MetalTag),]

# Add short protein names as panel rownames
rownames(panel) <- panel$shortname

Select relevant expression features

CellProfiler measures a number of different statistics per marker and cell. We will select the mean intensity per channel and per cell to obtain single-cell expression counts.

cur_counts <- cells[, grepl("Intensity_MeanIntensity_CleanStack", colnames(cells))]

Next, we will reorder the channels based on channel number.

channelNumber <- as.numeric(sub("^.*_c", "", colnames(cur_counts)))
cur_counts <- cur_counts[, order(channelNumber, decreasing = FALSE)]

Create the SingleCellExperiment object

We have now obtained all metadata and feature data to create the SingleCellExperiment object. We will first create it is based on the raw expression counts.

sce <- SingleCellExperiment(assays = list(counts = t(as.matrix(cur_counts))))

Furthermore, we will store the arcsinh-transformed (using a co-factor of 1) counts in the exprs assay slot.

assay(sce, "exprs") <- asinh(counts(sce)/1)

Now, we will set the dimnames of the object.

rownames(sce) <- rownames(panel)
colnames(sce) <- rownames(cell.metadata)

Finally, we will store the marker- and cell-specific metadata in the SingleCellExperiment object. Here, columns are cells and rows are markers.

colData(sce) <- cell.metadata
rowData(sce) <- panel
sce
class: SingleCellExperiment 
dim: 38 252059 
metadata(0):
assays(2): counts exprs
rownames(38): H3 SMA ... Ir191 Ir193
rowData names(15): TubeNb MetalTag ... miCAT2 miCAT
colnames(252059): E02_1 E02_2 ... J34_1149 J34_1150
colData names(26): slide id ... Ethnicity BMI
reducedDimNames(0):
altExpNames(0):

Save SCE

For further analysis, we will save the SingleCellExperiment object.

saveRDS(sce, "data/PancreasData/pancreas_sce.rds")

Clean up

Finally, we remove the downloaded objects to save storage space.

file.remove("data/PancreasData/All_Image.csv", 
            "data/PancreasData/CellSubset.csv", 
            "data/PancreasData/CellTypes.csv", 
            "data/PancreasData/Donors.csv", 
            "data/PancreasData/panel.csv", 
            "data/PancreasData/ChannelMass.csv")
[1] TRUE TRUE TRUE TRUE TRUE TRUE

sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.1 LTS

Matrix products: default
BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C             
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] parallel  stats4    stats     graphics  grDevices utils     datasets 
[8] methods   base     

other attached packages:
 [1] cytomapper_1.2.0            EBImage_4.32.0             
 [3] SingleCellExperiment_1.12.0 SummarizedExperiment_1.20.0
 [5] Biobase_2.50.0              GenomicRanges_1.42.0       
 [7] GenomeInfoDb_1.26.0         IRanges_2.24.0             
 [9] MatrixGenerics_1.2.0        matrixStats_0.57.0         
[11] S4Vectors_0.28.0            BiocGenerics_0.36.0        
[13] workflowr_1.6.2            

loaded via a namespace (and not attached):
 [1] viridis_0.5.1          viridisLite_0.3.0      svgPanZoom_0.3.4      
 [4] shiny_1.5.0            sp_1.4-4               GenomeInfoDbData_1.2.4
 [7] vipor_0.4.5            tiff_0.1-6             yaml_2.2.1            
[10] gdtools_0.2.2          pillar_1.4.6           lattice_0.20-41       
[13] glue_1.4.2             digest_0.6.27          RColorBrewer_1.1-2    
[16] promises_1.1.1         XVector_0.30.0         colorspace_2.0-0      
[19] htmltools_0.5.0        httpuv_1.5.4           Matrix_1.2-18         
[22] pkgconfig_2.0.3        raster_3.4-5           zlibbioc_1.36.0       
[25] purrr_0.3.4            xtable_1.8-4           fftwtools_0.9-9       
[28] scales_1.1.1           svglite_1.2.3.2        whisker_0.4           
[31] jpeg_0.1-8.1           later_1.1.0.1          git2r_0.27.1          
[34] tibble_3.0.4           generics_0.1.0         ggplot2_3.3.2         
[37] ellipsis_0.3.1         magrittr_2.0.1         crayon_1.3.4          
[40] mime_0.9               evaluate_0.14          fs_1.5.0              
[43] beeswarm_0.2.3         shinydashboard_0.7.1   tools_4.0.3           
[46] lifecycle_0.2.0        stringr_1.4.0          munsell_0.5.0         
[49] locfit_1.5-9.4         DelayedArray_0.16.0    compiler_4.0.3        
[52] systemfonts_0.3.2      rlang_0.4.8            grid_4.0.3            
[55] RCurl_1.98-1.2         rstudioapi_0.13        htmlwidgets_1.5.2     
[58] bitops_1.0-6           rmarkdown_2.5          gtable_0.3.0          
[61] codetools_0.2-18       abind_1.4-5            R6_2.5.0              
[64] gridExtra_2.3          knitr_1.30             dplyr_1.0.2           
[67] fastmap_1.0.1          rprojroot_2.0.2        stringi_1.5.3         
[70] ggbeeswarm_0.6.0       Rcpp_1.0.5             vctrs_0.3.5           
[73] png_0.1-7              tidyselect_1.1.0       xfun_0.19