-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMap2.2_5pr_vs_3pr_Harmony_Integration.R
187 lines (156 loc) · 7.45 KB
/
Map2.2_5pr_vs_3pr_Harmony_Integration.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
require("Seurat")
source("~/scripts/LiverMap2.0/Colour_Scheme.R")
set.seed(3921)
# Which do we include in the integrated map?
dir <- "/cluster/projects/macparland/TA/LiverMap2.0/Cleaned"
seurfiles <- c("C58_5pr_SoupX.rds",
"C58_RESEQ_SoupX.rds",
"C59_5pr_SoupX.rds",
"C59_SoupX.rds",
"C61_5pr_SoupX.rds",
"C61_RESEQ_SoupX.rds",
"C63_5pr_reseq_SoupX.rds",
"C63_reseq_SoupX.rds",
"C64_5pr_SoupX.rds",
"C64_RESEQ_SoupX.rds",
"C70_5pr_reseq_SoupX.rds",
"C70_RESEQ_SoupX.rds"
);
samp_names <- unlist(lapply(strsplit(seurfiles, "_"), function(x){x <- x[c(-length(x))]; return(paste(x, collapse="_"))}))
obj_list <- list()
for (i in 1:length(seurfiles)) {
n <- samp_names[i];
obj <- readRDS(paste(dir,seurfiles[i], sep="/"));
#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 <- paste(n, [email protected]$seurat_clusters, sep="_")
# 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, normalization.method="LogNormalize", scale.factor=1500)
[email protected]$cell_barcode <- colnames(obj);
[email protected][[7]] <- [email protected][[7]][[1]]
[email protected]$sample <- rep(n, ncol(obj));
[email protected]$cell_ID <- paste([email protected]$sample, [email protected]$cell_barcode, sep="_")
obj_list[[n]] <- obj
}
# Merge Datasets
#### Merging does not merge individually scaled datasets!!
# Find common HVGs and detected genes
merged_obj <- NULL;
universal_genes <- c(-1)
hvgs <- 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]]))
hvgs <- VariableFeatures(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]])))
hvgs <- c(hvgs, VariableFeatures(obj_list[[i]]));
}
}
fix_names <- paste([email protected]$orig.ident, [email protected]$cell_barcode, sep="_")
merged_obj <- RenameCells(merged_obj, new.names=fix_names)
# Keep HVGs seen in at least 2 datasets
hvgs <- unique(hvgs[duplicated(hvgs)])
hvgs <- hvgs[!grepl("^MT-", hvgs)]
hvgs <- hvgs[ hvgs %in% universal_genes]
# Scale within datasets
all_Scaled <- c();
scaled_cell_ids <- c()
for (i in 1:length(obj_list)) {
n <- samp_names[i];
obj <- obj_list[[i]]
obj <- Seurat::ScaleData(obj, features=hvgs);
scaled <- obj@[email protected];
scaled_cell_ids <- c(scaled_cell_ids, obj_list[[i]]@meta.data$cell_ID);
if (i == 1) {
all_Scaled <- scaled;
} else {
scaled <- scaled[match(rownames(all_Scaled), rownames(scaled)),]
all_Scaled <- cbind(all_Scaled, scaled);
}
}
colnames(all_Scaled) <- scaled_cell_ids;
merged_obj@[email protected] <- all_Scaled
merged_obj@misc$universal_genes <- universal_genes;
merged_obj@misc$repeated_hvgs <- hvgs;
merged_obj@misc$creation_date <- date();
VariableFeatures(merged_obj) <- hvgs;
[email protected]$seurat_clusters <- paste([email protected]$orig.ident,
as.character([email protected]$seurat_clusters), sep="_")
[email protected]$assay_type <- rep("3pr", ncol(merged_obj))
[email protected]$assay_type[grepl("5pr", [email protected]$sample)] <- "5pr"
saveRDS(merged_obj, "Merged_obj_SoupX_5pr_3pr.rds")
set.seed(9428)
merged_obj <- merged_obj[rownames(merged_obj) %in% universal_genes,]
merged_obj@[email protected] <- all_Scaled
merged_obj <- RunPCA(merged_obj, pc.genes = hvgs,
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("SN_SC_scaled_merged_not_integrated_tsne.png", width=9, height =6, units="in", res=300)
#DimPlot(merged_obj, reduction="tsne", group.by="sample", pt.size=0.1)
#dev.off();
png("SC_5pr_3pr_SoupX_merged_only_assay_umap.png", width=9, height =6, units="in", res=300)
DimPlot(merged_obj, reduction="umap", group.by="assay_type", pt.size=0.1)
dev.off();
png("SC_5pr_3pr_SoupX_merged_only_sample_umap.png", width=9, height =6, units="in", res=300)
DimPlot(merged_obj, reduction="umap", group.by="sample", pt.size=0.1)
dev.off();
#png("SN_SC_scaled_merged_not_integrated_tsne_autoanno.png", width=12, height =6, units="in", res=300)
#Type_DimPlot(merged_obj, reduction="tsne", type_col="marker_labs", cluster_col="marker_labs")
#dev.off();
png("SC_5pr_3pr_SoupX_merged_only_autoanno_umap.png", width=12, height =6, units="in", res=300)
Type_DimPlot(merged_obj, reduction="umap", type_col="marker_labs", cluster_col="marker_labs")
dev.off();
#rescale across datasets
obj <- Seurat::ScaleData(merged_obj, features=hvgs);
obj <- RunPCA(obj, pc.genes = hvgs,
npcs = 20, verbose = FALSE)
#obj <- RunTSNE(obj, dims = 1:10, verbose = FALSE)
obj <- RunUMAP(obj, dims = 1:10, verbose = FALSE)
#png("SN_SC_rescaled_merged_not_integrated_tsne.png", width=9, height =6, units="in", res=300)
#DimPlot(obj, reduction="tsne", group.by="sample", pt.size=0.1)
#dev.off();
png("SC_5pr_vs_3pr_rescaled_merged_sample_umap.png", width=9, height =6, units="in", res=300)
DimPlot(obj, reduction="umap", group.by="sample", pt.size=0.1)
dev.off();
png("SC_5pr_vs_3pr_rescaled_merged_assay_umap.png", width=9, height =6, units="in", res=300)
DimPlot(obj, reduction="umap", group.by="assay_type", pt.size=0.1)
dev.off();
#png("SN_SC_rescaled_merged_not_integrated_tsne_autoanno.png", width=12, height =6, units="in", res=300)
#Type_DimPlot(obj, reduction="tsne", type_col="marker_labs", cluster_col="marker_labs")
#dev.off();
png("SC_5pr_vs_3pr_rescaled_merged_only_autoanno_umap.png", width=12, height =6, units="in", res=300)
Type_DimPlot(obj, reduction="umap", type_col="marker_labs", cluster_col="marker_labs")
dev.off();
require("harmony")
set.seed(10131)
merged_obj <- RunHarmony(merged_obj, c("sample", "assay_type", "donor"), plot_convergence = TRUE)
merged_obj <- RunUMAP(merged_obj, reduction = "harmony", dims = 1:20)
#merged_obj <- RunTSNE(merged_obj, reduction = "harmony", dims = 1:20)
png("SN_5pr_vs_3pr_harmony_integrated_sample_umap.png", width=9, height =6, units="in", res=100)
DimPlot(merged_obj, reduction = "umap", group.by = "sample", pt.size = .1)
dev.off();
png("SN_5pr_vs_3pr_harmony_integrated_assay_umap.png", width=9, height =6, units="in", res=100)
DimPlot(merged_obj, reduction = "umap", group.by = "assay_type", pt.size = .1)
dev.off();
#png("SN_SC_merged_harmony_integrated_tsne.png", width=9, height =6, units="in", res=100)
#DimPlot(merged_obj, reduction = "tsne", group.by = "sample", pt.size = .1)
#dev.off();
png("SC_5pr_vs_3pr_harmony_integrated_autoanno_umap.png", width=12, height =6, units="in", res=100)
Type_DimPlot(merged_obj, reduction="umap", type_col="marker_labs", cluster_col="marker_labs")
dev.off();
#png("SN_SC_merged_harmony_integrated_tsne_autoanno.png", width=12, height =6, units="in", res=100)
#Type_DimPlot(merged_obj, reduction="tsne", type_col="marker_labs", cluster_col="marker_labs")
#dev.off();
saveRDS(merged_obj, "Harmony_integrated_5pr_vs_3pr.rds");
# add harmony dimensions to integrated object?