#########################################################
# A) Installing and loading required packages
# B) My functions
#########################################################
check = function(x) tryCatch(if(class(x) == 'logical') 1 else 1, error = function(e) 0) 

#########################################################
#lib_loc = "/usr/local/lib/R/site-library")

require("getopt", quietly = TRUE) # cmd parse arguments

if (!require ("DT")){
  install.packages("DT")
  library(DT)
}

if (!require ("plyr")){
  install.packages("plyr")
  library(plyr)
}

if (!require("tidyverse")) {
  install.packages("tidyverse", dependencies = TRUE)
  library(tidyverse)
}

#---------------------------
# covered by tidyverse

# if (!require("ggplot2")) {
#   install.packages("ggplot2", dependencies = TRUE)
#   library(ggplot2)
# }

# if (!require ("dplyr")){
#    install.packages("dplyr")
#    library(dplyr)
#  }
#-----------------------------

if (!require("shiny")) {
  install.packages("shiny", dependencies = TRUE)
  library(shiny)
}

if (!require("shinyBS")) {
  install.packages("shinyBS", dependencies = TRUE)
  library(shinyBS)
}

if (!require("shinydashboard")) {
  install.packages("shinydashboard", dependencies = TRUE)
  library(shinydashboard)
}

if (!require("gridExtra")) {
  install.packages("gridExtra", dependencies = TRUE)
  library(gridExtra)
}

if (!require("ggridges")) {
  install.packages("ggridges", dependencies = TRUE)
  library(ggridges)
}

# Install
#if(!require(devtools)) install.packages("devtools")
#devtools::install_github("kassambara/ggcorrplot")

if (!require ("ggbeeswarm")){
   install.packages("ggbeeswarm")
   library(ggbeeswarm)
}

if (!require("plotly")) {
  install.packages("plotly", dependencies = TRUE)
  library(plotly)
}

if (!require("cowplot")) {
  install.packages("copwplot", dependencies = TRUE)
  library(cowplot)
}

if (!require("ggcorrplot")) {
  install.packages("ggcorrplot", dependencies = TRUE)
  library(ggcorrplot)
}

if (!require("ggpubr")) {
  install.packages("ggpubr", dependencies = TRUE)
  library(ggpubr)
}

if (!require("RColorBrewer")) {
  install.packages("RColorBrewer", dependencies = TRUE)
  library(RColorBrewer)
}

if (!require ("GOplot")) {
  install.packages("GOplot")
  library(GOplot)
}

if(!require("VennDiagram")) {
  install.packages("VennDiagram", dependencies = T)
  library(VennDiagram)
}

if(!require("scales")) {
  install.packages("scales", dependencies = T)
  library(scales)
}

if(!require("plotrix")) {
  install.packages("plotrix", dependencies = T)
  library(plotrix)
}

if(!require("stats")) {
  install.packages("stats", dependencies = T)
  library(stats)
}

if(!require("stats4")) {
  install.packages("stats4", dependencies = T)
  library(stats4)
}

if(!require("data.table")) {
install.packages("data.table")
  library(data.table)
}

if (!require("PerformanceAnalytics")){
  install.packages("PerformanceAnalytics", dependencies = T)
  library(PerformaceAnalytics)
}

if (!require ("GGally")){
  install.packages("GGally")
  library(GGally)
}

if (!require ("corrr")){
  install.packages("corrr")
  library(corrr)
}

if (!require ("psych")){
  install.packages("psych")
  library(psych)
}

if (!require ("compare")){
  install.packages("compare")
  library(compare)
}

if (!require ("arsenal")){
  install.packages("arsenal")
  library(arsenal)
}

if(!require(ggseqlogo)){
  install.packages("ggseqlogo")
  library(ggseqlogo)
}

# for PDB files
if(!require(bio3d)){
  install.packages("bio3d")
  library(bio3d)
}

library(protr)
if(!require(protr)){
  install.packages("protr")
  library(protr)
}

# if (!requireNamespace("BiocManager", quietly = TRUE))
#  install.packages("BiocManager")

#BiocManager::install("Logolas")
#library("Logolas")
library("Biostrings")

####################################
# Load all my functions:
# only works if tidyverse is loaded
# hence included it here!
####################################

func_path = "~/git/LSHTM_analysis/scripts/functions/"
source_files <- list.files(func_path, "\\.R$")  # locate all .R files
map(paste0(func_path, source_files), source)  # source all your R scripts!

# set plot script dir
plot_script_path = "~/git/LSHTM_analysis/scripts/plotting/"

####################################################
consurf_palette1 = c("0" = "yellow2"
                     , "1" = "cyan1"
                     , "2" = "steelblue2"
                     , "3" = "cadetblue2"
                     , "4" = "paleturquoise2"
                     , "5" = "thistle3"
                     , "6" = "thistle2"
                     , "7" = "plum2"
                     , "8" = "maroon"
                     , "9" = "violetred2")

consurf_palette2 =  c("0" = "yellow2"
                      , "1" = "forestgreen"
                      , "2" = "seagreen3"
                      , "3" = "palegreen1"
                      , "4" = "darkseagreen2"
                      , "5" = "thistle3"
                      , "6" = "lightpink1"
                      , "7" = "orchid3"
                      , "8" = "orchid4"
                      , "9" = "darkorchid4")

##################################################

# Function name clashes with plyr and dplyr
# # loading dplyr after plyr causes issues
# if("dplyr" %in% (.packages())){
#   detach("package:dplyr", unload=TRUE) 
#   detach("package:plyr", unload=TRUE) 
# } 
# library(plyr)
# library(dplyr)

# another solution is to requireNamespace() instead of library()
# so its function names don't collide with dplyr's.
