-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegrationDemo.Rmd
279 lines (189 loc) · 8.57 KB
/
IntegrationDemo.Rmd
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
---
title: "Single Cell Data Integration Demo"
author: "Maximilian Lombardo"
date: "8/15/2020"
output: html_document
params:
raw.data.parent.dir: "~/locationOnYourMachine/"
sample.size: 1000
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Load Necessary libraries
```{r}
library(Seurat)
```
Load our Seurat objects corresponding to different datasets
```{r}
objOne <- readRDS("~/Documents/ObjectOnelocation.rds")
objTwo <- readRDS("~/Documents/ObjectTwolocation.rds")
objThree <- readRDS("~/Documents/ObjectThreelocation.rds")
#Add dataset as metadata
objOne[["dataset"]] <- rep("objOne", length(Cells(objOne)))
objTwo[["dataset"]] <- rep("objTwo", length(Cells(objTwo)))
objThree[["dataset"]] <- rep("objThree", length(Cells(objThree)))
```
Merge datasets and perform a standard clustering of the 3 different datasets
```{r}
obj <- merge(objOne, c(objTwo, objThree))
#Use sctransform in place of Normalize, Find Var features, and Scale
# run sctransform
obj <- SCTransform(obj,
vars.to.regress = "percent.mito",
verbose = FALSE)
# These are now standard steps in the Seurat workflow for visualization and clustering
obj <- RunPCA(obj, verbose = FALSE)
ndims <- 1:25
obj <- RunUMAP(obj, dims = ndims, verbose = FALSE)
obj <- RunTSNE(obj, dims = ndims,
perplexity = 30, max_iter = 2000,
theta = 0)
saveRDS(obj, "~/Documents/allNoIntegration.rds")
```
SCTransform based Integration
Set globals option
```{r}
options(future.globals.maxSize = 4000 * 1024^2)
```
First, setup the Seurat object list, and run SCTransform on each object separately:
```{r}
#Make a list of Seurat objects
obj.list <- list(objOne, objTwo, objThree)
#Perform SCTransform on each object individually
for (i in 1:length(obj.list)) {
obj.list[[i]] <- SCTransform(obj.list[[i]], verbose = FALSE)
}
```
Next, select features for downstream integration, and run PrepSCTIntegration, which ensures
that all necessary Pearson residuals have been calculated.
```{r}
obj.features <- SelectIntegrationFeatures(object.list = obj.list,
nfeatures = 3000)
obj.list <- PrepSCTIntegration(object.list = obj.list,
anchor.features = obj.features,
verbose = FALSE)
```
Next, identify anchors and integrate the datasets. Commands are identical to the standard workflow, but make sure to set normalization.method = 'SCT':
```{r}
obj.anchors <- FindIntegrationAnchors(object.list = obj.list,
normalization.method = "SCT",
anchor.features = obj.features,
verbose = FALSE)
obj.integrated <- IntegrateData(anchorset = obj.anchors,
normalization.method = "SCT",
verbose = FALSE)
```
Now proceed with downstream analysis (i.e. visualization, clustering) on the integrated dataset. Commands are identical to the standard workflow, but do not run the ScaleData function after integration. You can see that after integration, cells group by their biological cell type (which has been pre-annotated), instead of by their underlying technology.
```{r}
obj.integrated <- RunPCA(obj.integrated, verbose = FALSE)
ndims <- 1:20
obj.integrated <- RunUMAP(obj.integrated, dims = ndims)
obj.integrated <- RunTSNE(obj.integrated, dims = ndims)
```
```{r}
DefaultAssay(obj.integrated) <- 'integrated'
obj.integrated <- FindNeighbors(obj.integrated, dims = ndims, k.param = 10)
obj.integrated <- FindClusters(obj.integrated, resolution = 1.0)
DefaultAssay(obj.integrated) <- 'SCT'
saveRDS(obj.integrated, "~/Documents/objSCTransform.rds")
```
Plot Cluster Composition
```{r}
library(ggplot2)
ggplot([email protected], aes(x=seurat_clusters, fill=dataset)) + geom_bar(position = 'fill') + ggtitle(label = 'Cluster Composition')
```
Clusters 10 and 12 seem to be solely composed of cells from the objOne dataset
Let's see if we can find markers which help us confirm that these cells make up a distinct cell type.
```{r}
cluster.markers <- FindMarkers(obj.integrated, ident.1 = c(10, 12))
```
Find Correlated Features
```{r}
library(ggplot2)
findCorrelatedFeatures <- function(gene = '', obj){
exp.mat <- as.matrix(obj[['SCT']]@data)
gene.exp <- as.numeric(exp.mat[gene,])
correlations<-apply(exp.mat,
1,
function(x){cor(gene.exp,x)})
return(rev(sort(correlations)))
}
correlated.features <- findCorrelatedFeatures(gene = 'gene.name',
obj = obj.integrated)
```
Using different approach to integrate our datasets
LIGER
```{r}
library(liger)
library(SeuratWrappers)
#Create combined atlas object
liger.integration <- merge(objOne, c(objThree, objTwo))
liger.integration <- NormalizeData(liger.integration)
liger.integration <- FindVariableFeatures(liger.integration)
liger.integration <- ScaleData(liger.integration, split.by = "dataset", do.center = FALSE)
liger.integration <- RunOptimizeALS(liger.integration, k = 20, lambda = 5, split.by = "dataset")
liger.integration <- RunQuantileNorm(liger.integration, split.by = "dataset")
# You can optionally perform Louvain clustering (`FindNeighbors` and `FindClusters`) after
# `RunQuantileNorm` according to your needs
liger.integration <- FindNeighbors(liger.integration, reduction = "iNMF", dims = 1:20)
liger.integration <- FindClusters(liger.integration, resolution = 0.4)
# Dimensional reduction and plotting
liger.integration <- RunUMAP(liger.integration, dims = 1:ncol(liger.integration[["iNMF"]]), reduction = "iNMF")
DimPlot(liger.integration, group.by = c("dataset", "ident"), ncol = 3)
saveRDS(liger.integration, "~/Documents/objLiger.rds")
```
Data integration using Harmony
```{r}
library(harmony)
harmony.integration <- merge(objOne, c(objThree, objTwo))
harmony.integration <- NormalizeData(harmony.integration)
harmony.integration <- FindVariableFeatures(harmony.integration)
harmony.integration <- ScaleData(harmony.integration)
harmony.integration <- RunPCA(harmony.integration, verbose = FALSE)
harmony.integration <- RunHarmony(harmony.integration, group.by.vars = "dataset")
harmony.integration <- RunUMAP(harmony.integration, reduction = "harmony", dims = 1:30)
harmony.integration <- FindNeighbors(harmony.integration, reduction = "harmony", dims = 1:30)
harmony.integration <- FindClusters(harmony.integration)
DimPlot(harmony.integration, group.by = c("dataset", "ident"), ncol = 2)
saveRDS(harmony.integration, "~/Documents/objHarmony.rds")
```
Evaluating the data integration via: Mixing Metrix and Local Structure
```{r}
#Calculate Mixing Metric
sctransform.mixing <- MixingMetric(obj.integrated,
grouping.var = 'dataset',
reduction = 'pca')
#Add mixing metric to the object metadata
obj.integrated[["mixing.metric"]] <- sctransform.mixing
#plot with Violin plot
VlnPlot(obj.integrated, "mixing.metric", pt.size = 0.1) + NoLegend() + ggtitle(label = 'SCTransform Integration: Mixing Metric')
#Liger
liger.mixing <- MixingMetric(liger.integration,
grouping.var = 'dataset',
reduction = 'iNMF')
liger.integration[['mixing.metric']] <- liger.mixing
VlnPlot(liger.integration, "mixing.metric", pt.size = 0.1) + NoLegend() + ggtitle(label = 'Liger Integration: Mixing Metric')
#Harmony
harmony.mixing <- MixingMetric(harmony.integration,
grouping.var = 'dataset',
reduction = 'harmony')
harmony.integration[['mixing.metric']] <- harmony.mixing
VlnPlot(harmony.integration, "mixing.metric", pt.size = 0.1) + NoLegend() + ggtitle(label = 'Harmony Integration: Mixing Metric')
#Local Structure
sctransform.LocalStruct <- LocalStruct(obj.integrated,
grouping.var = 'dataset',
reduction = 'pca')
liger.LocalStruct <- LocalStruct(liger.integration,
grouping.var = 'dataset',
reduction = 'iNMF')
harmony.LocalStruct <- LocalStruct(harmony.integration,
grouping.var = 'dataset',
reduction = 'harmony')
obj.integrated[["mixing.metric"]] <- MixingMetric(obj.integrated,
grouping.var = 'dataset',
reduction = 'pca')
RidgePlot(obj.integrated,
features = c("mixing.metric"),
group.by = 'seurat_clusters') + NoLegend()
```