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 0d48ecb. 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/
    Ignored:    data/PancreasData/pancreas_images.rds
    Ignored:    data/PancreasData/pancreas_masks.rds
    Ignored:    data/PancreasData/pancreas_sce.rds

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/04-Figure_1.Rmd) and HTML (docs/04-Figure_1.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 0d48ecb Nils Eling 2020-11-18 Corrected chunk name
Rmd 96b285d Nils Eling 2020-11-18 Set correct figure paths
Rmd 194e172 Nils Eling 2020-11-18 Merged Figure 1 and 2

This script reproduces all images contained in Figure 1. Here, we will load the libraries required for data analysis:

library(cytomapper)

Figure 1 A-B

In the first part, we will solely display the example data shipped with cytomapper. They can be read in using the data function.

data("pancreasSCE")
data("pancreasImages")
data("pancreasMasks")

We will save the segmentation masks and single-channel images for image 1 and 3 for display purposes.

plotCells(pancreasMasks[1], legend = NULL, image_title = NULL, 
          scale_bar = NULL, interpolate = FALSE, 
          save_plot = list(filename = "docs/final_figures/main/Fig_1A_mask1.png"))
plotCells(pancreasMasks[3], legend = NULL, image_title = NULL, 
          scale_bar = NULL, interpolate = FALSE, 
          save_plot = list(filename = "docs/final_figures/main/Fig_1A_mask2.png"))

for(i in 1:5){
  cur_col <- list(c("black", "white"))
  names(cur_col) <- channelNames(pancreasImages)[i]
  
  cur_bcg <- list(c(0,3,1))
  names(cur_bcg) <- channelNames(pancreasImages)[i]
  
  plotPixels(pancreasImages[1], legend = NULL, image_title = NULL, 
          scale_bar = NULL, interpolate = FALSE, colour_by = channelNames(pancreasImages)[i],
          colour = cur_col, bcg = cur_bcg,
          save_plot = list(filename = paste0("docs/final_figures/main/Fig_1B_img1_ch", 
                                             i, ".png")))
  
  plotPixels(pancreasImages[3], legend = NULL, image_title = NULL, 
          scale_bar = NULL, interpolate = FALSE, colour_by = channelNames(pancreasImages)[i],
          colour = cur_col, bcg = cur_bcg,
          save_plot = list(filename = paste0("docs/final_figures/main/Fig_1B_img2_ch", 
                                             i, ".png")))
}

We will next visualize the example cell-types on the segmentation masks and the composite images of 5 colours.

plotCells(mask = pancreasMasks[1], object = pancreasSCE, img_id = "ImageNb", 
          cell_id = "CellNb", colour_by = "CellType",  
          legend = NULL,  image_title = NULL, scale_bar = list(label = "", lwidth = 5),
          save_plot = list(filename = "docs/final_figures/main/Fig_1A_celltype1.png",
                           scale = 10))
plotCells(mask = pancreasMasks[3], object = pancreasSCE, img_id = "ImageNb", 
          cell_id = "CellNb", colour_by = "CellType", 
          legend = NULL,  image_title = NULL, scale_bar = list(label = "", lwidth = 5),
          save_plot = list(filename = "docs/final_figures/main/Fig_1A_celltype2.png",
                           scale = 10))

plotPixels(image = pancreasImages[1], object = pancreasSCE,
           img_id = "ImageNb", cell_id = "CellNb", 
           legend = NULL,  image_title = NULL, scale_bar = list(label = "", lwidth = 5),
           colour_by = c("CD99", "PIN", "H3", "CD8a", "CDH"),
           bcg = list(H3 = c(0, 4, 1),
                      CD99 = c(0, 3, 1),
                      PIN = c(0, 5, 1),
                      CD8a = c(0, 5, 1),
                      CDH = c(0, 2, 1)),
           save_plot = list(filename = "docs/final_figures/main/Fig_1B_composite1.png",
                           scale = 10))
plotPixels(image = pancreasImages[3], object = pancreasSCE,
           legend = NULL,  image_title = NULL, scale_bar = list(label = "", lwidth = 5),
           img_id = "ImageNb", cell_id = "CellNb", 
           colour_by = c("CD99", "PIN", "H3", "CD8a", "CDH"),
           bcg = list(H3 = c(0, 1.5, 1),
                      CD99 = c(0, 3, 1),
                      PIN = c(0, 1, 1),
                      CD8a = c(0, 4, 1),
                      CDH = c(0, 2, 1)),
           save_plot = list(filename = "docs/final_figures/main/Fig_1B_composite2.png",
                           scale = 10))

Figure 1 C-D

This script reproduces the analysis performed in Figure 1 C-D. Here, we will load the libraries required for data analysis:

library(dplyr)

The pre-processing scripts have generated the following files in the data/PancreasData folder:

  1. A SingleCellExperiment object containing the cell- and marker-specific expression and metadata.
  2. A CytoImageList object containing 100 example images of 3 donors, where each image contains 38 channels
  3. A CytoImageList object containing 100 example masks, which are associated to and a segmentation result of the images in 2.

Here, we will load all relevant libraries and read in the data.

sce <- readRDS("data/PancreasData/pancreas_sce.rds")
images <- readRDS("data/PancreasData/pancreas_images.rds")
masks <- readRDS("data/PancreasData/pancreas_masks.rds")

In type 1 diabetes (T1D), pancreatic insulin-producing beta cells are killed by self-reacting immune cells. Here, we will visualize association between T cells and beta cells in three pancreas donors at different stage of the disease (Non-diabetic, recent onset T1D and long duration T1D).

In our dataset, cell types can be visualized in the CellType column of colData(sce) (unique(colData(sce)$CellType)). T cells have the cell type Tc (CD8+) or Th (CD4+) and beta cells are labelled as beta.

First, we will select for each donor the image with the highest density of T cells (number of T cells per mm^2). Then, we will subset the SingleCellExperiment object (sce) and the CytoImageList objects (images and masks).

# Select the three images with the higest T cell density
selected.images <- as_tibble(colData(sce)) %>% 
  # Calculate for each image the area, number of T cells and T cell density
  group_by(ImageNumber) %>%
  mutate(ImageArea = (width * height) / 10^6,
         TcellCount = sum(CellType == "Tc" | CellType == "Th"),
         TcellDensity = TcellCount / ImageArea) %>%
  ungroup() %>%
  # Select for each disease stage the image with the highest T cell density
  group_by(stage) %>%
  dplyr::slice(which.max(TcellDensity)) %>%
  pull(ImageName)

print(selected.images)
[1] "J01" "E30" "G23"
# Subset the objects
cur_sce <- sce[, sce$ImageName %in% selected.images]
cur_images <- images[mcols(images)$ImageName %in% selected.images]
cur_masks <- masks[mcols(masks)$ImageName %in% selected.images]

To visualize association between islet and T cells, we will use the plotCells function and display beta and T cells on the three images selected above.

The different cell types will be coloured as following: Tc cells in red, Th cells in blue, beta cells in yellow, the other islet cells (alpha, delta) in cyan and the rest of the cells in white

To display only a selected number of cells, the SingleCellExperiment can be subsetted.

cur_sce <- cur_sce[,cur_sce$CellType %in% c("beta", "alpha", "delta", "Tc", "Th")]

# Define the colors for the different cell types
ct_colours <- vector(mode = "character", length = length(unique(cur_sce$CellType)))
names(ct_colours) <- unique(cur_sce$CellType)
ct_colours["beta"] <- "yellow"
ct_colours[c("alpha", "delta")] <- "cyan"
ct_colours["Tc"] <- "red"
ct_colours["Th"] <- "blue"

# Plot the cells
plotCells(mask = cur_masks,
          object = cur_sce,
          cell_id = "CellNumber",
          img_id = "ImageName",
          colour_by = "CellType",
          colour = list(CellType = ct_colours),
          image_title = list(text = c("Non-diabetic",
                                      "Recent onset T1D",
                                      "Long duration T1D"),
                             colour = "black"),
          scale_bar = list(length = 100,
                           label = expression("100 " ~ mu * "m"),
                           colour = "black"),
          missing_colour = "white",
          background_colour = "gray")

# Save figure
plotCells(mask = cur_masks,
          object = cur_sce,
          cell_id = "CellNumber",
          img_id = "ImageName",
          colour_by = "CellType",
          colour = list(CellType = ct_colours),
          image_title = list(text = c("Non-diabetic",
                                      "Recent onset T1D",
                                      "Long duration T1D"),
                             colour = "black"),
          scale_bar = list(length = 100,
                           label = expression("100 " ~ mu * "m"),
                           colour = "black"),
          missing_colour = "white",
          background_colour = "gray", 
          save_plot = list(filename = "docs/final_figures/main/Fig_1C.png", scale = 3))

We now see that T cells are located closer to the islet on the “Recent onset T1D” image. In addition, no beta cells are visible on the “Long duration T1D” image, as expected because they have been destroyed by the immune system.

We will now confirm that this is reflected on the original images by plotting the pixel-level data with the plotPixels function. CD4 (CD4 expressed by Th cells) will be plotted in blue, CD8 (CD8a, Tc cells) in red and proinsulin (PIN, beta cells) in yellow.

plotPixels(image = cur_images,
           cell_id = "CellNumber",
           img_id = "ImageName",
           colour_by = c("PIN", "CD4", "CD8a"), 
           colour = list(PIN = c("black", "yellow"),
                         # SYP = c("black", "cyan"),
                         CD4 = c("black", "blue"),
                         CD8a = c("black", "red")),
           bcg = list(PIN = c(0, 10, 1),
                      # SYP = c(0, 3, 1),
                      CD4 = c(0, 8, 1),
                      CD8a = c(0, 10, 1)),
           image_title = list(text = c("Non-diabetic",
                                       "Recent onset T1D",
                                       "Long duration T1D")),
           scale_bar = list(length = 100,
                            label = expression("100 " ~ mu * "m")))

# Save figure
plotPixels(image = cur_images,
           cell_id = "CellNumber",
           img_id = "ImageName",
           colour_by = c("PIN", "CD4", "CD8a"), 
           colour = list(PIN = c("black", "yellow"),
                         # SYP = c("black", "cyan"),
                         CD4 = c("black", "blue"),
                         CD8a = c("black", "red")),
           bcg = list(PIN = c(0, 10, 1),
                      # SYP = c(0, 3, 1),
                      CD4 = c(0, 8, 1),
                      CD8a = c(0, 10, 1)),
           image_title = list(text = c("Non-diabetic",
                                       "Recent onset T1D",
                                       "Long duration T1D")),
           scale_bar = list(length = 100,
                            label = expression("100 " ~ mu * "m")),
           save_plot = list(filename = "docs/final_figures/main/Fig_1D.png", scale = 3))

This plot confirms that beta cells are surrounded by infiltrating T cells on the “Recent onset T1D” image. In addition, proinsulin abundance seems to be reduced on that image as compared to the “Non-diabetic” control image. This observation can be further explored by performing additional data analysis (see next figures).


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] dplyr_1.0.2                 cytomapper_1.2.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] S4Vectors_0.28.0            BiocGenerics_0.36.0        
[11] MatrixGenerics_1.2.0        matrixStats_0.57.0         
[13] EBImage_4.32.0              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             fastmap_1.0.1         
[67] rprojroot_2.0.2        stringi_1.5.3          ggbeeswarm_0.6.0      
[70] Rcpp_1.0.5             vctrs_0.3.5            png_0.1-7             
[73] tidyselect_1.1.0       xfun_0.19