########
# Name:
# CalculonResults
#
# Description:
# Creates a barplot with hardcoded information about the speed of the current
# 5GPM WGS pipeline used. This script was not added to the GitHub repository
# as it only generates a single plot and uses hard-coded values. The reason for
# using hard-coded values was because this plot was purely for basic comparison
# and the files containing the information with the needed times of the current
# pipeline would require building a tool that could correctly filter out these
# values (as these were not simple CSV files but files such multiple different
# log files) and manually adding them to this script was deemed a faster
# solution.
########


##################
### Libraries  ###
##################

# Loads in a needed library.
library(chron)


##################
### Functions  ###
##################

# Converts day values to hour values.
daysToHours <-  function(days) {
  return(days * 24)
}

# Capitalizes the firs character from a String.
capitalize <- function(string) {
  return(paste0(toupper(substr(string, 1, 1)), substr(string, 2, nchar(string))))
}

##################
###    Code    ###
##################

# Stores output directory for plots.
outputDir <- '~/Documents/stage/images/'

# Stores as number of days.
elapsedTimes <- list()
elapsedTimes$bwaAlign <- as.numeric(times(c("8:18:26", "10:44:31", "8:19:21", "9:31:59")))
elapsedTimes$samToBam <- as.numeric(times(c("2:48:10", "2:52:22", "2:37:03", "2:39:12")))
elapsedTimes$sortBam <- as.numeric(times(c("0:29:49", "0:37:37", "0:34:32", "0:33:11")))
elapsedTimes$mergeBam <- as.numeric(times("2:31:12"))
elapsedTimes.matrix <- matrix(c(rep(elapsedTimes[4][[1]],4), unlist(elapsedTimes[1:3])), ncol=4, byrow=TRUE,
                              dimnames = list(capitalize(names(elapsedTimes)[c(4,1:3)]), 1:4))
elapsedTimes.totalTime <- max(apply(elapsedTimes.matrix, 2, sum))

# Stores as number of days.
cpuTimes <- list()
cpuTimes$bwaAlign <- as.numeric(times(c("13:22:14", "16:54:53", "13:34:32", "13:17:18")))
cpuTimes$bwaAlign <- cpuTimes$bwaAlign + c(2,2,2,2) # add days
cpuTimes$samToBam <- as.numeric(times(c("2:47:47", "2:51:40", "2:34:49", "2:38:45")))
cpuTimes$sortBam <- as.numeric(times(c("4:05:49", "4:44:13", "4:58:47", "4:47:58")))
cpuTimes$mergeBam <- as.numeric(times("12:30:53"))
cpuTimes.matrix <- matrix(c(rep(cpuTimes[4][[1]],4), unlist(cpuTimes[1:3])), ncol=4, byrow=TRUE,
                              dimnames = list(capitalize(names(cpuTimes)[c(4,1:3)]), 1:4))
cpuTimes.totalTime <- max(apply(cpuTimes.matrix, 2, sum))

# Names of the different plot colors.
barNames <- capitalize(names(cpuTimes)[c(4,1:3)])
barNames[1] <- paste(barNames[1], "(not individual)")

# The different plot colors.
colorList <- c(colorRampPalette(c('darkblue', 'darkslategray1')) (n=4))
colors <- rep(colorList, 4)

# File to store plot in.
svg(paste0(outputDir, 'Rplot_current_pipeline.svg'), height=6, width=8)

# Generate plot.
par(mfrow=c(1,2), mar=c(9,4,4,4), xpd=FALSE)
barplot(daysToHours(elapsedTimes.matrix), col=colors, las=1, main="elapsed time", xlab="fastq file pair",
        ylab="time in hours", ylim=c(0,20), yaxt="n")
axis(2, at=seq(0,18,2), labels=seq(0,18,2), las=1)
axis(4, at=daysToHours(c(elapsedTimes$mergeBam, elapsedTimes.totalTime)), tick=FALSE, mgp=c(3,0.5,0),
            labels=round(daysToHours(c(elapsedTimes$mergeBam, elapsedTimes.totalTime)),2), las=1)
abline(daysToHours(elapsedTimes$mergeBam), 0, lty=1, lwd=3, col="black")
abline(daysToHours(elapsedTimes.totalTime), 0, lty=5, lwd=2, col="red")

barplot(cpuTimes.matrix, col=colors, las=1, main="CPU time", xlab="fastq file pair", ylab="time in days", ylim=c(0,4), yaxt="n")
axis(2, at=seq(0,4,0.5), labels=seq(0,4,0.5), las=1)
axis(4, at=c(cpuTimes$mergeBam, cpuTimes.totalTime), tick=FALSE, mgp=c(3,0.5,0),
     labels=round(c(cpuTimes$mergeBam, cpuTimes.totalTime),2), las=1)
abline(cpuTimes$mergeBam, 0, lty=1, lwd=3, col="black")
abline(cpuTimes.totalTime, 0, lty=5, lwd=2, col="red")

par(xpd=NA)
legend(-7,-1.1, fill=colorList, barNames, ncol=2)

# Write plot to file.
dev.off()
