-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMACA_10x_Notebook.Rmd
412 lines (288 loc) · 13.1 KB
/
MACA_10x_Notebook.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
---
title: "MACA Notebook"
output:
html_document: default
html_notebook: default
---
Enter the directory of the maca folder on your drive and the name of the tissue you want to analyze.
```{r}
rootdir = "~/src/maca"
tissue_of_interest = "Liver"
```
Load the requisite packages and some additional helper functions.
```{r}
library(Seurat)
library(dplyr)
library(Matrix)
```
Load the 10x metadata. Check which plates have been downloaded.
```{r}
tenx_metadata <- read.csv(file = paste0(rootdir, "/metadata/MACA_10x.csv"), sep=",", header = TRUE)
channel_folders = list.dirs(paste0(rootdir, "/data/10x"),recursive = FALSE)
n = length(strsplit(channel_folders[1],"[/]")[[1]])
downloaded_channels = lapply(channel_folders, function(x) strsplit(x,"[/]")[[1]][n])
tenx_metadata = tenx_metadata[tenx_metadata$channel %in% downloaded_channels,]
save_dir = paste0(rootdir, '/save')
dir.create(save_dir)
```
Load the 10x raw data.
```{r}
selected_metadata = filter(tenx_metadata, tissue == tissue_of_interest & mouse.age == 3)[,c('channel','tissue','mouse.age','mouse.sex')]
#Load the gene names and set the metadata columns by opening the first file
raw.data <- Read10X(data.dir = paste0(rootdir, "/data/10x/", downloaded_channels[1]))
colnames(raw.data) <- lapply(colnames(raw.data), function(x) paste0(downloaded_channels[1], '_', x))
meta.data = data.frame(row.names = colnames(raw.data))
meta.data['channel'] = downloaded_channels[1]
for(i in 2:nrow(selected_metadata)){
new.data = Read10X(data.dir = paste0(rootdir, "/data/10x/", downloaded_channels[i]))
colnames(new.data) <- lapply(colnames(new.data), function(x) paste0(downloaded_channels[i], '_', x))
new.metadata = data.frame(row.names = colnames(new.data))
new.metadata['channel'] = downloaded_channels[i]
raw.data = cbind(raw.data, new.data)
meta.data = rbind(meta.data, new.metadata)
}
rnames = row.names(meta.data)
meta.data <- merge(meta.data, selected_metadata, sort = F)
row.names(meta.data) <- rnames
```
Process the raw data and load it into the Seurat object.
```{r}
# Find ERCC's, compute the percent ERCC, and drop them from the raw data.
erccs <- grep(pattern = "^ERCC-", x = rownames(x = raw.data), value = TRUE)
percent.ercc <- Matrix::colSums(raw.data[erccs, ])/Matrix::colSums(raw.data)
ercc.index <- grep(pattern = "^ERCC-", x = rownames(x = raw.data), value = FALSE)
raw.data <- raw.data[-ercc.index,]
# Create the Seurat object with all the data
tiss <- CreateSeuratObject(raw.data = raw.data, project = tissue_of_interest,
min.cells = 5, min.genes = 5)
tiss <- AddMetaData(object = tiss, meta.data)
tiss <- AddMetaData(object = tiss, percent.ercc, col.name = "percent.ercc")
# Change default name for sums of counts from nUMI to nReads
# colnames(tiss@meta.data)[colnames(tiss@meta.data) == 'nUMI'] <- 'nReads'
# Create metadata columns for annotations and subannotations
tiss@meta.data[,'annotation'] <- NA
tiss@meta.data[,'subannotation'] <- NA
```
Calculate percent ribosomal genes.
```{r}
ribo.genes <- grep(pattern = "^Rp[sl][[:digit:]]", x = rownames(x = tiss@data), value = TRUE)
percent.ribo <- Matrix::colSums(tiss@raw.data[ribo.genes, ])/Matrix::colSums(tiss@raw.data)
tiss <- AddMetaData(object = tiss, metadata = percent.ribo, col.name = "percent.ribo")
```
A sanity check: genes per cell vs reads per cell.
```{r}
GenePlot(object = tiss, gene1 = "nUMI", gene2 = "nGene", use.raw=T)
```
Filter out cells with few reads and few genes.
```{r}
tiss <- FilterCells(object = tiss, subset.names = c("nGene", "nUMI"),
low.thresholds = c(500, 1000), high.thresholds = c(25000, 5000000))
```
Normalize the data, then regress out correlation with total reads
```{r}
tiss <- NormalizeData(object = tiss)
tiss <- ScaleData(object = tiss, vars.to.regress = c("nUMI", "percent.ribo","Rn45s"))
tiss <- FindVariableGenes(object = tiss, do.plot = TRUE, x.high.cutoff = Inf, y.cutoff = 0.5)
```
Run Principal Component Analysis.
```{r}
tiss <- RunPCA(object = tiss, do.print = FALSE)
tiss <- ProjectPCA(object = tiss, do.print = FALSE)
```
```{r, echo=FALSE, fig.height=4, fig.width=8}
PCHeatmap(object = tiss, pc.use = 1:3, cells.use = 500, do.balanced = TRUE, label.columns = FALSE, num.genes = 8)
```
Later on (in FindClusters and TSNE) you will pick a number of principal components to use. This has the effect of keeping the major directions of variation in the data and, ideally, supressing noise. There is no correct answer to the number to use, but a decent rule of thumb is to go until the plot plateaus.
```{r}
PCElbowPlot(object = tiss)
```
Choose the number of principal components to use.
```{r}
# Set number of principal components.
n.pcs = 15
```
The clustering is performed based on a nearest neighbors graph. Cells that have similar expression will be joined together. The Louvain algorithm looks for groups of cells with high modularity--more connections within the group than between groups. The resolution parameter determines the scale...higher resolution will give more clusters, lower resolution will give fewer.
For the top-level clustering, aim to under-cluster instead of over-cluster. It will be easy to subset groups and further analyze them below.
```{r}
# Set resolution
res.used <- 0.5
tiss <- FindClusters(object = tiss, reduction.type = "pca", dims.use = 1:n.pcs,
resolution = res.used, print.output = 0, save.SNN = TRUE)
```
To visualize
```{r}
# If cells are too spread out, you can raise the perplexity. If you have few cells, try a lower perplexity (but never less than 10).
tiss <- RunTSNE(object = tiss, dims.use = 1:n.pcs, seed.use = 10, perplexity=30, dim.embed = 2)
```
```{r}
# note that you can set do.label=T to help label individual clusters
TSNEPlot(object = tiss, do.label = T)
```
Check expression of genes of interset.
```{r, echo=FALSE, fig.height=8, fig.width=8}
genes_to_check = c('Alb', 'Cyp2f2', 'Cyp2e1', 'Hamp')
FeaturePlot(tiss, genes_to_check, pt.size = 4, nCol = 2)
```
Dotplots let you see the intensity of exppression and the fraction of cells expressing for each of your genes of interest.
```{r, echo=FALSE, fig.height=4, fig.width=8}
# To change the y-axis to show raw counts, add use.raw = T.
DotPlot(tiss, genes_to_check, plot.legend = T)
```
How big are the clusters?
```{r}
table(tiss@ident)
```
Which markers identify a specific cluster?
```{r}
clust.markers <- FindMarkers(object = tiss, ident.1 = 2, ident.2 = 1, only.pos = TRUE, min.pct = 0.25, thresh.use = 0.25)
```
```{r}
print(x = head(x= clust.markers, n = 10))
```
You can also compute all markers for all clusters at once. This may take some time.
```{r}
#tiss.markers <- FindAllMarkers(object = tiss, only.pos = TRUE, min.pct = 0.25, thresh.use = 0.25)
```
Display the top markers you computed above.
```{r}
#tiss.markers %>% group_by(cluster) %>% top_n(5, avg_diff)
```
## Assigning cell type identity to clusters
At a coarse level, we can use canonical markers to match the unbiased clustering to known cell types:
0: alpha
1: beta
2: beta
3: exocrine
4: duct
5: delta
6: gamma
7: endothelial
8: immune
9: stellate
```{r}
# stash current cluster IDs
tiss <- StashIdent(object = tiss, save.name = "cluster.ids")
# enumerate current cluster IDs and the labels for them
cluster.ids <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
annotation <- c("alpha cells", "beta cells", "beta cells", "exocrine cells", "duct cells", "delta cells", "gamma cells", "endothelial cells", "immune cells", "stellate cells")
tiss@meta.data[,'annotation'] <- plyr::mapvalues(x = tiss@meta.data$cluster.ids, from = cluster.ids, to = annotation)
TSNEPlot(object = tiss, do.label = TRUE, pt.size = 0.5, group.by='annotation')
```
## Checking for batch effects
Color by metadata, like plate barcode, to check for batch effects.
```{r}
TSNEPlot(object = tiss, do.return = TRUE, group.by = "channel")
```
```{r}
TSNEPlot(object = tiss, do.return = TRUE, group.by = "mouse.sex")
```
Print a table showing the count of cells in each identity category from each plate.
```{r}
table(as.character(tiss@ident), as.character(tiss@meta.data$channel))
```
### Save the R object so 1. you can reload without recomputing and 2. anyone else can reproduce your figures.
```{r}
save(tiss, file=paste0(save_dir,"/","10x_", tissue_of_interest, "_seurat_tiss.Robj"))
```
```{r}
# To reload a saved object
load(file=paste0(save_dir,"/","10x_", tissue_of_interest, "_seurat_tiss.Robj"))
```
# Subset and iterate
We can repeat the above analysis on a subset of cells, defined using cluster IDs or some other metadata. This is a good way to drill down and find substructure.
## First subset
```{r}
# Subset data based on cluster id
#subtiss <- SubsetData(object = tiss, ident.use = c(3), do.center = F, do.scale = F, cells.use = )
# To subset data based on annotation or other metadata, you can explicitly pass cell names
cells.to.use = tiss@cell.names[which(tiss@meta.data$mouse.sex == 'F')]
subtiss <- SubsetData(object = tiss, cells.use = cells.to.use, do.center = F, do.scale = F)
```
```{r}
subtiss <- NormalizeData(object = subtiss)
subtiss <- ScaleData(object = subtiss, vars.to.regress = c("nUMI", "percent.ribo","Rn45s"))
```
Run Principal Component Analysis.
```{r}
subtiss <- FindVariableGenes(object = subtiss, do.plot = TRUE, x.high.cutoff = Inf, y.cutoff = 0.8)
subtiss <- RunPCA(object = subtiss, pcs.compute = 20, weight.by.var = F)
subtiss <- ProjectPCA(object = subtiss, do.print = FALSE)
```
```{r}
# If this fails for your subset, it may be that cells.use is more cells than you have left! Try reducing it.
PCHeatmap(object = subtiss, pc.use = 1:3, cells.use = 250, do.balanced = TRUE, label.columns = FALSE, num.genes = 12)
```
Later on (in FindClusters and TSNE) you will pick a number of principal components to use. This has the effect of keeping the major directions of variation in the data and, ideally, supressing noise. There is no correct answer to the number to use, but a decent rule of thumb is to go until the plot plateaus.
```{r}
PCElbowPlot(object = subtiss)
```
Choose the number of principal components to use.
```{r}
# Set number of principal components.
sub.n.pcs = 5
```
The clustering is performed based on a nearest neighbors graph. Cells that have similar expression will be joined together. The Louvain algorithm looks for groups of cells with high modularity--more connections within the group than between groups. The resolution parameter determines the scale...higher resolution will give more clusters, lower resolution will give fewer.
```{r}
# Set resolution
sub.res.used <- 1
subtiss <- FindClusters(object = subtiss, reduction.type = "pca", dims.use = 1:sub.n.pcs,
resolution = sub.res.used, ,print.output = 0, save.SNN = TRUE)
```
To visualize
```{r}
# If cells are too spread out, you can raise the perplexity. If you have few cells, try a lower perplexity (but never less than 10).
subtiss <- RunTSNE(object = subtiss, dims.use = 1:sub.n.pcs, seed.use = 10, perplexity=20)
```
```{r}
# note that you can set do.label=T to help label individual clusters
TSNEPlot(object = subtiss, do.label = T)
```
```{r}
subtiss.markers <- FindAllMarkers(object = subtiss, only.pos = TRUE, min.pct = 0.25, thresh.use = 0.25)
```
```{r}
subtiss.markers %>% group_by(cluster) %>% top_n(6, avg_diff)
```
Check expression of genes of interset.
```{r}
genes_to_check = c('Alb', 'Cyp2f2', 'Cyp2e1', 'Hamp', 'Glul', 'Ass1', 'Axin2', 'Hal', 'Igfbp2')
FeaturePlot(subtiss, genes_to_check, pt.size = 1)
```
Dotplots let you see the intensity of exppression and the fraction of cells expressing for each of your genes of interest.
```{r}
# To change the y-axis to show raw counts, add use.raw = T.
DotPlot(subtiss, genes_to_check, plot.legend = T)
```
How big are the clusters?
```{r}
table(subtiss@ident)
```
## Checking for batch effects
Color by metadata, like plate barcode, to check for batch effects.
```{r}
TSNEPlot(object = subtiss, do.return = TRUE, group.by = "channel")
```
Print a table showing the count of cells in each identity category from each plate.
```{r}
table(as.character(subtiss@ident), as.character(subtiss@meta.data$channel))
```
### Assigning subannotations
For the subsets, we produce subannotations. These will be written back as metadata in the original object, so we can see all subannotations together.
If some of the clusters you find in the subset deserve additional annotation, you can add that right here. Use NA for clusters for which no subannotation is needed.
```{r}
subcluster.ids <- c(0, 1, 2)
subannotation <- c("Jun-negative","Jun-positive", NA)
subtiss@meta.data[,'subannotation'] <- plyr::mapvalues(x = subtiss@ident, from = subcluster.ids, to = subannotation)
tiss@meta.data[subtiss@cell.names,'subannotation'] <- as.character(subtiss@meta.data$subannotation)
TSNEPlot(object = subtiss, do.label = TRUE, pt.size = 0.5, group.by='subannotation')
```
When you save the subtissue, please give it a name.
```{r}
subtiss.name = 'liver_hepatocytes'
save(subtiss, file=paste0(save_dir,"/","10x_",subtiss.name, "_seurat_subtiss.Robj"))
```
# Export the final metadata
So that Biohub can easily combine all your annotations, please export them as a simple csv.
```{r}
write.csv(tiss@meta.data[,c('annotation','subannotation')],file =paste0(save_dir,"/", "10x_", tissue_of_interest,"_annotation.csv"))
```