#!/usr/bin/env Rscript
getwd()
setwd("~/git/LSHTM_analysis/scripts/plotting")
getwd()
source("~/git/LSHTM_analysis/scripts/Header_TT.R")

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)")
}

#===========
# Input
#===========
source("get_plotting_dfs.R")

#===========
# output
#===========
# PS
bp_subcols_duet = "barplot_coloured_PS.svg"
plot_bp_subcols_duet = paste0(plotdir, "/", bp_subcols_duet)

##############################################################################
# add frequency of positions (from lib data.table)

setDT(subcols_df_ps)[, pos_count := .N, by = .(position)]


foo = data.frame(subcols_df_ps$mutationinformation
                 , subcols_df_ps$position
                 , subcols_df_ps$pos_count)

#snpsBYpos_df <- subcols_df_ps %>%
#  group_by(position) %>%
#  summarize(snpsBYpos = mean(pos_count))


#********************
# generate plot: PS
# NO axis colours
#********************
g = ggplot(subcols_df_ps
           , aes(x = factor(position, ordered = T)))
g2 = g + geom_bar()

g2

foo = g2 + geom_text(stat='count', aes(label = ..count..))
foo

######
bp_subcols_duet = "TEST_PS.svg"
plot_bp_subcols_duet = paste0(plotdir, "/", bp_subcols_duet)
print(paste0("plot name:", plot_bp_subcols_duet))
svg(plot_bp_subcols_duet, width = 26, height = 4)

g1 = ggplot(subcols_df_ps, aes(x = factor(position, ordered = T)
                               , y = pos_count)) + 
  geom_bar(stat = "summary"
           , aes(fill = group), colour = "grey") +





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

g = ggplot(subcols_df_ps
           , aes(x = factor(position, ordered = T)))
outPlot_bp_ps = g + 
  geom_bar(aes(fill = group), colour = "grey") +
  scale_fill_manual( values = subcols_ps
                     , guide = "none") +
  theme( axis.text.x = element_text(size = my_xaxls
                                    , angle = 90
                                    , hjust = 1
                                    , vjust = 0.4)
         , axis.text.y = element_text(size = my_yaxls 
                                      , angle = 0
                                      , hjust = 1
                                      , vjust = 0)
         , axis.title.x = element_text(size = my_xaxts)
         , axis.title.y = element_text(size = my_yaxts ) ) +
  labs(title = ""
       #title = my_title
       , x = "Position"
       , y = "Frequency")

print(outPlot_bp_ps)
#dev.off()

######################################################################=
#                             End of script
######################################################################=

