-
Notifications
You must be signed in to change notification settings - Fork 91
/
reordering_injects_randomness.Rmd
296 lines (215 loc) · 9.27 KB
/
reordering_injects_randomness.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
---
title: "Tabula Muris: single organ analysis"
output: html_notebook
---
Load the requisite packages and some additional helper functions.
```{r}
library(Seurat)
library(dplyr)
library(Matrix)
library(stringr)
library(tidyverse)
library(here)
```
Load the count data for one organ and add it to the Seurat object.
```{r}
organ = "Bladder"
```
```{r}
raw.data <- read.csv(here("00_data_ingest","facs_raw_data", "FACS",paste0(organ,"-counts.csv")),row.names = 1)
meta.data <- read.csv(here("00_data_ingest","facs_raw_data", "metadata_FACS.csv"))
```
```{r}
plates <- str_split(colnames(raw.data),"[.]", simplify = TRUE)[,2]
rownames(meta.data) <- meta.data$plate.barcode
cell.meta.data <- meta.data[plates,]
rownames(cell.meta.data) <- colnames(raw.data)
```
```{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,]
```
```{r}
# Create the Seurat object with all the data
tiss <- CreateSeuratObject(raw.data = raw.data, min.cells = 5, min.genes = 5)
tiss <- AddMetaData(object = tiss, cell.meta.data)
tiss <- AddMetaData(object = tiss, percent.ercc, col.name = "percent.ercc")
# Change default name for sums of counts from nUMI to nReads
colnames([email protected])[colnames([email protected]) == 'nUMI'] <- 'nReads'
```
```{r}
ribo.genes <- grep(pattern = "^Rp[sl][[:digit:]]", x = rownames(x = tiss@data), value = TRUE)
percent.ribo <- Matrix::colSums([email protected][ribo.genes, ])/Matrix::colSums([email protected])
tiss <- AddMetaData(object = tiss, metadata = percent.ribo, col.name = "percent.ribo")
```
```{r}
percent.Rn45s <- Matrix::colSums([email protected][c('Rn45s'), ])/Matrix::colSums([email protected])
tiss <- AddMetaData(object = tiss, metadata = percent.Rn45s, col.name = "percent.Rn45s")
```
A sanity check: genes per cell vs reads per cell.
```{r}
GenePlot(object = tiss, gene1 = "nReads", gene2 = "nGene", use.raw=T)
```
Filter out cells with few reads and few genes.
```{r}
tiss <- FilterCells(object = tiss, subset.names = c("nGene", "nReads"),
low.thresholds = c(500, 50000), high.thresholds = c(25000, 2000000))
```
Normalize the data, then regress out correlation with total reads
```{r}
tiss <- NormalizeData(object = tiss)
tiss <- ScaleData(object = tiss, vars.to.regress = c("nReads", "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 = 10
```
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)
```
```{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=12, fig.width=8}
genes_to_check = c('Ins1', 'Gcg', 'Ppy', 'Sst', 'Chga', 'Krt19', 'Pecam1', 'Pdgfra', 'Ptprc', 'Ghrl')
#genes_to_check = c('Alb', 'Cyp2f2', 'Cyp2e1', 'Hamp')
FeaturePlot(tiss, genes_to_check, pt.size = 1, nCol = 3)
```
# Shuffle and do it again
```{r}
shuffle = colnames(raw.data)[shuffled_cells]
View(raw.data[,shuffle])
```
```{r}
dense = Matrix(as.matrix(raw.data), sparse = TRUE)
```
```{r}
dense[,shuffle]
```
```{r}
set.seed(1)
shuffled_cells = sample(ncol(raw.data))
raw.data <- raw.data[,shuffled_cells]
```
```{r}
plates <- str_split(colnames(raw.data),"[.]", simplify = TRUE)[,2]
rownames(meta.data) <- meta.data$plate.barcode
cell.meta.data <- meta.data[plates,]
rownames(cell.meta.data) <- colnames(raw.data)
```
```{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,]
```
```{r}
# Create the Seurat object with all the data
siss <- CreateSeuratObject(raw.data = raw.data, min.cells = 5, min.genes = 5)
siss <- AddMetaData(object = siss, cell.meta.data)
siss <- AddMetaData(object = siss, percent.ercc, col.name = "percent.ercc")
# Change default name for sums of counts from nUMI to nReads
colnames([email protected])[colnames([email protected]) == 'nUMI'] <- 'nReads'
```
```{r}
ribo.genes <- grep(pattern = "^Rp[sl][[:digit:]]", x = rownames(x = siss@data), value = TRUE)
percent.ribo <- Matrix::colSums([email protected][ribo.genes, ])/Matrix::colSums([email protected])
siss <- AddMetaData(object = siss, metadata = percent.ribo, col.name = "percent.ribo")
```
```{r}
percent.Rn45s <- Matrix::colSums([email protected][c('Rn45s'), ])/Matrix::colSums([email protected])
siss <- AddMetaData(object = siss, metadata = percent.Rn45s, col.name = "percent.Rn45s")
```
A sanity check: genes per cell vs reads per cell.
```{r}
GenePlot(object = siss, gene1 = "nReads", gene2 = "nGene", use.raw=T)
```
Filter out cells with few reads and few genes.
```{r}
siss <- FilterCells(object = siss, subset.names = c("nGene", "nReads"),
low.thresholds = c(500, 50000), high.thresholds = c(25000, 2000000))
```
Normalize the data, then regress out correlation with total reads
```{r}
siss <- NormalizeData(object = siss)
siss <- ScaleData(object = siss, vars.to.regress = c("nReads", "percent.ribo","Rn45s"))
siss <- FindVariableGenes(object = siss, do.plot = TRUE, x.high.cutoff = Inf, y.cutoff = 0.5)
```
Run Principal Component Analysis.
```{r}
siss <- RunPCA(object = siss, do.print = FALSE)
siss <- ProjectPCA(object = siss, do.print = FALSE)
```
```{r, echo=FALSE, fig.height=4, fig.width=8}
PCHeatmap(object = siss, 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 = siss)
```
Choose the number of principal components to use.
```{r}
# Set number of principal components.
n.pcs = 10
```
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
siss <- FindClusters(object = siss, 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).
siss <- RunTSNE(object = siss, dims.use = 1:n.pcs, seed.use = 10, perplexity=30)
```
```{r}
# note that you can set do.label=T to help label individual clusters
TSNEPlot(object = siss, do.label = T)
```
# Compare the shuffled and unshuffled versions
```{r}
all(colnames([email protected])[shuffled_cells] == colnames([email protected]))
```
```{r}
df <- as_tibble(tiss@ident)
df['cell'] <- names(tiss@ident)
df <- rename(df, cluster = value)
sf <- as_tibble(siss@ident)
sf <- rename(sf, scluster = value)
sf['cell'] <- names(siss@ident)
af <- left_join(df, sf, on='cell')
af %>% group_by(cluster, scluster) %>% summarize(count = n()) %>% spread(key = scluster, value = count)
```