-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCluster_scReference_reference mapping cell annotations.R
491 lines (401 loc) · 21.8 KB
/
Cluster_scReference_reference mapping cell annotations.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
srun -p short --cpus-per-task 10 --pty bash
module purge
module load HDF5/1.10.5-gompi-2019a
module load umap-learn/0.3.10-foss-2019a-Python-3.7.2
module load Seurat/3.1.2-foss-2019a-R-3.6.0
module load Harmony/1.0.0-foss-2019a-R-3.6.0
R
###############
library("Seurat")
library('harmony')
library(ggplot2)
library(pryr)
library(future)
concat = function(v) {
res = ""
for (i in 1:length(v)){res = paste0(res,v[i])}
res
}
add.alpha <- function(col, alpha=1){
if(missing(col))
stop("Please provide a vector of colours.")
apply(sapply(col, col2rgb)/255, 2,
function(x)
rgb(x[1], x[2], x[3], alpha=alpha)) }
################# get an example dataset (Peng et al myeloid cells) that you can infer cell annotation labels from a pre-labelled dataset (PancrImmune):
# example data is myeloid data but here I have given you some outputs that you can use for the other cell type objects
cell_type_obejct = "myeloid"
Get_example_data<-function(){
batch = "PDAC150K_COMBINED"
PLOTS = "/"
out_dir = "/well/immune-rep/shared/10X_GENOMICS/PDAK150K_WORKING_DATA/COMBINED_WITH_PUBLISHED/"
out_dir_raw = "/well/immune-rep/shared/10X_GENOMICS/PDAK150K_WORKING_DATA/COMBINED_WITH_PUBLISHED/"
file = concat(c("/gpfs3/well/immune-rep/shared/10X_GENOMICS/PDAK150K_WORKING_DATA/PLOTS/Overall_UMAP_annotations_PDAC150Ka_all.txt"))
p <- as.matrix(read.csv(file, head=T, sep="\t"))
p=p[grep("Myeloid", p[,"cell_types"]),]
# Peng all old annotations
file = concat(c("/well/immune-rep/shared/10X_GENOMICS/PDAC_CHINESE_DATA/PLOTS/Overall_UMAP_annotations_2019_PUBLISHED_PDAC_Myeloid_cells.txt"))
p1 <- as.matrix(read.csv(file, head=T, sep="\t"))
###################################### PDAC150K
type = "Myeloid_cells"
analysis = "Merge_PDAC150K_Peng_Steele"
data_1 = readRDS(file = "/well/immune-rep/shared/10X_GENOMICS/PDAK150K_WORKING_DATA/PLOTS//Seurat_harmonised_Myeloid_cells_clusters_classified_PDAC150Ka_23_12_2021_inc_ILCs.pbmc")
head([email protected])
table([email protected]$filtered_cell_type)
inter1 = intersect(rownames(p), rownames([email protected]))
data_1 <- subset(x = data_1, cells = inter1 )
cell_ids =rownames([email protected])
Patient = [email protected]$Patient
Sample.Type = [email protected]$Sample.Type
overall_cell_type = [email protected]$filtered_cell_type
names(overall_cell_type) = cell_ids
names(Patient) = cell_ids
names(Sample.Type) = cell_ids
count_data1= data_1@assays$ RNA@ counts
#################################### Peng
out_dir = "/well/immune-rep/users/mfj169/10X_GENOMICS/2019_PUBLISHED_PDAC/"
PLOTS = "PLOTS/"
batch = "2019_PUBLISHED_PDAC"
type = "Myeloid_cells"
data_2 = readRDS(file=concat(c(out_dir,PLOTS,"/Seurat_UMAP_2019_PUBLISHED_PDAC_21_09_2021_myeloid_cell.rds")))
head([email protected])
cell_ids =rownames([email protected])
Patient2 = [email protected]$orig.ident
Sample.Type2 = [email protected]$Histology
names(Patient2) = cell_ids
names(Sample.Type2) = cell_ids
count_data2= data_2@assays$ RNA@ counts
#################################### Merge and combine
out_dir = "/gpfs2/well/immune-rep/shared/10X_GENOMICS/PDAK150K_WORKING_DATA/COMBINED_WITH_PUBLISHED/"
batch = "COMBINED_PDAC150K"
PLOTS = ""
nz1 = apply(count_data1, 1, sum)
nz2 = apply(count_data2, 1, sum)
intersect_genes = intersect(names(which(nz1>0)), names (which(nz2>0)))
data1 <- CreateSeuratObject(count_data1[intersect_genes,], project = "PDAC150K")
data2 <- CreateSeuratObject(count_data2[intersect_genes,], project = "PENG")
[email protected]$orig.ident = Patient
[email protected]$Sample.Type = Sample.Type
[email protected]$cell_type = overall_cell_type
[email protected]$source = rep("PDAC150K", length(overall_cell_type))
head([email protected])
[email protected]$orig.ident = Patient2
[email protected]$Sample.Type = Sample.Type2
[email protected]$cell_type = rep("UNKNOWN", length(Patient2))
[email protected]$source = rep("PENG", length(Sample.Type2))
head([email protected])
merge1<- merge(data1,y = data2)
head([email protected])
table([email protected]$orig.ident)
table([email protected]$Sample.Type)
table([email protected]$cell_type)
table([email protected]$source)
#################################### Dimensionality reduction and batch correction via harmony
merge1 <- NormalizeData(merge1)
merge1 <- ScaleData(merge1)
merge1 <- FindVariableFeatures(merge1, selection.method = "vst", nfeatures = 2000)
merge1 <- RunPCA(merge1, verbose = FALSE)
merge1 <- RunHarmony(merge1, c("orig.ident","Sample.Type", "source"),plot_convergence = TRUE, nclust = 50, max.iter.cluster = 100, max.iter.harmony = 10)
merge1 = RunUMAP(merge1 ,reduction = "harmony", dims = 1:30,n.components = 10)
merge1 = FindNeighbors(merge1 ,reduction = "umap", dims = 1:2)
merge1 = FindClusters(merge1,reduction = "umap" ,resolution = 1)
head([email protected])
return(merge1)
}
object<-Get_example_data()
################# plot the output of merging datasets together to ensure that this is done properly:
## location for output (prefix): please edit to what you need
Plot_merged_output_data<-function(object, fileout_prefix){
library(cowplot)
p1 <- DimPlot(object = merge1, reduction = "harmony", pt.size = .1, group.by = "source", do.return = TRUE)
p2 <- VlnPlot(object = merge1, features = "harmony_1", group.by = "source", pt.size = .1)
p3 <- DimPlot(object = merge1, reduction = "harmony", pt.size = .1, group.by = "cell_type", do.return = TRUE)
p4 <- DimPlot(object = merge1, reduction = "harmony", pt.size = .1, group.by = "seurat_clusters", do.return = TRUE)
fileout1=concat(c(fileout_prefix,"_1.pdf"))
w=6
pdf(file=fileout1, height=w*2, width=w*2)
par(mfrow= c(1,1), mar = c(4,4,4,4))
plot_grid(p1,p2,p3,p4)
dev.off()
p1 <- DimPlot(merge1, reduction = "umap", group.by = "Sample.Type", pt.size = .1, do.return = TRUE)
p2 <- DimPlot(merge1, reduction = "umap", group.by = "cell_type", pt.size = .1, do.return = TRUE)
p3 <- DimPlot(merge1, reduction = "umap", group.by = "seurat_clusters", pt.size = .1, do.return = TRUE,label = T)
p4 <- DimPlot(merge1, reduction = "umap", group.by = "source", pt.size = .1, do.return = TRUE)
fileout1=concat(c(fileout_prefix,"_2.pdf"))
w=6
pdf(file=fileout1, height=w*2, width=w*2.5)
par(mfrow= c(1,1), mar = c(4,4,4,4))
plot_grid(p1,p2,p3,p4)
dev.off()
}
fileout_prefix=concat(c("Example_Seurat_filtering_", batch,"_",cell_type_object,"_"))
Plot_merged_output_data(object, fileout_prefix)
################# Reference cell type annotation from pre-batch-corrected data including reference and query cells
# The Seurat object needs to have a column in meta data
# called "cell_type" in which the reference cell annotations are provided
# and the query cell annotations are given the term "UNKNOWN"
# Batch correction should be done via harmony with the
# object@ reductions$ harmony@ cell.embeddings as the embeddings object name
# number_cells_sample: the maximum number of reference cells per cell subtype
# to sample on which to build the model (default = 300)
# The output column will be labelled "scReference_label"
scReference<-function(object, number_cells_sample = 300){
### build a classifier on old cells
library(e1071)
cell_ids = rownames([email protected])
cell_type = [email protected]$cell_type
cells_pre_annotated = cell_ids[which(cell_type!="UNKNOWN")]
cells_of_interest_query =setdiff(cell_ids, cells_pre_annotated)
ct = [email protected][,"cell_type"]
names(ct) = rownames([email protected])
table(ct[cells_of_interest_query])
ref_metadata=ct[cells_pre_annotated]
ref_exp_full=object@ reductions$ harmony@ cell.embeddings[cells_pre_annotated,]
dat = data.frame(ref_exp_full, y = as.factor(ref_metadata))
## only select X cells per classification
n = 300
include = NULL
classes = sort(unique(ref_metadata))
for(c in c(1:length(classes))){
w = which(ref_metadata==classes[c])
if(length(w)>n){w = sample(w,n)}
include = c(include, w)
}
print("subsample levels per cell type:")
print(table(ref_metadata[include]))
dat = dat[include,]
svmfit = svm(y ~ ., data = dat, kernel = "radial", cost = 10, scale = FALSE)
print(svmfit)
# predict data
query_exp_full=merge1@ reductions$ harmony@ cell.embeddings[cells_of_interest_query,]
exp_full=merge1@ reductions$ harmony@ cell.embeddings
cell_typ_pred= predict(svmfit, exp_full)
# check how good the prediction was on the reference data
library(caret)
conf = confusionMatrix(dat$y, cell_typ_pred[rownames(dat)])
print(conf$ overall )
print(table(cell_typ_pred, [email protected]$cell_type))
## create output column with predicted labels
scReference_label = cell_type
names(scReference_label) = cell_ids
scReference_label[cells_of_interest_query] = as.character(cell_typ_pred[cells_of_interest_query])
table(scReference_label)
[email protected]$scReference_label = scReference_label
return(object)
}
scReference(object, number_cells_sample = 300)
saveRDS(file=concat(c("Seurat_scReference_", batch,"_",cell_type_object,".seurat")), object)
################# check output (standard plots)
fileout_prefix=concat(c("Example_Seurat_scReference_", batch,"_",cell_type_object,"_"))
Check_scReference_output<-function(object, fileout_prefix){
[email protected]$scReference_label = scReference_label
## original cell labels returned
names(cell_type) = cell_ids
names(cell_type_overall) = cell_ids
table(cell_type_overall[cells_pre_annotated],cell_type[cells_pre_annotated])
[email protected]$refined_annotations = cell_type_overall
table(cell_type_overall, [email protected]$source)
table(cell_type_overall, [email protected]$Sample.Type)
table(cell_type_overall, [email protected]$seurat_clusters)
library(cowplot)
fileout1=concat(c(fileout_prefix,"_1.pdf"))
w=4
pdf(file=fileout1, height=w*2, width=w*4)
par(mfrow= c(1,1), mar = c(4,4,4,4))
p1 <- DimPlot(object =merge1, reduction = "umap", group.by = "source", pt.size = .1, do.return = TRUE,label = T)
p2 <- DimPlot(object =merge1, reduction = "umap", group.by = "cell_type", pt.size = .1, do.return = TRUE)
p3 <- DimPlot(object =merge1, reduction = "umap", group.by = "scReference_label", pt.size = .1, do.return = TRUE)
plot_grid(p1,p2,p3)
dev.off()
}
Check_scReference_output(object, fileout_prefix)
Myeloid_plot_gene_expression<-function(object, fileout_prefix){
file = "/well/immune-rep/shared/10X_GENOMICS/PDAK150K_WORKING_DATA/MYELOID/cluster_annotation_gene_signatures.txt"
p <- as.matrix(read.csv(file, head=T, sep="\t"))
sign = p[,"annot"]
gene.signature = strsplit(gsub(" ", "",p[,"gene.signature"],fixed = T),",")
names(gene.signature) = sign
gene.signature1 = gene.signature
sign_genes = sort(unique(unlist(gene.signature)))
genes = rownames(object@ assays$RNA@ counts)
file = "/well/immune-rep/shared/10X_GENOMICS/PDAK150K_WORKING_DATA/MYELOID/MYELOID_BLOOD/cluster_annotation_gene_signatures.txt"
p <- as.matrix(read.csv(file, head=T, sep="\t"))
sign = p[,"annot"]
gene.signature = strsplit(gsub(" ", "",p[,"gene.list"],fixed = T),",")
names(gene.signature) = sign
gene.signature2 = gene.signature
sign_genes1 = sort(unique(unlist(gene.signature)))
sign_genes=sort(intersect(unique(sign_genes, sign_genes1), genes))
genes =rownames(object@assays$ RNA@ data)
genes_of_interest =sort(unique(intersect(genes, sign_genes)))
exp = object@ assays$ RNA@ data
exp1 = as.matrix(exp[genes_of_interest,])
cell_types = sort(unique([email protected]$refined_annotations))
mat_mean = matrix(data=0, nrow = length(cell_types), ncol = length(genes_of_interest), dimnames = c(list(cell_types),list(genes_of_interest)))
for(i in c(1:length(cell_types))){
w = rownames([email protected])[which([email protected]$refined_annotations == cell_types[i])]
mat_mean [cell_types[i],] = apply(exp1[,w], 1, mean)
}
mat_mean1= mat_mean[,which(apply(mat_mean, 2, sd)!=0)]
mat_mean1 = mat_mean1 [,c(which(apply(mat_mean1, 2, max)>1))]
hc = hclust(dist(t(mat_mean1)))
labels = hc$ labels[hc$order]
hc = hclust(dist(mat_mean1))
labels1 = hc$ labels[hc$order]
Idents(object = object) = [email protected]$refined_annotations
levels(x = object) = sort(unique([email protected]$refined_annotations))
library(RColorBrewer)
cols = add.alpha (brewer.pal(6, "Dark2"), alpha = 0.95)
fileout1=concat(c(fileout_prefix,"_2.pdf"))
w=4
pdf(file=fileout1, height=w*1.9, width=w*8)
par(mfrow= c(1,1), mar = c(5,5,3,3))
DotPlot(object, assay = "RNA", features = labels) +
scale_color_viridis_c() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
dev.off()
xy = object@ reductions$ umap@ cell.embeddings[,c(1:2)]
cell_type = [email protected]$refined_annotations
table([email protected]$refined_annotations, [email protected]$orig.ident)
orig.ident = [email protected]$orig.ident
w = grep("CD45p", orig.ident)
orig.ident[w] = apply(cbind([email protected]$orig.ident[w],[email protected]$Sample.Type[w]),1,paste,collapse = "_")
orig.ident=gsub("_CD45p2_Tumour","_biopsy",gsub("_CD45p1_Tumour","_biopsy",orig.ident))
orig.ident=gsub("_CD45p2_PBMC","_blood",gsub("_CD45p1_PBMC","_blood",orig.ident))
sort(unique(orig.ident))
out_file_table = concat(c(fileout_prefix,"_Overall_UMAP_", batch,".txt"))
out = cbind(xy, apply(cbind("Myeloid" , cell_type), 1, paste, collapse = " "),orig.ident, [email protected]$source, [email protected]$Sample.Type)
colnames(out) = c("UMAP1", "UMAP2","cell type","sample","source","sample_type")
write.table(out, file = out_file_table, append = FALSE, quote = FALSE, sep = "\t",eol = "\n", na = "NA", dec = ".", row.names = T,col.names = T, qmethod = c("escape", "double"),fileEncoding = "")
t = table([email protected]$refined_annotations, [email protected]$orig.ident)
out_file_table = concat(c(fileout_prefix,"_Overall_cell_counts_", batch,".txt"))
write.table(t(t), file = out_file_table, append = FALSE, quote = FALSE, sep = "\t",eol = "\n", na = "NA", dec = ".", row.names = T,col.names = T, qmethod = c("escape", "double"),fileEncoding = "")
}
B_cell_plot_gene_expression<-function(object, fileout_prefix){
########## plot full list of labels
ref_cell_data = "/well/immune-rep/users/mfj169/10X_GENOMICS/REFERENCE_FILES/B_subset_marker_genes.txt"
genes = rownames(pbmc@ assays$ RNA@ counts)
p <- as.matrix(read.csv(ref_cell_data, head=T, sep="\t"))
list_phenotypes = NULL
phenotypes = NULL
for(i in c(1:length(p[,1]))){
g = strsplit(p[i,2], ",")[[1]]
g = gsub(" ","", g)
g = intersect(g, genes)
if(length(g)>=3){
phenotypes = c(phenotypes, p[i,1])
list_phenotypes = c(list_phenotypes, list(g))
}}
names(list_phenotypes) = phenotypes
genes =rownames(merge1@assays$ RNA@ data)
genes_of_interest = c("TXNIP FCER2 FCMR SELL BANK1 EGR1 CD69 CXCR4 DUSP2 JUN IL6 FCRL3 FCRL2 LINC01857 SAMSN1 SIGLEC10 RASSF6 FRZB HOPX BTNL9 FGFR1 IGHD IGHM IGHG1 CD27 CD38 SDC1 JCHAIN PRDM1 XBP1 MZB1 SSR4 GPR183 CD44 KLF2 TNFRSF13B VIM PLAC8 FCRL4 CCR1 ITGAX HMGB2 TUBA1B MKI67 AURKB CD19 MAPK1 CD81 CD40 CD22 CD24 CR2 CD3E CD7 HCST GZMA ZAP70 CD160")
genes_of_interest = unique(c(unlist(strsplit(genes_of_interest," ", fixed = T)), unlist(list_phenotypes)))
genes_of_interest = c(genes_of_interest, "IGHD","IGHM","IGHG1","IGHA1")
genes_of_interest = sort(unique(intersect(genes_of_interest, genes)))
genes_of_interest = setdiff(genes_of_interest,c( "CD3E" ))
genes =rownames(object@assays$ RNA@ data)
genes_of_interest =sort(unique(intersect(genes, c(genes_of_interest, sign_genes))))
exp = object@ assays$ RNA@ data
exp1 = as.matrix(exp[genes_of_interest,])
cell_types = sort(unique([email protected]$refined_annotations))
mat_mean = matrix(data=0, nrow = length(cell_types), ncol = length(genes_of_interest), dimnames = c(list(cell_types),list(genes_of_interest)))
for(i in c(1:length(cell_types))){
w = rownames([email protected])[which([email protected]$refined_annotations == cell_types[i])]
mat_mean [cell_types[i],] = apply(exp1[,w], 1, mean)
}
mat_mean1= mat_mean[,which(apply(mat_mean, 2, sd)!=0)]
mat_mean1 = mat_mean1 [,c(which(apply(mat_mean1, 2, max)>1))]
hc = hclust(dist(t(mat_mean1)))
labels = hc$ labels[hc$order]
hc = hclust(dist(mat_mean1))
labels1 = hc$ labels[hc$order]
Idents(object = object) = [email protected]$refined_annotations
levels(x = object) = sort(unique([email protected]$refined_annotations))
library(RColorBrewer)
cols = add.alpha (brewer.pal(6, "Dark2"), alpha = 0.95)
fileout1=concat(c(fileout_prefix,"_2.pdf"))
w=4
pdf(file=fileout1, height=w*1.9, width=w*8)
par(mfrow= c(1,1), mar = c(5,5,3,3))
DotPlot(object, assay = "RNA", features = labels) +
scale_color_viridis_c() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
dev.off()
xy = object@ reductions$ umap@ cell.embeddings[,c(1:2)]
cell_type = [email protected]$refined_annotations
table([email protected]$refined_annotations, [email protected]$orig.ident)
orig.ident = [email protected]$orig.ident
w = grep("CD45p", orig.ident)
orig.ident[w] = apply(cbind([email protected]$orig.ident[w],[email protected]$Sample.Type[w]),1,paste,collapse = "_")
orig.ident=gsub("_CD45p2_Tumour","_biopsy",gsub("_CD45p1_Tumour","_biopsy",orig.ident))
orig.ident=gsub("_CD45p2_PBMC","_blood",gsub("_CD45p1_PBMC","_blood",orig.ident))
sort(unique(orig.ident))
out_file_table = concat(c(fileout_prefix,"_Overall_UMAP_", batch,".txt"))
out = cbind(xy, apply(cbind("Myeloid" , cell_type), 1, paste, collapse = " "),orig.ident, [email protected]$source, [email protected]$Sample.Type)
colnames(out) = c("UMAP1", "UMAP2","cell type","sample","source","sample_type")
write.table(out, file = out_file_table, append = FALSE, quote = FALSE, sep = "\t",eol = "\n", na = "NA", dec = ".", row.names = T,col.names = T, qmethod = c("escape", "double"),fileEncoding = "")
t = table([email protected]$refined_annotations, [email protected]$orig.ident)
out_file_table = concat(c(fileout_prefix,"_Overall_cell_counts_", batch,".txt"))
write.table(t(t), file = out_file_table, append = FALSE, quote = FALSE, sep = "\t",eol = "\n", na = "NA", dec = ".", row.names = T,col.names = T, qmethod = c("escape", "double"),fileEncoding = "")
}
T_cell_plot_gene_expression<-function(object, fileout_prefix){
file = "/well/immune-rep/shared/10X_GENOMICS/PDAK150K_WORKING_DATA/Ordered_gene_list_T_cells.txt"
p <- as.matrix(read.csv(file, head=F, sep="\t"))
T_cell_gene_set = p[,1]
names(T_cell_gene_set) = p[,2]
file = "/well/immune-rep/shared/10X_GENOMICS/PDAK150K_WORKING_DATA/Ordered_gene_list_NK_cells.txt"
p <- as.matrix(read.csv(file, head=F, sep="\t"))
NK_cell_gene_set = p[,1]
names(NK_cell_gene_set) = p[,2]
genes =rownames(merge1@assays$ RNA@ data)
labels = intersect(T_cell_gene_set ,genes)
genes =rownames(object@assays$ RNA@ data)
genes_of_interest =sort(unique(intersect(genes, labels)))
exp = object@ assays$ RNA@ data
exp1 = as.matrix(exp[genes_of_interest,])
cell_types = sort(unique([email protected]$refined_annotations))
mat_mean = matrix(data=0, nrow = length(cell_types), ncol = length(genes_of_interest), dimnames = c(list(cell_types),list(genes_of_interest)))
for(i in c(1:length(cell_types))){
w = rownames([email protected])[which([email protected]$refined_annotations == cell_types[i])]
mat_mean [cell_types[i],] = apply(exp1[,w], 1, mean)
}
mat_mean1= mat_mean[,which(apply(mat_mean, 2, sd)!=0)]
mat_mean1 = mat_mean1 [,c(which(apply(mat_mean1, 2, max)>1))]
hc = hclust(dist(t(mat_mean1)))
labels = hc$ labels[hc$order]
hc = hclust(dist(mat_mean1))
labels1 = hc$ labels[hc$order]
Idents(object = object) = [email protected]$refined_annotations
levels(x = object) = sort(unique([email protected]$refined_annotations))
library(RColorBrewer)
cols = add.alpha (brewer.pal(6, "Dark2"), alpha = 0.95)
fileout1=concat(c(fileout_prefix,"_2.pdf"))
w=4
pdf(file=fileout1, height=w*1.9, width=w*8)
par(mfrow= c(1,1), mar = c(5,5,3,3))
DotPlot(object, assay = "RNA", features = labels) +
scale_color_viridis_c() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
dev.off()
xy = object@ reductions$ umap@ cell.embeddings[,c(1:2)]
cell_type = [email protected]$refined_annotations
table([email protected]$refined_annotations, [email protected]$orig.ident)
orig.ident = [email protected]$orig.ident
w = grep("CD45p", orig.ident)
orig.ident[w] = apply(cbind([email protected]$orig.ident[w],[email protected]$Sample.Type[w]),1,paste,collapse = "_")
orig.ident=gsub("_CD45p2_Tumour","_biopsy",gsub("_CD45p1_Tumour","_biopsy",orig.ident))
orig.ident=gsub("_CD45p2_PBMC","_blood",gsub("_CD45p1_PBMC","_blood",orig.ident))
sort(unique(orig.ident))
out_file_table = concat(c(fileout_prefix,"_Overall_UMAP_", batch,".txt"))
out = cbind(xy, apply(cbind("Myeloid" , cell_type), 1, paste, collapse = " "),orig.ident, [email protected]$source, [email protected]$Sample.Type)
colnames(out) = c("UMAP1", "UMAP2","cell type","sample","source","sample_type")
write.table(out, file = out_file_table, append = FALSE, quote = FALSE, sep = "\t",eol = "\n", na = "NA", dec = ".", row.names = T,col.names = T, qmethod = c("escape", "double"),fileEncoding = "")
t = table([email protected]$refined_annotations, [email protected]$orig.ident)
out_file_table = concat(c(fileout_prefix,"_Overall_cell_counts_", batch,".txt"))
write.table(t(t), file = out_file_table, append = FALSE, quote = FALSE, sep = "\t",eol = "\n", na = "NA", dec = ".", row.names = T,col.names = T, qmethod = c("escape", "double"),fileEncoding = "")
}
if(cell_type_obejct == "myeloid"){Myeloid_plot_gene_expression(object, fileout_prefix)}
if(cell_type_obejct == "B_cell"){B_cell_plot_gene_expression(object, fileout_prefix)}
if(cell_type_obejct == "T_cell"){T_cell_plot_gene_expression(object, fileout_prefix)}
###################### Get pseudobulk output (to be completed soon!)
Get_pseudobulk_expression<-function(object){}