-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOptimize_Clustering_Params_harmony.R
140 lines (111 loc) · 6.54 KB
/
Optimize_Clustering_Params_harmony.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
require("Seurat")
proj_name="harmony_integrated2_20livers"
#liver.integrated <- readRDS("harmony_integrated.rds")
#liver.integrated <- readRDS("integration_v2.rds")
liver.integrated <- readRDS("All_merged_universal_genes_harmony_integrated_v2.rds")
set.seed(7742)
# Dimensionality Reduction
#require("sctransform")
#liver.integrated <- ScaleData(liver.integrated);
#liver.integrated <- RunPCA(liver.integrated, features = VariableFeatures(object = liver.integrated))
npcs <- 15 # was 20 - 29/04/2020
require("ggplot2")
png(paste(proj_name,"pca_elbow.png", sep="_"), width=4, height=4, units="in", res=100)
a <- ElbowPlot(liver.integrated)+geom_vline(aes(xintercept=npcs), linetype="dotted", color="grey35", size=2)
plot(a)
dev.off()
# Cluster with many different parameters
res <- seq(from=0.3, to=2, by=0.3)
nkNN <- seq(from=40, to=80, by=10)
for(res_param in res) {
for(nkNN_param in nkNN){
liver.integrated <- FindNeighbors(liver.integrated, reduction="harmony", dims = 1:npcs, k.param=nkNN_param)
#liver.integrated <- FindClusters(liver.integrated, reduction="harmony", resolution = res_param, k.param=nkNN_param)
liver.integrated <- FindClusters(liver.integrated, reduction="harmony", resolution = res_param, group.singletons=FALSE, method="igraph") # for UHN Seurat installation
name <- paste("knn_",nkNN_param,"_res_", res_param, sep="");
[email protected][[name]] <- [email protected]$seurat_clusters;
}}
saveRDS(liver.integrated, paste(proj_name, "harmony_plus_clusters.rds", sep="_"))
# Compare all these clusterings
require(igraph)
require(gplots)
clust_table <- [email protected][, grepl("^knn_", colnames([email protected]))]
clust_table <- as.matrix(apply(clust_table,2,as.numeric))
require("proxy")
clust_dists <- proxy::dist(clust_table, method=function(x,y){igraph::compare(x,y,method="vi")}, by_rows=FALSE)
#clust_similr1 <- proxy::simil(clust_table, method=function(x,y){igraph::compare(x,y,method="nmi")}, by_rows=FALSE)
#clust_similr2 <- proxy::simil(clust_table, method=function(x,y){igraph::compare(x,y,method="adjusted.rand")}, by_rows=FALSE)
# Find robust exemplar clustering(s)
require("apcluster")
require("gplots")
set.seed(18371)
res1 <- apcluster(-1*as.matrix(clust_dists), p=-2.5)
#res2 <- apcluster(as.matrix(clust_similr1), p=-2)
#res3 <- apcluster(as.matrix(clust_similr1), p=-2)
#valid_clusterings <- res1@exemplars[which(res1@exemplars %in% res2@exemplars & res1@exemplars %in% res3@exemplars)]
coarse_lvl <- names(res1@exemplars)[2]
fine_lvl <- names(res1@exemplars)[3]
#coarse_lvl <- "knn_70_res_0.3" # basic_integration_analysis.rds = knn_70_res_0.3
#fine_lvl <- "knn_70_res_0.9" # basic_integration_analysis.rds = knn_90_res_1.5
#manually select which exemplar to use
[email protected]$Coarse_clusters <- [email protected][[coarse_lvl]]
[email protected]$Fine_clusters <- [email protected][[fine_lvl]]
apcluster::heatmap(res1, -1*as.matrix(clust_dists))
png(paste(proj_name,"compare_clusterings_heatmap.png",sep="_"), width=6, height=6, units="in", res=300)
lab <- matrix("", ncol=ncol(clust_table), nrow=ncol(clust_table))
lab[colnames(clust_table)==fine_lvl, colnames(clust_table)==fine_lvl] <- "F"
lab[colnames(clust_table)==coarse_lvl, colnames(clust_table)==coarse_lvl] <- "C"
heatmap.2(as.matrix(clust_dists), trace="none", distfun=function(x){return(as.dist(clust_dists))}, cellnote=lab)
dev.off()
# Visualize the Chosen clusterings
#nkNN <- 70
#liver.integrated <- RunTSNE(liver.integrated, reduction="harmony", dims = 1:npcs)
#liver.integrated <- RunUMAP(liver.integrated, reduction="harmony", dims = 1:npcs, parallel=FALSE, n.neighbour=nkNN)
png(paste(proj_name,"coarse_umap.png", sep="_"), width=6, height=6, units="in", res=150)
DimPlot(liver.integrated, reduction = "umap", group.by="Coarse_clusters")
dev.off()
png(paste(proj_name,"coarse_tsne.png",sep="_"), width=6, height=6, units="in", res=150)
DimPlot(liver.integrated, reduction = "tsne", group.by="Coarse_clusters")
dev.off()
png(paste(proj_name,"fine_umap.png", sep="_"), width=6, height=6, units="in", res=150)
DimPlot(liver.integrated, reduction = "umap", group.by="Fine_clusters")
dev.off()
png(paste(proj_name,"fine_tsne.png", sep="_"), width=6, height=6, units="in", res=150)
DimPlot(liver.integrated, reduction = "tsne", group.by="Fine_clusters")
dev.off()
saveRDS(liver.integrated, paste(proj_name, "harmony_plus_analysis.rds", sep="_"))
#png("Coarse_harmony_clusters_by_donor.png", width=6, height=6, units="in", res=150)
#barplot(table([email protected]$orig.ident, [email protected]$Coarse_clusters), col=rainbow(20))
#dev.off()
#png("Fine_harmony_clusters_by_donor.png", width=6, height=6, units="in", res=150)
#barplot(table([email protected]$orig.ident, [email protected]$Fine_clusters), col=rainbow(20))
#dev.off()
# Auto-annotation
#source("Setup_autoannotation.R")
#all_anno <- readRDS("All20_automatedannotation.rds");
#[email protected]$scmap_anno <- rep("unknown", ncol(liver.integrated));
#[email protected]$scmap_anno2 <- rep("unknown", ncol(liver.integrated));
#for (donor in unique([email protected]$orig.ident)) {
# cell_ids <- [email protected]$cell_barcode[[email protected]$donor == donor]
# anno <- all_anno[[donor]];
# anno <- anno[anno$cell_barcode %in% cell_ids,]
# anno <- anno[match(cell_ids, anno$cell_barcode),]
# [email protected]$scmap_anno[[email protected]$orig.ident == donor] <- as.character(anno$scmap_cluster_anno$lm1)
# [email protected]$scmap_anno2[[email protected]$orig.ident == donor] <- as.character(anno$scmap_cell_anno)
#}
#png("AutoAnno_harmony_integrated_umap.png", width=8, height=6, units="in", res=150)
#DimPlot(liver.integrated, reduction = "umap", group.by="scmap_anno")
#dev.off()
#png("AutoAnno_harmony_integrated_tsne.png", width=8, height=6, units="in", res=150)
#DimPlot(liver.integrated, reduction = "tsne", group.by="scmap_anno")
#dev.off()
#png("AutoAnno2_harmony_integrated_umap.png", width=8, height=6, units="in", res=150)
#DimPlot(liver.integrated, reduction = "umap", group.by="scmap_anno2")
#dev.off()
#png("AutoAnno2_harmony_integrated_tsne.png", width=8, height=6, units="in", res=150)
#DimPlot(liver.integrated, reduction = "tsne", group.by="scmap_anno2")
#dev.off()
#saveRDS(liver.integrated, "integration_harmony_plus_analysis.rds")
# Would this be helpful?
# create a heatmap where: cell = average similarity of this clustering to all other clusterings
# distance between clusterings is measured using igraph::compare(c1, c2, method="vi")