-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisd_crete_network_analysis.R
executable file
·178 lines (147 loc) · 5.76 KB
/
isd_crete_network_analysis.R
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
#!/usr/bin/env Rscript
###############################################################################
# script name: isd_crete_network_analysis.R
# developed by: Savvas Paragkamian
# framework: ISD Crete
###############################################################################
# GOAL:
# Aim of this script is to use analyse the networks of the soil microbiome of
# Crete.
###############################################################################
# OUTPUT:
#
###############################################################################
# usage:./isd_crete_network_analysis.R
###############################################################################
source("scripts/functions.R")
library(igraph)
library(tibble)
library(readr)
library(magrittr)
library(tidyr)
library(dplyr)
library(ggplot2)
library(tidygraph)
library(ggraph)
library(pheatmap)
############################### load data #####################################
community_matrix <- read_delim("results/network_community_matrix.tsv", delim="\t")
network_taxa_metadata <- read_delim("results/network_taxa_metadata.tsv", delim="\t")# %>% dplyr::select(-reads_srs_sd)
graph <- igraph::read_graph("results/network_output.gml", format="gml")
#for (attrib in colnames(network_taxa_metadata)){
# print(attrib)
# graph <- graph |> set_vertex_attr(as.character(attrib),
# value=network_taxa_metadata[,attrib])
#
#}
E(graph)$sign <- ifelse(E(graph)$weight > 0, "positive", "negative")
E(graph)$color <- ifelse(E(graph)$sign == "positive", "green", "red")
taxa_cooccur <- cor(community_matrix, method="spearman")
isSymmetric(taxa_cooccur) # is true so we can remove the lower triangle
#taxa_cooccur[lower.tri(taxa_cooccur)] <- NA
taxa_cooccur_l <- dist_long(taxa_cooccur,"cooccurrence") %>%
filter(rowname!=colname) %>%
na.omit()
png("figures/network_heatmap_spearman_taxa.png",
res=300,
width=70,
height=30,
unit="cm")
pheatmap(taxa_cooccur,
color=colorRampPalette(c("white","skyblue", "palevioletred3"))(20))
dev.off()
############################### graph metrics #################################
is.connected(graph)
igraph::is.directed(graph)
vcount(graph)
is.weighted(graph)
edge_density(graph,loops = F)
reciprocity(graph)
###### only for positive values of weights
diameter(graph_positive)
average.path.length(graph_positive)
############################### subgraph metrics #######################
graph_motifs <- motifs(graph, 3)
########################### Centralities ###############################
V(graph)$degree <- degree(graph)
V(graph)$strength <- strength(graph)
V(graph)$betweenness <- igraph::betweenness(graph,weights = NA,directed = TRUE)
V(graph)$closeness_all <- igraph::closeness(graph,weights = NA, mode = "all")
V(graph)$transitivity_l <- igraph::transitivity(graph,type = "local",weights = NA)
V(graph)$transitivity_g <- igraph::transitivity(graph,type = "global",weights = NA)
graph_tbl <- graph |>
as_tbl_graph() |>
activate(nodes) |>
left_join(network_taxa_metadata, by=c("label"="asv_id")) |>
activate(edges) |>
mutate(weight_original = weight) |>
mutate(weight = abs(weight))
####################### clustering ###########################
graph_info <- cluster_infomap(graph_tbl)
graph_louvain <- cluster_louvain(graph_tbl)
V(graph_tbl)$louvain <- membership(graph_louvain)
############################## plot #####################################
png(file="figures/network_asv_sign.png",
width = 50,
height = 30,
res=300,
units = "cm",
bg="white")
plot(graph_tbl, layout=layout_in_circle,
vertex.label=NA,
vertex.size=V(graph_tbl)$degree/10,
edge.color=E(graph_tbl)$color,
edge.size=0.001,
vertex.frame.color=V(graph_tbl)$louvain,
vertex.color=V(graph_tbl)$louvain,
main = "Crete soil microbial interactome")
dev.off()
gg <- ggraph(graph_tbl, layout = 'fr', weights=abs(weight)) +
geom_edge_link(aes(color=color)) +
geom_node_point(mapping=aes(colour=Phylum, size=degree)) +
scale_edge_color_manual(values=c("red"="palevioletred3", "green"="darkolivegreen4"))+
theme(legend.position = 'bottom') +
theme_bw() +
theme(
# panel.background = element_rect(fill='transparent'), #transparent panel bg
panel.border = element_blank(),
plot.background = element_rect(fill='transparent', color=NA), #transparent plot bg
panel.grid.major = element_blank(), #remove major gridlines
panel.grid.minor = element_blank(), #remove minor gridlines
# legend.background = element_rect(fill='transparent'), #transparent legend bg
# legend.box.background = element_rect(fill='transparent'), #transparent legend panel
line = element_blank(),
axis.title=element_blank(),
axis.text=element_blank(),
legend.text=element_text(size=8),
legend.title = element_text(size=8),
legend.position = "bottom")
ggsave("figures/network_fr.png",
plot=gg,
device="png",
height = 50,
width = 53,
dpi = 300,
units="cm")
gg_circle <- ggraph(graph_tbl, circular=T) +
geom_edge_link(aes(color=color)) +
geom_node_point(mapping=aes(colour=Phylum)) +
scale_edge_color_manual(values=c("red"="palevioletred3", "green"="darkolivegreen4"))+
theme(legend.position = 'bottom') +
theme_bw()
ggsave("figures/network_circular.png",
plot=gg_circle,
device="png",
height = 50,
width = 53,
units="cm")
gg_matrix <- ggraph(graph, 'matrix') +
geom_edge_point(aes(colour = color, size=abs(weight)), mirror = TRUE) +
scale_edge_color_manual(values=c("red"="red", "green"="green"))+
theme(legend.position = 'bottom')
ggsave("figures/network_matrix.png",
plot=gg_matrix,
device="png",
height = 50,
width = 53,
units="cm")