|
a |
|
b/figures/Figure7.Rmd |
|
|
1 |
--- |
|
|
2 |
title: "Figure 7" |
|
|
3 |
author: Tobias Roider |
|
|
4 |
date: "Last compiled on `r format(Sys.time(), '%d %B, %Y, %X')`" |
|
|
5 |
output: |
|
|
6 |
rmdformats::readthedown: |
|
|
7 |
|
|
|
8 |
editor_options: |
|
|
9 |
chunk_output_type: console |
|
|
10 |
--- |
|
|
11 |
|
|
|
12 |
```{r options, include=FALSE, warning = FALSE} |
|
|
13 |
|
|
|
14 |
library(knitr) |
|
|
15 |
opts_chunk$set(echo=TRUE, tidy=FALSE, include=TRUE, message=FALSE, |
|
|
16 |
dpi = 100, cache = FALSE, warning = FALSE) |
|
|
17 |
opts_knit$set(root.dir = "../") |
|
|
18 |
options(bitmapType = "cairo") |
|
|
19 |
|
|
|
20 |
``` |
|
|
21 |
|
|
|
22 |
# Read data, functions and packages |
|
|
23 |
```{r read data} |
|
|
24 |
|
|
|
25 |
source("R/ReadPackages.R") |
|
|
26 |
source("R/Functions.R") |
|
|
27 |
source("R/ReadData.R") |
|
|
28 |
source("R/ThemesColors.R") |
|
|
29 |
source("R/Helpers.R") |
|
|
30 |
|
|
|
31 |
``` |
|
|
32 |
|
|
|
33 |
# Representative rLN image |
|
|
34 |
```{r create rLN image, fig.height=3} |
|
|
35 |
|
|
|
36 |
plots_codex <- list() |
|
|
37 |
dpi <- 400 |
|
|
38 |
|
|
|
39 |
rLN <- codex_annotation %>% filter(unique_region== "191_4reg001") %>% |
|
|
40 |
filter(x>500, x<7500) %>% |
|
|
41 |
filter(y>500, y<7500) %>% |
|
|
42 |
mutate(Merged_all_simple=ifelse(Merged_final %in% c("Granulo", "Macro", "DC"), "Myeloid", Merged_final)) %>% |
|
|
43 |
mutate(Merged_all_simple=ifelse(Merged_all_simple %in% c("MC", "NKT", "PC", "NK"), "Other", Merged_all_simple)) %>% |
|
|
44 |
filter(((x-mean(.$x))^2+(y-mean(.$y))^2)<2500^2) |
|
|
45 |
|
|
|
46 |
image_rln <- ggplot()+ |
|
|
47 |
geom_point_rast(data=rLN %>% filter(Merged_all_simple=="B"), aes(x=x,y=y), shape=21, size=0.25, stroke=0, alpha=1, raster.dpi =dpi, |
|
|
48 |
color=colors_codex[["B"]], fill=colors_codex[["B"]])+ |
|
|
49 |
geom_point_rast(data=rLN %>% filter(Merged_all_simple!="B"), aes(x=x,y=y, fill=Merged_all_simple, color=Merged_all_simple), |
|
|
50 |
shape=21, size=0.25, stroke=0, alpha=1, raster.dpi=dpi)+ |
|
|
51 |
scale_color_manual(values = colors_codex, limits=limits_codex, labels=labels_codex, name=NULL)+ |
|
|
52 |
scale_fill_manual(values = colors_codex, limits=limits_codex, labels=labels_codex, name=NULL)+ |
|
|
53 |
guides(fill=guide_legend(nrow = 6, override.aes = list(size=1.5, color="white", stroke=0.1)))+ |
|
|
54 |
ggtitle(unique(rLN$Entity))+ |
|
|
55 |
coord_fixed()+ |
|
|
56 |
theme_void()+ |
|
|
57 |
theme(legend.position = "right", |
|
|
58 |
legend.box.background = element_rect(fill = "black"), |
|
|
59 |
legend.box.margin = unit(units = "cm", c(0, 0, 0, -0.25)), |
|
|
60 |
legend.spacing.x = unit("cm", x = 0.1), |
|
|
61 |
legend.key.height = unit("cm", x = 0.34), |
|
|
62 |
legend.key.width = unit("cm", x = 0.2), |
|
|
63 |
legend.text = element_text(color="white", size=7), |
|
|
64 |
plot.title = element_text(color="white", hjust=0.1, size=8, |
|
|
65 |
margin = unit(units = "cm", c(0,0,-0.6,0)), face = "bold"), |
|
|
66 |
plot.margin = unit(units = "cm", c(0.35, 0.1, 0.1, 0.1)), |
|
|
67 |
plot.background = element_rect(fill = "black", color="black"), |
|
|
68 |
panel.background = element_rect(fill = "black", color="black")) |
|
|
69 |
|
|
|
70 |
image_rln |
|
|
71 |
|
|
|
72 |
#ggsave(width = 7, height = 4.75, units = "cm", filename = "Figure7_p1.pdf") |
|
|
73 |
|
|
|
74 |
``` |
|
|
75 |
|
|
|
76 |
## Mini B-cell plot |
|
|
77 |
```{r mini B cell plot, fig.height=1, eval=FALSE, include=FALSE} |
|
|
78 |
|
|
|
79 |
ggplot(rLN %>% filter(Merged_all_simple=="B"), aes(x=x,y=y))+ |
|
|
80 |
geom_point_rast(raster.dpi = dpi, alpha=0.5, shape=".", color="grey75")+ |
|
|
81 |
guides(color=guide_legend(override.aes = list(size=2,alpha=0.75)))+ |
|
|
82 |
scale_y_continuous(expand = c(0,0))+#, limits = c(min(rLN$y), max(rLN$y)+edgeFt))+ |
|
|
83 |
scale_x_continuous(expand = c(0,0))+#, limits = c(min(rLN$x), max(rLN$x)+edgeFt))+ |
|
|
84 |
coord_fixed()+ |
|
|
85 |
mytheme_codex+ |
|
|
86 |
theme(plot.margin = unit(units = "cm", c(0,0,0,0))) |
|
|
87 |
|
|
|
88 |
#ggsave(width = 5, height = 5, units = "cm", filename = "Figure7_mini.pdf") |
|
|
89 |
|
|
|
90 |
``` |
|
|
91 |
|
|
|
92 |
# Neighborhood (NH) plots |
|
|
93 |
## Load NH analysis and PCA |
|
|
94 |
```{r neighbourhood analysis} |
|
|
95 |
|
|
|
96 |
# Read results from neighborhood analysis |
|
|
97 |
# Please run file: analysis/NeighborhoodAnalysis.Rmd |
|
|
98 |
load("output/Neighborhood_results.RData") |
|
|
99 |
|
|
|
100 |
# Add codex annotation |
|
|
101 |
codex_annotation <- left_join(codex_annotation, nn_classes, by="unique_cell_id") |
|
|
102 |
codex_annotation |
|
|
103 |
|
|
|
104 |
``` |
|
|
105 |
|
|
|
106 |
## In situ NH plot of rLN |
|
|
107 |
```{r overview nn, fig.height=3.5} |
|
|
108 |
|
|
|
109 |
plot_overview <- codex_annotation %>% |
|
|
110 |
filter(unique_region=="191_4reg001") %>% |
|
|
111 |
filter(x>500, x<7500) %>% |
|
|
112 |
filter(y>500, y<7500) %>% |
|
|
113 |
filter(((x-mean(.$x))^2+(y-mean(.$y))^2)<2500^2) %>% |
|
|
114 |
ggplot()+ |
|
|
115 |
ggrastr::geom_point_rast(aes(x=x,y=y,color=Region, fill=Region), shape=21, size=0.25, stroke=0, alpha=1, raster.dpi =300)+ |
|
|
116 |
scale_color_manual(values = colors_nn)+ |
|
|
117 |
scale_fill_manual(values = colors_nn)+ |
|
|
118 |
guides(color=guide_legend(override.aes = list(size=3)))+ |
|
|
119 |
coord_fixed()+ |
|
|
120 |
theme_void()+ |
|
|
121 |
theme(legend.position = "none", |
|
|
122 |
legend.title = element_blank(), |
|
|
123 |
legend.text = element_text(size=6), |
|
|
124 |
legend.spacing.x = unit("cm", x = 0.1), |
|
|
125 |
legend.key.height = unit("cm", x = 0.4), |
|
|
126 |
legend.key.width = unit("cm", x = 0.2)) |
|
|
127 |
|
|
|
128 |
plot_overview |
|
|
129 |
|
|
|
130 |
#ggsave(width = 5.3, height = 5.3, units = "cm", filename = "Figure7_p2.pdf") |
|
|
131 |
|
|
|
132 |
``` |
|
|
133 |
|
|
|
134 |
## PCA |
|
|
135 |
```{r pca nn, fig.height=3.5, fig.width=3.5} |
|
|
136 |
|
|
|
137 |
df_loadings <- pca_codex$rotation[c("Stromal cells", "Macro", "B", "TFH", "FDC", "TTOX", "CD4T", "Treg"), c("PC1", "PC2")] %>% |
|
|
138 |
data.frame() %>% mutate(x=0, y=0) %>% |
|
|
139 |
rownames_to_column("Ident") |
|
|
140 |
|
|
|
141 |
scaling <- 7.5 |
|
|
142 |
|
|
|
143 |
plot_pca <- pca_codex$x %>% |
|
|
144 |
data.frame() %>% |
|
|
145 |
rownames_to_column("unique_cell_id") %>% |
|
|
146 |
left_join(., nn_classes) %>% |
|
|
147 |
filter(unique_cell_id %in% rLN$unique_cell_id) %>% |
|
|
148 |
sample_frac(0.3) %>% |
|
|
149 |
ggplot(aes(x=PC1, y=PC2))+ |
|
|
150 |
ggrastr::geom_point_rast(size=0.5, alpha=1, shape=21, stroke=0, aes(color=Region, fill=Region), raster.dpi = 400)+ |
|
|
151 |
geom_segment(data=df_loadings, aes(x=0, xend=7.5*PC1, y=0, yend=7.5*PC2), |
|
|
152 |
arrow = arrow(type = "closed", length = unit(units = "cm", 0.1)), size=0.25)+ |
|
|
153 |
#ggrepel::geom_text_repel(data=df_loadings, aes(x=7.5*PC1, y=7.5*PC2, label=Ident), size=2.5, segment.size=0.25)+ |
|
|
154 |
guides(color=guide_legend(override.aes = list(size=3, alpha=1)))+ |
|
|
155 |
scale_color_manual(values = colors_nn)+ |
|
|
156 |
scale_fill_manual(values = colors_nn)+ |
|
|
157 |
ylim(-7.5,5)+ |
|
|
158 |
mytheme_1+ |
|
|
159 |
#coord_fixed(clip = "off")+ |
|
|
160 |
theme(legend.position = "none") |
|
|
161 |
|
|
|
162 |
plot_pca |
|
|
163 |
|
|
|
164 |
#ggsave(width = 5, height = 5.1, units = "cm", filename = "Figure7_p3.pdf") |
|
|
165 |
|
|
|
166 |
``` |
|
|
167 |
|
|
|
168 |
## NH composition |
|
|
169 |
```{r, include=FALSE} |
|
|
170 |
|
|
|
171 |
df_nh <- |
|
|
172 |
codex_annotation %>% |
|
|
173 |
add_prop(vars = c("Region", "Merged_final"), group.vars = 1) %>% |
|
|
174 |
group_by(Merged_final) %>% |
|
|
175 |
dplyr::mutate(Prop=scale(Prop)[,1]) |
|
|
176 |
|
|
|
177 |
pheat_nh <- df_nh %>% |
|
|
178 |
pivot_wider(names_from = "Merged_final", values_from = "Prop") %>% |
|
|
179 |
column_to_rownames("Region") %>% |
|
|
180 |
pheatmap::pheatmap(silent = T) |
|
|
181 |
|
|
|
182 |
plot_nn_rLN <- ggplot(df_nh, aes(x=Merged_final, y=Region, fill=Prop))+ |
|
|
183 |
geom_tile()+ |
|
|
184 |
scale_fill_gradientn(colours = colorRampPalette(colors = c("#762a83", "#f7f7f7", "#1b7837"))(100), limits=c(-3, 3), |
|
|
185 |
name="Scaled\nAbundance", breaks=c(-3,-1.5,0,1.5,3))+ |
|
|
186 |
scale_x_discrete(limits=pheat_nh$tree_col$labels[pheat_nh$tree_col$order], expand = c(0,0), |
|
|
187 |
labels=c("Plasma cells", "Mast cells", "Stromal cells", "Granulocytes", "NK cells", expression('T'[Pr]), |
|
|
188 |
expression('T'[REG]), expression('Memory T'[TOX]), expression('CD8'^'+'~'naive'), "DC", |
|
|
189 |
expression('CD4'^'+'~'naive'), expression('Memory T'[H]), "NK T-cells", "Macrophages", |
|
|
190 |
expression('Exh. T'[TOX]), "B cells", "FDC", expression('T'[FH])))+ |
|
|
191 |
scale_y_discrete(limits=rev(pheat_nh$tree_row$labels[pheat_nh$tree_row$order]), expand = c(0,0), name="Neighborhoods")+ |
|
|
192 |
geom_vline(xintercept = seq(1.5, 17.5, 1), color="white", size=0.25)+ |
|
|
193 |
geom_hline(yintercept = seq(1.5, 14.5, 1), color="white", size=0.25)+ |
|
|
194 |
mytheme_1+ |
|
|
195 |
coord_cartesian(clip = "off")+ |
|
|
196 |
theme_bw()+ |
|
|
197 |
mytheme_1+ |
|
|
198 |
theme(legend.position = "right", |
|
|
199 |
axis.title.x = element_blank(), |
|
|
200 |
axis.title.y = element_text(vjust = 11), |
|
|
201 |
axis.text.x = element_text(angle=45, hjust=1, size=7), |
|
|
202 |
legend.text = element_text(size = 7, color="black"), |
|
|
203 |
legend.title = element_text(size = 7, color="black", vjust = 0.8), |
|
|
204 |
legend.key.height = unit(0.3, "cm"), |
|
|
205 |
legend.key.width = unit(0.3, "cm"), |
|
|
206 |
legend.box.spacing = unit(0.1, "cm"), |
|
|
207 |
axis.text.y = element_blank(), |
|
|
208 |
axis.ticks.y = element_blank(), |
|
|
209 |
plot.tag = element_text(margin = unit(c(0,0.45,0,0), units = "cm")), |
|
|
210 |
plot.margin = unit(c(0,0.25,0,0.25), "cm"))+ |
|
|
211 |
labs(tag = "E") |
|
|
212 |
|
|
|
213 |
order_y <- rev(pheat_nh$tree_row$labels[pheat_nh$tree_row$order]) |
|
|
214 |
|
|
|
215 |
for(i in 1:length(pheat_nh$tree_row$order)) { |
|
|
216 |
|
|
|
217 |
plot_nn_rLN <- plot_nn_rLN+ |
|
|
218 |
annotation_custom(grob = rectGrob(gp = gpar( fill=colors_nn[order_y][i], lex=1, col="white")), |
|
|
219 |
ymin = seq(0.5, length(colors_nn)-0.5, 1)[i], |
|
|
220 |
ymax = seq(1.5, length(colors_nn)+0.5, 1)[i], |
|
|
221 |
xmin = 0, xmax = -1.1)+ |
|
|
222 |
annotation_custom(grob = textGrob(label = paste0("N", order_y)[i], gp = gpar(cex=0.6)), |
|
|
223 |
ymin = seq(0.5, length(colors_nn)-0.5, 1)[i], |
|
|
224 |
ymax = seq(1.5, length(colors_nn)+0.5, 1)[i], |
|
|
225 |
xmin = 0, xmax = -1.1) |
|
|
226 |
|
|
|
227 |
} |
|
|
228 |
|
|
|
229 |
``` |
|
|
230 |
|
|
|
231 |
## NH proportions |
|
|
232 |
```{r neighborhood composition, fig.height=3} |
|
|
233 |
|
|
|
234 |
roi <- c(4,2,1,7) |
|
|
235 |
|
|
|
236 |
df_freq_nh <- codex_annotation %>% |
|
|
237 |
left_join(., nn_classes) %>% |
|
|
238 |
add_prop(vars = c("Entity", "Region", "unique_region"), group.vars = 3) %>% |
|
|
239 |
fill_zeros(names_from = "Region", values_from = "Prop") |
|
|
240 |
|
|
|
241 |
pvalues <- df_freq_nh %>% |
|
|
242 |
compare_means(data=., formula = Prop ~ Entity, ref.group = "rLN", |
|
|
243 |
group.by = "Region", p.adjust.method = "BH") %>% |
|
|
244 |
filter(p.adj<0.07) %>% |
|
|
245 |
mutate(p.adj_s=format(p.adj, scientific = TRUE, digits=1)) %>% |
|
|
246 |
mutate(p.adj_f=case_when(p.adj > 0.01 ~ as.character(round(p.adj, 2)), |
|
|
247 |
p.adj==0.01 ~ "0.01", |
|
|
248 |
p.adj < 0.01 ~ p.adj_s), |
|
|
249 |
Entity=group2) %>% |
|
|
250 |
filter(!is.na(p.adj)) %>% |
|
|
251 |
mutate(Entity=factor(Entity, levels = c("rLN", "DLBCL", "MCL", "FL", "MZL"))) %>% |
|
|
252 |
filter(Region %in% roi) %>% |
|
|
253 |
mutate(Region=factor(Region, levels = roi)) %>% |
|
|
254 |
arrange(Region,Entity) %>% |
|
|
255 |
mutate(Y=c(0.68,0.1,0.35,0.55,0.77,0.09,0.20,0.09)) %>% |
|
|
256 |
mutate(Region_nn=paste0("N", Region)) %>% |
|
|
257 |
mutate(Region_nn=factor(Region_nn, levels = paste0("N", roi))) |
|
|
258 |
|
|
|
259 |
# Points to modify facet scales |
|
|
260 |
d <- data.frame(Entity="rLN", Region=roi, Y=c(0.7, 0.55, 0.79, 0.35)) %>% |
|
|
261 |
mutate(Region=factor(Region, levels = roi)) %>% |
|
|
262 |
mutate(Region_nn=paste0("N", Region)) %>% |
|
|
263 |
mutate(Region_nn=factor(Region_nn, levels = paste0("N", roi))) |
|
|
264 |
|
|
|
265 |
df_medianLines <- df_freq_nh %>% |
|
|
266 |
filter(Entity=="rLN") %>% |
|
|
267 |
group_by(Region) %>% |
|
|
268 |
dplyr::summarise(MedianProp=median(Prop)) %>% |
|
|
269 |
filter(Region %in% roi) %>% |
|
|
270 |
mutate(Region=factor(Region, levels = roi)) %>% |
|
|
271 |
mutate(Region_nn=paste0("N", Region)) %>% |
|
|
272 |
mutate(Region_nn=factor(Region_nn, levels = paste0("N", roi))) |
|
|
273 |
|
|
|
274 |
plot_freq_nn <- df_freq_nh %>% |
|
|
275 |
filter(Region %in% roi) %>% |
|
|
276 |
mutate(Region_nn=paste0("N", Region)) %>% |
|
|
277 |
mutate(Region_nn=factor(Region_nn, levels = paste0("N", roi))) %>% |
|
|
278 |
mutate(Region=factor(Region, levels = roi)) %>% |
|
|
279 |
ggplot(aes(x=Entity, y=Prop)) + |
|
|
280 |
geom_hline(data=df_medianLines, aes(yintercept=MedianProp), |
|
|
281 |
size=0.25, linetype="dashed", color="grey60")+ |
|
|
282 |
geom_boxplot(width=0.5, outlier.alpha = 0, size=0.25)+ |
|
|
283 |
ggbeeswarm::geom_beeswarm(size=0.8, shape=21, stroke=0.1, cex = 1.75, aes(fill=Region))+ |
|
|
284 |
geom_text(data=pvalues, inherit.aes = F, aes(y=Y, x=Entity, label=p.adj_f), size=2.5)+ |
|
|
285 |
geom_point(data = d, alpha=0, aes(x=Entity, y=Y))+ |
|
|
286 |
scale_fill_manual(values = colors_nn)+ |
|
|
287 |
scale_x_discrete(limits=c("rLN", "DLBCL", "MCL", "FL", "MZL"))+ |
|
|
288 |
facet_wrap(~Region_nn, strip.position = "right", scales = "free_y")+ |
|
|
289 |
ylab("% of total area")+ |
|
|
290 |
mytheme_1+ |
|
|
291 |
theme(strip.text.y = element_text(angle = 0, size=7, margin = unit(units = "cm", c(0.075,0.075,0.075,0.075))), |
|
|
292 |
axis.text.x = element_text(angle=45, hjust=1), |
|
|
293 |
axis.title.x = element_blank(), |
|
|
294 |
plot.margin = unit(c(0,0,0,0.1), "cm"))+ |
|
|
295 |
labs(tag = "F") |
|
|
296 |
|
|
|
297 |
g <- ggplot_gtable(ggplot_build(plot_freq_nn)) |
|
|
298 |
|
|
|
299 |
g$grobs[[22]]$grobs[[1]]$children[[1]]$gp$fill <- colors_nn["2"] |
|
|
300 |
g$grobs[[23]]$grobs[[1]]$children[[1]]$gp$fill <- colors_nn["7"] |
|
|
301 |
g$grobs[[24]]$grobs[[1]]$children[[1]]$gp$fill <- colors_nn["4"] |
|
|
302 |
g$grobs[[25]]$grobs[[1]]$children[[1]]$gp$fill <- colors_nn["1"] |
|
|
303 |
|
|
|
304 |
plot_nn_rLN+wrap_ggplot_grob(g)+plot_layout(widths = c(1.6,1.2)) |
|
|
305 |
|
|
|
306 |
ggsave(width = 18.3, height = 6, units = "cm", filename = "Figure7_p4.pdf") |
|
|
307 |
|
|
|
308 |
``` |
|
|
309 |
|
|
|
310 |
# B-NHL examples |
|
|
311 |
## Cells colored by NH |
|
|
312 |
```{r, fig.height=3} |
|
|
313 |
|
|
|
314 |
regions_nn <- c("191_2reg006", "191_3reg003", "191_4reg002") |
|
|
315 |
names(regions_nn) <- c("MCL", "FL", "DLBCL") |
|
|
316 |
plot_nn <- list() |
|
|
317 |
|
|
|
318 |
for(r in regions_nn){ |
|
|
319 |
df_tmp <- codex_annotation %>% |
|
|
320 |
filter(unique_region==r) %>% |
|
|
321 |
filter(x>500, x<7500) %>% |
|
|
322 |
filter(y>500, y<7500) %>% |
|
|
323 |
filter(((x-mean(.$x))^2+(y-mean(.$y))^2)<2500^2) |
|
|
324 |
|
|
|
325 |
plot_nn[[r]] <- df_tmp %>% |
|
|
326 |
ggplot()+ |
|
|
327 |
geom_point(data = data.frame(x=min(df_tmp$x)+2500, y=min(df_tmp$y)+2500), stroke=2, |
|
|
328 |
aes(x=x,y=y), shape=21, color="white", fill="white", size=72.5)+ |
|
|
329 |
ggrastr::geom_point_rast(aes(x=x,y=y,color=Region, fill=Region), shape=21, size=0.25, stroke=0, alpha=1, raster.dpi = 400)+ |
|
|
330 |
scale_color_manual(values = colors_nn)+ |
|
|
331 |
scale_fill_manual(values = colors_nn)+ |
|
|
332 |
guides(color=guide_legend(override.aes = list(size=3)))+ |
|
|
333 |
coord_fixed(clip = "off")+ |
|
|
334 |
theme_void()+ |
|
|
335 |
theme(legend.position = "none", |
|
|
336 |
legend.title = element_blank(), |
|
|
337 |
plot.background = element_rect(fill=NA, color=NA), |
|
|
338 |
legend.text = element_text(size=6), |
|
|
339 |
legend.spacing.x = unit("cm", x = 0.1), |
|
|
340 |
legend.key.height = unit("cm", x = 0.4), |
|
|
341 |
legend.key.width = unit("cm", x = 0.2)) |
|
|
342 |
|
|
|
343 |
#ggsave(plot_nn[[r]], width = 6, height = 6, units = "cm", filename = paste("Figure7_", r, ".pdf")) |
|
|
344 |
} |
|
|
345 |
|
|
|
346 |
plot_nn |
|
|
347 |
|
|
|
348 |
``` |
|
|
349 |
|
|
|
350 |
## Cells colored by subset |
|
|
351 |
```{r, fig.height=3} |
|
|
352 |
|
|
|
353 |
regions_nn <- c("191_2reg006", "191_3reg003", "191_4reg002") |
|
|
354 |
names(regions_nn) <- c("MCL", "FL", "DLBCL") |
|
|
355 |
df_images <- list() |
|
|
356 |
images <- list() |
|
|
357 |
|
|
|
358 |
margins <- c(0.1, 0, 0.4, -0.5) |
|
|
359 |
dpi <- 600 |
|
|
360 |
|
|
|
361 |
for(r in 1:3){ |
|
|
362 |
|
|
|
363 |
df_images[[r]] <- codex_annotation %>% |
|
|
364 |
filter(unique_region==regions_nn[r]) %>% |
|
|
365 |
filter(x>500, x<7500) %>% |
|
|
366 |
filter(y>500, y<7500) %>% |
|
|
367 |
mutate(Merged_all_simple=ifelse(Merged_final %in% c("Granulo", "Macro", "DC"), "Myeloid", Merged_final)) %>% |
|
|
368 |
mutate(Merged_all_simple=ifelse(Merged_all_simple %in% c("PC", "MC", "NK", "NKT"), "Other", Merged_all_simple)) %>% |
|
|
369 |
filter(((x-mean(.$x))^2+(y-mean(.$y))^2)<2500^2) |
|
|
370 |
|
|
|
371 |
images[[r]] <- |
|
|
372 |
ggplot()+ |
|
|
373 |
geom_point_rast(data=df_images[[r]] %>% filter(Merged_all_simple=="B"), aes(x=x,y=y), |
|
|
374 |
shape=21, size=0.25, stroke=0, alpha=1, raster.dpi =dpi, |
|
|
375 |
color=colors_codex[["B"]], fill=colors_codex[["B"]])+ |
|
|
376 |
geom_point_rast(data=df_images[[r]] %>% filter(Merged_all_simple!="B"), |
|
|
377 |
aes(x=x,y=y, fill=Merged_all_simple, color=Merged_all_simple), |
|
|
378 |
shape=21, size=0.25, stroke=0, alpha=1, raster.dpi=dpi)+ |
|
|
379 |
scale_color_manual(values = colors_codex, limits=limits_codex, labels=labels_codex, name="Cell type")+ |
|
|
380 |
scale_fill_manual(values = colors_codex, limits=limits_codex, labels=labels_codex, name="Cell type")+ |
|
|
381 |
ggtitle(unique(df_images[[r]]$Entity))+ |
|
|
382 |
coord_fixed()+ |
|
|
383 |
theme_void()+ |
|
|
384 |
theme(legend.position = "none", |
|
|
385 |
plot.title = element_text(color="white", hjust=0.1, size=10, |
|
|
386 |
margin = unit(units = "cm", c(0,0,-1,0)), face = "bold"), |
|
|
387 |
plot.margin = unit(units = "cm", margins), |
|
|
388 |
panel.background = element_rect(fill = "black", color="black"), |
|
|
389 |
plot.background = element_rect(fill = "black", color="black")) |
|
|
390 |
|
|
|
391 |
} |
|
|
392 |
|
|
|
393 |
#images |
|
|
394 |
|
|
|
395 |
emptyplot <- ggplot()+ |
|
|
396 |
geom_point_rast(data=df_images[[1]] %>% filter(Merged_final=="BC"), aes(x=x,y=y), raster.dpi = dpi, shape=".", |
|
|
397 |
color=colors_codex[["B"]])+ |
|
|
398 |
coord_fixed()+ |
|
|
399 |
mytheme_codex+ |
|
|
400 |
theme(panel.background = element_rect(fill = "black", color="black"), |
|
|
401 |
plot.background = element_rect(fill = "black", color="black")) |
|
|
402 |
|
|
|
403 |
p_full <- images[[3]]+emptyplot+images[[1]]+emptyplot+images[[2]]+emptyplot+plot_layout(widths = c(1,0.1,1,0.1,1,0.3)) |
|
|
404 |
p_full |
|
|
405 |
|
|
|
406 |
#ggsave(p_full, width = 22.5, height = 6, units = "cm", filename = "Figure7_mini.pdf") |
|
|
407 |
|
|
|
408 |
``` |
|
|
409 |
|
|
|
410 |
## Legend |
|
|
411 |
```{r, fig.height=1} |
|
|
412 |
|
|
|
413 |
plot.legend <- images[[r]]+ |
|
|
414 |
guides(fill=guide_legend(nrow = 1, override.aes = list(size=1.75, color="white", stroke=0.25)))+ |
|
|
415 |
guides(color=guide_legend(nrow = 1, override.aes = list(size=1.75, color="white", stroke=0.25)))+ |
|
|
416 |
theme(legend.position = "bottom", |
|
|
417 |
legend.title = element_blank(), |
|
|
418 |
legend.box.background = element_rect(fill = "black"), |
|
|
419 |
legend.box.margin = unit(units = "cm", c(0, 0, 0, 0)), |
|
|
420 |
legend.spacing.x = unit("cm", x = 0.1), |
|
|
421 |
legend.key.height = unit("cm", x = 0.34), |
|
|
422 |
legend.key.width = unit("cm", x = 0.16), |
|
|
423 |
plot.margin = unit(units = "cm", c(0,0,0,0)), |
|
|
424 |
plot.background = element_rect(fill = "black", color="black"), |
|
|
425 |
panel.background = element_rect(fill = "black", color="black"), |
|
|
426 |
legend.text = element_text(color="white", size=6.5)) |
|
|
427 |
|
|
|
428 |
as_ggplot(get_legend(plot.legend)) |
|
|
429 |
#ggsave(width = 19, height = 1, units = "cm", filename = "Figure7_legend.pdf") |
|
|
430 |
|
|
|
431 |
``` |
|
|
432 |
|
|
|
433 |
# Closest cells to B-cells |
|
|
434 |
```{r fig.height=2.2} |
|
|
435 |
|
|
|
436 |
codex_freq <- codex_annotation %>% |
|
|
437 |
add_prop(vars = c("unique_region", "Merged_final"), group.vars = 1) |
|
|
438 |
|
|
|
439 |
nn <- run_NNanalysis(data = codex_annotation, regions = unique(codex_annotation$unique_region), |
|
|
440 |
plan_session = "multisession", |
|
|
441 |
add.prop=FALSE, |
|
|
442 |
n_workers = 10, |
|
|
443 |
nn = 1) |
|
|
444 |
|
|
|
445 |
nn_sum <- |
|
|
446 |
nn %>% select(-name) %>% |
|
|
447 |
left_join(codex_annotation %>% select(unique_cell_id, unique_region)) %>% |
|
|
448 |
left_join(codex_annotation %>% select(unique_cell_id, Ident_center=Merged_final)) %>% |
|
|
449 |
filter(Ident_center=="B", ) %>% |
|
|
450 |
add_prop(vars = c("unique_region", "Merged_final"), group.vars = 1) %>% |
|
|
451 |
mutate(Prop=100*Prop) %>% |
|
|
452 |
left_join(., codex_annotation %>% select(unique_region, Entity) %>% distinct) |
|
|
453 |
|
|
|
454 |
|
|
|
455 |
art_max <- c(20, 14, 11, 12) |
|
|
456 |
names(art_max) <- c("rLN", "DLBCL", "MCL", "FL") |
|
|
457 |
plot_nn <- list() |
|
|
458 |
|
|
|
459 |
for(e in names(art_max)){ |
|
|
460 |
|
|
|
461 |
selected <- |
|
|
462 |
nn_sum %>% filter(Entity==e) %>% |
|
|
463 |
group_by(Merged_final) %>% |
|
|
464 |
summarise(Mean=mean(Prop), SEM=sd(Prop)/sqrt(length(Prop))) %>% |
|
|
465 |
top_n(Mean, n = 10) %>% |
|
|
466 |
mutate(code=Merged_final=="FDC") %>% |
|
|
467 |
mutate(SEM=ifelse(Mean>art_max[e], NA, SEM)) %>% |
|
|
468 |
mutate(Mean_new=ifelse(Mean>art_max[e], art_max[e], Mean)) |
|
|
469 |
|
|
|
470 |
order_ <- selected %>% |
|
|
471 |
arrange(desc(Mean_new)) %>% |
|
|
472 |
pull(Merged_final) |
|
|
473 |
|
|
|
474 |
mean_pat <- nn_sum %>% filter(Entity==e) %>% |
|
|
475 |
left_join(codex_annotation %>% select(PatientID, unique_region) %>% distinct) %>% |
|
|
476 |
group_by(PatientID, Merged_final) %>% |
|
|
477 |
summarise(Mean_pat=mean(Prop)) |
|
|
478 |
|
|
|
479 |
plot_nn[[e]] <- |
|
|
480 |
selected %>% |
|
|
481 |
ggplot(aes(x=reorder(Merged_final, -Mean_new), y=Mean_new, fill=Merged_final, color=code))+ |
|
|
482 |
geom_errorbar(aes(ymin=Mean_new, ymax=Mean_new+SEM), width=0.2, color="black", size=0.25)+ |
|
|
483 |
geom_bar(stat = "identity", size=0.5, width=0.5, fill="white", color="white")+ |
|
|
484 |
geom_bar(stat = "identity", size=0.25, width=0.5, alpha=0.6)+ |
|
|
485 |
ggbeeswarm::geom_beeswarm(data=mean_pat, inherit.aes = F, aes(x=Merged_final, y=Mean_pat), |
|
|
486 |
color="black", stroke=0, size=0.65, alpha=0.5, cex=1.75)+ |
|
|
487 |
annotation_custom(grob = textGrob(label = e, just = "right", x = 0.92, y=0.9, gp = gpar(cex=0.6)))+ |
|
|
488 |
geom_segment(inherit.aes = F, |
|
|
489 |
aes(x=1, xend=1, y=1.02*Mean_new[1], yend=1.14*Mean_new[1]), |
|
|
490 |
color="black", size=0.15, |
|
|
491 |
arrow = arrow(type = "closed", length = unit(units = "cm", 0.1)))+ |
|
|
492 |
geom_text(aes(x=2.2, y=1.08*Mean_new[1], label=round(Mean[1],1)), |
|
|
493 |
check_overlap = T, size=2.5, color="black")+ |
|
|
494 |
scale_color_manual(values = c("white", "black"), limits=c(F, T))+ |
|
|
495 |
scale_fill_manual(values = colors_codex_exp)+ |
|
|
496 |
scale_x_discrete(limits=order_, labels=unlist(labels_codex_exp))+ |
|
|
497 |
scale_y_continuous(name = "% of cells closest\nto B-cells", limits=c(0,1.15*art_max[e]))+ |
|
|
498 |
mytheme_1+ |
|
|
499 |
theme(legend.position = "none", |
|
|
500 |
axis.text.x = element_text(angle=45, hjust=1, size=7), |
|
|
501 |
plot.background = element_rect(fill = NA, colour = NA), |
|
|
502 |
panel.background = element_rect(fill = NA, colour = NA), |
|
|
503 |
plot.margin = unit(c(0,0.1,0,0), "cm"), |
|
|
504 |
axis.title.x = element_blank()) |
|
|
505 |
|
|
|
506 |
if(e!="rLN") { |
|
|
507 |
plot_nn[[e]] <- plot_nn[[e]]+ |
|
|
508 |
theme(axis.title.y = element_blank())} |
|
|
509 |
} |
|
|
510 |
|
|
|
511 |
plot_nn$rLN+labs(tag = "G")+plot_nn$DLBCL+ |
|
|
512 |
plot_nn$MCL+plot_nn$FL+ |
|
|
513 |
plot_layout(nrow = 1) |
|
|
514 |
|
|
|
515 |
#ggsave(width = 18, height = 4.75, units = "cm", filename = "Figure7_p5.pdf") |
|
|
516 |
|
|
|
517 |
``` |
|
|
518 |
|
|
|
519 |
# Session info |
|
|
520 |
```{r} |
|
|
521 |
|
|
|
522 |
sessionInfo() |
|
|
523 |
|
|
|
524 |
``` |