-
Notifications
You must be signed in to change notification settings - Fork 0
/
D.2023_11_08_sc-cluster-assignment-batch2.r
356 lines (297 loc) · 14.8 KB
/
D.2023_11_08_sc-cluster-assignment-batch2.r
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#Have previously done QC, filtering, and azimuth on aggregate J1994 sc data
#split by batch. Will now assess cluster markers and make phenotype determinations
#Redoing from code written June 2023. Making plots to include all markers simultaneously in addition
#to saving plots that focus on one cell type (eg Treg)
#AAG 8 November 2023
library(Seurat)
library(ragg)
library(ggplot2)
library(ggprism)
library(RColorBrewer)
setwd("~/salvage_tmp/final/")
batch2 = load('./code_inputs/2023-06-07_batch2filt.rda')
azimuth_res = load('./code_inputs/2023-06-07_batch2-azimuth.rda')
filt2 = filt; rm(filt)
tab = table(azimuth_meta$seurat_clusters, azimuth_meta$predicted.celltype.l2)
plot_list = list(
all = FeaturePlot(filt2, raster = FALSE, features = c('CD3E', 'CD4', 'CD8B', 'CD14', 'CD19', #General
'LEF1', 'TCF7', 'SELL', #Naive
'IL2RA', 'FOXP3', 'CTLA4', #Treg
'TNFRSF4', 'IL7R', #TCM
'KLRB1', 'SLC4A10', 'TRAV1-2', #MAIT
'ITGB1', 'CCL5', 'CST7', #TEM
'MKI67' #Proliferating
), reduction = 'umap')
)
for(p in 1:length(plot_list)){
fname = paste0('./code_outputs/', names(plot_list)[p], '.png')
agg_png(filename = fname, width = 5600, height = 4800, units = 'px', scaling = 4)
plot( plot_list[[p]])
dev.off()
}
dot_plot_list = list(
all = DotPlot(filt2, features = c('CD3E', 'CD4', 'CD8B', 'CD14', 'CD19', #General
'LEF1', 'TCF7', 'SELL', #Naive
'IL2RA', 'FOXP3', 'CTLA4', #Treg
'TNFRSF4', 'IL7R', #TCM
'KLRB1', 'SLC4A10', 'TRAV1-2', #MAIT
'ITGB1', 'CCL5', 'CST7', #TEM
'MKI67') #Proliferating
)
)
for(p in 1:length(dot_plot_list)){
fname = paste0('./code_outputs/dot_', names(plot_list)[p], '.png')
agg_png(filename = fname, width = 5600, height = 4800, units = 'px', scaling = 5)
plot( dot_plot_list[[p]])
dev.off()
}
#Make mini plots subsetted by cell type
plot_list = list(
general = FeaturePlot(filt2, raster = FALSE, features = c('CD3E', 'CD4', 'CD8B', 'CD14', 'CD19'), reduction = 'umap'),
naive = FeaturePlot(filt2, features = c('LEF1', 'TCF7', 'SELL'), reduction = 'umap', raster = FALSE), #Naive
treg = FeaturePlot(filt2, features = c('CD25', 'IL2RA', 'FOXP3', 'CTLA4'), reduction = 'umap', raster = FALSE), #Treg
tcm = FeaturePlot(filt2, features = c('TNFRSF4', 'TCF7', 'IL7R'), reduction = 'umap', raster = FALSE), #TCM
mait = FeaturePlot(filt2, features = c('KLRB1', 'SLC4A10', 'TRAV1-2'), reduction = 'umap', raster = FALSE), #MAIT
tem = FeaturePlot(filt2, features = c('ITGB1', 'CCL5', 'CST7'), reduction = 'umap', raster = FALSE), #TEM
prolif = FeaturePlot(filt2, features = c('CD4', 'CD8A', 'MKI67'), reduction = 'umap', raster = FALSE) #Proliferating
)
for(p in 1:length(plot_list)){
fname = paste0('./code_outputs/', names(plot_list)[p], '.png')
agg_png(filename = fname, width = 1600, height = 1200, units = 'px', scaling = 4)
plot( plot_list[[p]])
dev.off()
}
dot_plot_list = list(
general = DotPlot(filt2, features = c('CD3E', 'CD4', 'CD8B', 'CD14', 'CD19')),
naive = DotPlot(filt2, features = c('LEF1', 'TCF7', 'SELL')), #Naive
treg = DotPlot(filt2, features = c('CD25', 'IL2RA', 'FOXP3', 'CTLA4')), #Treg
tcm = DotPlot(filt2, features = c('TNFRSF4', 'TCF7', 'IL7R')), #TCM
mait = DotPlot(filt2, features = c('KLRB1', 'SLC4A10', 'TRAV1-2')), #MAIT
tem = DotPlot(filt2, features = c('ITGB1', 'CCL5', 'CST7')), #TEM
prolif = DotPlot(filt2, features = c('CD4', 'CD8A', 'MKI67')) #Proliferating
)
for(p in 1:length(dot_plot_list)){
fname = paste0('./code_outputs/dot_', names(plot_list)[p], '.png')
agg_png(filename = fname, width = 1000, height = 1600, units = 'px', scaling = 3)
plot( dot_plot_list[[p]])
dev.off()
}
#Find cluster markers and save-------
allMk = FindAllMarkers(filt2, min.pct = .5, min.diff.pct = 0.25)
bycl = split(allMk, allMk$cluster)
top100 = lapply(bycl, FUN = function(x){
x = x[1:100, ]
cluster = x$cluster[1]
diff = x$pct.1 - x$pct.2
x = x[order(diff, decreasing = TRUE),]
return(x)
})
for(c in 1:length(top100)){
fname = paste0('./code_outputs/Cluster', c-1, '_topMarkers.csv')
write.csv(file = fname, top100[[c]], row.names = FALSE)
}
save(top100, tab, file = './code_outputs/batch2wholemk.rda')
[email protected]$azimuth = azimuth_meta$predicted.celltype.l2
DimPlot(filt2, reduction = 'umap', raster = FALSE)
tab = table([email protected]$azimuth, [email protected]$seurat_clusters)
write.table(tab, file = './code_outputs/batch2_azimuth_clusters.csv', col.names = NA, sep = ',',row.names = TRUE)
meta = [email protected]
umaps = as.data.frame(filt2@[email protected])
meta = cbind(meta, umaps)
#plot showing VDJs
gg1 = ggplot(meta, aes(x = UMAP_1, y = UMAP_2)) +
geom_point(size = 0.25, color = 'grey') +
geom_point(size = 0.25, data = meta[which(meta$VDJ_beta == TRUE),], color = 'black') +
theme_prism()
fname = paste0('./code_outputs/', 'tcr-beta-vdj', '.png')
agg_png(filename = fname, width = 1200, height = 1200, units = 'px', scaling = 4)
plot(gg1)
dev.off()
#Plot showing clusters
gg2 = ggplot(meta, aes(x = UMAP_1, y = UMAP_2, color = seurat_clusters)) +
geom_point(size = 0.25) +
theme_prism()
fname = paste0('./code_outputs/', 'seurat-clusters', '.png')
agg_png(filename = fname, width = 1200, height = 1200, units = 'px', scaling = 4)
plot(gg2)
dev.off()
#Subset filtered object based on likely T cell clusters------
to_rm= c(1,6,7,8,12,18,23,24)
to_keep = setdiff(c(0:24), to_rm)
filt = subset(filt2, seurat_clusters %in% to_keep == TRUE)
save(filt, file = paste0('./code_outputs/', Sys.Date(), '_batch2filt_TCellOnly.rda'))
#I think a lot of barcode hopping happnes.
tab2 = table(meta$seurat_clusters %in% to_keep, meta$VDJ_beta)
tab2 = table(meta$seurat_clusters, meta$VDJ_beta)
#23/(23k + 9k) VDJ_beta containing barcodes are kept in mu filter
#11k / (11 + 2k) non-VDJ containing barcodes are kept in my filter.
write.table(tab2, file = './code_outputs/batch2_vdj-hopping_allcells.csv', col.names = NA, sep = ',',row.names = TRUE)
#Re cluster data on Filt.-------------------------------------
filt <- FindVariableFeatures(filt, selection.method = 'vst', nfeatures = 5000)
top10 <- head(VariableFeatures(filt), 10)
plot1<- VariableFeaturePlot(filt)
plot2 <- LabelPoints(plot = plot1, points = top10, repel = TRUE)
plot2
filt <- ScaleData(filt, features = VariableFeatures(filt))
filt <- RunPCA(filt, features = VariableFeatures(filt))
plot3 <- ElbowPlot(filt, ndims = 30)
N=25
filt<- RunUMAP(filt, dims = 1:N)
filt <- FindNeighbors(filt, dims = 1:N)
filt <- FindClusters(filt, resolution = 0.5)
#Save refined cluster labels. Added 3 Jan 2024
df = data.frame(Sample = m$Sample, Barcode = m$Barcode, TCluster = m$seurat_clusters)
write.csv(df, file = './code_outputs/batch2-TCluster-Assignments.csv', row.names = FALSE, quote = FALSE)
#Now continue...
idx = match(rownames([email protected]), rownames([email protected]))
[email protected]$azimuth = [email protected]$azimuth[idx]
tab = table([email protected]$seurat_clusters, [email protected]$azimuth)
allMk = FindAllMarkers(filt, min.pct = .5, min.diff.pct = 0.1)
bycl = split(allMk, allMk$cluster)
bycl = bycl[order(names(bycl))]
bycl = lapply(bycl, FUN = function(x){
diff = x$pct.1 - x$pct.2
x = x[order(diff, decreasing = TRUE),]
return(x)
})
require(xlsx)
fname = './code_outputs/batch2-TCluster-Markers.xlsx'
write.xlsx(bycl[[1]], file = fname, append = FALSE, row.names = FALSE,
sheetName = paste0('Cluster', names(bycl)[1]))
for(i in 2:length(bycl)){
write.xlsx(bycl[[i]], file = fname, append = TRUE, row.names = FALSE,
sheetName = paste0('Cluster', names(bycl)[i]))
}
top25 = lapply(bycl, FUN = function(x){
x = x[1:25, ]
cluster = x$cluster[1]
diff = x$pct.1 - x$pct.2
x = x[order(diff, decreasing = TRUE),]
return(x)
})
for(c in 1:length(top25)){
fname = paste0('./code_outputs/Cluster', c-1, '_topMarkers.csv')
write.csv(file = fname, top100[[c]], row.names = FALSE)
}
meta = [email protected]
umaps = as.data.frame(filt@[email protected])
meta = cbind(meta, umaps)
gg3 = ggplot(meta, aes(x = UMAP_1, y = UMAP_2)) +
geom_point(size = 0.25, color = 'grey') +
geom_point(size = 0.25, data = meta[which(meta$VDJ_beta == TRUE),], color = 'black') +
theme_prism()
fname = paste0('./code_outputs/', 'tcr-beta-vdj', '.png')
agg_png(filename = fname, width = 1200, height = 1200, units = 'px', scaling = 4)
plot(gg3)
dev.off()
gg4 = ggplot(meta, aes(x = UMAP_1, y = UMAP_2, color = seurat_clusters)) +
geom_point(size = 0.25) +
theme_prism() + guides(colour = guide_legend(override.aes = list(size=2)))
fname = paste0('./code_outputs/', 'cluster-plot', '.png')
agg_png(filename = fname, width = 1600, height = 1200, units = 'px', scaling = 4)
plot(gg4)
dev.off()
#Make Feature Plots on refined clusters---------
plot_list = list(
all = FeaturePlot(filt, raster = FALSE, features = c('CD3E', 'CD4', 'CD8B', 'CD14', 'CD19', #General
'LEF1', 'TCF7', 'SELL', #Naive
'IL2RA', 'FOXP3', 'CTLA4', #Treg
'TNFRSF4', 'IL7R', #TCM
'KLRB1', 'SLC4A10', 'TRAV1-2', #MAIT
'ITGB1', 'CCL5', 'CST7', #TEM
'MKI67' #Proliferating
), reduction = 'umap')
)
for(p in 1:length(plot_list)){
fname = paste0('./code_outputs/', names(plot_list)[p], '.png')
agg_png(filename = fname, width = 5600, height = 4800, units = 'px', scaling = 4)
plot( plot_list[[p]])
dev.off()
}
dot_plot_list = list(
all = DotPlot(filt, features = c('CD3E', 'CD4', 'CD8B', 'CD14', 'CD19', #General
'LEF1', 'TCF7', 'SELL', #Naive
'IL2RA', 'FOXP3', 'CTLA4', #Treg
'TNFRSF4', 'IL7R', #TCM
'KLRB1', 'SLC4A10', 'TRAV1-2', #MAIT
'ITGB1', 'CCL5', 'CST7', #TEM
'MKI67') #Proliferating
)
)
for(p in 1:length(dot_plot_list)){
fname = paste0('./dot_', names(plot_list)[p], '.png')
agg_png(filename = fname, width = 5600, height = 4800, units = 'px', scaling = 5)
plot( dot_plot_list[[p]])
dev.off()
}
#Make mini plots subsetted by cell type
plot_list = list(
general = FeaturePlot(filt, raster = FALSE, features = c('CD3E', 'CD4', 'CD8B', 'CD14', 'CD19'), reduction = 'umap'),
naive = FeaturePlot(filt, features = c('LEF1', 'TCF7', 'SELL'), reduction = 'umap', raster = FALSE), #Naive
treg = FeaturePlot(filt, features = c('CD25', 'IL2RA', 'FOXP3', 'CTLA4'), reduction = 'umap', raster = FALSE), #Treg
tcm = FeaturePlot(filt, features = c('TNFRSF4', 'TCF7', 'IL7R'), reduction = 'umap', raster = FALSE), #TCM
mait = FeaturePlot(filt, features = c('KLRB1', 'SLC4A10', 'TRAV1-2'), reduction = 'umap', raster = FALSE), #MAIT
tem = FeaturePlot(filt, features = c('ITGB1', 'CCL5', 'CST7'), reduction = 'umap', raster = FALSE), #TEM
prolif = FeaturePlot(filt, features = c('CD4', 'CD8A', 'MKI67'), reduction = 'umap', raster = FALSE) #Proliferating
)
for(p in 1:length(plot_list)){
fname = paste0('./', names(plot_list)[p], '.png')
agg_png(filename = fname, width = 1600, height = 1200, units = 'px', scaling = 4)
plot( plot_list[[p]])
dev.off()
}
dot_plot_list = list(
general = DotPlot(filt, features = c('CD3E', 'CD4', 'CD8B', 'CD14', 'CD19')),
naive = DotPlot(filt, features = c('LEF1', 'TCF7', 'SELL')), #Naive
treg = DotPlot(filt, features = c('CD25', 'IL2RA', 'FOXP3', 'CTLA4')), #Treg
tcm = DotPlot(filt, features = c('TNFRSF4', 'TCF7', 'IL7R')), #TCM
mait = DotPlot(filt, features = c('KLRB1', 'SLC4A10', 'TRAV1-2')), #MAIT
tem = DotPlot(filt, features = c('ITGB1', 'CCL5', 'CST7')), #TEM
prolif = DotPlot(filt2, features = c('CD4', 'CD8A', 'MKI67')) #Proliferating
)
for(p in 1:length(dot_plot_list)){
fname = paste0('./code_outputs/dot_', names(plot_list)[p], '.png')
agg_png(filename = fname, width = 1000, height = 1600, units = 'px', scaling = 3)
plot( dot_plot_list[[p]])
dev.off()
}
#Review Beta Chain annotations------
tab2 = table(meta$seurat_clusters %in% to_keep, meta$VDJ_beta)
tab2 = table(meta$seurat_clusters, meta$VDJ_beta)
# 23/(23+9)k VDJ_beta containing barcodes are kept in mu filter
# 11 / (11+2)k non-VDJ containing barcodes are kept in my filter.
write.table(tab2, file = './code_outputs/batch2_vdj-hopping.csv', col.names = NA, sep = ',',row.names = TRUE)
#Alter annotations following manual inspection------
#Cluster 14 is MAIT
#Cluster 20 is proliferative
#Remainder of azimuth classification looks pretty solid, so I'll keep it.
in_10 = [email protected]$Barcode[which([email protected]$seurat_clusters == 20)]
c10_l2 = l2_scores[, match(in_10, colnames(l2_scores))]
c10_l2 = c10_l2[grep('CD4|CD8', rownames(c10_l2)),]
CD4vCD8 = apply(c10_l2, 2, FUN = function(x){
if(all(x == 0)){return(NA)}
i = which(x == max(x))
CD4_num = length(grep('CD4', names(i)))
CD8_num = length(grep('CD8', names(i)))
if(CD4_num > CD8_num){return('CD4 Proliferating')}
else if(CD8_num > CD4_num){return('CD8 Proliferating')}else{return(NA)}
})
df = data.frame(barcode = in_10, type = CD4vCD8)
[email protected]$aag_annot = [email protected]$azimuth
[email protected]$aag_annot[which([email protected]$seurat_clusters == 20)] = df$type
[email protected]$aag_annot[which([email protected]$seurat_clusters %in% c(14))] = 'MAIT'
tab = table([email protected]$aag_annot, [email protected]$seurat_clusters)
write.table(tab, file = './code_outputs/batch2_final_clusters.csv', col.names = NA, sep = ',',row.names = TRUE)
# tcell_annot = data.frame(
# barcode = [email protected]$Barcode[which([email protected]$VDJ_alpha | [email protected]$VDJ_beta)],
# annot = [email protected]$aag_annot[which([email protected]$VDJ_alpha | [email protected]$VDJ_beta)]
# )
#I don't need to require alpha or beta TCR chain to call a T cell!
tcell_annot = data.frame(
barcode = [email protected]$Barcode[],
annot = [email protected]$aag_annot[]
)
tcell_annot = tcell_annot[-which(tcell_annot$annot %in% c('NK', 'CD14 Mono', 'ILC', 'gdT', 'NK Proliferating',
'NK_CD56bright', 'Platelet', 'CD16 Mono')),]
write.table(tcell_annot, row.names = F, file = paste0('./code_outputs/', Sys.Date(), '_batch2-tcell-annot.csv'), sep = ',')