#!/usr/bin/env Rscript

# working dir and loading libraries
getwd()
setwd("~/git/LSHTM_analysis/scripts/functions/")
getwd()

# infile_params = paste0(outdir, "/" , tolower(gene), "_comb_afor.csv")
# infile_metadata = paste0(outdir, "/", tolower(gene), "_metadata")
# 
# 
# source("combining_dfs_plotting_func.R")
# 
####################################################################
# in_file_params = "~/git/Data/streptomycin/output/gid_comb_afor.csv"
# in_file_metadata = "~/git/Data/streptomycin/output/gid_metadata.csv"
# 
# all_plot_dfs = combining_dfs_plotting(df1_mcsm_comb = infile_params
#                        , df2_gene_metadata = infile_metadata
#                        , lig_dist_colname = 'ligand_distance'
#                        , lig_dist_cutoff = 10)
# 
# merged_df2        = all_plot_dfs[[1]]
# merged_df3        = all_plot_dfs[[2]]
# merged_df2_comp   = all_plot_dfs[[3]]
# merged_df3_comp   = all_plot_dfs[[4]]
# merged_df2_lig    = all_plot_dfs[[5]]
# merged_df3_lig    = all_plot_dfs[[6]]
# 
# bar_colnames = data.frame(colnames(merged_df2))
###########################################################
source("plotting_globals.R")
source("plotting_data.R")
source("combining_dfs_plotting.R")

#---------------------
# call: import_dirs()
#---------------------
#gene = 'gid'
#drug = 'streptomycin'
#source("/home/tanu/git/LSHTM_analysis/config/gid.R")
#source("/home/tanu/git/LSHTM_analysis/config/alr.R")
#source("/home/tanu/git/LSHTM_analysis/config/katg.R")
source("/home/tanu/git/LSHTM_analysis/config/pnca.R")
#source("/home/tanu/git/LSHTM_analysis/config/rpob.R")
#source("/home/tanu/git/LSHTM_analysis/config/embb.R")



import_dirs(drug_name = drug, gene_name = gene)


#============================
# Input 1: plotting_data()
#============================
if (!exists("infile_params") && exists("gene")){
#if (!is.character(infile_params) && exists("gene")){
  #in_filename_params = paste0(tolower(gene), "_all_params.csv") 
  in_filename_params = paste0(tolower(gene), "_comb_afor.csv") # part combined for gid
  infile_params = paste0(outdir, "/", in_filename_params)
  cat("\nInput file for mcsm comb data not specified, assuming filename: ", infile_params, "\n")
}

mcsm_comb_data = read.csv(infile_params, header = T)

#-------------------------------
# call function: plotting_data()
#-------------------------------
pd_df = plotting_data(df = mcsm_comb_data
                      , lig_dist_colname = LigDist_colname
                      , lig_dist_cutoff = LigDist_cutoff)

my_df_u = pd_df[[2]] 

#======================================
# Input 2: read <gene>_meta data.csv
#======================================
if (!exists("infile_metadata") && exists("gene")){
#if (!is.character(infile_params) && exists("gene")){{
  in_filename_metadata = paste0(tolower(gene), "_metadata.csv") # part combined for gid
  infile_metadata = paste0(outdir, "/", in_filename_metadata)
  cat("\nInput file for gene metadata not specified, assuming filename: ", infile_metadata, "\n")
}

cat("\nReading meta data file:", infile_metadata)

gene_metadata <- read.csv(infile_metadata
                          , stringsAsFactors = F
                          , header = T)
                          
#-----------------------------------------
# test function: combining_dfs_plotting()
#-----------------------------------------
all_plot_dfs = combining_dfs_plotting(my_df_u
                       , gene_metadata
                       , lig_dist_colname = LigDist_colname
                       , lig_dist_cutoff = LigDist_cutoff)

merged_df2          = all_plot_dfs[[1]]
merged_df3          = all_plot_dfs[[2]]
merged_df2_comp     = all_plot_dfs[[3]]
merged_df3_comp     = all_plot_dfs[[4]]
merged_df2_lig      = all_plot_dfs[[5]]
merged_df3_lig      = all_plot_dfs[[6]]
merged_df2_comp_lig = all_plot_dfs[[7]]
merged_df3_comp_lig = all_plot_dfs[[8]]
########################################################################
#                           End of script
########################################################################
