#!/usr/bin/env Rscript       
getwd()
setwd("~/git/LSHTM_analysis/scripts/plotting/")
getwd()
#########################################################
# TASK: Basic lineage barplots showing numbers

# Output: Basic barplot with lineage samples and mut count
# + SNP diversity

##########################################################
# 				Installing and loading required packages 			 
##########################################################
source("~/git/LSHTM_analysis/scripts/Header_TT.R")

#===========
# input
#===========
#drug = 'streptomycin'
#gene = 'gid'

spec = matrix(c(
    "drug"       , "d",  1, "character",
    "gene"       , "g",  1, "character",
    "data_file1" , "fa", 2, "character",
    "data_file2" , "fb", 2, "character" 
), byrow = TRUE, ncol = 4)

opt = getopt(spec)

drug            = opt$drug
gene            = opt$gene
infile_params   = opt$data_file1
infile_metadata = opt$data_file2

if(is.null(drug)|is.null(gene)) {
    stop("Missing arguments: --drug and --gene must both be specified (case-sensitive)")
}

source ('get_plotting_dfs.R')

#=======
# output
#=======
# plot 1
basic_bp_lineage_cl = "basic_lineage_barplots_combined.svg"
plot_basic_bp_lineage_cl  =  paste0(plotdir,"/", basic_bp_lineage_cl)
plot_basic_bp_lineage_cl 
#################################################################
#=============================
# PLOT 1: Lineage count plot:
# LF data
#=============================
#------------------------
# Data: All lineages or
# selected few
#------------------------
lin_lf_plot = lin_lf[lin_lf$sel_lineages%in%c("L1", "L2", "L3", "L4"),]
str(lin_lf_plot)

# drop unused factor levels
lin_lf_plot$sel_lineages = factor(lin_lf_plot$sel_lineages)
levels(lin_lf_plot$sel_lineages)
str(lin_lf_plot)

#------------------------
# plot from my function:
#------------------------
lin_countP = lin_count_bp(lin_lf_plot
             , x_categ = "sel_lineages"
             , y_count = "p_count"
             , bar_fill_categ = "count_categ"
             , display_label_col = "p_count"
             , bar_stat_stype = "identity"
             , x_lab_angle = 90
             , my_xats = 20
             , bar_col_labels = c("Mutations", "Total Samples")
             , bar_col_values = c("grey50", "gray75")
             , y_scale_percent = F # T for diversity
             , y_log10 = F
             , y_label = "Count")
lin_countP
#================================
# PLOT 2: Lineage Diversity plot
# WF data
#================================
#------------------------
# Data: All lineages or
# selected few
#------------------------
lin_wf_plot = lin_wf[lin_wf$sel_lineages%in%c("L1", "L2", "L3", "L4"),]
str(lin_wf_plot)

# drop unused factor levels
lin_wf_plot$sel_lineages = factor(lin_wf_plot$sel_lineages)
levels(lin_wf_plot$sel_lineages)
str(lin_wf_plot)

#------------------------
# plot from my function:
#------------------------
lin_diversityP = lin_count_bp(lin_wf_plot
             , x_categ = "sel_lineages"
             , y_count = "snp_diversity"
             , display_label_col = "snp_diversity_f"
             , bar_stat_stype = "identity"
             , x_lab_angle = 90
             , my_xats = 20
             , y_scale_percent = T
             , y_label = "SNP diversity")

lin_diversityP
#########################################################################333
#================================
# Combine plots
#================================

svg(plot_basic_bp_lineage_cl , width = 8, height = 15 )

lineage_bp_combined = cowplot::plot_grid(lin_countP, lin_diversityP
                                      #, labels = c("(a)", "(b)", "(c)", "(d)")
                                      , nrow = 2
                                      , labels = "AUTO"
                                      , label_size = 25)

lineage_bp_combined
dev.off()
