Skip to content

Commit 9807fbb

Browse files
committed
figure 5
1 parent 430587e commit 9807fbb

7 files changed

+844
-53
lines changed

Diff for: R/#4_Network.R#

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
library(RColorBrewer) # needed for some extra colours in one of the graphs
2+
library(vegan)
3+
library(phyloseq)
4+
library(igraph)
5+
library(Hmisc)
6+
library(Matrix)
7+
library(SpiecEasi)
8+
library(microbiome)
9+
10+
PS.TSS <- readRDS("tmp/PS.TSS_filtered.rds")
11+
12+
Bac <- subset_taxa(PS.TSS, Kingdom %in%"Bacteria")
13+
Parasite <- subset_taxa(PS.TSS, Genus %in%c("Eimeria", "Cryptosporidium", "Syphacia", "Aspiculuris", "Ascaridida", "Mastophorus","Trichuris", "Hymenolepis", "Tritrichomonas"))
14+
Parasite@tax_table[,1] <- "Parasite"
15+
Fungi <- subset_taxa(PS.TSS, Phylum %in% c("Mucoromycota", "Ascomycota", "Basidiomycota"))
16+
Fungi@tax_table[,1] <- "Fungi"
17+
18+
19+
Euk <- merge_phyloseq(Parasite, Fungi)
20+
Bac
21+
22+
23+
## prevalebce filtering of 5%
24+
KeepTaxap <- microbiome::prevalence(Bac)>0.05
25+
Bac <- phyloseq::prune_taxa(KeepTaxap, Bac)
26+
27+
KeepTaxap <- microbiome::prevalence(Euk)>0.05
28+
Euk <- phyloseq::prune_taxa(KeepTaxap, Euk)
29+
30+
#### spiec easi
31+
pargs <- list(rep.num=1000, seed=10010, ncores=90, thresh=0.05)
32+
## mb
33+
#t1 <- Sys.time()
34+
#se.net <- spiec.easi(list(Bac, Euk), method="mb", pulsar.params=pargs)
35+
#t2 <- Sys.time()
36+
#t2-t1
37+
#saveRDS(se.net, "tmp/se.fnet.rds")
38+
39+
se.net <- readRDS("tmp/se.fnet.rds")
40+
41+
se.net$select$stars$summary # lambda path
42+
43+
se.net$select
44+
45+
# coding bacteria/eukaryote nodes
46+
dtype <- c(rep(1,ntaxa(Bac)), rep(2,ntaxa(Euk)))
47+
48+
bac.ids=taxa_names(Bac)
49+
euk.ids= taxa_names(Euk)
50+
net.ids <- c(bac.ids,euk.ids)
51+
52+
# plotting
53+
bm=symBeta(getOptBeta(se.net), mode="maxabs")
54+
55+
diag(bm) <- 0
56+
57+
#weights <- Matrix::summary(t(bm))[,3] # includes negative weights
58+
weights <- (1-Matrix::summary(t(bm))[,3])/2 # ort
59+
60+
weights
61+
62+
net <- SpiecEasi::adj2igraph(Matrix::drop0(getRefit(se.net)),
63+
edge.attr=list(weight=weights),
64+
vertex.attr = list(name=net.ids))
65+
66+
E(net)
67+
68+
betaMat=as.matrix(symBeta(getOptBeta(se.net)))
69+
70+
# we want positive edges to be green and negative to be red
71+
edges <- E(net)
72+
edge.colors=c()
73+
for (e.index in 1:length(edges)){
74+
adj.nodes=ends(net, edges[e.index])
75+
xindex=which(net.ids==adj.nodes[1])
76+
yindex=which(net.ids==adj.nodes[2])
77+
beta=betaMat[xindex, yindex]
78+
if (beta>0){
79+
edge.colors=append(edge.colors, "#1B7837")
80+
}else if(beta<0){
81+
edge.colors=append(edge.colors, "#762A83")
82+
}
83+
}
84+
E(net)$color=edge.colors
85+
86+
### defining attributes
87+
V(net)$type=c(rep("Bacteria", length(taxa_names(Bac))), rep("Eukaryote", length(taxa_names(Euk))))
88+
V(net)$genus=c(Bac@tax_table[,6], Euk@tax_table[,6])
89+
V(net)$family=c(Bac@tax_table[,5], Euk@tax_table[,5])
90+
V(net)$phylum=c(Bac@tax_table[,2], Euk@tax_table[,2])
91+
V(net)$domain=c(Bac@tax_table[,1], Euk@tax_table[,1])
92+
93+
V(net)$stype <- c(rep("circle",ntaxa(Bac)), rep("square",ntaxa(Euk)))
94+
95+
bad<-V(net)[degree(net) == 0]
96+
97+
net <-delete.vertices(net, bad)
98+
99+
hub.s <- hub_score(net)$vector
100+
101+
V(net)$lab.hub <- ""
102+
103+
104+
V(net)$lab.hub <- V(net)$genus
105+
106+
V(net)$label.cex <- 0.5
107+
V(net)$label.dist <- 0
108+
109+
V(net)$label.degree <- pi/2
110+
111+
# we also want the node color to code for phylum
112+
nb.col <- length(levels(as.factor(V(net)$phylum)))
113+
coul <- colorRampPalette(brewer.pal(8, "Accent"))(nb.col)
114+
mc <- coul[as.numeric(as.factor(V(net)$phylum))]
115+
116+
117+
118+
pdf("fig/Network_prev05.pdf",
119+
width =10, height = 10)
120+
set.seed(1002)
121+
plot(net,
122+
layout=layout_with_fr(net),
123+
vertex.shape=V(net)$stype,
124+
vertex.label=V(net)$lab.hub,
125+
vertex.label.dist=0.4,
126+
vertex.label.degree=-pi/2,
127+
vertex.size=3,
128+
vertex.color=adjustcolor(mc,0.8),
129+
edge.width=as.integer(cut(E(net)$weight, breaks=6))/3,
130+
margin=c(0,1,0,0))
131+
legend(x=-2, y=1, legend=levels(as.factor(V(net)$phylum)), col=coul, bty="n",x.intersp=0.25,text.width=0.045, pch=20, pt.cex=1.5)
132+
dev.off()
133+
134+
135+
################### modularity analysis
136+
### modules with fast and greedy
137+
modules =cluster_fast_greedy(net, weights=E(net)$weight)
138+
modules.louvain <- cluster_louvain(net, weights=E(net)$weight)
139+
140+
modularity(modules)
141+
142+
sizes(modules)
143+
144+
V(net)$cluster=modules$membership
145+
146+
## should we plot this?
147+
nodes <- V(net)$name
148+
149+
cluster_id <- V(net)$cluster
150+
151+
gen <- paste(V(net)$domain, V(net)$genus, sep="__")
152+
153+
nodes<-as.data.frame(cbind(nodes, cluster_id, gen))
154+
155+
#nodes$cluster_id <- as.numeric(nodes$cluster_id)
156+
#
157+
#nodes$cluster_id[order(nodes$cluster_id)]
158+
159+
nodes[cluster_id==3,]
160+
161+
nodes <- nodes[nodes$cluster_id<46,]
162+
163+
nodes[grep("Fungi", nodes$gen),]
164+
165+
fam.cl <- as.data.frame(table(nodes$fam, nodes$cluster <- id))
166+
167+
##############some viz of cluster 2
168+
net10c <- delete <- edges(net10b, which(E(net10b)$color=="red"))
169+
V(net10c)$c2 <- ""
170+
V(net10c)$c2[which(V(net10c)$cluster==6)] <- V(net10c)$species[which(V(net10c)$cluster==6)]
171+
V(net10c)$c2
172+
V.cl2 <- V(net10b)$name[which(V(net10b)$cluster==6)]
173+
net.cl2 <- induced <- subgraph(net10b, V.cl2)
174+
nodes[cluster <- id==6,]
175+
degc <- igraph::degree(net.cl2, mode="all")

0 commit comments

Comments
 (0)