/
25_network_analysis.Rmd
279 lines (186 loc) · 5.06 KB
/
25_network_analysis.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
---
title: "Metabolic network analysis"
author: "Sergio Picart-Armada"
date: "8th March, 2020"
output:
html_document:
toc: TRUE
toc_float: true
code_folding: hide
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE,
error = FALSE, warning = FALSE,
fig.height = 4)
```
# Loading the data
```{r cars}
library(plyr)
library(dplyr)
library(tidyr)
library(magrittr)
library(ggplot2)
library(ggpmisc)
library(plotly)
library(psych)
library(data.table)
library(clusterProfiler)
# metabolomics
library(FELLA)
library(igraph)
config <- new.env()
source("config.R", local = config)
source("helpers.R")
theme_set(theme_bw())
dir.out <- config$dir.fella
if (!dir.exists(dir.out)) dir.create(dir.out)
set.seed(1)
```
FELLA database
```{r}
# save db in project root
fella.db <- config$fella.db
if (!dir.exists(fella.db)) {
g <- FELLA::buildGraphFromKEGGREST(organism = "mmu", filter.path = "01100")
FELLA::buildDataFromGraph(
g, databaseDir = fella.db,
internalDir = FALSE,
matrices = c("hypergeom", "diffusion"),
normality = c("hypergeom", "diffusion"),
niter = 1000)
}
fella.data <- FELLA::loadKEGGdata(fella.db, internalDir = FALSE, loadMatrix = "hypergeom")
```
```{r}
fc.ref <- config$fc.ref
q.ref <- config$q.ref
```
Load fold changes
```{r}
df.fc.trt <- read.csv(config$df.fc.trt)
```
# FELLA network analysis
## Network summary
Mouse network from KEGG
```{r}
fella.data
```
## Lists of metabolites
```{r}
cpd.young <- filter(df.fc.trt, Age == "Young" & se == "Metabolites" & signif)$db.id %>% unique %>% na.omit
cpd.old <- filter(df.fc.trt, Age == "Old" & se == "Metabolites" & signif)$db.id %>% unique %>% na.omit
cpd.both <- intersect(cpd.old, cpd.young)
cpd.bkgd <- filter(df.fc.trt, Age == "Old" & se == "Metabolites")$db.id %>% unique %>% na.omit
```
## Young
```{r}
enr.young <- FELLA::enrich(cpd.young, methods = "diffusion", data = fella.data)
g.young <- FELLA::generateResultsGraph(object = enr.young, data = fella.data)
g.young %>%
igraph_to_visnetwork()
```
Number of nodes
```{r}
vcount(g.young)
```
Entities in it
```{r}
tab.young <- generateResultsTable(object = enr.young, data = fella.data)
write.csv(tab.young, paste0(dir.out, "/subnetwork_all_young.csv"))
DT::datatable(tab.young)
```
Enzymes with gene annotations
```{r}
enz.young <- generateEnzymesTable(object = enr.young, data = fella.data)
write.csv(enz.young, paste0(dir.out, "/subnetwork_enzymes_young.csv"))
DT::datatable(enz.young)
```
## Old
```{r}
enr.old <- FELLA::enrich(cpd.old, methods = "diffusion", data = fella.data)
g.old <- FELLA::generateResultsGraph(object = enr.old, data = fella.data)
g.old %>%
igraph_to_visnetwork()
```
Number of nodes
```{r}
vcount(g.old)
```
Entities in it
```{r}
tab.old <- generateResultsTable(object = enr.old, data = fella.data)
write.csv(tab.old, paste0(dir.out, "/subnetwork_all_old.csv"))
DT::datatable(tab.old)
```
Enzymes with gene annotations
```{r}
enz.old <- generateEnzymesTable(object = enr.old, data = fella.data)
write.csv(enz.old, paste0(dir.out, "/subnetwork_enzymes_old.csv"))
DT::datatable(enz.old)
```
## Intersection old and young
```{r, fig.width=7, fig.height=7}
enr.both <- FELLA::enrich(cpd.both, methods = "diffusion", data = fella.data)
g.both <- FELLA::generateResultsGraph(object = enr.both, data = fella.data, LabelLengthAtPlot = 100)
g.both %>% igraph_to_visnetwork()
```
For exporting: really large plot (first right click and save PNG, then browse it)
```{r}
g.both %>% igraph_to_visnetwork(width = "3200px", height = "3200px")
```
Number of nodes
```{r}
vcount(g.both)
```
Entities in it
```{r}
tab.both <- generateResultsTable(object = enr.both, data = fella.data, LabelLengthAtPlot = 100)
write.csv(tab.both, paste0(dir.out, "/subnetwork_all_intersectoldyoung.csv"))
DT::datatable(tab.both)
```
Enzymes with gene annotations
```{r}
enz.both <- generateEnzymesTable(object = enr.both, data = fella.data)
write.csv(enz.both, paste0(dir.out, "/subnetwork_enzymes_intersectoldyoung.csv"))
DT::datatable(enz.both)
```
### Export plot with customised igraph plotting
Can we export to SVG?
```{r, fig.width=8, fig.height=8}
# reproducible layout?
set.seed(2)
g.both.plt <- igraph_add_graphical_params(g.both)
V(g.both.plt)[com %in% c(4)]$label <- ""
V(g.both.plt)$label.dist <- .7
V(g.both.plt)$label.cex <- .7
# V(g.both.plt)$shape <- "rectangle"
V(g.both.plt)$label.color %<>% paste0("FF")
V(g.both.plt)$color %<>% paste0("55")
pdf(paste0(dir.out, "/subnetwork_intersect.pdf"), width = 10, height = 10)
plot(g.both.plt)
dev.off()
```
### ggnet2
```{r}
# library(network)
# library(ggnet2)
# not in easybuild/cran
```
### NetworkD3
Not easy to export...
```{r}
d3.both.plt <- networkD3::igraph_to_networkD3(g.both.plt, group = V(g.both.plt)$com)
# Create force directed network plot
networkD3::forceNetwork(
Links = d3.both.plt$links, Nodes = d3.both.plt$nodes,
Source = 'source', Target = 'target', NodeID = 'name',
Group = 'group')
```
# Reproducibility
```{r}
date()
```
```{r}
sessionInfo()
```