---
title: "BOOScript"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
require(tidyverse)
require(MotilityLab)
require(magrittr)
require(tidyr)
require(stats)

```

Basic peace of code to show the amount of cells over time (MCS) in the model. The second graph takes the root of these cells, the third one the cubic root. We believe it is most comparable to take the square root and not the cubic root (initially because volume is cubic, as is our data.

We calculated this by getting the r out of the Volume (4/3*pi*r(1/3): volume of a sphere), and then using that r to calculate the surface area of a square area (pi*r^2: surface area)

```{r}
#PLOT AMOUNT OF CELLS/CTL
setwd("D:/BFW/BOO/TestRuns/BOOModel_/")
Logger<- read.csv(file ="logger.txt", sep="")
plot(Logger$time, Logger$celltype.cells.size, main="Amount of cells in model", xlab="Time in MCS", ylab="Amount of cells in model", col = "blue")
plot(Logger$time, Logger$celltype.cells.size, main="Amount of cells in model (log)", xlab="Time in MCS (Logarithmic)", ylab="Amount of cells in model", log = "x", col = "blue")
plot(Logger$time, Logger$celltype.CTL.size, main="Amount of CTL in model", xlab="Time in MCS", ylab="Amount of cells in model")

setwd("D:/BFW/BOO/TestRuns/BOOModel_12/")
Logger2<- read.csv(file ="logger.txt", sep="")
plot(Logger2$time, Logger2$celltype.cells.size, main="Amount of cells in model", xlab="Time in MCS", ylab="Amount of cells in model", col = "red")
plot(Logger2$time, Logger2$celltype.cells.size, main="Amount of cells in model (log)", xlab="Time in MCS (Logarithmic)", ylab="Amount of cells in model", log = "x", col = "red")

Logger <- as.data.frame(Logger)
Logger$SurfaceSize <- Logger$celltype.cells.size

Logger2 <- as.data.frame(Logger2)
Logger2$SurfaceSize <- Logger2$celltype.cells.size

#PLOT CELLS IN MM2 (PLUS LOG)
Logger$SurfaceSize <- sqrt(Logger$SurfaceSize)
plot(Logger$time, Logger$SurfaceSize, main="Amount of cell area (mm2)", xlab="Time in MCS", ylab="sqrt of n cells", col = "blue")
plot(Logger$time, Logger$SurfaceSize, main="Amount of cell area (mm2)(log)", xlab="Time in MCS (Logarithmic)", ylab="sqrt of n cells", log = "x", col = "blue")

Logger2$SurfaceSize <- sqrt(Logger2$SurfaceSize)
plot(Logger2$time, Logger2$SurfaceSize, main="Amount of cell area (mm2)(log)", xlab="Time in MCS", ylab="sqrt of n cells", col = "red")
plot(Logger2$time, Logger2$SurfaceSize, main="Amount of cell area (mm2)", xlab="Time in MCS (Logarithmic)", ylab="sqrt of n cells", log = "x", col = "red")

#PLOT CELLS IN MM3
Logger$VolumeSize <- Logger$celltype.cells.size
Logger$VolumeSize <- Logger$VolumeSize^(1/3)
plot(Logger$time, Logger$VolumeSize, main="Approximate amount of cell area (cubic, mm3)", xlab="Time in MCS", ylab="^(1/3) of n cells")
```

NEXT UP
Calculation and showing of CTL tracks, and thus migration speed and MSD, this can than be compared to the graphs we have from Weigelin et al.

```{r}
#CELL TRACKS, not sure how it is presented however, just for the sake of it.
data_tracks <- as.tracks.data.frame(Logger, id.column="cell.id", time.column = "time", pos.columns = c("cell.center.x", "cell.center.y"))
#plot(data_tracks, main ="cell tracks density 100")
#plot(data_tracks)

data_tracks2 <- as.tracks.data.frame(Logger2, id.column="cell.id", time.column = "time", pos.columns = c("cell.center.x", "cell.center.y"))
#plot(data_tracks, main ="cell tracks density 100")
#plot(data_tracks)

#celltracks <- as.tracks.data.frame(Logger, id.column = "cell.id", time.column = "time", pos.columns = c("cell.center.x", "cell.center.y"))
#plot(celltracks)

#MIGRATION SPEED
speed_CTL <- list()
for(i in seq_along(data_tracks)){
  speed_CTL[[i]] <- speed(data_tracks[[i]])
}

speed_CTL2 <- list()
for(i in seq_along(data_tracks2)){
  speed_CTL2[[i]] <- speed(data_tracks2[[i]])
}

#HERE THE ID's OF THE CTL CELLS (Define amount of tumor first, then calculate the amount of CTL's)
speed_cells_frame <- as.data.frame(speed_CTL[51:70])
speed_cells_frame_long <- gather(speed_cells_frame)
speed_cells_frame_long<- speed_cells_frame_long$value

boxplot(speed_cells_frame_long, main = "speed density 100")
?plot

speed_cells_frame2 <- as.data.frame(speed_CTL2[51:70])
speed_cells_frame_long2 <- gather(speed_cells_frame2)
speed_cells_frame_long2<- speed_cells_frame_long2$value

boxplot(speed_cells_frame_long2, main = "speed density 100")
?plot

#MEAN SQUARE DISPLACEMENT with CI 95%
# "mean.ci.95" Outputs the mean and upper and lower bound of a parametric 95 percent confidence intervall.
CTL_msqrd <- aggregate(data_tracks, squareDisplacement, FUN="mean.ci.95")
CTL_msqrd2 <- aggregate(data_tracks2, squareDisplacement, FUN="mean.ci.95")

# data set with ONLY i and the Mean Sqr displacement
CTL_msqrd_1 <- subset(CTL_msqrd, select = -c(lower, upper))
CTL_msqrd_2 <- subset(CTL_msqrd2, select = -c(lower, upper))

plot(CTL_msqrd_1, xlab="time step", 
  	ylab="mean square displacement", main="Mean Square Displacement", type="l", col="blue")
lines(CTL_msqrd_2, xlab="time step", 
  	ylab="mean square displacement", main="Mean Square Displacement", type="l", col="red")

plot(CTL_msqrd_2, xlab="time step", 
  	ylab="mean square displacement", main="Mean Square Displacement", type="l", col="red")
lines(CTL_msqrd_1, xlab="time step", 
  	ylab="mean square displacement", main="Mean Square Displacement", type="l", col="blue")
legend(1, 60000, legend=c("No treatment, control", "CD137 treatment"),
       col=c("red", "blue"), lty=1:2, cex=0.8)
```

```{r}
#AMOUNT OF CELLS KILLED
#The way this works is, we take the amount of cells proliferated, add the amount of cells spawned in with them. That is the total amount of cells we then COULD have in the model. But since CTL kill the cells, we take the total amount of cells in the model and calculate the difference: (All TC proliferation + initial TC amount) - Amount of cells in model
setwd("D:/BFW/BOO/MorpheusRuns/BOOModel_565/")
celldivision <- read.delim("cell_division_cells.txt")
AmountDivisions <- (nrow(celldivision)-1)
AmountDivisions

#MANUAL INPUT TWICE
TotalPossibleCells <- 50 + AmountDivisions
TotalCellsInModel <- tail(Logger$celltype.cells.size, n=1)
TotalCellsKilled <- TotalPossibleCells - TotalCellsInModel

TotalPossibleCells
TotalCellsInModel
TotalCellsKilled
celldivision

plot(Logger$time, Logger$celltype.cells.size)

mc.cores
```

