/
Statistics.Rmd
250 lines (189 loc) · 8.07 KB
/
Statistics.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
---
title: "Statistics Functions"
date: 'Compiled: `r format(Sys.Date(), "%B %d, %Y")`'
output: rmarkdown::html_vignette
theme: united
vignette: >
%\VignetteIndexEntry{Statistics Functions}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
***
<style>
p.caption {
font-size: 0.9em;
}
</style>
```{r setup, include=FALSE}
all_times <- list() # store the time for each chunk
knitr::knit_hooks$set(time_it = local({
now <- NULL
function(before, options) {
if (before) {
now <<- Sys.time()
} else {
res <- difftime(Sys.time(), now, units = "secs")
all_times[[options$label]] <<- res
}
}
}))
knitr::opts_chunk$set(
tidy = TRUE,
tidy.opts = list(width.cutoff = 95),
message = FALSE,
warning = FALSE,
time_it = TRUE
)
```
# Statistics Functions
scCustomize contains a couple simple helper functions to return metrics of interest.
For this vignette I will be utilizing pbmc3k dataset from the SeuratData package.
```{r init}
# Load Packages
library(ggplot2)
library(dplyr)
library(magrittr)
library(Seurat)
library(scCustomize)
library(qs)
# Load example dataset for tutorial
pbmc <- pbmc3k.SeuratData::pbmc3k.final
```
```{r include=FALSE}
# Update pbmc check
pbmc <- UpdateSeuratObject(pbmc)
```
Now let's add some extra meta data for use with tutorial
```{r}
# Add mito and ribo data
pbmc <- Add_Mito_Ribo(object = pbmc, species = "human")
# Add random sample and group variables
pbmc$orig.ident <- sample(c("sample1", "sample2", "sample3", "sample4"), size = ncol(pbmc), replace = TRUE)
pbmc@meta.data$group[pbmc@meta.data$orig.ident == "sample1" | pbmc@meta.data$orig.ident == "sample3"] <- "Group 1"
pbmc@meta.data$group[pbmc@meta.data$orig.ident == "sample2" | pbmc@meta.data$orig.ident == "sample4"] <- "Group 2"
# Add dummy module score
pbmc <- AddModuleScore(object = pbmc, features = list(c("CD3E", "CD4", "THY1", "TCRA")), name = "module_score")
```
## Cells Per Identity
It can be really helpful to know the number and percentage of cells per identity/cluster during analysis.
scCustomize provides the `Cluster_Stats_All_Samples` function to make this easy.
```{r eval=FALSE}
cluster_stats <- Cluster_Stats_All_Samples(seurat_object = pbmc)
cluster_stats
```
```{r echo=FALSE}
cluster_stats <- Cluster_Stats_All_Samples(seurat_object = pbmc)
cluster_stats %>%
kableExtra::kbl() %>%
kableExtra::kable_styling(bootstrap_options = c("bordered", "condensed", "responsive", "striped"))
```
By default `Cluster_Stats_All_Samples` uses "orig.ident" as the group variable but user can specify any `@meta.data` slot variable.
```{r eval=FALSE}
cluster_stats <- Cluster_Stats_All_Samples(seurat_object = pbmc, group_by_var = "group")
cluster_stats
```
```{r echo=FALSE}
cluster_stats <- Cluster_Stats_All_Samples(seurat_object = pbmc, group_by_var = "group")
cluster_stats %>%
kableExtra::kbl() %>%
kableExtra::kable_styling(bootstrap_options = c("bordered", "condensed", "responsive", "striped"))
```
## Percent of Cells Expressing Feature(s)
It can also be informative to understand the percent of cells/nuclei that express a given feature or set of features.
scCustomize provides the `Percent_Expressing` function to return these results.
```{r eval=FALSE}
percent_express <- Percent_Expressing(seurat_object = pbmc, features = c("CD4", "CD8A"))
```
```{r echo=FALSE}
percent_express <- Percent_Expressing(seurat_object = pbmc, features = c("CD4", "CD8A"))
percent_express %>%
kableExtra::kbl() %>%
kableExtra::kable_styling(bootstrap_options = c("bordered", "condensed", "responsive", "striped"))
```
### Change grouping variable
By default the function groups expression across `@active.ident` (see above) but user can specify difference variable if desired.
```{r eval=FALSE}
percent_express <- Percent_Expressing(seurat_object = pbmc, features = c("CD4", "CD8A"), group_by = "orig.ident")
```
```{r echo=FALSE}
percent_express <- Percent_Expressing(seurat_object = pbmc, features = c("CD4", "CD8A"), group_by = "orig.ident")
percent_express %>%
kableExtra::kbl() %>%
kableExtra::kable_styling(bootstrap_options = c("bordered", "condensed", "responsive", "striped"))
```
### Split within groups
User can also supply a `split_by` variable to quantify expression within group split by meta data variable
```{r eval=FALSE}
percent_express <- Percent_Expressing(seurat_object = pbmc, features = c("CD4", "CD8A"), split_by = "group")
```
```{r echo=FALSE}
percent_express <- Percent_Expressing(seurat_object = pbmc, features = c("CD4", "CD8A"), split_by = "group")
percent_express %>%
kableExtra::kbl() %>%
kableExtra::kable_styling(bootstrap_options = c("bordered", "condensed", "responsive", "striped"))
```
### Set threshold of expression
Users can also supply a threshold value to quantify percent of cells expressing feature(s) above a certain threshold (be sure to note which slot is being used to quantify expression when setting thresholds).
*NOTE: `Percent_Expressing` currently only supports single threshold across all features*
```{r eval=FALSE}
percent_express <- Percent_Expressing(seurat_object = pbmc, features = c("CD4", "CD8A"), threshold = 2)
```
```{r echo=FALSE}
percent_express <- Percent_Expressing(seurat_object = pbmc, features = c("CD4", "CD8A"), threshold = 2)
percent_express %>%
kableExtra::kbl() %>%
kableExtra::kable_styling(bootstrap_options = c("bordered", "condensed", "responsive", "striped"))
```
## Calculate Summary Median Values
scCustomize contains function `Median_Stats` to quickly calculate the medians for basic QC stats (Genes/, UMIs/, % Mito/Cell).
### Basic Use
By default `Median_Stats` will calculate medians for the following meta data columns (if present): "nCount_RNA", "nFeature_RNA", "percent_mito", "percent_ribo", "percent_mito_ribo".
```{r eval=FALSE}
median_stats <- Median_Stats(seurat_object = pbmc, group_by_var = "orig.ident")
```
```{r echo=FALSE}
median_stats <- Median_Stats(seurat_object = pbmc, group_by_var = "orig.ident")
median_stats %>%
kableExtra::kbl() %>%
kableExtra::kable_styling(bootstrap_options = c("bordered", "condensed", "responsive", "striped"))
```
### Additional Variables
In addition to default variables, users can supply their own additional meta data columns to calculate medians using the `median_var` parameter.
*NOTE: The meta data column must be in numeric format (e.g., integer, numeric, dbl)*
```{r eval=FALSE}
median_stats <- Median_Stats(seurat_object = pbmc, group_by_var = "orig.ident", median_var = "module_score1")
```
```{r echo=FALSE}
median_stats <- Median_Stats(seurat_object = pbmc, group_by_var = "orig.ident", median_var = "module_score1")
median_stats %>%
kableExtra::kbl() %>%
kableExtra::kable_styling(bootstrap_options = c("bordered", "condensed", "responsive", "striped"))
```
### Calculate Median Absolute Deviations
In addition to calculating the median values scCustomize includes function to calculate the median absolute deviation for each of those features with function `MAD_Stats`. By setting the parameter `mad_num` the function will retrun the MAD*mad_num.
```{r eval=FALSE}
mad <- MAD_Stats(seurat_object = pbmc, group_by_var = "orig.ident", mad_num = 2)
```
```{r echo=FALSE}
mad <- MAD_Stats(seurat_object = pbmc, group_by_var = "orig.ident", mad_num = 2)
mad %>%
kableExtra::kbl() %>%
kableExtra::kable_styling(bootstrap_options = c("bordered", "condensed", "responsive", "striped"))
```
## Plotting Median Data
scCustomize also contains series of functions for plotting the results of these median calculations:
* `Plot_Median_Genes()`
* `Plot_Median_UMIs()`
* `Plot_Median_Mito()`
* `Plot_Median_Other()`
```{r eval=FALSE}
Plot_Median_Genes(seurat_object = pbmc)
Plot_Median_Genes(seurat_object = pbmc, group_by = "group")
Plot_Median_Other(seurat_object = pbmc, median_var = "module_score1", group_by = "group")
```
```{r echo=FALSE, fig.height=7, fig.width=13, fig.align='center'}
p1 <- Plot_Median_Genes(seurat_object = pbmc)
p2 <- Plot_Median_Genes(seurat_object = pbmc, group_by = "group")
p3 <- Plot_Median_Other(seurat_object = pbmc, median_var = "module_score1", group_by = "group")
patchwork::wrap_plots(p1, p2, p3, ncol = 3)
```