-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDCM_Reference_Integration.R
244 lines (214 loc) · 11.3 KB
/
DCM_Reference_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
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
library(Seurat)
library(dplyr)
library(ggplot2)
library(sctransform)
library(patchwork)
library(SeuratDisk)
## Gathering And Preparing Data
load("./DCM_Cells_Filtered.Robj") #Filtered cell object used as query (This object is the raw untransformed/unclustered object filtered to include only cells in the final clustered cell object)
load("./DCM_Nuclei.Robj") #Final clustered nuclei object used as reference
HDCM[["percent.mt"]] <- PercentageFeatureSet(HDCM, pattern = "^MT-")
HDCM <- SCTransform(HDCM, verbose = FALSE, conserve.memory=TRUE, vars.to.regress = "percent.mt")
##Find Anchors
anchors <- FindTransferAnchors(
reference = nuclei,
query = HDCM,
normalization.method = "SCT",
reference.reduction = "pca",
dims = 1:80, recompute.residuals = FALSE
)
save(anchors,file="reference_anchors.Robj")
##Mapping
HDCM <- MapQuery(
anchorset = anchors,
query = HDCM,
reference = nuclei,
refdata = list(
celltype.l1 = "seurat_clusters",
celltype.l2 = "names"
),
reference.reduction = "pca",
reduction.model = "umap"
)
save(HDCM,file="HDCM_RefMapped.Robj")
##Pre-Merge Plots
png("RefUMAP_clusters.png",
width = 10*100, height = 8*100,
res=100)
DimPlot(object = HDCM, reduction = 'ref.umap',pt.size=0.1, group.by="predicted.celltype.l1")
dev.off()
png("RefUMAP_names.png",
width = 10*100, height = 8*100,
res=100)
DimPlot(object = HDCM, reduction = 'ref.umap',pt.size=0.1, group.by="predicted.celltype.l2")
dev.off()
png("Myeloid_PredictionScore.png",
width = 20*100, height = 8*100,
res=100)
FeaturePlot(object = HDCM, features = c("Macrophages","Monocytes"),pt.size=0.1,reduction = "ref.umap",
cols = c("lightgrey", "darkred"), ncol = 3) & theme(plot.title = element_text(size = 10))
dev.off()
## Merging
nuclei$id <- 'reference'
HDCM$id <- 'query'
RefMerge <- merge(nuclei, HDCM)
RefMerge[["pca"]] <- merge(nuclei[["pca"]], HDCM[["ref.pca"]])
RefMerge <- RunUMAP(RefMerge, reduction = 'pca', dims = 1:80)
png("Merged_UMAP_IDs.png",
width = 10*100, height = 8*100,
res=100)
DimPlot(RefMerge, group.by = 'id',raster=FALSE)
dev.off()
save(RefMerge,file="RefMerged.Robj")
## Merged Plots
png("Merged_UMAP_IDs_Split.png",
width = 20*100, height = 8*100,
res=100)
DimPlot(RefMerge, group.by = 'id', split.by = "id",raster = FALSE)
dev.off()
png("Merge_RefUMAP_0.6_Nuclei.png",
width = 10*100, height = 8*100,
res=100)
DimPlot(object = RefMerge, reduction = 'umap',pt.size=0.1, group.by="SCT_snn_res.0.6",cells = WhichCells(RefMerge,idents="reference"))
dev.off()
png("Merge_RefUMAP_0.6_Cells.png",
width = 10*100, height = 8*100,
res=100)
DimPlot(object = RefMerge, reduction = 'umap',pt.size=0.1, group.by="predicted.celltype.l1",cells = WhichCells(RefMerge,idents="query"))
dev.off()
##Re-clustering
RefMerge <- FindNeighbors(object = RefMerge, dims = 1:80)
RefMerge <- FindClusters(object = RefMerge, reduction.type = "pca",
dims = 1:80,
resolution = c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0),
print.output = 0, save.SNN = TRUE, force.recalc = TRUE)
Idents(RefMerge)<-RefMerge$SCT_snn_res.0.6
##Re-clustered plots
png("UMAP_0.6.png",
width = 10*100, height = 8*100,
res=100)
DimPlot(object = RefMerge, reduction = 'umap',pt.size=0.1)
dev.off()
png("UMAP_0.6_SplitTech.png",
width = 20*100, height = 8*100,
res=100)
DimPlot(object = RefMerge, reduction = 'umap',pt.size=0.1,split.by="id")
dev.off()
save(RefMerge,file="RefMerged_Reclustered.Robj")
## Add Condition Meta
RefMerge$Condition<-ifelse([email protected]$orig.ident %in% c("HDCM1","HDCM3","HDCM4","HDCM6","HDCM8","H_ZC-LVAD",
"TWCM-11-93",
"TWCM-13-181",
"TWCM-13-201",
"TWCM-13-208",
"TWCM-LVAD2",
"TWCM-LVAD3",
"TWCM-11-3",
"TWCM-13-285",
"TWCM-13-280",
"TWCM-13-47",
"TWCM-13-102",
"TWCM-13-84",
"TWCM-13-17"), "DCM","Donor")
##Adding Age Tertile Meta
RefMerge$Age_Group_Tertile<- ifelse(RefMerge$orig.ident %in% c('TWCM-13-181',
'TWCM-11-93',
'TWCM-13-198',
'TWCM-13-235',
'TWCM-13-96',
'TWCM-13-192',
'TWCM-11-78',
'TWCM-13-285',
'TWCM-13-152',
'TWCM-11-256',
'TWCM-13-47',
'TWCM-13-102',
'TWCM-11-82',
"HDCM3",
"HDCM8"), "Young",ifelse(RefMerge$orig.ident %in% c('TWCM-11-103',
'TWCM-13-80',
'TWCM-11-42',
'TWCM-13-104',
'TWCM-13-132',
'TWCM-13-208',
'TWCM-10-5',
'TWCM-13-84',
'H_ZC-LVAD',
'TWCM-11-41',
'TWCM-13-17',
'TWCM-13-101',
'TWCM-11-3',
'TWCM-13-1',
'TWCM-LVAD3',
'HDCM1',
'HDCM6'),"Middle","Old"))
##Adding Sex Meta
RefMerge$Sex<- ifelse(RefMerge$orig.ident %in% c('H_ZC-11-292',
'H_ZC-LVAD',
'TWCM-11-41',
'TWCM-11-74',
'TWCM-11-78',
'TWCM-11-82',
'TWCM-11-93',
'TWCM-11-103',
'TWCM-11-192',
'TWCM-13-47',
'TWCM-13-80',
'TWCM-13-84',
'TWCM-13-102',
'TWCM-13-152',
'TWCM-13-168',
'TWCM-13-192',
'TWCM-13-198',
'TWCM-13-208',
'TWCM-LVAD3',
'HDCM1',
'HDCM6',
'HDCM7',
'HDCM4'), "Male", "Female")
##Adding Additional Meta
RefMerge$tech<-ifelse(RefMerge$id =="query","SC","SN")
RefMerge$CondTech<-paste(RefMerge$condition,RefMerge$tech, sep="_")
##Find Marker Genes
markers <- FindAllMarkers(object = RefMerge,
only.pos = TRUE,
min.pct = 0.10)
write.table(markers, "markers_0.6.tsv", sep="\t", quote=F, row.names=F)
## Adding Cell Names Meta
current.cluster.ids<-levels(RefMerge)
new.cluster.ids<-c('Cardiomyocytes',
'Fibroblasts',
'NK/T-Cells',
'Macrophages',
'Smooth_Muscle',
'Fibroblasts',
'Fibroblasts',
'Fibroblasts',
'Fibroblasts',
'Cardiomyocytes',
'Lymphatic',
'Cardiomyocytes',
'Macrophages',
'Neurons',
'Fibroblasts',
'Cardiomyocytes',
'Endothelium',
'Epicardium',
'Mast',
'Adipocytes',
"Macrophages",
"Macrophages",
"Fibroblasts",
"Endothelium",
"B-Cells",
"Cardiomyocytes",
"Pericytes",
"Pericytes",
"Fibroblasts",
"Endocardium",
"Endothelium",
"Endothelium",
"Pericytes")
Idents(RefMerge) <- plyr::mapvalues(x = RefMerge$SCT_snn_res.0.6, from = current.cluster.ids, to = new.cluster.ids)
RefMerge$Names<-Idents(RefMerge)
Save(RefMerge,file="RefMerged_Reclustered_AddlMeta.Robj")