-
Notifications
You must be signed in to change notification settings - Fork 0
/
figures_part2.Rmd
224 lines (185 loc) · 8.36 KB
/
figures_part2.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
---
title: "figures for single cell analysis"
output:
html_document:
toc: true
toc_float: true
theme: united
---
Xiyu Peng
This file contains scripts to generate figures for single cell analysis, including UMAP and heatmap.
```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_knit$set(root.dir= normalizePath('..'))
knitr::opts_chunk$set(error = FALSE)
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
```
```{r library}
library(ggplot2)
library(Seurat)
library(tidyverse)
library(patchwork)
library(ggpubr)
library(rstatix)
library(ggsci)
library(scales)
```
```{r functions}
downsamplebygroup<-function(obj, metadata_column,downsample = 30000,...){
all_cells<- colnames(obj)
groups<- levels([email protected][, metadata_column])
ps<-list()
for (group in groups) {
subset_indx<- [email protected][, metadata_column] == group
if(sum(subset_indx)>0){
subset_cells<- sample(all_cells[subset_indx],downsample)
subobject<-obj[,subset_cells]
p<- DimPlot(subobject,reduction = "umap",...)+ggtitle(group) +
theme(plot.title = element_text(size = 10, face = "bold"),legend.position = "none")
ps[[group]]<- p
}
}
return(ps)
}
```
## UMAP for individual patients, Fig 4-6
* pt 17-162-05
```{r}
pt1716205<-readRDS(file = "C:/Users/pengx1/OneDrive - Memorial Sloan Kettering Cancer Center/Desktop/reports/seurat_objects/seurat_pt17-162-05redo_v2.rds")
Idents(pt1716205)<-"pool_cluster"
## an example for activation topic
selected_cols<-c("gray",hue_pal()(20)[c(5,9,10,13,17)])
pt1716205$activation_group<-Idents(pt1716205)
pt1716205$activation_group[!Idents(pt1716205)%in% c(4,8,12,16,9)]<-0
#DimPlot(pt1716205, label = T, group.by = "activation_group",cols = selected_cols)
current.cluster.ids <- 0:27
new.cluster.ids <- c("0:CD4 Tn","1:CD4 Tem/cm","2:CD4 Tn","3:CD8 Temra","4:CD4 Tem/cm","5:CD8 Temra","6:CD8 Tn","7:CD4 Tcm (CXCR5+)","8:CD4 Tem/cm (KI67+)","9:Treg","10:CD8 Tem","11:Naive Treg","12:CD8 Tem (KI67+)","13:CD4-CD8-","14:CD4 Tem (Cytotoxic)","15:CD8 Tem (PD1+)","16:CD4-CD8- (KI67+)","17:CD4-CD8-","18:CD4-CD8- (KI67+)","19:CD4+CD8+",rep("others",8))
old_labels<- Idents(pt1716205)
Idents(pt1716205) <- plyr::mapvalues(x = Idents(pt1716205), from = current.cluster.ids, to = new.cluster.ids)
DimPlot(subset(pt1716205, idents = new.cluster.ids[1:20]),reduction = "umap", label = TRUE)
Idents(pt1716205)<-old_labels
```
```{r,eval=FALSE}
genes.reorder<-c("CD4","CD8","FOXP3","CCR7","CD45RA","GITR","CD25","CCR4","CD27","CD28","CD127","TBET","GZM-B","CD57","EOMES","LAG3","CXCR5","HLADR","KI67","TIM3","CD38","ICOS","CTLA4","PD1","TIGIT")
pt1716205_subset<-subset(pt1716205,idents = 0:19,downsample = 1000)
VlnPlot(pt1716205_subset,features = genes.reorder[1:9],pt.size = 0)
VlnPlot(pt1716205_subset,features = genes.reorder[10:18],pt.size = 0)
VlnPlot(pt1716205_subset,features = genes.reorder[19:25],pt.size = 0)
pt1716205_subset<-subset(pt1716205,idents = c(6,12),downsample = 1000)
VlnPlot(pt1716205_subset,features = genes.reorder,pt.size = 0)
FindMarkers(object = pt1716205_subset, ident.1 = 6, ident.2 = 12, slot = "counts",only.pos = FALSE,test.use = "wilcox")
```
```{r,eval=FALSE}
## cluster 6
pt1716205_C6<-subset(pt1716205,idents = 6,downsample = 1000)
data_C6 <- FetchData(pt1716205_C6,
vars = genes.reorder,
slot = "data")
longdata_C6 <- data_C6 %>%
gather(key = "Marker", value = "Intensity")
p1<-ggplot(longdata_C6,
aes(x = Marker, y = Intensity)) +
geom_violin(size = 0, fill = hue_pal()(20)[6])+scale_x_discrete(limits = genes.reorder)+ylim(-500,3700)
p1<-ggpar(p1,legend = "none",title = "Cluster 6",font.xtickslab = c(8),xtickslab.rt = 45)
## cluster 12
pt1716205_C12<-subset(pt1716205,idents = 12,downsample = 1000)
data_C12 <- FetchData(pt1716205_C12,
vars = genes.reorder,
slot = "data")
longdata_C12 <- data_C12 %>%
gather(key = "Marker", value = "Intensity")
p2<-ggplot(longdata_C12,
aes(x = Marker, y = Intensity)) +
geom_violin(size = 0, fill = hue_pal()(20)[12])+scale_x_discrete(limits = genes.reorder)+ylim(-500,3700)
p2<-ggpar(p2,legend = "none",title = "Cluster 12",font.xtickslab = c(8),xtickslab.rt = 45)
wrap_plots(p1,p2,nrow =2)
```
```{r,fig.width=12,fig.height=4}
p_list<-downsamplebygroup(subset(pt1716205, idents = 0:19),"time",20000, group.by = "activation_group",cols = selected_cols,label = TRUE)
layout1<-"
ABC
###
"
wrap_plots(p_list ,guides = 'collect', design = layout1)
```
```{r,fig.width=4,fig.height=4}
## LAG+ example (at week 0) for exhaustion topic
selected_cols<-c("gray",hue_pal()(20)[c(4,6,14,15)])
pt1716205$exhaustion_group<-Idents(pt1716205)
pt1716205$exhaustion_group[!Idents(pt1716205)%in% c(14,3,5,13)]<-0
#DimPlot(pt1716205, label = T, group.by = "exhaustion_group",cols = selected_cols)
p_list<-downsamplebygroup(subset(pt1716205, idents = 0:19),"time",20000, group.by = "exhaustion_group",cols = selected_cols,label = TRUE)
p_list[[1]]+ggtitle("LAG+")
```
* pt 17-162EXT-09
```{r,fig.width=12, fig.height=4}
pt17162ext09<-readRDS(file = "C:/Users/pengx1/OneDrive - Memorial Sloan Kettering Cancer Center/Desktop/reports/seurat_objects/seurat_pt17-162EXT-09redo_v2.rds")
Idents(pt17162ext09)<-"pool_cluster"
selected_cols<-c(hue_pal()(20)[c(1,3,7,12)],"gray")
pt17162ext09$activation_group<-Idents(pt17162ext09)
pt17162ext09$activation_group[!Idents(pt17162ext09)%in% c(0,2,6,11)]<-21
#DimPlot(pt17162ext09, label = T, group.by = "activation_group",cols = selected_cols)
p_list<-downsamplebygroup(subset(pt17162ext09, idents = 0:19),"time",5000, group.by = "activation_group",cols = selected_cols)
wrap_plots(p_list ,guides = 'collect', design = layout1)
```
* pt 17-162-27
```{r,fig.width=4, fig.height=4}
pt1716227<-readRDS(file = "C:/Users/pengx1/OneDrive - Memorial Sloan Kettering Cancer Center/Desktop/reports/seurat_objects/seurat_pt17-162-27redo_v2.rds")
Idents(pt1716227)<-"pool_cluster"
selected_cols<-c("gray",hue_pal()(20)[c(4,6,14,15)])
pt1716227$exhaustion_group<-Idents(pt1716227)
pt1716227$exhaustion_group[!Idents(pt1716227)%in% c(14,3,5,13)]<-0
#DimPlot(pt1716227, label = T, group.by = "exhaustion_group",cols = selected_cols)
p_list<-downsamplebygroup(subset(pt1716205, idents = 0:19),"time",20000, group.by = "exhaustion_group",cols = selected_cols,label = TRUE)
p_list[[1]]+ggtitle("LAG-")
```
## customized Heatmaps
```{r}
load("C:/Users/pengx1/OneDrive - Memorial Sloan Kettering Cancer Center/Desktop/reports/seurat_objects/average_cluster_new.Rdata")
paletteLength <- 50
myColor <- viridis::viridis(paletteLength)
myColor1<-Seurat::PurpleAndYellow()
quantile_breaks <- function(xs, n = 10) {
breaks <- quantile(xs, probs = seq(0, 1, length.out = n))
breaks[!duplicated(breaks)]
}
mat_breaks <- quantile_breaks(mat1, n = 51)
factor(sign(mat1["CD4",]))->CD4_sign
factor(sign(mat1["CD8",]))->CD8_sign
factor(sign(mat1["CCR7",]))->CCR7_sign
factor(sign(mat1["CD45RA",]))->CD45RA_sign
levels(CD8_sign)<-c("neg","pos")
levels(CD4_sign)<-c("neg","pos")
levels(CD45RA_sign)<-c("neg","pos")
levels(CCR7_sign)<-c("neg","pos")
annotation_col = data.frame(
CD4 = CD4_sign,
CD8 = CD8_sign,
CD45RA = CD45RA_sign,
CCR7 = CCR7_sign
)
ann_colors = list(
CD4 = c(neg = "white", pos = "firebrick"),
CD8 = c(neg = "white", pos = "firebrick"),
CD45RA = c(neg = "white", pos = "darkblue"),
CCR7 = c(neg = "white", pos = "darkblue")
)
genes.reorder<-c("CD4","CD8","FOXP3","CCR7","CD45RA","GITR","CD25","CCR4","CD27","CD28","CD127","TBET","GZM-B","CD57","EOMES","LAG3","CXCR5","HLADR","KI67","TIM3","CD38","ICOS","CTLA4","PD1","TIGIT")
cluster.reorder<-c("")
mat1<-mat1[genes.reorder,]
pheatmap::pheatmap(mat1[!rownames(mat1) %in% c("CD3","CD1419"),1:20], color = myColor1,
breaks = mat_breaks, drop_levels = TRUE, annotation_col = annotation_col,annotation_colors = ann_colors,cluster_rows = FALSE)
```
```{r, fig.width= 3}
## for specific clusters
mat_sub<-mat1[,c("4","9","8","12","16")]
pheatmap::pheatmap(mat_sub, color = myColor1,
breaks = mat_breaks, drop_levels = TRUE, cluster_rows = FALSE,cluster_cols = FALSE)
mat_sub<-mat1[,c("0","2","11","6")]
pheatmap::pheatmap(mat_sub, color = myColor1,
breaks = mat_breaks, drop_levels = TRUE, cluster_rows = FALSE,cluster_cols = FALSE)
mat_sub<-mat1[,c("3","5","14","13")]
pheatmap::pheatmap(mat_sub, color = myColor1,
breaks = mat_breaks, drop_levels = TRUE, cluster_rows = FALSE,cluster_cols = FALSE)
```