-
Notifications
You must be signed in to change notification settings - Fork 2
/
03-Figure_5.Rmd
222 lines (197 loc) · 8.19 KB
/
03-Figure_5.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
---
title: "Figure 5"
author: "Nils Eling and Nicolas Damond"
date: "`r Sys.Date()`"
output: workflowr::wflow_html
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
This script reproduces the analysis performed in Figure 5.
Here, we will load the libraries and data for this figure:
```{r load-libraries-and-data, message=FALSE}
library(cytomapper)
library(dplyr)
sce <- readRDS("data/PancreasData/pancreas_sce.rds")
masks <- readRDS("data/PancreasData/pancreas_masks.rds")
images <- readRDS("data/PancreasData/pancreas_images.rds")
```
Here, we will highlight a few images that contain different cell-types and outline these using the segmentation masks.
This analysis will visually confirm cell-type phenotyping and segmentation results.
We will first select images with a high count of CD4 and CD8 T cells.
```{r select-Tcells}
# 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(ImageName) %>%
summarise(width = mean(width),
height = mean(height),
ImageArea = (width * height) / 10^6,
TcellCount = sum(CellType == "Tc" | CellType == "Th"),
TcellDensity = TcellCount / ImageArea) %>%
arrange(desc(TcellDensity))
```
Now, we will visualize the top three images and outline CD4 and CD8 T cells.
```{r highlight-Tcell-outline}
top_images <- selected_images$ImageName[1:3]
cur_images <- images[match(top_images, mcols(images)$ImageName)]
cur_masks <- masks[match(top_images, mcols(images)$ImageName)]
cur_sce <- sce[,sce$CellType == "Th"]
plotPixels(image = cur_images,
object = cur_sce,
mask = cur_masks,
img_id = "ImageName",
cell_id = "CellNumber",
colour_by = c("H3", "CD4"),
outline_by = "CellType",
colour = list(H3 = c("black", "blue"),
CD4 = c("black", "red"),
CellType = c(Th = "white")),
bcg = list(H3 = c(0, 3, 1),
CD4 = c(0, 4, 1)),
scale_bar = list(length = 100,
label = expression("100 " ~ mu * "m")))
# Save image
plotPixels(image = cur_images,
object = cur_sce,
mask = cur_masks,
img_id = "ImageName",
cell_id = "CellNumber",
colour_by = c("H3", "CD4"),
outline_by = "CellType",
colour = list(H3 = c("black", "blue"),
CD4 = c("black", "red"),
CellType = c(Th = "white")),
bcg = list(H3 = c(0, 3, 1),
CD4 = c(0, 4, 1)),
scale_bar = list(length = 100,
label = expression("100 " ~ mu * "m"),
margin = c(40, 40)),
save_plot = list(filename = "docs/final_figures/main/Fig_5A.png", scale = 3))
cur_sce <- sce[,sce$CellType == "Tc"]
plotPixels(image = cur_images,
object = cur_sce,
mask = cur_masks,
img_id = "ImageName",
cell_id = "CellNumber",
colour_by = c("H3", "CD8a"),
outline_by = "CellType",
colour = list(H3 = c("black", "blue"),
CD8a = c("black", "red"),
CellType = c(Tc = "white")),
bcg = list(H3 = c(0, 3, 1),
CD8a = c(0, 4, 1)),
scale_bar = list(length = 100,
label = expression("100 " ~ mu * "m"),
margin = c(40, 40)))
# Save image
plotPixels(image = cur_images,
object = cur_sce,
mask = cur_masks,
img_id = "ImageName",
cell_id = "CellNumber",
colour_by = c("H3", "CD8a"),
outline_by = "CellType",
colour = list(H3 = c("black", "blue"),
CD8a = c("black", "red"),
CellType = c(Tc = "white")),
bcg = list(H3 = c(0, 3, 1),
CD8a = c(0, 4, 1)),
scale_bar = list(length = 100,
label = expression("100 " ~ mu * "m"),
margin = c(40, 40)),
save_plot = list(filename = "docs/final_figures/main/Fig_5B.png", scale = 3))
```
As a second example, we will select images with high alpha and beta cell count and perform a similar analysis as above.
Due to the loss of beta cells, we will only select images of healthy patients.
```{r select-islet-cells}
# Select the three images with the higest alpha and beta cell density
selected_images <- as_tibble(colData(sce)) %>%
filter(stage == "Non-diabetic") %>%
group_by(ImageName) %>%
summarise(width = mean(width),
height = mean(height),
ImageArea = (width * height) / 10^6,
alphaCellCount = sum(CellType == "alpha"),
alphaCellDensity = alphaCellCount / ImageArea,
betaCellCount = sum(CellType == "beta"),
betaCellDensity = betaCellCount / ImageArea) %>%
mutate(alphaCellRank = rank(alphaCellDensity),
betaCellRank = rank(betaCellDensity),
rankSum = alphaCellRank + betaCellRank) %>%
arrange(desc(rankSum))
```
We will now outline alpha and beta cells.
```{r outline-alpha-cells}
top_images <- selected_images$ImageName[1:3]
cur_images <- images[match(top_images, mcols(images)$ImageName)]
cur_masks <- masks[match(top_images, mcols(images)$ImageName)]
cur_sce <- sce[,sce$CellType == "alpha"]
plotPixels(image = cur_images,
object = cur_sce,
mask = cur_masks,
img_id = "ImageName",
cell_id = "CellNumber",
colour_by = c("H3", "GCG"),
outline_by = "CellType",
colour = list(H3 = c("black", "blue"),
GCG = c("black", "red"),
CellType = c(alpha = "white")),
bcg = list(H3 = c(0, 3, 1),
GCG = c(0, 4, 1)),
scale_bar = list(length = 100,
label = expression("100 " ~ mu * "m"),
margin = c(40, 40)))
# Save image
plotPixels(image = cur_images,
object = cur_sce,
mask = cur_masks,
img_id = "ImageName",
cell_id = "CellNumber",
colour_by = c("H3", "GCG"),
outline_by = "CellType",
colour = list(H3 = c("black", "blue"),
GCG = c("black", "red"),
CellType = c(alpha = "white")),
bcg = list(H3 = c(0, 3, 1),
GCG = c(0, 4, 1)),
scale_bar = list(length = 100,
label = expression("100 " ~ mu * "m"),
margin = c(40, 40)),
save_plot = list(filename = "docs/final_figures/main/Fig_5C.png", scale = 3))
cur_sce <- sce[,sce$CellType == "beta"]
plotPixels(image = cur_images,
object = cur_sce,
mask = cur_masks,
img_id = "ImageName",
cell_id = "CellNumber",
colour_by = c("H3", "PIN"),
outline_by = "CellType",
colour = list(H3 = c("black", "blue"),
PIN = c("black", "red"),
CellType = c(beta = "white")),
bcg = list(H3 = c(0, 3, 1),
PIN = c(0, 4, 1)),
scale_bar = list(length = 100,
label = expression("100 " ~ mu * "m"),
margin = c(40, 40)))
# Save image
plotPixels(image = cur_images,
object = cur_sce,
mask = cur_masks,
img_id = "ImageName",
cell_id = "CellNumber",
colour_by = c("H3", "PIN"),
outline_by = "CellType",
colour = list(H3 = c("black", "blue"),
PIN = c("black", "red"),
CellType = c(beta = "white")),
bcg = list(H3 = c(0, 3, 1),
PIN = c(0, 4, 1)),
scale_bar = list(length = 100,
label = expression("100 " ~ mu * "m"),
margin = c(40, 40)),
save_plot = list(filename = "docs/final_figures/main/Fig_5D.png", scale = 3))
```