-
Notifications
You must be signed in to change notification settings - Fork 2
/
metab.Rmd
345 lines (292 loc) · 8.06 KB
/
metab.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
---
title: "Metabolites"
author: "Brian Yandell"
date: "`r format(Sys.time(), '%d %B %Y')`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
```
```{r}
library(tidyverse)
library(foundr)
```
# Trait names across metabolites
Strategy is to try to try to match up names when difference is only about capitalization. Then we look at 0-120 pairs for PlaMet to see how they are associated. In particular, consider the model
<center>
```
PlaMet120 ~ PlaMet0 + strain * sex * diet
PlaMet120 ~ PlaMet0 + signal + rest
```
<\center>
If PlaMet0 explains the signal at least, and ideally the rest,
then all the information about factor effects is there at the beginning and there are no GTT adjustments. If PlaMet0 remains significant after adjusting for signal and rest, then there is additional information in PlaMet0 beyond the experimental design.
Coding will involve adjustment to strainstats to allow for covariate.
Also consider what to do with LivMet. See stuff later in this document.
Goal also is to have beginning of document static and rest dynamic.
```{r}
traitSignal <- readRDS("traitSignal.rds") %>%
filter(dataset %in% c("LivMet", "PlaMet0", "PlaMet120"))
```
```{r}
trnames <- traitSignal %>%
mutate(Trait = as.character(trait),
trait = str_to_title(Trait)) %>%
select(dataset, trait, Trait) %>%
distinct(dataset, trait, Trait) %>%
group_by(dataset, trait) %>%
summarize(Trait = paste(Trait, collapse = "; "),
trait = trait[1],
.groups = "drop") %>%
ungroup() %>%
select(dataset, trait, Trait) %>%
arrange(trait)
```
```{r}
traitct <- traitSignal %>%
mutate(trait = str_to_title(trait)) %>%
count(dataset, trait) %>%
pivot_wider(names_from = "dataset", values_from = "n", values_fill = 0) %>%
mutate(status = ifelse(LivMet > 0 & PlaMet0 > 0 & PlaMet120 > 0,
"all", NA),
status = ifelse(is.na(status) & (PlaMet120 > 0 & PlaMet0 > 0),
"PlaMets", status),
status = ifelse(is.na(status) & (PlaMet120 > 0 & PlaMet0 == 0),
"PlaMet120", status),
status = ifelse(is.na(status) & (PlaMet0 > 0 & PlaMet120 == 0),
"PlaMet0", status))
```
```{r}
table(traitct$status)
```
Cytidine 192
2 N-Acetylneuraminate 192
3 Quinate 192
```{r}
LivMetData <- readRDS("LivMetData.rds") %>%
filter(tolower(trait) %in% c("cytidine", "quinate", "n-acetylneuraminate"))
```
```{r}
traitall <-
traitct %>%
filter(status %in% c("PlaMets","all")) %>%
select(status, trait) %>%
arrange(trait)
```
```{r}
traitpla <-
traitct %>%
filter(status %in% c("PlaMet0","PlaMet120")) %>%
select(status, trait) %>%
arrange(trait)
```
```{r}
traitjoin <- function(traitpla, trnames, dataname) {
trnames <- trnames %>%
filter(dataset == dataname) %>%
select(-dataset)
names(trnames)[match("Trait", names(trnames))] <-dataname
arrange(
select(
left_join(
traitpla,
trnames,
by = "trait"),
status, everything()),
trait)
}
```
```{r}
traitpla <-
traitjoin(
traitjoin(
traitjoin(
traitpla,
trnames,
"PlaMet0"),
trnames,
"PlaMet120"),
trnames,
"LivMet")
```
Following data frame has 0 rows since trait names forced into Title case (stringr::str_to_title() in R/Met.R).
```{r}
traitall <-
traitjoin(
traitjoin(
traitjoin(
traitall,
trnames,
"PlaMet0"),
trnames,
"PlaMet120"),
trnames,
"LivMet") %>%
filter(!(PlaMet0 == PlaMet120 & LivMet == PlaMet0))
```
```{r}
traitall <- bind_rows(
traitall,
traitpla)
```
```{r eval = FALSE}
write_csv(
traitall,
"PlaMetNames.csv"
)
```
# Compare two datasets
```{r}
datasets <- c("PlaMet0", "LivMet")
```
```{r}
traits <-
(traitct %>%
filter(.data[[datasets[1]]] > 0,
.data[[datasets[2]]] > 0))$trait
```
```{r}
traitData <- readRDS("traitData.rds") %>%
filter(dataset %in% datasets,
trait %in% traits) %>%
mutate(trait = factor(trait, traits))
traitSignal <- readRDS("traitSignal.rds") %>%
filter(dataset %in% datasets,
trait %in% traits) %>%
mutate(trait = factor(trait, traits))
traitStats <- readRDS("traitStats.rds") %>%
filter(dataset %in% datasets,
trait %in% traits) %>%
mutate(trait = factor(trait, traits))
```
```{r}
traitJoin <-
left_join(
traitData,
traitSignal,
by = c("dataset", "strain", "sex", "condition", "trait"))
```
# Correlation of values
```{r}
cortrait <- function(object, datasets) {
dplyr::ungroup(
dplyr::summarize(
dplyr::group_by(
tidyr::pivot_wider(
object,
names_from = "dataset", values_from = "value"),
trait),
value = stats::cor(
.data[[datasets[1]]],
.data[[datasets[2]]],
use = "pair"),
.groups = "drop"))
}
```
```{r}
cors <- bind_cols(
# Correlation of values
traitData %>%
cortrait(datasets),
# Correlation of cellmean
traitSignal %>%
select(-signal) %>%
rename(value = "cellmean") %>%
cortrait(datasets) %>%
select(-trait),
# Correlation of signal
traitSignal %>%
select(-cellmean) %>%
rename(value = "signal") %>%
cortrait(datasets) %>%
select(-trait),
# Correlation of rest
traitSignal %>%
mutate(value = cellmean - signal) %>%
select(-signal, -cellmean) %>%
cortrait(datasets) %>%
select(-trait),
# Correlation of individual
traitJoin %>%
mutate(value = value - cellmean) %>%
select(-signal, -cellmean) %>%
cortrait(datasets) %>%
select(-trait))
```
```{r}
ggplot(
cors %>%
pivot_longer(cellmean:noise,
names_to = "response", values_to = "cors") %>%
mutate(response = factor(response, c("cellmean","signal","ind_signal","noise")))) +
aes(individual, cors) +
facet_wrap(~ response) +
geom_point(shape = 1) +
geom_smooth(method = "lm", se = FALSE, formula = "y~x", col = "red") +
geom_abline(slope = 1, intercept = 0, col = "blue") +
ylab("extracted correlation") +
xlab("raw correlation")
```
```{r}
ggplot(cors) +
aes(ind_signal, signal) +
geom_point(shape = 1) +
geom_smooth(method = "lm", se = FALSE, formula = "y~x", col = "red") +
geom_abline(slope = 1, intercept = 0, col = "blue")
```
```{r}
ggplot(cors) +
aes(ind_signal, noise) +
geom_point(shape = 1) +
geom_smooth(method = "lm", se = FALSE, formula = "y~x", col = "red") +
geom_abline(slope = 1, intercept = 0, col = "blue")
```
# Correlation of Stats
```{r}
(stats <- traitStats %>%
mutate(p.value = -log10(p.value)) %>%
pivot_longer(SD:p.value, names_to = "stats", values_to = "value") %>%
pivot_wider(names_from = "dataset", values_from = "value"))
```
```{r}
CB_colors <- RColorBrewer::brewer.pal(n = 3, name = "Dark2")
c("#1B9E77", "#D95F02", "#7570B3")
```
```{r}
plot_stats <- function(stats, terms = unique(stats$term)) {
p1 <- ggplot(
stats %>%
filter(stats == "p.value",
term %in% terms) %>%
mutate(term = factor(term, terms))) +
aes(PlaMet, LivMet) +
facet_grid(stats ~ term, scale = "free") +
geom_point(shape = 1, col = CB_colors[3]) +
geom_smooth(method = "lm", se = FALSE, formula = "y~x", col = CB_colors[2]) +
geom_abline(slope = 1, intercept = 0, col = CB_colors[1]) +
xlab("")
p2 <- ggplot(
stats %>%
filter(stats == "SD",
term %in% terms) %>%
mutate(term = factor(term, terms))) +
aes(PlaMet, LivMet) +
facet_grid(stats ~ term, scale = "free") +
geom_point(shape = 1, col = CB_colors[3]) +
geom_smooth(method = "lm", se = FALSE, formula = "y~x", col = CB_colors[2]) +
geom_abline(slope = 1, intercept = 0, col = CB_colors[1])
cowplot::plot_grid(p1, p2, nrow = 2)
}
```
```{r}
plot_stats(stats, c("cellmean", "signal", "rest"))
```
```{r}
plot_stats(stats, c("signal", "strain_condition", "strain_sex_condition"))
```
```{r}
plot_stats(stats, c("strain", "sex", "strain_sex"))
```
```{r}
plot_stats(stats, c("condition", "sex_condition"))
```