-
Notifications
You must be signed in to change notification settings - Fork 0
/
S1_Pan_neuro_integrated.Rmd
executable file
·248 lines (180 loc) · 9 KB
/
S1_Pan_neuro_integrated.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
---
title: "Neuro_Droso_ND75KD - Generating integrated dataset including both Pan_neuro_control and Pan_neuro_ND75KD Seurat objects"
author: "Vincent Gardeux"
date_created: "2023-05-12"
date_modified: "2024-10-23"
output:
html_document:
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = "/data/gardeux/Neuro_Droso_ND75KD/")
```
## Introduction
In this script, I will analyze the Pan Neuro ND75-KD single-cell RNA-seq dataset using Seurat.
## Libraries
```{r}
suppressPackageStartupMessages(library(Seurat, lib.loc = "/home/gardeux/.R/lib/")) # Seurat v4.4.0, library for single-cell analysis
suppressPackageStartupMessages(library(SeuratObject, lib.loc = "/home/gardeux/.R/lib/")) # For compatibility purpose
suppressPackageStartupMessages(library(harmony)) # For integration
suppressPackageStartupMessages(library(ggplot2)) # For Plotting
suppressPackageStartupMessages(library(data.table)) # For reading/writing text files
suppressPackageStartupMessages(library(crayon)) # Just for bolding the console output :D
cat(bold("Seurat"), "version", as.character(packageVersion("Seurat")), "\n")
cat(bold("SeuratObject"), "version", as.character(packageVersion("Seurat")), "\n")
cat(bold("harmony"), "version", as.character(packageVersion("harmony")), "\n")
cat(bold("ggplot2"), "version", as.character(packageVersion("ggplot2")), "\n")
cat(bold("data.table"), "version", as.character(packageVersion("data.table")), "\n")
```
## Parameters
```{r}
input_seurat_path <- c("./data/Pan_neuro_control.rds", "./data/Pan_neuro_ND75KD.rds")
output_seurat_path <- "./data/Pan_neuro_integrated.rds"
output_project_name <- tools::file_path_sans_ext(basename(output_seurat_path))
```
## I. Parsing
First step is to read the Seurat objects, previously created
```{r}
message("Loading Seurat objects...")
seurat_objects <- list()
for(path in input_seurat_path){
filename <- tools::file_path_sans_ext(basename(path))
data.seurat.tmp <- readRDS(path)
message(ncol(data.seurat.tmp), " cells were loaded")
message(nrow(data.seurat.tmp), " genes were loaded")
#data.seurat.tmp <- RenameCells(data.seurat.tmp, new.names = paste0(colnames(data.seurat.tmp), "_", strsplit(filename, split = "_")[[1]][3]))
# I ended up adding manually _ctrl or _ndkd
seurat_objects[[filename]] <- data.seurat.tmp
}
```
## II. Merge
Inspired from http://htmlpreview.github.io/?https://github.com/immunogenomics/harmony/blob/master/docs/SeuratV3.html
First step is to create a single Seurat object from the two files
```{r}
# Merge the seurat objects
data.seurat <- merge(x = seurat_objects[[1]], y = seurat_objects[[2]], project = output_project_name)
# Filling metadatas
data.seurat@meta.data$RNA_snn_res.4 <- NULL # Makes no sense now
data.seurat@meta.data$seurat_clusters <- NULL # Makes no sense now
# Size of the new Seurat object
message(ncol(data.seurat), " cells in integrated Seurat object")
message(nrow(data.seurat), " genes in integrated Seurat object")
# Saving RAM
rm(seurat_objects)
```
# Normalization
```{r}
# Default params
data.seurat <- NormalizeData(data.seurat, normalization.method = "LogNormalize", scale.factor = 10000)
```
# Highly variable genes
```{r}
data.seurat <- FindVariableFeatures(data.seurat, selection.method = "vst", nfeatures = 2000)
VariableFeaturePlot(data.seurat)
```
# Scaling
```{r}
data.seurat <- ScaleData(data.seurat, features = rownames(data.seurat))
```
# PCA
```{r}
data.seurat <- RunPCA(data.seurat, features = VariableFeatures(data.seurat), verbose = F, npcs = 100)
DimPlot(object = data.seurat, reduction = "pca", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_ND75KD")
DimPlot(object = data.seurat, reduction = "pca", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_control")
```
# Visualizing the differences in each batch
```{r}
VlnPlot(object = data.seurat, features = "PC_1", group.by = "orig.ident", pt.size = 1)
```
It actually looks pretty similar. We don't see much batch-effect.
# JackStraw
```{r}
data.seurat <- JackStraw(data.seurat, num.replicate = 100, dims = 100)
data.seurat <- ScoreJackStraw(data.seurat, dims = 1:100)
data.seurat@reductions$pca@jackstraw$overall.p.values
```
The p-value distribution show that ~100 PCs are good (similar to the individual files), so I keep 100 PCs for the rest of the pipeline.
I've plotted the tail of the JackStraw plot (80 to 100 PCs) to see that indeed the sweet spot is around there.
```{r}
JackStrawPlot(data.seurat, dims = 80:100)
npcs <- 100
```
# What about UMAP (visualization)?
```{r}
data.seurat <- RunUMAP(data.seurat, dims = 1:npcs, reduction = "pca", reduction.name = "umap_uncorrected")
DimPlot(object = data.seurat, reduction = "umap_uncorrected", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_ND75KD")
DimPlot(object = data.seurat, reduction = "umap_uncorrected", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_control")
```
Both datasets already merge very well. We don't want to over-correct and loose the unique ND75KD cluster.
# And t-SNE (visualization)?
```{r}
data.seurat <- RunTSNE(data.seurat, dims = 1:npcs, reduction = "pca", reduction.name = "tsne_uncorrected")
DimPlot(object = data.seurat, reduction = "tsne_uncorrected", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_ND75KD")
DimPlot(object = data.seurat, reduction = "tsne_uncorrected", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_control")
```
# Run Harmony
```{r}
# Lenient correction (what is needed here)
data.seurat <- RunHarmony(object = data.seurat, reduction.use = "pca", dims.use = 1:npcs, group.by.vars = "orig.ident", plot_convergence = TRUE, early_stop = T, reduction.save = "harmony")
# Over-correction (see what will happen)
data.seurat <- RunHarmony(object = data.seurat, reduction.use = "pca", dims.use = 1:npcs, group.by.vars = "orig.ident", plot_convergence = TRUE, early_stop = F, max_iter = 100, reduction.save = "harmony_overcorrected")
```
Now plot the Harmony embeddings
```{r}
DimPlot(object = data.seurat, reduction = "harmony", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_ND75KD")
DimPlot(object = data.seurat, reduction = "harmony", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_control")
```
Still looks good. What about the first component of the Harmony embedding?
```{r}
VlnPlot(object = data.seurat, features = "harmony_1", group.by = "orig.ident", pt.size = .1)
```
Looks ok
## III Downstream analysis
Now that all data are integrated into Harmony embeddings, I will perform the standard pipeline, but using Harmony embeddings, instead of PCA
# Clustering
```{r}
data.seurat <- FindNeighbors(data.seurat, dims = 1:npcs, reduction = "harmony", graph.name = c("RNA_nn_harmony", "RNA_snn_harmony"))
# I use a resolution of 4, which is completely arbitrary, we may tune it to have more/less clusters
data.seurat <- FindClusters(data.seurat, resolution = 4, graph.name = "RNA_snn_harmony")
data.seurat$seurat_clusters_harmony_res.4 <- data.seurat@meta.data$RNA_snn_harmony_res.4
data.seurat@meta.data$RNA_snn_harmony_res.4 <- NULL
```
# UMAP (visualization)
```{r}
data.seurat <- RunUMAP(data.seurat, dims = 1:npcs, reduction = "harmony", reduction.name = "umap_harmony")
DimPlot(data.seurat, reduction = "umap_harmony", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_control")
DimPlot(data.seurat, reduction = "umap_harmony", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_ND75KD")
```
Not too bad
```{r}
DimPlot(data.seurat, reduction = "umap_harmony", pt.size = 1, group.by = "seurat_clusters")
```
What about over-corrected Harmony embeddings
```{r}
data.seurat <- RunUMAP(data.seurat, dims = 1:npcs, reduction = "harmony_overcorrected", reduction.name = "umap_harmony_overcorrected")
DimPlot(data.seurat, reduction = "umap_harmony_overcorrected", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_control")
DimPlot(data.seurat, reduction = "umap_harmony_overcorrected", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_ND75KD")
```
Indeed the ND75KD-specific cluster was merged
# t-SNE (visualization)
```{r}
data.seurat <- RunTSNE(data.seurat, dims = 1:npcs, reduction = "harmony", reduction.name = "tsne_harmony")
DimPlot(data.seurat, reduction = "tsne_harmony", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_control")
DimPlot(data.seurat, reduction = "tsne_harmony", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_ND75KD")
```
Same here
```{r}
DimPlot(data.seurat, reduction = "tsne_harmony", pt.size = 1, group.by = "seurat_clusters")
```
What about over-corrected Harmony embeddings
```{r}
data.seurat <- RunTSNE(data.seurat, dims = 1:npcs, reduction = "harmony_overcorrected", reduction.name = "tsne_harmony_overcorrected")
DimPlot(data.seurat, reduction = "tsne_harmony_overcorrected", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_control")
DimPlot(data.seurat, reduction = "tsne_harmony_overcorrected", pt.size = 1, group.by = "orig.ident", order = "Pan_neuro_ND75KD")
```
Indeed the ND75KD-specific cluster was a bit more merged
## IV. Saving the Seurat object
```{r}
saveRDS(data.seurat, file = output_seurat_path)
```