/
tidy_single_cell.Rmd
253 lines (187 loc) · 5.47 KB
/
tidy_single_cell.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
---
title: "Single-cell Tidy Transcriptomics - analysis of single-cell RNA sequencing data with R tidy principles"
author:
- name: Stefano Mangiola
affiliation: The Walter and Eliza Hall Institute of Medical Research, 1G Royal Parade, Parkville, VIC 3052, Melbourne, Australia; Department of Medical Biology, The University of Melbourne, Parkville, VIC 3010, Melbourne, Australia
- name: Maria Doyle
affiliation: Peter MacCallum Cancer Centre, 305 Grattan Street, Parkville, Melbourne, Victoria, Australia
date: "11 November 2020"
vignette: >
%\VignetteIndexEntry{Single-cell Tidy Transcriptomics - analysis of single-cell RNA sequencing data with R tidy principles}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
bibliography: "`r file.path(system.file(package='zhejiang2020', 'vignettes'), 'tidytranscriptomics.bib')`"
output:
BiocStyle::html_document:
fig_caption: true
---
![](tidybulk_logo.png){width=100px}
## Introduction
# Set-up
```{r setup2, eval=TRUE, message=FALSE}
library(zhejiang2020)
# Bioconductor
library(scran)
library(scater)
library(EnsDb.Hsapiens.v86)
# tidyverse core packages
library(tibble)
library(dplyr)
library(tidyr)
library(readr)
library(magrittr)
library(ggplot2)
library(ggbeeswarm)
#library(tidyHeatmap)
library(SingleCellExperiment)
library(tidySingleCellExperiment)
```
### Participation
After the lecture, participants are expected to follow along the hands-on session. we highly recommend participants bringing your own laptop.
### _R_ / _Bioconductor_ packages used
The following R/Bioconductor packages will be explicitly used:
* tidySingleCellExperiment
* DropletUtils
* scran
* scater
* singleR
### Time outline
| Activity | Time |
|----------------------------------|------|
| Analysis workflow | 30m |
| Q & A | 20m |
## Data loading
We get the original SingleCellExperiment data used in the previous session.
```{r}
zhejiang2020::single_cell_experiment
```
We can get a tidy representation where cell-wise information is displayed. The dataframe is displayed as `tibble abstraction`, to indicate that appears and act as a `tibble`, but underlies a `SingleCellExperiment`.
```{r}
counts =
zhejiang2020::single_cell_experiment %>%
tidy()
counts
```
If we need, we can extract transcript information too. A regular tibble will be returned for independent visualisation and analyses.
```{r}
counts %>%
join_transcripts("ENSG00000228463")
```
As before, we add gene symbols to the data
```{r}
#--- gene-annotation ---#
rownames(counts) <-
uniquifyFeatureNames(
rowData(counts)$ID,
rowData(counts)$Symbol
)
```
We check the mitochondrial expression for all cells
```{r}
# Gene product location
location <- mapIds(
EnsDb.Hsapiens.v86,
keys=rowData(counts)$ID,
column="SEQNAME",
keytype="GENEID"
)
#--- quality-control ---#
counts_annotated =
counts %>%
# Join mitochondrion statistics
left_join(
perCellQCMetrics(., subsets=list(Mito=which(location=="MT"))) %>%
as_tibble(rownames="cell"),
by="cell"
) %>%
# Label cells
mutate(high_mitochondrion = isOutlier(subsets_Mito_percent, type="higher"))
```
We can plot various statistics
```{r}
counts_annotated %>%
plotColData(
y = "subsets_Mito_percent",
colour_by = "high_mitochondrion"
) +
ggtitle("Mito percent")
counts_annotated %>%
ggplot(aes(x=1,y=subsets_Mito_percent,
color = high_mitochondrion,
alpha=high_mitochondrion,
size= high_mitochondrion
)) +
ggbeeswarm::geom_quasirandom() +
# Customisation
scale_color_manual(values=c("black", "#e11f28")) +
scale_size_discrete(range = c(0, 2))
```
We can filter the the alive cells using dplyr function.
```{r}
counts_filtered =
counts_annotated %>%
# Filter data
filter(!high_mitochondrion)
counts_filtered
```
### Scaling
As before, we can use standard Bioconductor utilities for calculating scaled log counts.
```{r}
#--- normalization ---#
set.seed(1000)
# Calculate clusters
clusters <- quickCluster(counts_filtered)
# Add scaled counts
counts_scaled <-
counts_filtered %>%
computeSumFactors(cluster=clusters) %>%
logNormCounts()
counts_scaled %>%
join_transcripts("CD79B")
```
### Detect variable gene-transcripts
As before, we can use standard Bioconductor utilities to identify variable genes.
```{r}
#--- variance-modelling ---#
set.seed(1001)
gene_variability <- modelGeneVarByPoisson(counts_scaled)
top_variable <- getTopHVGs(gene_variability, prop=0.1)
```
### Dimensionality reduction
As before, we can use standard Bioconductor utilities to calculate reduced dimensions.
```{r}
#--- dimensionality-reduction ---#
set.seed(10000)
counts_reduction <-
counts_scaled %>%
denoisePCA(subset.row=top_variable, technical=gene_variability) %>%
runTSNE(dimred="PCA") %>%
runUMAP(dimred="PCA")
counts_reduction
```
## Clustering
We use mutate function from dplyr to attach the cluster label to the existing dataset.
```{r}
counts_cluster <-
counts_reduction %>%
mutate(
cluster =
buildSNNGraph(., k=10, use.dimred = 'PCA') %>%
igraph::cluster_louvain() %$%
membership %>%
as.factor()
)
counts_cluster
```
We can customise the tSNE plot plotting with ggplot.
```{r}
plotTSNE(counts_cluster, colour_by="cluster",text_by="cluster")
counts_cluster %>%
ggplot(aes(
TSNE1, TSNE2,
color=cluster,
size = 1/subsets_Mito_percent
)) +
geom_point(alpha=0.2) +
theme_bw()
```