-
Notifications
You must be signed in to change notification settings - Fork 3
/
codon-example.rmd
122 lines (92 loc) · 3.09 KB
/
codon-example.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
```{r echo=FALSE, results='hide', warning=FALSE}
library = function (name)
suppressMessages(base::library(as.character(substitute(name)),
character.only = TRUE,
quietly = TRUE, warn.conflicts = FALSE))
library(dplyr)
library(pander)
library(knitr)
options(stringsAsFactors = FALSE)
transform_counts = function (counts, ...)
mutate_each_(counts, funs_(lazyeval::lazy_dots(...)), col_data$Sample)
summarize_each = summarise_each
panderOptions('table.split.table', Inf)
panderOptions('table.alignment.default',
function (df) ifelse(sapply(df, is.numeric), 'right', 'left'))
panderOptions('table.alignment.rownames', 'left')
# Enable automatic table reformatting.
opts_chunk$set(render = function (object, ...) {
if (pander_supported(object))
pander(object, style = 'rmarkdown')
else if (isS4(object))
show(object)
else
print(object)
})
pander_supported = function (object)
UseMethod('pander_supported')
pander_supported.default = function (object)
any(class(object) %in% sub('^pander\\.', '', methods('pander')))
pander.table = function (x, ...)
pander(`rownames<-`(rbind(x), NULL), ...)
# Helpers for dplyr tables
is.tbl_df = function (x)
inherits(x, 'tbl_df')
pander.tbl_df = function (x, ...)
pander(trunc_mat(x), ...)
# Copied from dplyr:::print.trunc_mat
pander.trunc_mat = function (x, ...) {
if (! is.null(x$table))
pander(x$table, ...)
if (length(x$extra) > 0) {
var_types = paste0(names(x$extra), ' (', x$extra, ')', collapse = ', ')
pander(dplyr:::wrap('Variables not shown: ', var_types))
}
}
# Disable code re-formatting, set caching and adjust figure output
opts_chunk$set(tidy = FALSE,
cache = TRUE,
dev = c('png', 'pdf'),
fig.path = 'knitr/codon/figure/',
cache.path = 'knitr/codon/cache/')
```
The data.
```{r}
data = c(𝐴 = 'AUG GAU UAC AAA GAC AAA GAC GAC AAA UAC AUG AAG GAC UGA',
𝐵 = 'AUG AAA UAC GAU AUG AAG GAU UAC AUG AAG UAC UAC UAC AAG UAC GAU UAC UGA')
data = strsplit(data, ' ')
# No stop codon!
levels = sort(setdiff(unique(unlist(data)), 'UGA'))
```
Codon usage.
```{r}
x = as.data.frame(sapply(sapply(data, factor, levels), table)) %>%
add_rownames('Codon') %>%
tbl_df()
(x = mutate(x, `𝐴 + 𝐵` = 𝐴 + 𝐵, µ = `𝐴 + 𝐵` / 2))
```
Codon usage with gene expression counts.
```{r}
(counts = list(𝐴 = 10, 𝐵 = 20))
xp = x %>% mutate_each(funs(. * counts$.), 2, 3) %>%
mutate(`𝐴 + 𝐵` = 𝐴 + 𝐵, µ = `𝐴 + 𝐵` / 2)
xp
```
Relative codon usage.
```{r}
genetic_code = data.frame(Codon = levels,
AA = c('Lys', 'Lys', 'Met', 'Asp', 'Asp', 'Tyr'))
m = inner_join(genetic_code, x, by = 'Codon') %>%
group_by(AA) %>%
mutate_each(funs(. / sum(.)), -Codon, -AA) %>%
ungroup()
m
```
Relative codon usage using expression counts.
```{r}
mp = inner_join(genetic_code, xp, by = 'Codon') %>%
group_by(AA) %>%
mutate_each(funs(. / sum(.)), -Codon, -AA) %>%
ungroup()
mp
```