-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHarmony_Integration_2.R
151 lines (132 loc) · 5.65 KB
/
Harmony_Integration_2.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
require("Seurat")
set.seed(3921)
# Which do we include in the integrated map?
dir <-
seurfiles <- c("C37_Anno_SeurObj.rds",
"C39_Anno_SeurObj.rds",
"C41_Anno_SeurObj.rds",
"C41-NPC_Anno_SeurObj.rds",
"C42_Anno_SeurObj.rds",
"C42-NPC_Anno_SeurObj.rds",
"C43_Anno_SeurObj.rds",
"C43-NPC_Anno_SeurObj.rds",
"C46_Anno_SeurObj.rds",
"C48_Anno_SeurObj.rds",
"C49_Anno_SeurObj.rds",
"C50_Anno_SeurObj.rds",
"C51_Anno_SeurObj.rds",
"C52_Anno_SeurObj.rds",
"C53_Anno_SeurObj.rds",
"C54_Anno_SeurObj.rds",
"C56_Anno_SeurObj.rds",
"C58_Anno_SeurObj.rds",
"C59_Anno_SeurObj.rds",
"C61_Anno_SeurObj.rds",
"C64_Anno_SeurObj.rds",
"C68_Anno_SeurObj.rds",
"C70_Anno_SeurObj.rds",
"C72_Anno_SeurObj.rds");
samp_names <- unlist(lapply(strsplit(seurfiles, "_"), function(x){x[[1]]}))
obj_list <- list()
all_genes <- c();
for (i in 1:length(seurfiles)) {
n <- samp_names[i];
obj <- readRDS(seurfiles[i]);
# fix some auto-annotation issues
[email protected]$consistent_labs <- as.character([email protected]$consistent_labs);
anno1 <- [email protected]$scmap_cluster_anno
anno2 <- [email protected]$scmap_cell_anno
mac_groups <- c("Non-inflammatoryMacrophages", "InflamatoryMacrophages")
g_anno <- as.character([email protected]$general_labs)
g_anno[g_anno == "ambiguous" & anno1 %in% mac_groups & anno2 %in% mac_groups] <- "Macrophage"
[email protected]$general_labs <- g_anno
null_vals <- is.na([email protected]$consistent_labs);
[email protected]$consistent_labs[null_vals] <- g_anno[null_vals]
#Fix sample ID, and Donor ID
[email protected]$sample <- [email protected]$orig.ident
[email protected]$donor <- sapply(strsplit(as.character([email protected]$sample), "_"), function(x){x[[1]]})
# save sample specific clusters
[email protected]$sample_specific_clusters <- [email protected]$seurat_clusters
# get rid of factors
metadata_classes <- sapply(1:ncol([email protected]), function(i){class([email protected][,i])})
for (j in which(metadata_classes == "factor")) {
[email protected][,j] <- as.character([email protected][,j]);
}
obj <- Seurat::NormalizeData(obj, verbose = FALSE, method="LogNormalize", scale.factor=10000)
obj <- Seurat::ScaleData(obj);
[email protected]$cell_barcode <- colnames(obj);
if (length(all_genes) == 0) {
all_genes <- rownames(obj);
} else {
all_genes <- union(all_genes, rownames(obj));
}
[email protected][[7]] <- [email protected][[7]][[1]]
[email protected]$donor <- rep(n, ncol(obj));
obj_list[[n]] <- obj
}
all_genes <- sort(all_genes);
#all_genes <- all_genes[!grepl("^MT-", all_genes)]
#global_count_mat <- NULL
#global_meta_data <- NULL
#for (i in 1:length(obj_list)) {
# obj_list[[i]] <- obj_list[[i]][match(all_genes, rownames(obj_list[[i]])),]
#}
# Merge Datasets
#### Merging does not merge individually scaled datasets!!
merged_obj <- NULL;
universal_genes <- c(-1)
all_genes <- c();
for (i in 1:length(obj_list)) {
n <- samp_names[i];
if (i == 1) {
merged_obj <- obj_list[[i]]
universal_genes <- as.character(rownames(obj_list[[i]]))
} else {
merged_obj <- merge(merged_obj, y=obj_list[[i]], add.cell.ids=c("", n), project="LiverMap")
universal_genes <- intersect(universal_genes, as.character(rownames(obj_list[[i]])))
}
}
merged_obj@misc$universal_genes <- universal_genes;
merged_obj@misc$creation_date <- date();
saveRDS(merged_obj, "All_genes_Merged_obj_v2.rds")
set.seed(9428)
merged_obj <- merged_obj[rownames(merged_obj) %in% universal_genes,]
#merged_obj <- Seurat::NormalizeData(merged_obj, verbose = FALSE)
merged_obj <- FindVariableFeatures(merged_obj, selection.method = "vst", nfeatures = 2000)
merged_obj <- ScaleData(merged_obj, verbose = FALSE)
#merged_obj <- RunPCA(merged_obj, pc.genes = VariableGenes(merged_obj), npcs = 20, verbose = FALSE)
merged_obj <- RunPCA(merged_obj, pc.genes = VariableFeatures(merged_obj), npcs = 20, verbose = FALSE)
merged_obj <- RunTSNE(merged_obj, dims = 1:10, verbose = FALSE)
merged_obj <- RunUMAP(merged_obj, dims = 1:10, verbose = FALSE)
png("merged_not_integrated_tsne.png", width=9, height =6, units="in", res=300)
DimPlot(merged_obj, reduction="tsne", group.by="donor", pt.size=0.1)
dev.off();
png("merged_not_integrated_umap.png", width=9, height =6, units="in", res=300)
DimPlot(merged_obj, reduction="umap", group.by="donor", pt.size=0.1)
dev.off();
png("merged_not_integrated_tsne_autoanno.png", width=12, height =6, units="in", res=300)
DimPlot(merged_obj, reduction="tsne", group.by="consistent_labs", pt.size=0.1)
dev.off();
png("merged_not_integrated_umap_autoanno.png", width=12, height =6, units="in", res=300)
DimPlot(merged_obj, reduction="umap", group.by="consistent_labs", pt.size=0.1)
dev.off();
require("harmony")
set.seed(10131)
merged_obj <- RunHarmony(merged_obj, "donor", plot_convergence = TRUE)
#DimPlot(object = merged_obj, reduction = "harmony", pt.size = .1, group.by = "donor")
merged_obj <- RunUMAP(merged_obj, reduction = "harmony", dims = 1:20)
merged_obj <- RunTSNE(merged_obj, reduction = "harmony", dims = 1:20)
png("merged_harmony_integrated_umap.png", width=9, height =6, units="in", res=100)
DimPlot(merged_obj, reduction = "umap", group.by = "donor", pt.size = .1)
dev.off();
png("merged_harmony_integrated_tsne.png", width=9, height =6, units="in", res=100)
DimPlot(merged_obj, reduction = "tsne", group.by = "donor", pt.size = .1)
dev.off();
png("merged_harmony_integrated_umap_autoanno.png", width=12, height =6, units="in", res=100)
DimPlot(merged_obj, reduction = "umap", group.by = "consistent_labs", pt.size = .1)
dev.off();
png("merged_harmony_integrated_tsne_autoanno.png", width=12, height =6, units="in", res=100)
DimPlot(merged_obj, reduction = "tsne", group.by = "consistent_labs", pt.size = .1)
dev.off();
saveRDS(merged_obj, "All_merged_universal_genes_harmony_integrated_v2.rds");
# add harmony dimensions to integrated object?