-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqc_thresholds.R
485 lines (358 loc) · 21.3 KB
/
qc_thresholds.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
### loading the required libraries
source('Codes/Functions.R')
source('Codes/FactorAnalysisUtils.R')
Initialize()
get_scaled_by_gene <- function(x){
y <- scale(t(x), center = TRUE, scale = TRUE)
y[is.nan(y)] <- 0
y2 <- t(y)
y2 <- as.data.frame(y2)
return(y2)
}
qc_variables <- c( "Library_size", "num_expressed_genes","mito_perc" )
check_qc_cor <- function(merged_samples, pca_embedding_df, main){
df_cor <- data.frame(pca_embedding_df,
Library_size=merged_samples$nCount_RNA,
num_expressed_genes=merged_samples$nFeature_RNA,
mito_perc=merged_samples$mito_perc)
df_cor <- cor(df_cor[,!colnames(df_cor)%in% c('clusters')])
df_cor_sub <- df_cor[colnames(df_cor)%in%qc_variables, !colnames(df_cor)%in%qc_variables]
pheatmap(df_cor_sub, fontsize_row=10, main=main, color = magma(20,direction = 1))
}
qc_info = list(MIT_CUT_OFF=40, LIB_SIZE_CUT_OFF=2000)
sample_names = c('rat_DA_M_10WK_003', 'rat_DA_01_reseq', 'rat_Lew_01', 'rat_Lew_02' )
strain_info = c('DA', 'DA', 'LEW', 'LEW')
data_norm_list = list(NA, NA, NA, NA)
names(data_norm_list) = sample_names
for(i in 1:length(sample_names)){
sample_name = sample_names[i]
input_from_10x <- paste0("Data/", sample_name,'/')
data <- CreateSeuratObject(counts=Read10X(input_from_10x, gene.column = 2),
min.cells=0,min.features=1,
project = "snRNAseq")
dim(data)
MIT_PATTERN = '^Mt-'
mito_genes_index <- grep(pattern = MIT_PATTERN, rownames(data) )
data[["mito_perc"]] <- PercentageFeatureSet(data, features = mito_genes_index)
to_drop_mito = data$mito_perc > qc_info$MIT_CUT_OFF
to_drop_lib_size = data$nCount_RNA < qc_info$LIB_SIZE_CUT_OFF
to_drop_numgenes = data$nFeature_RNA < 100
data$sample_name = sample_name
summary(data$mito_perc )
summary(data$nFeature_RNA )
sum(!to_drop_mito & !to_drop_numgenes)
sum(!to_drop_mito & !to_drop_lib_size)
sum(to_drop_lib_size)
sum(to_drop_numgenes)
sum(to_drop_mito)
data <- data[,!to_drop_mito & !to_drop_lib_size]
data = SCTransform(data, vst.flavor = "v2", verbose = TRUE)
data_norm_list[[i]] = data
}
rm(data)
gc()
merged_data <- merge(data_norm_list[[1]], c(data_norm_list[[2]], data_norm_list[[3]], data_norm_list[[4]] ), #
add.cell.ids = names(data_norm_list),
project = names(data_norm_list),
merge.data = TRUE)
dim(merged_data)
Idents(merged_data) = merged_data$sample_name
merged_data <- SCTransform(merged_data,verbose=T)
merged_data = FindVariableFeatures(merged_data)
merged_data <- RunPCA(merged_data,verbose=T)
plot(100 * merged_data@reductions$pca@stdev^2 / merged_data@reductions$pca@misc$total.variance,
pch=20,xlab="Principal Component",ylab="% variance explained",log="y")
#### UMAP on corrected PC components and clustering
merged_data <- RunHarmony(merged_data, group.by.vars = "sample_name")
merged_data <- RunUMAP(merged_data, reduction = "harmony", dims = 1:30, reduction.name = "umap_h") %>%
FindNeighbors(reduction = "harmony", dims = 1:30, verbose = FALSE) %>%
FindClusters(resolution = 0.7, verbose = FALSE)
merged_data <- readRDS('Results/review_analysis/merged_samples_TLH_harmonized_qc_mt40_lib2000.rds')
Resolution = 0.3
merged_data <- FindClusters(merged_data, resolution = Resolution, verbose = FALSE)
gene_name = 'Ecm1'
cluster_num = 5
is_cluster = ifelse(merged_data$seurat_clusters== cluster_num, paste0('cluster',cluster_num), 'other')
df_umap <- data.frame(#UMAP_1=getEmb(merged_data, 'umap')[,1],
#UMAP_2=getEmb(merged_data, 'umap')[,2],
UMAP_h_1=getEmb(merged_data, 'umap_h')[,1],
UMAP_h_2=getEmb(merged_data, 'umap_h')[,2],
library_size= merged_data$nCount_RNA,
mito_perc=merged_data$mito_perc,
n_expressed=merged_data$nFeature_RNA,
cluster=merged_data$SCT_snn_res.0.3,
is_cluster=is_cluster,
#cell_status = merged_data$cell_status,
#nuclear_fraction=merged_data$nuclear_fraction,
Alb=GetAssayData(merged_data)['Alb',],
gene=GetAssayData(merged_data)[gene_name,],
#strain = merged_data$strain
sample_name = merged_data$sample_name)
ggplot(df_umap, aes(x=UMAP_h_1, y=UMAP_h_2, color=cluster))+geom_point(alpha=0.6)+theme_classic()+
scale_color_manual(values = colorPalatte)+ggtitle(paste0('res: ', Resolution))+
xlab('UMAP_1')+ylab('UMAP_2')
ggplot(df_umap, aes(x=UMAP_h_1, y=UMAP_h_2, color=gene))+geom_point(alpha=0.4)+theme_classic()+
scale_color_viridis(direction = -1)+ggtitle(gene_name)
ggplot(df_umap, aes(x=UMAP_h_1, y=UMAP_h_2, color=is_cluster))+geom_point(alpha=0.8)+theme_classic()#+ggtitle(paste0('res: ', Resolution))
ggplot(df_umap, aes(x=UMAP_h_1, y=UMAP_h_2, color=sample_name))+geom_point(alpha=0.3)+theme_classic()#+ggtitle(paste0('res: ', Resolution))
ggplot(df_umap, aes(x=UMAP_h_1, y=UMAP_h_2, color=Alb))+geom_point(alpha=0.4)+theme_classic()+
scale_color_viridis(direction = -1)+ggtitle('Alb')
########################################################################################################################
#######################################################################################################
############## Running Varimax on the harmonized QC samples ########################################
######################################################################################################
files_rds = sapply(1:length(data_norm_list),
function(i) {
x = data_norm_list[[i]]
# x = x[,!x$SCT_snn_res.0.7 %in% hep_clusters[[i]]]
DefaultAssay(x) <- 'RNA'
x@assays$SCT <- NULL
x = SCTransform(x, conserve.memory=F, verbose=T, return.only.var.genes=F,
variable.features.n = nrow(x[['RNA']]),
## according to the paper scaling is not recommended prior to PCA:
## https://www.biorxiv.org/content/10.1101/576827v2
do.scale = FALSE, ### default value
do.center = TRUE) ### default value ))
x = CreateSeuratObject(GetAssayData(x, assay='SCT'))
x$sample_name = sample_names[i]
x$strain = strain_info[i]
return(x)
},simplify = F)
names(files_rds) = sample_names
merged_samples <- merge(files_rds[[1]], c(files_rds[[2]], files_rds[[3]], files_rds[[4]]),
add.cell.ids = names(files_rds),
project = "harmonizedQC",
merge.data = TRUE)
dim(merged_samples)
lapply(files_rds, dim)
merged_samples = SCTransform(merged_samples, conserve.memory=F, verbose=T, return.only.var.genes=F,
variable.features.n = nrow(merged_samples[['RNA']]),
## according to the paper scaling is not recommended prior to PCA:
## https://www.biorxiv.org/content/10.1101/576827v2
do.scale = FALSE, ### default value
do.center = TRUE) ### default value ))
#merged_samples <- ScaleData(merged_samples, features = rownames(merged_samples))
############ adding metadata calculated using the standard analysis
######## using the complete data
sum(colnames(merged_samples) != colnames(merged_data))
rownames([email protected]) == rownames([email protected])
merged_samples$strain=unlist(lapply(str_split(string = [email protected]$sample_name,pattern = '_'), '[[',2))
####################################################################
################## Peforming the varimax analysis ################
####################################################################
### needed data for the varimax PCA
merged_samples <- RunPCA(merged_samples, features = rownames(merged_samples))
loading_matrix = Loadings(merged_samples, 'pca')
# USING RNA is REALLY IMPORTANT. REMOVING THE SCT does not work and seurat obj seems to remember that
gene_exp_matrix = GetAssayData(merged_samples, assay = 'RNA')
dim(gene_exp_matrix)
dim(loading_matrix)
rot_data <- readRDS('Results/review_analysis/VarimaxPCA_merged_samples_allfeatures_TLH_harmonized_qc_mt40_lib2000_scTrans_res.rds')
#rot_data <- get_varimax_rotated(gene_exp_matrix, loading_matrix)
rotatedLoadings <- rot_data$rotLoadings
scores <- data.frame(rot_data$rotScores)
colnames(scores) = paste0('PC_', 1:ncol(scores))
head(scores)
head(rotatedLoadings)
plot(scores[,1], scores[,2]) ### checking if varimax factors look meaningful
embedd_df_rotated <- data.frame(scores)
PCs_to_check = 1:ncol(embedd_df_rotated)
embedd_df_rotated_2 <- embedd_df_rotated[,PCs_to_check]
colnames(embedd_df_rotated_2) <- paste0('Var_', PCs_to_check)
#### adding meta data to the varimax data frame
sum(rownames([email protected]) != rownames(embedd_df_rotated_2))
embedd_df_rotated_2 = data.frame(cbind(embedd_df_rotated_2, [email protected])) ### run for the complete data
merged_data$strain=unlist(lapply(str_split(string = [email protected]$sample_name,pattern = '_'), '[[',2))
table(merged_data$strain)
check_qc_cor(merged_data, embedd_df_rotated_2[,PCs_to_check],
main='Varimax-PC embeddings correlation with technical covariates')
pdf('Results/review_analysis/VarimaxPCA_merged_samples_allfeatures_TLH_harmonized_qc_mt40_lib2000_scTrans_scale.pdf',width = 10,height=13)
for(i in PCs_to_check){
pc_num = i
rot_df <- data.frame(Varimax_1=embedd_df_rotated_2$Var_1,
emb_val=embedd_df_rotated_2[,pc_num],
#cluster=as.character(merged_samples$final_cluster),
cluster=as.character(embedd_df_rotated_2$seurat_clusters),
#label = as.character(embedd_df_rotated_2$annot_TLH),
sample_name=embedd_df_rotated_2$sample_name,
strain=embedd_df_rotated_2$strain,
#nuclear_fraction = embedd_df_rotated_2$nuclear_fraction,
nCount_RNA = embedd_df_rotated_2$nCount_SCT,
UMAP_h_1=getEmb(merged_data, 'umap_h')[,1],
UMAP_h_2=getEmb(merged_data, 'umap_h')[,2])
p1=ggplot(rot_df, aes(x=Varimax_1, y=emb_val, color=cluster))+geom_point(alpha=0.8, size=1.1)+
theme_classic()+ylab(paste0('Varimax_',i))+scale_color_manual(values=colorPalatte)
p2=ggplot(rot_df, aes(x=Varimax_1, y=emb_val, color=sample_name))+geom_point(alpha=0.4, size=1.2)+
theme_classic()+ylab(paste0('Varimax_',i))+scale_color_manual(values=colorPalatte)
p3=ggplot(rot_df, aes(x=Varimax_1, y=emb_val, color=strain))+geom_point(alpha=0.4, size=1.2)+
theme_classic()+ylab(paste0('Varimax_',i))+scale_color_manual(values=colorPalatte)
p4=ggplot(rot_df, aes(x=Varimax_1, y=emb_val, color=nCount_RNA))+geom_point(size=1.2)+
theme_classic()+ylab(paste0('Varimax_',i))+scale_color_viridis(direction = -1)
#p5=ggplot(rot_df, aes(x=sample_name, y=emb_val, color=nCount_RNA))+geom_boxplot()+theme_classic()
gridExtra::grid.arrange(p1,p2,p3,p4,ncol=2,nrow=2)
}
dev.off()
strain_factor = 23
strain_factor = 7
i = strain_factor
pc_num = i
rot_df <- data.frame(Varimax_1=embedd_df_rotated_2$Var_1,
emb_val=embedd_df_rotated_2[,pc_num],
#cluster=as.character(merged_samples$final_cluster),
cluster=as.character(embedd_df_rotated_2$seurat_clusters),
#label = as.character(embedd_df_rotated_2$annot_TLH),
sample_name=embedd_df_rotated_2$sample_name,
strain=merged_data$strain,
#nuclear_fraction = embedd_df_rotated_2$nuclear_fraction,
nCount_RNA = embedd_df_rotated_2$nCount_SCT,
UMAP_1=getEmb(merged_data, 'umap_h')[,1],
UMAP_2=getEmb(merged_data, 'umap_h')[,2])
ggplot(rot_df, aes(x=Varimax_1, y=emb_val, color=strain))+geom_point(alpha=0.6, size=1.1)+
theme_classic()+ylab(paste0('Varimax ',i))+xlab("Varimax 1")+
scale_color_manual(values = c("#CC79A7","#0072B2"))+
theme(text = element_text(size=16), legend.title = element_blank())
head(loading_matrix[order(loading_matrix[,strain_factor], decreasing = T),],20)
head(loading_matrix[order(loading_matrix[,strain_factor], decreasing = F),],20)
ggplot(rot_df, aes(x=UMAP_h_1, y=UMAP_h_2, color=emb_val))+geom_point(alpha=0.4,size=1)+theme_classic()+
scale_color_viridis(direction = -1, option = 'magma')+ggtitle(paste0('Varimax ', strain_factor))
ggplot(rot_df, aes(y=emb_val, x=strain, fill=strain))+geom_boxplot()+theme_classic()+
ylab(paste0('Varimax ', strain_factor, ''))+
scale_fill_manual(values = c("#CC79A7","#0072B2"))+theme(text = element_text(size=18),
axis.text.x = element_text(size=13,angle=90,color='black'),
legend.title = element_blank(), legend.position = "none")
scale_x_discrete(labels = xlabel2)+xlab('')
ggplot(, aes(y=emb_val, x=strain, fill=strain))+geom_boxplot()+theme_classic()+
ggtitle(paste0('Varimax ', strain_factor, ' Macrophages'))+ylab(paste0('Varimax-', strain_factor, ' Score'))
ggplot(rot_df[rot_df$cluster==5,], aes(y=emb_val, x=strain, fill=strain))+geom_boxplot()+theme_classic()+
ylab(paste0('Varimax ', strain_factor, ''))+
scale_fill_manual(values = c("#CC79A7","#0072B2"))+theme(text = element_text(size=18),
axis.text.x = element_text(size=13,angle=90,color='black'),
legend.title = element_blank(), legend.position = "none")
library(plyr)
library(stats)
library(ggpubr)
library(RColorBrewer)
library(viridis)
library(scales)
data_summary <- function(x) {
m <- mean(x)
ymin <- m-sd(x)
ymax <- m+sd(x)
return(c(y=m,ymin=ymin,ymax=ymax))
}
ggplot(rot_df[rot_df$cluster==5,], aes(x=strain, y=emb_val, fill=strain))+geom_boxplot()+theme_classic()+
scale_fill_manual(values = c("#CC79A7","#0072B2"))+theme_classic()+
ylab(paste0('Varimax ', pc_num))+xlab('')+
theme(text = element_text(size=17),axis.text.x = element_text(size=18,color='black'), legend.title = element_blank())+
stat_summary(fun.data=data_summary)+stat_compare_means(label.x = 0.9, label.y = 3.8, size=5)
ggplot(rot_df, aes(x=strain, y=emb_val, fill=strain))+geom_boxplot()+theme_classic()+
scale_fill_manual(values = c("#CC79A7","#0072B2"))+theme_classic()+
ylab(paste0('Varimax ', pc_num))+xlab('')+
theme(text = element_text(size=17),axis.text.x = element_text(size=18,color='black'), legend.title = element_blank())+
stat_summary(fun.data=data_summary)+stat_compare_means(label.x = 0.9, label.y = 3.8, size=5)
rot_df$Varimax = abs(rot_df$emb_val)
ggplot(rot_df, aes(x=UMAP_1, y=UMAP_2, color=abs(Varimax)))+geom_point(alpha=0.5,size=1)+
scale_color_viridis(direction = -1, option = 'inferno',name=paste0("Varimax ", pc_num))+theme_classic()+
theme(text = element_text(size=16), legend.title=element_text(size=15))
######################################################
varimax_res_harmonized <- readRDS('Results/review_analysis/VarimaxPCA_merged_samples_allfeatures_TLH_harmonized_qc_mt40_lib2000_scTrans_res.rds')
varimax_res_TLH <- readRDS('Results/old_samples/varimax_rotated_OldMergedSamples_mt40_lib1500_MTremoved.rds')
rotLoadings_harmonized = data.frame(sapply(1:ncol(varimax_res_harmonized$rotLoadings),function(i)varimax_res_harmonized$rotLoadings[,i],simplify = T ))
rotLoadings_harmonized = rotLoadings_harmonized[,1:25]
rotLoadings_TLH = data.frame(sapply(1:ncol(varimax_res_TLH$rotLoadings),function(i)varimax_res_TLH$rotLoadings[,i],simplify = T ))
rotLoadings_TLH = rotLoadings_TLH[,1:25]
colnames(rotLoadings_harmonized) = paste0('Var ',1:ncol(rotLoadings_harmonized), '')
colnames(rotLoadings_TLH) = paste0('Var.PC',1:ncol(rotLoadings_TLH), '')
rotLoadings_harmonized$genes = rownames(rotLoadings_harmonized)
rotLoadings_TLH$genes = rownames(rotLoadings_TLH)
merged_var_loadings = merge(rotLoadings_harmonized, rotLoadings_TLH, by.x='genes', by.y='genes')
head(merged_var_loadings)
cor_mat = cor(merged_var_loadings[,-1])
colnames(cor_mat)
cor_mat_sub = cor_mat[1:(ncol(rotLoadings_harmonized)-1), ncol(rotLoadings_harmonized):ncol(cor_mat)]
pheatmap(cor_mat_sub)
########################################################################################################
################## Performing correaltion between average expression of clusters ##################
########################################################################################################
##################################################################
############## calculating the cluster average expression of the new data ##############
merged_data$seurat_clusters <- merged_data$SCT_snn_res.0.3
nb.cols <- length(names(table(merged_data$seurat_clusters)))
mycolors <- colorRampPalette(brewer.pal(8, "Set1"))(nb.cols) #Pastel1
merged_data <- FindNeighbors(merged_data, reduction = "harmony", dims = 1:30)
merged_data <- FindClusters(merged_data, resolution = 0.7)
df_umap$cluster = merged_data$SCT_snn_res.0.3
ggplot(df_umap, aes(x=UMAP_1, y=UMAP_2, color=cluster))+geom_point(size=1,alpha=0.6)+
theme_classic()+scale_color_manual(values = c(colorPalatte)) #mycolors
ggplot(df_umap, aes(x=UMAP_1, y=UMAP_2, color=cluster))+geom_point(size=1,alpha=0.6)+theme_classic()
merged_data$cluster = as.character(merged_data$seurat_clusters)
cluster_names_types = names(table(merged_data$cluster))
### dividing the expression matrix based on the clusters
cluster_expression <- sapply(1:length(cluster_names_types), function(i){
a_cluster_name = cluster_names_types[i]
GetAssayData(merged_data, 'data')[,merged_data$cluster == a_cluster_name]
}, simplify = F)
names(cluster_expression) = cluster_names_types
lapply(cluster_expression, head)
## calculate the average expression of each gene in each cluster
cluster_average_exp <- lapply(cluster_expression, function(x){
df = data.frame(average=rowSums(x)/ncol(x))
return(df)
})
lapply(cluster_average_exp, dim)
lapply(cluster_average_exp, head)
## Concatenate all the clusters together to make a matrix
cluster_average_exp_df = do.call(cbind,cluster_average_exp)
#colnames(cluster_average_exp_df) = paste0('cluster_',names(cluster_average_exp))
colnames(cluster_average_exp_df) = paste0('c_',names(cluster_average_exp))
head(cluster_average_exp_df)
## scale and center all the genes in the matrix
cluster_average_exp_df <- get_scaled_by_gene(cluster_average_exp_df) ## scaling over the clusters of interest
cluster_average_exp_df$rat_ID=rownames(cluster_average_exp_df)
head(cluster_average_exp_df)
cluster_average_exp_df = cluster_average_exp_df[rownames(cluster_average_exp_df)%in%VariableFeatures(merged_data),]
head(cluster_average_exp_df)
dim(cluster_average_exp_df)
##################################################################
###### importing cluster average means of reference maps
ref_cluster_average_exp_df <- readRDS('~/RatLiver/Results/rat_old_cluster_average_exp_all.rds')
### refine the annotation names for each of the clusters
colnames_set1 = c("Hep (0)", "Hep (1)", "Hep (12)", "Hep (15)", "Hep (16)", "Hep (2)", "Hep (4)",
"Hep (6)", "Hep (8)", "Lyz2/Cd74 Mo/Mac (9)", "Endothelial (11)", "Endothelial (3)", "Lymphocyte (13)",
"Marco/Cd5l Mac (10)", "Marco/Cd5l Mac (5)", "Mesenchymal (14)", "Mesenchymal (7)", "rat_ID" )
colnames(ref_cluster_average_exp_df) = colnames_set1
nFeatures = 2000
old_data_scClustViz_object <- "~/RatLiver/Results/old_samples/for_scClustViz_mergedOldSamples_mt40_lib1500_MTremoved.RData"
load(old_data_scClustViz_object)
ref_data = your_scRNAseq_data_object
DefaultAssay(ref_data) <- 'RNA'
ref_data <- FindVariableFeatures(ref_data, nfeatures=nFeatures)
HVGs <- VariableFeatures(ref_data)
ref_cluster_average_exp_df = ref_cluster_average_exp_df[rownames(ref_cluster_average_exp_df)%in%HVGs,]
########################################################################
######## merging the Healthy Rat ref data with the new samples ########
average_exp_merged = merge(cluster_average_exp_df, ref_cluster_average_exp_df, by.x='rat_ID', by.y='rat_ID')
dim(average_exp_merged) ## additional layers to ne to be added to the filter -> 10126 one-2-one orthos
head(average_exp_merged)
rat_cor_mat = cor(average_exp_merged[,-1],method = 'pearson')
colnames(rat_cor_mat)
colnames(rat_cor_mat) <- gsub('Inflammatory', 'Inf', colnames(rat_cor_mat))
rownames(rat_cor_mat) = colnames(rat_cor_mat)
number_of_clusters = length(cluster_names_types)
rat_cor_mat = rat_cor_mat[1:number_of_clusters,(number_of_clusters+1):ncol(rat_cor_mat)]
pheatmap(t(rat_cor_mat),color = inferno(20), clustering_method='ward.D2')
get_matched_label <- function(index, cor.mat, thr){
label = 'unknown'
a_clust_value = cor.mat[index,]
tmp_label = names(which.max(cor.mat[index,]))
if(a_clust_value[tmp_label]>thr) label = tmp_label
return(label)
}
annotation.df = data.frame(cluster=rownames(rat_cor_mat),
annotation=sapply(1:nrow(rat_cor_mat),
function(i) get_matched_label(index=i, rat_cor_mat, thr=0.3) ))
annotation.df$annotation_g = sub(" \\(.*", "", annotation.df$annotation)
dev.off()
gridExtra::grid.table(annotation.df[,-3])
dev.off()