########################################
#Spatial coexistence for three species #
#######################################
getwd()
## [1] "C:/Users/hp/OneDrive/Desktop/Winter 2021/MOD3"
setwd("C:/Users/hp/OneDrive/Desktop/Winter 2021/MOD3")
getwd()
## [1] "C:/Users/hp/OneDrive/Desktop/Winter 2021/MOD3"
## General outlook for local and global species interaction 
d <- read.csv("Project MOD1_Group5 cellular atomata-table.csv", skip = 6, header = T,
              stringsAsFactors = FALSE)
head(d)
##   X.run.number. r.cells.growth c.cells.growth s.cells.growth tox.effect
## 1             2           0.17           0.14            0.2        0.1
## 2             2           0.17           0.14            0.2        0.1
## 3             1           0.17           0.14            0.2        0.1
## 4             1           0.17           0.14            0.2        0.1
## 5             1           0.17           0.14            0.2        0.1
## 6             2           0.17           0.14            0.2        0.1
##      model.version X.step. count.s.cells count.c.cells count.r.cells
## 1 global neighbors       0           664           620           652
## 2 global neighbors       1           744           713           752
## 3  local neighbors       0           610           660           665
## 4  local neighbors       1           720           766           733
## 5  local neighbors       2           755           824           789
## 6 global neighbors       2           770           762           792
nrun <- max(d$X.run.number.)
ntime <- max(d$X.step.)

# local and global interaction 
par(mfrow = c(1,2))
i <- which(d$X.run.number. == 1, unique(d$model.version == "local neighbors"))
plot(x = d$X.step.[i], y = d$count.r.cells[i], type = "l", lwd = 2, las = 1,
     ylim = c(0,1500), xlim = c(0, 800), xlab = "time", ylab = "population size", col = "green")
lines(x = d$X.step.[i], y = d$count.s.cells[i], col =  "blue", lwd = 2)
lines(x = d$X.step.[i], y = d$count.c.cells[i], col =  "red", lwd = 2)
legend("topleft", col = c("green", "blue",  "red"), lwd = 2, legend = c("local r-cells", "local s-cells", "local c-cells"), bty = "n")
title(main = "local neighbors")



j <- which(d$X.run.number. == 2, unique(d$model.version == "global neighbors"))
plot(x = d$X.step.[j], y = d$count.r.cells[j], type = "l", lwd = 2, las = 1,
     ylim = c(0,2800), xlim = c(0, 800), xlab = "time", ylab = "population size", col = "green")
lines(x = d$X.step.[j], y = d$count.s.cells[j], col =  "blue", lwd = 2)
lines(x = d$X.step.[j], y = d$count.c.cells[j], col =  "red", lwd = 2)
legend("topleft", col = c("green", "blue",  "red"), lwd = 2, legend = c("local r-cells", "local s-cells", "local c-cells"), bty = "n")
title(main = "global neighbors")

#parameter sensitivity analysis

#Parameter values at local coexistence

parms <- c("r-cell" = 0.17, "s-cells" = 0.20, "c-cells" = 0.14, "tox" = 0.10)

#multiplication factors of -20% - +20%
f <- c(0.8, 0.9, 1, 1.1, 1.2)

a  <- parms[1] * f
a1 <- parms[2] * f
a2 <- parms[3] * f
a3 <- parms[4] * f

par(mfrow = c(2,2))

#r-cell growth effect 
r <- read.csv("Project MOD1_Group5 cellular atomat_r-table.csv", skip = 6, header = T,
               stringsAsFactors = FALSE)

num_of_run <- max(r$X.run.number.)



m <- which(r$X.run.number.==1)
plot(x = r$r.cells.growth[m], y = r$count.r.cells[m], type = "o", lwd = 2, las = 1, 
     ylim = c(0,2500), xlim = c(0.136, max(a)), xlab = "parameter", ylab = "population size", col = "green")
for(n in 1:num_of_run){
        m <- which(r$X.run.number. == n)
        lines(x = r$r.cells.growth[m], y = r$count.r.cells[m], type = "o", col = "green",lwd = 2)
}
for(n in 1:num_of_run){
        m <- which(r$X.run.number. == n)
        lines(x = r$r.cells.growth[m], y = r$count.s.cells[m], type = "l",col = "blue", lwd = 3)
}

for(n in 1:num_of_run){
        m <- which(r$X.run.number. == n)
        lines(x = r$r.cells.growth[m], y = r$count.c.cells[m], type = "l", col = "red", lwd = 2)
}
legend("topright", col = c("green", "blue",  "red"), lwd = 2, legend = c("local r-cells", "local s-cells", "local c-cells"), bty = "n")
title(main = "r-cell growth effect")



#s-cell growth effect 
s <- read.csv("Project MOD1_Group5 cellular atomat_s-table.csv", skip = 6, header = T,
              stringsAsFactors = FALSE)

num_of_run <- max(s$X.run.number.)



p <- which(s$X.run.number.==1)
plot(x = s$s.cells.growth[p], y = s$count.r.cells[p], type = "o", lwd = 2, las = 1, 
     ylim = c(0,2500), xlim = c(0.16, max(a1)), xlab = "parameter", ylab = "population size", col = "green")
for(q in 1:num_of_run){
        p <- which(s$X.run.number. == q)
        lines(x = s$s.cells.growth[p], y = s$count.r.cells[p], type = "o", col = "green",lwd = 2)
}
for(q in 1:num_of_run){
        p <- which(s$X.run.number. == q)
        lines(x = s$s.cells.growth[p], y = s$count.s.cells[p], type = "l",col = "blue", lwd = 3)
}

for(q in 1:num_of_run){
        p <- which(s$X.run.number. == q)
        lines(x = s$s.cells.growth[p], y = s$count.c.cells[p], type = "l", col = "red", lwd = 2)
}
legend("topright", col = c("green", "blue",  "red"), lwd = 2, legend = c("local r-cells", "local s-cells", "local c-cells"), bty = "n")
title(main = "s-cell growth effect")




#c-cell growth effect 
c <- read.csv("Project MOD1_Group5 cellular atomat_c-table.csv", skip = 6, header = T,
              stringsAsFactors = FALSE)

num_of_run <- max(c$X.run.number.)



u <- which(c$X.run.number.==1)
plot(x = c$c.cells.growth[u], y = c$count.r.cells[u], type = "o", lwd = 2, las = 1, 
     ylim = c(0,2500), xlim = c(0.112, max(a2)), xlab = "parameter", ylab = "population size", col = "green")
for(v in 1:num_of_run){
        u <- which(c$X.run.number. == v)
        lines(x = c$c.cells.growth[u], y = c$count.r.cells[u], type = "o", col = "green",lwd = 2)
}
for(v in 1:num_of_run){
        u <- which(c$X.run.number. == v)
        lines(x = c$c.cells.growth[u], y = c$count.s.cells[u], type = "l",col = "blue", lwd = 3)
}

for(v in 1:num_of_run){
        u <- which(c$X.run.number. == v)
        lines(x = c$c.cells.growth[u], y = c$count.c.cells[u], type = "l", col = "red", lwd = 2)
}
legend("topright", col = c("green", "blue",  "red"), lwd = 2, legend = c("local r-cells", "local s-cells", "local c-cells"), bty = "n")
title(main = "c-cell growth effect")


#toxicity effect 
tox <- read.csv("Project MOD1_Group5 cellular atomat_tox-table.csv", skip = 6, header = T,
              stringsAsFactors = FALSE)

num_of_run <- max(tox$X.run.number.)



y <- which(tox$X.run.number.==1)
plot(x = tox$tox.effect[y], y = tox$count.r.cells[y], type = "o", lwd = 2, las = 1, 
     ylim = c(0,2500), xlim = c(0.08, max(a3)), xlab = "parameter", ylab = "population size", col = "green")
for(z in 1:num_of_run){
        y <- which(tox$X.run.number. == z)
        lines(x = tox$tox.effect[y], y = tox$count.r.cells[y], type = "o", col = "green",lwd = 2)
}
for(z in 1:num_of_run){
        y <- which(tox$X.run.number. == z)
        lines(x = tox$tox.effect[y], y = tox$count.s.cells[y], type = "l",col = "blue", lwd = 3)
}

for(z in 1:num_of_run){
        y <- which(tox$X.run.number. == z)
        lines(x = tox$tox.effect[y], y = tox$count.c.cells[y], type = "l", col = "red", lwd = 2)
}
legend("topright", col = c("green", "blue",  "red"), lwd = 2, legend = c("local r-cells", "local s-cells", "local c-cells"), bty = "n")
title(main = "Toxicity effect")

#Parameter values at global coexistence

par(mfrow = c(2,2))

#r-cell growth effect 
r1 <- read.csv("Project MOD1_Group5 cellular atomata_r1-table.csv", skip = 6, header = T,
              stringsAsFactors = FALSE)

num_of_run <- max(r1$X.run.number.)



m1 <- which(r1$X.run.number.==1)
plot(x = r1$r.cells.growth[m1], y = r1$count.r.cells[m1], type = "o", lwd = 2, las = 1, 
     ylim = c(0,3000), xlim = c(0.136, max(a)), xlab = "parameter", ylab = "population size", col = "green")
for(n1 in 1:num_of_run){
        m1 <- which(r1$X.run.number. == n1)
        lines(x = r1$r.cells.growth[m1], y = r1$count.r.cells[m1], type = "o", col = "green",lwd = 2)
}
for(n1 in 1:num_of_run){
        m1 <- which(r1$X.run.number. == n1)
        lines(x = r1$r.cells.growth[m1], y = r1$count.s.cells[m1], type = "l",col = "blue", lwd = 3)
}

for(n1 in 1:num_of_run){
        m1 <- which(r1$X.run.number. == n1)
        lines(x = r1$r.cells.growth[m1], y = r1$count.c.cells[m1], type = "l", col = "red", lwd = 2)
}
legend("topright", col = c("green", "blue",  "red"), lwd = 2, legend = c("local r-cells", "local s-cells", "local c-cells"), bty = "n")
title(main = "r-cell growth effect")



#s-cell growth effect 
s1 <- read.csv("Project MOD1_Group5 cellular atomata_s1-table.csv", skip = 6, header = T,
              stringsAsFactors = FALSE)

num_of_run <- max(s1$X.run.number.)



p1 <- which(s1$X.run.number.==1)
plot(x = s1$s.cells.growth[p1], y = s1$count.r.cells[p1], type = "o", lwd = 2, las = 1, 
     ylim = c(0,3000), xlim = c(0.16, max(a1)), xlab = "parameter", ylab = "population size", col = "green")
for(q1 in 1:num_of_run){
        p1 <- which(s1$X.run.number. == q1)
        lines(x = s1$s.cells.growth[p1], y = s1$count.r.cells[p1], type = "o", col = "green",lwd = 2)
}
for(q1 in 1:num_of_run){
        p1 <- which(s1$X.run.number. == q1)
        lines(x = s1$s.cells.growth[p1], y = s1$count.s.cells[p1], type = "l",col = "blue", lwd = 3)
}

for(q1 in 1:num_of_run){
        p1 <- which(s1$X.run.number. == q1)
        lines(x = s1$s.cells.growth[p1], y = s1$count.c.cells[p1], type = "l", col = "red", lwd = 2)
}
legend("topright", col = c("green", "blue",  "red"), lwd = 2, legend = c("local r-cells", "local s-cells", "local c-cells"), bty = "n")
title(main = "s-cell growth effect")




#c-cell growth effect 
c1 <- read.csv("Project MOD1_Group5 cellular atomata_c1-table.csv", skip = 6, header = T,
              stringsAsFactors = FALSE)

num_of_run <- max(c1$X.run.number.)



u1 <- which(c1$X.run.number.==1)
plot(x = c1$c.cells.growth[u1], y = c1$count.r.cells[u1], type = "o", lwd = 2, las = 1, 
     ylim = c(0,3000), xlim = c(0.112, max(a2)), xlab = "parameter", ylab = "population size", col = "green")
for(v1 in 1:num_of_run){
        u1 <- which(c1$X.run.number. == v1)
        lines(x = c1$c.cells.growth[u1], y = c1$count.r.cells[u1], type = "o", col = "green",lwd = 2)
}
for(v1 in 1:num_of_run){
        u1 <- which(c1$X.run.number. == v1)
        lines(x = c1$c.cells.growth[u1], y = c1$count.s.cells[u1], type = "l",col = "blue", lwd = 3)
}

for(v1 in 1:num_of_run){
        u1 <- which(c1$X.run.number. == v1)
        lines(x = c1$c.cells.growth[u1], y = c1$count.c.cells[u1], type = "l", col = "red", lwd = 2)
}
legend("topright", col = c("green", "blue",  "red"), lwd = 2, legend = c("local r-cells", "local s-cells", "local c-cells"), bty = "n")
title(main = "c-cell growth effect")




#toxicity effect 
tox1 <- read.csv("Project MOD1_Group5 cellular atomata_tox1-table.csv", skip = 6, header = T,
                stringsAsFactors = FALSE)

num_of_run <- max(tox1$X.run.number.)



y1 <- which(tox1$X.run.number.==1)
plot(x = tox1$tox.effect[y1], y = tox1$count.r.cells[y1], type = "o", lwd = 2, las = 1, 
     ylim = c(0,3000), xlim = c(0.08, max(a3)), xlab = "parameter", ylab = "population size", col = "green")
for(z1 in 1:num_of_run){
        y1 <- which(tox1$X.run.number. == z1)
        lines(x = tox1$tox.effect[y1], y = tox1$count.r.cells[y1], type = "o", col = "green",lwd = 2)
}
for(z1 in 1:num_of_run){
        y1 <- which(tox1$X.run.number. == z1)
        lines(x = tox1$tox.effect[y1], y = tox1$count.s.cells[y1], type = "l",col = "blue", lwd = 3)
}

for(z1 in 1:num_of_run){
        y1 <- which(tox1$X.run.number. == z1)
        lines(x = tox1$tox.effect[y1], y = tox1$count.c.cells[y1], type = "l", col = "red", lwd = 2)
}
legend("topright", col = c("green", "blue",  "red"), lwd = 2, legend = c("local r-cells", "local s-cells", "local c-cells"), bty = "n")
title(main = "Toxicity effect")