-
Notifications
You must be signed in to change notification settings - Fork 10
/
Networks.Rmd
executable file
·119 lines (90 loc) · 3.16 KB
/
Networks.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
---
title: "Taxonomic network visualization"
author: "Leo Lahti, Sudarshan Shetty et al."
bibliography:
- bibliography.bib
output:
BiocStyle::html_document:
number_sections: no
toc: yes
toc_depth: 4
toc_float: true
self_contained: true
thumbnails: true
lightbox: true
gallery: true
use_bookdown: false
highlight: haddock
---
<!--
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{microbiome tutorial - networks}
%\usepackage[utf8]{inputenc}
%\VignetteEncoding{UTF-8}
-->
Load [example data](Data.html):
```{r networks1, message=FALSE, warning=FALSE}
library(microbiome)
data(dietswap)
pseq <- dietswap
# Keep only the prevalent taxa to speed up examples
pseq <- core(pseq, detection = 5^2, prevalence = 80/100)
pseq <- subset_samples(pseq, nationality == "AFR" & group == "DI" & bmi_group == "lean")
```
### Taxonomic network reconstruction
See the [phyloseq tutorial](http://joey711.github.io/phyloseq/plot_network-examples) for
additional network visualization tools.
The widely reported compositionality bias in similarity measures can
be fixed with SpiecEasi or SparCC; the implementations are available
via the [SpiecEasi package](https://github.com/zdk123/SpiecEasi). Note
that the execution is slow.
```{r networks4, warning=FALSE, message=FALSE, fig.width=10, fig.height=10}
# Pick the OTU table
library(phyloseq)
otu <- abundances(pseq)
```
```{r spieceasi, warning=FALSE, message=FALSE, fig.width=10, fig.height=10, eval=TRUE}
# SPIEC-EASI network reconstruction
# In practice, use more repetitions
library(devtools)
#install_github("zdk123/SpiecEasi")
library(SpiecEasi) #install_github("zdk123/SpiecEasi")
net <- spiec.easi(t(otu), method='mb', lambda.min.ratio=1e-2, nlambda=5, icov.select.params=list(rep.num=1))
## Create graph object
n <- net$refit
#colnames(n) <- rownames(n) <- rownames(otu)
# Network format
library(network)
#netw <- network(as.matrix(n), directed = FALSE)
# igraph format
library(igraph)
# ig <- graph.adjacency(n, mode='undirected', add.rownames = TRUE)
# Network layout
# coord <- layout.fruchterman.reingold(ig)
## set size of vertex to log2 mean abundance
# vsize <- log2(rowMeans(otu))
# Visualize the network
# print(plot(ig, layout = coord, vertex.size = vsize, vertex.label = names(vsize)))
```
Investigate degree distribution with the following:
```{r degree, warning=FALSE, message=FALSE, fig.width=10, fig.height=7, eval=FALSE}
#dd <- degree.distribution(ig)
#plot(0:(length(dd)-1), dd, ylim = c(0,.35), type = 'b', ylab = "Frequency", xlab = "Degree", main = "Degree Distributions")
```
Visualize the network with [ggnet2](https://briatte.github.io/ggnet):
```{r networks5, warning=FALSE, message=FALSE, fig.width=12, fig.height=7, eval = TRUE}
library(GGally)
#library(ggnet)
library(network)
library(sna)
library(ggplot2)
library(intergraph) # ggnet2 works also with igraph with this
phyla <- map_levels(rownames(otu),
from = "Genus", to = "Phylum",
tax_table(pseq))
#netw %v% "Phylum" <- phyla
#p <- ggnet2(netw, color = "Phylum", label = TRUE, label.size = 2)
```
```{r networks5_plot, warning=FALSE, message=FALSE, fig.width=12, fig.height=7, eval = TRUE}
print(p)
```