Diff of /figures/Figure4.Rmd [000000] .. [9905a0]

Switch to unified view

a b/figures/Figure4.Rmd
1
---
2
title: "Figure 4"
3
author: Tobias Roider
4
date: "Last compiled on `r format(Sys.time(), '%d %B, %Y, %X')`"
5
output: 
6
  rmdformats::readthedown: 
7
  self_contained: false
8
  
9
editor_options: 
10
  chunk_output_type: console
11
---
12
13
```{r options, include=FALSE, warning = FALSE}
14
15
library(knitr)
16
opts_chunk$set(echo=TRUE, tidy=FALSE, include=TRUE, message=FALSE,
17
               dpi = 100, cache = FALSE, warning = FALSE)
18
opts_knit$set(root.dir = "../")
19
options(bitmapType = "cairo")
20
21
```
22
23
# Read data, functions and packages
24
```{r read data}
25
26
source("R/ReadPackages.R")
27
source("R/Functions.R")
28
source("R/ReadData.R")
29
source("R/ThemesColors.R")
30
source("R/Helpers.R")
31
32
```
33
34
# Clonotypes projected onto UMAP
35
```{r clonotype sizes, fig.height=2.5}
36
37
set.seed(1)
38
df_clonotypes <- 
39
  DFtotal_5prime %>% 
40
  filter(!is.na(raw_clonotype_id)) %>% 
41
  select(Barcode_full, PatientID, refUMAP_1, refUMAP_2, Entity, IdentI, raw_clonotype_id) %>% 
42
  distinct() %>% 
43
  add_count(IdentI, PatientID, raw_clonotype_id) %>% ungroup() %>% 
44
  select(-Barcode_full) %>% 
45
  group_by(PatientID, raw_clonotype_id, IdentI) %>% 
46
  summarise(refUMAP_1=median(refUMAP_1), refUMAP_2=median(refUMAP_2), n, Entity, .groups = "drop") %>% 
47
  distinct() %>% 
48
  #mutate(n=ifelse(n>50, 50, n)) %>% 
49
  mutate(Entity=factor(Entity, levels=c("rLN", "DLBCL", "MCL", "FL", "MZL")))
50
  
51
DF_5prime_umap <- DFtotal_5prime %>% 
52
  select(refUMAP_1, refUMAP_2, IdentI, Entity, PatientID) %>% 
53
  distinct() %>% 
54
  mutate(Entity=factor(Entity, levels=c("rLN", "DLBCL", "MCL", "FL", "MZL")))
55
56
df_clonotypes_subset <- list()
57
58
# Filter clonotypes to avoud overplotting
59
w <- 30
60
#for(e in entities){
61
  for(i in c(3,5,6)){
62
    for(p in unique(df_clonotypes$PatientID)){
63
        tmp <- df_clonotypes %>% 
64
          filter(n>2, IdentI==i, PatientID==p) 
65
        
66
        if(nrow(tmp)>w){
67
          
68
          df_clonotypes_subset[[paste0(i, p)]] <- tmp %>% sample_n(size = w)
69
          
70
          } else {
71
            df_clonotypes_subset[[paste0(i, p)]] <- tmp
72
          }
73
        
74
      }
75
    }
76
#  }
77
rm(w,p)
78
79
df_clonotypes_subset <- bind_rows(df_clonotypes_subset) %>% 
80
  rbind(df_clonotypes %>% filter(n>2, !IdentI %in% c(3,5,6)),
81
        .)
82
83
p1 <- ggplot()+
84
  geom_point_rast(data=DF_5prime_umap %>% select(-Entity), aes(x=refUMAP_1, y=refUMAP_2), 
85
                  size=0.2, alpha=0.2, stroke=0, shape=21,  fill="grey90")+
86
  geom_point_rast(data=DF_5prime_umap, aes(x=refUMAP_1, y=refUMAP_2, fill=IdentI), size=0.2, 
87
                  alpha=ifelse(DF_5prime_umap$IdentI %in% c("6", "3", "5", "11", "14"), 0.04, 0.2), 
88
                  stroke=0, shape=21)+
89
  geom_point(data=df_clonotypes_subset, aes(x=refUMAP_1, y=refUMAP_2, size=n, color=IdentI), shape=21, stroke=0.25,
90
             alpha=ifelse(df_clonotypes_subset$Entity %in% c("FL", "MZL"), 1, 0.75))+
91
  scale_color_manual(values = colors_umap_cl, guide="none")+
92
  scale_fill_manual(values = colors_umap_cl, guide="none")+
93
  scale_size_continuous(range=c(1, 5), limits=c(3, 50), breaks=c(3, 20, 35, 50),
94
                        labels=c("3", "20", "35", "> 50"), name = NULL)+
95
  facet_wrap(~Entity, nrow = 1)+
96
  geom_text(data = df_clonotypes %>% select(PatientID, Entity) %>% distinct() %>% add_count(Entity), aes(label = paste0("n = ", n)),
97
            x = 9.65, y = -6.9, hjust=1, check_overlap = T, size=2.5)+
98
  xlab("refUMAP-1")+
99
  ylab("refUMAP-2")+
100
  mytheme_1+
101
  theme(legend.position = "top",
102
        legend.text = element_text(size=7),
103
        legend.background = element_rect(fill=NA),
104
        legend.box = unit("cm", x = 0.01),
105
        legend.spacing.x = unit("cm", x = 0.05),
106
        legend.spacing.y = unit("cm", x = 0.001),
107
        panel.border = element_rect(size=0.25, color="black"),
108
        legend.box.spacing = unit(0, units = "cm"),
109
        strip.background = element_rect(size=0.25),
110
        legend.box.margin = unit(c(0,-12.5,-0.1,0), units = "cm"))+
111
  labs(tag = "A")
112
113
p1
114
115
```
116
117
# Quantification of clonotypes
118
```{r quantify clonotypes, fig.height=2.5}
119
120
df_clon_sign <- df_clonotypes %>% mutate(isClonal=ifelse(n>1, "clonal", "not")) %>% 
121
  group_by(isClonal, PatientID, IdentI) %>% 
122
  summarise(Total=sum(n)) %>% 
123
  fill_zeros(names_from = "IdentI", values_from = "Total") %>% 
124
  pivot_wider(names_from = "isClonal", values_from = "Total") %>% 
125
  mutate(Prop=round(clonal/(not+clonal), 2)) %>% 
126
  add_entity() %>% 
127
  filter(IdentI %in% c("5", "3", "6", "11")) %>% 
128
  mutate(Entity=factor(Entity, levels = c("rLN", "DLBCL", "MCL", "FL", "MZL"))) %>% 
129
  mutate(Prop=ifelse(is.nan(Prop), 0, Prop)) %>% 
130
  ungroup() %>% 
131
  mutate(IdentI=factor(IdentI, levels=cluster_order)) %>% 
132
  mutate(Label=factor(IdentI, levels=cluster_order, labels = labels_cl_parsed[as.character(cluster_order)]))
133
134
df_clon_sign_p <- 
135
  df_clon_sign %>% 
136
  mutate(Entity=as.factor(Entity)) %>% 
137
  mutate(Prop=Prop+sample(seq(0.01, 0.03, 0.0001), 17)) %>% 
138
  group_by(IdentI) %>% 
139
  wilcox_test(data=., formula = Prop ~ Entity, ref.group = "rLN", alternative = "less") %>% 
140
  select(IdentI, Entity=group2, p) %>% 
141
  mutate(Entity=factor(Entity, levels = c("rLN", "DLBCL", "MCL", "FL", "MZL"))) %>% 
142
  mutate(p=ifelse(p>0.05, NA, p)) %>% 
143
  filter(!is.na(p)) %>% 
144
  left_join(., data.frame(IdentI=factor(c(5,6,11)), height=c(106, 62.5, 50)))
145
146
p <- list()
147
148
for(i in c(1:4)){
149
  
150
  y <- list(c(3),c(5),c(6),c(11))[[i]]
151
  ylim <- c(65, 125, 75, 58)
152
  
153
  p[[i]] <- df_clon_sign %>% 
154
  filter(IdentI %in% y) %>% 
155
  ggplot(aes(x=Entity, y=100*Prop, fill=IdentI))+
156
  ggbeeswarm::geom_beeswarm(cex = 3.5, stroke=0.25, groupOnX = TRUE, shape = 21, size = 1.25,  alpha = 1, color="white")+
157
  #geom_text(data=df_clon_sign_p %>% filter(IdentI %in% y), inherit.aes = F, aes(y=height, x=Entity, label=p), hjust=0.1, size=2.25, angle=45)+
158
  geom_text(data=df_clon_sign_p %>% filter(IdentI %in% y), inherit.aes = F, aes(y=height, x=Entity, label=p), hjust=0.2, size=2.5, angle=45)+
159
  scale_color_manual(values = colors_umap_cl, guide="none")+
160
  scale_fill_manual(values = colors_umap_cl, guide="none")+
161
  ylab("Cell number")+
162
  scale_y_continuous(name="% of clontypes of size > 1", limits=c(0,ylim[i]))+
163
  scale_x_discrete(expand = c(0.17,0.17))+
164
  facet_wrap(~Label, ncol = 2, labeller = label_parsed)+
165
  mytheme_1+
166
  theme(axis.title.x = element_blank(),
167
        strip.background = element_rect(color=NA),
168
        plot.margin = unit(c(0.1, 0.2, 0, 0), units = "cm"),
169
        panel.border = element_rect(size = 0.5),
170
        axis.text.x = element_text(angle=45, hjust = 1))
171
  
172
  if(i!=1){
173
    p[[i]] <- p[[i]]+
174
      theme(axis.title.y = element_blank()
175
            )
176
  }
177
  
178
  if(i==1){
179
    p[[i]] <- p[[i]]+
180
      labs(tag = "B")
181
  }
182
  
183
  if(i!=4){
184
    p[[i]] <- p[[i]]+theme(
185
      plot.margin = unit(c(0.1, 0.5, 0, 0), units = "cm"))
186
  }
187
  
188
}
189
190
wrap_plots(p, nrow = 1)
191
#ggsave(width = 16, height = 5, units = "cm", filename = "ResponseFigure4.pdf")
192
193
```
194
195
# Shared clonotypes
196
```{r shared clonotypes, fig.height=2.3}
197
198
df_clonotypes_shared <- 
199
  left_join(DFtotal_5prime %>% filter(!is.na(raw_clonotype_id)) %>% 
200
            select(Barcode_fulla=Barcode_full, PatientID, refUMAP_1a=refUMAP_1, refUMAP_2a=refUMAP_2, IdentIa=IdentI, raw_clonotype_id) %>% distinct(),
201
          DFtotal_5prime %>% filter(!is.na(raw_clonotype_id)) %>% 
202
            select(Barcode_fullb=Barcode_full, PatientID, refUMAP_1b=refUMAP_1, refUMAP_2b=refUMAP_2, IdentIb=IdentI, raw_clonotype_id) %>% distinct()
203
          ) %>% 
204
  filter(Barcode_fulla!=Barcode_fullb) %>% 
205
  filter(IdentIa!=IdentIb)
206
207
df_clonotypes_shared_subset <- 
208
  df_clonotypes_shared %>% 
209
  add_entity() %>% 
210
  mutate(Entity=factor(Entity, levels=c("rLN", "DLBCL", "MCL", "FL", "MZL"))) %>% 
211
  filter(PatientID %in% c("LN0132", "LN0302", "LN0193", "LN0198", "LN0078")) %>% 
212
  filter(IdentIa==14)  %>% 
213
  mutate(Ident_shared=ifelse(IdentIa==14, IdentIb, IdentIa))
214
215
p2 <- ggplot()+
216
  geom_point_rast(data=DF_5prime_umap %>% select(-Entity), aes(x=refUMAP_1, y=refUMAP_2), 
217
                  size=0.2, alpha=0.2, stroke=0, shape=21, fill="grey90")+
218
  geom_point_rast(data=DF_5prime_umap %>% 
219
                    filter(PatientID %in% c("LN0132", "LN0302", "LN0193", "LN0198", "LN0078")) %>% 
220
                    mutate(PatientID_new=paste0(PatientID, " (", Entity, ")")),
221
                  aes(x=refUMAP_1, y=refUMAP_2, fill=IdentI), size=0.25, alpha=0.5, stroke=0, shape=21)+
222
  geom_curve(data= df_clonotypes_shared_subset,
223
             aes(x=refUMAP_1a, y=refUMAP_2a, xend=refUMAP_1b, yend=refUMAP_2b, color=Ident_shared,
224
                 group=paste(raw_clonotype_id, PatientID)), size=0.15, alpha=0.2, curvature = 0.3)+
225
  scale_fill_manual(values = colors_umap_cl, guide="none")+
226
  scale_color_manual(values = colors_umap_cl, guide="none")+
227
  facet_wrap(~Entity, nrow = 1)+
228
  geom_text(data = data.frame(PatientID=c("LN0132", "LN0302", "LN0193", "LN0198", "LN0078")) %>% 
229
              add_entity() %>% 
230
              mutate(Entity=factor(Entity, levels=c("rLN", "DLBCL", "MCL", "FL", "MZL"))), aes(label = PatientID), 
231
            x = 9.65, y = -6.9, hjust=1, check_overlap = T, size=2.5)+
232
  xlab("refUMAP-1")+
233
  ylab("refUMAP-2")+
234
  ggtitle(expression('Pairs of identical clonotypes between T'[Pr]~'and other T-cell subsets'))+
235
  mytheme_1+
236
  theme(legend.position = "none",
237
        strip.background = element_rect(size=0.25),
238
        panel.border = element_rect(size=0.25, color="black"))+
239
  labs(tag = "C")
240
241
p2
242
243
```
244
245
# Additional legend
246
```{r legend, fig.height=1.5}
247
248
p_legend <- 
249
  df_comb %>% 
250
  sample_n(100) %>% 
251
  ggplot(aes(x=wnnUMAP_1, y=wnnUMAP_2, color=as.factor(IdentI), fill=as.factor(IdentI)))+
252
  geom_point(size=2.25, stroke=0, shape=21, alpha=1)+
253
  scale_color_manual(values = colors_umap_cl, limits=factor(cluster_order), labels=unlist(labels_cl))+
254
  scale_fill_manual(values = colors_umap_cl, limits=factor(cluster_order), labels=unlist(labels_cl))+
255
  guides(fill=guide_legend(nrow = 2, byrow = T))+
256
  guides(color=guide_legend(nrow = 2, byrow = T))+
257
  coord_fixed(clip = "off")+
258
  mytheme_1+
259
  theme_void()+
260
  theme(legend.position = "top",
261
        legend.text = element_text(size=7.2, margin = unit(units = "cm", x = c(0,0,0,-0.3))),
262
        legend.spacing.x = unit("cm", x = 0.42),
263
        legend.spacing.y = unit("cm", x = 0.001),
264
        legend.key.width = unit("cm", x = 0.055),
265
        legend.key.height = unit("cm", x = 0.5),
266
        legend.box.spacing = unit(0, units = "cm"),
267
        legend.title = element_blank())
268
269
as_ggplot(get_legend(p_legend))
270
271
#ggsave(as_ggplot(get_legend(p_legend)), width = 13, height = 1, units = "cm", filename = "Figure4_legend.pdf")
272
273
```
274
275
# Compose
276
```{r}
277
278
p_full <- p1/wrap_plots(p[[1]], p[[2]], p[[3]], p[[4]], nrow = 1)/p2
279
#ggsave(p_full, width = 18.5, height = 16.75, units = "cm", filename = "Figure4.pdf")
280
281
```
282
283
# Session info
284
```{r session info}
285
286
sessionInfo()
287
288
```