##A toy tripartite network with intra-guild negative interactions,
##Inter-guild mutualistic interactions and inter-guild antagonistic interactions.
set.seed(12)
##4 a-nodes, 5 b-nodes, and 3 c-nodes
##Intra-guild interaction matrices
mat_aa<-matrix(runif(16,-0.8,-0.2),4,4)
mat_bb<-matrix(runif(25,-0.8,-0.2),5,5)
mat_cc<-matrix(runif(9,-0.8,-0.2),3,3)
##Inter-guild interaction matrices between a- and b-nodes.
mat_ab<-mat_ba<-matrix(sample(c(rep(0,8),runif(12,0,0.5))),4,5,byrow=TRUE)
mat_ba[mat_ba>0]<-runif(12,0,0.5);mat_ba<-t(mat_ba)
##Inter-guild interaction matrices between b- and c-nodes.
mat_cb<-mat_bc<-matrix(sample(c(rep(0,8),runif(7,0,0.5))),3,5,byrow=TRUE)
mat_bc[mat_bc>0]<-runif(7,0,0.5);mat_bc<--t(mat_bc)
mat<-rbind(cbind(mat_aa,mat_ab,matrix(0,4,3)),cbind(mat_ba,mat_bb,mat_bc))
mat<-rbind(mat,cbind(matrix(0,3,4),mat_cb,mat_cc))
##Set the node names
rownames(mat)<-c(paste0("a",1:4),paste0("b",1:5),paste0("c",1:3))
colnames(mat)<-c(paste0("a",1:4),paste0("b",1:5),paste0("c",1:3))
diag(mat)<--1 #assume -1 for diagonal elements
##Visualization of this block matrix.
library(plot.matrix)
pal <- colorRampPalette(c("darkblue", "lightblue", "white", "pink", "darkred"))(100)
par(mar=c(5,5,5,5));plot(mat,col = pal,
breaks = seq(-max(abs(mat)), max(abs(mat)), length.out = 101),
main = "Matrix visualization")
clip(x1 = 0.5,# Left boundary
x2 = ncol(mat) + 0.5, # Right boundary
y1 = 0.5, # Top boundary
y2 = nrow(mat) + 0.5 )
abline(v = c(4.5,9.5), h = c(3.5,8.5), lwd = 3, col = "black")
myguilds=c(rep("a",4),rep("b",5),rep("c",3))
ig_overlap_guild(mat,guilds=myguilds)
Run the code above in your browser using DataLab