-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGeneral_Subcluster_harmony.R
145 lines (101 loc) · 5.92 KB
/
General_Subcluster_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
140
141
142
143
144
require("Seurat")
source("My_R_Scripts.R")
Set=4;
params <- read.delim("Table_SubClustering.csv", sep=",", header=T, stringsAsFactors=FALSE);
proj_name <- params[Set,"Celltype"]
clusters <- unlist(strsplit(params[Set,"orig.clusters"], "-"))
liver.integrated <- readRDS("integration_harmony_plus_analysis.rds")
cluster_labs <- [email protected]$Fine_clusters
names(cluster_labs) <- paste([email protected]$donor, [email protected]$cell_barcode, sep="_")
harmony_redim <- liver.integrated@reductions$harmony
meta.data <- [email protected]
meta.data <- meta.data[,!grepl("^knn", colnames(meta.data))]
rm(liver.integrated)
liver.integrated <- readRDS("all_gene_merged_obj.rds")
[email protected] <- [email protected][match(colnames(liver.integrated), rownames([email protected])),]
liver.integrated@reductions$harmony <- harmony_redim;
meta.data <- meta.data[match(colnames(liver.integrated), rownames(meta.data)),]
[email protected] <- meta.data
liver.integrated <- liver.integrated[,[email protected]$Fine_clusters %in% clusters];
# require all genes to be detected in at least one cell in a majority of donors
gene_filter <- group_rowmeans(liver.integrated@assays$RNA@counts, [email protected]$donor);
gene_filter <- apply(gene_filter, 1, median)
gene_filter <- gene_filter > 0;
liver.integrated <- liver.integrated[gene_filter,]
set.seed(as.numeric(params[Set,"seed1"]))
# Dimensionality Reduction
### Probably need to re-run harmony on just these cell for best results....####
liver.integrated <- NormalizeData(liver.integrated)
liver.integrated <- ScaleData(liver.integrated);
liver.integrated <- FindVariableFeatures(liver.integrated, selection.method = "vst", nfeatures = 2500)
liver.integrated <- RunPCA(liver.integrated, features = VariableFeatures(object = liver.integrated))
ElbowPlot(liver.integrated)
require("harmony")
set.seed(10131)
liver.integrated <- RunHarmony(liver.integrated, "donor", plot_convergence = TRUE)
npcs <- as.numeric(params[Set,"nPCs"])
# Cluster with many different parameters
res <- seq(from=0.1, to=1.5, by=0.2)
nkNN <- seq(from=30, to=60, 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)
name <- paste("knn_",nkNN_param,"_res_", res_param, sep="");
[email protected][[name]] <- [email protected]$seurat_clusters;
}}
# Compare all these clusterings
require(igraph)
require(gplots)
clust_table <- [email protected][, grepl("^knn_", names([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 <- params[Set,"Coarse_res"] # <- Update
fine_lvl <- params[Set,"Fine_res"] # <- Update
use_lvl <- params[Set,"Use_res"]
#manually select which exemplar to use
[email protected]$Coarse_clusters <- [email protected][[coarse_lvl]]
[email protected]$Fine_clusters <- [email protected][[fine_lvl]]
[email protected]$Use_clusters <- [email protected][[use_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 <- as.numeric(params[Set,"nkNN"])
set.seed(as.numeric(params[Set,"seed2"]))
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, "_umap.png", sep="_"), width=6, height=6, units="in", res=50)
DimPlot(liver.integrated, reduction = "umap", group.by="Use_clusters")
dev.off()
png(paste(proj_name, "_tsne.png", sep="_"), width=6, height=6, units="in", res=50)
DimPlot(liver.integrated, reduction = "tsne", group.by="Use_clusters")
dev.off()
png(paste(proj_name, "_umap_donor.png", sep="_"), width=6, height=6, units="in", res=50)
DimPlot(liver.integrated, reduction = "umap", group.by="orig.ident")
dev.off()
DimPlot(liver.integrated, reduction = "tsne", group.by="orig.ident")
barplot(table([email protected]$orig.ident, [email protected]$Coarse_clusters), col=rainbow(20))
tab <- table([email protected]$orig.ident, [email protected]$Coarse_clusters)
DimPlot(liver.integrated, reduction = "umap", group.by="scmap_anno")
DimPlot(liver.integrated, reduction = "tsne", group.by="scmap_anno")
png(paste(proj_name, "_umap_autoanno.png", sep="_"), width=7.5, height=6, units="in", res=50)
DimPlot(liver.integrated, reduction = "umap", group.by="scmap_anno2")
dev.off()
DimPlot(liver.integrated, reduction = "tsne", group.by="scmap_anno2")
saveRDS(liver.integrated, paste(proj_name, "_analysis.rds", sep=""))