# R script to simulate networks in Braga et al. 2018 Nat. Comm.

# number of species in high and low diversity clades

highdiv <- 15
lowdiv <- 5

# Radiation scenario ----

Rnhosts <- 7

RC1 <- RC3 <- RC5 <- RC7 <- RC9 <- matrix(nrow = Rnhosts, ncol = highdiv)

RC2 <- RC4 <- RC6 <- RC8 <- RC10 <- matrix(nrow = Rnhosts, ncol = lowdiv)

Rhostlist <- list()
Rhostlist[1] <- 3
Rhostlist[3] <- 4
Rhostlist[5] <- 5
Rhostlist[7] <- 6
Rhostlist[9] <- 7
Rhostlist[[2]] <- Rhostlist[[4]] <- Rhostlist[[6]] <- Rhostlist[[8]] <- Rhostlist[[10]] <-c(1,2)


for(k in 1:length(Rhostlist)){
  ncols <- ncol(get(paste0("RC",k)))
  nrows <- nrow(get(paste0("RC",k)))
  RC <- matrix(nrow = nrows, ncol = ncols)
  
  for (j in 1:ncols){
    for (i in 1:nrows){
      if(i %in% Rhostlist[[k]]){
        RC[i,j] <- 1
      } else{
        RC[i,j] <- 0
      }
    }
  }
  assign(paste0("RC",k), RC)
}

Rmatrix <- cbind(RC1, RC2, RC3, RC4, RC5, RC6, RC7, RC8, RC9, RC10)



# Plasticity scenario ----

Pnhosts <- 40

PC1 <- PC3 <- PC5 <- PC7 <- PC9 <- matrix(nrow = Pnhosts, ncol = highdiv)

PC2 <- PC4 <- PC6 <- PC8 <- PC10 <- matrix(nrow = Pnhosts, ncol = lowdiv)

Phostlist <- list()

Phostlist[[1]] <- 1:10
Phostlist[[2]] <- sample.int(10, 2)
Phostlist[[3]] <- 1:20
Phostlist[[4]] <- sample.int(20, 2)
Phostlist[[5]] <- 1:30
Phostlist[[6]] <- sample.int(30, 2)
Phostlist[[7]] <- 1:40
Phostlist[[8]] <- sample.int(40, 2)
Phostlist[[9]] <- 1:40
Phostlist[[10]] <- sample.int(40, 2)

for(k in 1:length(Phostlist)){
  ncols <- ncol(get(paste0("PC",k)))
  nrows <- nrow(get(paste0("PC",k)))
  PC <- matrix(nrow = nrows, ncol = ncols)
  
  for (j in 1:ncols){
    for (i in 1:nrows){
      if(i %in% Phostlist[[k]]){
        PC[i,j] <- 1
      } else{
        PC[i,j] <- 0
      }
    }
  }
  assign(paste0("PC",k), PC)
}

Pmatrix_FHR <- cbind(PC1, PC2, PC3, PC4, PC5, PC6, PC7, PC8, PC9, PC10)

Pmatrix <- Pmatrix_FHR
sums <- colSums(Pmatrix_FHR)
prob <- 0.8
set.seed(2)

for(j in 1:ncol(Pmatrix)){
  if(sums[j] > 2){
    for(i in 1:nrow(Pmatrix)){
      if(Pmatrix[i,j] == 1){
        rand <- runif(1)
        if(rand < prob){
          if(sum(Pmatrix[,j]) > 1){
            Pmatrix[i,j] <- 0
          }
        }
      }
    }
  }
}



## Plasticity 1 Radiation 4 ----

Pnhosts <- 14

PC1 <- PC3 <- PC5 <- PC7 <- PC9 <- matrix(nrow = Pnhosts, ncol = highdiv)

PC2 <- PC4 <- PC6 <- PC8 <- PC10 <- matrix(nrow = Pnhosts, ncol = lowdiv)

PRhostlist <- list()

PRhostlist[[1]] <- 3
PRhostlist[[2]] <- c(1,2)
PRhostlist[[3]] <- 4
PRhostlist[[4]] <- c(1,2)
PRhostlist[[5]] <- 5
PRhostlist[[6]] <- c(1,2)
PRhostlist[[7]] <- 6
PRhostlist[[8]] <- c(1,2)
PRhostlist[[9]] <- c(1,2,7:14)
PRhostlist[[10]] <- sample(c(1,2,7:14), 2)

for(k in 1:length(PRhostlist)){
  ncols <- ncol(get(paste0("PC",k)))
  nrows <- nrow(get(paste0("PC",k)))
  PC <- matrix(nrow = nrows, ncol = ncols)
  
  for (j in 1:ncols){
    for (i in 1:nrows){
      if(i %in% PRhostlist[[k]]){
        PC[i,j] <- 1
      } else{
        PC[i,j] <- 0
      }
    }
  }
  assign(paste0("PC",k), PC)
}

P1R4matrix_FHR <- cbind(PC1, PC2, PC3, PC4, PC5, PC6, PC7, PC8, PC9, PC10)

P1R4matrix <- P1R4matrix_FHR
sums <- colSums(P1R4matrix_FHR)
prob <- 0.8
set.seed(2)
start <- 4*20+1  ### update with number of radiations

for(j in start:ncol(P1R4matrix)){
  if(sums[j] > 2){
    for(i in 1:nrow(P1R4matrix)){
      if(P1R4matrix[i,j] == 1){
        rand <- runif(1)
        if(rand < prob){
          if(sum(P1R4matrix[,j]) > 1){
            P1R4matrix[i,j] <- 0
          }
        }
      }
    }
  }
}



## Plasticity 2 Radiation 3 ----

Pnhosts <- 13

PC1 <- PC3 <- PC5 <- PC7 <- PC9 <- matrix(nrow = Pnhosts, ncol = highdiv)

PC2 <- PC4 <- PC6 <- PC8 <- PC10 <- matrix(nrow = Pnhosts, ncol = lowdiv)

PRhostlist <- list()

PRhostlist[[1]] <- 3
PRhostlist[[2]] <- c(1,2)
PRhostlist[[3]] <- 4
PRhostlist[[4]] <- c(1,2)
PRhostlist[[5]] <- 5
PRhostlist[[6]] <- c(1,2)
PRhostlist[[7]] <- c(1,2,6:13)
PRhostlist[[8]] <- sample(c(1,2,6:13), 2)
PRhostlist[[9]] <- c(1,2,6:13)
PRhostlist[[10]] <- sample(c(1,2,6:13), 2)

for(k in 1:length(PRhostlist)){
  ncols <- ncol(get(paste0("PC",k)))
  nrows <- nrow(get(paste0("PC",k)))
  PC <- matrix(nrow = nrows, ncol = ncols)
  
  for (j in 1:ncols){
    for (i in 1:nrows){
      if(i %in% PRhostlist[[k]]){
        PC[i,j] <- 1
      } else{
        PC[i,j] <- 0
      }
    }
  }
  assign(paste0("PC",k), PC)
}

P2R3matrix_FHR <- cbind(PC1, PC2, PC3, PC4, PC5, PC6, PC7, PC8, PC9, PC10)

P2R3matrix <- P2R3matrix_FHR
sums <- colSums(P2R3matrix_FHR)
prob <- 0.8
#set.seed(2)
start <- 3*20+1  ### update with number of radiations

for(j in start:ncol(P2R3matrix)){
  if(sums[j] > 2){
    for(i in 1:nrow(P2R3matrix)){
      if(P2R3matrix[i,j] == 1){
        rand <- runif(1)
        if(rand < prob){
          if(sum(P2R3matrix[,j]) > 1){
            P2R3matrix[i,j] <- 0
          }
        }
      }
    }
  }
}



## Plasticity 3 Radiation 2 ----

Pnhosts <- 22

PC1 <- PC3 <- PC5 <- PC7 <- PC9 <- matrix(nrow = Pnhosts, ncol = highdiv)

PC2 <- PC4 <- PC6 <- PC8 <- PC10 <- matrix(nrow = Pnhosts, ncol = lowdiv)

PRhostlist <- list()

PRhostlist[[1]] <- 3
PRhostlist[[2]] <- c(1,2)
PRhostlist[[3]] <- 4
PRhostlist[[4]] <- c(1,2)
PRhostlist[[5]] <- c(1,2,5:12)
PRhostlist[[6]] <- sample(c(1,2,5:12), 2)
PRhostlist[[7]] <- c(1,2,5:22)
PRhostlist[[8]] <- sample(c(1,2,5:22), 2)
PRhostlist[[9]] <- c(1,2,5:22)
PRhostlist[[10]] <- sample(c(1,2,5:22), 2) 

for(k in 1:length(PRhostlist)){
  ncols <- ncol(get(paste0("PC",k)))
  nrows <- nrow(get(paste0("PC",k)))
  PC <- matrix(nrow = nrows, ncol = ncols)
  
  for (j in 1:ncols){
    for (i in 1:nrows){
      if(i %in% PRhostlist[[k]]){
        PC[i,j] <- 1
      } else{
        PC[i,j] <- 0
      }
    }
  }
  assign(paste0("PC",k), PC)
}

P3R2matrix_FHR <- cbind(PC1, PC2, PC3, PC4, PC5, PC6, PC7, PC8, PC9, PC10)

P3R2matrix <- P3R2matrix_FHR
sums <- colSums(P3R2matrix_FHR)
prob <- 0.8
#set.seed(2)
start <- 2*20+1  ### update with number of radiations

for(j in start:ncol(P3R2matrix)){
  if(sums[j] > 2){
    for(i in 1:nrow(P3R2matrix)){
      if(P3R2matrix[i,j] == 1){
        rand <- runif(1)
        if(rand < prob){
          if(sum(P3R2matrix[,j]) > 1){
            P3R2matrix[i,j] <- 0
          }
        }
      }
    }
  }
}



## Plasticity 4 Radiation 1 ----

Pnhosts <- 31

PC1 <- PC3 <- PC5 <- PC7 <- PC9 <- matrix(nrow = Pnhosts, ncol = highdiv)

PC2 <- PC4 <- PC6 <- PC8 <- PC10 <- matrix(nrow = Pnhosts, ncol = lowdiv)

PRhostlist <- list()

PRhostlist[[1]] <- 3
PRhostlist[[2]] <- c(1,2)
PRhostlist[[3]] <- c(1,2,4:11)
PRhostlist[[4]] <- sample(c(1,2,4:11),2)
PRhostlist[[5]] <- c(1,2,4:21)
PRhostlist[[6]] <- sample(c(1,2,4:21), 2)
PRhostlist[[7]] <- c(1,2,4:31)
PRhostlist[[8]] <- sample(c(1,2,4:31), 2)
PRhostlist[[9]] <- c(1,2,4:31)
PRhostlist[[10]] <- sample(c(1,2,4:31), 2) 

for(k in 1:length(PRhostlist)){
  ncols <- ncol(get(paste0("PC",k)))
  nrows <- nrow(get(paste0("PC",k)))
  PC <- matrix(nrow = nrows, ncol = ncols)
  
  for (j in 1:ncols){
    for (i in 1:nrows){
      if(i %in% PRhostlist[[k]]){
        PC[i,j] <- 1
      } else{
        PC[i,j] <- 0
      }
    }
  }
  assign(paste0("PC",k), PC)
}

P4R1matrix_FHR <- cbind(PC1, PC2, PC3, PC4, PC5, PC6, PC7, PC8, PC9, PC10)

P4R1matrix <- P4R1matrix_FHR
sums <- colSums(P4R1matrix_FHR)
prob <- 0.8
#set.seed(2)
start <- 1*20+1  ### update with number of radiations

for(j in start:ncol(P4R1matrix)){
  if(sums[j] > 2){
    for(i in 1:nrow(P4R1matrix)){
      if(P4R1matrix[i,j] == 1){
        rand <- runif(1)
        if(rand < prob){
          if(sum(P4R1matrix[,j]) > 1){
            P4R1matrix[i,j] <- 0
          }
        }
      }
    }
  }
}



# Random network ----

nbut <- 100
nhosts <- 40
Null1_FHR <- matrix(nrow = nhosts, ncol = nbut)

for (j in 1:nbut){
  for (i in 1:nhosts){
    Null1_FHR[i,j] <- 1
  }
}


N1matrix <- Null1_FHR
sums <- colSums(Null1_FHR)
prob <- 0.8
set.seed(2)

for(j in 1:ncol(N1matrix)){
  if(sums[j] > 2){
    for(i in 1:nrow(N1matrix)){
      if(N1matrix[i,j] == 1){
        rand <- runif(1)
        if(rand < prob){
          if(sum(N1matrix[,j]) > 1){
            N1matrix[i,j] <- 0
          }
        }
      }
    }
  }
}



## Uniform evolution scenario ----

highdiv <- 15
lowdiv <- 5

nhosts <- 30

PC1 <- PC2 <- PC3 <- PC4 <- PC5 <- matrix(nrow = nhosts, ncol = highdiv + lowdiv)

PRhostlist <- list()

PRhostlist[[1]] <- 1:10
PRhostlist[[2]] <- c(1,2,11:18)
PRhostlist[[3]] <- c(1,2,11,12,19:24)
PRhostlist[[4]] <- c(1,2,11,12,19,20,25:28)
PRhostlist[[5]] <- c(1,2,11,12,19,20,25,26,29,30)

for(k in 1:length(PRhostlist)){
  ncols <- ncol(get(paste0("PC",k)))
  nrows <- nrow(get(paste0("PC",k)))
  PC <- matrix(nrow = nrows, ncol = ncols)
  
  for (j in 1:ncols){
    for (i in 1:nrows){
      if(i %in% PRhostlist[[k]]){
        PC[i,j] <- 1
      } else{
        PC[i,j] <- 0
      }
    }
  }
  assign(paste0("PC",k), PC)
}

Null2_FHR <- cbind(PC1, PC2, PC3, PC4, PC5)

N2matrix <- Null2_FHR
sums <- colSums(Null2_FHR)
prob <- 0.8
set.seed(5)

for(j in 1:ncol(N2matrix)){
  if(sums[j] > 2){
    for(i in 1:nrow(N2matrix)){
      if(N2matrix[i,j] == 1){
        rand <- runif(1)
        if(rand < prob){
          if(sum(N2matrix[,j]) > 1){
            N2matrix[i,j] <- 0
          }
        }
      }
    }
  }
}


## Tree ----

library(phytools)

s <- "((H1:1,L1:1):2,((H2:1,L2:1):1.5,((H3:1,L3:1):1,((H4:1,L4:1):0.5,(H5:1,L5:1):0.5):0.5):0.5):0.5);"
tree <- read.tree(text = s)
tip.label <- tree$tip.label
clade.label <- c("H1", "L1","H2", "L2","H3", "L3","H4", "L4","H5", "L5")
N <- rep(c(15,5),5)
depth <- rep(0.8, 10)
trans<-data.frame(tip.label,clade.label,N,depth)
backbone <- phylo.toBackbone(tree, trans)
plot(backbone, col = "black")

