#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Created on Tue Aug  6 12:56:03 2019

@author: tanu
'''
# FIXME: import dirs.py to get the basic dir paths available
#=======================================================================
# TASK: 
# 

# Input: 

# Output: 
#=======================================================================
#%% load libraries
import os, sys
import pandas as pd
#import numpy as np
#from varname import nameof
import argparse
#=======================================================================
#%% specify input and curr dir
homedir = os.path.expanduser('~')

# set working dir
os.getcwd()
os.chdir(homedir + '/git/LSHTM_analysis/scripts')
os.getcwd()

from aa_prop import get_aa_prop 
#=======================================================================
#%% command line args
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('-d', '--drug', help='drug name', default = None)
arg_parser.add_argument('-g', '--gene', help='gene name', default = None)

arg_parser.add_argument('--datadir', help = 'Data Directory. By default, it assmumes homedir + git/Data')
arg_parser.add_argument('-i', '--input_dir', help = 'Input dir containing pdb files. By default, it assmumes homedir + <drug> + input')
arg_parser.add_argument('-o', '--output_dir', help = 'Output dir for results. By default, it assmes homedir + <drug> + output')

arg_parser.add_argument('--debug', action ='store_true', help = 'Debug Mode') # not used atm

args = arg_parser.parse_args()
#%% variable assignment: input and output 
drug    = args.drug
gene    = args.gene
datadir = args.datadir
indir   = args.input_dir
outdir  = args.output_dir

#%%=======================================================================
#==============
# directories
#==============
if not datadir:
    datadir = homedir + '/' + 'git/Data'
    
if not indir:
    indir = datadir + '/' + drug + '/input'
    
if not outdir:
    outdir = datadir + '/' + drug + '/output'

#=======
# input
#=======
#in_filename  = 'merged_df3.csv'
#in_filename  = gene.lower() + '_complex_mcsm_norm.csv'
in_filename_mcsm  = gene.lower() + '_complex_mcsm_norm_SRY.csv' # gid
infile_mcsm = outdir + '/' + in_filename_mcsm
print('Input file: ', infile_mcsm
      , '\n============================================================')

#=======
# output 
#=======
out_filename_aaps = gene.lower() + '_mcsm_aaps.txt'
outfile_aaps =  outdir + '/' + out_filename_aaps
print('Output file: ', outfile_aaps
      , '\n============================================================')

#%% end of variable assignment for input and output files
#=======================================================================
#%% Read input files
print('Reading input file (merged file):', infile_mcsm)

comb_df = pd.read_csv(infile_mcsm, sep = ',')  

print('Input filename: ', in_filename_mcsm
      , '\nPath :', outdir
      , '\nNo. of rows: ', len(comb_df)
      , '\nNo. of cols: ', len(comb_df.columns)
      , '\n============================================================')

# column names
list(comb_df.columns)

#%% sanity check
nrows_df = len(comb_df)
ncols_add =  12
expected_cols = len(comb_df.columns) + ncols_add
print('\nAdding aa properties for wt and mutant: ', ncols_add, ' columns in total'
	, '\n===============================================================')

#%% call get_aa_prop():
get_aa_prop(df = comb_df)

#%% check dim of df
if len(comb_df) == nrows_df and len(comb_df.columns) == expected_cols:
    print('Checking dim of df: '
          , '\nPASS: df dim match'
          , '\nno.of rows: ', len(comb_df)
          , '\nno. of cols: ', len(comb_df.columns))
else:
    print('\FAIL: dim mismatch'
             , 'Expected rows: ', nrows_df
             , '\Got: ', len(comb_df)
             , '\nExpected cols: ', expected_cols
             , '\nGot: ', len(comb_df.columns))
    sys.exit('Aborting')
#%% summary output
sys.stdout = open(outfile_aaps, 'w')

print('################################################'
      , '\n aa properties: ', gene 
      , '\n################################################')

print('\n-------------------'
      , '\nWild-type: aap 1'
      , '\n-------------------\n'
      , comb_df['wt_aap1'].value_counts()
      , '\n-------------------'
      , '\nmutant-type: aap 1'
      , '\n-------------------\n'
      , comb_df['mut_aap1'].value_counts()
      , '\n===================================================')

print('\n-------------------'
      , '\nWild-type: aap 2'
      , '\n-------------------\n'
      , comb_df['wt_aap2'].value_counts()
      , '\n-------------------'
      , '\nmutant-type: aap 2'
      , '\n-------------------\n'
      , comb_df['mut_aap2'].value_counts()
      , '\n===================================================')

print('\n-------------------'
      , '\nWild-type: aap taylor'
      , '\n-------------------\n'
      , comb_df['wt_aap_taylor'].value_counts()
      , '\n-------------------'
      , '\nmutant-type: taylor'
      , '\n-------------------\n'
      , comb_df['mut_aap_taylor'].value_counts()
      , '\n===================================================')

print('\n-------------------'
      , '\nWild-type: aap water/kd'
      , '\n-------------------\n'
      , comb_df['wt_aap_kd'].value_counts()
      , '\n-------------------'
      , '\nmutant-type: water/kd'
      , '\n-------------------\n'
      , comb_df['mut_aap_kd'].value_counts()
      , '\n===================================================')

print('\n-------------------'
      , '\nWild-type: aap polarity'
      , '\n-------------------\n'
      , comb_df['wt_aap_polarity'].value_counts()
      , '\n-------------------'
      , '\nmutant-type: polarity'
      , '\n-------------------\n'
      , comb_df['mut_aap_polarity'].value_counts()
      , '\n===================================================')

print('\n-------------------'
      , '\nWild-type: aa calcprop'
      , '\n-------------------\n'
      , comb_df['wt_aa_calcprop'].value_counts()
      , '\n-------------------'
      , '\nmutant-type: aa calcprop'
      , '\n-------------------\n'
      , comb_df['mut_aa_calcprop'].value_counts()
      , '\n===================================================')

#%% end of script
#=======================================================================
