0
|
1 ---
|
|
2 title: 'DESeq2 analysis'
|
|
3 output:
|
|
4 html_document:
|
|
5 number_sections: true
|
|
6 toc: true
|
|
7 theme: cosmo
|
|
8 highlight: tango
|
|
9 ---
|
|
10
|
|
11 ```{r setup, include=FALSE, warning=FALSE, message=FALSE}
|
|
12 knitr::opts_chunk$set(
|
|
13 echo = opt$echo,
|
|
14 error = TRUE
|
|
15 )
|
|
16 ```
|
|
17
|
|
18
|
1
|
19 # User input
|
0
|
20
|
|
21 ```{r 'user input'}
|
|
22 df = data.frame(name = names(opt)[-1],
|
|
23 value = unlist(opt))
|
1
|
24 datatable(df, rownames = FALSE)
|
0
|
25 ```
|
|
26
|
|
27
|
1
|
28 # Count Matrix
|
|
29
|
|
30 Display the first 100 rows of count data matrix.
|
0
|
31
|
|
32 ```{r 'count matrix'}
|
|
33 count_data = read.table(opt$count_data)
|
1
|
34 col_names = trimws(strsplit(opt$count_matrix_column_names, ',')[[1]])[1:ncol(count_data)]
|
|
35 col_names = col_names[!is.na(col_names)]
|
|
36 colnames(count_data)[1:length(col_names)] = col_names
|
|
37 datatable(head(count_data, 100))
|
|
38 ```
|
|
39
|
|
40 # Column Data
|
|
41
|
|
42 ```{r 'column data'}
|
|
43 col_data = read.table(opt$col_data,
|
|
44 stringsAsFactors = FALSE, sep=',', header = TRUE, row.names = 1)
|
|
45 datatable(col_data)
|
|
46 ```
|
|
47
|
|
48 # Match sample names
|
|
49
|
|
50 The goal of this step is to rearrange the rows of the column data matrix so that the samples rows in the count data matrix and the sample columns in the count data matrix are in the same order.
|
|
51
|
|
52 ```{r 'match sample names'}
|
2
|
53 col_data = col_data[col_names, , drop = FALSE]
|
1
|
54 datatable(col_data)
|
0
|
55 ```
|
|
56
|
1
|
57 # DESeqDataSet
|
|
58
|
|
59 ```{r 'DeseqDataSet'}
|
|
60 dds = DESeqDataSetFromMatrix(countData = count_data,
|
|
61 colData = col_data,
|
|
62 design = formula(opt$design_formula))
|
|
63 dds
|
|
64 ```
|
|
65
|
|
66 Pre-filter low count genes
|
0
|
67
|
1
|
68 ```{r 'pre-filtering'}
|
|
69 keep = rowSums(counts(dds)) >= 10
|
|
70 dds = dds[keep, ]
|
|
71 dds
|
|
72 ```
|
|
73
|
|
74 # Differential expression analysis
|
|
75
|
|
76 ```{r 'differential expression analysis'}
|
|
77 dds = DESeq(dds)
|
|
78 # res = results(dds, contrast = c(opt$contrast_condition, opt$treatment, opt$control))
|
|
79 res = results(dds)
|
|
80 resultsNames(dds)
|
|
81 if(nrow(res) > 500) {
|
|
82 cat('The result table has more than 500 rows. Only 500 rows are randomly selected to dispaly.')
|
|
83 datatable(as.data.frame(res)[sample(1:nrow(res), 500), ])
|
|
84 } else {
|
|
85 datatable(as.data.frame(res))
|
|
86 }
|
0
|
87 ```
|
|
88
|
|
89
|
1
|
90
|
|
91 ```{r 'write results into csv'}
|
|
92 #Write results into a CSV file.
|
|
93 write.csv(res, 'differential_genes.csv')
|
|
94 ```
|
|
95
|
|
96 # MAplot
|
|
97
|
|
98 ```{r}
|
|
99 plotMA(res)
|
|
100 ```
|
|
101
|
|
102
|
|
103 ```{r 'save R objects'}
|
|
104 # Save R objects to a file
|
|
105 save(dds, opt, file = 'deseq2.RData')
|
|
106 ```
|
|
107
|