-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
456 lines (296 loc) · 14.5 KB
/
README.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
warning = FALSE,
message = FALSE
)
```
```{r, include=FALSE}
library(magrittr)
library(ggplot2)
```
# parcutils
<!-- badges: start -->
```{r , echo=FALSE , results='asis' , message=FALSE}
cat(
badger::badge_devel("cparsania/parcutils" , color = "blue"),
badger::badge_lifecycle()
)
```
<!-- badges: end -->
The goal of `parcutils` is to provide day to day bioinformatics utility functions. Most of the functions in the package are useful for analyzing and visualizing complex RNA-seq studies.
## Installation
```{r, eval=FALSE}
if(require("devtools") && require("BiocManager")){
options(repos = BiocManager::repositories() )
devtools::install_github("cparsania/parcutils")
} else{
install.packages(c("devtools","BiocManager"))
options(repos = BiocManager::repositories() )
devtools::install_github("cparsania/parcutils")
}
```
## RNA-seq analysis
### Differential expression analysis
#### Prepare a count table
```{r, message=FALSE, warning=FALSE}
count_file <- system.file("extdata","toy_counts.txt" , package = "parcutils")
count_data <- readr::read_delim(count_file, delim = "\t")
count_data
```
#### Group replicates by samples
To run DESeq2, replicates for each sample needs to be grouped.
```{r , message=FALSE , warning=FALSE}
sample_info <- count_data %>% colnames() %>% .[-1] %>%
tibble::tibble(samples = . , groups = rep(c("control" ,"treatment1" , "treatment2") ,
each = 3))
sample_info
```
> NOTE: Samples which are present in the object 'sample_info' will be considered for differential expressed analysis.
#### Run `DESeq2` for multiple differential gene comparison.
```{r , message=FALSE, warning=FALSE}
res <- parcutils::run_deseq_analysis(counts = count_data ,
sample_info = sample_info,
column_geneid = "gene_id" ,
cutoff_lfc = 1,
cutoff_pval = 0.05,
group_numerator = c("treatment1", "treatment2") ,
group_denominator = c("control"))
```
#### Let's have a look in to `res`
```{r}
res
```
`res` is an object of improved `dataframe` - `tibble`. Each row in the `res` is a differential comparison which can be identified by the value from the column `comp`.
```{r}
res$de_comparisons
```
Data related to each differential comparison can be found from other columns of `res`.
For example, summary of differently expressed genes can be found from the column `deg_summmary`
```{r}
res$deg_summmary
```
As described below there are several helper functions to get data from the `res` .
## Get data from `res` using helper functions
```{r}
# get normalised gene expression value for all genes across all samples.
parcutils::get_normalised_expression_matrix(x = res,
samples = NULL,
genes = NULL,
summarise_replicates = FALSE)
# average gene expression values across relicates
parcutils::get_normalised_expression_matrix(x = res,
samples = NULL,
genes = NULL,
summarise_replicates = T,
summarise_method = "median")
# get fold change values for all genes and all comparisons.
q_genes = c("ENSG00000196415:PRTN3", "ENSG00000221988:PPT2", "ENSG00000163138:PACRGL", "ENSG00000183840:GPR39", "ENSG00000146700:SSC4D", "ENSG00000163746:PLSCR2", "ENSG00000155918:RAET1L", "ENSG00000151458:ANKRD50", "ENSG00000167074:TEF", "ENSG00000130159:ECSIT")
parcutils::get_fold_change_matrix(x = res,
sample_comparisons = res$de_comparisons,
genes = q_genes)
# get differentially expressed genes for given comparison
parcutils::get_genes_by_regulation(x = res,
sample_comparison = "treatment1_VS_control",
regulation = "both" # can be one of the "up" , "down" , "both", "other", "all"
)
# get replicates group data
parcutils::.group_replicates_by_sample(res)
```
## Generate several visualizations from `res`
### Visualize pairwise correlation between replicates
```{r}
parcutils::get_pairwise_corr_plot(res, samples =c("control" ,"treatment1"))
```
### Visualize all sample correlation by heat box
```{r, fig.height=8, fig.width=6}
parcutils::get_corr_heatbox(x = res, show_corr_values = T, cluster_samples = F)
```
### Visualize samples by Principle Component Analysis (PCA)
```{r, fig.width=6, fig.height=4}
parcutils::get_pca_plot(x = res,
samples =c("control" ,"treatment1" ,"treatment2"))
```
### Counts of diff expressed genes
```{r, fig.height=3, fig.width=6}
parcutils::get_diff_gene_count_barplot(x = res)
```
change color of the bars
```{r, fig.height=3, fig.width=6}
parcutils::get_diff_gene_count_barplot(x = res, col_down = "green4")
```
### Visualize differential expressed genes by volcano plot
```{r, fig.height=6, fig.width=5}
parcutils::get_volcano_plot(x = res, sample_comparison = "treatment2_VS_control",
col_up = "#a40000",
col_down = "#16317d",
repair_genes = T,
col_other = "grey")
# change cutoffs
parcutils::get_volcano_plot(x = res, repair_genes = T,
sample_comparison = "treatment2_VS_control",
pval_cutoff = 0.01,
log2fc_cutoff = 0.6,
col_up = "#a40000",
col_down = "#16317d",
col_other = "grey")
```
### Visualize gene expression distribution using box plot
```{r,fig.height=4, fig.width=5}
# all replicates
parcutils::get_gene_expression_box_plot(x = res,
samples =c("control" ,"treatment1"),
group_replicates = FALSE,
convert_log2 = T)
# summarise replicates
parcutils::get_gene_expression_box_plot(x = res,
samples =c("control" ,"treatment1"),
group_replicates = T,
convert_log2 = T)
```
### Visualize genes by heatmaps
```{r , fig.height=4, fig.width=5}
genes_for_hm = parcutils::get_genes_by_regulation(x = res,
sample_comparison = res$de_comparisons[[2]],
regulation = "both")
# heatmap of normalised gene expression values across samples
hm1 <- parcutils::get_gene_expression_heatmap(x = res,
samples = c("control","treatment1" , "treatment2") ,
genes = genes_for_hm %>% names() ,
convert_zscore = FALSE,
convert_log2 = T,
summarise_replicates = T,
name = "log2(value)" , color_default = F,
col =
circlize::colorRamp2(breaks = c(-5,0,15), colors = c("#16317d","white","#a40000")),
cluster_columns = FALSE)
ComplexHeatmap::draw(hm1)
# Visualise z-score and show all replicates.
hm2 <- parcutils::get_gene_expression_heatmap(x = res,
samples = c("control","treatment1") ,
name = "Z-score",
summarise_replicates = F,
col =
circlize::colorRamp2(breaks = c(-2,0,2), colors = c("#16317d","white","#a40000")),color_default = F,
genes = genes_for_hm %>% names() ,
convert_zscore = TRUE,
cluster_columns = FALSE)
ComplexHeatmap::draw(hm2)
# log2 FC heatamap
hm3 <- parcutils::get_fold_change_heatmap(x = res,
sample_comparisons = res$de_comparisons,
genes = genes_for_hm %>% names() ,
color_default = F,
col =
circlize::colorRamp2(breaks = c(-5,0,5), colors = c("#16317d","white","#a40000")),
name= "Log2FC")
ComplexHeatmap::draw(hm3)
```
### Visualize differential genes overlap between comparison
```{r}
us_plot <- parcutils::plot_deg_upsets(x = res,
sample_comparisons = res$de_comparisons)
us_plot$treatment1_VS_control_AND_treatment2_VS_control$upset_plot %>% print()
# get list of intersecting genes.
us_plot$treatment1_VS_control_AND_treatment2_VS_control$upset_intersects %>% print()
```
### Visualize common DE genes between comparison by scatter plot
```{r, fig.height=4, fig.width=5}
# show common up and down genes
parcutils::get_fold_change_scatter_plot(x = res,
sample_comparisons = res$de_comparisons, point_size = 3,label_size = 3,repair_genes = T)
# show common up and down genes
parcutils::get_fold_change_scatter_plot(x = res,
sample_comparisons = res$de_comparisons,
point_size = 3,
label_size = 3,
repair_genes = T)
# show common up genes
parcutils::get_fold_change_scatter_plot(x = res,
sample_comparisons = res$de_comparisons,
point_size = 5,
label_size = 4,
color_label = "both_up",
col_up = "red",
repair_genes = T)
# show common down genes
parcutils::get_fold_change_scatter_plot(x = res,
sample_comparisons = res$de_comparisons,
point_size = 5,
label_size = 4,
color_label = "both_down",
col_down = "green4",
repair_genes = T)
```
### Visualize genes by line plot
```{r, fig.width=5, fig.height=4}
genes_for_lineplot = parcutils::get_genes_by_regulation(x = res,
sample_comparison = res$de_comparisons[[2]],
regulation = "both") %>% names()
# line plot of gene expression values
parcutils::get_gene_expression_line_plot(x = res,
genes = genes_for_lineplot ,
samples = c("control","treatment1","treatment2"),summarise_replicates = T, show_average_line = T) +
ggplot2::theme(text = ggplot2::element_text(size = 15))
# line plot of gene expression values with k-means clustering
parcutils::get_gene_expression_line_plot(x = res,
km = 4,
genes = genes_for_lineplot ,
samples = c("control","treatment1","treatment2"),summarise_replicates = T, show_average_line = T) +
ggplot2::theme(text = ggplot2::element_text(size = 15))
# line plot of gene expression values with k-means clustering
parcutils::get_gene_expression_line_plot(x = res,
km = 4,
facet_clusters = T,
genes = genes_for_lineplot ,
samples = c("control","treatment1","treatment2"),summarise_replicates = T, show_average_line = T) +
ggplot2::theme(text = ggplot2::element_text(size = 15),
axis.text.x = ggplot2::element_text(angle = 40,hjust = 0.8))
# Fold change values
parcutils::get_fold_change_line_plot(x = res,
genes = genes_for_lineplot ,
line_transparency = 0.5,
km = 2,facet_clusters = T,
sample_comparisons = c("treatment1_VS_control", "treatment2_VS_control"),
average_line_summary_method = "mean",
show_average_line = T) +
ggplot2::theme(text = ggplot2::element_text(size = 15),
axis.text.x = ggplot2::element_text(angle = 40,hjust = 0.8))
```
## Perform gene ontology analysis and visualization of all UP/DOWN genes from all comparisons in one go.
```{r, fig.height=7, fig.width=7}
go_results <- parcutils::get_go_emap_plot(x = res)
# GO results as a table
go_results$go_enrichment_output
# GO results as an emap plot
go_results$go_emap_plots
```
## Show overlapping genes through VENN diagram
```{r}
parcutils::plot_deg_venn(res, sample_comparisons = res$de_comparisons,regulation = "up")
parcutils::plot_deg_venn(res, sample_comparisons = res$de_comparisons,regulation = "down")
parcutils::plot_deg_venn(res, sample_comparisons = res$de_comparisons,regulation = "both")
```
## Other functions
### Alignment summary
```{r , message=FALSE,warning=FALSE}
star_align_log_file <- system.file("extdata" , "a_Log.final.out" , package = "parcutils")
x = parcutils::get_star_align_log_summary(log_file = star_align_log_file)
print(x)
# plot alignment summary
star_align_log_file_dir <- system.file("extdata" , package = "parcutils")
star_align_log_files <- fs::dir_ls(star_align_log_file_dir,
glob = "*Log.final.out" ,
recurse = T,type = "file")
names(star_align_log_files) <- NULL
parcutils::get_star_align_log_summary_plot(x = star_align_log_files,
col_total_reads = "red",
col_mapped_reads = "blue")
```
##